numbergen

Module

Callable objects that generate numbers according to different distributions.

class numbergen.NormalRandom(**params)[source]

Bases: numbergen.RandomDistribution

Normally distributed (Gaussian) random number.

Specified with mean mu and standard deviation sigma. See the random module for further details.

param Number mu (allow_None=False, bounds=None, constant=False, default=0.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Mean value.
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=False, default=False, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Whether the given time_fn should be used to constrain the results generated.
param Number sigma (allow_None=False, bounds=(0.0, None), constant=False, default=1.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Standard deviation.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.BinaryOperator(lhs, rhs, operator, reverse=False, **args)[source]

Bases: numbergen.NumberGenerator

Applies any binary operator to NumberGenerators or numbers to yield a NumberGenerator.

class numbergen.UniformRandom(**params)[source]

Bases: numbergen.RandomDistribution

Specified with lbound and ubound; when called, return a random number in the range [lbound, ubound).

See the random module for further details.

param Number ubound (allow_None=False, bounds=None, constant=False, default=1.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
exclusive upper bound
param Number lbound (allow_None=False, bounds=None, constant=False, default=0.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
inclusive lower bound
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=False, default=False, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Whether the given time_fn should be used to constrain the results generated.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.BoxCar(**params)[source]

Bases: numbergen.NumberGenerator, numbergen.TimeDependent

The boxcar function over the specified time interval. The bounds are exclusive: zero is returned at the onset time and at the offset (onset+duration).

If duration is None, then this reduces to a step function around the onset value with no offset.

See http://en.wikipedia.org/wiki/Boxcar_function

param Number onset (allow_None=False, bounds=None, constant=False, default=0.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Time of onset.
param Number duration (allow_None=True, bounds=(0.0, None), constant=False, default=None, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Duration of step value.
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=True, default=True, instantiate=False, pickle_default_value=True, precedence=None, readonly=True)
Read-only parameter that is always True.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.SquareWave(**params)[source]

Bases: numbergen.NumberGenerator, numbergen.TimeDependent

Generate a square wave with ‘on’ periods returning 1.0 and ‘off’periods returning 0.0 of specified duration(s). By default the portion of time spent in the high state matches the time spent in the low state (a duty cycle of 50%), but the duty cycle can be controlled if desired.

The ‘on’ state begins after a time specified by the ‘onset’ parameter. The onset duration supplied must be less than the off duration.

param Number onset (allow_None=False, bounds=None, constant=False, default=0.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Time of onset of the first ‘on’ state relative to time 0. Must be set to a value less than the ‘off_duration’ parameter.
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=True, default=True, instantiate=False, pickle_default_value=True, precedence=None, readonly=True)
Read-only parameter that is always True.
param Number duration (allow_None=False, bounds=(0.0, None), constant=False, default=1.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Duration of the ‘on’ state during which a value of 1.0 is returned.
param Number off_duration (allow_None=True, bounds=(0.0, None), constant=False, default=None, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Duration of the ‘off’ value state during which a value of 0.0 is returned. By default, this duration matches the value of the ‘duration’ parameter.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.ScaledTime(**params)[source]

Bases: numbergen.NumberGenerator, numbergen.TimeDependent

The current time multiplied by some conversion factor.

param Number factor (allow_None=False, bounds=None, constant=False, default=1.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
The factor to be multiplied by the current time value.
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=True, default=True, instantiate=False, pickle_default_value=True, precedence=None, readonly=True)
Read-only parameter that is always True.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.VonMisesRandom(**params)[source]

Bases: numbergen.RandomDistribution

Circularly normal distributed random number.

If kappa is zero, this distribution reduces to a uniform random angle over the range 0 to 2*pi. Otherwise, it is concentrated to a greater or lesser degree (determined by kappa) around the mean mu. For large kappa (narrow peaks), this distribution approaches the Gaussian (normal) distribution with variance 1/kappa. See the random module for further details.

param Number mu (allow_None=False, bounds=None, constant=False, default=0.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Mean value, typically in the range 0 to 2*pi.
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=False, default=False, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Whether the given time_fn should be used to constrain the results generated.
param Number kappa (allow_None=False, bounds=(0.0, None), constant=False, default=1.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Concentration (inverse variance).
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.Choice(**params)[source]

Bases: numbergen.RandomDistribution

Return a random element from the specified list of choices.

Accepts items of any type, though they are typically numbers. See the choice() function in the random module for further details.

param List choices (bounds=(0, None), constant=False, default=[0, 1], instantiate=True, pickle_default_value=True, precedence=None, readonly=False)
List of items from which to select.
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=False, default=False, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Whether the given time_fn should be used to constrain the results generated.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.UniformRandomInt(**params)[source]

Bases: numbergen.RandomDistribution

Specified with lbound and ubound; when called, return a random number in the inclusive range [lbound, ubound].

See the randint function in the random module for further details.

param Number ubound (allow_None=False, bounds=None, constant=False, default=1000, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
inclusive upper bound
param Number lbound (allow_None=False, bounds=None, constant=False, default=0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
inclusive lower bound
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=False, default=False, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Whether the given time_fn should be used to constrain the results generated.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.BoundedNumber(**params)[source]

Bases: numbergen.NumberGenerator

Function object that silently enforces numeric bounds on values returned by a callable object.

param Callable generator (constant=False, default=None, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Object to call to generate values.
param Parameter bounds (constant=False, default=(None, None), instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Legal range for the value returned, as a pair. The default bounds are (None,None), meaning there are actually no bounds. One or both bounds can be set by specifying a value. For instance, bounds=(None,10) means there is no lower bound, and an upper bound of 10.
class numbergen.ExponentialDecay(**params)[source]

Bases: numbergen.NumberGenerator, numbergen.TimeDependent

Function object that provides a value that decays according to an exponential function, based on a given time function.

Returns starting_value*base^(-time/time_constant).

See http://en.wikipedia.org/wiki/Exponential_decay.

param Number ending_value (allow_None=False, bounds=None, constant=False, default=0.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Value used for time infinity.
param Number base (allow_None=False, bounds=None, constant=False, default=2.71828182846, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Base of the exponent; the default yields starting_value*exp(-t/time_constant). Another popular choice of base is 2, which allows the time_constant to be interpreted as a half-life.
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=True, default=True, instantiate=False, pickle_default_value=True, precedence=None, readonly=True)
Read-only parameter that is always True.
param Number time_constant (allow_None=False, bounds=None, constant=False, default=10000, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Time scale for the exponential; large values give slow decay.
param Number starting_value (allow_None=False, bounds=None, constant=False, default=1.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Value used for time zero.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.UnaryOperator(operand, operator, **args)[source]

Bases: numbergen.NumberGenerator

Applies any unary operator to a NumberGenerator to yield another NumberGenerator.

class numbergen.RandomDistribution(**params)[source]

Bases: numbergen.NumberGenerator, numbergen.TimeAware

Python’s random module provides the Random class, which can be instantiated to give an object that can be asked to generate numbers from any of several different random distributions (e.g. uniform, Gaussian).

To make it easier to use these, Numbergen provides here a hierarchy of classes, each tied to a particular random distribution. RandomDistributions support setting parameters on creation rather than passing them each call, and allow pickling to work properly. Code that uses these classes will be independent of how many parameters are used by the underlying distribution, and can simply treat them as a generic source of random numbers.

The underlying random.Random() instance and all its methods can be accessed from the ‘random_generator’ attribute.

RandomDistributions are TimeAware, and thus can be locked to a global time if desired. By default, time_dependent=False, and so a new random value will be generated each time these objects are called. If you have a global time function, you can set time_dependent=True, so that the random values will instead be constant at any given time, changing only when the time changes. Using time_dependent values can help you obtain fully reproducible streams of random numbers, even if you e.g. move time forwards and backwards for testing.

If declared time_dependent, a hash is generated for seeding the random state on each call, using a triple consisting of the object name, the time returned by time_fn and the global value of param.random_seed. As a consequence, for a given name and fixed value of param.random_seed, the random values generated will be a fixed function of time.

If the object name has not been set and time_dependent is True, a message is generated warning that the default object name is dependent on the order of instantiation. To ensure that the random number stream will remain constant even if other objects are added or reordered in your file, supply a unique name explicitly when you construct the RandomDistribution object.

param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=False, default=False, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Whether the given time_fn should be used to constrain the results generated.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.TimeSampledFn(**params)[source]

Bases: numbergen.NumberGenerator, numbergen.TimeDependent

Samples the values supplied by a time_dependent callable at regular intervals of duration ‘period’, with the sampled value held constant within each interval.

param Number period (allow_None=False, bounds=(0.0, None), constant=False, default=1.0, inclusive_bounds=(False, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
The periodicity with which the values of fn are sampled.
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=True, default=True, instantiate=False, pickle_default_value=True, precedence=None, readonly=True)
Read-only parameter that is always True.
param Number offset (allow_None=False, bounds=(0.0, None), constant=False, default=0.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
The offset from time 0.0 at which the first sample will be drawn. Must be less than the value of period.
param Callable fn (constant=False, default=None, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
The time-dependent function used to generate the sampled values.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.
class numbergen.NumberGenerator(**params)[source]

Bases: param.parameterized.Parameterized

Abstract base class for any object that when called produces a number.

Primarily provides support for using NumberGenerators in simple arithmetic expressions, such as abs((x+y)/z), where x,y,z are NumberGenerators or numbers.

class numbergen.UniformRandomOffset(**params)[source]

Bases: numbergen.RandomDistribution

Identical to UniformRandom, but specified by mean and range. When called, return a random number in the range [mean - range/2, mean + range/2).

See the random module for further details.

param Number mean (allow_None=False, bounds=None, constant=False, default=0.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Mean value
param Number range (allow_None=False, bounds=(0.0, None), constant=False, default=1.0, inclusive_bounds=(True, True), instantiate=False, pickle_default_value=True, precedence=None, readonly=False, time_dependent=False, time_fn=<Time Time00001>)
Difference of maximum and minimum value
param Boolean time_dependent (allow_None=False, bounds=(0, 1), constant=False, default=False, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Whether the given time_fn should be used to constrain the results generated.
param Callable time_fn (constant=False, default=<Time Time00001>, instantiate=False, pickle_default_value=True, precedence=None, readonly=False)
Callable used to specify the time that determines the state and return value of the object, if time_dependent=True.