Skip to content

Commit

Permalink
Add use_times option in get_duration/total_duration
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoe91 committed Jan 15, 2025
1 parent 01d1479 commit 7f37dff
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/spikeinterface/core/baserecording.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __repr__(self):
if num_segments > 1:
samples_per_segment = [self.get_num_samples(segment_index) for segment_index in range(num_segments)]
memory_per_segment_bytes = (self.get_memory_size(segment_index) for segment_index in range(num_segments))
durations = [self.get_duration(segment_index) for segment_index in range(num_segments)]
durations = [self.get_duration(segment_index, use_times=False) for segment_index in range(num_segments)]

samples_per_segment_formated = [f"{samples:,}" for samples in samples_per_segment]
durations_per_segment_formated = [convert_seconds_to_str(d) for d in durations]
Expand Down Expand Up @@ -95,7 +95,7 @@ def _repr_header(self):
dtype = self.get_dtype()

total_samples = self.get_total_samples()
total_duration = self.get_total_duration()
total_duration = self.get_total_duration(use_times=False)
total_memory_size = self.get_total_memory_size()

sf_hz = self.get_sampling_frequency()
Expand Down Expand Up @@ -216,7 +216,7 @@ def get_total_samples(self) -> int:

return sum(samples_per_segment)

def get_duration(self, segment_index=None) -> float:
def get_duration(self, segment_index=None, use_times=True) -> float:
"""
Returns the duration in seconds.
Expand All @@ -226,6 +226,9 @@ def get_duration(self, segment_index=None) -> float:
The sample index to retrieve the duration for.
For multi-segment objects, it is required, default: None
With single segment recording returns the duration of the single segment
use_times : bool, default: True
If True, the duration is calculated using the time vector if available.
If False, the duration is calculated using the number of samples and the sampling frequency.
Returns
-------
Expand All @@ -234,7 +237,7 @@ def get_duration(self, segment_index=None) -> float:
"""
segment_index = self._check_segment_index(segment_index)

if self.has_time_vector(segment_index):
if self.has_time_vector(segment_index) and use_times:
times = self.get_times(segment_index)
segment_duration = times[-1] - times[0] + (1 / self.get_sampling_frequency())
else:
Expand All @@ -243,16 +246,24 @@ def get_duration(self, segment_index=None) -> float:

return segment_duration

def get_total_duration(self) -> float:
def get_total_duration(self, use_times=True) -> float:
"""
Returns the total duration in seconds
Parameters
----------
use_times : bool, default: True
If True, the duration is calculated using the time vector if available.
If False, the duration is calculated using the number of samples and the sampling frequency.
Returns
-------
float
The duration in seconds
"""
duration = sum([self.get_duration(idx) for idx in range(self.get_num_segments())])
duration = sum(
[self.get_duration(segment_index, use_times) for segment_index in range(self.get_num_segments())]
)
return duration

def get_memory_size(self, segment_index=None) -> int:
Expand Down

0 comments on commit 7f37dff

Please sign in to comment.