pynapple-org / pynapple

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

concatenating Tsd #366

Open samdeoxys1 opened 2 days ago

samdeoxys1 commented 2 days ago

In concatenating the Tsd, why having the restriction that

image

? Maybe this is an unlikely use case: I'm trying to do something like: doing some shuffle of spike times for spikes that happen during the small window of the ripple, but keep spikes outside of that small window (within an extended window) unchanged. I then form a Tsd for the shuffled spikes, as well as a Tsd for the non-shuffled spikes outside of the small window. The second Tsd include before and after the small window, so the two Tsds cannot be concatenated. But it would be nice it they could be, and pynapple just sort the final time automatically. What do you think?

sjvenditto commented 1 day ago

Concatenating Tsd objects inherits functionality from numpy, which doesn't do any sorting; therefore, to match numpy's definition, we also don't do any sorting. This means we have to restrict concatenating Tsd objects with out-of-order or overlapping time indexes so that the output is sensible. We can theoretically extend it to allow post-concatenation sorting, but it's unclear how complex it may turn out under-the-hood with added edge cases (like duplicate time indices, for example). But we'll definitely consider it in future development!

In the meantime for your case, you can solve your concatenation issue by splitting the large window into two Tsd objects, one for before the small window and one for after. Then you can concatenate the three Tsd objects. For example:

tsd1 = nap.Tsd(t=[3, 4], d=[1, 1]) # middle
tsd2 = nap.Tsd(t=[1, 2, 5, 6], d=[2, 2, 2, 2]) # edges

np.concatenate((tsd1, tsd2))  # error

tsd2_1, tsd2_2 = tsd2.split([2])  # split at index 2
np.concatenate((tsd2_1, tsd1, tsd2_2))  # no error