Skip to content

Commit

Permalink
Update tests, validate script for default load ns
Browse files Browse the repository at this point in the history
  • Loading branch information
rly committed Oct 4, 2023
1 parent a833c9e commit 8b86a20
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
1 change: 0 additions & 1 deletion src/pynwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ def __init__(self, **kwargs):
load_namespaces = False

if load_namespaces:

tm = get_type_map()
super().load_namespaces(tm, path, file=file_obj, driver=driver)
manager = BuildManager(tm)
Expand Down
1 change: 1 addition & 0 deletions src/pynwb/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def validate(**kwargs):
file=sys.stderr,
)
else:
io_kwargs.update(load_namespaces=False)

Check warning on line 159 in src/pynwb/validate.py

View check run for this annotation

Codecov / codecov/patch

src/pynwb/validate.py#L159

Added line #L159 was not covered by tests
namespaces_to_validate = [CORE_NAMESPACE]

if namespace is not None:
Expand Down
26 changes: 18 additions & 8 deletions tests/back_compat/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class TestReadOldVersions(TestCase):
"- expected an array of shape '[None]', got non-array data 'one publication'")],
}

def get_io(self, path):
"""Get an NWBHDF5IO object for the given path."""
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message=r"Ignoring cached namespace .*",
category=UserWarning,
)
return NWBHDF5IO(str(path), 'r')

def test_read(self):
"""Test reading and validating all NWB files in the same folder as this file.
Expand All @@ -43,7 +53,7 @@ def test_read(self):
with self.subTest(file=f.name):
with warnings.catch_warnings(record=True) as warnings_on_read:
warnings.simplefilter("always")
with NWBHDF5IO(str(f), 'r') as io:
with self.get_io(f) as io:
errors = validate(io)
io.read()
for w in warnings_on_read:
Expand All @@ -69,28 +79,28 @@ def test_read(self):
def test_read_timeseries_no_data(self):
"""Test that a TimeSeries written without data is read with data set to the default value."""
f = Path(__file__).parent / '1.5.1_timeseries_no_data.nwb'
with NWBHDF5IO(str(f), 'r') as io:
with self.get_io(f) as io:
read_nwbfile = io.read()
np.testing.assert_array_equal(read_nwbfile.acquisition['test_timeseries'].data, TimeSeries.DEFAULT_DATA)

def test_read_timeseries_no_unit(self):
"""Test that an ImageSeries written without unit is read with unit set to the default value."""
f = Path(__file__).parent / '1.5.1_timeseries_no_unit.nwb'
with NWBHDF5IO(str(f), 'r') as io:
with self.get_io(f) as io:
read_nwbfile = io.read()
self.assertEqual(read_nwbfile.acquisition['test_timeseries'].unit, TimeSeries.DEFAULT_UNIT)

def test_read_imageseries_no_data(self):
"""Test that an ImageSeries written without data is read with data set to the default value."""
f = Path(__file__).parent / '1.5.1_imageseries_no_data.nwb'
with NWBHDF5IO(str(f), 'r') as io:
with self.get_io(f) as io:
read_nwbfile = io.read()
np.testing.assert_array_equal(read_nwbfile.acquisition['test_imageseries'].data, ImageSeries.DEFAULT_DATA)

def test_read_imageseries_no_unit(self):
"""Test that an ImageSeries written without unit is read with unit set to the default value."""
f = Path(__file__).parent / '1.5.1_imageseries_no_unit.nwb'
with NWBHDF5IO(str(f), 'r') as io:
with self.get_io(f) as io:
read_nwbfile = io.read()
self.assertEqual(read_nwbfile.acquisition['test_imageseries'].unit, ImageSeries.DEFAULT_UNIT)

Expand All @@ -100,7 +110,7 @@ def test_read_imageseries_non_external_format(self):
f = Path(__file__).parent / fbase
expected_warning = self.expected_warnings[fbase][0]
with self.assertWarnsWith(UserWarning, expected_warning):
with NWBHDF5IO(str(f), 'r') as io:
with self.get_io(f) as io:
read_nwbfile = io.read()
self.assertEqual(read_nwbfile.acquisition['test_imageseries'].format, "tiff")

Expand All @@ -110,13 +120,13 @@ def test_read_imageseries_nonmatch_starting_frame(self):
f = Path(__file__).parent / fbase
expected_warning = self.expected_warnings[fbase][0]
with self.assertWarnsWith(UserWarning, expected_warning):
with NWBHDF5IO(str(f), 'r') as io:
with self.get_io(f) as io:
read_nwbfile = io.read()
np.testing.assert_array_equal(read_nwbfile.acquisition['test_imageseries'].starting_frame, [1, 2, 3])

def test_read_subject_no_age__reference(self):
"""Test that reading a Subject without an age__reference set with NWB schema 2.5.0 sets the value to None"""
f = Path(__file__).parent / '2.2.0_subject_no_age__reference.nwb'
with NWBHDF5IO(str(f), 'r') as io:
with self.get_io(f) as io:
read_nwbfile = io.read()
self.assertIsNone(read_nwbfile.subject.age__reference)
58 changes: 24 additions & 34 deletions tests/validation/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,64 +199,54 @@ class TestValidateFunction(TestCase):
# 1.0.3_nwbfile.nwb has cached "core" specification
# 1.1.2_nwbfile.nwb has cached "core" and "hdmf-common" specificaitions

def get_io(self, path):
"""Get an NWBHDF5IO object for the given path, ignoring the warning about ignoring cached namespaces."""
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message=r"Ignoring cached namespace .*",
category=UserWarning,
)
return NWBHDF5IO(str(path), 'r')

def test_validate_io_no_cache(self):
"""Test that validating a file with no cached spec against the core namespace succeeds."""
with NWBHDF5IO('tests/back_compat/1.0.2_nwbfile.nwb', 'r') as io:
with self.get_io('tests/back_compat/1.0.2_nwbfile.nwb') as io:
errors = validate(io)
self.assertEqual(errors, [])

def test_validate_io_no_cache_bad_ns(self):
"""Test that validating a file with no cached spec against a specified, unknown namespace fails."""
with NWBHDF5IO('tests/back_compat/1.0.2_nwbfile.nwb', 'r') as io:
with self.get_io('tests/back_compat/1.0.2_nwbfile.nwb') as io:
with self.assertRaisesWith(KeyError, "\"'notfound' not a namespace\""):
validate(io, 'notfound')

def test_validate_io_cached(self):
"""Test that validating a file with cached spec against its cached namespace succeeds."""
with NWBHDF5IO('tests/back_compat/1.1.2_nwbfile.nwb', 'r') as io:
with self.get_io('tests/back_compat/1.1.2_nwbfile.nwb') as io:
errors = validate(io)
self.assertEqual(errors, [])

def test_validate_io_cached_extension(self):
"""Test that validating a file with cached spec against its cached namespaces succeeds."""
with warnings.catch_warnings(record=True):
warnings.filterwarnings(
"ignore",
message=r"Ignoring cached namespace .*",
category=UserWarning,
)
with NWBHDF5IO('tests/back_compat/2.1.0_nwbfile_with_extension.nwb', 'r') as io:
errors = validate(io)
self.assertEqual(errors, [])
with self.get_io('tests/back_compat/2.1.0_nwbfile_with_extension.nwb') as io:
errors = validate(io)
self.assertEqual(errors, [])

def test_validate_io_cached_extension_pass_ns(self):
"""Test that validating a file with cached extension spec against the extension namespace succeeds."""
with warnings.catch_warnings(record=True):
warnings.filterwarnings(
"ignore",
message=r"Ignoring cached namespace .*",
category=UserWarning,
)
with NWBHDF5IO('tests/back_compat/2.1.0_nwbfile_with_extension.nwb', 'r') as io:
errors = validate(io, 'ndx-testextension')
self.assertEqual(errors, [])
with self.get_io('tests/back_compat/2.1.0_nwbfile_with_extension.nwb') as io:
errors = validate(io, 'ndx-testextension')
self.assertEqual(errors, [])

def test_validate_io_cached_core_with_io(self):
"""
For back-compatability, test that validating a file with cached extension spec against the core
namespace succeeds when using the `io` + `namespace` keywords.
"""
with warnings.catch_warnings(record=True):
warnings.filterwarnings(
"ignore",
message=r"Ignoring cached namespace .*",
category=UserWarning,
)
with NWBHDF5IO(
path='tests/back_compat/2.1.0_nwbfile_with_extension.nwb', mode='r'
) as io:
results = validate(io=io, namespace="core")
self.assertEqual(results, [])
with self.get_io(path='tests/back_compat/2.1.0_nwbfile_with_extension.nwb') as io:
results = validate(io=io, namespace="core")
self.assertEqual(results, [])

def test_validate_file_cached_extension(self):
"""
Expand Down Expand Up @@ -310,13 +300,13 @@ def test_validate_file_cached_no_cache_bad_ns(self):

def test_validate_io_cached_bad_ns(self):
"""Test that validating a file with cached spec against a specified, unknown namespace fails."""
with NWBHDF5IO('tests/back_compat/1.1.2_nwbfile.nwb', 'r') as io:
with self.get_io('tests/back_compat/1.1.2_nwbfile.nwb') as io:
with self.assertRaisesWith(KeyError, "\"'notfound' not a namespace\""):
validate(io, 'notfound')

def test_validate_io_cached_hdmf_common(self):
"""Test that validating a file with cached spec against the hdmf-common namespace fails."""
with NWBHDF5IO('tests/back_compat/1.1.2_nwbfile.nwb', 'r') as io:
with self.get_io('tests/back_compat/1.1.2_nwbfile.nwb') as io:
# TODO this error should not be different from the error when using the validate script above
msg = "builder must have data type defined with attribute 'data_type'"
with self.assertRaisesWith(ValueError, msg):
Expand Down

0 comments on commit 8b86a20

Please sign in to comment.