Keep the batch dim on batches of one and accept a SpatialData object in SpatialDataset - #22
Merged
Merged
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 + meanbroadcast.squeeze(-1)drops only the gene-token dim; forB > 1the result is identical.SpatialDatasetaccepts an already-readSpatialDataobject aszarr_pathand uses it as is, so a caller cansd.read_zarr(path, selection=("images", "shapes", "tables"))once (skipping the largetranscriptselement; ~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 throughsd.read_zarr(zarr_path)unchanged.Tests
tests/conftest.pygainsmake_synthetic_store, which writes a minimal SpatialData zarr (multiscalehe_image, circularnucleus_boundaries, Poisson-counttable, identity transforms), and asynthetic_storefixture. The old placeholder comment said such a store was impractical to fabricate; with spatialdata 0.8 it is ~15 lines.SpatialDatasettest is replaced by real tests: length, the demo image transform yields(3, 224, 224), a border cell yields the blank patch, construction from aSpatialDataobject withselection=gives identical items, gene-list subsetting keeps the requested order.test_inference.pyaddstest_flow_pipeline_handles_batch_size_one, which fails against the previoussqueeze().Verified
CPU, against 2436b23:
pytest tests33 passed, 4 skipped (the apex/flash-attn-only modules, as before);ruff checkandruff format --checkclean onsrcandtests.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