LatencyEncoder#
- class braintools.spike.LatencyEncoder(min_val=None, max_val=None, method='log', threshold=0.01, clip=False, tau=Quantity(1., 'ms'), normalize=False, first_spk_time=Quantity(0., 'ms'), epsilon=1e-07)#
Encode input intensities as the time of a single spike (latency coding).
Each input feature fires at most once; a larger value fires earlier. Inputs are expected in
[0, 1]; withmin_valandmax_valthey are first rescaled as \(x = (x - \text{min_val}) / (\text{max_val} - \text{min_val})\).- Parameters:
min_val (
float|None) – The minimal value of the data, used for rescaling. Rescaling happens only when bothmin_valandmax_valare given.max_val (
float|None) – The maximal value of the data, used for rescaling.method (
str) – The latency code (see Notes).threshold (
float) – Value in[0, 1]. Formethod='log', inputs at or below it fire at the last step whennormalize=True; withclip=Trueinputs below it do not fire at all.clip (
bool) – Whether features belowthresholdshould not fire.tau (
Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Time scale of the code whennormalize=False, and the default window whenn_timeis None. Plain numbers are in milliseconds.normalize (
bool) – Whether to scale the codes to the window (see Notes).first_spk_time (
Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Time of the earliest possible spike. Plain numbers are in milliseconds.epsilon (
float) – Small value that keeps the'log'code finite.
- Raises:
ValueError – If
methodis not'linear'or'log', orthresholdis outside[0, 1].
See also
RankOrderEncoderOne spike per feature, ordered by value or rank.
SpikeCountEncoderA fixed number of spikes per feature.
Notes
The output has
n_timetime steps (the window). Spike times are computed as follows, with \(t_0\) =first_spk_time:method='linear': \(t(x) = t_0 + T\,(1 - x)\).method='log'(RC charging time of a leaky integrator driven by \(x\)): \(t(x) = t_0 + T \log\frac{x}{x - \theta}\), where \(\theta\) isthreshold; inputs at or below \(\theta\) are raised to \(\theta + \epsilon\).
With
normalize=Truethe scale \(T\) is chosen so the slowest input (x = 0for'linear',x = thresholdfor'log') fires at the last step of the window. For'linear',x = 1then fires at \(t_0\); for'log'the code is strongly compressed toward \(t_0\) for all inputs well abovethreshold. Withnormalize=False, \(T\) is the physical time constanttau, and spikes that would fall after the window are dropped.Examples
>>> import brainstate >>> import brainunit as u >>> import jax.numpy as jnp >>> import braintools >>> brainstate.environ.set(dt=1. * u.ms) >>> encoder = braintools.spike.LatencyEncoder(method='linear', normalize=True) >>> encoder(jnp.array([0.0, 0.5, 1.0]), n_time=5) Array([[0., 0., 1.], [0., 0., 0.], [0., 1., 0.], [0., 0., 0.], [1., 0., 0.]], dtype=float32) >>> >>> # The log code fires inputs well above threshold early. >>> log_encoder = braintools.spike.LatencyEncoder(method='log', normalize=True, threshold=0.1) >>> spikes = log_encoder(jnp.array([0.1, 0.5, 1.0]), n_time=100) >>> jnp.argmax(spikes, axis=0) Array([99, 2, 1], dtype=int32)