Skip to content

Commit 42b81a9

Browse files
authored
Bugfix for outline parameter in render_shapes (#119)
1 parent 5711a87 commit 42b81a9

11 files changed

+44
-10
lines changed

src/spatialdata_plot/pl/basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def render_shapes(
147147
size: float = 1.0,
148148
outline: bool = False,
149149
outline_width: tuple[float, float] = (0.3, 0.05),
150-
outline_color: tuple[str, str] = ("#000000ff", "#ffffffff"), # black, white
150+
outline_color: str | list[float] = "#000000ff",
151151
alt_var: str | None = None,
152152
layer: str | None = None,
153153
palette: ListedColormap | str | None = None,

src/spatialdata_plot/pl/render.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ def _render_shapes(
9090
else:
9191
table = sdata.table[sdata.table.obs[_get_region_key(sdata)].isin(elements)]
9292

93-
# refactor plz, squidpy leftovers
94-
render_params.outline_params.bg_color = (0.83, 0.83, 0.83, render_params.fill_alpha)
95-
9693
# get color vector (categorical or continuous)
9794
color_source_vector, color_vector, _ = _set_color_source_vec(
9895
adata=table,
@@ -144,7 +141,7 @@ def _get_collection_shape(
144141
fill_c[..., -1] = render_params.fill_alpha
145142

146143
if render_params.outline_params.outline:
147-
outline_c = ColorConverter().to_rgba_array(c)
144+
outline_c = ColorConverter().to_rgba_array(render_params.outline_params.outline_color)
148145
outline_c[..., -1] = render_params.outline_alpha
149146
else:
150147
outline_c = None

src/spatialdata_plot/pl/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,31 +482,32 @@ class OutlineParams:
482482
"""Cmap params."""
483483

484484
outline: bool
485+
outline_color: str | list[float]
485486
gap_size: float
486-
gap_color: str
487487
bg_size: float
488-
bg_color: str | tuple[float, ...]
489488

490489

491490
def _set_outline(
492491
size: float,
493492
outline: bool = False,
494493
outline_width: tuple[float, float] = (0.3, 0.05),
495-
outline_color: tuple[str, str] = ("#0000000ff", "#ffffffff"), # black, white
494+
outline_color: str | list[float] = "#0000000ff", # black, white
496495
**kwargs: Any,
497496
) -> OutlineParams:
498497
bg_width, gap_width = outline_width
499498
point = np.sqrt(size)
500499
gap_size = (point + (point * gap_width) * 2) ** 2
501500
bg_size = (np.sqrt(gap_size) + (point * bg_width) * 2) ** 2
502501
# the default black and white colors can be changes using the contour_config parameter
503-
bg_color, gap_color = outline_color
502+
503+
if (len(outline_color) == 3 or len(outline_color) == 4) and all(isinstance(c, float) for c in outline_color):
504+
outline_color = matplotlib.colors.to_hex(outline_color)
504505

505506
if outline:
506507
kwargs.pop("edgecolor", None) # remove edge from kwargs if present
507508
kwargs.pop("alpha", None) # remove alpha from kwargs if present
508509

509-
return OutlineParams(outline, gap_size, gap_color, bg_size, bg_color)
510+
return OutlineParams(outline, outline_color, gap_size, bg_size)
510511

511512

512513
def _get_subplots(num_images: int, ncols: int = 4, width: int = 4, height: int = 3) -> plt.Figure | plt.Axes:
Loading
Loading
Loading
Loading
Loading
Loading

tests/pl/test_render_shapes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,27 @@ class TestShapes(PlotTester, metaclass=PlotTesterMeta):
2222
def test_plot_can_render_circles(self, sdata_blobs: SpatialData):
2323
sdata_blobs.pl.render_shapes(elements="blobs_circles").pl.show()
2424

25+
def test_plot_can_render_circles_with_outline(self, sdata_blobs: SpatialData):
26+
sdata_blobs.pl.render_shapes(elements="blobs_circles", outline=True).pl.show()
27+
28+
def test_plot_can_render_circles_with_colored_outline(self, sdata_blobs: SpatialData):
29+
sdata_blobs.pl.render_shapes(elements="blobs_circles", outline=True, outline_color="red").pl.show()
30+
2531
def test_plot_can_render_polygons(self, sdata_blobs: SpatialData):
2632
sdata_blobs.pl.render_shapes(elements="blobs_polygons").pl.show()
33+
34+
def test_plot_can_render_polygons_with_outline(self, sdata_blobs: SpatialData):
35+
sdata_blobs.pl.render_shapes(elements="blobs_polygons", outline=True).pl.show()
36+
37+
def test_plot_can_render_polygons_with_str_colored_outline(self, sdata_blobs: SpatialData):
38+
sdata_blobs.pl.render_shapes(elements="blobs_polygons", outline=True, outline_color="red").pl.show()
39+
40+
def test_plot_can_render_polygons_with_rgb_colored_outline(self, sdata_blobs: SpatialData):
41+
sdata_blobs.pl.render_shapes(
42+
elements="blobs_polygons", outline=True, outline_color=(0.0, 0.0, 1.0, 1.0)
43+
).pl.show()
44+
45+
def test_plot_can_render_polygons_with_rgba_colored_outline(self, sdata_blobs: SpatialData):
46+
sdata_blobs.pl.render_shapes(
47+
elements="blobs_polygons", outline=True, outline_color=(0.0, 1.0, 0.0, 1.0)
48+
).pl.show()

tests/pl/test_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
from spatialdata.datasets import blobs
3+
4+
5+
@pytest.mark.parametrize(
6+
"outline_color",
7+
[
8+
(0.0, 1.0, 0.0, 1.0),
9+
"#00ff00",
10+
],
11+
)
12+
def test_set_outline_accepts_str_or_float_or_list_thereof(outline_color):
13+
sdata = blobs()
14+
sdata.pl.render_shapes(elements="blobs_polygons", outline=True, outline_color=outline_color).pl.show()

0 commit comments

Comments
 (0)