From b1f6af36404766d3b4c02bc6714fd0e8bb1ffd4c Mon Sep 17 00:00:00 2001 From: JoeZiminski Date: Thu, 17 Sep 2026 17:39:29 +0100 Subject: [PATCH] Add. --- src/spikeinterface/core/numpyextractors.py | 3 +++ .../core/tests/test_numpy_extractors.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/spikeinterface/core/numpyextractors.py b/src/spikeinterface/core/numpyextractors.py index b58a98eddb..fe8c22a10c 100644 --- a/src/spikeinterface/core/numpyextractors.py +++ b/src/spikeinterface/core/numpyextractors.py @@ -304,6 +304,9 @@ def from_sorting(source_sorting: BaseSorting, with_metadata=False, copy_spike_ve sorting = NumpySorting(spike_vector, source_sorting.get_sampling_frequency(), source_sorting.unit_ids.copy()) if source_sorting.has_recording(): sorting._recording = source_sorting._recording + for source_segment, target_segment in zip(source_sorting._sorting_segments, sorting._sorting_segments): + target_segment._t_start = source_segment._t_start + target_segment._native_t_start = source_segment._native_t_start if with_metadata: source_sorting.copy_metadata(sorting) return sorting diff --git a/src/spikeinterface/core/tests/test_numpy_extractors.py b/src/spikeinterface/core/tests/test_numpy_extractors.py index daaf99d72a..d0a8c5be70 100644 --- a/src/spikeinterface/core/tests/test_numpy_extractors.py +++ b/src/spikeinterface/core/tests/test_numpy_extractors.py @@ -90,6 +90,22 @@ def test_NumpySorting(setup_NumpyRecording): sorting = NumpySorting.from_sorting(other_sorting) # print(sorting) + # Verify recording segment offsets and shifted sorting times survive conversion to NumpySorting. + recording = generate_recording(num_channels=2, durations=[1.0, 1.0]) + recording.shift_times(shift=5.0, segment_index=0) + recording.shift_times(shift=120.0, segment_index=1) + other_sorting.register_recording(recording) + other_sorting.shift_times(2.0) + sorting_with_times = NumpySorting.from_sorting(other_sorting) + + for segment_index in range(other_sorting.get_num_segments()): + assert sorting_with_times.get_start_time(segment_index) == other_sorting.get_start_time(segment_index) + for unit_id in other_sorting.unit_ids: + assert np.array_equal( + sorting_with_times.get_unit_spike_train(unit_id, segment_index=segment_index, return_times=True), + other_sorting.get_unit_spike_train(unit_id, segment_index=segment_index, return_times=True), + ) + # construct back from kwargs keep the same array sorting2 = load(sorting.to_dict()) assert np.shares_memory(sorting2._cached_spike_vector, sorting._cached_spike_vector)