diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 524bc84..a90b393 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -950,8 +950,8 @@ is the first suspect. embedding canvas the read could not have built, and no duplicate spatial one; it prints a `[skip]` line when that fixture is absent. A third pass needs no fixture: it writes a SpatialData `.zarr.zip` shaped like an instrument export (two wells placed in a shared - `global` system, region-local coordinates stored as an `X`/`Y` data frame, a points - element, several tables with the cell table not first, the store one folder down in + `global` system, region-local coordinates stored as an `X`/`Y` data frame, a text + data frame in another `obsm` entry, a points element, several tables with the cell table not first, the store one folder down in 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 diff --git a/backend/app/sessions/transform.py b/backend/app/sessions/transform.py index bcb6c79..29b5fb2 100644 --- a/backend/app/sessions/transform.py +++ b/backend/app/sessions/transform.py @@ -184,7 +184,10 @@ def world_space(sdata, table) -> tuple[str, str | None]: if key == "spatial": continue arr = np.asarray(table.obsm[key]) - if arr.ndim != 2 or arr.shape[0] != world.shape[0] or arr.shape[1] < 2: + # An obsm entry may be a data frame of text (an Element export's per-cell guide + # assignment), which can't hold coordinates and won't convert to float. + if (not np.issubdtype(arr.dtype, np.number) or arr.ndim != 2 + or arr.shape[0] != world.shape[0] or arr.shape[1] < 2): continue if np.abs(arr[:, :2].astype(float) - world).max() <= 1e-3 * span: return key, system diff --git a/backend/app/transport/arrow.py b/backend/app/transport/arrow.py index 8425e5d..7300e49 100644 --- a/backend/app/transport/arrow.py +++ b/backend/app/transport/arrow.py @@ -148,6 +148,10 @@ def describe_fields(adata, sdata) -> dict: obsm = [] for k, v in adata.obsm.items(): a = np.asarray(v) + # A data frame of text (a per-cell guide assignment) is neither coordinates nor an + # embedding, and `resolve_field` cannot draw it. + if not np.issubdtype(a.dtype, np.number): + continue obsm.append({"name": k, "n_components": int(a.shape[1]) if a.ndim > 1 else 1}) return { "obs": obs, diff --git a/backend/test_cli.py b/backend/test_cli.py index 04734ce..7abeaf8 100644 --- a/backend/test_cli.py +++ b/backend/test_cli.py @@ -132,6 +132,9 @@ def write_foreign_store(tmp: str) -> str: obs=obs[["region_key", "cell_id"]].assign( region_key=lambda o: o["region_key"].astype("category"))) cells.obsm["spatial"] = obs[["X", "Y"]] + # Other obsm entries can be data frames of text, such as each cell's assigned guide. + cells.obsm["target_assignment"] = pd.DataFrame( + {"guide": [f"CTNNB1_{i % 3}" for i in range(len(obs))], "n_reads": 1}, index=obs.index) region = list(labels) tables = { "a-guides": TableModel.parse(cells[:, :2].copy(), region=region, @@ -204,6 +207,12 @@ def check_foreign_store(cli) -> int: print("[ok] region-local coordinates stitched into obsm['spatial_global'], " "and the canvas draws those") + from app.transport.arrow import describe_fields + offered = [f["name"] for f in describe_fields(table, sdata)["obsm"]] + assert "target_assignment" not in offered and "spatial_global" in offered, offered + print(f"[ok] a text obsm entry is skipped as coordinates and not offered in pickers " + f"(offered: {offered})") + # A writer that already put world coordinates in obsm['spatial'] is left alone. from app.sessions.transform import stitch_world_coordinates table.obsm["spatial"] = np.asarray(table.obsm.pop("spatial_global"))