Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error when settings timestamps #377

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also good to understand if you have more frames than timestamps or the opposite


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
Loading