From 499f9353c735b5d9a30301a2f002c8aafde7f87c Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Mon, 22 Jul 2024 14:51:22 -0400 Subject: [PATCH] fix updating live preview on change to subset --- .../spectral_extraction.py | 16 ++++++++--- jdaviz/core/template_mixin.py | 28 +++++++++++++------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py b/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py index 39a1fbbc78..3d5a4a53ad 100644 --- a/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py +++ b/jdaviz/configs/cubeviz/plugins/spectral_extraction/spectral_extraction.py @@ -130,6 +130,7 @@ def __init__(self, *args, **kwargs): self.aperture._default_text = 'Entire Cube' self.aperture._manual_options = ['Entire Cube'] self.aperture.items = [{"label": "Entire Cube"}] + self.aperture._subset_selected_changed_callback = self._update_extract # need to reinitialize choices since we overwrote items and some subsets may already # exist. self.aperture._initialize_choices() @@ -142,7 +143,8 @@ def __init__(self, *args, **kwargs): 'bg_scale_factor', dataset='dataset', multiselect=None, - default_text='None') + default_text='None', + subset_selected_changed_callback=self._update_extract) self.bg_spec_add_results = AddResults(self, 'bg_spec_results_label', 'bg_spec_results_label_default', @@ -701,8 +703,11 @@ def _clear_marks(self): 'function_selected', 'aperture_method_selected', 'previews_temp_disabled') + def _live_update_marks(self, event={}): + self._update_marks(event) + @skip_if_not_tray_instance() - def _toggle_marks(self, event={}): + def _update_marks(self, event={}): visible = self.show_live_preview and self.is_active if not visible: @@ -714,11 +719,14 @@ def _toggle_marks(self, event={}): self.marks['extract'].visible = True # _live_update will skip if no updates since last active - self._live_update(event) + self._live_update_extract(event) @skip_if_no_updates_since_last_active() @with_temp_disable(timeout=0.4) - def _live_update(self, event={}): + def _live_update_extract(self, event={}): + self._update_extract() + + def _update_extract(self): try: ext, bg_extract = self.extract(return_bg=True, add_data=False) except (ValueError, Exception): diff --git a/jdaviz/core/template_mixin.py b/jdaviz/core/template_mixin.py index 541cfafcf2..67cbfcb610 100644 --- a/jdaviz/core/template_mixin.py +++ b/jdaviz/core/template_mixin.py @@ -1816,7 +1816,7 @@ class SubsetSelect(SelectPluginComponent): """ def __init__(self, plugin, items, selected, multiselect=None, selected_has_subregions=None, dataset=None, viewers=None, default_text=None, manual_options=[], filters=[], - default_mode='default_text'): + default_mode='default_text', subset_selected_changed_callback=None): """ Parameters ---------- @@ -1864,6 +1864,7 @@ def __init__(self, plugin, items, selected, multiselect=None, selected_has_subre self._cached_properties += ["selected_subset_state", "selected_spatial_region", "selected_subset_mask"] + self._subset_selected_changed_callback = subset_selected_changed_callback if dataset is not None: # clear selected_subset_mask and selected_spatial_region on change to dataset self.add_observe(self.dataset._plugin_traitlets['selected'], @@ -1928,6 +1929,15 @@ def is_not_annulus(subset): return super()._is_valid_item(subset, locals()) def _update_subset(self, subset, attribute=None): + if (attribute == 'subset_state' and + ((self.is_multiselect and subset.label in self.selected) + or (subset.label == self.selected))): + # updated the currently selected subset, clear all cache + self._clear_cache() + update_has_subregions = True + else: + update_has_subregions = False + if subset.label not in self.labels: # NOTE: this logic will need to be revisited if generic renaming of subsets is added # see https://github.com/spacetelescope/jdaviz/pull/1175#discussion_r829372470 @@ -1946,14 +1956,12 @@ def _update_subset(self, subset, attribute=None): else self._subset_to_dict(subset) for s in self.items] - if (attribute == 'subset_state' and - ((self.is_multiselect and subset.label in self.selected) - or (subset.label == self.selected))): - # updated the currently selected subset - self._clear_cache("selected_obj", "selected_item", "selected_subset_state", - "selected_subset_mask", "selected_subset", "selected_spatial_region") + if update_has_subregions: self._update_has_subregions() + if self._subset_selected_changed_callback is not None: + self._subset_selected_changed_callback() + def _update_has_subregions(self): if "selected_has_subregions" in self._plugin_traitlets.keys(): if self.is_multiselect: @@ -2238,7 +2246,8 @@ class ApertureSubsetSelect(SubsetSelect): """ def __init__(self, plugin, items, selected, selected_validity, scale_factor, multiselect=None, - dataset=None, viewers=None, default_text=None): + dataset=None, viewers=None, default_text=None, + subset_selected_changed_callback=None): """ Parameters ---------- @@ -2269,7 +2278,8 @@ def __init__(self, plugin, items, selected, selected_validity, filters=['is_spatial'], dataset=dataset, viewers=viewers, - default_text=default_text) + default_text=default_text, + subset_selected_changed_callback=subset_selected_changed_callback) self.add_traitlets(selected_validity=selected_validity, scale_factor=scale_factor)