Skip to content

Commit 16dc893

Browse files
Sonja-StockhausSonja Stockhaus
and
Sonja Stockhaus
authored
fix outline width parameter, add unittests (#139)
Co-authored-by: Sonja Stockhaus <[email protected]>
1 parent 05ef045 commit 16dc893

File tree

7 files changed

+26
-13
lines changed

7 files changed

+26
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning][].
1414

1515
- Multi-scale images/labels are now correctly substituted and the action is logged (#131).
1616
- Empty geometries among the shapes can be handeled (#133).
17+
- `outline_width` parameter in render_shapes is now a float that actually determines the line width (#139).
1718

1819
## [0.0.2] - 2023-06-25
1920

src/spatialdata_plot/pl/basic.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def render_shapes(
145145
groups: str | Sequence[str] | None = None,
146146
size: float = 1.0,
147147
outline: bool = False,
148-
outline_width: tuple[float, float] = (0.3, 0.05),
148+
outline_width: float = 1.5,
149149
outline_color: str | list[float] = "#000000ff",
150150
alt_var: str | None = None,
151151
layer: str | None = None,
@@ -198,6 +198,7 @@ def render_shapes(
198198
Notes
199199
-----
200200
Empty geometries will be removed at the time of plotting.
201+
An ``outline_width`` of 0.0 leads to no border being plotted.
201202
202203
Returns
203204
-------
@@ -538,7 +539,7 @@ def show(
538539
render_cmds[cmd] = params
539540

540541
if len(render_cmds.keys()) == 0:
541-
raise TypeError("Please specify what to plot using the 'render_*' functions before calling 'imshow().")
542+
raise TypeError("Please specify what to plot using the 'render_*' functions before calling 'imshow()'.")
542543

543544
if title is not None:
544545
if isinstance(title, str):

src/spatialdata_plot/pl/render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def _get_collection_shape(
156156
patches,
157157
snap=False,
158158
# zorder=4,
159-
lw=1.5,
159+
lw=render_params.outline_params.linewidth,
160160
facecolor=fill_c,
161161
edgecolor=outline_c,
162162
**kwargs,

src/spatialdata_plot/pl/utils.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -485,31 +485,36 @@ class OutlineParams:
485485

486486
outline: bool
487487
outline_color: str | list[float]
488-
gap_size: float
489-
bg_size: float
488+
linewidth: float
490489

491490

492491
def _set_outline(
493492
size: float,
494493
outline: bool = False,
495-
outline_width: tuple[float, float] = (0.3, 0.05),
494+
outline_width: float = 1.5,
496495
outline_color: str | list[float] = "#0000000ff", # black, white
497496
**kwargs: Any,
498497
) -> OutlineParams:
499-
bg_width, gap_width = outline_width
500-
point = np.sqrt(size)
501-
gap_size = (point + (point * gap_width) * 2) ** 2
502-
bg_size = (np.sqrt(gap_size) + (point * bg_width) * 2) ** 2
503-
# the default black and white colors can be changes using the contour_config parameter
504-
498+
# Type checks for outline_width
499+
if isinstance(outline_width, int):
500+
outline_width = float(outline_width)
501+
if not isinstance(outline_width, float):
502+
raise TypeError(f"Invalid type of `outline_width`: {type(outline_width)}, expected `float`.")
503+
if outline_width == 0.0:
504+
outline = False
505+
if outline_width < 0.0:
506+
logging.warning(f"Negative line widths are not allowed, changing {outline_width} to {(-1)*outline_width}")
507+
outline_width = (-1) * outline_width
508+
509+
# the default black and white colors can be changed using the contour_config parameter
505510
if (len(outline_color) == 3 or len(outline_color) == 4) and all(isinstance(c, float) for c in outline_color):
506511
outline_color = matplotlib.colors.to_hex(outline_color)
507512

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

512-
return OutlineParams(outline, outline_color, gap_size, bg_size)
517+
return OutlineParams(outline, outline_color, outline_width)
513518

514519

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

tests/pl/test_render_shapes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ def test_plot_can_render_polygons_with_rgba_colored_outline(self, sdata_blobs: S
5151
def test_plot_can_render_empty_geometry(self, sdata_blobs: SpatialData):
5252
sdata_blobs.shapes["blobs_circles"].at[0, "geometry"] = gpd.points_from_xy([None], [None])[0]
5353
sdata_blobs.pl.render_shapes().pl.show()
54+
55+
def test_plot_can_render_circles_with_default_outline_width(self, sdata_blobs: SpatialData):
56+
sdata_blobs.pl.render_shapes(elements="blobs_circles", outline=True).pl.show()
57+
58+
def test_plot_can_render_circles_with_specified_outline_width(self, sdata_blobs: SpatialData):
59+
sdata_blobs.pl.render_shapes(elements="blobs_circles", outline=True, outline_width=3.0).pl.show()

0 commit comments

Comments
 (0)