Skip to content

Commit 33ca373

Browse files
authored
Update tests to use in_construct_mode=True for invalid timeseries (#556)
1 parent cf24e4b commit 33ca373

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

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

1214
# v0.6.1
1315

tests/test_inspector.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ def add_flipped_data_orientation_to_processing(nwbfile: NWBFile):
9595
def add_non_matching_timestamps_dimension(nwbfile: NWBFile):
9696
timestamps = [1.0, 2.1, 3.0]
9797
timestamps_length = len(timestamps)
98-
time_series = TimeSeries(
98+
# Use __new__ and in_construct_mode=True to bypass the check in pynwb for data.shape[0] == len(timestamps)
99+
time_series = TimeSeries.__new__(TimeSeries, in_construct_mode=True)
100+
time_series.__init__(
99101
name="test_time_series_3",
100102
data=np.zeros(shape=(timestamps_length + 1, timestamps_length)),
101103
timestamps=timestamps,

tests/unit_tests/test_time_series.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,20 @@ def test_check_timestamps_match_first_dimension_special_skip(tmp_path):
124124
image_height = 15
125125
num_channels = 3
126126
dtype = "uint8"
127-
image_series = pynwb.image.ImageSeries(
127+
128+
# Use __new__ and in_construct_mode=True to bypass the check in pynwb for data.shape[0] == len(timestamps)
129+
image_series = pynwb.image.ImageSeries.__new__(pynwb.image.ImageSeries, in_construct_mode=True)
130+
image_series.__init__(
128131
name="ImageSeries",
129-
unit="n.a.",
132+
unit="N/A",
130133
data=np.empty(shape=(num_images, image_width, image_height, num_channels), dtype=dtype),
131134
timestamps=[],
132135
)
133136
nwbfile.add_acquisition(image_series)
134137
nwbfile.add_acquisition(
135138
pynwb.image.IndexSeries(
136139
name="IndexSeries",
137-
unit="n.a.",
140+
unit="N/A",
138141
data=[0, 1],
139142
indexed_timeseries=image_series,
140143
timestamps=[0.5, 0.6],
@@ -152,14 +155,15 @@ def test_check_timestamps_match_first_dimension_special_skip(tmp_path):
152155

153156

154157
def test_check_timestamps_match_first_dimension_bad():
155-
assert check_timestamps_match_first_dimension(
156-
time_series=pynwb.TimeSeries(
157-
name="test_time_series",
158-
unit="test_units",
159-
data=np.empty(shape=4),
160-
timestamps=[1.0, 2.0, 3.0],
161-
)
162-
) == InspectorMessage(
158+
# Use __new__ and in_construct_mode=True to bypass the check in pynwb for data.shape[0] == len(timestamps)
159+
time_series = pynwb.TimeSeries.__new__(pynwb.TimeSeries, in_construct_mode=True)
160+
time_series.__init__(
161+
name="test_time_series",
162+
unit="test_units",
163+
data=np.empty(shape=4),
164+
timestamps=[1.0, 2.0, 3.0],
165+
)
166+
assert check_timestamps_match_first_dimension(time_series=time_series) == InspectorMessage(
163167
message="The length of the first dimension of data (4) does not match the length of timestamps (3).",
164168
importance=Importance.CRITICAL,
165169
check_function_name="check_timestamps_match_first_dimension",
@@ -170,9 +174,15 @@ def test_check_timestamps_match_first_dimension_bad():
170174

171175

172176
def test_check_timestamps_empty_data():
173-
assert check_timestamps_match_first_dimension(
174-
time_series=pynwb.TimeSeries(name="test_time_series", unit="test_units", data=[], timestamps=[1.0, 2.0, 3.0])
175-
) == InspectorMessage(
177+
# Use __new__ and in_construct_mode=True to bypass the check in pynwb for data.shape[0] == len(timestamps)
178+
time_series = pynwb.TimeSeries.__new__(pynwb.TimeSeries, in_construct_mode=True)
179+
time_series.__init__(
180+
name="test_time_series",
181+
unit="test_units",
182+
data=[],
183+
timestamps=[1.0, 2.0, 3.0],
184+
)
185+
assert check_timestamps_match_first_dimension(time_series=time_series) == InspectorMessage(
176186
message="The length of the first dimension of data (0) does not match the length of timestamps (3).",
177187
importance=Importance.CRITICAL,
178188
check_function_name="check_timestamps_match_first_dimension",
@@ -183,9 +193,15 @@ def test_check_timestamps_empty_data():
183193

184194

185195
def test_check_timestamps_empty_timestamps():
186-
assert check_timestamps_match_first_dimension(
187-
time_series=pynwb.TimeSeries(name="test_time_series", unit="test_units", data=np.empty(shape=4), timestamps=[])
188-
) == InspectorMessage(
196+
# Use __new__ and in_construct_mode=True to bypass the check in pynwb for data.shape[0] == len(timestamps)
197+
time_series = pynwb.TimeSeries.__new__(pynwb.TimeSeries, in_construct_mode=True)
198+
time_series.__init__(
199+
name="test_time_series",
200+
unit="test_units",
201+
data=np.empty(shape=4),
202+
timestamps=[],
203+
)
204+
assert check_timestamps_match_first_dimension(time_series=time_series) == InspectorMessage(
189205
message="The length of the first dimension of data (4) does not match the length of timestamps (0).",
190206
importance=Importance.CRITICAL,
191207
check_function_name="check_timestamps_match_first_dimension",

0 commit comments

Comments
 (0)