step_rate_generator#
- class brainpy.state.step_rate_generator(in_size=1, amplitude_times=(), amplitude_values=(), start=Quantity(0., 'ms'), stop=None, origin=Quantity(0., 'ms'), name=None)#
Piecewise-constant rate generator – NEST-compatible stimulation device.
Generate a deterministic piecewise-constant rate trace and gate it with a half-open activity window using NEST-compatible parameter semantics.
1. Model equations and schedule selection
Let \(\{(t_k, a_k)\}_{k=1}^{K}\) be configured change-time/rate pairs, where \(t_k\) are times in ms and \(a_k\) are rates in spikes/s (Hz). The scheduled rate is
\[\begin{split}A(t) = \begin{cases} 0, & t < t_1, \\ a_k, & t_k \le t < t_{k+1},\ k=1,\dots,K-1, \\ a_K, & t \ge t_K. \end{cases}\end{split}\]The output is gated by
\[g(t) = \mathbf{1}\!\left[t \ge t_0+t_{\mathrm{start,rel}}\right] \cdot \mathbf{1}\!\left[t < t_0+t_{\mathrm{stop,rel}}\right],\]with the second indicator omitted when
stop is None. Final output:\[r_{\mathrm{out}}(t) = g(t)\,A(t).\]2. Timing semantics, assumptions, and constraints
This implementation chooses, at environment time
t, the latest schedule entry satisfyingt_k <= t. With discrete simulation time on a grid, this reproduces NEST-compatible step semantics where a configured change time marks the first step at which the new rate is emitted.Enforced constraints:
len(amplitude_times) == len(amplitude_values).amplitude_timesare strictly increasing.
Accepted but not additionally constrained:
Unitless
amplitude_timesare interpreted as ms.Unitless
amplitude_valuesare interpreted as spikes/s.NEST documentation recommends positive change times; positivity is not explicitly enforced here.
3. Computational implications
Each
update()call usesu.math.searchsorted()to find the active plateau, then selects the pre-broadcast rate array forself.varshapeand applies one boolean activity mask. Per-call complexity is \(O(\log K + \prod \mathrm{varshape})\), where \(K\) is the number of schedule entries.- Parameters:
in_size (
Size, optional) – Output size/shape specification consumed bybrainstate.nn.Dynamics. The emitted rate has shapeself.varshapederived fromin_size. Default is1.amplitude_times (
Sequence, optional) – Ordered sequence of change times with lengthK. Each value may be a unitful time (typically ms) or a unitless numeric. Passed directly tou.math.asarray(), which validates unit consistency across all entries. Must be strictly increasing. Default is().amplitude_values (
Sequence, optional) – Sequence of plateau rates with lengthKmatchingamplitude_timeselementwise. Values represent spikes/s (Hz) and may be unitful or unitless. Each entry is converted viau.math.asarray()and expanded to the maximum ndim found across all entries (by prepending size-1 axes); the results are stacked to a shape that is broadcastable to(K, *varshape). Default is().start (
ArrayLike, optional) – Relative start time \(t_{\mathrm{start,rel}}\) (typically ms), broadcast toself.varshapeviabraintools.init.param(). Effective lower bound isorigin + start(inclusive). Default is0. * u.ms.stop (
ArrayLikeorNone, optional) – Relative stop time \(t_{\mathrm{stop,rel}}\) (typically ms), broadcast toself.varshapewhen provided. Effective upper bound isorigin + stop(exclusive).Nonemeans no upper bound. Default isNone.origin (
ArrayLike, optional) – Time origin \(t_0\) (typically ms) added tostartandstop, broadcast toself.varshape. Default is0. * u.ms.name (
strorNone, optional) – Optional node name passed tobrainstate.nn.Dynamics.
Parameter Mapping
Table 27 Parameter mapping to model symbols# Parameter
Default
Math symbol
Semantics
amplitude_times()\(t_k\)
Change times for piecewise-constant rate plateaus.
amplitude_values()\(a_k\)
Plateau rates (spikes/s) selected at and after each
t_k.start0. * u.ms\(t_{\mathrm{start,rel}}\)
Relative inclusive lower bound of the active output window.
stopNone\(t_{\mathrm{stop,rel}}\)
Relative exclusive upper bound of the active output window.
origin0. * u.ms\(t_0\)
Global time offset added to
startandstop.- Raises:
ValueError – If
amplitude_timesandamplitude_valueslengths differ, or ifamplitude_timesis not strictly increasing.TypeError – If
u.math.asarray()detects unit inconsistency across entries, or if unitful/unitless arithmetic is invalid during broadcasting or time-window comparisons.KeyError – At update time, if simulation time
't'is missing frombrainstate.environ.
Notes
NEST recommends specifying
amplitude_timeson a grid of simulation resolutiondt. Using off-grid change times is allowed but may shift the effective change by up to onedtstep depending on floating-point rounding when comparingt >= amp_time. Usedc_generatorwhen only a constant current drive is needed; usestep_rate_generatorwhen a rate-coded drive must take different values at different simulation intervals. Unlikestep_current_generator, the emitted quantity is dimensionless (spikes/s) and is not multiplied by a unit before output.See also
step_current_generatorPiecewise-constant current stimulation device.
dc_generatorConstant current stimulation device.
ac_generatorSinusoidal current stimulation device.
References
Examples
>>> import brainpy >>> import brainstate >>> import saiunit as u >>> with brainstate.environ.context(dt=0.1 * u.ms): ... gen = brainpy.state.step_rate_generator( ... amplitude_times=[10.0 * u.ms, 110.0 * u.ms, 210.0 * u.ms], ... amplitude_values=[400.0, 1000.0, 200.0], ... start=0.0 * u.ms, ... stop=300.0 * u.ms, ... ) ... with brainstate.environ.context(t=160.0 * u.ms): ... rate = gen.update() ... _ = rate.shape
>>> import brainpy >>> import saiunit as u >>> gen1 = brainpy.state.step_rate_generator( ... amplitude_times=[0.0 * u.ms, 100.0 * u.ms, 200.0 * u.ms], ... amplitude_values=[50.0, 0.0, 80.0], ... ) >>> gen2 = brainpy.state.step_rate_generator( ... in_size=10, ... amplitude_times=[50.0 * u.ms, 150.0 * u.ms], ... amplitude_values=[120.0, 40.0], ... start=40.0 * u.ms, ... stop=180.0 * u.ms, ... origin=10.0 * u.ms, ... )
- update()[source]#
Compute scheduled rate at environment time
t.The implementation is fully compatible with
jax.jit: the schedule look-up usesu.math.searchsorted()on the staticamplitude_timesarray, whiletremains a traced value throughout.- Returns:
out – Dimensionless rate array with shape
self.varshapeand values in spikes/s. For each output channel, value equals the latest scheduled plateau whose change time is<= t. Channels outside the active window[origin + start, origin + stop)are set to zero (ort >= origin + startwhenstop is None).- Return type:
jax.Array- Raises:
KeyError – If
brainstate.environhas no't'entry.
Notes
Both
amplitude_timesandtare divided byu.msto obtain dimensionless arrays before callingu.math.searchsorted().u.math.searchsorted(..., side='right') - 1returns the index of the most-recently-passed change point, or-1whentprecedes all change times (zero rate).u.math.clip()keeps the index in bounds for the gather;u.math.where()then suppresses the result when the index is negative. Start is inclusive and stop is exclusive, matching NEST semantics.See also
step_rate_generatorClass-level parameter definitions and model equations.
step_current_generator.updateWindowed piecewise-constant current update rule.
dc_generator.updateWindowed constant-current update rule.