Skip to content

Support NWB files stored as Zarr (.nwb.zarr) - #1996

Draft
CodyCBakerPhD wants to merge 4 commits into
claude/asset-kind-predicatesfrom
claude/nwb-zarr-in-zarrasset
Draft

CodyCBakerPhD wants to merge 4 commits into
claude/asset-kind-predicatesfrom
claude/nwb-zarr-in-zarrasset

Conversation

@CodyCBakerPhD

@CodyCBakerPhD CodyCBakerPhD commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Implements #1920; supersedes #1994, #1995 and #1312.

A *.nwb.zarr directory stays a ZarrAsset, 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 by NWBAsset'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.

  • Dependencies: hdmf-zarr ≥ 0.14, which needs Python ≥ 3.12, is in the zarr extra; there is no separate extra. On Python 3.11, *.nwb.zarr are handled only as Zarr, with master's dandi_zarr.nwb_zarr_python_version warning. On Python ≥ 3.12, a file that cannot be read (no hdmf-zarr, pickled data, broken metadata) gets one dandi_zarr.nwb_unreadable error saying why, and the metadata of any Zarr, as before.
  • Pickled data: files written by hdmf-zarr < 0.14 may store data with the pickle codec, which can run code when read. They are read only if NUMCODECS_ALLOW_UNSAFE_PICKLE_DECODE=1 is set.
  • organize: *.nwb.zarr keeps its extension and is hard-linked or copied, never symlinked, as upload skips symlinked directories.
  • BIDS: a *.nwb.zarr in a BIDS dataset is validated as NWB too, and its NWB metadata is merged with its BIDS metadata, as for a *.nwb there.
  • Directories named *.nwb are 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 as DANDI_TESTS_NWB_ZARR_V2; nothing binary is committed.

https://claude.ai/code/session_01DQrzw5ecrJY9QseLyohB4j


Generated by Claude Code

@CodyCBakerPhD CodyCBakerPhD added minor Increment the minor version when merged zarr labels Nov 13, 2026 — with Claude
@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.30713% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.70%. Comparing base (5c45416) to head (e076e66).

Files with missing lines Patch % Lines
dandi/pynwb_utils.py 97.85% 5 Missing ⚠️
dandi/tests/test_organize.py 96.11% 4 Missing ⚠️
dandi/tests/test_metadata.py 91.66% 2 Missing ⚠️
dandi/tests/test_utils.py 90.47% 2 Missing ⚠️
dandi/tests/test_files.py 99.21% 1 Missing ⚠️
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     
Flag Coverage Δ
unittests 91.70% <98.30%> (+0.31%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread pyproject.toml Resolved
@CodyCBakerPhD

Copy link
Copy Markdown
Contributor Author

This is the simplest approach

Pre-PR refactoring to make even more minimal

Look towards considating the is_* logic as the simplest way of extending, instead of isinstance on the nightmarish subclass network

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 ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, can you add me to that Dandiset? https://dandiarchive.org/dandiset/000027

@CodyCBakerPhD
CodyCBakerPhD force-pushed the claude/nwb-zarr-in-zarrasset branch from dc2ea80 to e9156a5 Compare December 2, 2026 19:28
@CodyCBakerPhD CodyCBakerPhD changed the title Support NWB files stored as Zarr (.nwb.zarr) in ZarrAsset, on the zarr-python validation Support NWB files stored as Zarr (.nwb.zarr) Dec 2, 2026
@CodyCBakerPhD
CodyCBakerPhD changed the base branch from claude/pr-1971-base to claude/asset-kind-predicates December 2, 2026 19:28
@CodyCBakerPhD
CodyCBakerPhD added this pull request to stack #2011 December 2, 2026 21:01
@CodyCBakerPhD CodyCBakerPhD self-assigned this Dec 2, 2026
Comment thread dandi/files/bases.py
from nwbinspector import Importance, inspect_nwbfile, load_config

# Avoid heavy import by importing within function:
from dandi.pynwb_utils import inspect_nwbfile
Comment thread dandi/files/zarr.py
)

from .bases import LocalDirectoryAsset, _add_data_standard
from .bases import LocalDirectoryAsset, NWBAsset, _add_data_standard
Comment thread dandi/files/zarr.py
)

from .bases import LocalDirectoryAsset, _add_data_standard
from .bases import LocalDirectoryAsset, NWBAsset, _add_data_standard
Comment thread dandi/files/zarr.py Fixed
Comment thread dandi/pynwb_utils.py
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
@CodyCBakerPhD
CodyCBakerPhD force-pushed the claude/nwb-zarr-in-zarrasset branch from e9156a5 to 247a6d8 Compare December 4, 2026 16:23
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
Comment thread dandi/files/zarr.py
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Increment the minor version when merged zarr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants