Skip to content

fix(ome_zarr): 🐛 preserve dimension names during resharding - #48

Merged
lguerard merged 2 commits into
imcf:mainfrom
therealtrueunderground:fix/preserve-dimension-names
Dec 3, 2026
Merged

lguerard merged 2 commits into
imcf:mainfrom
therealtrueunderground:fix/preserve-dimension-names

Conversation

@therealtrueunderground

Copy link
Copy Markdown
Contributor

Thanks for confirming the issue.

As suggested, this passes the existing dimension_names to _to_zarr_level() when creating the replacement array, so it goes through the existing array creation path.

Unnamed staging arrays stay unnamed until metadata finalization, and no names are inferred from the group axes.

Tests:

  • Added a parameterized regression test next to the existing reshard_level tests, covering complete, partial and absent dimension names.
  • Confirmed that both named cases fail before the fix and all three pass after it.
  • Verified both standalone resharding modes.

Local verification (Linux):

  • Python 3.11, 3.12 and 3.14: 615 passed, 43 skipped in each.
  • Ruff format, Ruff check and mypy pass.
  • Strict documentation build passes.

One note, the Cellpose doctest is hardware-dependent and fails the same way on both the base revision and this branch when CPU affinity is unrestricted. With affinity limited to two CPUs, both pass (46 passed, 24 skipped), so this failure is unrelated to the change.

Fixes #47

@lguerard lguerard left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this looks good: it goes through the existing creation path, unnamed arrays stay unnamed, and the test covers the three cases.

One optional change, if you have a minute: _to_zarr_level only honours dimension_names on its sharded branch. reshard_level can't reach the unsharded one (v2 returns early, and on v3 a truthy shard always gives a shard tuple), so #47 is fixed either way. The point is that the parameter should mean the same on both branches. GitHub won't take suggestion blocks outside the diff, so here are the two other spots:

 def _create_level_array(
     group: "zarr.Group",
     name: str,
     shape: tuple[int, ...],
     chunks: tuple[int, ...],
     dtype,
+    dimension_names: tuple[str | None, ...] | None = None,
 ) -> "zarr.Array":
     """Create (replacing any existing) a pyramid level array in *group*.
 
     Parameters
     ----------
     ...
     dtype : dtype-like
         Its dtype.
+    dimension_names : tuple of (str or None) or None, optional
+        Names for the array's dimensions; default none.
 
     Returns
     -------
     zarr.Array
     """
     if name in group:
         del group[name]
     kwargs = zarr_compressor_kwargs(_group_zarr_format(group))
     return group.create_array(
-        name, shape=shape, chunks=chunks, dtype=dtype, **kwargs
+        name,
+        shape=shape,
+        chunks=chunks,
+        dtype=dtype,
+        dimension_names=dimension_names,
+        **kwargs,
     )
         z = _create_level_array(
             _open_group(group_path),
             component,
             arr.shape,
             arr.chunksize,
             arr.dtype,
+            dimension_names=dimension_names,
         )

Behaviour is unchanged for every existing caller, since the default is None. A zarr v2 array accepts dimension_names=None but raises ValueError for real names, so a future v2 caller passing names would fail loudly rather than drop them. The docstring suggestion below goes with it.


Generated by Claude Code

Comment thread src/patchworks/plugins/ome_zarr.py Outdated
then cut straight into shards: blocks as small as a viewer's chunks
(one plane of 512 x 512) would make millions of tasks.
dimension_names : tuple of (str or None) or None, optional
Existing dimension names to preserve when writing a sharded level.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With the unsharded branch passing the names too, the docstring no longer needs to restrict it:

Suggested change
Existing dimension names to preserve when writing a sharded level.
Names for the new array's dimensions, e.g. the original's when
resharding; default none.

Generated by Claude Code

@lguerard

lguerard commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Thanks a lot for this ! It'll be included in the next release or available immediately from GitHub !

@lguerard
lguerard merged commit c9f8e65 into imcf:main Dec 3, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Standalone resharding drops dimension_names from Zarr v3 arrays

2 participants