Skip to content

Commit 3c97c29

Browse files
authored
Multi-channel images now correctly normalised, even when uint16 (#520)
1 parent 1594358 commit 3c97c29

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/spatialdata_plot/pl/render.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,13 +1103,11 @@ def _render_images(
11031103
layers[ch] = img.sel(c=ch).copy(deep=True).squeeze()
11041104
if isinstance(render_params.cmap_params, list):
11051105
ch_norm = render_params.cmap_params[ch_idx].norm
1106-
ch_cmap_is_default = render_params.cmap_params[ch_idx].cmap_is_default
11071106
else:
11081107
ch_norm = render_params.cmap_params.norm
1109-
ch_cmap_is_default = render_params.cmap_params.cmap_is_default
11101108

1111-
if not ch_cmap_is_default and ch_norm is not None:
1112-
layers[ch_idx] = ch_norm(layers[ch_idx])
1109+
if ch_norm is not None:
1110+
layers[ch] = ch_norm(layers[ch])
11131111

11141112
# 2A) Image has 3 channels, no palette info, and no/only one cmap was given
11151113
if palette is None and n_channels == 3 and not isinstance(render_params.cmap_params, list):
10.3 KB
Loading
72.7 KB
Loading

tests/pl/test_render_images.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import dask.array as da
22
import matplotlib
3+
import matplotlib.pyplot as plt
4+
import numpy as np
35
import scanpy as sc
46
from matplotlib.colors import Normalize
57
from spatial_image import to_spatial_image
68
from spatialdata import SpatialData
9+
from spatialdata.models import Image2DModel
710

811
import spatialdata_plot # noqa: F401
912
from tests.conftest import DPI, PlotTester, PlotTesterMeta, _viridis_with_under_over
@@ -130,3 +133,17 @@ def test_plot_can_stick_to_zorder(self, sdata_blobs: SpatialData):
130133

131134
def test_plot_can_render_multiscale_image_with_custom_cmap(self, sdata_blobs: SpatialData):
132135
sdata_blobs.pl.render_images("blobs_multiscale_image", channel=0, scale="scale2", cmap="Greys").pl.show()
136+
137+
def test_plot_correctly_normalizes_multichannel_images(self, sdata_raccoon: SpatialData):
138+
sdata_raccoon["raccoon_int16"] = Image2DModel.parse(
139+
sdata_raccoon["raccoon"].data.astype(np.uint16) * 257, # 255 * 257 = 65535,
140+
dims=("c", "y", "x"),
141+
)
142+
143+
# show multi-channel vs single-channel
144+
fig, axs = plt.subplots(nrows=1, ncols=2)
145+
sdata_raccoon.pl.render_images("raccoon_int16", channel=[0]).pl.show(ax=axs[0], colorbar=False)
146+
axs[0].set_title("single-channel uint16")
147+
sdata_raccoon.pl.render_images("raccoon_int16", channel=[0, 1], palette=["yellow", "red"]).pl.show(ax=axs[1])
148+
axs[1].set_title("two-channel uint16")
149+
fig.tight_layout()

0 commit comments

Comments
 (0)