Skip to content

Commit

Permalink
Improve error when settings timestamps (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-mayorquin authored Oct 21, 2024
1 parent 2cf9235 commit 2cbf4f0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Improvements
* Removed unnecessary import checks for scipy, h5py, and zarr [PR #364](https://github.com/catalystneuro/roiextractors/pull/364)
* Improved the error message for the `set_timestamps` method in the `ImagingExtractor` class[PR #377](https://github.com/catalystneuro/roiextractors/pull/377)

### Testing

Expand Down
20 changes: 17 additions & 3 deletions src/roiextractors/imagingextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,24 @@ def set_times(self, times: ArrayType) -> None:
Parameters
----------
times: array-like
The times in seconds for each frame
The times in seconds for each frame.
Raises
------
ValueError
If the length of 'times' does not match the number of frames.
"""
assert len(times) == self.get_num_frames(), "'times' should have the same length of the number of frames!"
self._times = np.array(times).astype("float64")
num_frames = self.get_num_frames()
num_timestamps = len(times)

if num_timestamps != num_frames:
raise ValueError(
f"Mismatch between the number of frames and timestamps: "
f"{num_frames} frames, but {num_timestamps} timestamps provided. "
"Ensure the length of 'times' matches the number of frames."
)

self._times = np.array(times).astype("float64", copy=False)

def has_time_vector(self) -> bool:
"""Detect if the ImagingExtractor has a time vector set or not.
Expand Down
10 changes: 6 additions & 4 deletions tests/test_internals/test_multiimagingextractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ def test_get_video_single_frame(self):
assert_array_equal(test_frames, expected_frames)

def test_set_incorrect_times(self):
with self.assertRaisesWith(
exc_type=AssertionError,
exc_msg="'times' should have the same length of the number of frames!",
):
with self.assertRaises(ValueError) as cm:
self.multi_imaging_extractor.set_times(times=np.arange(0, 10) / 30.0)
self.assertEqual(
str(cm.exception),
"Mismatch between the number of frames and timestamps: 30 frames, but 10 timestamps provided. "
"Ensure the length of 'times' matches the number of frames.",
)

self.assertEqual(self.multi_imaging_extractor._times, None)

Expand Down

0 comments on commit 2cbf4f0

Please sign in to comment.