From b28ea8f7456ab99c0bfa44f26f8b7de0d0e4a7ca Mon Sep 17 00:00:00 2001 From: Sam Minot Date: Mon, 30 Nov 2026 13:16:25 -0800 Subject: [PATCH] Read the session's table in a multi-table checkpoint's metrics dataset_metrics.py refused any checkpoint with more than one table, so a SpatialData store imported with all of its tables failed the ANALYSE task after the checkpoint was already saved. With several tables it now reads the first of the viewer sidecar's table_keys, the table the session opened on. Co-Authored-By: Claude Opus 5.5 --- DEVELOPMENT.md | 4 +++- backend/test_cli.py | 15 +++++++++++++++ nextflow/bin/dataset_metrics.py | 14 ++++++++++---- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index a90b393..d8ba591 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -955,7 +955,9 @@ is the first suspect. the archive) and reads it as the workflow's `spatialdata` type does, asserting the points are left out, the session and the sidecar open on the chosen table, and the wells are stitched into `obsm['spatial_global']` (and that world coordinates already - in `obsm['spatial']` are not stitched again). + in `obsm['spatial']` are not stitched again). It then runs the workflow's + `nextflow/bin/dataset_metrics.py` on that checkpoint and asserts it summarises the + chosen table rather than whichever sorts first. - `cd backend && PYTHONPATH=. python test_compression.py` — dataset-free unit test for `SelectiveGZipMiddleware`: which content types compress, round-trip/passthrough correctness, and the regression guard that gzip runs off the event loop (a diff --git a/backend/test_cli.py b/backend/test_cli.py index 7abeaf8..696fee3 100644 --- a/backend/test_cli.py +++ b/backend/test_cli.py @@ -14,6 +14,7 @@ import io import json import os +import subprocess import sys import tempfile @@ -228,6 +229,20 @@ def check_foreign_store(cli) -> int: assert sidecar["world_key"]["cells"] == "spatial_global", sidecar["world_key"] print("[ok] checkpoint written with the store at the archive root; its sidecar opens " "the viewer on 'cells' drawn from obsm['spatial_global']") + + # The workflow's MultiQC step reads the same checkpoint, and has to pick the + # table the session opened on out of several ('a-guides' sorts first). + metrics_dir = os.path.join(tmp, "metrics") + os.makedirs(metrics_dir) + subprocess.run([sys.executable, os.path.join(_REPO_ROOT, "nextflow", "bin", "dataset_metrics.py"), + "--sample", "store_result", "--data-type", "spatialdata", "--status", "ok", + "--checkpoint", out_zip, "--source-dir", tmp, "--outdir", metrics_dir], + check=True) + with open(os.path.join(metrics_dir, "store_result_analysis_mqc.json")) as f: + summary = json.load(f)["data"]["store_result"] + assert summary == {"cells": table.n_obs, "features": table.n_vars}, summary + print(f"[ok] the workflow's metrics step reads the 'cells' table of a multi-table " + f"checkpoint ({summary})") return 0 diff --git a/nextflow/bin/dataset_metrics.py b/nextflow/bin/dataset_metrics.py index e8528a4..833589e 100644 --- a/nextflow/bin/dataset_metrics.py +++ b/nextflow/bin/dataset_metrics.py @@ -61,13 +61,19 @@ def _parse_args(): def _read_checkpoint(checkpoint: str) -> tuple[pd.DataFrame, pd.DataFrame, dict]: - """`obs` and `var` of the checkpoint's single table, plus its `app_state`.""" + """`obs` and `var` of the table the checkpoint opens on, plus its `app_state`. + + A checkpoint imported from a SpatialData store keeps every table the store had, and + the one the analysis worked on is the first in the `viewer/` sidecar's `table_keys` + (the order the session saved them in; zarr lists them sorted by name).""" with zarr.storage.ZipStore(checkpoint, mode="r") as store: - root = zarr.open_group(store, mode="r") + root = zarr.open_group(store, mode="r", use_consolidated=False) tables = root["tables"] keys = list(tables) - if len(keys) != 1: - raise SystemExit(f"{checkpoint}: expected exactly one table, found {keys}") + if len(keys) > 1: + keys = root["viewer"].attrs["table_keys"] + if not keys: + raise SystemExit(f"{checkpoint}: has no table") group = tables[keys[0]] return read_elem(group["obs"]), read_elem(group["var"]), dict(root.attrs).get("app_state", {})