Skip to content

Commit

Permalink
fix updating live preview on change to subset
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Jul 30, 2024
1 parent 3937e16 commit 499f935
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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',
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
28 changes: 19 additions & 9 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
----------
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 499f935

Please sign in to comment.