Add read_kilosort4_motion function#4518
Add read_kilosort4_motion function#4518alejoe91 wants to merge 18 commits intoSpikeInterface:mainfrom
read_kilosort4_motion function#4518Conversation
galenlynch
left a comment
There was a problem hiding this comment.
This is great! This introduces two unrelated things: (1) a new read_kilosort4_motion helper that reconstructs an SI Motion object from KS4's ops.npy (yblk, dshift, batch_size, fs); (2) closes the kilosort file logger to fix #4470 (Windows file lock). However, it seems to have some bugs, and doesn't yet include tests for the added code.
Bugs
-
npnot imported at module level. The new function usesnp.load,np.linspace,np.arange, butimport numpy as npin this file only exists locally inside_setup_recording_arguments(line 173). Callingread_kilosort4_motionas-is will raiseNameError. Needs a module-levelimport numpy as np. -
np.linspace(t_start + t_bin/2, t_end - t_bin/2)is missingnum=— defaults to 50 samples regardless ofdisplacement.shape[0]. Theelsebranch gets the count right vianp.arange(displacement.shape[0]); theif recording is not Nonebranch silently builds a 50-long array and the interpolator will be garbage whenever the session doesn't happen to have 50 batches. Should benp.linspace(..., num=displacement.shape[0]). -
displacement = dshift + yblkis wrong. In KS4,dshift = imin * ops['binning_depth']is the estimated shift per batch per block (shape(n_batches, n_blocks)), andyblkis the y-center of each block; both are assigned into ops at datashift.py:214-215. SI'sMotion.displacementis the shift, not shifted position — see the interpolator inget_displacement_at_time_and_depth. Addingyblkinjects a per-column offset equal to the block depth — the resulting "displacement" would be hundreds of microns instead of the few-µm drift. Should bedisplacement = dshift;spatial_bins_um = yblkis already correct.
Could you add a unit test with a synthetic ops.npy? That would catch (1) and (2) immediately
Other notes
- Bare
except Exceptionfor the yblk/dshift check is lax —KeyErrorwould be more informative. - Logger cleanup looks fine; matches kilosort's own
close_loggerpattern. - Minor: stray double blank line after
from packaging import version.
|
Thanks @galenlynch! All fixed in the latest commit. I also added a comment to the |
and fix #4470