@@ -179,7 +179,9 @@ def get_unit_spike_train(
179179 return spike_frames
180180
181181 def register_recording (self , recording , check_spike_frames = True ):
182- """Register a recording to the sorting.
182+ """
183+ Register a recording to the sorting. If the sorting and recording both contain
184+ time information, the recording’s time information will be used.
183185
184186 Parameters
185187 ----------
@@ -472,6 +474,43 @@ def frame_slice(self, start_frame, end_frame, check_spike_frames=True):
472474 )
473475 return sub_sorting
474476
477+ def time_slice (self , start_time : float | None , end_time : float | None ) -> BaseSorting :
478+ """
479+ Returns a new sorting object, restricted to the time interval [start_time, end_time].
480+
481+ Parameters
482+ ----------
483+ start_time : float | None, default: None
484+ The start time in seconds. If not provided it is set to 0.
485+ end_time : float | None, default: None
486+ The end time in seconds. If not provided it is set to the total duration.
487+
488+ Returns
489+ -------
490+ BaseSorting
491+ A new sorting object with only samples between start_time and end_time
492+ """
493+
494+ assert self .get_num_segments () == 1 , "Time slicing is only supported for single segment sortings."
495+
496+ start_frame = self .time_to_sample_index (start_time , segment_index = 0 ) if start_time else None
497+ end_frame = self .time_to_sample_index (end_time , segment_index = 0 ) if end_time else None
498+
499+ return self .frame_slice (start_frame = start_frame , end_frame = end_frame )
500+
501+ def time_to_sample_index (self , time , segment_index = 0 ):
502+ """
503+ Transform time in seconds into sample index
504+ """
505+ if self .has_recording ():
506+ sample_index = self ._recording .time_to_sample_index (time , segment_index = segment_index )
507+ else :
508+ segment = self ._sorting_segments [segment_index ]
509+ t_start = segment ._t_start if segment ._t_start is not None else 0
510+ sample_index = round ((time - t_start ) * self .get_sampling_frequency ())
511+
512+ return sample_index
513+
475514 def get_all_spike_trains (self , outputs = "unit_id" ):
476515 """
477516 Return all spike trains concatenated.
0 commit comments