Skip to content

More informative error when subsetting results in no observations retained in Visium HD #1096

Open

Description

@matt-sd-watson

When filtering bin spots in Visium HD, when using a sufficiently large expression filter:

mask = (sdata.tables[table].X > 0).sum(axis=1).A1 >= 500

The resulting table, if filtered using the mask, would contain no observations.

However, the error provided does not indicate this, and instead suggests a different type of data mismatch:

schema = get_model(value)
  File "/home/admin/.local/lib/python3.10/site-packages/spatialdata/models/models.py", line 1195, in get_model
    return _validate_and_return(TableModel, e)
  File "/home/admin/.local/lib/python3.10/site-packages/spatialdata/models/models.py", line 1178, in _validate_and_return
    schema().validate(e)
  File "/home/admin/.local/lib/python3.10/site-packages/spatialdata/models/models.py", line 1056, in validate
    self._validate_table_annotation_metadata(data)
  File "/home/admin/.local/lib/python3.10/site-packages/spatialdata/models/models.py", line 1034, in _validate_table_annotation_metadata
    raise ValueError(f"Regions in the AnnData object and `{attr[self.REGION_KEY_KEY]}` do not match.")
ValueError: Regions in the AnnData object and `region` do not match.

It may be useful to first have a check that the relevant table is non-empty before throwing this ValueError, as it suggested to be that a metadata column was corrupted or removed during processing.

Activity

  1. LucaMarconato commented on Sep 13, 2026

    @LucaMarconato
    Member

    This whole message is AI-generated. The issue was automatically discovered and reported by an AI agent (Claude) during an autonomous bug hunt on the spatialdata code base. It has not been verified or triaged by a human yet; the needs: triage label is set so that a maintainer can confirm it. The reproduction script below was executed by the agent in an isolated environment (see Environment) and its output is pasted verbatim.

    I re-attempted to reproduce this issue on the current main branch (commit ccf1ea048d054b6624214bf618008a9f9ae223e0) as part of a triage pass over open issues, and confirmed it reproduces.

    Reproduction script (PEP 723 inline metadata; run with uv run repro.py, no other setup needed):

    # /// script
    # requires-python = ">=3.12"
    # dependencies = [
    #     "spatialdata @ git+https://github.com/scverse/spatialdata.git@ccf1ea048d054b6624214bf618008a9f9ae223e0",
    # ]
    # ///
    """#1096: a table filtered to zero observations fails validation with a misleading 'Regions ... do not match' error."""
    import warnings
    import numpy as np
    import pandas as pd
    from anndata import AnnData
    from spatialdata import SpatialData
    from spatialdata.models import Labels2DModel, TableModel
    
    warnings.simplefilter("ignore")
    labels = Labels2DModel.parse(np.array([[0, 1], [2, 3]], dtype=np.uint16))
    obs = pd.DataFrame({"region": pd.Categorical(["labels"] * 3), "instance_id": [1, 2, 3], "n_counts": [10, 20, 30]})
    table = TableModel.parse(AnnData(X=np.ones((3, 2)), obs=obs), region="labels", region_key="region", instance_key="instance_id")
    sdata = SpatialData(labels={"labels": labels}, tables={"table": table})
    mask = table.obs["n_counts"] >= 500  # too strict: keeps nothing
    empty = table[mask].copy()
    print("filtered table shape:", empty.shape)
    try:
        sdata.tables["filtered"] = empty
        print("assigning the empty table: OK")
        bug = False
    except Exception as e:  # noqa: BLE001
        print("assigning the empty table:", type(e).__name__, str(e)[:120])
        bug = "do not match" in str(e)
    print("VERDICT:", "REPRODUCED (misleading error for an empty table)" if bug else "NOT REPRODUCED")

    Environment: uv run --no-project --python 3.13, which resolves spatialdata from git+https://github.com/scverse/spatialdata.git@ccf1ea048d054b6624214bf618008a9f9ae223e0 (extra dependencies, if any, are pinned in the script's inline metadata above).

    Output (verbatim, only uv package-resolution/install log lines removed):

    filtered table shape: (0, 2)
    assigning the empty table: ValueError Regions in the AnnData object and `region` do not match.
    VERDICT: REPRODUCED (misleading error for an empty table)
    exit=0
    

    Agent's severity assessment: low (see the priority: * label added to this issue).


    Automatically generated; discovered by an AI agent (Claude) and not yet reviewed by a human.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions