class brainpy.encoding.LatencyEncoder(min_val=None, max_val=None, method='log', threshold=0.01, clip=False, tau=1.0, normalize=False, first_spk_time=0.0, epsilon=1e-07)[source]#

Encode the rate input as the spike train using the latency encoding.

Use input features to determine time-to-first spike.

Expected inputs should be between 0 and 1. If not, the latency encoder will encode x (normalized into [0, 1] according to \(x_{\text{normalize}} = \frac{x-\text{min_val}}{\text{max_val} - \text{min_val}}\)) to spikes whose firing time is \(0 \le t_f \le \text{num_period}-1\). A larger x will cause the earlier firing time.


>>> a = bm.array([0.02, 0.5, 1])
>>> encoder = LatencyEncoder(method='linear', normalize=True)
>>> encoder.multi_steps(a, n_time=5)
Array([[0., 0., 1.],
       [0., 0., 0.],
       [0., 1., 0.],
       [0., 0., 0.],
       [1., 0., 0.]])
  • min_val (Optional[float]) – float. The minimal value in the given data x, used to the data normalization.

  • max_val (Optional[float]) – float. The maximum value in the given data x, used to the data normalization.

  • method (str) –

    str. How to convert intensity to firing time. Currently, we support linear or log. - If method='linear', the firing rate is calculated as

    \(t_f(x) = (\text{num_period} - 1)(1 - x)\).

    • If method='log', the firing rate is calculated as \(t_f(x) = (\text{num_period} - 1) - ln(\alpha * x + 1)\), where \(\alpha\) satisfies \(t_f(1) = \text{num_period} - 1\).

  • threshold (float) – float. Input features below the threhold will fire at the final time step unless clip=True in which case they will not fire at all, defaults to 0.01.

  • clip (bool) – bool. Option to remove spikes from features that fall below the threshold, defaults to False.

  • tau (float) – float. RC Time constant for LIF model used to calculate firing time, defaults to 1.

  • normalize (bool) – bool. Option to normalize the latency code such that the final spike(s) occur within num_steps, defaults to False.

  • epsilon (float) – float. A tiny positive value to avoid rounding errors when using torch.arange, defaults to 1e-7.

multi_steps(data, n_time=None)[source]#

Generate latency spikes according to the given input data.

Ensuring x in [0., 1.].

  • data – The rate-based input.

  • n_time (Optional[float]) – float. The total time to generate data. If None, use tau instead.


array. The output spiking trains.

Return type: