SpikeInterface / spikeinterface

A Python-based module for creating flexible and robust spike sorting pipelines.
https://spikeinterface.readthedocs.io
MIT License
531 stars 188 forks source link

Fix a cross-band interpolation bug, and allow time_vector in interpolate_motion #3517

Open cwindolf opened 2 weeks ago

cwindolf commented 2 weeks ago

Hi all, this PR addresses two issues related to motion interpolation, specifically estimating motion in the LFP band and applying the correction to the AP band.

I also added a small test for cross-band registration. The idea was to generate an "LFP" and an "AP" recording, which are zeros except for a channel of 1s which jumps after 3s. Motion is estimated from the LFP recording and then used to interpolate the AP recording, and we assert that the result has no drift at all.

Interestingly, since the LFP recording's time bins are treated as bin centers by interpolate_motion but as left bin edges by time_to_sample_index(), there is a half-bin offset between the motion and the recording which I had to account for in this test. But, I actually think this is the right way to go? 2 reasons:

(And, to @DaohanZhang: this PR together with a recent dredge repo commit (https://github.com/evarol/dredge/commit/5cfe179f5896de859fc4b658bd5330f499dd57c8) should fix your issue, if you'd like to test them. I've updated the demo notebook there to reflect the latest updates -- dredge's understanding of spikeinterface's time handling needed to be updated.)

cwindolf commented 1 week ago

Hi @samuelgarcia -- updated this PR with the stuff we talked about. To recap: we need the interpolation time bin edges to interpolate traces, which we were recomputing over and over for each interpolation chunk. Now they are cached. They are cached in the motion object, but also in the interpolated recording object if for example a user specifies their own interpolation time bin size.

I also added a small thing to vectorize time_to_sample_index, flagged in a comment above. If that feels outside the scope here I'm happy to put it in another PR!

cwindolf commented 18 hours ago

Thanks a ton for the review @JoeZiminski ! I think I've made changes corresponding to all of your comments. No clue why some of my comments got duplicated... that's what I get for trying out this github code spaces thing!

samuelgarcia commented 2 hours ago

This is OK for me.