Skip to content

Commit 175d013

Browse files
committed
Simplify obs/var shadow check
- Use `get_element_annotators` instead of hand-rolling the table-annotates- element check via `table.uns["spatialdata_attrs"]["region"]`. - Drop unused `TableModel` import. - Align error message phrasing with the existing `_check_color_column_collision` pattern (`` `color=...` is ambiguous ``).
1 parent b9d004e commit 175d013

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

‎src/spatialdata_plot/pl/utils.py‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
)
6565
from spatialdata._core.query.relational_query import _locate_value
6666
from spatialdata._types import ArrayLike
67-
from spatialdata.models import Image2DModel, Labels2DModel, SpatialElement, TableModel, get_table_keys
67+
from spatialdata.models import Image2DModel, Labels2DModel, SpatialElement, get_table_keys
6868
from spatialdata.transformations.operations import get_transformation
6969
from spatialdata.transformations.transformations import Scale, Translation
7070
from spatialdata.transformations.transformations import Sequence as TransformSequence
@@ -110,26 +110,30 @@ def _check_obs_var_shadow(
110110
value_to_plot: str | None,
111111
table_name: str | None,
112112
) -> None:
113-
"""Raise if `value_to_plot` exists in both `table.obs.columns` and `table.var_names`.
113+
"""Raise if ``value_to_plot`` exists in both ``table.obs.columns`` and ``table.var_names``.
114114
115-
Upstream `_get_table_origins` uses an `elif` chain, so a key that lives in both
116-
locations is silently resolved to `obs` — masking the user's likely intent of
117-
plotting gene expression. Catch this here before any value fetch.
115+
Upstream ``_get_table_origins`` uses an ``elif`` chain, so a key that lives in
116+
both locations is silently resolved to ``obs`` — masking the user's likely
117+
intent of plotting gene expression. Catch this here before any value fetch.
118+
Any ``None`` parameter short-circuits the check.
118119
"""
119-
if value_to_plot is None or table_name is None or sdata is None or table_name not in sdata.tables:
120-
return
121-
table = sdata.tables[table_name]
122-
if value_to_plot not in table.obs.columns or value_to_plot not in table.var_names:
120+
if (
121+
value_to_plot is None
122+
or table_name is None
123+
or element_name is None
124+
or sdata is None
125+
or table_name not in sdata.tables
126+
):
123127
return
124-
region = table.uns[TableModel.ATTRS_KEY][TableModel.REGION_KEY]
125-
annotates_element = element_name in region if isinstance(region, list) else element_name == region
126-
if not annotates_element:
128+
if table_name not in get_element_annotators(sdata, element_name):
127129
return
128-
raise ValueError(
129-
f"Color key '{value_to_plot}' is ambiguous: it exists in both "
130-
f"`table['{table_name}'].obs.columns` and `table['{table_name}'].var_names`. "
131-
"Rename one of them (or drop the obs column) so the intended source is unambiguous."
132-
)
130+
table = sdata.tables[table_name]
131+
if value_to_plot in table.obs.columns and value_to_plot in table.var_names:
132+
raise ValueError(
133+
f"`color={value_to_plot!r}` is ambiguous: it exists in both "
134+
f"`table[{table_name!r}].obs.columns` and `table[{table_name!r}].var_names`. "
135+
"Rename one of them (or drop the obs column) so the intended source is unambiguous."
136+
)
133137

134138

135139
def _gate_palette_and_groups(

0 commit comments

Comments
 (0)