Correlograms & ISI#

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.0250  1.03
-0.8  0.9800  0.93
-0.7  1.0600  0.98
-0.6  0.9200  0.84
-0.5  1.0250  0.85
-0.4  0.9625  1.09
-0.3  0.9850  1.01
-0.2  0.9050  0.84
-0.1  0.9325  1.02
 0.0  0.0000  0.00
 0.1  0.9325  1.02
 0.2  0.9050  0.84
 0.3  0.9850  1.01
 0.4  0.9625  1.09
 0.5  1.0250  0.85
 0.6  0.9200  0.84
 0.7  1.0600  0.98
 0.8  0.9800  0.93
 0.9  1.0250  1.03

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.910
-0.8  1.120
-0.7  0.910
-0.6  1.000
-0.5  1.050
-0.4  1.045
-0.3  1.045
-0.2  0.975
-0.1  0.980
 0.0  0.960
 0.1  0.975
 0.2  1.060
 0.3  1.105
 0.4  0.965
 0.5  1.185
 0.6  0.980
 0.7  0.885
 0.8  0.955
 0.9  0.935

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.360544  0.000000
-0.8  1.360544  0.000000
-0.7  0.000000  0.000000
-0.6  0.000000  0.000000
-0.5  0.000000  0.000000
-0.4  1.360544  0.000000
-0.3  1.360544  0.000000
-0.2  0.000000  3.333333
-0.1  1.360544  0.000000
 0.0  0.000000  0.000000
 0.1  1.360544  0.000000
 0.2  0.000000  0.000000
 0.3  0.000000  0.000000
 0.4  0.000000  0.000000
 0.5  0.000000  0.000000
 0.6  0.000000  0.000000
 0.7  0.000000  3.333333
 0.8  2.721088  0.000000
 0.9  2.721088  0.000000

Interspike interval (ISI) distribution#

The interspike interval distribution shows how the time differences between subsequent spikes (events) are distributed. The input can be any object with timestamps. Passing epochs restricts the computation to the given epochs. The output will be a dataframe with the bin centres as index and containing the corresponding ISI counts per unit.

isi_distribution = nap.compute_isi_distribution(
    data=ts_group, bins=10, epochs=epoch
    )
print(isi_distribution)
             0    1
0.365475  1540  512
1.096284   354  265
1.827094    73  115
2.557903    23   49
3.288712     8   35
4.019522     0   12
4.750331     1    6
5.481140     0    4
6.211950     0    0
6.942759     0    1

Hide code cell source

for col in isi_distribution.columns:
    plt.bar(
        isi_distribution.index,
        isi_distribution[col].values,
        width=np.diff(isi_distribution.index).mean(),
        alpha=0.5,
        label=col,
        align='center',
        edgecolor='none'
    )
plt.xlabel("ISI (s)")
plt.ylabel("Count")
plt.legend(title="Unit")
plt.show()
../_images/de491fd9f8d9d6a46199dde9aa7cab418b5e625c431a50e417b2e59434ade9cf.png

The bins argument allows for choosing either the number of bins as an integer or the bin edges as an array directly:

isi_distribution = nap.compute_isi_distribution(
    data=ts_group, bins=np.linspace(0, 3, 10), epochs=epoch
    )
print(isi_distribution)
            0    1
0.166667  967  277
0.500000  507  189
0.833333  259  163
1.166667  132  110
1.500000   64   75
1.833333   31   50
2.166667   16   44
2.500000    8   17
2.833333    7   18

Hide code cell source

for col in isi_distribution.columns:
    plt.bar(
        isi_distribution.index,
        isi_distribution[col].values,
        width=np.diff(isi_distribution.index).mean(),
        alpha=0.5,
        label=col,
        align='center',
        edgecolor='none'
    )
plt.xlabel("log ISI (s)")
plt.ylabel("Count")
plt.legend(title="Unit")
plt.show()
../_images/555769a50d5b020e767508abe4fb15ed927be39e178d963e68addfa5e8955165.png

The log_scale argument allows for applying the log-transform to the ISIs:

isi_distribution = nap.compute_isi_distribution(
    data=ts_group, bins=10, log_scale=True, epochs=epoch
    )
print(isi_distribution)
             0    1
-8.987100    1    0
-7.831722    1    1
-6.676344    8    1
-5.520966   17    9
-4.365588   66   18
-3.210210  175   36
-2.054831  448  137
-0.899453  818  308
 0.255925  439  396
 1.411303   26   93

Hide code cell source

for col in isi_distribution.columns:
    plt.bar(
        isi_distribution.index,
        isi_distribution[col].values,
        width=np.diff(isi_distribution.index).mean(),
        alpha=0.5,
        label=col,
        align='center',
        edgecolor='none'
    )
plt.xlabel("log ISI (s)")
plt.ylabel("Count")
plt.legend(title="Unit")
plt.show()
../_images/b5b3cbc7cde84e55ae6762e9bbba478403128595657f56960249ade726d6064e.png