Skip to content

OME-Zarr copies in 1024 x 1024 chunks; warn about stores too coarse for windows - #625

Merged
guijacquemet merged 1 commit into
integration/v0.10.1from
fix/convert-ome-zarr-chunks
Dec 4, 2026
Merged

guijacquemet merged 1 commit into
integration/v0.10.1from
fix/convert-ome-zarr-chunks

Conversation

@guijacquemet

Copy link
Copy Markdown
Contributor

Summary

Bug: convert --store ome-zarr (and the notebook's / convert_experiment(..., store="ome-zarr") copies) wrote each plane as one chunk. A windowed route (fiberfeatures run.execution_route = "patch_window", via RunInputs.window_reader) decodes a chunk whole for any window, so the first patch of a large converted slide was refused (WindowMemoryError) while check passed.

By module:

  • ome_zarr_copy: chunks 1 x ... x min(Y,1024) x min(X,1024) (CHUNK_SIDE, chunk_shape). Each plane is written as it is read, block by block within the memory limit (Plane.blocks()), each block's part of each chunk written in place (_PlaneChunks; edge chunks full size, zero = fill_value, as Zarr 2 stores them). A plane read in blocks is never held whole. Still uncompressed Zarr v2 / OME-NGFF 0.4, deterministic bytes.
  • windows: smallest_window_memory(source, cache_bytes) (1-px window, header only) and chunk_note(...), appended to window memory refusals for OME-Zarr whose chunks leave no window within the limit (CHUNK_FIX: re-convert into a new folder, or rechunk to at most 1024 x 1024).
  • window_reader: WindowReader (so RunInputs.window_reader) now refuses such a store at open (WindowMemoryError, same type as before, naming the chunks and the fix) instead of on the first window.
  • window_chunks (new): check_window_chunks -> warning chunks_too_large_for_windows; window_chunk_need(path, mapping) for consumers' own checks. Called from run's header checks (with --memory-limit) and from CLI check (default 1 GiB; check has no limit option), and recorded in the check report.

Decisions

  • Chunk size 1024 x 1024, 1 along every other axis: bioformats2raw's default and common OME-NGFF 0.4 practice; 1–8 MiB per chunk depending on dtype, so a window reader decodes a few MiB per chunk it touches and a cache of a few chunk rows stays small; one plane per chunk along C/Z/T keeps channels separately readable (the reader's channel groups). Planes <= 1024 px per side keep one chunk = the plane.
  • No pyramid: the writer had none, the brick reads level 0 only (windows.LEVEL), and a downsampling method would be a new, recorded choice. Not added.
  • Warning, not error: preprocessing itself reads such stores (in blocks); only windowed consumers cannot. Threshold = a 1-pixel window through a cache holding one chunk (4 x chunk + chunk + header) above the limit. The reader's open refusal uses its actual cache_bytes.

Byte / identity changes (CHANGELOG, Unreleased v0.10.1)

Copies of images larger than 1024 px along Y or X are new bytes (0/.zarray chunks, chunk files 0/<plane indices>/<row>/<col>), hence a different copy_sha256. Pixels, .zattrs, the rest of the record unchanged. Copies of planes <= 1024 x 1024 are byte-identical (verified against the base commit: 2 x 700 x 1000 identical; 1 x 1500 x 300 differs only in .zarray and the split chunk). A re-conversion into a folder that already holds an old single-chunk copy reuses it (plan and content unchanged) — the advice says convert into a new folder.

Notes for review

  • TIFF stored as one compressed strip/tile is not covered by the new warning (it already has plane_over_memory_limit only when over the plane limit); possible follow-up.
  • convert copies OME-Zarr sources byte for byte, so it cannot rechunk a user's own single-chunk store; the advice says to save it again with smaller chunks.
  • fiberfeatures check could call window_chunks.window_chunk_need / open its reader during check to catch this with its own limit (follow-up in that repo).
  • Not tested on a real slide; synthetic tiled TIFF (3000 x 2600 x 2, written tile by tile) and metadata-only 12000 x 12000 stores.

Tests

  • New tests/test_ome_zarr_chunks.py (6): lazily-written tiled TIFF converted with an 8 MiB limit -> chunks [1,1024,1024], 3x3 full-size chunk files per channel, every pixel equal, tracemalloc peak < half a plane; windowed read across four chunks through WindowReader + cache_plan within 64 MiB, bitwise equal; same image in one chunk per plane -> warning, refused at reader open and by read_window, with the fix; CLI check on metadata-only 12000 x 12000 single-chunk stores warns (exit 0) and records it in the report.
  • Full suite (python -m pytest -q -n 4 -p no:cacheprovider, conda env morphobricks-gate, PYTHONPATH=src): 9354 passed, 21 skipped, 3 failed. The 3 failures (test_code_identity::test_the_installed_preprocessing_is_identified_for_real, test_he_tissue_array::test_a_non_editable_install_states_verified_identities_for_real, test_he_tissue_mask::test_a_non_editable_install_is_verified_and_reused_for_real: "module not from this installation") fail identically on the base commit 6973070 in this setup: PYTHONPATH=src shadows the env's installed wheel. Peak RSS of one process 1.7 GB.
  • python tools/check.py --rules passes; ruff check . / ruff format --check . pass.
  • pip freeze before and after identical (nothing installed).

🤖 Generated with Claude Code

…or windows

convert --store ome-zarr (and the notebook's copy) wrote each plane as one
chunk, so a windowed reader decoded the whole plane for every window and
refused the first patch of a large slide after check had passed.

- ome_zarr_copy: chunks 1 x ... x min(Y,1024) x min(X,1024), written block by
  block in place (edge chunks full size, zero-filled); no pyramid.
- windows: smallest_window_memory, chunk_note (fix advice on refusals).
- window_reader: WindowReader refuses at open an OME-Zarr whose chunks leave no
  window within the limit with its cache.
- window_chunks (new): check_window_chunks -> warning
  chunks_too_large_for_windows, from check and run.
- tests/test_ome_zarr_chunks.py; CHANGELOG, contract, user guide, AGENTS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@guijacquemet
guijacquemet marked this pull request as ready for review December 4, 2026 10:07
@guijacquemet
guijacquemet merged commit 0907891 into integration/v0.10.1 Dec 4, 2026
3 checks passed
@guijacquemet
guijacquemet deleted the fix/convert-ome-zarr-chunks branch December 4, 2026 10:08
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.

2 participants