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  0.9950  1.09
-0.8  1.0350  1.00
-0.7  0.9425  1.08
-0.6  0.9800  0.83
-0.5  0.9750  0.89
-0.4  1.0725  0.94
-0.3  1.0325  1.09
-0.2  0.9525  1.07
-0.1  1.0475  1.07
 0.0  0.0000  0.00
 0.1  1.0475  1.07
 0.2  0.9525  1.07
 0.3  1.0325  1.09
 0.4  1.0725  0.94
 0.5  0.9750  0.89
 0.6  0.9800  0.83
 0.7  0.9425  1.08
 0.8  1.0350  1.00
 0.9  0.9950  1.09

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.995
-0.8  0.905
-0.7  0.875
-0.6  0.995
-0.5  1.005
-0.4  1.025
-0.3  0.990
-0.2  0.900
-0.1  0.945
 0.0  1.045
 0.1  0.955
 0.2  0.885
 0.3  1.120
 0.4  0.935
 0.5  0.890
 0.6  0.980
 0.7  1.030
 0.8  1.030
 0.9  0.970

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  2.298851  0.000000
-0.7  0.000000  0.000000
-0.6  0.000000  0.000000
-0.5  0.000000  0.000000
-0.4  0.000000  0.000000
-0.3  2.298851  0.000000
-0.2  0.000000  0.000000
-0.1  4.597701  0.000000
 0.0  0.000000  0.000000
 0.1  0.000000  0.000000
 0.2  0.000000  3.703704
 0.3  0.000000  0.000000
 0.4  2.298851  0.000000
 0.5  0.000000  0.000000
 0.6  0.000000  7.407407
 0.7  0.000000  0.000000
 0.8  2.298851  0.000000
 0.9  0.000000  3.703704

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.343467  1493  472
1.029738   388  272
1.716009    91  139
2.402279    18   61
3.088550     9   23
3.774821     0   18
4.461091     0    6
5.147362     0    6
5.833633     0    1
6.519904     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/94d530537c95adb2848bdb184565d64dd861d68a4dd2f4d83783314d8307e45a.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  289
0.500000  507  177
0.833333  246  146
1.166667  153  116
1.500000   54   87
1.833333   41   60
2.166667   15   37
2.500000    6   24
2.833333    6   15

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/f86d3d862367bb6c5e571f56b65f62678f5085ac664ac8497e067c128d612880.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.513747    3    1
-6.520073    4    1
-5.526400   17    3
-4.532727   53   10
-3.539053  112   26
-2.545380  280   86
-1.551706  523  173
-0.558033  689  290
 0.435640  308  340
 1.429314   10   69

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/0b1509833fc34f44c0946a454c7a4744bd43df1ea835460f5e4c4716aef78889.png