braintools.analysis.spike.spike_triggered_average#
- braintools.analysis.spike.spike_triggered_average(trains, signal, dt, window)#
Average a sampled signal around the spikes of every neuron.
For neuron \(n\) with usable spikes \(t_1, \dots, t_m\),
\[\mathrm{STA}_n[l] = \frac{1}{m} \sum_{k=1}^{m} x[s_k + l], \qquad l = 0, \dots, L - 1,\]where \(s_k = \lfloor (t_k - t_\text{before} - t_0) / dt \rfloor\) is the first sample of the window of spike \(k\) and \(L = \lceil (t_\text{before} + t_\text{after}) / dt \rceil\).
- Parameters:
trains (
SpikeTrains) – Spike trains withNneurons.signal (
Any) – Signal of shape(T, ...): sampleiis at timetrains.t_start + i * dt. Shared by all neurons.dt (
Any) – Sampling interval, static and positive. A plain number is in milliseconds, like the other spike-time arguments.window (
Sequence[Any]) –(t_before, t_after): the average spans[-t_before, t_after)around each spike (a positivet_beforereaches before the spike). Static; plain numbers are in milliseconds.
- Return type:
- Returns:
sta (jax.Array or Quantity) – Averages of shape
(N, L) + signal.shape[1:], a Quantity whensignalis one.nanfor a neuron without a usable spike.lags (jax.Array or Quantity) – Nominal lag of every sample,
-t_before + l * dt, shape(L,); a Quantity when the spike times carry units, else milliseconds.
- Raises:
TypeError – If
trainsis not aSpikeTrains, ordt/windoware traced.ValueError – If
signalis a scalar,dtis not positive, or the window has non-positive length.
See also
psthEvent-aligned spike histograms.
braintools.spike.SpikeTrains.from_denseBuild the input from a raster.
Notes
A spike is used only if its whole window lies inside the signal,
t - t_before >= t_0andt + t_after <= t_0 + T dt; other spikes, e.g. near the start of the recording, are excluded [1].This matches
elephant.sta.spike_triggered_average(signal, trains, window=(-t_before, t_after)): Elephant gives the window as signed(start, stop)offsets, while heret_beforeis the (positive) time before the spike.The windows are gathered with
O(N K L D)memory, whereDis the number of signal channels. Works underjax.jitandjax.vmapwith staticdtandwindow, and is differentiable with respect tosignal.References
Examples
>>> import brainunit as u >>> import jax.numpy as jnp >>> from braintools.spike import SpikeTrains >>> from braintools.analysis.spike import spike_triggered_average >>> signal = jnp.arange(10.) * u.mV >>> raster = jnp.zeros((10, 1)).at[jnp.array([1, 4, 6]), 0].set(1) >>> st = SpikeTrains.from_dense(raster, dt=1. * u.ms) >>> sta, lags = spike_triggered_average(st, signal, dt=1. * u.ms, ... window=(2. * u.ms, 1. * u.ms)) >>> sta # the spike at 1 ms is excluded Quantity([[3. 4. 5.]], "mV") >>> lags Quantity([-2. -1. 0.], "ms")