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

plugin results: support overwriting when data loaded in multiple viewers #2222

Merged
merged 1 commit into from
Jun 1, 2023
Merged
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
26 changes: 18 additions & 8 deletions jdaviz/components/plugin_add_results.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@
:hint="label_hint ? label_hint : 'Label for the resulting data item.'"
></plugin-auto-label>

<plugin-viewer-select v-if="add_to_viewer_items.length > 2"
:items="add_to_viewer_items"
:selected="add_to_viewer_selected"
@update:selected="$emit('update:add_to_viewer_selected', $event)"
show_if_single_entry="true"
label='Plot in Viewer'
:hint="add_to_viewer_hint ? add_to_viewer_hint : 'Plot results in the specified viewer. Data entry will be available in the data dropdown for all applicable viewers.'"
></plugin-viewer-select>
<div v-if="add_to_viewer_items.length > 2">
<v-switch v-if="label_overwrite"
class="hide-input"
label="Show in viewers"
:disabled="true"
:hint="'Visibility of the modified entry will be adopted from the current \''+label+'\' data entry.'"
persistent-hint
>
</v-switch>
<plugin-viewer-select v-else
:items="add_to_viewer_items"
:selected="add_to_viewer_selected"
@update:selected="$emit('update:add_to_viewer_selected', $event)"
show_if_single_entry="true"
label='Plot in Viewer'
:hint="add_to_viewer_hint ? add_to_viewer_hint : 'Plot results in the specified viewer. Data entry will be available in the data dropdown for all applicable viewers.'"
></plugin-viewer-select>
</div>

<v-row v-else>
<v-switch v-if="label_overwrite"
Expand Down
54 changes: 33 additions & 21 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
SubsetDeleteMessage,
SubsetUpdateMessage)
from glue.core.subset import RoiSubsetState
from glue_jupyter.bqplot.image import BqplotImageView
from glue_jupyter.widgets.linked_dropdown import get_choices as _get_glue_choices
from specutils import Spectrum1D
from traitlets import Any, Bool, HasTraits, List, Unicode, observe
Expand Down Expand Up @@ -1678,9 +1679,9 @@ class AutoTextFieldMixin(VuetifyTemplate, HubListener):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.auto_label = AddResults(self, 'label',
'label_default', 'label_auto',
'label_invalid_msg')
self.auto_label = AutoTextField(self, 'label',
'label_default', 'label_auto',
'label_invalid_msg')


class AddResults(BasePluginComponent):
Expand Down Expand Up @@ -1837,25 +1838,31 @@ def add_results_from_plugin(self, data_item, replace=None, label=None):
if label is None:
label = self.label

if replace is None:
replace = self.viewer.selected_reference != 'spectrum-viewer'

if self.label_overwrite and len(self.add_to_viewer_items) <= 2:
if self.label_overwrite:
# the switch for add_to_viewer is hidden, and so the loaded state of the overwritten
# entry should be the same as the original entry (to avoid deleting reference data)
viewer_reference = self.add_to_viewer_items[-1]['reference']
viewer_item = self.app._viewer_item_by_reference(viewer_reference)
viewer = self.app.get_viewer(viewer_reference)
viewer_loaded_labels = [layer.layer.label for layer in viewer.layers]
add_to_viewer_selected = viewer_reference if label in viewer_loaded_labels else 'None' # noqa
visible = label in viewer_item['visible_layers']
add_to_viewer_refs = []
add_to_viewer_vis = []
for viewer_select_item in self.add_to_viewer_items[1:]:
# index 0 is for "None"
viewer_ref = viewer_select_item['reference']
viewer_item = self.app._viewer_item_by_reference(viewer_ref)
viewer = self.app.get_viewer(viewer_ref)
viewer_loaded_labels = [layer.layer.label for layer in viewer.layers]
if label in viewer_loaded_labels:
add_to_viewer_refs.append(viewer_ref)
add_to_viewer_vis.append(label in viewer_item['visible_layers'])
else:
add_to_viewer_selected = self.add_to_viewer_selected
visible = True
if self.add_to_viewer_selected == 'None':
add_to_viewer_refs = []
add_to_viewer_vis = []
else:
add_to_viewer_refs = [self.add_to_viewer_selected]
add_to_viewer_vis = [True]

if label in self.app.data_collection:
if add_to_viewer_selected != 'None':
self.app.remove_data_from_viewer(self.viewer.selected_reference, label)
for viewer_ref in add_to_viewer_refs:
self.app.remove_data_from_viewer(viewer_ref, label)
self.app.data_collection.remove(self.app.data_collection[label])

if not hasattr(data_item, 'meta'):
Expand All @@ -1865,12 +1872,17 @@ def add_results_from_plugin(self, data_item, replace=None, label=None):
data_item.meta['mosviz_row'] = self.app.state.settings['mosviz_row']
self.app.add_data(data_item, label)

if add_to_viewer_selected != 'None':
for viewer_ref, visible in zip(add_to_viewer_refs, add_to_viewer_vis):
# replace the contents in the selected viewer with the results from this plugin
# TODO: switch to an instance/classname check?
self.app.add_data_to_viewer(self.viewer.selected_id,
if replace is not None:
this_replace = replace
else:
this_viewer = self.app.get_viewer(viewer_ref)
this_replace = isinstance(this_viewer, BqplotImageView)

self.app.add_data_to_viewer(viewer_ref,
label,
visible=visible, clear_other_data=replace)
visible=visible, clear_other_data=this_replace)

# update overwrite warnings, etc
self._on_label_changed()
Expand Down