Skip to content

Commit 6b617ab

Browse files
committed
Add t_start option to get_times().
1 parent 4539550 commit 6b617ab

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/spikeinterface/core/baserecording.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,26 @@ def has_time_vector(self, segment_index=None):
458458
return d["time_vector"] is not None
459459

460460
def set_times(self, times, segment_index=None, with_warning=True):
461-
"""Set times for a recording segment.
461+
"""Set times for a recording segment. Any existing times
462+
will be overwritten.
463+
464+
Times can be manually set on the recording segment. If times are
465+
not set, the sample index and sampling frequency are used to
466+
calculate time. Otherwise, `t_start` or `time_vector` can be
467+
provided:
468+
469+
`t_start` - the start time for the segment. The times for
470+
this recording segment will be calculated as
471+
t_start + sample_index * (1 / sampling_frequency)
472+
473+
`time_vector` - A vector of length segment.get_num_samples()
474+
that holds the exact time for each sample in the recording.
462475
463476
Parameters
464477
----------
465-
times : 1d np.array
466-
The time vector
478+
times : float | 1d np.array
479+
If `int`, this is the `t_start` for the segment,
480+
otherwise, it is the time vector.
467481
segment_index : int or None, default: None
468482
The segment index (required for multi-segment)
469483
with_warning : bool, default: True
@@ -472,11 +486,18 @@ def set_times(self, times, segment_index=None, with_warning=True):
472486
segment_index = self._check_segment_index(segment_index)
473487
rs = self._recording_segments[segment_index]
474488

475-
assert times.ndim == 1, "Time must have ndim=1"
476-
assert rs.get_num_samples() == times.shape[0], "times have wrong shape"
489+
if isinstance(times, float) or isinstance(times, int):
490+
rs.t_start = times
491+
rs.time_vector = None
492+
elif isinstance(times, np.ndarray):
477493

478-
rs.t_start = None
479-
rs.time_vector = times.astype("float64", copy=False)
494+
assert times.ndim == 1, "Time must have ndim=1"
495+
assert rs.get_num_samples() == times.shape[0], "times have wrong shape"
496+
497+
rs.t_start = None
498+
rs.time_vector = times.astype("float64", copy=False)
499+
else:
500+
raise TypeError("`times` must be an integer / float (`t_start`) or " "numpy array (`time_vector`).")
480501

481502
if with_warning:
482503
warnings.warn(

0 commit comments

Comments
 (0)