Skip to content

Commit f60e861

Browse files
committed
new unit tests for nan behaviour
1 parent 3cbf468 commit f60e861

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

tests/pl/test_render_shapes.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,3 +684,52 @@ def test_warns_when_table_does_not_annotate_element(sdata_blobs: SpatialData):
684684
table_name="other_table",
685685
).pl.show()
686686
)
687+
688+
def test_plot_can_handle_nan_values_in_color_data(self, sdata_blobs: SpatialData):
689+
"""Test that NaN values in color data are handled gracefully."""
690+
sdata_blobs["table"].obs["region"] = pd.Categorical(["blobs_circles"] * sdata_blobs["table"].n_obs)
691+
sdata_blobs["table"].uns["spatialdata_attrs"]["region"] = "blobs_circles"
692+
693+
# Add color column with NaN values
694+
sdata_blobs.shapes["blobs_circles"]["color_with_nan"] = [1.0, 2.0, np.nan, 4.0, 5.0]
695+
696+
# Test that rendering works with NaN values and issues warning
697+
with pytest.warns(UserWarning, match="Found 1 NaN values in color data"):
698+
sdata_blobs.pl.render_shapes(
699+
element="blobs_circles",
700+
color="color_with_nan",
701+
na_color="red"
702+
).pl.show()
703+
704+
def test_plot_colorbar_normalization_with_nan_values(self, sdata_blobs: SpatialData):
705+
"""Test that colorbar normalization works correctly with NaN values."""
706+
sdata_blobs["table"].obs["region"] = pd.Categorical(["blobs_polygons"] * sdata_blobs["table"].n_obs)
707+
sdata_blobs["table"].uns["spatialdata_attrs"]["region"] = "blobs_polygons"
708+
709+
sdata_blobs.shapes["blobs_polygons"]["color_with_nan"] = [1.0, 2.0, np.nan, 4.0, 5.0]
710+
711+
# Test colorbar with NaN values - should use nanmin/nanmax
712+
sdata_blobs.pl.render_shapes(
713+
element="blobs_polygons",
714+
color="color_with_nan",
715+
na_color="gray"
716+
).pl.show()
717+
718+
def test_plot_can_handle_non_numeric_radius_values(self, sdata_blobs: SpatialData):
719+
"""Test that non-numeric radius values are handled gracefully."""
720+
sdata_blobs.shapes["blobs_circles"]["radius_mixed"] = [1.0, "invalid", 3.0, np.nan, 5.0]
721+
722+
sdata_blobs.pl.render_shapes(element="blobs_circles", color="red").pl.show()
723+
724+
def test_plot_can_handle_mixed_numeric_and_color_data(self, sdata_blobs: SpatialData):
725+
"""Test handling of mixed numeric and color-like data."""
726+
sdata_blobs["table"].obs["region"] = pd.Categorical(["blobs_circles"] * sdata_blobs["table"].n_obs)
727+
sdata_blobs["table"].uns["spatialdata_attrs"]["region"] = "blobs_circles"
728+
729+
sdata_blobs.shapes["blobs_circles"]["mixed_data"] = [1.0, 2.0, np.nan, "red", 5.0]
730+
731+
sdata_blobs.pl.render_shapes(
732+
element="blobs_circles",
733+
color="mixed_data",
734+
na_color="gray"
735+
).pl.show()

tests/pl/test_utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,38 @@ def test_is_color_like(color_result: tuple[ColorLike, bool]):
8989
assert spatialdata_plot.pl.utils._is_color_like(color) == result
9090

9191

92+
def test_extract_scalar_value():
93+
"""Test the new _extract_scalar_value function for robust numeric conversion."""
94+
from spatialdata_plot.pl.utils import _extract_scalar_value
95+
96+
# Test basic functionality
97+
assert _extract_scalar_value(3.14) == 3.14
98+
assert _extract_scalar_value(42) == 42.0
99+
100+
# Test with collections
101+
assert _extract_scalar_value(pd.Series([1.0, 2.0, 3.0])) == 1.0
102+
assert _extract_scalar_value([1.0, 2.0, 3.0]) == 1.0
103+
104+
# Test edge cases
105+
assert _extract_scalar_value(np.nan) == 0.0
106+
assert _extract_scalar_value("invalid") == 0.0
107+
assert _extract_scalar_value([], default=1.0) == 1.0
108+
109+
110+
def test_plot_can_handle_per_row_rgba_colors(sdata_blobs: SpatialData):
111+
"""Test handling of per-row RGBA color arrays."""
112+
rgba_colors = np.array([
113+
[1.0, 0.0, 0.0, 1.0], # red
114+
[0.0, 1.0, 0.0, 1.0], # green
115+
[0.0, 0.0, 1.0, 1.0], # blue
116+
[1.0, 1.0, 0.0, 1.0], # yellow
117+
[1.0, 0.0, 1.0, 1.0], # magenta
118+
])
119+
sdata_blobs.shapes["blobs_circles"]["rgba_colors"] = rgba_colors.tolist()
120+
121+
sdata_blobs.pl.render_shapes(element="blobs_circles", color="rgba_colors").pl.show()
122+
123+
92124
@pytest.mark.parametrize(
93125
"input_output",
94126
[

0 commit comments

Comments
 (0)