Skip to content

Do not count the chunk keys of Zarr arrays towards the depth limit - #2008

Merged
CodyCBakerPhD merged 2 commits into
masterfrom
claude/zarr-depth-chunk-keys
Dec 4, 2026
Merged

CodyCBakerPhD merged 2 commits into
masterfrom
claude/zarr-depth-chunk-keys

Conversation

@CodyCBakerPhD

Copy link
Copy Markdown
Contributor

Fixes #1919.

The chunk keys of a Zarr array span as many directories as it has dimensions (plus c) with Zarr v3's default chunk key encoding (data/c/1/0), or with / as dimension separator in Zarr v2. Because they were counted, a Zarr whose groups nest only a few levels deep, such as an NWB file stored as Zarr v3, failed validation with dandi_zarr.tree_depth_exceeded.

ZarrAsset._is_too_deep() now counts an array's chunks as files directly in its directory, and no longer lists arrays at all (sparing listing their chunks). Groups and other directories still count.

Split out of #1996, which builds on it.


Generated by Claude Code

@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.72727% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.36%. Comparing base (6d9c31a) to head (0a5a023).

Files with missing lines Patch % Lines
dandi/files/zarr.py 95.23% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2008      +/-   ##
==========================================
+ Coverage   91.29%   91.36%   +0.07%     
==========================================
  Files          99       99              
  Lines       15877    15919      +42     
==========================================
+ Hits        14495    14545      +50     
+ Misses       1382     1374       -8     
Flag Coverage Δ
unittests 91.36% <97.72%> (+0.07%) ⬆️

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.

@CodyCBakerPhD
CodyCBakerPhD added this pull request to stack #2011 December 2, 2026 21:01
@CodyCBakerPhD CodyCBakerPhD self-assigned this Dec 2, 2026
@CodyCBakerPhD
CodyCBakerPhD marked this pull request as ready for review December 2, 2026 21:06
@CodyCBakerPhD

Copy link
Copy Markdown
Contributor Author

@yarikoptic-gitmate fight me on this

@yarikoptic-gitmate yarikoptic-gitmate left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The change is correct and does what it says, so approving; the remarks below are not blocking.

Where the limit came from (since #1919 asks whether it is still needed): it was not introduced for NWB, nor for chunk keys.

  • Proposed in #852 (comment) (2021-12-16) as a guard against a Zarr's innards being uploaded as "thousands of assets which would only need to be removed", with a tentative N ("5?", possibly to be made an option), reasoned from dandiset layouts (dandi organize: 1 level, BIDS: 4) rather than from anything inside a Zarr.
  • Implemented in ca049a1 (#853) as MAX_ZARR_DEPTH = 5, applied inside an already recognised Zarr.
  • Raised to 7 in ae99630 (#984) for #983: the NGFF Zarrs of 000108 failed on chunk paths 0/0/0/0/0/0 (resolution level + 5 dimensions with / as separator), and 7 was picked "for the moment" ("7 (even 6) should cover our current use cases"), on the reasoning that "we are limiting zarr to ngff files", with other kinds of Zarr left to "discuss in the future".

So the only change to the value was to make room for chunk keys, which this PR stops counting altogether. I found no depth limit in dandi-archive either: the only check it applies to Zarr entry paths (validate_zarr_path()) rejects .. segments, so this looks to be a client-side check only.

What I checked

Old vs new _is_too_deep() on stores written by zarr-python 3.4.2:

Store master this PR
#1919 reproducer (v3, a/b/c/d/series/data, 2D) too deep ok
v2, 5D, / separator, at labels/cells/0 too deep ok
v3, sharded 3D array whose path inside the Zarr has 6 components (g1/…/g5/data) too deep ok
v3, 8D array at the root too deep ok
v3, array whose path inside the Zarr has 7 components (g1/…/g6/data) too deep too deep
directory without Zarr metadata holding a file with 8 path parts too deep too deep
non-chunk tree 9 levels deep below an array directory too deep ok

The boundary outside arrays is unchanged (a file with MAX_ZARR_DEPTH + 1 path parts is still flagged), and the check no longer walks chunks: on a 64k-chunk array it went from 1.1 s to under 1 ms.

Remarks

  1. With chunk keys out, the limit measures group nesting only, which the original rationale (accidentally uploading a heavy tree as separate assets) does not motivate inside a recognised Zarr. It still rejects an array whose path inside the Zarr has 7 components (6 nested groups below the root, plus the array), which an NWB extension with nested containers could reach. #1919 asked to "remove it or exclude chunk coordinates"; it would be good to state here why the limit is kept, or to follow up by removing it or demoting it to a warning.
  2. Nothing below an array directory is inspected any more (last row of the table). Fine for the purpose, but worth a sentence in the docstring, which currently only speaks of chunks; the description's "Groups and other directories still count" likewise holds only outside arrays.
  3. The docstring says chunk keys "span as many directories as it has dimensions (plus one, c)" (the description likewise), which overcounts by one: c/1/0 of a 2D v3 array lies in 2 directories, 1/0/0/0 of a 4D v2 array in 3. "Have as many path components as the array has dimensions (plus a leading c in v3)" would be exact.
  4. Tests: the new test goes from "passes" straight to three more subgroups. Asserting that it still passes with a/b (whose metadata file has 7 path parts, the most allowed outside arrays) would pin the boundary, and adding an array that is itself too deep (e.g. g1/…/g6/data) would exercise the return True in the array branch, which no test reaches now (presumably the line Codecov reports as missed).
  5. Counting, not new here: what is flagged is a file with more than MAX_ZARR_DEPTH path components, i.e. inside 7 or more nested directories. The error message, the new docstring and the test comment ("a file 8 directories deep") describe that as more than 7, or 8, directories deep; "path components" would be unambiguous.

Generated by Claude Code

@yarikoptic

Copy link
Copy Markdown
Member

@CodyCBakerPhD you have "my" blessing to merge unless you want to reflect on recommendations from the gitmate

The chunk keys of an array span as many directories as it has dimensions
(plus one, "c") with Zarr v3's default chunk key encoding (e.g.
data/c/1/0), or with "/" as dimension separator in Zarr v2, so they do not
tell how deep the Zarr hierarchy is.  A Zarr with groups nested a few levels
deep, such as an NWB file stored as Zarr v3, thus failed validation with
dandi_zarr.tree_depth_exceeded.

ZarrAsset._is_too_deep() now counts the chunks of an array as files
directly in its directory, and does not list arrays at all (sparing listing
their chunks).  Groups and other directories still count.

Fixes #1919

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DQrzw5ecrJY9QseLyohB4j
@CodyCBakerPhD
CodyCBakerPhD force-pushed the claude/zarr-depth-chunk-keys branch from 205c6eb to ec7cbb1 Compare December 4, 2026 16:23
@CodyCBakerPhD

Copy link
Copy Markdown
Contributor Author

Where the limit came from (since #1919 asks whether it is still needed): it was not introduced for NWB, nor for chunk keys.

We should determine that in a dedicated session doing its own logical justification and research; the current value is just a starting point

@CodyCBakerPhD

Copy link
Copy Markdown
Contributor Author

@CodyCBakerPhD you have "my" blessing to merge unless you want to reflect on recommendations from the gitmate

I did find that interesting that the gitmate could actually formally grant approval

So as not to change the imports of test_helpers, which other open pull
requests change too, and which the zarr-publishing test setup merges:
test_upload_discovery already imports mkpaths() from test_files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DQrzw5ecrJY9QseLyohB4j
@CodyCBakerPhD
CodyCBakerPhD merged commit 5615b1f into master Dec 4, 2026
41 checks passed
@CodyCBakerPhD
CodyCBakerPhD deleted the claude/zarr-depth-chunk-keys branch December 4, 2026 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch Increment the patch version when merged zarr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zarr depth limit rejects NWB chunk paths

4 participants