Repository navigation
Support NWB files stored as Zarr (.nwb.zarr) - #1996
CodyCBakerPhD wants to merge 4 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## claude/asset-kind-predicates #1996 +/- ##
================================================================
+ Coverage 91.38% 91.70% +0.31%
================================================================
Files 99 99
Lines 15954 16714 +760
================================================================
+ Hits 14580 15328 +748
- Misses 1374 1386 +12
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This is the simplest approach Pre-PR refactoring to make even more minimal Look towards considating the Explore backward compatibility breaks through notebooks and other uses of dandi-cli |
| python -m venv /tmp/hdmf-zarr-0.13 | ||
| /tmp/hdmf-zarr-0.13/bin/pip install "hdmf-zarr >= 0.13, < 0.14" | ||
| /tmp/hdmf-zarr-0.13/bin/python dandi/tests/data/nwb_zarr/make_ecephys_v2.py /tmp | ||
| DANDI_TESTS_NWB_ZARR_V2=/tmp/ecephys_v2.nwb.zarr pytest ... |
There was a problem hiding this comment.
if it produces small file -- we better do create and use that file in the tests without complicating CI setup: ideally CI should remain as lean as possible to trigger what we could do locally e.g. via just running tox.
Just upload it to dandi://DANDI/000027 which we already use for similar purposes. Make it "legit" as to current requirements and publish a new version so it could guarantee to exist in the future. Then reduce CI bloat needed to generate it
There was a problem hiding this comment.
Sure thing, can you add me to that Dandiset? https://dandiarchive.org/dandiset/000027
dc2ea80 to
e9156a5
Compare
.nwb.zarr) in ZarrAsset, on the zarr-python validation.nwb.zarr)
| from nwbinspector import Importance, inspect_nwbfile, load_config | ||
|
|
||
| # Avoid heavy import by importing within function: | ||
| from dandi.pynwb_utils import inspect_nwbfile |
| ) | ||
|
|
||
| from .bases import LocalDirectoryAsset, _add_data_standard | ||
| from .bases import LocalDirectoryAsset, NWBAsset, _add_data_standard |
| ) | ||
|
|
||
| from .bases import LocalDirectoryAsset, _add_data_standard | ||
| from .bases import LocalDirectoryAsset, NWBAsset, _add_data_standard |
| metadata_nwb_subject_fields, | ||
| ) | ||
| from .exceptions import NWBZarrReadError | ||
| from .files.zarr import get_zarr_format_version |
A *.nwb.zarr directory remains a ZarrAsset, uploaded and validated as any Zarr asset, and is now read as the NWB file it is (ZarrAsset.is_nwb): its metadata is extracted from it, and it is validated with PyNWB and NWB Inspector, as for any NWB file, by NWBAsset's methods. Implements #1920. - Reading: get_nwb_version(), get_object_id(), get_nwb_extensions(), get_neurodata_types() and nwb_has_external_links() read the JSON metadata of Zarr v2/v3 directly, so they need neither zarr nor hdmf-zarr and never descend into arrays. Metadata extraction, PyNWB validation and NWB Inspector (pynwb_utils.inspect_nwbfile()) read the file with open_nwb_zarr(): with NWBZarrV2IO for Zarr v2 and NWBZarrIO for Zarr v3. - hdmf-zarr >= 0.14 (the first to read Zarr v3, and Zarr v2 with zarr-python 3), and thus Python >= 3.12, is required: it is in the `zarr` extra. On older Python, NWB files stored as Zarr are handled as any Zarr, with the dandi_zarr.nwb_zarr_python_version warning. When a file cannot be read (no or older hdmf-zarr, pickled data not allowed, unreadable Zarr metadata), validation reports one dandi_zarr.nwb_unreadable error saying why, along with the Zarr, metadata and path-layout results, and its metadata is that of any Zarr, as before. - Pickled data: hdmf-zarr < 0.14 stored some data (e.g. the electrodes' group column) with the pickle codec, decoding which can execute arbitrary code. Such files are read only when NUMCODECS_ALLOW_UNSAFE_PICKLE_DECODE=1 (or numcodecs.pickles.ALLOW_UNSAFE_PICKLE_DECODE) allows it. - NWB Inspector's check_large_dataset_compression, which crashes on Zarr v3 with nwbinspector <= 0.7.2, is skipped for those (NeurodataWithoutBorders/nwbinspector#767). - Functions memoized on a path are not memoized for directories, as fingerprinting a Zarr stats every chunk file. - A directory named *.nwb is neither an asset nor walked into for assets: find_dandi_files() and organize warn that NWB files stored as Zarr must be named *.nwb.zarr. - organize: finds *.nwb.zarr and keeps the extension. As upload does not follow symlinks to directories, the auto mode hard-links their files or copies them (dereferencing symlinks, with reflinks where cp supports them), and the symlink mode refuses them. Those which refer to external media files are refused for now. Also fixes an UnboundLocalError in find_files() on symlinks to directories. - Tests use NWB files stored as Zarr written on the fly. Those of an NWB file stored as Zarr v2 by hdmf-zarr < 0.14 (with pickled data), which hdmf-zarr >= 0.14 cannot write, read it from DANDI_TESTS_NWB_ZARR_V2, and are skipped without it: CI writes it with dandi/tests/data/nwb_zarr/make_ecephys_v2.py on Python 3.11, once, as it caches it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DQrzw5ecrJY9QseLyohB4j
e9156a5 to
247a6d8
Compare
…to nwbzarr-rebuild
As there, so as not to change the imports of test_helpers and test_upload, which other open pull requests change too (and which the zarr-publishing test setup merges): the NWB file stored as Zarr is copied into the Dandiset of test_upload_nwb_zarr by a fixture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DQrzw5ecrJY9QseLyohB4j
A *.nwb.zarr in a BIDS dataset, a ZarrBIDSAsset which is_nwb, was validated as an NWB file, but its metadata was only that of its BIDS path. Its NWB metadata is now merged into it, as for an NWB file stored as HDF5 in a BIDS dataset (NWBBIDSAsset): ZarrAsset._get_nwb_metadata() gives it, or None when the Zarr is not, or cannot be, read as an NWB file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DQrzw5ecrJY9QseLyohB4j
| if not self._read_as_nwb: | ||
| return None | ||
| # Avoid heavy import by importing within function: | ||
| from dandi.pynwb_utils import get_nwb_zarr_reading_problem |
Implements #1920; supersedes #1994, #1995 and #1312.
A
*.nwb.zarrdirectory stays aZarrAsset, uploaded and validated as any Zarr, and is now also read as the NWB file it is (is_nwb). Its metadata is extracted from the NWB file, which is validated with PyNWB and NWB Inspector byNWBAsset's own methods, with no new class.Stacked on #2008 (Zarr depth limit, #1919) and #2009 (
is_nwb/is_bids/is_ome_zarr), which keep this PR's changes small.zarrextra; there is no separate extra. On Python 3.11,*.nwb.zarrare handled only as Zarr, with master'sdandi_zarr.nwb_zarr_python_versionwarning. On Python ≥ 3.12, a file that cannot be read (no hdmf-zarr, pickled data, broken metadata) gets onedandi_zarr.nwb_unreadableerror saying why, and the metadata of any Zarr, as before.NUMCODECS_ALLOW_UNSAFE_PICKLE_DECODE=1is set.*.nwb.zarrkeeps its extension and is hard-linked or copied, never symlinked, as upload skips symlinked directories.*.nwb.zarrin a BIDS dataset is validated as NWB too, and its NWB metadata is merged with its BIDS metadata, as for a*.nwbthere.*.nwbare skipped with a warning.Tests cover Zarr v2 and v3. hdmf-zarr ≥ 0.14 cannot write Zarr v2, so CI writes a v2 file once on Python 3.11 with
dandi/tests/data/nwb_zarr/make_ecephys_v2.py, caches it, and passes it to the tests asDANDI_TESTS_NWB_ZARR_V2; nothing binary is committed.https://claude.ai/code/session_01DQrzw5ecrJY9QseLyohB4j
Generated by Claude Code