Perievent#

The perievent module allows to re-center time series and timestamps data around a particular event as well as computing events (spikes) trigger average.

Hide code cell content
import pynapple as nap
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(style="ticks", palette="colorblind", font_scale=1.5, rc=custom_params)

Peri-Event Time Histogram (PETH)#

stim = nap.Tsd(
    t=np.sort(np.random.uniform(0, 1000, 50)), 
    d=np.random.rand(50), time_units="s"
)
ts1 = nap.Ts(t=np.sort(np.random.uniform(0, 1000, 2000)), time_units="s")

The function compute_perievent align timestamps to a particular set of timestamps.

peth = nap.compute_perievent(
  timestamps=ts1, 
  tref=stim, 
  minmax=(-0.1, 0.2), 
  time_unit="s")

print(peth)
Index    rate     ref_times
-------  -------  -----------
0        3.33333  13.35
1        6.66667  27.97
2        6.66667  40.81
3        3.33333  89.23
4        6.66667  91.13
5        3.33333  103.87
6        3.33333  107.93
...      ...      ...
43       3.33333  910.21
44       nan      922.84
45       nan      936.14
46       nan      948.56
47       6.66667  956.24
48       nan      975.94
49       3.33333  999.05

The returned object is a TsGroup. The column ref_times is a metadata column that indicates the center timestamps.

Raster plot#

It is then easy to create a raster plot around the times of the stimulation event by calling the to_tsd function of pynapple to “flatten” the TsGroup peth.

plt.figure(figsize=(10, 6))
plt.subplot(211)
plt.plot(np.mean(peth.count(0.01), 1) / 0.01, linewidth=3, color="red")
plt.xlim(-0.1, 0.2)
plt.ylabel("Rate (spikes/sec)")
plt.axvline(0.0)
plt.subplot(212)
plt.plot(peth.to_tsd(), "|", markersize=20, color="red", mew=4)
plt.xlabel("Time from stim (s)")
plt.ylabel("Stimulus")
plt.xlim(-0.1, 0.2)
plt.axvline(0.0)
<matplotlib.lines.Line2D at 0x7f28361f6f30>
../_images/7a885efa0530322d9fd777d77a8f2ae5c74b4e1871aee07d0bdb567173029710.png

The same function can be applied to a group of neurons. In this case, it returns a dict of TsGroup

Event trigger average#

The function compute_event_trigger_average compute the average feature around a particular event time.

Hide code cell content
group = {
    0: nap.Ts(t=np.sort(np.random.uniform(0, 100, 10))),
    1: nap.Ts(t=np.sort(np.random.uniform(0, 100, 20))),
    2: nap.Ts(t=np.sort(np.random.uniform(0, 100, 30))),
}

tsgroup = nap.TsGroup(group)
eta = nap.compute_event_trigger_average(
  group=tsgroup, 
  feature=stim, 
  binsize=0.1, 
  windowsize=(-1, 1))

print(eta)
Time (s)    0        1       2
----------  -------  ------  -------
-1.0        0.20034  0.1931  0.17565
-0.9        0.20034  0.1931  0.17565
-0.8        0.20034  0.1931  0.17565
-0.7        0.20034  0.1931  0.17565
-0.6        0.20034  0.1931  0.17565
-0.5        0.20034  0.1931  0.17565
-0.4        0.20034  0.1931  0.18127
...         ...      ...     ...
0.4         0.20034  0.1931  0.20481
0.5         0.20034  0.1931  0.20481
0.6         0.20034  0.1931  0.20481
0.7         0.20034  0.1931  0.20481
0.8         0.20034  0.1931  0.20481
0.9         0.20034  0.1931  0.1999
1.0         0.20034  0.1931  0.1999
dtype: float64, shape: (21, 3)

Peri-Event continuous time series#

The function nap.compute_perievent_continuous align a time series of any dimensions around events.

Hide code cell content
features = nap.TsdFrame(t=np.arange(0, 100), d=np.random.randn(100,6))
events = nap.Ts(t=np.sort(np.random.uniform(0, 100, 5)))
perievent = nap.compute_perievent_continuous(
  timeseries=features, 
  tref=events, 
  minmax=(-1, 1))

print(perievent)
Time (s)
----------  -------------------------------
-1          [[0.15041  ... 1.575135] ...]
0           [[ 0.953271 ... -0.835727] ...]
1           [[0.191293 ... 0.110772] ...]
dtype: float64, shape: (3, 5, 6)

The object perievent is now of shape (number of bins, (dimensions of input), number of events ) :

print(perievent.shape)
(3, 5, 6)