Skip to content

render_labels raises IndexError when the table has rows for instances absent from the mask #775

Description

@Zethson

A table may annotate instances that no longer exist in the labels element — e.g. objects lost when segmentation is regenerated, or a table read from an upstream pipeline. render_labels(color=...) then fails instead of treating them as missing.

import anndata as ad, numpy as np, pandas as pd, spatialdata as sd, spatialdata_plot  # noqa
from spatialdata.models import Labels2DModel, TableModel

labels = np.zeros((16, 16), np.uint32)
labels[2:6, 2:6] = 1
labels[8:12, 8:12] = 2

obs = pd.DataFrame({"instance_id": [1, 2, 3], "region": pd.Categorical(["labels"] * 3)})
obs.index = obs.index.astype(str)
table = TableModel.parse(
    ad.AnnData(np.zeros((3, 1), np.float32), obs=obs),
    region="labels", region_key="region", instance_key="instance_id",
)
table.obs["value"] = [1.0, 2.0, 3.0]

sdata = sd.SpatialData(
    labels={"labels": Labels2DModel.parse(labels, dims=("y", "x"))},
    tables={"table": table},
)
sdata.pl.render_labels("labels", color="value", table_name="table").pl.show()
IndexError: boolean index did not match indexed array along axis 0;
size of axis is 2 but size of corresponding boolean axis is 3

The mask is computed from the instance ids present in the labels, but ColorSpec.source_vector/color_vector still have one entry per table row, so ColorSpec.filter indexes a length-2 array with a length-3 mask:

spatialdata_plot/pl/render.py:2409spatialdata_plot/pl/_color.py:791

Rendering without color works, as does dropping the unmatched rows from the table.

spatialdata 0.8.0, spatialdata-plot 0.4.2, Python 3.14.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions