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 specify 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.9625  0.85
-0.8  0.9450  1.06
-0.7  0.9000  0.97
-0.6  1.0200  0.87
-0.5  1.0150  1.19
-0.4  0.9975  1.06
-0.3  1.0575  0.92
-0.2  0.9925  1.07
-0.1  1.0075  1.06
 0.0  0.0000  0.00
 0.1  1.0075  1.06
 0.2  0.9925  1.07
 0.3  1.0575  0.92
 0.4  0.9975  1.06
 0.5  1.0150  1.19
 0.6  1.0200  0.87
 0.7  0.9000  0.97
 0.8  0.9450  1.06
 0.9  0.9625  0.85

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.080
-0.8  0.960
-0.7  0.995
-0.6  0.985
-0.5  0.980
-0.4  0.990
-0.3  1.055
-0.2  0.960
-0.1  0.890
 0.0  0.920
 0.1  0.980
 0.2  0.950
 0.3  1.130
 0.4  1.015
 0.5  0.995
 0.6  0.945
 0.7  1.145
 0.8  1.110
 0.9  0.945

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  4.545455  0.000000
-0.8  0.000000  0.000000
-0.7  0.000000  0.000000
-0.6  1.515152  0.000000
-0.5  1.515152  0.000000
-0.4  1.515152  0.000000
-0.3  0.000000  0.000000
-0.2  0.000000  2.898551
-0.1  0.000000  2.898551
 0.0  0.000000  0.000000
 0.1  0.000000  0.000000
 0.2  0.000000  0.000000
 0.3  0.000000  5.797101
 0.4  1.515152  0.000000
 0.5  1.515152  0.000000
 0.6  0.000000  0.000000
 0.7  1.515152  0.000000
 0.8  0.000000  0.000000
 0.9  1.515152  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.639466   1822  719
1.916415    166  211
3.193364     10   49
4.470313      1   11
5.747262      0    7
7.024211      0    1
8.301160      0    0
9.578109      0    0
10.855058     0    0
12.132007     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/d33abbbf205b97a7d4f67dc45f66718b8a789a6b7fff1aded251b53f9bc22428.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  1001  279
0.500000   475  214
0.833333   238  123
1.166667   129  115
1.500000    80   83
1.833333    47   63
2.166667    14   29
2.500000     6   26
2.833333     5   17

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/7b9ebc28963e0e3aca1bd704e005166f7bdf1b33d2346e2537e8d8857d193795.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
-6.443518   14    2
-5.497133   20    3
-4.550749   47   10
-3.604364  110   26
-2.657979  208   58
-1.711595  489  149
-0.765210  656  281
 0.181175  417  335
 1.127559   38  123
 2.073944    0   12

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