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.0350  0.97
-0.8  0.9525  0.96
-0.7  1.0225  0.98
-0.6  0.8500  0.88
-0.5  1.0300  1.05
-0.4  0.9800  1.01
-0.3  0.9725  0.97
-0.2  1.0375  0.84
-0.1  0.9825  0.94
 0.0  0.0000  0.00
 0.1  0.9825  0.94
 0.2  1.0375  0.84
 0.3  0.9725  0.97
 0.4  0.9800  1.01
 0.5  1.0300  1.05
 0.6  0.8500  0.88
 0.7  1.0225  0.98
 0.8  0.9525  0.96
 0.9  1.0350  0.97

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.070
-0.8  1.015
-0.7  0.990
-0.6  1.110
-0.5  0.970
-0.4  1.020
-0.3  1.005
-0.2  1.020
-0.1  1.065
 0.0  0.965
 0.1  0.990
 0.2  1.055
 0.3  0.965
 0.4  0.895
 0.5  1.040
 0.6  1.050
 0.7  1.210
 0.8  0.910
 0.9  1.040

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  3.030303  0.000000
-0.7  0.000000  0.000000
-0.6  1.515152  3.174603
-0.5  0.000000  0.000000
-0.4  0.000000  0.000000
-0.3  1.515152  0.000000
-0.2  0.000000  0.000000
-0.1  0.000000  0.000000
 0.0  0.000000  0.000000
 0.1  1.515152  0.000000
 0.2  1.515152  0.000000
 0.3  0.000000  0.000000
 0.4  1.515152  0.000000
 0.5  0.000000  0.000000
 0.6  1.515152  0.000000
 0.7  1.515152  3.174603
 0.8  1.515152  6.349206
 0.9  0.000000  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.299223  1406  450
0.896810   414  241
1.494398   124  141
2.091986    38   79
2.689574    14   45
3.287162     2   19
3.884749     1    6
4.482337     0    5
5.079925     0    7
5.677513     0    6

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/d903561a0aed0d2b1c640f9a5436560721d4fcaccaa1b6f63dca98a05464b474.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  980  276
0.500000  486  209
0.833333  261  143
1.166667  125  101
1.500000   73   80
1.833333   36   51
2.166667   20   45
2.500000   12   31
2.833333    3   20

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/b4629b700ba32a45a9db95633c772661247de4b61f03d6d69ef1e794e8caae21.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
-7.277712    3    0
-6.323447    6    2
-5.369182   24    3
-4.414918   51   15
-3.460653  112   35
-2.506388  260   68
-1.552124  535  158
-0.597859  662  302
 0.356406  327  319
 1.310670   19   97

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/a80df3cc4494eb4e7814db47d5b412bf099b5c017e4e30733f1eac3dd34147d2.png