Skip to content

Commit e97a7fc

Browse files
Categorical data is now correctly colored in the legend when plotting (#323)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c6575e2 commit e97a7fc

20 files changed

+538
-260
lines changed

CHANGELOG.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,28 @@ and this project adheres to [Semantic Versioning][].
1010

1111
## [0.2.x] - tbd
1212

13-
## [0.2.4] - 2024-08-07
14-
1513
### Added
1614

17-
- Added utils function for 0-transparent cmaps (#302)
15+
- Replaced `outline` parameter in `render_labels` with alpha-based logic (#323)
16+
- Lowered RMSE-threshold for plot-based tests from 60 to 45 (#323)
17+
- Minor fixes for several tests as a result of the threshold change (#323)
1818

1919
### Changed
2020

2121
-
2222

2323
### Fixed
2424

25+
-
26+
27+
## [0.2.4] - 2024-08-07
28+
29+
### Added
30+
31+
- Added utils function for 0-transparent cmaps (#302)
32+
33+
### Fixed
34+
2535
- Took RNG out of categorical label test (#306)
2636
- Performance bug when plotting shapes (#298)
2737
- scale parameter was ignored for single-scale images (#301)

src/spatialdata_plot/pl/basic.py

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
)
6060
from spatialdata_plot.pp.utils import _verify_plotting_tree
6161

62+
# replace with
63+
# from spatialdata._types import ColorLike
64+
# once https://github.com/scverse/spatialdata/pull/689/ is in a release
6265
ColorLike = Union[tuple[float, ...], str]
6366

6467

@@ -158,11 +161,10 @@ def render_shapes(
158161
fill_alpha: float | int = 1.0,
159162
groups: list[str] | str | None = None,
160163
palette: list[str] | str | None = None,
161-
na_color: ColorLike | None = "lightgrey",
162-
outline: bool = False,
164+
na_color: ColorLike | None = "default",
163165
outline_width: float | int = 1.5,
164166
outline_color: str | list[float] = "#000000ff",
165-
outline_alpha: float | int = 1.0,
167+
outline_alpha: float | int = 0.0,
166168
cmap: Colormap | str | None = None,
167169
norm: bool | Normalize = False,
168170
scale: float | int = 1.0,
@@ -201,19 +203,17 @@ def render_shapes(
201203
Palette for discrete annotations. List of valid color names that should be used for the categories. Must
202204
match the number of groups. If element is None, broadcasting behaviour is attempted (use the same values for
203205
all elements). If groups is provided but not palette, palette is set to default "lightgray".
204-
na_color : ColorLike | None, default "lightgrey"
206+
na_color : ColorLike | None, default "default" (gets set to "lightgray")
205207
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
206208
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
207209
won't be shown.
208-
outline : bool, default False
209-
If `True`, a border around the shape elements is plotted.
210210
outline_width : float | int, default 1.5
211211
Width of the border.
212212
outline_color : str | list[float], default "#000000ff"
213213
Color of the border. Can either be a named color ("red"), a hex representation ("#000000ff") or a list of
214214
floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0).
215-
outline_alpha : float | int, default 1.0
216-
Alpha value for the outline of shapes.
215+
outline_alpha : float | int, default 0.0
216+
Alpha value for the outline of shapes. Invisible by default.
217217
cmap : Colormap | str | None, optional
218218
Colormap for discrete or continuous annotations using 'color', see :class:`matplotlib.colors.Colormap`.
219219
norm : bool | Normalize, default False
@@ -249,7 +249,6 @@ def render_shapes(
249249
palette=palette,
250250
color=color,
251251
na_color=na_color,
252-
outline=outline,
253252
outline_alpha=outline_alpha,
254253
outline_color=outline_color,
255254
outline_width=outline_width,
@@ -263,16 +262,15 @@ def render_shapes(
263262
sdata = self._copy()
264263
sdata = _verify_plotting_tree(sdata)
265264
n_steps = len(sdata.plotting_tree.keys())
266-
cmap_params = _prepare_cmap_norm(
267-
cmap=cmap,
268-
norm=norm,
269-
na_color=na_color, # type: ignore[arg-type]
270-
**kwargs,
271-
)
272-
273-
outline_params = _set_outline(outline, outline_width, outline_color)
265+
outline_params = _set_outline(outline_alpha > 0, outline_width, outline_color)
274266

275267
for element, param_values in params_dict.items():
268+
cmap_params = _prepare_cmap_norm(
269+
cmap=cmap,
270+
norm=norm,
271+
na_color=params_dict[element]["na_color"], # type: ignore[arg-type]
272+
**kwargs,
273+
)
276274
sdata.plotting_tree[f"{n_steps+1}_render_shapes"] = ShapesRenderParams(
277275
element=element,
278276
color=param_values["color"],
@@ -301,7 +299,7 @@ def render_points(
301299
alpha: float | int = 1.0,
302300
groups: list[str] | str | None = None,
303301
palette: list[str] | str | None = None,
304-
na_color: ColorLike | None = "lightgrey",
302+
na_color: ColorLike | None = "default",
305303
cmap: Colormap | str | None = None,
306304
norm: None | Normalize = None,
307305
size: float | int = 1.0,
@@ -339,7 +337,7 @@ def render_points(
339337
Palette for discrete annotations. List of valid color names that should be used for the categories. Must
340338
match the number of groups. If `element` is `None`, broadcasting behaviour is attempted (use the same values
341339
for all elements). If groups is provided but not palette, palette is set to default "lightgray".
342-
na_color : ColorLike | None, default "lightgrey"
340+
na_color : ColorLike | None, default "default" (gets set to "lightgray")
343341
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
344342
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
345343
won't be shown.
@@ -389,14 +387,13 @@ def render_points(
389387
sdata = _verify_plotting_tree(sdata)
390388
n_steps = len(sdata.plotting_tree.keys())
391389

392-
cmap_params = _prepare_cmap_norm(
393-
cmap=cmap,
394-
norm=norm,
395-
na_color=na_color, # type: ignore[arg-type]
396-
**kwargs,
397-
)
398-
399390
for element, param_values in params_dict.items():
391+
cmap_params = _prepare_cmap_norm(
392+
cmap=cmap,
393+
norm=norm,
394+
na_color=param_values["na_color"], # type: ignore[arg-type]
395+
**kwargs,
396+
)
400397
sdata.plotting_tree[f"{n_steps+1}_render_points"] = PointsRenderParams(
401398
element=element,
402399
color=param_values["color"],
@@ -422,7 +419,7 @@ def render_images(
422419
channel: list[str] | list[int] | str | int | None = None,
423420
cmap: list[Colormap | str] | Colormap | str | None = None,
424421
norm: Normalize | None = None,
425-
na_color: ColorLike | None = (0.0, 0.0, 0.0, 0.0),
422+
na_color: ColorLike | None = "default",
426423
palette: list[str] | str | None = None,
427424
alpha: float | int = 1.0,
428425
percentiles_for_norm: tuple[float, float] | None = None,
@@ -452,8 +449,10 @@ def render_images(
452449
norm : Normalize | None, optional
453450
Colormap normalization for continuous annotations, see :class:`matplotlib.colors.Normalize`.
454451
Applies to all channels if set.
455-
na_color : ColorLike | None, default (0.0, 0.0, 0.0, 0.0)
456-
Color to be used for NA values. Accepts color-like values (string, hex, RGB(A)).
452+
na_color : ColorLike | None, default "default" (gets set to "lightgray")
453+
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
454+
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
455+
won't be shown.
457456
palette : list[str] | str | None
458457
Palette to color images. The number of palettes should be equal to the number of channels.
459458
alpha : float | int, default 1.0
@@ -494,27 +493,32 @@ def render_images(
494493
sdata = _verify_plotting_tree(sdata)
495494
n_steps = len(sdata.plotting_tree.keys())
496495

497-
cmap_params: list[CmapParams] | CmapParams
498-
if isinstance(cmap, list):
499-
cmap_params = [
500-
_prepare_cmap_norm(
501-
cmap=c,
496+
for element, param_values in params_dict.items():
497+
# cmap_params = _prepare_cmap_norm(
498+
# cmap=params_dict[element]["cmap"],
499+
# norm=norm,
500+
# na_color=params_dict[element]["na_color"], # type: ignore[arg-type]
501+
# **kwargs,
502+
# )
503+
cmap_params: list[CmapParams] | CmapParams
504+
if isinstance(cmap, list):
505+
cmap_params = [
506+
_prepare_cmap_norm(
507+
cmap=c,
508+
norm=norm,
509+
na_color=param_values["na_color"],
510+
**kwargs,
511+
)
512+
for c in cmap
513+
]
514+
515+
else:
516+
cmap_params = _prepare_cmap_norm(
517+
cmap=cmap,
502518
norm=norm,
503-
na_color=na_color, # type: ignore[arg-type]
519+
na_color=param_values["na_color"],
504520
**kwargs,
505521
)
506-
for c in cmap
507-
]
508-
509-
else:
510-
cmap_params = _prepare_cmap_norm(
511-
cmap=cmap,
512-
norm=norm,
513-
na_color=na_color, # type: ignore[arg-type]
514-
**kwargs,
515-
)
516-
517-
for element, param_values in params_dict.items():
518522
sdata.plotting_tree[f"{n_steps+1}_render_images"] = ImageRenderParams(
519523
element=element,
520524
channel=param_values["channel"],
@@ -536,12 +540,11 @@ def render_labels(
536540
color: str | None = None,
537541
groups: list[str] | str | None = None,
538542
contour_px: int | None = 3,
539-
outline: bool = False,
540543
palette: list[str] | str | None = None,
541544
cmap: Colormap | str | None = None,
542545
norm: Normalize | None = None,
543-
na_color: ColorLike | None = (0.0, 0.0, 0.0, 0.0),
544-
outline_alpha: float | int = 1.0,
546+
na_color: ColorLike | None = "default",
547+
outline_alpha: float | int = 0.0,
545548
fill_alpha: float | int = 0.4,
546549
scale: str | None = None,
547550
table_name: str | None = None,
@@ -576,16 +579,16 @@ def render_labels(
576579
contour_px : int, default 3
577580
Draw contour of specified width for each segment. If `None`, fills entire segment, see:
578581
func:`skimage.morphology.erosion`.
579-
outline : bool, default False
580-
Whether to plot boundaries around segmentation masks.
581582
cmap : Colormap | str | None
582583
Colormap for continuous annotations, see :class:`matplotlib.colors.Colormap`.
583584
norm : Normalize | None
584585
Colormap normalization for continuous annotations, see :class:`matplotlib.colors.Normalize`.
585-
na_color : ColorLike | None
586-
Color to be used for NAs values, if present.
587-
outline_alpha : float | int, default 1.0
588-
Alpha value for the outline of the labels.
586+
na_color : ColorLike | None, default "default" (gets set to "lightgray")
587+
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
588+
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
589+
won't be shown.
590+
outline_alpha : float | int, default 0.0
591+
Alpha value for the outline of the labels. Invisible by default.
589592
fill_alpha : float | int, default 0.3
590593
Alpha value for the fill of the labels.
591594
scale : str | None
@@ -615,7 +618,6 @@ def render_labels(
615618
groups=groups,
616619
na_color=na_color,
617620
norm=norm,
618-
outline=outline,
619621
outline_alpha=outline_alpha,
620622
palette=palette,
621623
scale=scale,
@@ -625,20 +627,19 @@ def render_labels(
625627
sdata = self._copy()
626628
sdata = _verify_plotting_tree(sdata)
627629
n_steps = len(sdata.plotting_tree.keys())
628-
cmap_params = _prepare_cmap_norm(
629-
cmap=cmap,
630-
norm=norm,
631-
na_color=na_color, # type: ignore[arg-type]
632-
**kwargs,
633-
)
634630

635631
for element, param_values in params_dict.items():
632+
cmap_params = _prepare_cmap_norm(
633+
cmap=cmap,
634+
norm=norm,
635+
na_color=param_values["na_color"], # type: ignore[arg-type]
636+
**kwargs,
637+
)
636638
sdata.plotting_tree[f"{n_steps+1}_render_labels"] = LabelsRenderParams(
637639
element=element,
638640
color=param_values["color"],
639641
groups=param_values["groups"],
640642
contour_px=param_values["contour_px"],
641-
outline=param_values["outline"],
642643
cmap_params=cmap_params,
643644
palette=param_values["palette"],
644645
outline_alpha=param_values["outline_alpha"],

0 commit comments

Comments
 (0)