Skip to content

Commit d0919ad

Browse files
authored
Merge pull request #3623 from alejoe91/use-times-in-get-durations
Add `get_start_time`/`get_end_time` functions and use them in `get_duration`
2 parents dbd433d + 3c6444b commit d0919ad

2 files changed

Lines changed: 54 additions & 11 deletions

File tree

src/spikeinterface/core/baserecording.py

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,9 @@ def get_duration(self, segment_index=None) -> float:
232232
float
233233
The duration in seconds
234234
"""
235-
segment_index = self._check_segment_index(segment_index)
236-
237-
if self.has_time_vector(segment_index):
238-
times = self.get_times(segment_index)
239-
segment_duration = times[-1] - times[0] + (1 / self.get_sampling_frequency())
240-
else:
241-
segment_num_samples = self.get_num_samples(segment_index=segment_index)
242-
segment_duration = segment_num_samples / self.get_sampling_frequency()
243-
235+
segment_duration = (
236+
self.get_end_time(segment_index) - self.get_start_time(segment_index) + (1 / self.get_sampling_frequency())
237+
)
244238
return segment_duration
245239

246240
def get_total_duration(self) -> float:
@@ -252,7 +246,7 @@ def get_total_duration(self) -> float:
252246
float
253247
The duration in seconds
254248
"""
255-
duration = sum([self.get_duration(idx) for idx in range(self.get_num_segments())])
249+
duration = sum([self.get_duration(segment_index) for segment_index in range(self.get_num_segments())])
256250
return duration
257251

258252
def get_memory_size(self, segment_index=None) -> int:
@@ -445,6 +439,40 @@ def get_times(self, segment_index=None) -> np.ndarray:
445439
times = rs.get_times()
446440
return times
447441

442+
def get_start_time(self, segment_index=None) -> float:
443+
"""Get the start time of the recording segment.
444+
445+
Parameters
446+
----------
447+
segment_index : int or None, default: None
448+
The segment index (required for multi-segment)
449+
450+
Returns
451+
-------
452+
float
453+
The start time in seconds
454+
"""
455+
segment_index = self._check_segment_index(segment_index)
456+
rs = self._recording_segments[segment_index]
457+
return rs.get_start_time()
458+
459+
def get_end_time(self, segment_index=None) -> float:
460+
"""Get the stop time of the recording segment.
461+
462+
Parameters
463+
----------
464+
segment_index : int or None, default: None
465+
The segment index (required for multi-segment)
466+
467+
Returns
468+
-------
469+
float
470+
The stop time in seconds
471+
"""
472+
segment_index = self._check_segment_index(segment_index)
473+
rs = self._recording_segments[segment_index]
474+
return rs.get_end_time()
475+
448476
def has_time_vector(self, segment_index=None):
449477
"""Check if the segment of the recording has a time vector.
450478
@@ -903,6 +931,21 @@ def get_times(self) -> np.ndarray:
903931
time_vector += self.t_start
904932
return time_vector
905933

934+
def get_start_time(self) -> float:
935+
if self.time_vector is not None:
936+
return self.time_vector[0]
937+
else:
938+
return self.t_start if self.t_start is not None else 0.0
939+
940+
def get_end_time(self) -> float:
941+
if self.time_vector is not None:
942+
return self.time_vector[-1]
943+
else:
944+
t_stop = (self.get_num_samples() - 1) / self.sampling_frequency
945+
if self.t_start is not None:
946+
t_stop += self.t_start
947+
return t_stop
948+
906949
def get_times_kwargs(self) -> dict:
907950
"""
908951
Retrieves the timing attributes characterizing a RecordingSegment

src/spikeinterface/core/loading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def load(file_or_folder_or_dict, base_folder=None) -> BaseExtractor:
104104
raise ValueError(error_msg)
105105
else:
106106
# remote case - zarr
107-
if str(file_path).endswith(".zarr"):
107+
if str(file_path).endswith(".zarr") or str(file_path).endswith(".zarr/"):
108108
from .zarrextractors import read_zarr
109109

110110
extractor = read_zarr(file_path)

0 commit comments

Comments
 (0)