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

Update tests to use in_construct_mode=True for invalid timeseries #556

Merged
merged 1 commit into from
Jan 28, 2025
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

## Improvements
* Added a section for describing the issues with negative timestamps in `TimeSeries` [#545](https://github.com/NeurodataWithoutBorders/nwbinspector/pull/545)
* Use alternate way of generating `TimeSeries` objects to avoid new pynwb error when the shape of the first dimension of
data does not match the length of timestamps [#556](https://github.com/NeurodataWithoutBorders/nwbinspector/pull/556)

# v0.6.1

Expand Down
4 changes: 3 additions & 1 deletion tests/test_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def add_flipped_data_orientation_to_processing(nwbfile: NWBFile):
def add_non_matching_timestamps_dimension(nwbfile: NWBFile):
timestamps = [1.0, 2.1, 3.0]
timestamps_length = len(timestamps)
time_series = TimeSeries(
# Use __new__ and in_construct_mode=True to bypass the check in pynwb for data.shape[0] == len(timestamps)
time_series = TimeSeries.__new__(TimeSeries, in_construct_mode=True)
time_series.__init__(
name="test_time_series_3",
data=np.zeros(shape=(timestamps_length + 1, timestamps_length)),
timestamps=timestamps,
Expand Down
50 changes: 33 additions & 17 deletions tests/unit_tests/test_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,20 @@ def test_check_timestamps_match_first_dimension_special_skip(tmp_path):
image_height = 15
num_channels = 3
dtype = "uint8"
image_series = pynwb.image.ImageSeries(

# Use __new__ and in_construct_mode=True to bypass the check in pynwb for data.shape[0] == len(timestamps)
image_series = pynwb.image.ImageSeries.__new__(pynwb.image.ImageSeries, in_construct_mode=True)
image_series.__init__(
name="ImageSeries",
unit="n.a.",
unit="N/A",
data=np.empty(shape=(num_images, image_width, image_height, num_channels), dtype=dtype),
timestamps=[],
)
nwbfile.add_acquisition(image_series)
nwbfile.add_acquisition(
pynwb.image.IndexSeries(
name="IndexSeries",
unit="n.a.",
unit="N/A",
data=[0, 1],
indexed_timeseries=image_series,
timestamps=[0.5, 0.6],
Expand All @@ -152,14 +155,15 @@ def test_check_timestamps_match_first_dimension_special_skip(tmp_path):


def test_check_timestamps_match_first_dimension_bad():
assert check_timestamps_match_first_dimension(
time_series=pynwb.TimeSeries(
name="test_time_series",
unit="test_units",
data=np.empty(shape=4),
timestamps=[1.0, 2.0, 3.0],
)
) == InspectorMessage(
# Use __new__ and in_construct_mode=True to bypass the check in pynwb for data.shape[0] == len(timestamps)
time_series = pynwb.TimeSeries.__new__(pynwb.TimeSeries, in_construct_mode=True)
time_series.__init__(
name="test_time_series",
unit="test_units",
data=np.empty(shape=4),
timestamps=[1.0, 2.0, 3.0],
)
assert check_timestamps_match_first_dimension(time_series=time_series) == InspectorMessage(
message="The length of the first dimension of data (4) does not match the length of timestamps (3).",
importance=Importance.CRITICAL,
check_function_name="check_timestamps_match_first_dimension",
Expand All @@ -170,9 +174,15 @@ def test_check_timestamps_match_first_dimension_bad():


def test_check_timestamps_empty_data():
assert check_timestamps_match_first_dimension(
time_series=pynwb.TimeSeries(name="test_time_series", unit="test_units", data=[], timestamps=[1.0, 2.0, 3.0])
) == InspectorMessage(
# Use __new__ and in_construct_mode=True to bypass the check in pynwb for data.shape[0] == len(timestamps)
time_series = pynwb.TimeSeries.__new__(pynwb.TimeSeries, in_construct_mode=True)
time_series.__init__(
name="test_time_series",
unit="test_units",
data=[],
timestamps=[1.0, 2.0, 3.0],
)
assert check_timestamps_match_first_dimension(time_series=time_series) == InspectorMessage(
message="The length of the first dimension of data (0) does not match the length of timestamps (3).",
importance=Importance.CRITICAL,
check_function_name="check_timestamps_match_first_dimension",
Expand All @@ -183,9 +193,15 @@ def test_check_timestamps_empty_data():


def test_check_timestamps_empty_timestamps():
assert check_timestamps_match_first_dimension(
time_series=pynwb.TimeSeries(name="test_time_series", unit="test_units", data=np.empty(shape=4), timestamps=[])
) == InspectorMessage(
# Use __new__ and in_construct_mode=True to bypass the check in pynwb for data.shape[0] == len(timestamps)
time_series = pynwb.TimeSeries.__new__(pynwb.TimeSeries, in_construct_mode=True)
time_series.__init__(
name="test_time_series",
unit="test_units",
data=np.empty(shape=4),
timestamps=[],
)
assert check_timestamps_match_first_dimension(time_series=time_series) == InspectorMessage(
message="The length of the first dimension of data (4) does not match the length of timestamps (0).",
importance=Importance.CRITICAL,
check_function_name="check_timestamps_match_first_dimension",
Expand Down
Loading