Skip to content

Commit e5959ae

Browse files
committed
add support for single-channel plotting with selected colormaps; bump version to 0.3.5
1 parent 32d103e commit e5959ae

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
66

77
[project]
88
name = "ez_zarr"
9-
version = "0.3.4"
9+
version = "0.3.5"
1010
description = "Give easy, high-level access to ome-zarr filesets."
1111
readme = { file = "README.md", content-type = "text/markdown" }
1212
authors = [

src/ez_zarr/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = '0.3.4'
1+
__version__ = '0.3.5'
22
__author__ = 'Silvia Barbiero, Michael Stadler, Charlotte Soneson'

src/ez_zarr/ome_zarr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
__all__ = ['Image', 'ImageList', 'create_name_row_col',
1111
'create_name_plate_A01', 'import_plate']
12-
__version__ = '0.3.4'
12+
__version__ = '0.3.5'
1313
__author__ = 'Silvia Barbiero, Michael Stadler, Charlotte Soneson'
1414

1515

src/ez_zarr/plotting.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,17 @@ def plot_image(im: np.ndarray,
276276
in points (e.g. `12.5`) or a relative size (e.g. `'xx-large'`).
277277
channels (list[int]): The image channel(s) to be plotted. For example,
278278
to plot the first and third channel of a 4-channel image with
279-
shape (4,1,500,600), you can use `channels=[0, 2]`.
279+
shape (4,1,500,600), you can use `channels=[0, 2]`. If several
280+
channels are given, they will be mapped to colors using
281+
`channel_colors`. In the case of a single channel, `channel_colors`
282+
can also be the name of a supported colormap.
280283
channel_colors (list[str]): A list with python color strings
281284
(e.g. 'red') defining the color for each channel in `channels`.
282285
For example, to map the selected `channels=[0, 2]` to
283286
cyan and magenta, respectively, you can use
284-
`channel_colors=['cyan', 'magenta']`.
287+
`channel_colors=['cyan', 'magenta']`. If a single channel is
288+
given (e.g. `channels=[0]`), this can also be one of the following
289+
colormaps: 'viridis', 'plasma', 'inferno', 'magma', 'cividis'.
285290
channel_ranges (list[list[float]]): A list of 2-element lists
286291
(e.g. [0.01, 0.95]) giving the the value ranges that should
287292
be mapped to colors for each channel. If the given numerical
@@ -361,6 +366,10 @@ def plot_image(im: np.ndarray,
361366
assert all([ch <= nch for ch in channels]), (
362367
f"Invalid `channels` parameter, must be less or equal to {nch}"
363368
)
369+
assert len(channels) == len(channel_colors), (
370+
f"`channels` and `channel_colors` must have the same length, "
371+
f"but are {len(channels)} and {len(channel_colors)}"
372+
)
364373
assert axis_style != 'micrometer' or spacing_yx != None, (
365374
f"For `axis_style='micrometer', the parameter `spacing_yx` needs to be provided."
366375
)
@@ -410,14 +419,22 @@ def plot_image(im: np.ndarray,
410419
if not image_transform is None:
411420
im = image_transform(im)
412421

413-
# convert (ch,y,x) to rgb (y,x,3) and plot
414-
im_rgb = convert_to_rgb(im=im[channels],
415-
channel_colors=channel_colors,
416-
channel_ranges=channel_ranges)
422+
# convert (ch,y,x) to rgb (y,x,3)
423+
if len(channels) == 1 and channel_colors[0] in ['viridis', 'plasma', 'inferno',
424+
'magma', 'cividis']:
425+
# map to colormap
426+
im_rgb = np.squeeze(im[channels], axis=0)
427+
cmap = channel_colors[0]
428+
else:
429+
# map to rgb
430+
im_rgb = convert_to_rgb(im=im[channels],
431+
channel_colors=channel_colors,
432+
channel_ranges=channel_ranges)
433+
cmap = None
417434

418435
# define nested function with main plotting code
419436
def _do_plot():
420-
plt.imshow(im_rgb)
437+
plt.imshow(im_rgb, cmap=cmap)
421438
if not msk is None and np.max(msk) > 0:
422439
plt.imshow(msk,
423440
interpolation='none',

tests/test_plotting.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ def test_plot_image(npa4d: np.ndarray, npa3d: np.ndarray, tmpdir: str):
102102
scalebar_pixel=100,
103103
scalebar_position='error')
104104

105+
with pytest.raises(Exception) as e_info:
106+
plot_image(im=npa4d, msk=None,
107+
channels=[0, 1],
108+
channel_colors=['white'],
109+
channel_ranges=[[0.01, 0.99]],
110+
title='test')
111+
112+
with pytest.raises(Exception) as e_info:
113+
plot_image(im=npa4d, msk=None,
114+
channels=[0, 1],
115+
channel_colors=['viridis', 'inferno'],
116+
channel_ranges=[[0.01, 0.99]],
117+
title='test')
118+
105119
with pytest.raises(Exception) as e_info:
106120
plot_image(im=npa4d, msk=None,
107121
channels=[1],
@@ -133,6 +147,12 @@ def test_plot_image(npa4d: np.ndarray, npa3d: np.ndarray, tmpdir: str):
133147
image_transform=np.log1p,
134148
axis_style=st,
135149
spacing_yx=[0.325, 0.325])
150+
# single channel with colormap
151+
plot_image(im=npa4d, msk=None,
152+
channels=[1],
153+
channel_colors=['inferno'],
154+
channel_ranges=[[0.01, 0.99]],
155+
title='test', call_show=False)
136156
# image with masks
137157
plot_image(im=npa4d, msk=npa3d,
138158
channels=[1],

0 commit comments

Comments
 (0)