Skip to content

Commit

Permalink
update cached namespace retrieval in validation tests (#1961)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
stephprince and rly authored Sep 9, 2024
1 parent 1178e0d commit 6196568
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

### Documentation and tutorial enhancements
- Added pre-release pull request instructions to release process documentation @stephprince [#1928](https://github.com/NeurodataWithoutBorders/pynwb/pull/1928)
Expand Down
26 changes: 15 additions & 11 deletions src/pynwb/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _validate_helper(io: HDMFIO, namespace: str = CORE_NAMESPACE) -> list:
return validator.validate(builder)


def _get_cached_namespaces_to_validate(
def get_cached_namespaces_to_validate(
path: str, driver: Optional[str] = None, aws_region: Optional[str] = None,
) -> Tuple[List[str], BuildManager, Dict[str, str]]:
"""
Expand All @@ -39,14 +39,18 @@ def _get_cached_namespaces_to_validate(
-------
The following example illustrates how we can use this function to validate against namespaces
cached in a file. This is useful, e.g., when a file was created using an extension
>>> from pynwb import validate
>>> from pynwb.validate import _get_cached_namespaces_to_validate
>>> path = "my_nwb_file.nwb"
>>> validate_namespaces, manager, cached_namespaces = _get_cached_namespaces_to_validate(path)
>>> with NWBHDF5IO(path, "r", manager=manager) as reader:
>>> errors = []
>>> for ns in validate_namespaces:
>>> errors += validate(io=reader, namespace=ns)
.. code-block:: python
from pynwb import validate
from pynwb.validate import get_cached_namespaces_to_validate
path = "my_nwb_file.nwb"
validate_namespaces, manager, cached_namespaces = get_cached_namespaces_to_validate(path)
with NWBHDF5IO(path, "r", manager=manager) as reader:
errors = []
for ns in validate_namespaces:
errors += validate(io=reader, namespace=ns)
:param path: Path for the NWB file
:return: Tuple with:
- List of strings with the most specific namespace(s) to use for validation.
Expand Down Expand Up @@ -149,7 +153,7 @@ def validate(**kwargs):
io_kwargs = dict(path=path, mode="r", driver=driver)

if use_cached_namespaces:
cached_namespaces, manager, namespace_dependencies = _get_cached_namespaces_to_validate(
cached_namespaces, manager, namespace_dependencies = get_cached_namespaces_to_validate(
path=path, driver=driver
)
io_kwargs.update(manager=manager)
Expand Down Expand Up @@ -231,7 +235,7 @@ def validate_cli():

if args.list_namespaces:
for path in args.paths:
cached_namespaces, _, _ = _get_cached_namespaces_to_validate(path=path)
cached_namespaces, _, _ = get_cached_namespaces_to_validate(path=path)
print("\n".join(cached_namespaces))
else:
validation_errors, validation_status = validate(
Expand Down
13 changes: 2 additions & 11 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def validate_nwbs():
examples_nwbs = glob.glob('*.nwb')

import pynwb
from pynwb.validate import get_cached_namespaces_to_validate

for nwb in examples_nwbs:
try:
Expand All @@ -171,17 +172,7 @@ def validate_nwbs():
for err in errors:
print("Error: %s" % err)

def get_namespaces(nwbfile):
comp = run(["python", "-m", "pynwb.validate",
"--list-namespaces", nwbfile],
stdout=PIPE, stderr=STDOUT, universal_newlines=True, timeout=30)

if comp.returncode != 0:
return []

return comp.stdout.split()

namespaces = get_namespaces(nwb)
namespaces, _, _ = get_cached_namespaces_to_validate(nwb)

if len(namespaces) == 0:
FAILURES += 1
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/ros3/test_ros3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pynwb import NWBHDF5IO
from pynwb import validate
from pynwb.validate import _get_cached_namespaces_to_validate
from pynwb.validate import get_cached_namespaces_to_validate
from pynwb.testing import TestCase
import urllib.request
import h5py
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_dandi_get_cached_namespaces(self):
)
}
}
found_namespaces, _, found_namespace_dependencies = _get_cached_namespaces_to_validate(
found_namespaces, _, found_namespace_dependencies = get_cached_namespaces_to_validate(
path=self.s3_test_path, driver="ros3"
)

Expand Down

0 comments on commit 6196568

Please sign in to comment.