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  1.0175  0.84
-0.8  1.0800  1.20
-0.7  1.0775  0.92
-0.6  1.0125  1.01
-0.5  1.0350  0.96
-0.4  1.0425  0.98
-0.3  0.9175  1.18
-0.2  0.9950  0.83
-0.1  1.0975  1.09
 0.0  0.0000  0.00
 0.1  1.0975  1.09
 0.2  0.9950  0.83
 0.3  0.9175  1.18
 0.4  1.0425  0.98
 0.5  1.0350  0.96
 0.6  1.0125  1.01
 0.7  1.0775  0.92
 0.8  1.0800  1.20
 0.9  1.0175  0.84

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  1.110
-0.8  0.900
-0.7  0.880
-0.6  0.835
-0.5  0.915
-0.4  0.970
-0.3  1.005
-0.2  1.095
-0.1  1.040
 0.0  1.100
 0.1  1.015
 0.2  1.060
 0.3  0.860
 0.4  0.935
 0.5  0.950
 0.6  0.950
 0.7  0.905
 0.8  1.070
 0.9  0.925

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  1.257862  0.000000
-0.8  1.257862  7.843137
-0.7  1.257862  0.000000
-0.6  0.000000  0.000000
-0.5  0.000000  0.000000
-0.4  0.000000  0.000000
-0.3  1.257862  0.000000
-0.2  1.257862  0.000000
-0.1  0.000000  0.000000
 0.0  1.257862  3.921569
 0.1  0.000000  0.000000
 0.2  1.257862  3.921569
 0.3  0.000000  3.921569
 0.4  0.000000  0.000000
 0.5  1.257862  0.000000
 0.6  0.000000  0.000000
 0.7  0.000000  0.000000
 0.8  0.000000  0.000000
 0.9  0.000000  0.000000