Numbergen API reference#
Callable objects that generate numbers according to different distributions.
- class numbergen.BinaryOperator(lhs, rhs, operator, reverse=False, **args)[source]#
Bases:
NumberGenerator
Applies any binary operator to NumberGenerators or numbers to yield a NumberGenerator.
- class numbergen.BoundedNumber(*, bounds, generator, name)[source]#
Bases:
NumberGenerator
Function object that silently enforces numeric bounds on values returned by a callable object.
generator
= param.Callable(allow_None=True, allow_refs=False, label=’Generator’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f4550520>)Object to call to generate values.
bounds
= param.Parameter(allow_refs=False, default=(None, None), label=’Bounds’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f4550100>)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.BoxCar(*, duration, onset, time_dependent, time_fn, name)[source]#
Bases:
NumberGenerator
,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
Parameters inherited from:
numbergen.TimeAware
: time_fnnumbergen.TimeDependent
: time_dependentonset
= param.Number(allow_refs=False, default=0.0, inclusive_bounds=(True, True), label=’Onset’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f42c9bb0>)Time of onset.
duration
= param.Number(allow_None=True, allow_refs=False, bounds=(0.0, None), inclusive_bounds=(True, True), label=’Duration’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f42c95e0>)Duration of step value.
- class numbergen.Choice(*, choices, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
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.
Parameters inherited from:
numbergen.TimeAware
: time_dependent, time_fnnumbergen.TimeAwareRandomState
: random_generatorchoices
= param.List(allow_refs=False, bounds=(0, None), default=[0, 1], label=’Choices’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f42c8fa0>)List of items from which to select.
- class numbergen.ExponentialDecay(*, base, ending_value, starting_value, time_constant, time_dependent, time_fn, name)[source]#
Bases:
NumberGenerator
,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.
Parameters inherited from:
numbergen.TimeAware
: time_fnnumbergen.TimeDependent
: time_dependentstarting_value
= param.Number(allow_refs=False, default=1.0, inclusive_bounds=(True, True), label=’Starting value’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f44f6df0>)Value used for time zero.
ending_value
= param.Number(allow_refs=False, default=0.0, inclusive_bounds=(True, True), label=’Ending value’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f432ae50>)Value used for time infinity.
time_constant
= param.Number(allow_refs=False, default=10000, inclusive_bounds=(True, True), label=’Time constant’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f4550ac0>)Time scale for the exponential; large values give slow decay.
base
= param.Number(allow_refs=False, default=2.718281828459045, inclusive_bounds=(True, True), label=’Base’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f432ae50>)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.
- class numbergen.NormalRandom(*, mu, sigma, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
RandomDistribution
Normally distributed (Gaussian) random number.
Specified with mean mu and standard deviation sigma. See the random module for further details.
Parameters inherited from:
numbergen.TimeAware
: time_dependent, time_fnnumbergen.TimeAwareRandomState
: random_generatormu
= param.Number(allow_refs=False, default=0.0, inclusive_bounds=(True, True), label=’Mu’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f45dd640>)Mean value.
sigma
= param.Number(allow_refs=False, bounds=(0.0, None), default=1.0, inclusive_bounds=(True, True), label=’Sigma’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f45ed0a0>)Standard deviation.
- class numbergen.NumberGenerator(*, name)[source]#
Bases:
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.RandomDistribution(*, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
NumberGenerator
,TimeAwareRandomState
The base class for all Numbergenerators using random state.
Numbergen provides a hierarchy of classes to make it easier to use the random distributions made available in Python’s random module, where each class is 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.
RandomDistributions are examples of TimeAwareRandomState, 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.
Note: Each RandomDistribution object has independent random state.
Parameters inherited from:
numbergen.TimeAware
: time_dependent, time_fnnumbergen.TimeAwareRandomState
: random_generatorseed
= param.Integer(allow_None=True, allow_refs=False, inclusive_bounds=(True, True), label=’Seed’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f462b7c0>)Sets the seed of the random number generator and can be used to randomize time dependent streams. If seed is None, there is no control over the random stream (i.e. no reproducibility of the stream).
- class numbergen.ScaledTime(*, factor, time_dependent, time_fn, name)[source]#
Bases:
NumberGenerator
,TimeDependent
The current time multiplied by some conversion factor.
Parameters inherited from:
numbergen.TimeAware
: time_fnnumbergen.TimeDependent
: time_dependentfactor
= param.Number(allow_refs=False, default=1.0, inclusive_bounds=(True, True), label=’Factor’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f459f250>)The factor to be multiplied by the current time value.
- class numbergen.SquareWave(*, duration, off_duration, onset, time_dependent, time_fn, name)[source]#
Bases:
NumberGenerator
,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.
Parameters inherited from:
numbergen.TimeAware
: time_fnnumbergen.TimeDependent
: time_dependentonset
= param.Number(allow_refs=False, default=0.0, inclusive_bounds=(True, True), label=’Onset’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f42de850>)Time of onset of the first ‘on’ state relative to time 0. Must be set to a value less than the ‘off_duration’ parameter.
duration
= param.Number(allow_refs=False, bounds=(0.0, None), default=1.0, inclusive_bounds=(True, True), label=’Duration’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f45bb400>)Duration of the ‘on’ state during which a value of 1.0 is returned.
off_duration
= param.Number(allow_None=True, allow_refs=False, bounds=(0.0, None), inclusive_bounds=(True, True), label=’Off duration’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f455ebe0>)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.
- class numbergen.TimeSampledFn(*, fn, offset, period, time_dependent, time_fn, name)[source]#
Bases:
NumberGenerator
,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.
Parameters inherited from:
numbergen.TimeAware
: time_fnnumbergen.TimeDependent
: time_dependentperiod
= param.Number(allow_refs=False, bounds=(0.0, None), default=1.0, inclusive_bounds=(False, True), label=’Period’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f42f3760>, softbounds=(0.0, 5.0))The periodicity with which the values of fn are sampled.
offset
= param.Number(allow_refs=False, bounds=(0.0, None), default=0.0, inclusive_bounds=(True, True), label=’Offset’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f42f3310>, softbounds=(0.0, 5.0))The offset from time 0.0 at which the first sample will be drawn. Must be less than the value of period.
fn
= param.Callable(allow_None=True, allow_refs=False, label=’Fn’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f42f3490>)The time-dependent function used to generate the sampled values.
- class numbergen.UnaryOperator(operand, operator, **args)[source]#
Bases:
NumberGenerator
Applies any unary operator to a NumberGenerator to yield another NumberGenerator.
- class numbergen.UniformRandom(*, lbound, ubound, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
RandomDistribution
Specified with lbound and ubound; when called, return a random number in the range [lbound, ubound).
See the random module for further details.
Parameters inherited from:
numbergen.TimeAware
: time_dependent, time_fnnumbergen.TimeAwareRandomState
: random_generatorlbound
= param.Number(allow_refs=False, default=0.0, inclusive_bounds=(True, True), label=’Lbound’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f45bb700>)Inclusive lower bound.
ubound
= param.Number(allow_refs=False, default=1.0, inclusive_bounds=(True, True), label=’Ubound’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f42e3b80>)Exclusive upper bound.
- class numbergen.UniformRandomInt(*, lbound, ubound, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
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.
Parameters inherited from:
numbergen.TimeAware
: time_dependent, time_fnnumbergen.TimeAwareRandomState
: random_generatorlbound
= param.Number(allow_refs=False, default=0, inclusive_bounds=(True, True), label=’Lbound’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f42d57f0>)Inclusive lower bound.
ubound
= param.Number(allow_refs=False, default=1000, inclusive_bounds=(True, True), label=’Ubound’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f42d55e0>)Inclusive upper bound.
- class numbergen.UniformRandomOffset(*, mean, range, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
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.
Parameters inherited from:
numbergen.TimeAware
: time_dependent, time_fnnumbergen.TimeAwareRandomState
: random_generatormean
= param.Number(allow_refs=False, default=0.0, inclusive_bounds=(True, True), label=’Mean’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f4560280>)Mean value
range
= param.Number(allow_refs=False, bounds=(0.0, None), default=1.0, inclusive_bounds=(True, True), label=’Range’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f47793a0>)Difference of maximum and minimum value
- class numbergen.VonMisesRandom(*, kappa, mu, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
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.
Parameters inherited from:
numbergen.TimeAware
: time_dependent, time_fnnumbergen.TimeAwareRandomState
: random_generatormu
= param.Number(allow_refs=False, default=0.0, inclusive_bounds=(True, True), label=’Mu’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f4560190>, softbounds=(0.0, 6.283185307179586))Mean value, typically in the range 0 to 2*pi.
kappa
= param.Number(allow_refs=False, bounds=(0.0, None), default=1.0, inclusive_bounds=(True, True), label=’Kappa’, nested_refs=False, rx=<param.reactive.reactive_ops object at 0x7f35f44e2250>, softbounds=(0.0, 50.0))Concentration (inverse variance).