Skip to content

feat(zarr-indexing): lower transforms and parts to NumPy basic selections - #496

Draft
d-v-b wants to merge 7 commits into
mainfrom
feat/zarr-indexing-public-lowering
Draft

d-v-b wants to merge 7 commits into
mainfrom
feat/zarr-indexing-public-lowering

Conversation

@d-v-b

@d-v-b d-v-b commented Dec 4, 2026

Copy link
Copy Markdown
Owner

🤖 AI text below 🤖

IndexTransform.as_basic_selection() converts a transform to the NumPy basic selection (ints, slices and None) that reads the same cells, and raises the new NoBasicSelectionError when it can't. IndexTransform.decompose() and decompose_unit_step() split any transform into ascending slices to fetch plus an in-memory remainder, and Partition.source_selection / Partition.chunk_local_selection give each part's read as a basic selection, so code that plans with parts() can fetch through its own I/O layer.

Re-cut from #292 (piece 1 of 3: public lowering). #292's part-view projection design is superseded by zarr-developers#4349 and is not carried. Its result_into and asyncio example are not in this PR; #292 stays open until those are decided.

Agent notes

Public names added

  • zarr_indexing.BasicSelection: tuple[int | slice | None, ...], NumPy's basic-indexing dialect. Distinct from zarr.core.indexing.BasicSelection, which admits Ellipsis.
  • zarr_indexing.NoBasicSelectionError (a ValueError): the transform has no basic spelling the lowering accepts.
  • IndexTransform.as_basic_selection(): conservative and partial. It refuses ArrayMaps, stride 0, transposed or repeated axes, unreferenced non-singleton axes and negative coordinates, plus a few degenerate shapes NumPy could spell (transposed or broadcast axes of extent 0 or 1). A fabricated axis lowers to None.
  • IndexTransform.decompose() / decompose_unit_step(): total factorization into (ascending cover slices, residual transform). These are main's private reader._decompose_basic / _decompose_unit_step, moved, not copied. reader.py and iteration.py (from feat(zarr-indexing): read many selections, loading each shared chunk once zarr-developers/zarr-python#4691) now call the public methods, so there is still one implementation. A residual is applied with public API as numpy_reader.read_into(block, ReadContext(residual), out).
  • Partition.source_selection (global coordinates) and Partition.chunk_local_selection (cell-local coordinates).
  • zarr_indexing.testing: ProjectionReader and the newaxis_selections strategy.

How it composes with iteration.py. Iteration is the scheduling layer: it shares chunks across selections, prefetches and fills destinations. For each chunk it applies exactly part.projection.chunk_transform.decompose(). This PR makes that step public for consumers who own their I/O. Not done here: such consumers still can't reuse iteration's cross-selection chunk sharing, because its per-chunk pieces are private. Exposing them on SelectionPlan would be a separate PR.

One behavior change. decompose raises NoBasicSelectionError on a negative output coordinate, where readers used to let NumPy wrap it. No path from LazyArray, the readers or iteration reaches it: compose bounds-checks IndexTransform keys, empty reads make no reader calls, and iteration works in chunk-local coordinates. Only a direct reader call with a hand-built transform is affected.

Left out of #292. The _window / _projection plumbing, the 5 projection-pairing tests and the window-narrowing state-machine steps, all superseded by zarr-developers#4349. Also left out: result_into (it overlaps iter_filled), the asyncio example, and #292's packaging tweaks and fragments. #292's two runtime asserts and a nested function with nonlocal were removed by restructuring.

Verification. just test: 2112 passed, 48 skipped (dask, xarray and tensorstore not installed). just test-tensorstore: 230 passed. just typecheck: 0 errors, the same warning counts as main. just lint is clean. just docs-check (mkdocs --strict) builds.

Adversarial review, two rounds by fresh reviewer agents, neither serious:

  • Round 1:
    • ProjectionReader failed on rank-0 cells read at several points. Fixed in 07e69ec.
    • The source_selection docstring missed the IndexTransform-key transpose refusal. Fixed in 07e69ec.
    • Some spellable degenerate transforms are refused. Kept, and documented in bc4eb48.
  • Round 2 found everything correct: 20,000 raw transforms against a pointwise oracle, 6,000 LazyArray chains including rectilinear grids and partitions of partitions, and decompose compared against main on 20,000 transforms. Its minor findings were all addressed in bc4eb48:
    • An untested refusal: an empty fabricated axis. Test added.
    • The docs overclaimed the refusal set. Reworded as a conservative refusal.
    • The negative-coordinate check also applies to empty domains. Now documented.
    • No public way to apply a residual was named. numpy_reader.read_into is now named in the docstring.
    • Nit fixed: the BasicSelection name collision is noted. Nits not acted on: the guide's "cover" wording, reader read_into docs, the ProjectionReader export, and a theoretical 64-dimension limit in the state machine.

🤖 Generated with Claude Code

d-v-b added 7 commits December 4, 2026 16:05
The cover-and-residual factorization is a property of a transform, not of a
reader: `reader.py` and `iteration.py` both consume it. Move
`_decompose_basic`, `_decompose_unit_step` and their helpers into
`transform.py` unchanged, so the next commit can give them a public owner.

Assisted-by: ClaudeCode:claude-opus-5-5
Add `IndexTransform.as_basic_selection`, which spells a box transform as the
tuple of ints, slices and `None` that reads the same cells under NumPy basic
indexing, and refuses everything else with the new `NoBasicSelectionError`
(a `ValueError`). Fabricated axes lower back to the `None` that made them.

Add `IndexTransform.decompose` and `decompose_unit_step`, the total
counterparts: every transform factors into ascending cover slices plus an
in-memory residual. They are the former private `_decompose_basic` and
`_decompose_unit_step`, now the one implementation behind the built-in
readers and `iteration`. Both refuse a negative output coordinate, which
NumPy would count from the end of the source.

`BasicSelection` names the lowering's output type.

Assisted-by: ClaudeCode:claude-opus-5-5
Add `Partition.source_selection`, the basic selection on the raw wrapped
array that reads a part, and `Partition.chunk_local_selection`, the same
read relative to the part's grid cell. A consumer that plans with
`parts()` but fetches through its own I/O layer can assemble a view as
`out[part.out_selection] = source[part.source_selection]`, or read a
cached decoded cell with `cell[part.chunk_local_selection]`. Query parts,
and boxes with no basic spelling, raise `NoBasicSelectionError`.

Both are thin lowerings of `part.view.transform` and
`part.projection.chunk_transform`, which already address the source and
the cell in global and cell-local coordinates respectively.

Assisted-by: ClaudeCode:claude-opus-5-5
… machine

`ChainedIndexingStateMachine` gains a `fabricates_an_axis` rule, drawing
from the new `newaxis_selections` strategy, so views with axes no source
axis backs are reached at full weight. A new invariant runs the
consumer-owned-I/O assembly literally: every part's `source_selection` and
`chunk_local_selection` must reproduce the model under plain NumPy, query
and degenerate parts must refuse both spellings, and every part must obey
the `decompose` factorization law.

`ProjectionReader` reads through a part's projection instead of its
transform, gathering from the whole grid cell; the machine always draws it
alongside `basic_reader`, so both halves of a `ReadContext` are checked.

Assisted-by: ClaudeCode:claude-opus-5-5
Indexing a rank-0 cell with the empty tuple returns its one value however
many points repeat it, so a broadcast of a 0-d source failed to reshape.
Broadcast to one value per point. Also list transform-key transposes among
the boxes `Partition.source_selection` refuses.

Found by the adversarial review of this branch.

Assisted-by: ClaudeCode:claude-opus-5-5
Review follow-ups: say that as_basic_selection also refuses some
degenerate transforms NumPy could spell, that decompose checks negative
coordinates even on empty domains, how to apply a decompose residual
with public API, and that BasicSelection differs from zarr's type of
the same name. Pin the empty fabricated-axis refusal with a test.

Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-opus-5-5

This branch has not been deployed

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

1 participant