LatencyEncoder

Contents

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]; with min_val and max_val they 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 both min_val and max_val are 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]. For method='log', inputs at or below it fire at the last step when normalize=True; with clip=True inputs below it do not fire at all.

  • clip (bool) – Whether features below threshold should not fire.

  • tau (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Time scale of the code when normalize=False, and the default window when n_time is 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 method is not 'linear' or 'log', or threshold is outside [0, 1].

See also

RankOrderEncoder

One spike per feature, ordered by value or rank.

SpikeCountEncoder

A fixed number of spikes per feature.

Notes

The output has n_time time 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\) is threshold; inputs at or below \(\theta\) are raised to \(\theta + \epsilon\).

With normalize=True the scale \(T\) is chosen so the slowest input (x = 0 for 'linear', x = threshold for 'log') fires at the last step of the window. For 'linear', x = 1 then fires at \(t_0\); for 'log' the code is strongly compressed toward \(t_0\) for all inputs well above threshold. With normalize=False, \(T\) is the physical time constant tau, 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)