Skip to content

Commit f284065

Browse files
Merge pull request #137 from NeurodataWithoutBorders/missing_unit
[Add Check]: missing 'unit' in TimeSeries
2 parents adf5e05 + c947b8b commit f284065

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

docs/best_practices/time_series.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Every :nwb-schema:ref:`sec-TimeSeries` instance has ``unit`` as an attribute, wh
3535
measurement for that data, using the appropriate type from the
3636
:wikipedia:`International System of Units (SI) <International_System_of_Units>`.
3737

38+
Check function: :py:meth:`~nwbinspector.checks.time_series.check_missing_unit`
39+
3840

3941

4042
.. _best_practice_time_series_global_time_reference:

nwbinspector/checks/time_series.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,34 +68,23 @@ def check_timestamps_ascending(time_series: TimeSeries, nelems=200):
6868
return InspectorMessage(f"{time_series.name} timestamps are not ascending.")
6969

7070

71+
@register_check(importance=Importance.BEST_PRACTICE_VIOLATION, neurodata_type=TimeSeries)
72+
def check_missing_unit(time_series: TimeSeries):
73+
"""
74+
Check if the TimeSeries.unit field is empty.
75+
76+
Best Practice: :ref:`best_practice_unit_of_measurement`
77+
"""
78+
if not time_series.unit:
79+
return InspectorMessage(
80+
message="Missing text for attribute 'unit'. Please specify the scientific unit of the 'data'."
81+
)
82+
83+
7184
@register_check(importance=Importance.BEST_PRACTICE_VIOLATION, neurodata_type=TimeSeries)
7285
def check_resolution(time_series: TimeSeries):
7386
"""Check the resolution value of a TimeSeries for proper format (-1.0 or NaN for unknown)."""
7487
if time_series.resolution != -1.0 and time_series.resolution <= 0:
7588
return InspectorMessage(
7689
message=f"'resolution' should use -1.0 or NaN for unknown instead of {time_series.resolution}."
7790
)
78-
79-
80-
# TODO: break up logic of extra stuff into separate checks
81-
# def check_timeseries(nwbfile):
82-
# """Check dataset values in TimeSeries objects"""
83-
# for ts in all_of_type(nwbfile, pynwb.TimeSeries):
84-
# if ts.data is None:
85-
# # exception to the rule: ImageSeries objects are allowed to have no data
86-
# if not isinstance(ts, pynwb.image.ImageSeries):
87-
# error_code = "A101"
88-
# print("- %s: '%s' %s data is None" % (error_code, ts.name, type(ts).__name__))
89-
# else:
90-
# if ts.external_file is None:
91-
# error_code = "A101"
92-
# print(
93-
# "- %s: '%s' %s data is None and external_file is None"
94-
# % (error_code, ts.name, type(ts).__name__)
95-
# )
96-
# continue
97-
98-
# if not ts.unit:
99-
# error_code = "A101"
100-
# print("- %s: '%s' %s data is missing text for attribute 'unit'"
101-
# % (error_code, ts.name, type(ts).__name__))

tests/unit_tests/test_time_series.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
check_data_orientation,
88
check_timestamps_match_first_dimension,
99
check_timestamps_ascending,
10+
check_missing_unit,
1011
check_resolution,
1112
Importance,
1213
)
@@ -117,6 +118,23 @@ def test_pass_check_timestamps_ascending():
117118
assert check_timestamps_ascending(time_series) is None
118119

119120

121+
def test_check_missing_unit_pass():
122+
time_series = pynwb.TimeSeries(name="test_time_series", unit="test_units", data=[1, 2, 3], timestamps=[1, 2, 3])
123+
assert check_missing_unit(time_series) is None
124+
125+
126+
def test_check_missing_unit_fail():
127+
time_series = pynwb.TimeSeries(name="test_time_series", unit="", data=[1, 2, 3], timestamps=[1, 2, 3])
128+
assert check_missing_unit(time_series) == InspectorMessage(
129+
message="Missing text for attribute 'unit'. Please specify the scientific unit of the 'data'.",
130+
importance=Importance.BEST_PRACTICE_VIOLATION,
131+
check_function_name="check_missing_unit",
132+
object_type="TimeSeries",
133+
object_name="test_time_series",
134+
location="/",
135+
)
136+
137+
120138
def test_check_positive_resolution_pass():
121139
time_series = pynwb.TimeSeries(name="test", unit="test_units", data=[1, 2, 3], timestamps=[1, 2, 3], resolution=3.4)
122140
assert check_timestamps_ascending(time_series) is None

0 commit comments

Comments
 (0)