Skip to content

Commit 61589c7

Browse files
Adjust points params (#271)
* add temporary deprecation decorator * initial refactor * mostly fixed tests * fix remaining 2 tests * some more refactor * some more refactor * mypy * add images properly to elements to render * Initial refactor * additional refactor * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix tets * fix last tests * fix mypy * initial refactor * adjust docstring --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 07574ec commit 61589c7

4 files changed

Lines changed: 264 additions & 318 deletions

File tree

src/spatialdata_plot/pl/basic.py

Lines changed: 74 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
_set_outline,
5353
_update_params,
5454
_validate_image_render_params,
55+
_validate_points_render_params,
5556
_validate_render_params,
5657
_validate_shape_render_params,
5758
_validate_show_parameters,
@@ -175,40 +176,37 @@ def render_shapes(
175176
Parameters
176177
----------
177178
element : str | None, optional
178-
The name of the shapes element to render. If `None`, all shapes
179-
elements in the `SpatialData` object will be used. If you would like multiple specific elements to be
180-
rendered, please chain the calls to render: pl.render_shapes(...).pl.render_shapes(...).pl.show()
179+
The name of the shapes element to render. If `None`, all shapes elements in the `SpatialData` object will be
180+
used. If you would like multiple specific elements to be rendered, please chain the calls to render:
181+
pl.render_shapes(...).pl.render_shapes(...).pl.show().
181182
color : str | None
182-
Can either be string representing a color-like or key in :attr:`sdata.table.obs`. The latter
183-
can be used to color by categorical or continuous variables. If element is None, if possible the color will
184-
be broadcasted to all elements. For this, the table in which the color key is found must
185-
annotate the respective element (region must be set to the specific element). If the color column is found
186-
in multiple locations, please provide the table_name to be used for the elements.
183+
Can either be string representing a color-like or key in :attr:`sdata.table.obs`. The latter can be used to
184+
color by categorical or continuous variables. If `element` is `None`, if possible the color will be
185+
broadcasted to all elements. For this, the table in which the color key is found must annotate the
186+
respective element (region must be set to the specific element). If the color column is found in multiple
187+
locations, please provide the table_name to be used for the elements.
187188
fill_alpha : float | int, default 1.0
188-
Alpha value for the fill of shapes. If the alpha channel is present in a cmap passed by the
189-
user, this value will multiply the value present in the cmap.
189+
Alpha value for the fill of shapes. If the alpha channel is present in a cmap passed by the user, this value
190+
will multiply the value present in the cmap.
190191
groups : list[str] | str | None
191-
When using `color` and the key represents discrete labels, `groups`
192-
can be used to show only a subset of them. Other values are set to NA. If elment is None,
193-
broadcasting behaviour is attempted (use the same values for all elements).
192+
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
193+
them. Other values are set to NA. If elment is None, broadcasting behaviour is attempted (use the same
194+
values for all elements).
194195
palette : list[str] | str | None
195-
Palette for discrete annotations. List of valid color names that should be
196-
used for the categories. Must match the number of groups. If element is None, broadcasting behaviour is
197-
attempted (use the same values for all elements). If groups is provided but not palette, palette is set to
198-
default "lightgray".
196+
Palette for discrete annotations. List of valid color names that should be used for the categories. Must
197+
match the number of groups. If element is None, broadcasting behaviour is attempted (use the same values for
198+
all elements). If groups is provided but not palette, palette is set to default "lightgray".
199199
na_color : ColorLike | None, default "lightgrey"
200-
Color to be used for NAs values, if present. Can either be a named color
201-
("red"), a hex representation ("#000000ff") or a list of floats that
202-
represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values won't
203-
be shown.
200+
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
201+
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
202+
won't be shown.
204203
outline : bool, default False
205204
If `True`, a border around the shape elements is plotted.
206205
outline_width : float | int, default 1.5
207206
Width of the border.
208207
outline_color : str | list[float], default "#000000ff"
209-
Color of the border. Can either be a named color ("red"), a hex
210-
representation ("#000000ff") or a list of floats that represent RGB/RGBA
211-
values (1.0, 0.0, 0.0, 1.0).
208+
Color of the border. Can either be a named color ("red"), a hex representation ("#000000ff") or a list of
209+
floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0).
212210
outline_alpha : float | int, default 1.0
213211
Alpha value for the outline of shapes.
214212
cmap : Colormap | str | None, optional
@@ -217,7 +215,7 @@ def render_shapes(
217215
Colormap normalization for continuous annotations.
218216
scale : float | int, default 1.0
219217
Value to scale circles, if present.
220-
table_name:
218+
table_name: str | None
221219
Name of the table containing the color(s) columns. If one name is given than the table is used for each
222220
spatial element to be plotted if the table annotates it. If you want to use different tables for particular
223221
elements, as specified under element.
@@ -284,67 +282,61 @@ def render_shapes(
284282

285283
return sdata
286284

285+
@deprecation_alias(elements="element", version="0.3.0")
287286
def render_points(
288287
self,
289-
elements: list[str] | str | None = None,
290-
color: list[str | None] | str | None = None,
288+
element: str | None = None,
289+
color: str | None = None,
291290
alpha: float | int = 1.0,
292-
groups: list[list[str | None]] | list[str | None] | str | None = None,
293-
palette: list[list[str | None]] | list[str | None] | str | None = None,
291+
groups: list[str] | str | None = None,
292+
palette: list[str] | str | None = None,
294293
na_color: ColorLike | None = "lightgrey",
295294
cmap: Colormap | str | None = None,
296295
norm: None | Normalize = None,
297296
size: float | int = 1.0,
298-
table_name: list[str] | str | None = None,
297+
table_name: str | None = None,
299298
**kwargs: Any,
300299
) -> sd.SpatialData:
301300
"""
302301
Render points elements in SpatialData.
303302
304303
Parameters
305304
----------
306-
elements : list[str] | str | None, optional
307-
The name(s) of the points element(s) to render. If `None`, all points
308-
elements in the `SpatialData` object will be used.
309-
color : list[str | None] | str | None
310-
Can either be string(s) representing a color-like or key(s) in :attr:`sdata.table.obs`. The latter
311-
can be used to color by categorical or continuous variables. If provided as a list, the length of the list
312-
must match the number of elements that will be plotted. Otherwise, if possible the color will be broadcasted
313-
to all elements. For this, the table in which the color key is found must
314-
annotate the respective element (region must be set to the specific element). If the color column is found
315-
in multiple locations, please provide the table_name to be used for the element.
305+
element : str | None, optional
306+
The name of the points element to render. If `None`, all points elements in the `SpatialData` object will be
307+
used. If you would like multiple specific elements to be rendered, please chain the calls to render:
308+
pl.render_points(...).pl.render_points(...).pl.show()
309+
color : str | None
310+
Can either be string representing a color-like or key in :attr:`sdata.table.obs`. The latter can be used to
311+
color by categorical or continuous variables. If `element` is `None`, if possible the color will be
312+
broadcasted to all elements. For this, the table in which the color key is found must annotate the
313+
respective element (region must be set to the specific element). If the color column is found in multiple
314+
locations, please provide the table_name to be used for the elements.
316315
alpha : float | int, default 1.0
317316
Alpha value for the points.
318-
groups : list[list[str | None]] | list[str | None] | str | None
319-
When using `color` and the key represents discrete labels, `groups`
320-
can be used to show only a subset of them. Other values are set to NA. In general the case of a list of
321-
lists means that there is one list per element to be plotted in the list and this list can contain multiple
322-
discrete labels to be visualized. If not provided as list of lists, broadcasting behaviour is attempted
323-
(use the same values for all elements). If groups is provided but not palette, palette is set to
324-
default "lightgray".
325-
palette : list[list[str | None]] | list[str | None] | str | None
326-
Palette for discrete annotations. List of valid color names that should be
327-
used for the categories. Must match the number of groups. Similarly to groups, in the case of a list of
328-
lists means that there is one list per element to be plotted in the list and this list can contain multiple
329-
palettes (one per group) to be visualized. If not provided as list of lists, broadcasting behaviour is
330-
attempted (use the same values for all elements).
331-
na_color : str | list[float] | None, default "lightgrey"
332-
Color to be used for NAs values, if present. Can either be a named color
333-
("red"), a hex representation ("#000000ff") or a list of floats that
334-
represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values won't
335-
be shown.
317+
groups : list[str] | str | None
318+
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
319+
them. Other values are set to NA. If elment is None, broadcasting behaviour is attempted (use the same
320+
values for all elements).
321+
palette : list[str] | str | None
322+
Palette for discrete annotations. List of valid color names that should be used for the categories. Must
323+
match the number of groups. If element is None, broadcasting behaviour is attempted (use the same values for
324+
all elements). If groups is provided but not palette, palette is set to default "lightgray".
325+
na_color : ColorLike | None, default "lightgrey"
326+
Color to be used for NAs values, if present. Can either be a named color ("red"), a hex representation
327+
("#000000ff") or a list of floats that represent RGB/RGBA values (1.0, 0.0, 0.0, 1.0). When None, the values
328+
won't be shown.
336329
cmap : Colormap | str | None, optional
337-
Colormap for discrete or continuous annotations using 'color', see
338-
:class:`matplotlib.colors.Colormap`. If no palette is given and `color`
339-
refers to a categorical, the colors are sampled from this colormap.
330+
Colormap for discrete or continuous annotations using 'color', see :class:`matplotlib.colors.Colormap`. If
331+
no palette is given and `color` refers to a categorical, the colors are sampled from this colormap.
340332
norm : bool | Normalize, default False
341333
Colormap normalization for continuous annotations.
342334
size : float | int, default 1.0
343335
Size of the points
344-
table_name:
345-
Name of the table(s) containing the color(s) columns. If one name is given than the table is used for each
346-
spatial element to be plotted if the table annotates it. If multiple names are given in a list than the
347-
length must be equal to the number of spatial elements being plotted.
336+
table_name: str | None
337+
Name of the table containing the color(s) columns. If one name is given than the table is used for each
338+
spatial element to be plotted if the table annotates it. If you want to use different tables for particular
339+
elements, as specified under element.
348340
kwargs
349341
Additional arguments to be passed to cmap and norm.
350342
@@ -353,10 +345,9 @@ def render_points(
353345
sd.SpatialData
354346
The modified SpatialData object with the rendered shapes.
355347
"""
356-
params_dict = _validate_render_params(
357-
"points",
348+
params_dict = _validate_points_render_params(
358349
self._sdata,
359-
elements=elements,
350+
element=element,
360351
alpha=alpha,
361352
color=color,
362353
groups=groups,
@@ -365,6 +356,7 @@ def render_points(
365356
cmap=cmap,
366357
norm=norm,
367358
size=size,
359+
table_name=table_name,
368360
)
369361

370362
sdata = self._copy()
@@ -378,18 +370,20 @@ def render_points(
378370
**kwargs,
379371
)
380372

381-
sdata.plotting_tree[f"{n_steps+1}_render_points"] = PointsRenderParams(
382-
elements=params_dict["elements"],
383-
color=params_dict["color"],
384-
col_for_color=params_dict["col_for_color"],
385-
groups=params_dict["groups"],
386-
cmap_params=cmap_params,
387-
palette=params_dict["palette"],
388-
alpha=alpha,
389-
transfunc=kwargs.get("transfunc", None),
390-
size=size,
391-
element_table_mapping=table_name,
392-
)
373+
for element, param_values in params_dict.items():
374+
sdata.plotting_tree[f"{n_steps+1}_render_points"] = PointsRenderParams(
375+
element=element,
376+
color=param_values["color"],
377+
col_for_color=param_values["col_for_color"],
378+
groups=param_values["groups"],
379+
cmap_params=cmap_params,
380+
palette=param_values["palette"],
381+
alpha=param_values["alpha"],
382+
transfunc=kwargs.get("transfunc", None),
383+
size=param_values["size"],
384+
table_name=param_values["table_name"],
385+
)
386+
n_steps += 1
393387

394388
return sdata
395389

@@ -885,7 +879,6 @@ def show(
885879
)
886880

887881
if wanted_points_on_this_cs:
888-
params_copy = _update_params(sdata, params_copy, wanted_points_on_this_cs, "points")
889882
_render_points(
890883
sdata=sdata,
891884
render_params=params_copy,

0 commit comments

Comments
 (0)