Skip to content

Commit

Permalink
LayerSelect: fix wcs-only filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry authored and bmorris3 committed Nov 8, 2023
1 parent 179c8fe commit 1bd45a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(self, *args, **kwargs):

self.layer = LayerSelect(
self, 'layer_items', 'layer_selected', 'viewer_selected',
'multiselect', only_wcs_layers=True
'multiselect', filters=['is_wcs_only']
)
self.orientation_layer_label = AutoTextField(
self, 'new_layer_label', 'new_layer_label_default', 'new_layer_label_auto', None
Expand Down
36 changes: 14 additions & 22 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ def __init__(self, plugin, items, selected, viewer,
multiselect=None,
default_text=None, manual_options=[],
default_mode='first',
only_wcs_layers=False):
filters=['is_not_wcs_only']):
"""
Parameters
----------
Expand All @@ -1223,7 +1223,6 @@ def __init__(self, plugin, items, selected, viewer,
``default`` text is provided but not in ``manual_options`` it will still be included as
the first item in the list.
"""

super().__init__(plugin,
items=items,
selected=selected,
Expand All @@ -1233,8 +1232,6 @@ def __init__(self, plugin, items, selected, viewer,
manual_options=manual_options,
default_mode=default_mode)

self.only_wcs_layers = only_wcs_layers

self.hub.subscribe(self, AddDataMessage,
handler=lambda _: self._on_layers_changed())
self.hub.subscribe(self, RemoveDataMessage,
Expand All @@ -1249,7 +1246,7 @@ def __init__(self, plugin, items, selected, viewer,

self.app.state.add_callback('layer_icons', lambda _: self._on_layers_changed())
self.add_observe(viewer, self._on_viewer_changed)
self._on_layers_changed()
self.filters = filters

def _get_viewer(self, viewer):
# newer will likely be the viewer name in most cases, but viewer id in the case
Expand All @@ -1276,6 +1273,15 @@ def _on_viewer_changed(self, msg=None):
self._clear_cache()
self._on_layers_changed()

def _is_valid_item(self, layer):
def is_wcs_only(layer):
return hasattr(layer.layer, 'meta') and layer.layer.meta.get('_WCS_ONLY', False)

def is_not_wcs_only(layer):
return not is_wcs_only(layer)

return super()._is_valid_item(layer, locals())

@observe('filters')
def _on_layers_changed(self, msg=None):
# NOTE: _on_layers_changed is passed without a msg object during init
Expand All @@ -1289,23 +1295,9 @@ def _on_layers_changed(self, msg=None):

# use getattr so the super() call above doesn't try to access the attr before
# it is initialized:
if not getattr(self, 'only_wcs_layers', False):
layers = [
layer for viewer in viewers
for layer in getattr(viewer, 'layers', [])
# don't include WCS-only layers unless asked:
if (
not hasattr(layer.layer, 'meta') or
(not layer.layer.meta.get('_WCS_ONLY', False))
)
]
else:
layers = [
layer for viewer in viewers
for layer in getattr(viewer, 'layers', [])
# only include WCS-only layers:
if not hasattr(layer.layer, 'meta') or layer.layer.meta.get('_WCS_ONLY', False)
]
layers = [layer for viewer in viewers for layer in getattr(viewer, 'layers', [])
if self._is_valid_item(layer)]

# remove duplicates - NOTE: by doing this, any color-mismatch between layers with the
# same name in different viewers will be randomly assigned within plot_options
# based on which was found _first.
Expand Down

0 comments on commit 1bd45a7

Please sign in to comment.