Skip to content

feat: add the outer kind for orthogonal indexing with interval lists - #4

Draft
d-v-b wants to merge 1 commit into
zarr-developers:mainfrom
d-v-b:feat/outer-kind
Draft

d-v-b wants to merge 1 commit into
zarr-developers:mainfrom
d-v-b:feat/outer-kind

Conversation

@d-v-b

@d-v-b d-v-b commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Motivated by zarr-developers/zarr-extensions#74 (comment), which needs "a JSON serialization of orthogonal indexing with slices instead of explicit indices". The use case there is declaring an array's domain as a subset of its stored domain, such as HDF5's padded boundary chunks with the padding excluded.

What this adds

A sixth message kind, outer, where each dimension is selected independently:

{ "kind": "outer", "axes": [null, null, { "intervals": [[0, 90], [100, 190], [200, 290]] }] }

This is a[:, :, np.r_[0:90, 100:190, 200:290]]. Today the only encoding is a transform whose last map lists all 270 coordinates.

Each entry in axes takes one of four forms:

Selector Meaning Desugars to
null whole axis implicit (-inf, +inf) domain, identity map
{start, stop, step?} strided range exactly one dimension of slice (source frame kept)
{indices: [...]} these coordinates, in order index_array broadcast along dim k, domain [0, m)
{intervals: [[lo, hi), ...]} concatenated half-open runs same as indices, after expansion

The first three are TensorStore's oindex in JSON form. TensorStore has no JSON encoding for index expressions, and oindex takes one slice or one integer array per axis, so intervals are new to ndsel.

TensorStore parity

  • Every success fixture checked: each fixture in conformance/outer.json was generated from the spec rules, then checked equal (IndexTransform ==) to ts.IndexTransform(input_rank=n).oindex[...] for the same selection.
  • Whole axes resolve: the implicit ±inf bounds of a null axis resolve to the array's extent when TensorStore applies the transform. I checked this on a (100, 200, 300) array.
  • Collapse to constant: empty and single-element lists become constant maps, as oindex does. This also avoids the empty-array rank problem in §7.1.
  • One deliberate difference: oindex clears the label of a dimension it indexes with an array. outer keeps the labels the message declares.

Changes

  • Spec: new §5.5. Updated kind lists, error-code rows and the note in §2.2 about which dimensions are numbered from 0. Revision-history entry under "Unreleased".
  • Schema: new outer branch and an outerAxis definition.
  • Conformance: 14 success fixtures in conformance/outer.json and 18 error fixtures in errors.json. No new error codes: selectors are untagged like output maps, so a selector mixing two forms is unknown_field.
  • Python, Rust, TypeScript: each gains normalize support, a builder (Outer / Outer::new / outer), unit tests and a usage example. Each factors the per-dimension slice rule into a helper shared by slice and outer.
  • NumPy oracle: outer fixtures are checked against source[np.ix_(...)].
  • Fix: the NumPy example executor and oracle indexed a size-1 index_array dimension by the full input coordinate, so any broadcast-shaped array failed.

Open questions

  1. Normalized size. The message grows with the number of runs, but the normalized form grows with the number of selected coordinates. The canonical core has no interval-list map, and adding one would stop normalized bodies loading as IndexTransform. The spec says so in a non-normative note, and suggests consumers use the outer message directly when they only need per-axis selections. For the zarr use case (a domain declaration in attributes) the stored message stays compact, but anything that normalizes it pays O(n).
  2. Ordering rules. indices and intervals may be unsorted, overlapping or repeated, which matches points and index_array. A domain declaration probably wants sorted, disjoint intervals. That could be a consumer-side rule, or a stricter form here with a new error code.
  3. Error precedence when one message has several errors is not specified, as for the existing kinds. The corpus avoids those cases.

Testing

  • Python: uv run pytest gives 189 passed and 11 skipped; pyright is clean.
  • Rust: cargo test passes all unit tests and the corpus.
  • TypeScript: npm test gives 137 passed; npm run typecheck is clean.

🤖 Generated with Claude Code

A selection like `a[:, :, np.r_[0:90, 100:190, 200:290]]` has no shorthand
today. The only encoding is a `transform` whose last output map lists all 270
coordinates, and a selection covering an array's domain minus a few holes
grows with the array rather than with the number of holes.

`outer` selects each dimension independently, by one of four per-axis forms:
`null` (the whole axis), a `{start, stop, step?}` range, an `{indices}` list,
or an `{intervals}` list of half-open `[lo, hi)` runs. The first three are
TensorStore's `oindex` given a JSON form; intervals are ndsel's own, because
`oindex` accepts one slice or one integer array per axis and nothing that
combines ranges.

The desugaring matches TensorStore: a whole axis gets implicit infinite
bounds, which TensorStore resolves against the array when the transform is
applied; a range is exactly one dimension of `slice`; an index or interval
list becomes an `index_array` shaped to broadcast along its own dimension,
with empty and single-element lists collapsed to `constant` maps as `oindex`
does. Every success fixture was generated from the spec rules and checked
equal to TensorStore's `oindex` on the same selection. The one deliberate
difference is labels: `oindex` clears the label of an array-indexed
dimension, and `outer` keeps the labels the message declares.

No error codes are added. Selector forms are untagged, like output maps, so
a selector carrying members of two forms is `unknown_field`.

Also fixes the broadcasting of `index_array` in the NumPy example executor
and oracle test, which indexed a size-1 array dimension by the full input
coordinate and so failed on any broadcast-shaped array.

Assisted-by: ClaudeCode:claude-opus-5
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