Skip to content

Commit bb58da2

Browse files
authored
Merge branch 'main' into bugfix/issue403-render_labels-color-plot-no-longer-working2
2 parents 9086821 + e256b84 commit bb58da2

File tree

66 files changed

+1324
-424
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1324
-424
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repos:
1313
hooks:
1414
- id: prettier
1515
- repo: https://github.com/astral-sh/ruff-pre-commit
16-
rev: v0.13.1
16+
rev: v0.14.5
1717
hooks:
1818
- id: ruff
1919
args: [--fix, --exit-non-zero-on-fix]

src/spatialdata_plot/pl/basic.py

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import OrderedDict
66
from copy import deepcopy
77
from pathlib import Path
8-
from typing import Any
8+
from typing import Any, Literal
99

1010
import matplotlib.pyplot as plt
1111
import numpy as np
@@ -61,7 +61,7 @@
6161
# replace with
6262
# from spatialdata._types import ColorLike
6363
# once https://github.com/scverse/spatialdata/pull/689/ is in a release
64-
ColorLike = tuple[float, ...] | str
64+
ColorLike = tuple[float, ...] | list[float] | str
6565

6666

6767
@register_spatial_data_accessor("pl")
@@ -156,20 +156,22 @@ def _copy(
156156
def render_shapes(
157157
self,
158158
element: str | None = None,
159-
color: str | None = None,
160-
fill_alpha: float | int = 1.0,
159+
color: ColorLike | None = None,
160+
*,
161+
fill_alpha: float | int | None = None,
161162
groups: list[str] | str | None = None,
162163
palette: list[str] | str | None = None,
163164
na_color: ColorLike | None = "default",
164-
outline_width: float | int = 1.5,
165-
outline_color: str | list[float] = "#000000",
166-
outline_alpha: float | int = 0.0,
165+
outline_width: float | int | tuple[float | int, float | int] | None = None,
166+
outline_color: ColorLike | tuple[ColorLike] | None = None,
167+
outline_alpha: float | int | tuple[float | int, float | int] | None = None,
167168
cmap: Colormap | str | None = None,
168169
norm: Normalize | None = None,
169170
scale: float | int = 1.0,
170171
method: str | None = None,
171172
table_name: str | None = None,
172173
table_layer: str | None = None,
174+
shape: Literal["circle", "hex", "visium_hex", "square"] | None = None,
173175
**kwargs: Any,
174176
) -> sd.SpatialData:
175177
"""
@@ -186,15 +188,18 @@ def render_shapes(
186188
element : str | None, optional
187189
The name of the shapes element to render. If `None`, all shapes elements in the `SpatialData` object will be
188190
used.
189-
color : str | None
190-
Can either be string representing a color-like or key in :attr:`sdata.table.obs`. The latter can be used to
191-
color by categorical or continuous variables. If `element` is `None`, if possible the color will be
192-
broadcasted to all elements. For this, the table in which the color key is found must annotate the
193-
respective element (region must be set to the specific element). If the color column is found in multiple
194-
locations, please provide the table_name to be used for the elements.
195-
fill_alpha : float | int, default 1.0
196-
Alpha value for the fill of shapes. If the alpha channel is present in a cmap passed by the user, this value
197-
will multiply the value present in the cmap.
191+
color : ColorLike | None, optional
192+
Can either be color-like (name of a color as string, e.g. "red", hex representation, e.g. "#000000" or
193+
"#000000ff", or an RGB(A) array as a tuple or list containing 3-4 floats within [0, 1]. If an alpha value is
194+
indicated, the value of `fill_alpha` takes precedence if given) or a string representing a key in
195+
:attr:`sdata.table.obs`. The latter can be used to color by categorical or continuous variables. If
196+
`element` is `None`, if possible the color will be broadcasted to all elements. For this, the table in which
197+
the color key is found must annotate the respective element (region must be set to the specific element). If
198+
the color column is found in multiple locations, please provide the table_name to be used for the elements.
199+
fill_alpha : float | int | None, optional
200+
Alpha value for the fill of shapes. By default, it is set to 1.0 or, if a color is given that implies an
201+
alpha, that value is used for `fill_alpha`. If an alpha channel is present in a cmap passed by the user,
202+
`fill_alpha` will overwrite the value present in the cmap.
198203
groups : list[str] | str | None
199204
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
200205
them. Other values are set to NA. If elment is None, broadcasting behaviour is attempted (use the same
@@ -204,18 +209,25 @@ def render_shapes(
204209
match the number of groups. If element is None, broadcasting behaviour is attempted (use the same values for
205210
all elements). If groups is provided but not palette, palette is set to default "lightgray".
206211
na_color : ColorLike | None, default "default" (gets set to "lightgray")
207-
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
212+
Color to be used for NA values, if present. Can either be a named color ("red"), a hex representation
208213
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
209214
won't be shown.
210-
outline_width : float | int, default 1.5
211-
Width of the border.
212-
outline_color : str | list[float], default "#000000"
215+
outline_width : float | int | tuple[float | int, float | int], optional
216+
Width of the border. If 2 values are given (tuple), 2 borders are shown with these widths (outer & inner).
217+
If `outline_color` and/or `outline_alpha` are used to indicate that one/two outlines should be drawn, the
218+
default outline widths 1.5 and 0.5 are used for outer/only and inner outline respectively.
219+
outline_color : ColorLike | tuple[ColorLike], optional
213220
Color of the border. Can either be a named color ("red"), a hex representation ("#000000") or a list of
214221
floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). If the hex representation includes alpha, e.g.
215-
"#000000ff", the last two positions are ignored, since the alpha of the outlines is solely controlled by
216-
`outline_alpha`.
217-
outline_alpha : float | int, default 0.0
218-
Alpha value for the outline of shapes. Invisible by default.
222+
"#000000ff", and `outline_alpha` is not given, this value controls the opacity of the outline. If 2 values
223+
are given (tuple), 2 borders are shown with these colors (outer & inner). If `outline_width` and/or
224+
`outline_alpha` are used to indicate that one/two outlines should be drawn, the default outline colors
225+
"#000000" and "#ffffff are used for outer/only and inner outline respectively.
226+
outline_alpha : float | int | tuple[float | int, float | int] | None, optional
227+
Alpha value for the outline of shapes. Invisible by default, meaning outline_alpha=0.0 if both outline_color
228+
and outline_width are not specified. Else, outlines are rendered with the alpha implied by outline_color, or
229+
with outline_alpha=1.0 if outline_color does not imply an alpha. For two outlines, alpha values can be
230+
passed in a tuple of length 2.
219231
cmap : Colormap | str | None, optional
220232
Colormap for discrete or continuous annotations using 'color', see :class:`matplotlib.colors.Colormap`.
221233
norm : bool | Normalize, default False
@@ -232,6 +244,11 @@ def render_shapes(
232244
table_layer: str | None
233245
Layer of the table to use for coloring if `color` is in :attr:`sdata.table.var_names`. If None, the data in
234246
:attr:`sdata.table.X` is used for coloring.
247+
shape: Literal["circle", "hex", "visium_hex", "square"] | None
248+
If None (default), the shapes are rendered as they are. Else, if either of "circle", "hex" or "square" is
249+
specified, the shapes are converted to a circle/hexagon/square before rendering. If "visium_hex" is
250+
specified, the shapes are assumed to be Visium spots and the size of the hexagons is adjusted to be adjacent
251+
to each other.
235252
236253
**kwargs : Any
237254
Additional arguments for customization. This can include:
@@ -276,15 +293,20 @@ def render_shapes(
276293
scale=scale,
277294
table_name=table_name,
278295
table_layer=table_layer,
296+
shape=shape,
279297
method=method,
280298
ds_reduction=kwargs.get("datashader_reduction"),
281299
)
282300

283301
sdata = self._copy()
284302
sdata = _verify_plotting_tree(sdata)
285303
n_steps = len(sdata.plotting_tree.keys())
286-
outline_params = _set_outline(outline_alpha > 0, outline_width, outline_color)
287304
for element, param_values in params_dict.items():
305+
final_outline_alpha, outline_params = _set_outline(
306+
params_dict[element]["outline_alpha"],
307+
params_dict[element]["outline_width"],
308+
params_dict[element]["outline_color"],
309+
)
288310
cmap_params = _prepare_cmap_norm(
289311
cmap=cmap,
290312
norm=norm,
@@ -299,11 +321,12 @@ def render_shapes(
299321
outline_params=outline_params,
300322
cmap_params=cmap_params,
301323
palette=param_values["palette"],
302-
outline_alpha=param_values["outline_alpha"],
324+
outline_alpha=final_outline_alpha,
303325
fill_alpha=param_values["fill_alpha"],
304326
transfunc=kwargs.get("transfunc"),
305327
table_name=param_values["table_name"],
306328
table_layer=param_values["table_layer"],
329+
shape=param_values["shape"],
307330
zorder=n_steps,
308331
method=param_values["method"],
309332
ds_reduction=param_values["ds_reduction"],
@@ -316,8 +339,9 @@ def render_shapes(
316339
def render_points(
317340
self,
318341
element: str | None = None,
319-
color: str | None = None,
320-
alpha: float | int = 1.0,
342+
color: ColorLike | None = None,
343+
*,
344+
alpha: float | int | None = None,
321345
groups: list[str] | str | None = None,
322346
palette: list[str] | str | None = None,
323347
na_color: ColorLike | None = "default",
@@ -343,14 +367,17 @@ def render_points(
343367
element : str | None, optional
344368
The name of the points element to render. If `None`, all points elements in the `SpatialData` object will be
345369
used.
346-
color : str | None
347-
Can either be string representing a color-like or key in :attr:`sdata.table.obs`. The latter can be used to
348-
color by categorical or continuous variables. If `element` is `None`, if possible the color will be
349-
broadcasted to all elements. For this, the table in which the color key is found must annotate the
350-
respective element (region must be set to the specific element). If the color column is found in multiple
351-
locations, please provide the table_name to be used for the elements.
352-
alpha : float | int, default 1.0
353-
Alpha value for the points.
370+
color : str | None, optional
371+
Can either be color-like (name of a color as string, e.g. "red", hex representation, e.g. "#000000" or
372+
"#000000ff", or an RGB(A) array as a tuple or list containing 3-4 floats within [0, 1]. If an alpha value is
373+
indicated, the value of `fill_alpha` takes precedence if given) or a string representing a key in
374+
:attr:`sdata.table.obs`. The latter can be used to color by categorical or continuous variables. If
375+
`element` is `None`, if possible the color will be broadcasted to all elements. For this, the table in which
376+
the color key is found must annotate the respective element (region must be set to the specific element). If
377+
the color column is found in multiple locations, please provide the table_name to be used for the elements.
378+
alpha : float | int | None, optional
379+
Alpha value for the points. By default, it is set to 1.0 or, if a color is given that implies an alpha, that
380+
value is used instead.
354381
groups : list[str] | str | None
355382
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
356383
them. Other values are set to NA. If `element` is `None`, broadcasting behaviour is attempted (use the same
@@ -360,7 +387,7 @@ def render_points(
360387
match the number of groups. If `element` is `None`, broadcasting behaviour is attempted (use the same values
361388
for all elements). If groups is provided but not palette, palette is set to default "lightgray".
362389
na_color : ColorLike | None, default "default" (gets set to "lightgray")
363-
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
390+
Color to be used for NA values, if present. Can either be a named color ("red"), a hex representation
364391
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
365392
won't be shown.
366393
cmap : Colormap | str | None, optional
@@ -457,6 +484,7 @@ def render_points(
457484
def render_images(
458485
self,
459486
element: str | None = None,
487+
*,
460488
channel: list[str] | list[int] | str | int | None = None,
461489
cmap: list[Colormap | str] | Colormap | str | None = None,
462490
norm: Normalize | None = None,
@@ -574,6 +602,7 @@ def render_labels(
574602
self,
575603
element: str | None = None,
576604
color: str | None = None,
605+
*,
577606
groups: list[str] | str | None = None,
578607
contour_px: int | None = 3,
579608
palette: list[str] | str | None = None,
@@ -601,7 +630,7 @@ def render_labels(
601630
element : str | None
602631
The name of the labels element to render. If `None`, all label
603632
elements in the `SpatialData` object will be used and all parameters will be broadcasted if possible.
604-
color : list[str] | str | None
633+
color : str | None
605634
Can either be string representing a color-like or key in :attr:`sdata.table.obs` or in the index of
606635
:attr:`sdata.table.var`. The latter can be used to color by categorical or continuous variables. If the
607636
color column is found in multiple locations, please provide the table_name to be used for the element if you
@@ -626,7 +655,7 @@ def render_labels(
626655
won't be shown.
627656
outline_alpha : float | int, default 0.0
628657
Alpha value for the outline of the labels. Invisible by default.
629-
fill_alpha : float | int, default 0.3
658+
fill_alpha : float | int, default 0.4
630659
Alpha value for the fill of the labels.
631660
scale : str | None
632661
Influences the resolution of the rendering. Possibilities for setting this parameter:
@@ -703,6 +732,7 @@ def render_labels(
703732
def show(
704733
self,
705734
coordinate_systems: list[str] | str | None = None,
735+
*,
706736
legend_fontsize: int | float | _FontSize | None = None,
707737
legend_fontweight: int | _FontWeight = "bold",
708738
legend_loc: str | None = "right margin",

0 commit comments

Comments
 (0)