sinusoidal_poisson_generator#
- class brainpy.state.sinusoidal_poisson_generator(in_size=1, rate=Quantity(0., 'Hz'), amplitude=Quantity(0., 'Hz'), frequency=Quantity(0., 'Hz'), phase=0.0, individual_spike_trains=True, start=Quantity(0., 'ms'), stop=None, origin=Quantity(0., 'ms'), rng_seed=0, name=None)#
Sinusoidally modulated Poisson spike generator compatible with NEST.
Description
sinusoidal_poisson_generatorre-implements NEST’s stimulation device of the same name and emits per-step spike multiplicities.1. Stochastic model and discretization
The instantaneous firing rate in spikes/s is
\[f(t) = \max\left( 0,\ r + a \sin\left( 2\pi f_{\mathrm{mod}} t / 1000 + \phi \right) \right),\]where:
\(r\) is
rate(spikes/s),\(a\) is
amplitude(spikes/s),\(f_{\mathrm{mod}}\) is
frequency(Hz),\(\phi\) is
phase(deg, internally converted to radians),\(t\) is simulation time in ms.
For simulation resolution \(\Delta t\) in ms, each output train samples a Poisson multiplicity
\[K_n \sim \mathrm{Poisson}(\lambda_n), \qquad \lambda_n = f_n \Delta t / 1000,\]where the
1000factor converts Hz * ms to a dimensionless mean.K_nis an integer count0, 1, 2, ...and may exceed1.2. Oscillator-state recurrence and derivation
Following NEST, sinusoidal modulation is stored in a rotated two-component oscillator state:
\[y_0(t) = a/1000 \cdot \cos(\omega t + \phi), \qquad y_1(t) = a/1000 \cdot \sin(\omega t + \phi),\]with \(\omega = 2\pi f_{\mathrm{mod}}/1000\) (rad/ms). One-step propagation by \(\Delta t\) uses a rotation matrix \(R(\omega\Delta t)\):
\[\begin{split}\begin{bmatrix} y_0' \\ y_1' \end{bmatrix} = \begin{bmatrix} \cos(\omega\Delta t) & -\sin(\omega\Delta t) \\ \sin(\omega\Delta t) & \cos(\omega\Delta t) \end{bmatrix} \begin{bmatrix} y_0 \\ y_1 \end{bmatrix}.\end{split}\]The post-rotation
y_1'is then added torate/1000and clamped at zero before Poisson sampling. This avoids recomputing trigonometric functions each step and keeps per-step modulation update constant-time.3. Update ordering (NEST source order)
The internal two-component oscillator state is updated exactly in the order used by NEST
models/sinusoidal_poisson_generator.cpp:Start from the DC component
rate.Rotate oscillator state
(y_0, y_1)by one step.Add the rotated
y_1to obtain instantaneous rate.Clamp rate at zero.
Sample Poisson multiplicities if active.
The per-step recorded
ratevalue in NEST corresponds to this updated post-rotation rate. This implementation exposes it viaget_recorded_rate().4. Timing semantics
NEST currently classifies this model as
CURRENT_GENERATORinget_type(). Consequently, activity is evaluated with a two-step shift inStimulationDevice::is_active:\[t_{\min} < (n + 2) \le t_{\max},\]where
nis current simulation step index andt_{\min} = \mathrm{origin} + \mathrm{start},t_{\max} = \mathrm{origin} + \mathrm{stop}(in steps).This differs from regular spike generators and is intentionally replicated here to match NEST behavior.
5. Assumptions, constraints, and computational implications
Public parameters are scalar-only; non-scalar values raise
ValueError.stopmust satisfystop >= startafter unit conversion.When
dtis available, finiteorigin,start, andstopmust be representable on the simulation grid.If
dtchanges, timing caches and oscillator state are recomputed from absolute simulation time to preserve NEST-compatible behavior.Per-step complexity is \(O(\prod \mathrm{varshape})\) for Poisson sampling and \(O(1)\) for oscillator/timing updates.
- Parameters:
in_size (
Size, optional) – Output size specification forbrainstate.nn.Dynamics. The derivedself.varshapeis the shape of values returned byupdate(); each element corresponds to one emitted train. Default is1.rate (
ArrayLike, optional) – Scalar baseline firing rate in spikes/s (Hz), shape()after conversion. Accepted inputs include scalarArrayLikeandsaiunit.Quantityconvertible tou.Hz. Default is0.0 * u.Hz.amplitude (
ArrayLike, optional) – Scalar sinusoidal modulation amplitude in spikes/s (Hz), shape()after conversion. Units and conversion rules matchrate. Default is0.0 * u.Hz.frequency (
ArrayLike, optional) – Scalar modulation frequency in Hz, shape()after conversion. Internally converted to angular frequency in rad/ms. Default is0.0 * u.Hz.phase (
ArrayLike, optional) – Scalar modulation phase in degrees, shape()after conversion. Internally converted to radians. Default is0.0.individual_spike_trains (
bool, optional) – Sampling mode selector. IfTrue, Poisson sampling is independent for each index ofself.varshape. IfFalse, one sampled multiplicity is broadcast to all outputs. Default isTrue.start (
ArrayLike, optional) – Scalar relative activation start time in ms, shape()after conversion. Activity uses NEST current-generator semantics with a two-step shifted check. Default is0.0 * u.ms.stop (
ArrayLikeorNone, optional) – Scalar relative deactivation stop time in ms, shape()after conversion.Nonemaps to+inf. Must satisfystop >= startafter conversion. Default isNone.origin (
ArrayLike, optional) – Scalar global time offset in ms, shape()after conversion. Added tostartandstopfor activity-window bounds. Default is0.0 * u.ms.rng_seed (
int, optional) – Seed used to initializejax.random.PRNGKeyininit_state()and lazy initialization inupdate(). Default is0.name (
str, optional) – Optional node name.
Parameter Mapping
Table 34 Parameter mapping to model symbols# Parameter
Default
Math symbol
Semantics
rate0.0 * u.Hz\(r\)
Baseline firing rate in spikes/s.
amplitude0.0 * u.Hz\(a\)
Sinusoidal modulation amplitude in spikes/s.
frequency0.0 * u.Hz\(f_{\mathrm{mod}}\)
Modulation frequency in Hz.
phase0.0\(\phi\)
Modulation phase in degrees (internally radians).
start0.0 * u.ms\(t_{\mathrm{start,rel}}\)
Relative lower activity bound (NEST shifted semantics).
stopNone\(t_{\mathrm{stop,rel}}\)
Relative upper activity bound;
Nonemaps to+\infty.origin0.0 * u.ms\(t_0\)
Global time offset applied to start/stop.
in_size1Defines output train count/shape via
self.varshape.individual_spike_trainsTrueIndependent-per-output sampling vs shared broadcast sample.
rng_seed0Seed for JAX random key evolution.
- Raises:
ValueError – If any scalar-constrained parameter cannot be reduced to one value; if
stop < start; or if finiteorigin/start/stopare not representable on the simulation grid whendtis available.TypeError – If numeric/unit conversion fails for provided rate/time inputs.
KeyError – At runtime, if required simulation context keys (for example
dtinupdate()) are unavailable throughbrainstate.environ.
Notes
Time parameters are validated on the simulation grid when
dtis available, matching repository conventions used by other NEST-compatible generators.The oscillator state is re-initialized from absolute simulation time whenever the simulation resolution changes, matching NEST pre-run calibration behavior.
Recorded rate from
get_recorded_rate()is the post-rotation, post-clamp value in spikes/s used for current-step sampling logic.
Examples
>>> import brainpy >>> import brainstate >>> import saiunit as u >>> with brainstate.environ.context(dt=0.1 * u.ms): ... gen = brainpy.state.sinusoidal_poisson_generator( ... in_size=4, ... rate=800.0 * u.Hz, ... amplitude=200.0 * u.Hz, ... frequency=10.0 * u.Hz, ... phase=90.0, ... start=5.0 * u.ms, ... stop=50.0 * u.ms, ... rng_seed=123, ... ) ... with brainstate.environ.context(t=10.0 * u.ms): ... counts = gen.update() ... _ = counts.shape
>>> import brainpy >>> import brainstate >>> import saiunit as u >>> with brainstate.environ.context(dt=0.1 * u.ms): ... gen = brainpy.state.sinusoidal_poisson_generator( ... individual_spike_trains=False ... ) ... gen.set(rate=500.0 * u.Hz, amplitude=300.0 * u.Hz, phase=45.0) ... params = gen.get() ... _ = params['rate'], params['amplitude']
See also
poisson_generatorHomogeneous Poisson generator.
inhomogeneous_poisson_generatorPiecewise-constant inhomogeneous Poisson generator.
sinusoidal_gamma_generatorSinusoidally modulated gamma-renewal generator.
References
- get()[source]#
Return current public parameters and oscillator state snapshot.
- Returns:
Dictionary with keys
rate,frequency,phase,amplitude,individual_spike_trains,start,stop,origin,y_0, andy_1. Rates are in spikes/s, times are in ms, and oscillator states are in spikes/ms.- Return type:
- get_recorded_rate()[source]#
Return latest post-update instantaneous rate in spikes/s.
- Returns:
Most recent stored value of the post-rotation, post-clamp instantaneous rate. Returns
0.0before state initialization.- Return type:
- init_state(batch_size=None, **kwargs)[source]#
Initialize RNG, oscillator states, and cached recorded rate.
- set(*, rate=<object object>, amplitude=<object object>, frequency=<object object>, phase=<object object>, individual_spike_trains=<object object>, start=<object object>, stop=<object object>, origin=<object object>)[source]#
Set public parameters and refresh dependent cached state.
- Parameters:
rate (
ArrayLikeorobject, optional) – Scalar rate in spikes/s (Hz)._UNSETkeeps current value.amplitude (
ArrayLikeorobject, optional) – Scalar sinusoidal amplitude in spikes/s (Hz)._UNSETkeeps current value.frequency (
ArrayLikeorobject, optional) – Scalar frequency in Hz._UNSETkeeps current value.phase (
ArrayLikeorobject, optional) – Scalar phase in degrees._UNSETkeeps current value.individual_spike_trains (
boolorobject, optional) – Sampling mode flag._UNSETkeeps current value.start (
ArrayLikeorobject, optional) – Scalar relative start time in ms._UNSETkeeps current value.stop (
ArrayLike,None, orobject, optional) – Scalar relative stop time in ms, orNonefor+inf._UNSETkeeps current value.origin (
ArrayLikeorobject, optional) – Scalar origin time in ms._UNSETkeeps current value.
- Raises:
ValueError – If scalar conversion fails,
stop < start, or grid-time validation fails whendtis available.TypeError – If unit or numeric conversion fails for supplied inputs.
- update()[source]#
Advance generator by one simulation step and emit spike counts.
- Returns:
int64array with shapeself.varshape. Values are per-step spike multiplicities sampled from the configured sinusoidal Poisson process, or zeros when inactive/non-positive-rate.- Return type:
jax.Array- Raises:
KeyError – If required environment entries (for example
dt) are not available throughbrainstate.environat runtime.ValueError – If cached timing constraints become invalid after environment changes (for example non-grid-aligned finite time bounds).