Skip to content

Commit 2d4560b

Browse files
Merge pull request #400 from NeurodataWithoutBorders/fix_empty_string_container
Fix empty string for optional attributes that aren't strings
2 parents e7c87e2 + 4e53b0a commit 2d4560b

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Upcoming
22

3+
### Fixes
4+
5+
* Fixed issue in `check_empty_string_for_optional_attribute` where it would not skip optional non-`str` fields. [#400](https://github.com/NeurodataWithoutBorders/nwbinspector/pull/400)
6+
7+
8+
39
# v0.4.29
410

511
* Support for Python 3.7 has officially been dropped by the NWB Inspector. Please use Python 3.8 and above. [#380](https://github.com/NeurodataWithoutBorders/nwbinspector/pull/380)

src/nwbinspector/checks/nwb_containers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ def check_small_dataset_compression(
6666
@register_check(importance=Importance.BEST_PRACTICE_SUGGESTION, neurodata_type=NWBContainer)
6767
def check_empty_string_for_optional_attribute(nwb_container: NWBContainer):
6868
"""
69-
Check if any NWBContainer has optional fields that are written as an empty string. These values should just be
70-
omitted instead
69+
Check if any NWBContainer has optional fields that are written as an empty string.
70+
71+
These values should just be omitted instead.
7172
7273
Parameters
7374
----------
@@ -76,7 +77,9 @@ def check_empty_string_for_optional_attribute(nwb_container: NWBContainer):
7677
Best Practice: :ref:`best_practice_placeholders`
7778
"""
7879
docval_args = type(nwb_container).__init__.__docval__["args"]
79-
optional_attrs = [arg["name"] for arg in docval_args if "default" in arg and arg["default"] is None]
80+
optional_attrs = [
81+
arg["name"] for arg in docval_args if arg["type"] is str and "default" in arg and arg["default"] is None
82+
]
8083
fields = [attr for attr in optional_attrs if getattr(nwb_container, attr) == ""]
8184
for field in fields:
8285
yield InspectorMessage(

tests/unit_tests/test_containers.py renamed to tests/unit_tests/test_nwb_containers.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import h5py
88
import numpy as np
99
from pynwb import NWBContainer, NWBFile
10+
from pynwb.image import ImageSeries
1011

1112
from nwbinspector import (
1213
InspectorMessage,
@@ -94,9 +95,9 @@ def test_check_large_dataset_compression_below_20GB(self):
9495

9596

9697
def test_hit_check_empty_string_for_optional_attribute():
97-
nwb = NWBFile("aa", "aa", datetime.now(), pharmacology="")
98+
nwbfile = NWBFile(session_description="aa", identifier="aa", session_start_time=datetime.now(), pharmacology="")
9899

99-
assert check_empty_string_for_optional_attribute(nwb)[0] == InspectorMessage(
100+
assert check_empty_string_for_optional_attribute(nwb_container=nwbfile)[0] == InspectorMessage(
100101
message='The attribute "pharmacology" is optional and you have supplied an empty string. Improve my omitting '
101102
"this attribute (in MatNWB or PyNWB) or entering as None (in PyNWB)",
102103
importance=Importance.BEST_PRACTICE_SUGGESTION,
@@ -108,5 +109,18 @@ def test_hit_check_empty_string_for_optional_attribute():
108109

109110

110111
def test_miss_check_empty_string_for_optional_attribute():
111-
nwb = NWBFile("aa", "aa", datetime.now())
112-
assert check_empty_string_for_optional_attribute(nwb) is None
112+
nwbfile = NWBFile(session_description="aa", identifier="aa", session_start_time=datetime.now())
113+
assert check_empty_string_for_optional_attribute(nwb_container=nwbfile) is None
114+
115+
116+
def test_check_empty_string_for_optional_attribute_skip_non_string():
117+
image_series = ImageSeries(
118+
name="TestImageSeries",
119+
description="Behavior video of animal moving in environment",
120+
unit="n.a.",
121+
external_file=["test1.mp4", "test2.avi"],
122+
format="external",
123+
starting_frame=[0, 2],
124+
timestamps=[0.0, 0.04, 0.07, 0.1, 0.14, 0.16, 0.21],
125+
) # The `data` field will be created by PyNWB but it will be empty and will otherwise raise warning/error via numpy
126+
assert check_empty_string_for_optional_attribute(nwb_container=image_series) is None

0 commit comments

Comments
 (0)