From 92ec6d0eb7a27f2623d0a635ff7e5966bd749180 Mon Sep 17 00:00:00 2001 From: Zuolin Cheng Date: Fri, 4 Dec 2026 20:59:51 -0800 Subject: [PATCH] Explain value-range changes when rasterizing images --- src/spatialdata_plot/pl/utils.py | 2 +- tests/pl/test_utils.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/spatialdata_plot/pl/utils.py b/src/spatialdata_plot/pl/utils.py index 5f45a9a3..ab1d43df 100644 --- a/src/spatialdata_plot/pl/utils.py +++ b/src/spatialdata_plot/pl/utils.py @@ -939,7 +939,7 @@ def _rasterize_if_necessary( else: return image - logger.info("Rasterizing image for faster rendering.") + logger.info("Rasterizing image for faster rendering. Rasterization may change the image's value range.") image = _rasterize_to_bbox(image, bbox, coordinate_system, target_x_dims, target_y_dims) if hasattr(image.data, "compute"): # rasterize is lazy; downstream reads the result once per channel (NaN check, compositing, diff --git a/tests/pl/test_utils.py b/tests/pl/test_utils.py index 5fe325ee..85bd6615 100644 --- a/tests/pl/test_utils.py +++ b/tests/pl/test_utils.py @@ -520,6 +520,26 @@ def fake_rasterize(*args, **kwargs): ) +def test_rasterization_logs_changed_value_range(caplog, monkeypatch): + import logging + + from xarray import DataArray + + from spatialdata_plot.pl import utils as plut + + image = DataArray(np.zeros((120, 120))) + rasterized = DataArray(np.zeros((10, 10))) + monkeypatch.setattr(plut, "_rasterize_to_bbox", lambda *args: rasterized) + extent = {"x": (0, 120), "y": (0, 120)} + + with caplog.at_level(logging.INFO, logger=plut.logger.name): + assert plut._rasterize_if_necessary(image, 10, 15, 15, "global", extent) is image + assert not caplog.records + assert plut._rasterize_if_necessary(image, 10, 1, 1, "global", extent) is rasterized + + assert "value range" in caplog.text + + def _add_shapes_table(sdata: SpatialData, element: str = "blobs_polygons", name: str = "shapes_table") -> SpatialData: """Add a table annotating a shapes element so it can be measured.""" gdf = sdata[element]