RateEncoder

Contents

RateEncoder#

class braintools.spike.RateEncoder(gain=100.0, method='linear', min_rate=0.0, max_rate=None, normalize=False, min_val=None, max_val=None)#

Encode analog values into spike rates using various rate encoding methods.

The rate encoder converts continuous input values into spike trains where the firing rate grows with the input intensity. An input \(x\) is mapped to a rate between min_rate and max_rate (in Hz), and every time step fires independently with probability \(\text{rate} \cdot dt\) (clipped to [0, 1]), where \(dt\) is the brainstate.environ time step.

Parameters:
  • gain (float) – Firing rate (Hz) for an input of 1; used as max_rate when that is not given.

  • method (str) – How the input is mapped to a rate (see Notes).

  • min_rate (float) – Minimum firing rate in Hz.

  • max_rate (float | None) – Maximum firing rate in Hz. Defaults to gain.

  • normalize (bool) – Whether to rescale inputs to [0, 1] with min_val and max_val (both must be given).

  • min_val (float | None) – Minimum value for normalization.

  • max_val (float | None) – Maximum value for normalization.

Raises:

ValueError – If method is not one of 'linear', 'exponential' or 'sqrt'.

See also

PoissonEncoder

Takes rates in Hz directly.

BernoulliEncoder

Sets the spike probability per time step directly.

Notes

With \(r_0\) = min_rate and \(r_1\) = max_rate:

  • 'linear': \(r = r_0 + x\,(r_1 - r_0)\).

  • 'exponential': \(r = r_0 + \frac{e^x - 1}{e - 1}\,(r_1 - r_0)\).

  • 'sqrt': \(r = r_0 + \sqrt{x}\,(r_1 - r_0)\).

Examples

>>> import brainstate
>>> import brainunit as u
>>> import jax.numpy as jnp
>>> import braintools
>>> brainstate.environ.set(dt=1. * u.ms)
>>> brainstate.random.seed(0)
>>> encoder = braintools.spike.RateEncoder(gain=100., method='linear')
>>> spikes = encoder(jnp.array([0.1, 0.5, 0.9]), n_time=10000)
>>> spikes.shape
(10000, 3)
>>> # The mean is a probability per step; divide by dt (in s) for Hz.
>>> rates = spikes.mean(axis=0) / 1e-3  # approximately [10, 50, 90] Hz