Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide warning before clicking in dropdown and disable preset/function #30

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Imviz
- There is now option for image rotation in Orientation (was Links Control) plugin.
This feature requires WCS linking. [#2179]

- Add "Random" colormap for visualizing image segmentation maps. [#2671]

Mosviz
^^^^^^

Expand Down
35 changes: 35 additions & 0 deletions jdaviz/configs/default/plugins/plot_options/plot_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,34 @@
from jdaviz.core.custom_traitlets import IntHandleEmpty, FloatHandleEmpty

from scipy.interpolate import PchipInterpolator
from photutils.utils import make_random_cmap

__all__ = ['PlotOptions']


def _register_random_cmap(
cmap_name,
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] = 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 @@ -1093,3 +1117,14 @@ 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
self.stretch_function.value = 'linear'
29 changes: 26 additions & 3 deletions jdaviz/configs/default/plugins/plot_options/plot_options.vue
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,24 @@
v-model="image_colormap_value"
label="Colormap"
dense
></v-select>
>
<template slot="item" slot-scope="data">
<div class="single-line" style="width: 100%">
<j-tooltip v-if="data.item.text === 'Random'" tooltipcontent="Selecting the 'Random' colormap will set the stretch percentile preset
to min/max, the stretch function to linear, and prevent manually changing either.">
<span>
{{ data.item.text }}
</span>
<v-icon>
mdi-information-outline
</v-icon>
</j-tooltip>
<span v-else>
{{ data.item.text }}
</span>
</div>
</template>
</v-select>
</glue-state-sync-wrapper>
<glue-state-sync-wrapper v-if="image_color_mode_value !== 'Colormaps' || image_color_mode_sync['mixed']" :sync="image_color_sync" :multiselect="layer_multiselect" @unmix-state="unmix_state('image_color')">
<div>
Expand Down Expand Up @@ -493,7 +510,10 @@
:items="stretch_function_sync.choices"
v-model="stretch_function_value"
label="Stretch Function"
class="no-hint"
:disabled="image_colormap_value==='Random'"
:hint="image_colormap_value==='Random' ? 'Cannot set while colormap is set to Random': null"
persistent-hint
dense
></v-select>
</glue-state-sync-wrapper>

Expand All @@ -504,7 +524,10 @@
:items="stretch_preset_sync.choices"
v-model="stretch_preset_value"
label="Stretch Percentile Preset"
class="no-hint"
:disabled="image_colormap_value==='Random'"
:hint="image_colormap_value==='Random' ? 'Cannot set while colormap is set to Random': null"
persistent-hint
dense
></v-select>
</glue-state-sync-wrapper>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,26 @@ 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
assert plot_opts.stretch_function.value == 'linear'
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 @@ -182,7 +182,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
Loading