Skip to content

Commit 6196568

Browse files
stephprincerly
andauthored
update cached namespace retrieval in validation tests (#1961)
* filter out warnings when getting namespaces in test.py * make get_cached_namespaces function public * replace get_namespaces function * update code block in docstring * update CHANGELOG.md --------- Co-authored-by: Ryan Ly <[email protected]>
1 parent 1178e0d commit 6196568

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Enhancements and minor changes
66
- Added support for numpy 2.0. @mavaylon1 [#1956](https://github.com/NeurodataWithoutBorders/pynwb/pull/1956)
7+
- Make `get_cached_namespaces_to_validate` a public function @stephprince [#1961](https://github.com/NeurodataWithoutBorders/pynwb/pull/1961)
78

89
### Documentation and tutorial enhancements
910
- Added pre-release pull request instructions to release process documentation @stephprince [#1928](https://github.com/NeurodataWithoutBorders/pynwb/pull/1928)

src/pynwb/validate.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _validate_helper(io: HDMFIO, namespace: str = CORE_NAMESPACE) -> list:
2929
return validator.validate(builder)
3030

3131

32-
def _get_cached_namespaces_to_validate(
32+
def get_cached_namespaces_to_validate(
3333
path: str, driver: Optional[str] = None, aws_region: Optional[str] = None,
3434
) -> Tuple[List[str], BuildManager, Dict[str, str]]:
3535
"""
@@ -39,14 +39,18 @@ def _get_cached_namespaces_to_validate(
3939
-------
4040
The following example illustrates how we can use this function to validate against namespaces
4141
cached in a file. This is useful, e.g., when a file was created using an extension
42-
>>> from pynwb import validate
43-
>>> from pynwb.validate import _get_cached_namespaces_to_validate
44-
>>> path = "my_nwb_file.nwb"
45-
>>> validate_namespaces, manager, cached_namespaces = _get_cached_namespaces_to_validate(path)
46-
>>> with NWBHDF5IO(path, "r", manager=manager) as reader:
47-
>>> errors = []
48-
>>> for ns in validate_namespaces:
49-
>>> errors += validate(io=reader, namespace=ns)
42+
43+
.. code-block:: python
44+
45+
from pynwb import validate
46+
from pynwb.validate import get_cached_namespaces_to_validate
47+
path = "my_nwb_file.nwb"
48+
validate_namespaces, manager, cached_namespaces = get_cached_namespaces_to_validate(path)
49+
with NWBHDF5IO(path, "r", manager=manager) as reader:
50+
errors = []
51+
for ns in validate_namespaces:
52+
errors += validate(io=reader, namespace=ns)
53+
5054
:param path: Path for the NWB file
5155
:return: Tuple with:
5256
- List of strings with the most specific namespace(s) to use for validation.
@@ -149,7 +153,7 @@ def validate(**kwargs):
149153
io_kwargs = dict(path=path, mode="r", driver=driver)
150154

151155
if use_cached_namespaces:
152-
cached_namespaces, manager, namespace_dependencies = _get_cached_namespaces_to_validate(
156+
cached_namespaces, manager, namespace_dependencies = get_cached_namespaces_to_validate(
153157
path=path, driver=driver
154158
)
155159
io_kwargs.update(manager=manager)
@@ -231,7 +235,7 @@ def validate_cli():
231235

232236
if args.list_namespaces:
233237
for path in args.paths:
234-
cached_namespaces, _, _ = _get_cached_namespaces_to_validate(path=path)
238+
cached_namespaces, _, _ = get_cached_namespaces_to_validate(path=path)
235239
print("\n".join(cached_namespaces))
236240
else:
237241
validation_errors, validation_status = validate(

test.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def validate_nwbs():
153153
examples_nwbs = glob.glob('*.nwb')
154154

155155
import pynwb
156+
from pynwb.validate import get_cached_namespaces_to_validate
156157

157158
for nwb in examples_nwbs:
158159
try:
@@ -171,17 +172,7 @@ def validate_nwbs():
171172
for err in errors:
172173
print("Error: %s" % err)
173174

174-
def get_namespaces(nwbfile):
175-
comp = run(["python", "-m", "pynwb.validate",
176-
"--list-namespaces", nwbfile],
177-
stdout=PIPE, stderr=STDOUT, universal_newlines=True, timeout=30)
178-
179-
if comp.returncode != 0:
180-
return []
181-
182-
return comp.stdout.split()
183-
184-
namespaces = get_namespaces(nwb)
175+
namespaces, _, _ = get_cached_namespaces_to_validate(nwb)
185176

186177
if len(namespaces) == 0:
187178
FAILURES += 1

tests/integration/ros3/test_ros3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pynwb import NWBHDF5IO
22
from pynwb import validate
3-
from pynwb.validate import _get_cached_namespaces_to_validate
3+
from pynwb.validate import get_cached_namespaces_to_validate
44
from pynwb.testing import TestCase
55
import urllib.request
66
import h5py
@@ -85,7 +85,7 @@ def test_dandi_get_cached_namespaces(self):
8585
)
8686
}
8787
}
88-
found_namespaces, _, found_namespace_dependencies = _get_cached_namespaces_to_validate(
88+
found_namespaces, _, found_namespace_dependencies = get_cached_namespaces_to_validate(
8989
path=self.s3_test_path, driver="ros3"
9090
)
9191

0 commit comments

Comments
 (0)