11import warnings
22
33import numpy as np
4- import spikeinterface . core as sc
4+ import pickle
55
6+ from spikeinterface import NumpyRecording , load
7+ from spikeinterface .core import generate_ground_truth_recording
68from spikeinterface .core .motion import Motion
9+ from spikeinterface .core .testing import check_recordings_equal
710from spikeinterface .sortingcomponents .motion .motion_interpolation import (
811 InterpolateMotionRecording ,
912 correct_motion_on_peaks ,
1013 interpolate_motion ,
1114 interpolate_motion_on_traces ,
1215)
1316from spikeinterface .sortingcomponents .tests .common import make_dataset
14- from spikeinterface .core import generate_ground_truth_recording
1517
1618
1719def make_fake_motion (rec ):
@@ -36,12 +38,49 @@ def make_fake_motion(rec):
3638 return motion
3739
3840
41+ def test_motion_object_interpolators (tmp_path ):
42+ rec , sorting = make_dataset ()
43+ motion = make_fake_motion (rec )
44+
45+ # serialize with pickle after interpolation fit
46+ motion .make_interpolators ()
47+ assert motion .interpolators is not None
48+ motion2 = pickle .loads (pickle .dumps (motion ))
49+ assert motion2 .interpolators is not None
50+
51+ # to/from dict
52+ motion2 = Motion .from_dict (motion .to_dict ())
53+ assert motion == motion2
54+ # serialization to/from dict loses interpolators
55+ assert motion2 .interpolators is None
56+ motion2 .make_interpolators ()
57+
58+ # save/load to folder
59+ folder = tmp_path / "motion_saved"
60+ motion .save (folder )
61+ motion3 = Motion .load (folder )
62+ # save/load to folder loses interpolators
63+ assert motion3 .interpolators is None
64+
65+ # do interpolate
66+ displacement = motion .get_displacement_at_time_and_depth ([2 , 4.4 , 11 ], [120.0 , 80.0 , 150.0 ])
67+ displacement2 = motion2 .get_displacement_at_time_and_depth ([2 , 4.4 , 11 ], [120.0 , 80.0 , 150.0 ])
68+ displacement3 = motion3 .get_displacement_at_time_and_depth ([2 , 4.4 , 11 ], [120.0 , 80.0 , 150.0 ])
69+ # print(displacement)
70+ assert displacement .shape [0 ] == 3
71+ # check interpolators reload
72+ np .testing .assert_array_equal (displacement , displacement2 )
73+ np .testing .assert_array_equal (displacement , displacement3 )
74+
75+ # interpolate grid
76+ displacement = motion .get_displacement_at_time_and_depth ([2 , 4.4 , 11 , 15 , 19 ], [150.0 , 80.0 ], grid = True )
77+ assert displacement .shape == (2 , 5 )
78+
79+
3980def test_correct_motion_on_peaks ():
4081 rec , sorting = make_dataset ()
4182 peaks = sorting .to_spike_vector ()
42- print (peaks .dtype )
4383 motion = make_fake_motion (rec )
44- # print(motion)
4584
4685 # fake locations
4786 peak_locations = np .zeros ((peaks .size ), dtype = [("x" , "float32" ), ("y" , "float" )])
@@ -98,7 +137,7 @@ def test_interpolation_simple():
98137 num_chans_drifted = num_chans_orig + num_chans_orig - 1
99138 traces = np .zeros ((n_samples , num_chans_drifted ), dtype = "float32" )
100139 traces [:, :num_chans_orig ] = np .eye (num_chans_orig )
101- rec = sc . NumpyRecording (traces , sampling_frequency = 1 )
140+ rec = NumpyRecording (traces , sampling_frequency = 1 )
102141 rec .set_dummy_probe_from_locations (np .c_ [np .zeros (num_chans_drifted ), np .arange (num_chans_drifted )])
103142
104143 true_motion = Motion (np .arange (n_samples )[:, None ], 0.5 + np .arange (n_samples ), np .zeros (1 ))
@@ -157,14 +196,14 @@ def test_cross_band_interpolation():
157196 traces_lfp = np .zeros ((num_samples_lfp , num_chans ))
158197 traces_lfp [: int (t_switch * fs_lfp ), 5 ] = 1.0
159198 traces_lfp [int (t_switch * fs_lfp ) :, 6 ] = 1.0
160- rec_lfp = sc . NumpyRecording (traces_lfp , sampling_frequency = fs_lfp )
199+ rec_lfp = NumpyRecording (traces_lfp , sampling_frequency = fs_lfp )
161200 rec_lfp .set_dummy_probe_from_locations (geom )
162201
163202 # same for AP
164203 traces_ap = np .zeros ((num_samples_ap , num_chans ))
165204 traces_ap [: int (t_switch * fs_ap ) - halfbin_ap_lfp , 5 ] = 1.0
166205 traces_ap [int (t_switch * fs_ap ) - halfbin_ap_lfp :, 6 ] = 1.0
167- rec_ap = sc . NumpyRecording (traces_ap , sampling_frequency = fs_ap )
206+ rec_ap = NumpyRecording (traces_ap , sampling_frequency = fs_ap )
168207 rec_ap .set_dummy_probe_from_locations (geom )
169208
170209 # set times for both, and silence the warning
@@ -227,7 +266,9 @@ def test_InterpolateMotionRecording():
227266
228267 # test dump.load when multi segments
229268 rec2 .dump ("rec_motion_interp.pickle" )
230- rec3 = sc .load ("rec_motion_interp.pickle" )
269+ rec3 = load ("rec_motion_interp.pickle" )
270+
271+ check_recordings_equal (rec2 , rec3 )
231272
232273 # import matplotlib.pyplot as plt
233274 # import spikeinterface.widgets as sw
0 commit comments