Skip to content

Commit

Permalink
add random colormap for visualizing image segmentation in imviz
Browse files Browse the repository at this point in the history
  • Loading branch information
bmorris3 committed Jan 23, 2024
1 parent 4ab5df0 commit 330a3b4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
27 changes: 24 additions & 3 deletions jdaviz/configs/default/plugins/plot_options/plot_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,27 @@

def _register_random_cmap(
cmap_name,
background_color=[0, 0, 0],
background_alpha=0,
bkg_color=[0, 0, 0],
bkg_alpha=1,
seed=42,
ncolors=10_000
):
"""
Custom random colormap, useful for rendering image
segmentation maps. The default background for
`label==0` is *transparent*. If the segmentation map
contains more than 10,000 labels, adjust the `ncolors`
kwarg to ensure uniqueness.
"""
cmap = make_random_cmap(ncolors=ncolors, seed=seed)
cmap.colors[0] = background_color + [background_alpha]
cmap.colors[0] = bkg_color + [bkg_alpha]
cmap.name = cmap_name
colormaps.add(cmap_name, cmap)


_register_random_cmap('Random', bkg_alpha=1)


class SplineStretch:
"""
A class to represent spline stretches.
Expand Down Expand Up @@ -1100,3 +1111,13 @@ def _is_image_viewer(viewer):
viewers = [viewers]

return np.all([_is_image_viewer(viewer) for viewer in viewers])

@observe('image_colormap_value')
def _random_cmap_limit_update(self, *args, **kwargs):
# if 'Random' colormap is used for visualizing image segmentation,
# ensure the stretch limits are the min and max, so all label colors
# are unique:
if self.image_colormap_value != 'Random':
return

self.stretch_preset.value = 100
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,25 @@ def test_track_mixed_states(imviz_helper):
assert not po.stretch_function_sync['mixed']
assert po.stretch_function_value == 'spline'
assert not po.stretch_params_sync['mixed']


def test_segmentation_image(imviz_helper):
# Make one circular segment for a hypothetical
# image with one source:
nx = ny = 100
radius = 15
x0 = y0 = 50

segmentation_map = np.zeros((nx, ny))
xx, yy = np.meshgrid(np.arange(nx), np.arange(ny))
in_circle = np.hypot(xx - x0, yy - y0) < radius
segmentation_map[in_circle] = 1

imviz_helper.load_data(segmentation_map)

plot_opts = imviz_helper.plugins['Plot Options']
plot_opts.image_colormap = 'Random'

# ensure that stretch preset is listening to the update to the
# random colormap so that all colors are uniquely displayed:
assert plot_opts.stretch_preset.value == 100
2 changes: 1 addition & 1 deletion jdaviz/configs/imviz/tests/test_astrowidgets_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_colormap_options(self):
'Gray', 'Viridis', 'Plasma', 'Inferno', 'Magma', 'Purple-Blue',
'Yellow-Green-Blue', 'Yellow-Orange-Red', 'Red-Purple', 'Blue-Green',
'Hot', 'Red-Blue', 'Red-Yellow-Blue', 'Purple-Orange', 'Purple-Green',
'Rainbow', 'Seismic',
'Random', 'Rainbow', 'Seismic',
'Reversed: Gray', 'Reversed: Viridis', 'Reversed: Plasma', 'Reversed: Inferno',
'Reversed: Magma', 'Reversed: Hot', 'Reversed: Rainbow']

Expand Down

0 comments on commit 330a3b4

Please sign in to comment.