Skip to content

Commit 67b2135

Browse files
authored
Merge pull request #3659 from samuelgarcia/debug_motion_pickle
Multi segment handling in ensure_time_bins
2 parents cbe5471 + a0d658d commit 67b2135

2 files changed

Lines changed: 73 additions & 20 deletions

File tree

src/spikeinterface/sortingcomponents/motion/motion_utils.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,14 @@ def ensure_time_bins(time_bin_centers_s=None, time_bin_edges_s=None):
587587
Going from centers to edges is done by taking midpoints and padding with the
588588
left and rightmost centers.
589589
590+
To handle multi segment, this function is working both:
591+
* array/array input
592+
* list[array]/list[array] input
593+
590594
Parameters
591595
----------
592-
time_bin_centers_s : None or np.array
593-
time_bin_edges_s : None or np.array
596+
time_bin_centers_s : None or np.array or list[np.array]
597+
time_bin_edges_s : None or np.array or list[np.array]
594598
595599
Returns
596600
-------
@@ -600,17 +604,34 @@ def ensure_time_bins(time_bin_centers_s=None, time_bin_edges_s=None):
600604
raise ValueError("Need at least one of time_bin_centers_s or time_bin_edges_s.")
601605

602606
if time_bin_centers_s is None:
603-
assert time_bin_edges_s.ndim == 1 and time_bin_edges_s.size >= 2
604-
time_bin_centers_s = 0.5 * (time_bin_edges_s[1:] + time_bin_edges_s[:-1])
607+
if isinstance(time_bin_edges_s, list):
608+
# multi segment cas
609+
time_bin_centers_s = []
610+
for be in time_bin_edges_s:
611+
bc, _ = ensure_time_bins(time_bin_centers_s=None, time_bin_edges_s=be)
612+
time_bin_centers_s.append(bc)
613+
else:
614+
# simple segment
615+
assert time_bin_edges_s.ndim == 1 and time_bin_edges_s.size >= 2
616+
time_bin_centers_s = 0.5 * (time_bin_edges_s[1:] + time_bin_edges_s[:-1])
605617

606618
if time_bin_edges_s is None:
607-
time_bin_edges_s = np.empty(time_bin_centers_s.shape[0] + 1, dtype=time_bin_centers_s.dtype)
608-
time_bin_edges_s[[0, -1]] = time_bin_centers_s[[0, -1]]
609-
if time_bin_centers_s.size > 2:
610-
time_bin_edges_s[1:-1] = 0.5 * (time_bin_centers_s[1:] + time_bin_centers_s[:-1])
619+
if isinstance(time_bin_centers_s, list):
620+
# multi segment cas
621+
time_bin_edges_s = []
622+
for bc in time_bin_centers_s:
623+
_, be = ensure_time_bins(time_bin_centers_s=bc, time_bin_edges_s=None)
624+
time_bin_edges_s.append(be)
625+
else:
626+
# simple segment
627+
time_bin_edges_s = np.empty(time_bin_centers_s.shape[0] + 1, dtype=time_bin_centers_s.dtype)
628+
time_bin_edges_s[[0, -1]] = time_bin_centers_s[[0, -1]]
629+
if time_bin_centers_s.size > 2:
630+
time_bin_edges_s[1:-1] = 0.5 * (time_bin_centers_s[1:] + time_bin_centers_s[:-1])
611631

612632
return time_bin_centers_s, time_bin_edges_s
613633

614634

635+
615636
def ensure_time_bin_edges(time_bin_centers_s=None, time_bin_edges_s=None):
616637
return ensure_time_bins(time_bin_centers_s, time_bin_edges_s)[1]

src/spikeinterface/sortingcomponents/motion/tests/test_motion_interpolation.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,26 @@
1010
interpolate_motion_on_traces,
1111
)
1212
from spikeinterface.sortingcomponents.tests.common import make_dataset
13-
13+
from spikeinterface.core import generate_ground_truth_recording
1414

1515
def make_fake_motion(rec):
1616
# make a fake motion object
17-
duration = rec.get_total_duration()
17+
1818
locs = rec.get_channel_locations()
19-
temporal_bins = np.arange(0.5, duration - 0.49, 0.5)
2019
spatial_bins = np.arange(locs[:, 1].min(), locs[:, 1].max(), 100)
21-
displacement = np.zeros((temporal_bins.size, spatial_bins.size))
22-
displacement[:, :] = np.linspace(-30, 30, temporal_bins.size)[:, None]
2320

24-
motion = Motion([displacement], [temporal_bins], spatial_bins, direction="y")
21+
displacement = []
22+
temporal_bins = []
23+
for segment_index in range(rec.get_num_segments()):
24+
duration = rec.get_duration(segment_index=segment_index)
25+
seg_time_bins = np.arange(0.5, duration - 0.49, 0.5)
26+
seg_disp = np.zeros((seg_time_bins.size, spatial_bins.size))
27+
seg_disp[:, :] = np.linspace(-30, 30, seg_time_bins.size)[:, None]
28+
29+
temporal_bins.append(seg_time_bins)
30+
displacement.append(seg_disp)
31+
32+
motion = Motion(displacement, temporal_bins, spatial_bins, direction="y")
2533

2634
return motion
2735

@@ -176,7 +184,27 @@ def test_cross_band_interpolation():
176184

177185

178186
def test_InterpolateMotionRecording():
179-
rec, sorting = make_dataset()
187+
# rec, sorting = make_dataset()
188+
189+
# 2 segments
190+
rec, sorting = generate_ground_truth_recording(
191+
durations=[30.0],
192+
sampling_frequency=30000.0,
193+
num_channels=32,
194+
num_units=10,
195+
generate_probe_kwargs=dict(
196+
num_columns=2,
197+
xpitch=20,
198+
ypitch=20,
199+
contact_shapes="circle",
200+
contact_shape_params={"radius": 6},
201+
),
202+
generate_sorting_kwargs=dict(firing_rates=6.0, refractory_period_ms=4.0),
203+
noise_kwargs=dict(noise_levels=5.0, strategy="on_the_fly"),
204+
seed=2205,
205+
)
206+
207+
180208
motion = make_fake_motion(rec)
181209

182210
rec2 = InterpolateMotionRecording(rec, motion, border_mode="force_extrapolate")
@@ -187,15 +215,19 @@ def test_InterpolateMotionRecording():
187215

188216
rec2 = InterpolateMotionRecording(rec, motion, border_mode="remove_channels")
189217
assert rec2.channel_ids.size == 24
190-
for ch_id in (0, 1, 14, 15, 16, 17, 30, 31):
218+
for ch_id in ("0", "1", "14", "15", "16", "17", "30", "31"):
191219
assert ch_id not in rec2.channel_ids
192220

193221
traces = rec2.get_traces(segment_index=0, start_frame=0, end_frame=30000)
194222
assert traces.shape == (30000, 24)
195223

196-
traces = rec2.get_traces(segment_index=0, start_frame=0, end_frame=30000, channel_ids=[3, 4])
224+
traces = rec2.get_traces(segment_index=0, start_frame=0, end_frame=30000, channel_ids=["3", "4"])
197225
assert traces.shape == (30000, 2)
198226

227+
# test dump.load when multi segments
228+
rec2.dump("rec_motion_interp.pickle")
229+
rec3 = sc.load("rec_motion_interp.pickle")
230+
199231
# import matplotlib.pyplot as plt
200232
# import spikeinterface.widgets as sw
201233
# fig, ax = plt.subplots()
@@ -207,7 +239,7 @@ def test_InterpolateMotionRecording():
207239

208240
if __name__ == "__main__":
209241
# test_correct_motion_on_peaks()
210-
test_interpolate_motion_on_traces()
242+
# test_interpolate_motion_on_traces()
211243
# test_interpolation_simple()
212-
# test_InterpolateMotionRecording()
213-
test_cross_band_interpolation()
244+
test_InterpolateMotionRecording()
245+
# test_cross_band_interpolation()

0 commit comments

Comments
 (0)