Tuning curves#
Pynapple can compute 1 dimension tuning curves (for example firing rate as a function of angular direction) and 2 dimension tuning curves ( for example firing rate as a function of position). It can also compute average firing rate for different epochs (for example firing rate for different epochs of stimulus presentation).
Important
If you are using calcium imaging data with the activity of the cell as a continuous transient, the function to call ends with _continuous
for continuous time series (e.g. compute_1d_tuning_curves_continuous
).
Show code cell content
import pynapple as nap
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pprint import pprint
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(style="ticks", palette="colorblind", font_scale=1.5, rc=custom_params)
Show code cell content
group = {
0: nap.Ts(t=np.sort(np.random.uniform(0, 100, 10))),
1: nap.Ts(t=np.sort(np.random.uniform(0, 100, 20))),
2: nap.Ts(t=np.sort(np.random.uniform(0, 100, 30))),
}
tsgroup = nap.TsGroup(group)
from epochs#
The epochs should be stored in a dictionnary :
dict_ep = {
"stim0": nap.IntervalSet(start=0, end=20),
"stim1":nap.IntervalSet(start=30, end=70)
}
nap.compute_discrete_tuning_curves
takes a TsGroup
for spiking activity and a dictionary of epochs.
The output is a pandas DataFrame where each column is a unit in the TsGroup
and each row is one IntervalSet
type.
The value is the mean firing rate of the neuron during this set of intervals.
mean_fr = nap.compute_discrete_tuning_curves(tsgroup, dict_ep)
pprint(mean_fr)
0 1 2
stim0 0.0 0.15 0.250
stim1 0.1 0.20 0.325
from timestamps activity#
1-dimension tuning curves#
Show code cell content
from scipy.ndimage import gaussian_filter1d
# Fake Tuning curves
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)
generative_tc = np.array([np.roll(tmp, i*(len(bins)-1)//N) for i in range(N)]).T
# Feature
T = 50000
dt = 0.002
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(generative_tc[index])>0
tsgroup = nap.TsGroup(
{i:nap.Ts(timestep[count[:,i]]) for i in range(N)},
time_support = nap.IntervalSet(0, 100)
)
Mandatory arguments are TsGroup
, Tsd
(or TsdFrame
with 1 column only)
and nb_bins
for number of bins of the tuning curves.
If an IntervalSet
is passed with ep
, everything is restricted to ep
otherwise the time support of the feature is used.
The min and max of the tuning curve is by default the min and max of the feature. This can be tweaked with the argument minmax
.
The output is a pandas DataFrame. Each column is a unit from TsGroup
argument. The index of the DataFrame carries the center of the bin in feature space.
tuning_curve = nap.compute_1d_tuning_curves(
group=tsgroup,
feature=feature,
nb_bins=120,
minmax=(0, 2*np.pi)
)
print(tuning_curve)
0 1 2 3 4 5
0.026180 319.803350 50.762437 0.0 0.0 0.000000 32.995584
0.078540 306.152695 41.371986 0.0 0.0 0.000000 24.823191
0.130900 310.225183 79.077007 0.0 0.0 0.000000 13.382263
0.183260 304.026181 76.634699 0.0 0.0 0.000000 18.844598
0.235619 294.579535 107.237287 0.0 0.0 0.000000 7.752093
... ... ... ... ... ... ...
6.047566 293.254810 11.603608 0.0 0.0 0.000000 126.584810
6.099926 321.519433 17.730851 0.0 0.0 2.364113 70.923404
6.152286 317.863500 25.000500 0.0 0.0 0.000000 67.858500
6.204645 315.551555 31.323132 0.0 0.0 0.000000 47.564756
6.257005 320.299805 31.785477 0.0 0.0 0.000000 52.568289
[120 rows x 6 columns]
plt.figure()
plt.plot(tuning_curve)
plt.xlabel("Feature space")
plt.ylabel("Firing rate (Hz)")
plt.show()
Internally, the function is calling the method value_from
which maps a timestamps
to its closest value in time from a Tsd
object.
It is then possible to validate the tuning curves by displaying the
timestamps as well as their associated values.
Show code cell source
plt.figure()
plt.subplot(121)
plt.plot(tsgroup[3].value_from(feature), 'o')
plt.plot(feature, label="feature")
plt.ylabel("Feature")
plt.xlim(0, 2)
plt.xlabel("Time (s)")
plt.subplot(122)
plt.plot(tuning_curve[3].values, tuning_curve[3].index.values, label="Tuning curve (unit=3)")
plt.xlabel("Firing rate (Hz)")
plt.legend()
plt.show()
2-dimension tuning curves#
Show code cell content
dt = 0.01
T = 10
epoch = nap.IntervalSet(start=0, end=T, time_units="s")
features = np.vstack((np.cos(np.arange(0, T, dt)), np.sin(np.arange(0, T, dt)))).T
features = nap.TsdFrame(
t=np.arange(0, T, dt),
d=features,
time_units="s",
time_support=epoch,
columns=["a", "b"],
)
tsgroup = nap.TsGroup({
0: nap.Ts(t=np.sort(np.random.uniform(0, T, 10))),
1: nap.Ts(t=np.sort(np.random.uniform(0, T, 15))),
2: nap.Ts(t=np.sort(np.random.uniform(0, T, 20))),
}, time_support=epoch)
The group
argument must be a TsGroup
object.
The features
argument must be a 2-columns TsdFrame
object.
nb_bins
can be an int or a tuple of 2 ints.
tcurves2d, binsxy = nap.compute_2d_tuning_curves(
group=tsgroup,
features=features,
nb_bins=(5,5),
minmax=(-1, 1, -1, 1)
)
pprint(tcurves2d)
{0: array([[0. , 2.4691358 , 0. , 1.12359551, 0. ],
[0. , nan, nan, nan, 1.13636364],
[2.5 , nan, nan, nan, 1.2345679 ],
[0. , nan, nan, nan, 1.13636364],
[7.14285714, 0. , 0. , 1.13636364, 0. ]]),
1: array([[0. , 2.4691358 , 0. , 2.24719101, 3.50877193],
[4.44444444, nan, nan, nan, 1.13636364],
[2.5 , nan, nan, nan, 1.2345679 ],
[0. , nan, nan, nan, 0. ],
[0. , 4.44444444, 0. , 1.13636364, 1.75438596]]),
2: array([[0. , 2.4691358 , 3.75 , 0. , 1.75438596],
[0. , nan, nan, nan, 0. ],
[0. , nan, nan, nan, 2.4691358 ],
[4.54545455, nan, nan, nan, 1.13636364],
[3.57142857, 2.22222222, 6.55737705, 2.27272727, 1.75438596]])}
/home/runner/.local/lib/python3.10/site-packages/pynapple/process/tuning_curves.py:269: RuntimeWarning: invalid value encountered in divide
count = count / occupancy
tcurves2d
is a dictionnary with each key a unit in TsGroup
. binsxy
is a numpy array representing the centers of the bins and is useful for plotting tuning curves. Bins that have never been visited by the feature have been assigned a NaN value.
Checking the accuracy of the tuning curves can be bone by displaying the spikes aligned to the features with the function value_from
which assign to each spikes the corresponding features value for unit 0.
ts_to_features = tsgroup[0].value_from(features)
print(ts_to_features)
Time (s) a b
---------- -------- --------
0.993361 0.54869 0.83603
1.37005 0.19945 0.97991
2.09835 -0.50485 0.86321
2.64595 -0.88158 0.47203
3.50678 -0.9329 -0.36013
4.71627 0.00761 -0.99997
5.45129 0.67252 -0.74008
5.49612 0.70867 -0.70554
6.58373 0.95627 0.29248
9.64778 -0.97474 -0.22332
dtype: float64, shape: (10, 2)
tsgroup[0]
which is a Ts
object has been transformed to a TsdFrame
object with each timestamps (spike times) being associated with a features value.
Show code cell source
plt.figure()
plt.subplot(121)
plt.plot(features["b"], features["a"], label="features")
plt.plot(ts_to_features["b"], ts_to_features["a"], "o", color="red", markersize=4, label="spikes")
plt.xlabel("feature b")
plt.ylabel("feature a")
[plt.axvline(b, linewidth=0.5, color='grey') for b in np.linspace(-1, 1, 6)]
[plt.axhline(b, linewidth=0.5, color='grey') for b in np.linspace(-1, 1, 6)]
plt.subplot(122)
extents = (
np.min(features["a"]),
np.max(features["a"]),
np.min(features["b"]),
np.max(features["b"]),
)
plt.imshow(tcurves2d[0],
origin="lower", extent=extents, cmap="viridis",
aspect='auto'
)
plt.title("Tuning curve unit 0")
plt.xlabel("feature b")
plt.ylabel("feature a")
plt.grid(False)
plt.colorbar()
plt.tight_layout()
plt.show()
from continuous activity#
Tuning curves compute with the following functions are usually made with data from calcium imaging activities.
1-dimension tuning curves#
Show code cell content
from scipy.ndimage import gaussian_filter1d
# Fake Tuning curves
N = 3 # 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)
generative_tc = np.array([np.roll(tmp, i*(len(bins)-1)//N) for i in range(N)]).T
# Feature
T = 50000
dt = 0.002
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
tmp = generative_tc[index]
tmp = tmp + np.random.randn(*tmp.shape)*1
# Calcium activity
tsdframe = nap.TsdFrame(
t=timestep,
d=tmp
)
Arguments are TsdFrame
(for example continuous calcium data), Tsd
or TsdFrame
for the 1-d feature and nb_bins
for the number of bins.
tuning_curves = nap.compute_1d_tuning_curves_continuous(
tsdframe=tsdframe,
feature=feature,
nb_bins=120,
minmax=(0, 2*np.pi)
)
print(tuning_curves)
0 1 2
0.026180 1.036999 0.017706 -0.072927
0.078540 0.995681 0.048372 -0.014222
0.130900 0.812014 0.022531 0.044231
0.183260 0.929921 0.003504 0.003414
0.235619 0.753769 -0.017999 -0.082787
... ... ... ...
6.047566 0.813010 0.014774 -0.121134
6.099926 1.004482 0.034686 -0.042553
6.152286 0.974599 0.028493 -0.048718
6.204645 0.970381 -0.033318 -0.018441
6.257005 1.085766 -0.024711 -0.027156
[120 rows x 3 columns]
plt.figure()
plt.plot(tuning_curves)
plt.xlabel("Feature space")
plt.ylabel("Mean activity")
plt.show()
2-dimension tuning curves#
Show code cell content
dt = 0.01
T = 10
epoch = nap.IntervalSet(start=0, end=T, time_units="s")
features = np.vstack((np.cos(np.arange(0, T, dt)), np.sin(np.arange(0, T, dt)))).T
features = nap.TsdFrame(
t=np.arange(0, T, dt),
d=features,
time_units="s",
time_support=epoch,
columns=["a", "b"],
)
# Calcium activity
tsdframe = nap.TsdFrame(
t=timestep,
d=np.random.randn(len(timestep), 2)
)
Arguments are TsdFrame
(for example continuous calcium data), Tsd
or TsdFrame
for the 1-d feature and nb_bins
for the number of bins.
tuning_curves, xy = nap.compute_2d_tuning_curves_continuous(
tsdframe=tsdframe,
features=features,
nb_bins=5,
)
print(tuning_curves)
{0: array([[-0.04981698, 0.05013693, -0.08138478, 0.03452544, -0.01910623],
[ 0.00180448, nan, nan, nan, -0.09755402],
[ 0.01222257, nan, nan, nan, -0.01053156],
[-0.07019317, nan, nan, nan, -0.01384285],
[-0.04829488, -0.0539769 , 0.05153256, -0.00983982, -0.01791971]]), 1: array([[-0.1854611 , 0.11144342, 0.01841171, -0.00722098, -0.00123917],
[ 0.08469355, nan, nan, nan, -0.05694232],
[-0.00079209, nan, nan, nan, -0.0023015 ],
[ 0.14415005, nan, nan, nan, 0.01315794],
[ 0.06257169, 0.03123988, -0.12721245, -0.00845645, 0.01941917]])}
/home/runner/.local/lib/python3.10/site-packages/numpy/core/fromnumeric.py:3504: RuntimeWarning: Mean of empty slice.
return _methods._mean(a, axis=axis, dtype=dtype,
/home/runner/.local/lib/python3.10/site-packages/numpy/core/_methods.py:121: RuntimeWarning: invalid value encountered in divide
ret = um.true_divide(
plt.figure()
plt.subplot(121)
plt.plot(features["b"], features["a"], label="features")
plt.xlabel("feature b")
plt.ylabel("feature a")
[plt.axvline(b, linewidth=0.5, color='grey') for b in np.linspace(-1, 1, 6)]
[plt.axhline(b, linewidth=0.5, color='grey') for b in np.linspace(-1, 1, 6)]
plt.subplot(122)
extents = (
np.min(features["a"]),
np.max(features["a"]),
np.min(features["b"]),
np.max(features["b"]),
)
plt.imshow(tuning_curves[0],
origin="lower", extent=extents, cmap="viridis",
aspect='auto'
)
plt.title("Tuning curve unit 0")
plt.xlabel("feature b")
plt.ylabel("feature a")
plt.grid(False)
plt.colorbar()
plt.tight_layout()
plt.show()