Skip to content

Commit f3e13b8

Browse files
authored
Render constant-value channels as mid-grey with a warning (#636)
1 parent c6ab456 commit f3e13b8

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/spatialdata_plot/pl/render.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,15 @@ def _render_images(
14211421
if isinstance(ch_norm, Normalize):
14221422
ch_norm = copy(ch_norm)
14231423

1424-
layers[ch] = ch_norm(layers[ch])
1424+
ch_arr = np.asarray(layers[ch])
1425+
ch_min, ch_max = float(ch_arr.min()), float(ch_arr.max())
1426+
if ch_min == ch_max and not (
1427+
isinstance(ch_norm, Normalize) and (ch_norm.vmin is not None or ch_norm.vmax is not None)
1428+
):
1429+
logger.warning(f"Channel {ch!r} has a constant value ({ch_min:.6g}).")
1430+
layers[ch] = np.full(ch_arr.shape, 0.5, dtype=np.float64)
1431+
else:
1432+
layers[ch] = ch_norm(layers[ch])
14251433

14261434
# Colors for the channel legend (set by each branch if applicable)
14271435
legend_colors: list[str] | None = None
27 KB
Loading

tests/pl/test_render_images.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ def test_plot_correctly_normalizes_multichannel_images(self, sdata_raccoon: Spat
154154
axs[1].set_title("two-channel uint16")
155155
fig.tight_layout()
156156

157+
def test_plot_constant_channel_renders_as_midgrey(self):
158+
h, w = 64, 64
159+
data = np.full((1, h, w), 128, dtype=np.uint8)
160+
img = Image2DModel.parse(data, dims=("c", "y", "x"))
161+
sdata = SpatialData(images={"img": img})
162+
sdata.pl.render_images("img").pl.show(title="constant channel: mid-value (not black)")
163+
157164

158165
# ---------------------------------------------------------------------------
159166
# Grayscale + transfunc visual tests

0 commit comments

Comments
 (0)