From 2cbf4f0cfec6d983a1395a528fd09eb2775cce86 Mon Sep 17 00:00:00 2001 From: Heberto Mayorquin Date: Mon, 21 Oct 2024 09:19:48 -0600 Subject: [PATCH] Improve error when settings timestamps (#377) --- CHANGELOG.md | 1 + src/roiextractors/imagingextractor.py | 20 ++++++++++++++++--- .../test_multiimagingextractor.py | 10 ++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f36e7b8..45395e4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/roiextractors/imagingextractor.py b/src/roiextractors/imagingextractor.py index 44a1d39e..f0636588 100644 --- a/src/roiextractors/imagingextractor.py +++ b/src/roiextractors/imagingextractor.py @@ -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. diff --git a/tests/test_internals/test_multiimagingextractor.py b/tests/test_internals/test_multiimagingextractor.py index a62daa47..7ebf07fc 100644 --- a/tests/test_internals/test_multiimagingextractor.py +++ b/tests/test_internals/test_multiimagingextractor.py @@ -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)