21  Tuning curves

21.1 Simulated head direction cells

import pandas as pd
import pynapple as nap
import numpy as np
from scipy.ndimage import gaussian_filter1d

N = 6 # Number of neurons
bins = np.linspace(0, 2*np.pi, 61)
x = np.linspace(-np.pi, np.pi, len(bins)-1)
tmp = np.roll(np.exp(-(1.5*x)**2), (len(bins)-1)//2)
tc = np.array([np.roll(tmp, i*(len(bins)-1)//N) for i in range(N)]).T

tc_1d = pd.DataFrame(index=bins[0:-1], data=tc)

# Feature
T = 10000
dt = 0.01
timestep = np.arange(0, T)*dt
feature = nap.Tsd(
    t=timestep,
    d=gaussian_filter1d(np.cumsum(np.random.randn(T)*0.5), 20)%(2*np.pi)
    )
index = np.digitize(feature, bins)-1

# Spiking activity
count = np.random.poisson(tc[index])>0
tsgroup = nap.TsGroup({i:nap.Ts(timestep[count[:,i]]) for i in range(N)})
epochs = nap.IntervalSet(0, 10)
tsgroup
  Index     rate
-------  -------
      0  15.1461
      1  12.9352
      2  12.485
      3  12.495
      4  13.4154
      5  13.3453

21.2 xarray tuning curve object

tuning_curves = nap.compute_tuning_curves(
    data=tsgroup,
    features=feature,
    bins=120, 
    range=(0, 2*np.pi),
    feature_names=["feature"]
    )
tuning_curves
<xarray.DataArray (unit: 6, feature: 120)> Size: 6kB
array([[61.44578313, 65.55555556, 61.44578313, 56.81818182, 53.33333333,
        55.10204082, 57.14285714, 49.52380952, 48.24561404, 46.72897196,
        36.36363636, 35.45454545, 22.85714286, 24.35897436, 25.        ,
        18.33333333, 14.47368421, 16.66666667,  6.17283951, 10.52631579,
         6.75675676,  6.4516129 ,  1.53846154,  4.70588235,  0.        ,
         3.22580645,  0.        ,  0.        ,  0.        ,  1.25      ,
         1.35135135,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  1.28205128,
         1.35135135,  0.        ,  4.54545455,  0.        ,  0.        ,
         1.05263158,  4.28571429,  3.38983051,  3.75      ,  5.33333333,
...
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  1.20481928,
         0.96153846,  0.        ,  1.07526882,  1.88679245,  0.93457944,
         4.16666667,  2.53164557,  1.12359551, 11.76470588,  4.71698113,
         9.67741935,  6.59340659, 12.5       , 14.47368421, 21.62162162,
        21.79487179, 38.46153846, 27.39726027, 44.73684211, 28.20512821,
        48.64864865, 42.5       , 43.93939394, 55.71428571, 63.63636364,
        52.63157895, 51.42857143, 54.23728814, 58.75      , 69.33333333,
        58.82352941, 55.26315789, 63.63636364, 60.41666667, 59.84848485,
        63.63636364, 50.98039216, 41.02564103, 44.44444444, 33.33333333,
        39.18918919, 45.83333333, 37.96296296, 23.89380531, 18.55670103,
        26.82926829, 17.0212766 , 12.5       , 12.08791209,  3.80952381]])
Coordinates:
  * unit     (unit) int64 48B 0 1 2 3 4 5
  * feature  (feature) float64 960B 0.02618 0.07854 0.1309 ... 6.152 6.205 6.257
Attributes:
    occupancy:  [ 83.  90.  83.  88.  90.  98. 105. 105. 114. 107.  99. 110. ...
    bin_edges:  [array([0.        , 0.05235988, 0.10471976, 0.15707963, 0.209...
    fs:         100.0
    rates:      [15.14151415 12.93129313 12.48124812 12.49124912 13.41134113 ...

21.3 Easy visualisation

import matplotlib.pyplot as plt
tuning_curves.plot.line(x="feature", add_legend=False)
plt.ylabel("firing rate [Hz]")

21.4 Decoding

decoded, proba_feature = nap.decode_bayes(
    tuning_curves=tuning_curves,
    data=tsgroup,
    epochs=epochs,
    sliding_window_size=4,
    bin_size=0.02,
)
decoded
Time (s)
-------------------  -------
0.01                 3.22013
0.03                 3.22013
0.05                 3.22013
0.06999999999999999  2.95833
0.09                 2.69653
0.11                 2.69653
0.13                 2.69653
...
9.87                 5.52397
9.89                 5.31453
9.91                 4.84329
9.93                 4.94801
9.95                 5.36689
9.97                 5.57633
9.99                 5.78577
dtype: float64, shape: (500,)