Skip to content

Commit 16cdedd

Browse files
authored
Drop pynwb < 3.1 compatibility code from pynwb_utils
The declared floor is pynwb >= 3.1.0 (and nwbinspector >= 0.7.0 requires pynwb >= 3.1 anyway), which makes several version guards unreachable: - validate(): only the `pynwb.validate(path=...)` branch (pynwb >= 3.0) can run; the `paths=[...]` tuple-returning form (2.2 to 2.x) and the `io=` fallback for even older releases are gone. - copy_nwb_file(): `cache_spec` has been accepted by `export()` since pynwb 2.8.2, so pass it unconditionally. - _get_external_images(): `ExternalImage` exists in every supported pynwb, so import it at module level instead of guarding an ImportError. The now-unused `packaging.version.Version` import is removed as well. The NWB *schema* version checks (e.g. the < 2.1.0 error filter) are about the file being validated, not about pynwb, and are unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HFhMPs6vF5zyeHDvPTvRcA
1 parent 540c7c0 commit 16cdedd

1 file changed

Lines changed: 7 additions & 23 deletions

File tree

‎dandi/pynwb_utils.py‎

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import h5py
2828
import hdmf
2929
import numpy as np
30-
from packaging.version import Version
3130
import pynwb
3231
from pynwb import NWBHDF5IO
32+
from pynwb.base import ExternalImage
3333
import semantic_version
3434

3535
from . import __version__, get_logger
@@ -450,12 +450,6 @@ def _get_external_images(nwb: pynwb.NWBFile) -> list[dict]:
450450
list of dicts : [{id: <ExternalImage uuid>, name: <ExternalImage name>,
451451
external_files=[ExternalImage.data], field: "data"}]
452452
"""
453-
try:
454-
from pynwb.base import ExternalImage
455-
except ImportError:
456-
# `ExternalImage` was added in pynwb 3.1.0; nothing to collect on an older one.
457-
return []
458-
459453
out = []
460454
for ob in nwb.objects.values():
461455
if not isinstance(ob, ExternalImage) or ob.data is None:
@@ -685,16 +679,9 @@ def validate(path: str | Path, devel_debug: bool = False) -> list[ValidationResu
685679
)
686680

687681
try:
688-
if Version(pynwb.__version__) >= Version("3.0.0"):
689-
error_outputs = pynwb.validate(path=path)
690-
elif Version(pynwb.__version__) >= Version(
691-
"2.2.0"
692-
): # Use cached namespace feature
693-
# argument get_cached_namespaces is True by default
694-
error_outputs, _ = pynwb.validate(paths=[path])
695-
else: # Fallback if an older version
696-
with pynwb.NWBHDF5IO(path=path, mode="r", load_namespaces=True) as reader:
697-
error_outputs = pynwb.validate(io=reader)
682+
# Validates against the namespaces cached in the file; pynwb falls back
683+
# to its own namespaces if the file has none cached
684+
error_outputs = pynwb.validate(path=path)
698685
except Exception as exc:
699686
if devel_debug:
700687
raise
@@ -808,18 +795,15 @@ def copy_nwb_file(src: str | Path, dest: str | Path) -> str:
808795
dest = op.join(dest, op.basename(src))
809796
else:
810797
os.makedirs(op.dirname(dest), exist_ok=True)
811-
kws = {}
812-
if Version(pynwb.__version__) >= Version("2.8.2"):
813-
# we might make it leaner by not caching the spec if original
814-
# file did not have it. Possible only since 2.8.2.dev11
815-
kws["cache_spec"] = bool(pynwb.NWBHDF5IO.get_namespaces(src))
816798
with pynwb.NWBHDF5IO(src, "r") as ior, pynwb.NWBHDF5IO(dest, "w") as iow:
817799
data = ior.read()
818800
data.generate_new_id()
819801
iow.export(
820802
ior,
821803
nwbfile=data,
822-
**kws,
804+
# keep the copy leaner by not caching the spec if the original
805+
# file did not have it cached either
806+
cache_spec=bool(pynwb.NWBHDF5IO.get_namespaces(src)),
823807
)
824808
return str(dest)
825809

0 commit comments

Comments
 (0)