Skip to content

Commit 1df4990

Browse files
committed
tests
1 parent f60e861 commit 1df4990

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

tests/pl/test_render_shapes.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -689,47 +689,35 @@ def test_plot_can_handle_nan_values_in_color_data(self, sdata_blobs: SpatialData
689689
"""Test that NaN values in color data are handled gracefully."""
690690
sdata_blobs["table"].obs["region"] = pd.Categorical(["blobs_circles"] * sdata_blobs["table"].n_obs)
691691
sdata_blobs["table"].uns["spatialdata_attrs"]["region"] = "blobs_circles"
692-
692+
693693
# Add color column with NaN values
694694
sdata_blobs.shapes["blobs_circles"]["color_with_nan"] = [1.0, 2.0, np.nan, 4.0, 5.0]
695-
695+
696696
# Test that rendering works with NaN values and issues warning
697697
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()
698+
sdata_blobs.pl.render_shapes(element="blobs_circles", color="color_with_nan", na_color="red").pl.show()
703699

704700
def test_plot_colorbar_normalization_with_nan_values(self, sdata_blobs: SpatialData):
705701
"""Test that colorbar normalization works correctly with NaN values."""
706702
sdata_blobs["table"].obs["region"] = pd.Categorical(["blobs_polygons"] * sdata_blobs["table"].n_obs)
707703
sdata_blobs["table"].uns["spatialdata_attrs"]["region"] = "blobs_polygons"
708-
704+
709705
sdata_blobs.shapes["blobs_polygons"]["color_with_nan"] = [1.0, 2.0, np.nan, 4.0, 5.0]
710-
706+
711707
# 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()
708+
sdata_blobs.pl.render_shapes(element="blobs_polygons", color="color_with_nan", na_color="gray").pl.show()
717709

718710
def test_plot_can_handle_non_numeric_radius_values(self, sdata_blobs: SpatialData):
719711
"""Test that non-numeric radius values are handled gracefully."""
720712
sdata_blobs.shapes["blobs_circles"]["radius_mixed"] = [1.0, "invalid", 3.0, np.nan, 5.0]
721-
713+
722714
sdata_blobs.pl.render_shapes(element="blobs_circles", color="red").pl.show()
723715

724716
def test_plot_can_handle_mixed_numeric_and_color_data(self, sdata_blobs: SpatialData):
725717
"""Test handling of mixed numeric and color-like data."""
726718
sdata_blobs["table"].obs["region"] = pd.Categorical(["blobs_circles"] * sdata_blobs["table"].n_obs)
727719
sdata_blobs["table"].uns["spatialdata_attrs"]["region"] = "blobs_circles"
728-
720+
729721
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()
722+
723+
sdata_blobs.pl.render_shapes(element="blobs_circles", color="mixed_data", na_color="gray").pl.show()

tests/pl/test_utils.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import matplotlib
22
import matplotlib.pyplot as plt
3+
import numpy as np
34
import pandas as pd
45
import pytest
56
import scanpy as sc
@@ -91,16 +92,17 @@ def test_is_color_like(color_result: tuple[ColorLike, bool]):
9192

9293
def test_extract_scalar_value():
9394
"""Test the new _extract_scalar_value function for robust numeric conversion."""
95+
9496
from spatialdata_plot.pl.utils import _extract_scalar_value
95-
97+
9698
# Test basic functionality
9799
assert _extract_scalar_value(3.14) == 3.14
98100
assert _extract_scalar_value(42) == 42.0
99-
101+
100102
# Test with collections
101103
assert _extract_scalar_value(pd.Series([1.0, 2.0, 3.0])) == 1.0
102104
assert _extract_scalar_value([1.0, 2.0, 3.0]) == 1.0
103-
105+
104106
# Test edge cases
105107
assert _extract_scalar_value(np.nan) == 0.0
106108
assert _extract_scalar_value("invalid") == 0.0
@@ -109,15 +111,18 @@ def test_extract_scalar_value():
109111

110112
def test_plot_can_handle_per_row_rgba_colors(sdata_blobs: SpatialData):
111113
"""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-
])
114+
115+
rgba_colors = np.array(
116+
[
117+
[1.0, 0.0, 0.0, 1.0], # red
118+
[0.0, 1.0, 0.0, 1.0], # green
119+
[0.0, 0.0, 1.0, 1.0], # blue
120+
[1.0, 1.0, 0.0, 1.0], # yellow
121+
[1.0, 0.0, 1.0, 1.0], # magenta
122+
]
123+
)
119124
sdata_blobs.shapes["blobs_circles"]["rgba_colors"] = rgba_colors.tolist()
120-
125+
121126
sdata_blobs.pl.render_shapes(element="blobs_circles", color="rgba_colors").pl.show()
122127

123128

0 commit comments

Comments
 (0)