Skip to content

Commit 29b1cc1

Browse files
authored
Drop vmin/vmax kwargs across all render functions (#652)
1 parent 4606ef0 commit 29b1cc1

4 files changed

Lines changed: 45 additions & 41 deletions

File tree

src/spatialdata_plot/pl/basic.py

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from xarray import DataArray, DataTree
2929

3030
from spatialdata_plot._accessor import register_spatial_data_accessor
31-
from spatialdata_plot._logging import _log_context, logger
31+
from spatialdata_plot._logging import _log_context
3232
from spatialdata_plot.pl.render import (
3333
_draw_channel_legend,
3434
_render_images,
@@ -190,7 +190,8 @@ def render_shapes(
190190
shape: Literal["circle", "hex", "visium_hex", "square"] | None = None,
191191
colorbar: bool | str | None = "auto",
192192
colorbar_params: dict[str, object] | None = None,
193-
**kwargs: Any,
193+
datashader_reduction: Literal["sum", "mean", "any", "count", "std", "var", "max", "min"] | None = None,
194+
transfunc: Callable[[float], float] | None = None,
194195
) -> sd.SpatialData:
195196
"""
196197
Render shapes elements in SpatialData.
@@ -279,15 +280,10 @@ def render_shapes(
279280
specified, the shapes are converted to a circle/hexagon/square before rendering. If "visium_hex" is
280281
specified, the shapes are assumed to be Visium spots and the size of the hexagons is adjusted to be adjacent
281282
to each other.
282-
283-
**kwargs : Any
284-
Additional arguments for customization. This can include:
285-
286-
datashader_reduction : Literal[
287-
"sum", "mean", "any", "count", "std", "var", "max", "min"
288-
], default: "max"
289-
Reduction method for datashader when coloring by continuous values. Defaults to 'max'.
290-
283+
datashader_reduction : Literal["sum", "mean", "any", "count", "std", "var", "max", "min"] | None, optional
284+
Reduction method for datashader when coloring by continuous values. When ``None``, defaults to ``"max"``.
285+
transfunc : Callable[[float], float] | None, optional
286+
Optional transformation applied to the continuous color vector before normalization and colormap mapping.
291287
292288
Notes
293289
-----
@@ -300,8 +296,6 @@ def render_shapes(
300296
sd.SpatialData
301297
A copy of the SpatialData object with the rendering parameters stored in its plotting tree.
302298
"""
303-
if "vmin" in kwargs or "vmax" in kwargs:
304-
logger.warning("`vmin` and `vmax` are deprecated. Pass matplotlib `Normalize` object to norm instead.")
305299
params_dict = _validate_shape_render_params(
306300
self._sdata,
307301
element=element,
@@ -320,7 +314,7 @@ def render_shapes(
320314
table_layer=table_layer,
321315
shape=shape,
322316
method=method,
323-
ds_reduction=kwargs.get("datashader_reduction"),
317+
ds_reduction=datashader_reduction,
324318
colorbar=colorbar,
325319
colorbar_params=colorbar_params,
326320
gene_symbols=gene_symbols,
@@ -351,7 +345,7 @@ def render_shapes(
351345
palette=param_values["palette"],
352346
outline_alpha=final_outline_alpha,
353347
fill_alpha=param_values["fill_alpha"],
354-
transfunc=kwargs.get("transfunc"),
348+
transfunc=transfunc,
355349
table_name=param_values["table_name"],
356350
table_layer=param_values["table_layer"],
357351
shape=param_values["shape"],
@@ -384,7 +378,8 @@ def render_points(
384378
gene_symbols: str | None = None,
385379
colorbar: bool | str | None = "auto",
386380
colorbar_params: dict[str, object] | None = None,
387-
**kwargs: Any,
381+
datashader_reduction: Literal["sum", "mean", "any", "count", "std", "var", "max", "min"] | None = None,
382+
transfunc: Callable[[float], float] | None = None,
388383
) -> sd.SpatialData:
389384
"""
390385
Render points elements in SpatialData.
@@ -452,22 +447,16 @@ def render_points(
452447
Column name in :attr:`sdata.table.var` to use for looking up ``color``. Use this when
453448
``var_names`` are e.g. ENSEMBL IDs but you want to refer to genes by their symbols stored
454449
in another column of ``var``. Mimics scanpy's ``gene_symbols`` parameter.
455-
456-
**kwargs : Any
457-
Additional arguments for customization. This can include:
458-
459-
datashader_reduction : Literal[
460-
"sum", "mean", "any", "count", "std", "var", "max", "min"
461-
], default: "sum"
462-
Reduction method for datashader when coloring by continuous values. Defaults to 'sum'.
450+
datashader_reduction : Literal["sum", "mean", "any", "count", "std", "var", "max", "min"] | None, optional
451+
Reduction method for datashader when coloring by continuous values. When ``None``, defaults to ``"sum"``.
452+
transfunc : Callable[[float], float] | None, optional
453+
Optional transformation applied to the continuous color vector before normalization and colormap mapping.
463454
464455
Returns
465456
-------
466457
sd.SpatialData
467458
A copy of the SpatialData object with the rendering parameters stored in its plotting tree.
468459
"""
469-
if "vmin" in kwargs or "vmax" in kwargs:
470-
logger.warning("`vmin` and `vmax` are deprecated. Pass matplotlib `Normalize` object to norm instead.")
471460
params_dict = _validate_points_render_params(
472461
self._sdata,
473462
element=element,
@@ -481,7 +470,7 @@ def render_points(
481470
size=size,
482471
table_name=table_name,
483472
table_layer=table_layer,
484-
ds_reduction=kwargs.get("datashader_reduction"),
473+
ds_reduction=datashader_reduction,
485474
colorbar=colorbar,
486475
colorbar_params=colorbar_params,
487476
gene_symbols=gene_symbols,
@@ -511,7 +500,7 @@ def render_points(
511500
cmap_params=cmap_params,
512501
palette=param_values["palette"],
513502
alpha=param_values["alpha"],
514-
transfunc=kwargs.get("transfunc"),
503+
transfunc=transfunc,
515504
size=param_values["size"],
516505
table_name=param_values["table_name"],
517506
table_layer=param_values["table_layer"],
@@ -730,7 +719,7 @@ def render_labels(
730719
table_name: str | None = None,
731720
table_layer: str | None = None,
732721
gene_symbols: str | None = None,
733-
**kwargs: Any,
722+
transfunc: Callable[[float], float] | None = None,
734723
) -> sd.SpatialData:
735724
"""
736725
Render labels elements in SpatialData.
@@ -806,14 +795,14 @@ def render_labels(
806795
Column name in :attr:`sdata.table.var` to use for looking up ``color``. Use this when
807796
``var_names`` are e.g. ENSEMBL IDs but you want to refer to genes by their symbols stored
808797
in another column of ``var``. Mimics scanpy's ``gene_symbols`` parameter.
798+
transfunc : Callable[[float], float] | None, optional
799+
Optional transformation applied to the continuous color vector before normalization and colormap mapping.
809800
810801
Returns
811802
-------
812803
sd.SpatialData
813804
A copy of the SpatialData object with the rendering parameters stored in its plotting tree.
814805
"""
815-
if "vmin" in kwargs or "vmax" in kwargs:
816-
logger.warning("`vmin` and `vmax` are deprecated. Pass matplotlib `Normalize` object to norm instead.")
817806
params_dict = _validate_label_render_params(
818807
self._sdata,
819808
element=element,
@@ -859,7 +848,7 @@ def render_labels(
859848
scale=param_values["scale"],
860849
table_name=param_values["table_name"],
861850
table_layer=param_values["table_layer"],
862-
transfunc=kwargs.get("transfunc"),
851+
transfunc=transfunc,
863852
zorder=n_steps,
864853
colorbar=param_values["colorbar"],
865854
colorbar_params=param_values["colorbar_params"],

tests/pl/test_render_images.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,21 @@ def test_cmap_matches_selected_channels_not_full_image(sdata_blobs: SpatialData)
531531
plt.close(fig)
532532

533533

534+
# Regression for #612: vmin/vmax kwargs are no longer accepted on any render
535+
# function. The check covers all four to prevent the asymmetry from re-emerging.
536+
@pytest.mark.parametrize("kwarg", ["vmin", "vmax"])
537+
@pytest.mark.parametrize("func", ["render_images", "render_shapes", "render_points", "render_labels"])
538+
def test_vmin_vmax_kwargs_rejected_uniformly(sdata_blobs: SpatialData, func: str, kwarg: str) -> None:
539+
elements = {
540+
"render_images": "blobs_image",
541+
"render_labels": "blobs_labels",
542+
"render_points": "blobs_points",
543+
"render_shapes": "blobs_circles",
544+
}
545+
with pytest.raises(TypeError, match=kwarg):
546+
getattr(sdata_blobs.pl, func)(elements[func], **{kwarg: 0})
547+
548+
534549
# ---------------------------------------------------------------------------
535550
# channels_as_legend visual tests (#459)
536551
# ---------------------------------------------------------------------------

tests/pl/test_render_labels.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ def _make_tablemodel_with_categorical_labels(sdata_blobs, label):
156156

157157
_, axs = plt.subplots(nrows=1, ncols=3, layout="tight")
158158

159-
sdata_blobs.pl.render_labels(label, color="channel_1_sum", table="other_table", scale="scale0").pl.show(
160-
ax=axs[0], title="ch_1_sum", colorbar=False
161-
)
162-
sdata_blobs.pl.render_labels(label, color="channel_2_sum", table="other_table", scale="scale0").pl.show(
163-
ax=axs[1], title="ch_2_sum", colorbar=False
164-
)
165-
sdata_blobs.pl.render_labels(label, color="which_max", table="other_table", scale="scale0").pl.show(
159+
sdata_blobs.pl.render_labels(
160+
label, color="channel_1_sum", table_name="other_table", scale="scale0"
161+
).pl.show(ax=axs[0], title="ch_1_sum", colorbar=False)
162+
sdata_blobs.pl.render_labels(
163+
label, color="channel_2_sum", table_name="other_table", scale="scale0"
164+
).pl.show(ax=axs[1], title="ch_2_sum", colorbar=False)
165+
sdata_blobs.pl.render_labels(label, color="which_max", table_name="other_table", scale="scale0").pl.show(
166166
ax=axs[2], legend_fontsize=6
167167
)
168168

tests/pl/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ def test_plot_can_set_zero_in_cmap_to_transparent(self, sdata_blobs: SpatialData
6868
new_cmap = set_zero_in_cmap_to_transparent(cmap="viridis")
6969

7070
# baseline img
71-
sdata_blobs.pl.render_labels("blobs_labels", color="my_var", cmap="viridis", table="table").pl.show(
71+
sdata_blobs.pl.render_labels("blobs_labels", color="my_var", cmap="viridis", table_name="table").pl.show(
7272
ax=axs[0], colorbar=False
7373
)
7474

7575
sdata_blobs.tables["table"].obs.iloc[8:12, 2] = 0
7676

7777
# image with 0s as transparent, so some labels are "missing"
78-
sdata_blobs.pl.render_labels("blobs_labels", color="my_var", cmap=new_cmap, table="table").pl.show(
78+
sdata_blobs.pl.render_labels("blobs_labels", color="my_var", cmap=new_cmap, table_name="table").pl.show(
7979
ax=axs[1], colorbar=False
8080
)
8181

0 commit comments

Comments
 (0)