Skip to content

Commit c5c55b3

Browse files
authored
Add warning if table_name and color are specified but the table doesn't annotate the element (#490)
1 parent c60c29d commit c5c55b3

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

src/spatialdata_plot/pl/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,8 @@ def _validate_label_render_params(
18341834

18351835
element_params[el]["table_name"] = None
18361836
element_params[el]["color"] = None
1837-
if (color := param_dict["color"]) is not None:
1837+
color = param_dict["color"]
1838+
if color is not None:
18381839
color, table_name = _validate_col_for_column_table(sdata, el, color, param_dict["table_name"], labels=True)
18391840
element_params[el]["table_name"] = table_name
18401841
element_params[el]["color"] = color
@@ -1996,6 +1997,11 @@ def _validate_col_for_column_table(
19961997
if table_name not in tables or (
19971998
col_for_color not in sdata[table_name].obs.columns and col_for_color not in sdata[table_name].var_names
19981999
):
2000+
warnings.warn(
2001+
f"Table '{table_name}' does not annotate element '{element_name}'.",
2002+
UserWarning,
2003+
stacklevel=2,
2004+
)
19992005
table_name = None
20002006
col_for_color = None
20012007
else:

tests/pl/test_render_labels.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,25 @@ def test_plot_can_handle_dropping_small_labels_after_rasterize_categorical(self,
299299
sdata_blobs = self._prepare_small_labels(sdata_blobs)
300300

301301
sdata_blobs.pl.render_labels("blobs_labels_large", color="category", table_name="table").pl.show()
302+
303+
304+
def test_warns_when_table_does_not_annotate_element(sdata_blobs: SpatialData):
305+
# Work on an independent copy since we mutate tables
306+
sdata_blobs_local = deepcopy(sdata_blobs)
307+
308+
# Create a table that annotates a DIFFERENT element than the one we will render
309+
other_table = sdata_blobs_local["table"].copy()
310+
other_table.obs["region"] = "blobs_multiscale_labels"
311+
other_table.uns["spatialdata_attrs"]["region"] = "blobs_multiscale_labels"
312+
sdata_blobs_local["other_table"] = other_table
313+
314+
# Rendering "blobs_labels" with a table that annotates "blobs_multiscale_labels"
315+
# should raise a warning and fall back to using no table.
316+
with pytest.warns(UserWarning, match="does not annotate element"):
317+
(
318+
sdata_blobs_local.pl.render_labels(
319+
"blobs_labels",
320+
color="channel_0_sum",
321+
table_name="other_table",
322+
).pl.show()
323+
)

tests/pl/test_render_points.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import matplotlib.pyplot as plt
66
import numpy as np
77
import pandas as pd
8+
import pytest
89
import scanpy as sc
910
from anndata import AnnData
1011
from matplotlib.colors import Normalize
@@ -350,3 +351,25 @@ def test_plot_can_annotate_points_with_table_layer(self, sdata_blobs: SpatialDat
350351
sdata_blobs["points_table"].layers["normalized"] = RNG.random((nrows, ncols))
351352

352353
sdata_blobs.pl.render_points("blobs_points", color="feature0", size=10, table_layer="normalized").pl.show()
354+
355+
356+
def test_warns_when_table_does_not_annotate_element(sdata_blobs: SpatialData):
357+
# Work on an independent copy since we mutate tables
358+
sdata_blobs_local = deepcopy(sdata_blobs)
359+
360+
# Create a table that annotates a DIFFERENT element than the one we will render
361+
other_table = sdata_blobs_local["table"].copy()
362+
other_table.obs["region"] = "blobs_labels" # Different from blobs_points
363+
other_table.uns["spatialdata_attrs"]["region"] = "blobs_labels"
364+
sdata_blobs_local["other_table"] = other_table
365+
366+
# Rendering "blobs_points" with a table that annotates "blobs_labels"
367+
# should raise a warning and fall back to using no table.
368+
with pytest.warns(UserWarning, match="does not annotate element"):
369+
(
370+
sdata_blobs_local.pl.render_points(
371+
"blobs_points",
372+
color="channel_0_sum",
373+
table_name="other_table",
374+
).pl.show()
375+
)

tests/pl/test_render_shapes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import matplotlib.pyplot as plt
77
import numpy as np
88
import pandas as pd
9+
import pytest
910
import scanpy as sc
1011
from anndata import AnnData
1112
from matplotlib.colors import Normalize
@@ -564,3 +565,25 @@ def test_plot_can_annotate_shapes_with_table_layer(self, sdata_blobs: SpatialDat
564565
sdata_blobs["circle_table"].layers["normalized"] = RNG.random((nrows, ncols))
565566

566567
sdata_blobs.pl.render_shapes("blobs_circles", color="feature0", table_layer="normalized").pl.show()
568+
569+
570+
def test_warns_when_table_does_not_annotate_element(sdata_blobs: SpatialData):
571+
# Work on an independent copy since we mutate tables
572+
sdata_blobs_local = deepcopy(sdata_blobs)
573+
574+
# Create a table that annotates a DIFFERENT element than the one we will render
575+
other_table = sdata_blobs_local["table"].copy()
576+
other_table.obs["region"] = "blobs_points" # Different from blobs_circles
577+
other_table.uns["spatialdata_attrs"]["region"] = "blobs_points"
578+
sdata_blobs_local["other_table"] = other_table
579+
580+
# Rendering "blobs_circles" with a table that annotates "blobs_points"
581+
# should raise a warning and fall back to using no table.
582+
with pytest.warns(UserWarning, match="does not annotate element"):
583+
(
584+
sdata_blobs_local.pl.render_shapes(
585+
"blobs_circles",
586+
color="channel_0_sum",
587+
table_name="other_table",
588+
).pl.show()
589+
)

0 commit comments

Comments
 (0)