Skip to content

Commit

Permalink
use DatasetMixin instead of LayerMixin
Browse files Browse the repository at this point in the history
* minimizes added code and prevents subsets from being included in the cycler
  • Loading branch information
kecnry committed Feb 24, 2023
1 parent 135c0bc commit 3579693
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 65 deletions.
47 changes: 23 additions & 24 deletions jdaviz/configs/imviz/plugins/coords_info/coords_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from jdaviz.configs.specviz.plugins.viewers import SpecvizProfileView
from jdaviz.core.events import ViewerAddedMessage
from jdaviz.core.registries import tool_registry
from jdaviz.core.template_mixin import TemplateMixin, LayerSelectMultiviewerMixin
from jdaviz.core.template_mixin import TemplateMixin, DatasetSelectMixin
from jdaviz.core.marks import PluginScatter

__all__ = ['CoordsInfo']
Expand All @@ -27,10 +27,10 @@


@tool_registry('g-coords-info')
class CoordsInfo(TemplateMixin, LayerSelectMultiviewerMixin):
class CoordsInfo(TemplateMixin, DatasetSelectMixin):
template_file = __file__, "coords_info.vue"

layer_icon = Unicode("").tag(sync=True) # option for layer (auto, none, or specific layer)
dataset_icon = Unicode("").tag(sync=True) # option for layer (auto, none, or specific layer)
icon = Unicode("").tag(sync=True) # currently exposed layer

row1a_title = Unicode("").tag(sync=True)
Expand Down Expand Up @@ -60,8 +60,8 @@ def __init__(self, *args, **kwargs):
self._create_viewer_callbacks(viewer)
viewer_refs.append(viewer.reference_id)

self.layer._manual_options = ['auto', 'none']
self.layer.viewer = viewer_refs
self.dataset._manual_options = ['auto', 'none']
self.dataset.filters = ['layer_in_viewers']

# subscribe to mouse events on any new viewers
self.hub.subscribe(self, ViewerAddedMessage, handler=self._on_viewer_added)
Expand All @@ -75,7 +75,6 @@ def _create_viewer_callbacks(self, viewer):

def _on_viewer_added(self, msg):
self._create_viewer_callbacks(self.app.get_viewer_by_id(msg.viewer_id))
self.layer.viewer = self.app.get_viewer_reference_names()

@property
def marks(self):
Expand Down Expand Up @@ -160,17 +159,17 @@ def _layers_changed(self, viewer):
# cursor position
self.update_display(viewer, self._x, self._y)

@observe('layer_selected')
def _selected_layer_changed(self, *args):
if self.layer_selected == 'auto':
self.layer_icon = 'mdi-auto-fix'
elif self.layer_selected == 'none':
self.layer_icon = 'mdi-cursor-default'
@observe('dataset_selected')
def _selected_dataset_changed(self, *args):
if self.dataset_selected == 'auto':
self.dataset_icon = 'mdi-auto-fix'
elif self.dataset_selected == 'none':
self.dataset_icon = 'mdi-cursor-default'
else:
self.layer_icon = self.app.state.layer_icons.get(self.layer_selected, '')
self.dataset_icon = self.app.state.layer_icons.get(self.dataset_selected, '')

def vue_next_layer(self, *args, **kwargs):
self.layer.select_next()
self.dataset.select_next()

def update_display(self, viewer, x, y):
if isinstance(viewer, SpecvizProfileView):
Expand All @@ -191,14 +190,14 @@ def _image_viewer_update(self, viewer, x, y):
self._viewer_mouse_clear_event(viewer)
return

if self.layer.selected == 'auto':
if self.dataset.selected == 'auto':
image = active_layer.layer
elif self.layer.selected == 'none':
elif self.dataset.selected == 'none':
active_layer = viewer.layers[0]
image = viewer.layers[0].layer
else:
for layer in viewer.layers:
if layer.layer.label == self.layer.selected and layer.visible:
if layer.layer.label == self.dataset.selected and layer.visible:
active_layer = layer
image = layer.layer
break
Expand All @@ -213,12 +212,12 @@ def _image_viewer_update(self, viewer, x, y):
self._dict['y:unit'] = 'pix'
# set default empty values

if self.layer.selected != 'none' and image is not None:
if self.dataset.selected != 'none' and image is not None:
self.icon = self.app.state.layer_icons.get(image.label, '') # noqa
self._dict['data_label'] = image.label

# separate logic for each viewer type, ultimately needs to result in extracting sky coords
if self.layer.selected == 'none' or image is None:
if self.dataset.selected == 'none' or image is None:
self.icon = 'mdi-cursor-default'
self._dict['data_label'] = ''
coords_status = False
Expand Down Expand Up @@ -316,7 +315,7 @@ def _image_viewer_update(self, viewer, x, y):
# TODO: for now we just use the first visible layer but we should think
# of how to display values when multiple datasets are present.

if self.layer.selected == 'none' or image is None:
if self.dataset.selected == 'none' or image is None:
# no data values to extract
self.row1b_title = ''
self.row1b_text = ''
Expand Down Expand Up @@ -360,7 +359,7 @@ def _spectrum_viewer_update(self, viewer, x, y):
self.row1a_text = f'{x:10.5e}, {y:10.5e}'

# show the locked marker/coords only if either no tool or the default tool is active
if self.layer.selected == 'none':
if self.dataset.selected == 'none':
self.row2_title = '\u00A0'
self.row2_text = ''
self.row3_title = '\u00A0'
Expand All @@ -387,9 +386,9 @@ def _spectrum_viewer_update(self, viewer, x, y):
closest_icon = 'mdi-cursor-default'
closest_distance = None
for lyr in viewer.state.layers:
if self.layer.selected == 'auto' and not lyr.visible:
if self.dataset.selected == 'auto' and not lyr.visible:
continue
if self.layer.selected != 'auto' and self.layer.selected != lyr.layer.label:
if self.dataset.selected != 'auto' and self.dataset.selected != lyr.layer.label:
continue

if isinstance(lyr.layer, GroupedSubset):
Expand All @@ -412,7 +411,7 @@ def _spectrum_viewer_update(self, viewer, x, y):
self.app._get_object_cache[cache_key] = sp

# Out of range in spectral axis.
if (self.layer.selected != lyr.layer.label and
if (self.dataset.selected != lyr.layer.label and
(x < sp.spectral_axis.value.min() or x > sp.spectral_axis.value.max())):
continue

Expand Down
4 changes: 2 additions & 2 deletions jdaviz/configs/imviz/plugins/coords_info/coords_info.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
<v-toolbar-items>
<j-tooltip tipid='coords-info-cycle'>
<v-btn icon tile @click="next_layer()">
<j-layer-viewer-icon :icon="layer_icon" color="white" :prevent_invert_if_dark="true"></j-layer-viewer-icon>
<j-layer-viewer-icon :icon="dataset_icon" color="white" :prevent_invert_if_dark="true"></j-layer-viewer-icon>
</v-btn>
</j-tooltip>
<span style="padding-top: 22px">
{{layer_selected}}
{{dataset_selected}}
</span>
</v-toolbar-items>

Expand Down
40 changes: 1 addition & 39 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'SubsetSelect', 'SpatialSubsetSelectMixin', 'SpectralSubsetSelectMixin',
'DatasetSpectralSubsetValidMixin',
'ViewerSelect', 'ViewerSelectMixin',
'LayerSelect', 'LayerSelectMixin', 'LayerSelectMultiviewerMixin',
'LayerSelect', 'LayerSelectMixin',
'DatasetSelect', 'DatasetSelectMixin',
'Table', 'TableMixin',
'AutoTextField', 'AutoTextFieldMixin',
Expand Down Expand Up @@ -824,44 +824,6 @@ def __init__(self, *args, **kwargs):
'layer_multiselect')


class LayerSelectMultiviewerMixin(VuetifyTemplate, HubListener):
"""
Applies the LayerSelect component as a mixin in the base plugin. This
automatically adds traitlets as well as new properties to the plugin with minimal
extra code. For multiple instances or custom traitlet names/defaults, use the
component instead.
To use in a plugin:
* add ``LayerSelectMultiviewerMixin`` as a mixin to the class
* use the traitlets available from the plugin or properties/methods available from
``plugin.layer``.
Example template (label and hint are optional)::
<plugin-layer-select
:items="layer_items"
:selected.sync="layer_selected"
:show_if_single_entry="true"
label="Layer"
hint="Select layer."
/>
"""
layer_items = List().tag(sync=True)
layer_selected = Any().tag(sync=True)
layer_viewer = List().tag(sync=True)
layer_multiselect = Bool(False).tag(sync=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.layer = LayerSelect(self,
'layer_items',
'layer_selected',
'layer_viewer',
'layer_multiselect')


class SubsetSelect(SelectPluginComponent):
"""
Plugin select for subsets, with support for single or multi-selection.
Expand Down

0 comments on commit 3579693

Please sign in to comment.