Skip to content

feat(tests): Reference stores per storage format version - #1346

Open
ajkswamy wants to merge 4 commits into
mainfrom
ref_stores_testing
Open

ajkswamy wants to merge 4 commits into
mainfrom
ref_stores_testing

Conversation

@ajkswamy

@ajkswamy ajkswamy commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator
AI use transparency
  • Most of this PR was drafted with Claude (Anthropic's Claude Opus 5.5): the analysis behind it, the
    generator and test code, the commit messages and this description.
  • I made the decisions, ran the generator and the tests locally to fix bugs, reviewed the code, and generated
    and committed the stores myself.

What this adds

Small example datasets ("reference stores") written by older spatialdata releases, and tests
that check the current code still reads them correctly.

Part of #1345. No library code changes; this only adds tests and test data.

Why

Our existing format tests write a dataset and read it back with the same code. If writing and
reading change together, those tests still pass, even when files written by older versions no
longer read correctly. Before restructuring the reading code (#), we need a safety net
that uses files the current code did not write.

What's in it

  • 8 stores, about 300 KiB in total: for each of the four storage format versions (0.1 to 0.4),
    one store written by the first release that used it and one by the last.

    Storage format Written by
    0.1 0.1.0 and 0.2.1
    0.2 0.2.2 and 0.3.0
    0.3 0.4.0 and 0.5.0
    0.4 0.6.0 and 0.9.0
  • Every store holds the same small object (one image, labels, points, two shapes elements and a
    table), defined in tests/data/reference_stores/build.py.

  • A generator script (generate.py) that installs each old release in a temporary environment,
    writes its store, checks the recorded format versions against the specification, and zips the
    store. Each folder keeps the exact package list used (requirements.txt), so the stores can be
    reproduced. The README explains how and when to regenerate them (normally never).

  • Tests (tests/io/test_reference_stores.py) that read each store and compare it with the
    object from build.py. They also check sdata.path and the selection argument, which no test
    covered so far.

How the old releases are installed

Old releases have to run in the environment of their time:

  • Packages are limited to versions available up to three days after each release.
  • Python is the version available on the release date (3.12 where the release's CI tested it,
    otherwise the newest version it tested). spatialdata 0.1.0 therefore runs on Python 3.10.14, and
    the others on Python 3.12.4 to 3.12.16. Newer Python versions break some old dependencies, for
    example dask 2024.2.1, which 0.1.0 requires.

What we learned about the formats

Writing one object with every release showed a few things the formats don't keep. build.py builds
the object in the form every format stores it, so all stores can be compared with one object:

  • Raster transformations are stored over all of the image's axes: a scale over (y, x) on a
    (c, y, x) image comes back as a scale over (c, y, x).
  • Format 0.1 doesn't keep the column order of shapes, and stores only one geometry type per shapes
    element (a mix of polygons and multipolygons comes back as multipolygons only).

Mixing geometry types in one shapes element is therefore not covered. The README lists this and
other variants no store covers.

For reviewers

  • No library code changes.
  • The tests depend on spatialdata.testing, which compares strictly and has become stricter
    over time (for example Add tests for sdata.attrs across formats #1015). A future change there can make these tests fail even though no
    store changed.
  • Found along the way: comparing two Scale transformations over different axes raises an
    error instead of returning False. Fixing this would need a separate PR.

claude and others added 4 commits December 2, 2026 17:40
…ersion

Add tests/data/reference_stores/ with a generator for committed
reference stores: each storage format version (0.1-0.4) written by the
first and the last spatialdata release that wrote it by default
(0.1.0/0.2.1, 0.2.2/0.3.0, 0.4.0/0.5.0, 0.6.0/0.9.0). The reader on
main can then be tested against bytes it did not write; the round-trip
tests in test_format.py use the same code to write and read.

- generate.py (standard library only) resolves each writer's
  environment into requirements.txt with `uv pip compile --universal`
  and --exclude-newer (PyPI upload date + 3 days), installs it with the
  writer's Python version, writes the store, checks the recorded
  container, element, Zarr and NGFF versions against the specification,
  and zips the store reproducibly.
- write.py runs inside the writer's environment.
- build.py defines the object every store holds. It is frozen and uses
  only public APIs available from spatialdata 0.1.0 to main.

Each writer runs on the Python of its release date: 3.12 if the
release's CI tested it, otherwise the newest minor version it tested, at
the last patch release published on or before the release (3.10.14 for
0.1.0; 3.12.4 to 3.12.16 for the others). Old dependencies break on
later Python releases: dask <= 2024.2.1 (pinned by 0.1.0) fails to
import on Python >= 3.12.3, and pygeos 0.14 has no Python 3.12 wheels.
Writers on Python < 3.12 also lock setuptools, which virtual
environments of those Pythons included and which xarray-schema needs
(pkg_resources) without declaring it. `uv pip sync` also gets
--exclude-newer, so source builds use build tools of the release date.

build.py holds elements in the form every storage format keeps them:
the image's Scale over all of its axes (writers store a ("y", "x")
Scale over ("c", "y", "x")), the circles with geometry first, and the
polygons of one geometry type (format 0.1 stores shapes as single-type
ragged arrays without column order).

Each store lives in storage-format-<version>_spatialdata-<release>/,
with its requirements.txt and zip. The stores themselves are added in a
separate commit, generated with this script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011ecEfSxdLEEKahggNs3nqz
Read every reference store in tests/data/reference_stores with the
current code and compare it with the object it was written from
(build.py). Also check that sdata.path points to the store and that
`selection` reads only the selected element types; neither was covered
by any test so far, and the planned restructuring of read_zarr changes
both.

Accepted differences between a store and build.py go into
ACCEPTED_DIFFERENCES, each with a reason; it starts empty. A missing
store fails the tests rather than skipping them, so these tests fail
until the generated stores are committed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011ecEfSxdLEEKahggNs3nqz
@ajkswamy ajkswamy changed the title Ref stores testing feat(tests): Reference stores per storage format version Dec 2, 2026
@jan-glx jan-glx closed this Dec 4, 2026
@jan-glx jan-glx reopened this Dec 4, 2026
@codecov

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.79%. Comparing base (c1ca428) to head (fce176f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1346      +/-   ##
==========================================
+ Coverage   91.77%   91.79%   +0.02%     
==========================================
  Files          53       53              
  Lines        8398     8398              
==========================================
+ Hits         7707     7709       +2     
+ Misses        691      689       -2     

see 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jan-glx jan-glx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @ajkswamy, this looks really useful! A safety net built from bytes the current code didn't write is exactly what the read_zarr refactor needs. I was worried about committing the stores at first, but at ~260 KiB for the whole folder (~190 KiB of zips) that's totally fine.

CI hadn't run on this PR (the pull_request event seems to have been dropped), so I closed and reopened it to re-trigger it.

Three questions/suggestions:

I. Keep the generator files out of linting and dependency scanning. Right now generate.py and write.py go through ruff-check/ruff-format (hence the # fmt: skips). The eight requirements.txt files go through the pre-commit text hooks. GitHub's dependency graph is enabled for this repo and reads requirements.txt manifests anywhere, so the old 2024/2025 pins would show up as dependencies of spatialdata, and in security alerts once those are on. (mypy and coverage are already fine: files = ["src"] and run.source = ["spatialdata"].) Could we exclude them explicitly? For example, a top-level exclude: ^tests/data/reference_stores/ in .pre-commit-config.yaml plus extend-exclude = ["tests/data/reference_stores"] under [tool.ruff]. For the scanners, either put generate.py, write.py and the requirements.txt files into a zip too, or give them names nothing picks up (e.g. requirements.lock, generate.py.dont_run). build.py can stay as is, since the tests import it.

II. Would it make sense to also add these stores to the spatialdata-sandbox datasets on the scverse S3? Those are at https://exampledata.scverse.org/spatialdata/spatialdata-sandbox/ (uploaded by spatialdata-integration-testing, listed on the datasets page of the docs). All versions there were written by spatialdata 0.7.1–0.9.0, i.e. storage format 0.4. These eight stores would add formats 0.1–0.3, and other readers (napari-spatialdata, R/JS readers, …) could test against them without cloning this repo.

III. Could the README show what the reference object looks like? Right now it only says "the same small object, one element of each type, defined in build.py" (it's actually two shapes elements). A short overview would save maintainers from reading build.py when a test fails, e.g. a small table or the printed SpatialData repr. It could cover:

  • image_multiscale: 3×16×16 uint8, channels r/g/b, 2 scales
  • labels_single_scale: 16×16, labels 1 and 2
  • points: 5 points with gene/cell_id
  • circles: 2 circles with radius
  • polygons: 2 MultiPolygons, one with a hole
  • table: 2×3, annotating circles
  • coordinate systems global and aligned (Scale on the image, Translation on the circles)

@ajkswamy

ajkswamy commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks @jan-glx for the review, great ideas! I will implement your third suggestion as it is.

@jan-glx @LucaMarconato How about I move generate.py and write.py to spatialdata-sandbox? I would also rename write.py to to_zarr.py to better match the existing nomenclature there. We could also commit the requirements.txt files there for "locking" the envs that generated the ref stores.

We could also:

  1. modify generate.py to first check if the ref store is not on https://exampledata.scverse.org/spatialdata/spatialdata-sandbox/ before generating.
  2. add a commands and dags to spatialdata-integration-testing, that each seperately generates and uploads ref stores to https://exampledata.scverse.org/spatialdata/spatialdata-sandbox/
  3. update test_reference_stores.py here to download data from https://exampledata.scverse.org/spatialdata/spatialdata-sandbox/, using the helpers Tim added with the cells dataset.

With this setup, we would need to modify the config of formats + sdata versions in generate.py and run the dags when we create a new storage format version

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.

3 participants