Skip to content

Bugfix for outline parameter in render_shapes #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/spatialdata_plot/pl/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def render_shapes(
size: float = 1.0,
outline: bool = False,
outline_width: tuple[float, float] = (0.3, 0.05),
outline_color: tuple[str, str] = ("#000000ff", "#ffffffff"), # black, white
outline_color: str | list[float] = "#000000ff",
alt_var: str | None = None,
layer: str | None = None,
palette: ListedColormap | str | None = None,
Expand Down
5 changes: 1 addition & 4 deletions src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ def _render_shapes(
else:
table = sdata.table[sdata.table.obs[_get_region_key(sdata)].isin(elements)]

# refactor plz, squidpy leftovers
render_params.outline_params.bg_color = (0.83, 0.83, 0.83, render_params.fill_alpha)

# get color vector (categorical or continuous)
color_source_vector, color_vector, _ = _set_color_source_vec(
adata=table,
Expand Down Expand Up @@ -144,7 +141,7 @@ def _get_collection_shape(
fill_c[..., -1] = render_params.fill_alpha

if render_params.outline_params.outline:
outline_c = ColorConverter().to_rgba_array(c)
outline_c = ColorConverter().to_rgba_array(render_params.outline_params.outline_color)
outline_c[..., -1] = render_params.outline_alpha
else:
outline_c = None
Expand Down
11 changes: 6 additions & 5 deletions src/spatialdata_plot/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,31 +482,32 @@ class OutlineParams:
"""Cmap params."""

outline: bool
outline_color: str | list[float]
gap_size: float
gap_color: str
bg_size: float
bg_color: str | tuple[float, ...]


def _set_outline(
size: float,
outline: bool = False,
outline_width: tuple[float, float] = (0.3, 0.05),
outline_color: tuple[str, str] = ("#0000000ff", "#ffffffff"), # black, white
outline_color: str | list[float] = "#0000000ff", # black, white
**kwargs: Any,
) -> OutlineParams:
bg_width, gap_width = outline_width
point = np.sqrt(size)
gap_size = (point + (point * gap_width) * 2) ** 2
bg_size = (np.sqrt(gap_size) + (point * bg_width) * 2) ** 2
# the default black and white colors can be changes using the contour_config parameter
bg_color, gap_color = outline_color

if (len(outline_color) == 3 or len(outline_color) == 4) and all(isinstance(c, float) for c in outline_color):
outline_color = matplotlib.colors.to_hex(outline_color)

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

return OutlineParams(outline, gap_size, gap_color, bg_size, bg_color)
return OutlineParams(outline, outline_color, gap_size, bg_size)


def _get_subplots(num_images: int, ncols: int = 4, width: int = 4, height: int = 3) -> plt.Figure | plt.Axes:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions tests/pl/test_render_shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,27 @@ class TestShapes(PlotTester, metaclass=PlotTesterMeta):
def test_plot_can_render_circles(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes(elements="blobs_circles").pl.show()

def test_plot_can_render_circles_with_outline(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes(elements="blobs_circles", outline=True).pl.show()

def test_plot_can_render_circles_with_colored_outline(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes(elements="blobs_circles", outline=True, outline_color="red").pl.show()

def test_plot_can_render_polygons(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes(elements="blobs_polygons").pl.show()

def test_plot_can_render_polygons_with_outline(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes(elements="blobs_polygons", outline=True).pl.show()

def test_plot_can_render_polygons_with_str_colored_outline(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes(elements="blobs_polygons", outline=True, outline_color="red").pl.show()

def test_plot_can_render_polygons_with_rgb_colored_outline(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes(
elements="blobs_polygons", outline=True, outline_color=(0.0, 0.0, 1.0, 1.0)
).pl.show()

def test_plot_can_render_polygons_with_rgba_colored_outline(self, sdata_blobs: SpatialData):
sdata_blobs.pl.render_shapes(
elements="blobs_polygons", outline=True, outline_color=(0.0, 1.0, 0.0, 1.0)
).pl.show()
14 changes: 14 additions & 0 deletions tests/pl/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from spatialdata.datasets import blobs


@pytest.mark.parametrize(
"outline_color",
[
(0.0, 1.0, 0.0, 1.0),
"#00ff00",
],
)
def test_set_outline_accepts_str_or_float_or_list_thereof(outline_color):
sdata = blobs()
sdata.pl.render_shapes(elements="blobs_polygons", outline=True, outline_color=outline_color).pl.show()