Skip to content

Feat/fast subslice reads#70

Merged
BrianWhitneyAI merged 6 commits into
mainfrom
feat/fast-subslice-reads
Jun 24, 2026
Merged

Feat/fast subslice reads#70
BrianWhitneyAI merged 6 commits into
mainfrom
feat/fast-subslice-reads

Conversation

@toloudis

@toloudis toloudis commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Previously get_image_data called reshape_data(self.data, ...), which loaded the entire image before discarding everything outside the requested indices. This PR introduces a _read_indexed seam that receives the per-dimension getitem spec, so a reader that can read sub-regions cheaply can override it and skip the full read.

What changed

reshape_data split into two halves (transforms.py), with reshape_data retained as a thin wrapper so existing callers are unaffected:

  • compute_dim_specs(shape, given_dims, return_dims, **kwargs) — the indexer-building half. Turns the kwargs selection into one getitem op per dimension plus the resulting dim order.
  • finalize_dims(data, new_dims, given_dims, return_dims) — the reshape/transpose half. Pads missing return dims with depth-1 axes and transposes to the requested order.

_read_indexed seam (reader.py):

  • New Reader._read_indexed(given_dims, dim_specs) -> np.ndarray. The default implementation is self.data[tuple(dim_specs)] (full read + slice), so behavior is unchanged for every existing reader.
  • Readers that support cheap sub-region reads override this method.
  • get_image_data now runs compute_dim_specs_read_indexedfinalize_dims.

Typed dim selections/specs (types.py):

  • DimSelection = Union[int, List[int], Tuple[int, ...], range, slice] — a caller's per-dimension selection.
  • DimSpec = Union[int, slice, List[int]] — the normalized getitem op produced by compute_dim_specs.

toloudis and others added 2 commits June 11, 2026 11:33
…alize_dims

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…uested region

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread bioio_base/tests/test_reader.py Outdated
Comment thread bioio_base/tests/test_reader.py Outdated
Comment thread bioio_base/transforms.py
@BrianWhitneyAI
BrianWhitneyAI marked this pull request as ready for review June 18, 2026 16:25
@BrianWhitneyAI
BrianWhitneyAI requested a review from a team as a code owner June 18, 2026 16:25
Replace the Any typing on compute_dim_specs/reshape_data/_read_indexed with
new DimSelection and DimSpec aliases, and parameterize the get_image_data and
compute_dim_specs tests. finalize_dims test now verifies a real transpose.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread bioio_base/reader.py Outdated

This lets ``get_image_data`` read only the requested sub-region.
The default implementation materializes the full image and slices it;
readers that can read sub-regions cheaply should override it.

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.

Is this enough of a reference for readers to override?

@toloudis toloudis Jun 18, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Here's how I would write the "This lets..." paragrah, starting over:

The default behavior here is to call self.data which can cause a delayed-load 
of the whole image.  Readers should override this function in order to do 
more optimal finegrained data reads based on the `dim_specs`. 

Comment thread bioio_base/reader.py Outdated
sub-region. The default implementation materializes the full image and
slices it; readers that can read sub-regions cheaply should override it.

The result MUST equal ``self.data[tuple(dim_specs)]`` — i.e. integer specs

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.

Is this something we want in the docstring? my take was no

@BrianWhitneyAI BrianWhitneyAI 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.

Approved as Co-author, weigh review accordingly

Comment thread bioio_base/transforms.py
# All checks and operations passed, append dim operation to getitem ops
dim_specs.append(dim_spec)
# All checks and operations passed, append dim operation to getitem ops.
dim_specs.append(cast(types.DimSpec, dim_spec))

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.

Might be nice to cast this earlier (maybe at kwargs.get(dim), line 180)

Comment thread bioio_base/transforms.py
def reshape_data(
data: types.ArrayLike, given_dims: str, return_dims: str, **kwargs: Any
) -> types.ArrayLike:
def compute_dim_specs(

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.

Should this and finalize_dims be private (_compute_dim_specs) or are they intended to be used outside of reshape_data?

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.

they're public because get_image_data calls them directly with _read_indexed in
between, so a subclass reader can build the indexer, read only that sub-region, then pad/transpose.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@BrianWhitneyAI
BrianWhitneyAI merged commit 00ea97e into main Jun 24, 2026
16 checks passed
@BrianWhitneyAI
BrianWhitneyAI deleted the feat/fast-subslice-reads branch June 24, 2026 16:12
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.

4 participants