Skip to content

Commit

Permalink
Merge pull request #3034 from h-mayorquin/add_time_slice
Browse files Browse the repository at this point in the history
add recording.time_slice like recording.frame_slice
  • Loading branch information
alejoe91 authored Jun 19, 2024
2 parents d0f21ef + 62a2830 commit 220a2a5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/spikeinterface/core/baserecording.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,30 @@ def frame_slice(self, start_frame: int, end_frame: int) -> BaseRecording:
sub_recording = FrameSliceRecording(self, start_frame=start_frame, end_frame=end_frame)
return sub_recording

def time_slice(self, start_time: float, end_time: float) -> BaseRecording:
"""
Returns a new recording with sliced time. Note that this operation is not in place.
Parameters
----------
start_time : float
The start time in seconds.
end_time : float
The end time in seconds.
Returns
-------
BaseRecording
The object with sliced time.
"""

assert self.get_num_segments() == 1, "Time slicing is only supported for single segment recordings."

start_frame = self.time_to_sample_index(start_time)
end_frame = self.time_to_sample_index(end_time)

return self.frame_slice(start_frame=start_frame, end_frame=end_frame)

def _select_segments(self, segment_indices):
from .segmentutils import SelectSegmentRecording

Expand Down
25 changes: 25 additions & 0 deletions src/spikeinterface/core/tests/test_baserecording.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,30 @@ def test_select_channels():
assert np.array_equal(selected_channel_ids, ["a", "c"])


def test_time_slice():
# Case with sampling frequency
sampling_frequency = 10_000.0
recording = generate_recording(durations=[1.0], num_channels=3, sampling_frequency=sampling_frequency)

sliced_recording_times = recording.time_slice(start_time=0.1, end_time=0.8)
sliced_recording_frames = recording.frame_slice(start_frame=1000, end_frame=8000)

assert np.allclose(sliced_recording_times.get_traces(), sliced_recording_frames.get_traces())


def test_time_slice_with_time_vector():

# Case with time vector
sampling_frequency = 10_000.0
recording = generate_recording(durations=[1.0], num_channels=3, sampling_frequency=sampling_frequency)
times = 1 + np.arange(0, 10_000) / sampling_frequency
recording.set_times(times=times, segment_index=0, with_warning=False)

sliced_recording_times = recording.time_slice(start_time=1.1, end_time=1.8)
sliced_recording_frames = recording.frame_slice(start_frame=1000, end_frame=8000)

assert np.allclose(sliced_recording_times.get_traces(), sliced_recording_frames.get_traces())


if __name__ == "__main__":
test_BaseRecording()

0 comments on commit 220a2a5

Please sign in to comment.