Correlograms of discrete events#

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)

Let’s generate some data. Here we have two neurons recorded together. We can group them in a TsGroup.

ts1 = nap.Ts(t=np.sort(np.random.uniform(0, 1000, 2000)), time_units="s")
ts2 = nap.Ts(t=np.sort(np.random.uniform(0, 1000, 1000)), time_units="s")
epoch = nap.IntervalSet(start=0, end=1000, time_units="s")
ts_group = nap.TsGroup({0: ts1, 1: ts2}, time_support=epoch)
print(ts_group)
  Index    rate
-------  ------
      0       2
      1       1

Autocorrelograms#

We can compute their autocorrelograms meaning the number of spikes of a neuron observed in a time windows centered around its own spikes. For this we can use the function compute_autocorrelogram. We need to specifiy the binsize and windowsize to bin the spike train.

autocorrs = nap.compute_autocorrelogram(
    group=ts_group, binsize=100, windowsize=1000, time_units="ms", ep=epoch  # ms
)
print(autocorrs)
           0     1
-0.9  0.9600  1.05
-0.8  1.0400  1.17
-0.7  1.1425  0.94
-0.6  1.0200  0.86
-0.5  1.0250  1.09
-0.4  1.0350  0.96
-0.3  1.0150  1.08
-0.2  0.9700  0.94
-0.1  0.9650  0.95
 0.0  0.0000  0.00
 0.1  0.9650  0.95
 0.2  0.9700  0.94
 0.3  1.0150  1.08
 0.4  1.0350  0.96
 0.5  1.0250  1.09
 0.6  1.0200  0.86
 0.7  1.1425  0.94
 0.8  1.0400  1.17
 0.9  0.9600  1.05

The variable autocorrs is a pandas DataFrame with the center of the bins for the index and each column is an autocorrelogram of one unit in the TsGroup.

Cross-correlograms#

Cross-correlograms are computed between pairs of neurons.

crosscorrs = nap.compute_crosscorrelogram(
    group=ts_group, binsize=100, windowsize=1000, time_units="ms"  # ms
)
print(crosscorrs)
          0
          1
-0.9  0.940
-0.8  0.855
-0.7  1.065
-0.6  0.930
-0.5  0.985
-0.4  0.925
-0.3  1.055
-0.2  0.945
-0.1  1.025
 0.0  1.075
 0.1  1.070
 0.2  0.920
 0.3  1.035
 0.4  0.950
 0.5  1.020
 0.6  1.025
 0.7  0.995
 0.8  1.100
 0.9  1.095

Column name (0, 1) is read as cross-correlogram of neuron 0 and 1 with neuron 0 being the reference time.

Event-correlograms#

Event-correlograms count the number of event in the TsGroup based on an event timestamps object.

eventcorrs = nap.compute_eventcorrelogram(
    group=ts_group, event = nap.Ts(t=[0, 10, 20]), binsize=0.1, windowsize=1
    )
print(eventcorrs)
             0         1
-0.9  0.000000  0.000000
-0.8  4.040404  3.703704
-0.7  0.000000  0.000000
-0.6  4.040404  0.000000
-0.5  0.000000  0.000000
-0.4  0.000000  0.000000
-0.3  0.000000  3.703704
-0.2  2.020202  3.703704
-0.1  0.000000  0.000000
 0.0  0.000000  0.000000
 0.1  0.000000  3.703704
 0.2  0.000000  0.000000
 0.3  2.020202  0.000000
 0.4  0.000000  7.407407
 0.5  0.000000  0.000000
 0.6  2.020202  0.000000
 0.7  2.020202  0.000000
 0.8  0.000000  0.000000
 0.9  0.000000  0.000000