Skip to content

Commit 2893343

Browse files
timtreisclaude
andauthored
groups now hides NAs unless na_color is specified (#549)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 413464e commit 2893343

15 files changed

Lines changed: 227 additions & 129 deletions

src/spatialdata_plot/pl/basic.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ def render_shapes(
213213
`fill_alpha` will overwrite the value present in the cmap.
214214
groups : list[str] | str | None
215215
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
216-
them. Other values are set to NA. If element is None, broadcasting behaviour is attempted (use the same
217-
values for all elements).
216+
them. By default, non-matching elements are hidden. To show non-matching elements, set ``na_color``
217+
explicitly.
218+
If element is None, broadcasting behaviour is attempted (use the same values for all elements).
218219
palette : list[str] | str | None
219220
Palette for discrete annotations. List of valid color names that should be used for the categories. Must
220221
match the number of groups. If element is None, broadcasting behaviour is attempted (use the same values for
@@ -398,8 +399,9 @@ def render_points(
398399
value is used instead.
399400
groups : list[str] | str | None
400401
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
401-
them. Other values are set to NA. If `element` is `None`, broadcasting behaviour is attempted (use the same
402-
values for all elements).
402+
them. By default, non-matching points are filtered out entirely. To show non-matching points, set
403+
``na_color`` explicitly.
404+
If element is None, broadcasting behaviour is attempted (use the same values for all elements).
403405
palette : list[str] | str | None
404406
Palette for discrete annotations. List of valid color names that should be used for the categories. Must
405407
match the number of groups. If `element` is `None`, broadcasting behaviour is attempted (use the same values
@@ -671,7 +673,7 @@ def render_labels(
671673
table_name to be used for the element if you would like a specific table to be used.
672674
groups : list[str] | str | None
673675
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
674-
them. Other values are set to NA. The list can contain multiple discrete labels to be visualized.
676+
them. By default, non-matching labels are hidden. To show non-matching labels, set ``na_color`` explicitly.
675677
palette : list[str] | str | None
676678
Palette for discrete annotations. List of valid color names that should be used for the categories. Must
677679
match the number of groups. The list can contain multiple palettes (one per group) to be visualized. If

src/spatialdata_plot/pl/render.py

Lines changed: 148 additions & 98 deletions
Large diffs are not rendered by default.

src/spatialdata_plot/pl/utils.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -990,31 +990,17 @@ def _build_alignment_dtype_hint(
990990
table_name: str | None,
991991
) -> str:
992992
"""Build a diagnostic hint string for dtype mismatches between element and table indices."""
993-
hints: list[str] = []
994-
color_index_dtype = getattr(color_series.index, "dtype", None)
995-
element_index_dtype = getattr(getattr(element, "index", None), "dtype", None) if element is not None else None
996-
997-
table_instance_dtype = None
998-
instance_key = None
999-
if table_name is not None and sdata is not None and table_name in sdata.tables:
1000-
table = sdata.tables[table_name]
1001-
try:
1002-
_, _, instance_key = get_table_keys(table)
1003-
except (KeyError, ValueError, TypeError, AttributeError):
1004-
instance_key = None
1005-
if instance_key is not None and hasattr(table, "obs") and instance_key in table.obs:
1006-
table_instance_dtype = table.obs[instance_key].dtype
1007-
1008-
if (
1009-
element_index_dtype is not None
1010-
and table_instance_dtype is not None
1011-
and element_index_dtype != table_instance_dtype
1012-
):
1013-
hints.append(f"element index dtype is {element_index_dtype}, '{instance_key}' dtype is {table_instance_dtype}")
1014-
if color_index_dtype is not None and element_index_dtype is not None and color_index_dtype != element_index_dtype:
1015-
hints.append(f"color index dtype is {color_index_dtype}, element index dtype is {element_index_dtype}")
1016-
1017-
return f" (hint: {'; '.join(hints)})" if hints else ""
993+
el_dtype = getattr(getattr(element, "index", None), "dtype", None)
994+
if el_dtype is None or table_name is None or sdata is None or table_name not in sdata.tables:
995+
return ""
996+
try:
997+
_, _, instance_key = get_table_keys(sdata.tables[table_name])
998+
except (KeyError, ValueError):
999+
return ""
1000+
tbl_dtype = sdata.tables[table_name].obs[instance_key].dtype
1001+
if el_dtype != tbl_dtype:
1002+
return f" (hint: element index dtype is {el_dtype}, '{instance_key}' dtype is {tbl_dtype})"
1003+
return ""
10181004

10191005

10201006
def _set_color_source_vec(
@@ -1119,7 +1105,9 @@ def _set_color_source_vec(
11191105
table_to_use = None
11201106
else:
11211107
table_keys = list(sdata.tables.keys())
1122-
if table_keys:
1108+
if len(table_keys) == 1:
1109+
table_to_use = table_keys[0]
1110+
elif len(table_keys) > 1:
11231111
table_to_use = table_keys[0]
11241112
logger.warning(f"No table name provided, using '{table_to_use}' as fallback for color mapping.")
11251113
else:
-4.66 KB
Loading
-1.72 KB
Loading
-1.77 KB
Loading
-9.92 KB
Loading
-12.4 KB
Loading
58.1 KB
Loading
44.2 KB
Loading

0 commit comments

Comments
 (0)