poisson_generator#
- class brainpy.state.poisson_generator(in_size=1, rate=Quantity(0., 'Hz'), start=Quantity(0., 'ms'), stop=None, origin=Quantity(0., 'ms'), rng_seed=0, name=None)#
Poisson spike generator compatible with NEST.
Description
poisson_generatorre-implements NEST’s stimulation device of the same name and emits per-step spike multiplicities.1. Point-process model and discretization
Let
rbe the configured homogeneous rate in spikes/s and \(\Delta t\) be the simulation step in ms. For one output train, the count in one discrete bin is sampled as\[K_n \sim \mathrm{Poisson}(\lambda_n), \qquad \lambda_n = r \, \Delta t / 1000.\]The factor
1000converts milliseconds to seconds, so \(\lambda_n\) is dimensionless. This is the standard bin-count reduction of a homogeneous Poisson process where \(\mathbb{P}(K_n=k)=e^{-\lambda_n}\lambda_n^k/k!\).Implementation detail:
update()draws one vectorized Poisson sample withshape=self.varshapeviajax.random.poisson. Each element is an independent train; values are integer multiplicities0, 1, 2, ...and are not clipped to binary spikes.2. Activity window and NEST timing semantics
The active interval follows NEST
StimulationDevice::is_activefor spike generators:\[t_{\min} < t \le t_{\max}, \qquad t_{\min} = origin + start,\quad t_{\max} = origin + stop.\]Therefore
startis exclusive andstopis inclusive. Internally, times are projected to integer steps withround(time_ms / dt_ms)and activity is evaluated ast_min_step < curr_step <= t_max_step.3. Assumptions, constraints, and computational implications
Scalar parameters are converted to
float64in public units (Hz or ms). Ifdtis available, finiteorigin,start, andstopmust lie on the simulation grid (absolute tolerance1e-12intime/dtratio). Cache refresh is triggered whendtchanges. Per-step runtime is \(O(\prod \text{varshape})\) for sampling and memory is proportional to output size. Whenrate <= 0or inactive, the update path returns a zeroint64array without Poisson sampling.- Parameters:
in_size (
Size, optional) – Output size specification forbrainstate.nn.Dynamics. The derivedself.varshapeis the exact shape of arrays returned byupdate(). Each element corresponds to one independent output train. Default is1.rate (
ArrayLike, optional) – Scalar firing rate in spikes/s (Hz). Accepted forms are anyArrayLikewith exactly one element, optionally asaiunit.Quantityconvertible tou.Hz. Must satisfyrate >= 0. Default is0.0 * u.Hz.start (
ArrayLike, optional) – Scalar relative start time in ms (exclusive lower bound after addingorigin). Must be scalar-convertible tofloat64and, whendtis available, grid representable. Default is0.0 * u.ms.stop (
ArrayLikeorNone, optional) – Scalar relative stop time in ms (inclusive upper bound after addingorigin).Noneis mapped to+inf. If finite, must be scalar-convertible and grid representable whendtis available. Must satisfystop >= startafter conversion. Default isNone.origin (
ArrayLike, optional) – Scalar time origin offset in ms added to bothstartandstop. Must be scalar-convertible and grid representable whendtis available. Default is0.0 * u.ms.rng_seed (
int, optional) – Seed used to initializejax.random.PRNGKeyinsideinit_state(). Different seeds lead to different stochastic realizations for otherwise identical parameters. Default is0.
Parameter Mapping
Table 31 Parameter mapping to model symbols# Parameter
Default
Math symbol
Semantics
rate0.0 * u.Hz\(r\)
Homogeneous firing rate in spikes/s.
start0.0 * u.ms\(t_{\mathrm{start,rel}}\)
Relative exclusive lower bound of activity.
stopNone\(t_{\mathrm{stop,rel}}\)
Relative inclusive upper bound;
Nonemaps to+\infty.origin0.0 * u.ms\(t_0\)
Global offset added to
startandstop.in_size1Defines
self.varshape(number/shape of independent trains).rng_seed0Seed for JAX key state used by Poisson sampling.
- Raises:
ValueError – If
rate < 0; ifstop < start; if time/rate inputs are not scalar-convertible; or if finiteorigin/start/stopare not multiples of simulation resolution whendtis available.TypeError – If unit conversion to
u.Hzoru.msfails for supplied inputs.KeyError – At runtime, if required simulation context entries (for example
dtviabrainstate.environ.get_dt()) are missing.
Notes
updatelazily initializes RNG state ifinit_state()has not been called explicitly.Parameter updates through
set()recompute cached step bounds whendtis present in the environment.As in NEST, one generator can fan out to many targets while maintaining independent trains per output element.
Examples
>>> import brainpy >>> import brainstate >>> import saiunit as u >>> with brainstate.environ.context(dt=0.1 * u.ms): ... gen = brainpy.state.poisson_generator( ... in_size=(2, 3), ... rate=1200.0 * u.Hz, ... start=5.0 * u.ms, ... stop=20.0 * u.ms, ... rng_seed=11, ... ) ... with brainstate.environ.context(t=10.0 * u.ms): ... counts = gen.update() ... _ = counts.shape
>>> import brainpy >>> import saiunit as u >>> gen = brainpy.state.poisson_generator(rate=500.0 * u.Hz) >>> gen.set(start=2.0 * u.ms, stop=None, origin=1.0 * u.ms) >>> params = gen.get() >>> _ = params['rate'], params['stop']
See also
poisson_generator_psPrecise-time Poisson generator with dead time.
inhomogeneous_poisson_generatorPiecewise-constant time-varying Poisson rate.
sinusoidal_poisson_generatorSinusoidally modulated Poisson rate.
References
- get()[source]#
Return current public parameters as scalar SI-compatible values.
- Returns:
params – Dictionary with four
floatentries:'rate'– firing rate in spikes/s (Hz).'start'– relative exclusive lower bound in ms.'stop'– relative inclusive upper bound in ms;infwhen no deactivation time has been set.'origin'– time origin offset in ms.
- Return type:
Notes
Returned values are plain Python
floatscalars (float64precision). They mirror the internal scalar attributes set in__init__()or updated byset()and are not bound to anysaiunitquantities.See also
poisson_generator.setUpdate one or more parameters in place.
Examples
>>> import brainpy >>> import saiunit as u >>> gen = brainpy.state.poisson_generator( ... rate=800.0 * u.Hz, ... start=5.0 * u.ms, ... stop=100.0 * u.ms, ... origin=2.0 * u.ms, ... ) >>> params = gen.get() >>> params['rate'] 800.0 >>> params['stop'] 100.0
- init_state(batch_size=None, **kwargs)[source]#
Initialize the RNG state used by Poisson sampling.
- Parameters:
Notes
update()lazily calls this method on the first step ifinit_statehas not been invoked explicitly. Callinginit_stateresets the RNG to the original seed, so repeated calls restart the stochastic sequence from the beginning.See also
poisson_generator.updateConsumes
rng_keypopulated here.
Examples
>>> import brainstate >>> import saiunit as u >>> from brainpy.state import poisson_generator >>> with brainstate.environ.context(dt=0.1 * u.ms): ... gen = poisson_generator(in_size=4, rate=800.0 * u.Hz, rng_seed=7) ... gen.init_state()
- set(*, rate=<object object>, start=<object object>, stop=<object object>, origin=<object object>)[source]#
Update public parameters and refresh the timing cache when needed.
Only keyword arguments that are explicitly passed are modified; omitted arguments retain their current values.
- Parameters:
rate (
ArrayLikeorobject, optional) – New scalar firing rate in spikes/s (Hz). Accepts anyArrayLikewith exactly one element, or asaiunit.Quantityconvertible tou.Hz. Must satisfyrate >= 0after conversion. Omit to keep the current value.start (
ArrayLikeorobject, optional) – New scalar relative start time in ms (exclusive lower bound after addingorigin). Must be scalar-convertible and, whendtis in the environment, grid-representable. Omit to keep the current value.stop (
ArrayLikeorNoneorobject, optional) – New scalar relative stop time in ms (inclusive upper bound after addingorigin).Nonemaps to+inf. Must satisfystop >= startafter conversion. Omit to keep the current value.origin (
ArrayLikeorobject, optional) – New scalar time origin offset in ms added to bothstartandstop. Must be scalar-convertible and grid-representable whendtis available. Omit to keep the current value.
- Raises:
ValueError – If
rate < 0after conversion; ifstop < startafter conversion; or if any finite timing parameter is not representable on the current simulation grid (checked via_assert_grid_time()).TypeError – If unit conversion to
u.Hzoru.msfails for any supplied value.
See also
poisson_generator.getRead-back current parameter values.
Examples
>>> import brainpy >>> import saiunit as u >>> gen = brainpy.state.poisson_generator(rate=500.0 * u.Hz) >>> gen.set(rate=1000.0 * u.Hz, stop=50.0 * u.ms) >>> params = gen.get() >>> _ = params['rate'], params['stop']
- update()[source]#
Advance one simulation step and return per-step spike multiplicities.
- Returns:
spikes – Integer array with dtype
int64and shapeself.varshape. Each element is the number of spikes emitted by the corresponding independent output train in the current time step.Active and
rate > 0: entries are i.i.d. Poisson(\(\lambda\)) samples with \(\lambda = r \cdot \Delta t / 1000\).Inactive or
rate <= 0: all entries are exactly0.
- Return type:
jax.Array- Raises:
ValueError – If the timing cache is stale and a finite
origin,start, orstopis not representable on the current simulation grid (checked by_assert_grid_time()).KeyError – If
dtis unavailable frombrainstate.environ.get_dt()ortis expected but cannot be resolved.
Notes
The update proceeds as follows each call:
Lazy init – If
rng_keyhas not been created byinit_state(), it is initialized automatically withself.rng_seed.Cache refresh – When
dtchanges from the previously cached value,_refresh_timing_cache()recomputes the integer step bounds \(t_{\min}\) and \(t_{\max}\).Rate guard – If
rate <= 0, an all-zero array is returned without touching the PRNG state.Activity check – The current step index is compared against the cached step bounds: active iff \(t_{\min,\mathrm{step}} < \mathrm{curr\_step} \le t_{\max,\mathrm{step}}\). Inactive steps return zeros.
Poisson draw – If active, one vectorized sample
jax.random.poisson(lam, shape=self.varshape)is drawn via_sample_poisson(), consuming one PRNG split.
See also
poisson_generator.init_stateRNG initialization called lazily here.
poisson_generator.setUpdate parameters between runs.