Skip to content

Keep the batch dim on batches of one and accept a SpatialData object in SpatialDataset - #22

Merged
rushin682 merged 2 commits into
mainfrom
batch-inference
Sep 14, 2026
Merged

rushin682 merged 2 commits into
mainfrom
batch-inference

Conversation

@rushin682

Copy link
Copy Markdown
Collaborator

Two output-preserving package changes needed by a batch inference runner that processes many SpatialData stores in checkpointed blocks of cells (the runner itself is cluster-specific and stays out of the package).

What changes

  • FlowPipeline.__call__ squeezed every singleton dim of the sampled (B, n_genes, 1) tensor, so a batch of size one collapsed to (n_genes,) and broke the concatenation and the * std + mean broadcast. squeeze(-1) drops only the gene-token dim; for B > 1 the result is identical.
  • SpatialDataset accepts an already-read SpatialData object as zarr_path and uses it as is, so a caller can sd.read_zarr(path, selection=("images", "shapes", "tables")) once (skipping the large transcripts element; ~9 s instead of a full load on a 465k-cell store) and share the object with the dataset, the ground truth and any write-back. Paths still go through sd.read_zarr(zarr_path) unchanged.

Tests

  • tests/conftest.py gains make_synthetic_store, which writes a minimal SpatialData zarr (multiscale he_image, circular nucleus_boundaries, Poisson-count table, identity transforms), and a synthetic_store fixture. The old placeholder comment said such a store was impractical to fabricate; with spatialdata 0.8 it is ~15 lines.
  • The placeholder SpatialDataset test is replaced by real tests: length, the demo image transform yields (3, 224, 224), a border cell yields the blank patch, construction from a SpatialData object with selection= gives identical items, gene-list subsetting keeps the requested order.
  • test_inference.py adds test_flow_pipeline_handles_batch_size_one, which fails against the previous squeeze().

Verified

CPU, against 2436b23: pytest tests 33 passed, 4 skipped (the apex/flash-attn-only modules, as before); ruff check and ruff format --check clean on src and tests.

Not in this PR (follow-up candidate): a shared helper for the demo model/transform constants so the README, the notebook and downstream scripts stop duplicating them.

🤖 Generated with Claude Code

https://claude.ai/code/session_016zPfR5j2rpSaamQUrLmRuJ

…in SpatialDataset

Two output-preserving changes needed to run inference over many stores in
blocks:

- `FlowPipeline.__call__` squeezed every singleton dim of the sampled
  `(B, n_genes, 1)` tensor, so a batch of size one (the last block of a
  store) collapsed to `(n_genes,)` and broke the concatenation and the
  `* std + mean` broadcast. `squeeze(-1)` drops only the gene-token dim;
  for B > 1 the result is identical.
- `SpatialDataset` accepts an already-read `SpatialData` object as
  `zarr_path` and uses it as is, so a caller can `sd.read_zarr(path,
  selection=("images", "shapes", "tables"))` once (skipping the large
  `transcripts` element) and share it with the dataset, the ground truth
  and the write-back. Paths still go through `sd.read_zarr(zarr_path)`.

Tests: `tests/conftest.py` gains `make_synthetic_store`, which writes a
minimal SpatialData zarr (multiscale `he_image`, circular
`nucleus_boundaries`, Poisson-count `table`, identity transforms) and a
`synthetic_store` fixture. The placeholder `SpatialDataset` test is
replaced by real tests of length, the demo image transform, the blank
patch for a border cell, construction from a `SpatialData` object with
`selection=`, and gene-list subsetting; `test_inference.py` adds
`test_flow_pipeline_handles_batch_size_one`, which fails against the
previous `squeeze()`.

Verified on CPU against 2436b23: `pytest tests` 33 passed, 4 skipped
(the apex/flash-attn-only modules, as before); `ruff check` and
`ruff format --check` clean on `src` and `tests`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016zPfR5j2rpSaamQUrLmRuJ
@codecov-commenter

codecov-commenter commented Sep 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 47.63%. Comparing base (2436b23) to head (6950174).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #22      +/-   ##
==========================================
+ Coverage   42.46%   47.63%   +5.17%     
==========================================
  Files          10       10              
  Lines        1015     1016       +1     
==========================================
+ Hits          431      484      +53     
+ Misses        584      532      -52     
Files with missing lines Coverage Δ
src/phoenix/datasets/zarr_dataset.py 97.14% <100.00%> (+75.40%) ⬆️
src/phoenix/helpers/inference.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The pre-commit CI job failed on its mypy hook: `AnnData.__init__` annotates
`obsm` as `Mapping[str, Sequence[Any]]`, and the `(n_cells, 2)` coordinate
array passed there is not a `Sequence`, so `hatch check types` reported
`tests/conftest.py:75: error: Dict entry 0 has incompatible type` and the
job — and with it the "Tests pass in all hatch environments" gate — went red.

Assigning through `adata.obsm["spatial"]` instead goes through `AxisArrays`,
which is typed for arrays, and matches how the existing `adata` fixture in
this file sets the same key. The written store is unchanged.

Verified on CPU in the hatch envs: `hatch check types` clean on 22 files,
`prek run --all-files` (the exact CI invocation) all 13 hooks pass,
`hatch test` 33 passed, 4 skipped.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@rushin682
rushin682 merged commit 801e618 into main Sep 14, 2026
7 checks passed
@rushin682
rushin682 deleted the batch-inference branch September 14, 2026 15:33
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