Numbergen API reference#
Callable objects that generate numbers according to different distributions.
- class numbergen.BinaryOperator(lhs, rhs, operator, reverse=False, **args)[source]#
Bases:
NumberGeneratorApplies any binary operator to NumberGenerators or numbers to yield a NumberGenerator.
- Parameters:
lhs (NumberGenerator or Number) – The left-hand side operand, which can be a NumberGenerator or a number.
rhs (NumberGenerator or Number) – The right-hand side operand, which can be a NumberGenerator or a number.
operator (callable) – The binary operator to apply to the operands.
reverse (bool, optional) – If True, swaps the left and right operands. Defaults to False.
**args – Optional keyword arguments to pass to the operator when it is called.
Notes
It is currently not possible to set parameters in the superclass during initialization because **args is used by this class itself.
Parameter Definitions
- class numbergen.BoundedNumber(*, bounds, generator, name)[source]#
Bases:
NumberGeneratorFunction object that silently enforces numeric bounds on values returned by a callable object.
Parameter Definitions
generator = Callable(allow_None=True, label='Generator')Object to call to generate values.
bounds = Parameter(default=(None, None), label='Bounds')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,TimeDependentThe 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
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_fnnumbergen.TimeDependent: time_dependentonset = Number(default=0.0, inclusive_bounds=(True, True), label='Onset')Time of onset.
duration = Number(allow_None=True, bounds=(0.0, None), inclusive_bounds=(True, True), label='Duration')Duration of step value.
- class numbergen.Choice(*, choices, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
RandomDistributionReturn 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.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_dependent, time_fnnumbergen.TimeAwareRandomState: random_generatorchoices = List(bounds=(0, None), default=[0, 1], label='Choices')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,TimeDependentFunction 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.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_fnnumbergen.TimeDependent: time_dependentstarting_value = Number(default=1.0, inclusive_bounds=(True, True), label='Starting value')Value used for time zero.
ending_value = Number(default=0.0, inclusive_bounds=(True, True), label='Ending value')Value used for time infinity.
time_constant = Number(default=10000, inclusive_bounds=(True, True), label='Time constant')Time scale for the exponential; large values give slow decay.
base = Number(default=2.718281828459045, inclusive_bounds=(True, True), label='Base')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:
RandomDistributionNormally distributed (Gaussian) random number.
Specified with mean mu and standard deviation sigma. See the random module for further details.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_dependent, time_fnnumbergen.TimeAwareRandomState: random_generatormu = Number(default=0.0, inclusive_bounds=(True, True), label='Mu')Mean value.
sigma = Number(bounds=(0.0, None), default=1.0, inclusive_bounds=(True, True), label='Sigma')Standard deviation.
- class numbergen.NumberGenerator(*, name)[source]#
Bases:
ParameterizedAbstract 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.
Parameter Definitions
- class numbergen.RandomDistribution(*, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
NumberGenerator,TimeAwareRandomStateThe 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.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_dependent, time_fnnumbergen.TimeAwareRandomState: random_generatorseed = Integer(allow_None=True, inclusive_bounds=(True, True), label='Seed')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,TimeDependentThe current time multiplied by some conversion factor.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_fnnumbergen.TimeDependent: time_dependentfactor = Number(default=1.0, inclusive_bounds=(True, True), label='Factor')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,TimeDependentGenerate 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.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_fnnumbergen.TimeDependent: time_dependentonset = Number(default=0.0, inclusive_bounds=(True, True), label='Onset')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 = Number(bounds=(0.0, None), default=1.0, inclusive_bounds=(True, True), label='Duration')Duration of the ‘on’ state during which a value of 1.0 is returned.
off_duration = Number(allow_None=True, bounds=(0.0, None), inclusive_bounds=(True, True), label='Off duration')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,TimeDependentSamples the values supplied by a time_dependent callable at regular intervals of duration ‘period’, with the sampled value held constant within each interval.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_fnnumbergen.TimeDependent: time_dependentperiod = Number(bounds=(0.0, None), default=1.0, inclusive_bounds=(False, True), label='Period', softbounds=(0.0, 5.0))The periodicity with which the values of fn are sampled.
offset = Number(bounds=(0.0, None), default=0.0, inclusive_bounds=(True, True), label='Offset', 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 = Callable(allow_None=True, label='Fn')The time-dependent function used to generate the sampled values.
- class numbergen.UnaryOperator(operand, operator, **args)[source]#
Bases:
NumberGeneratorApplies any unary operator to a NumberGenerator to yield another NumberGenerator.
- Parameters:
operand (NumberGenerator) – The NumberGenerator to which the operator is applied.
operator (callable) – The unary operator to apply to the operand.
**args – Optional keyword arguments to pass to the operator when it is called.
Notes
It is currently not possible to set parameters in the superclass during initialization because **args is used by this class itself.
Parameter Definitions
- class numbergen.UniformRandom(*, lbound, ubound, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
RandomDistributionSpecified with lbound and ubound; when called, return a random number in the range [lbound, ubound).
See the random module for further details.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_dependent, time_fnnumbergen.TimeAwareRandomState: random_generatorlbound = Number(default=0.0, inclusive_bounds=(True, True), label='Lbound')Inclusive lower bound.
ubound = Number(default=1.0, inclusive_bounds=(True, True), label='Ubound')Exclusive upper bound.
- class numbergen.UniformRandomInt(*, lbound, ubound, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
RandomDistributionSpecified 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.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_dependent, time_fnnumbergen.TimeAwareRandomState: random_generatorlbound = Number(default=0, inclusive_bounds=(True, True), label='Lbound')Inclusive lower bound.
ubound = Number(default=1000, inclusive_bounds=(True, True), label='Ubound')Inclusive upper bound.
- class numbergen.UniformRandomOffset(*, mean, range, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
RandomDistributionIdentical 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.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_dependent, time_fnnumbergen.TimeAwareRandomState: random_generatormean = Number(default=0.0, inclusive_bounds=(True, True), label='Mean')Mean value
range = Number(bounds=(0.0, None), default=1.0, inclusive_bounds=(True, True), label='Range')Difference of maximum and minimum value
- class numbergen.VonMisesRandom(*, kappa, mu, seed, random_generator, time_dependent, time_fn, name)[source]#
Bases:
RandomDistributionCircularly 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.
Parameter Definitions
Parameters inherited from:
numbergen.TimeAware: time_dependent, time_fnnumbergen.TimeAwareRandomState: random_generatormu = Number(default=0.0, inclusive_bounds=(True, True), label='Mu', softbounds=(0.0, 6.283185307179586))Mean value, typically in the range 0 to 2*pi.
kappa = Number(bounds=(0.0, None), default=1.0, inclusive_bounds=(True, True), label='Kappa', softbounds=(0.0, 50.0))Concentration (inverse variance).