pynapple-org / pynapple

PYthon Neural Analysis Package :pineapple:
https://pynapple.org/
MIT License
279 stars 63 forks source link

Incorrect tuning curves for unsampled bins #334

Closed wulfdewolf closed 1 month ago

wulfdewolf commented 1 month ago

At the moment, when a particular bin of a feature is unsampled, the tuning curve functions will set the average firing rate for that bin to zero. This seems incorrect? We do not know if there is no firing for that bin, hence, does it not make more sense to set it to NaN?

gviejo commented 1 month ago

The tuning curves are normalized by occupancy. NaNs represents bin that have never been visited. 0 indicate no spikes. You can see an example here or you can run this code :

import numpy as np
import pynapple as nap
import matplotlib.pyplot as plt

dt = 0.1
features = np.vstack((np.cos(np.arange(0, 1000, dt)), np.sin(np.arange(0, 1000, dt)))).T
features = nap.TsdFrame(t=np.arange(0, 1000, dt),d=features)

ts_group = nap.TsGroup({0:nap.Ts(t=[0, 500, 999])})

tcurves2d, binsxy = nap.compute_2d_tuning_curves(
    group=ts_group, features=features, nb_bins=10
)

plt.figure(figsize=(12, 6))
plt.subplot(121)
plt.plot(features[:,0], features[:,1])
tmp=ts_group[0].value_from(features)
plt.plot(tmp[:,0], tmp[:,1], 'o')
plt.title("Feature + Spikes")
plt.subplot(122)
extents = (
    np.min(features[:,1]),
    np.max(features[:,1]),
    np.min(features[:,0]),
    np.max(features[:,0]),
)
plt.imshow(tcurves2d[0].T, origin="lower", extent=extents, cmap="viridis")
plt.title("Tuning curve")
plt.show()
wulfdewolf commented 1 month ago

I understand, and it seems to be so for 2D tuning curves. However, when I tried yesterday for 1D tuning curves:

138    for k in group_value:
139        count, _ = np.histogram(group_value[k].values, bins)
140        count = count / occupancy
141        count[np.isnan(count)] = 0.0
142        tuning_curves[k] = count
143        tuning_curves[k] = count * feature.rate

line 141 seems to be setting NaNs to 0.

In the 2D version, this line is commented.

gviejo commented 1 month ago

Good catch. In fact, it's quite rare to have a 1d feature that does not span its whole extent. This is high priority so I will bump a new version as soon as possible. Leaving it open until it is resolved.

gviejo commented 1 month ago

New version 0.7.1 is out with a fix to this issue. Closing it.