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

Open Beginner friendly
#775 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
numpy, pandas, python

Research direction

Run the provided Python reproduction, then inspect spatialdata_plot/pl/render.py around line 2409 and spatialdata_plot/pl/_color.py around line 791. Trace how the labels mask and ColorSpec vectors are filtered; done means render_labels(color=...) handles table rows absent from the labels without IndexError while retaining the existing behavior for matching rows.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
86
Forks
21
Avg merge
14h 50m
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from scverse/spatialdata-plot

All issues in scverse/spatialdata-plot

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.