Skip to content

Commit

Permalink
decorator to set traitlet during method
Browse files Browse the repository at this point in the history
and apply across all plugins
  • Loading branch information
kecnry committed Nov 10, 2023
1 parent fa990ce commit 6e8edf5
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 11 deletions.
4 changes: 3 additions & 1 deletion jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from jdaviz.core.template_mixin import (PluginTemplateMixin,
DatasetSelectMixin,
SpectralSubsetSelectMixin,
AddResultsMixin)
AddResultsMixin,
with_spinner)
from jdaviz.core.user_api import PluginUserApi

__all__ = ['MomentMap']
Expand Down Expand Up @@ -91,6 +92,7 @@ def _set_default_results_label(self, event={}):
label_comps += [f"moment {self.n_moment}"]
self.results_label_default = " ".join(label_comps)

@with_spinner()
def calculate_moment(self, add_data=True):
"""
Calculate the moment map
Expand Down
1 change: 1 addition & 0 deletions jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
:add_to_viewer_selected.sync="add_to_viewer_selected"
action_label="Calculate"
action_tooltip="Calculate moment map"
:action_spinner="spinner"
@click:action="calculate_moment"
></plugin-add-results>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from jdaviz.core.template_mixin import (PluginTemplateMixin,
SelectPluginComponent,
SpatialSubsetSelectMixin,
AddResultsMixin)
AddResultsMixin,
with_spinner)
from jdaviz.core.user_api import PluginUserApi
from jdaviz.configs.cubeviz.plugins.parsers import _return_spectrum_with_correct_units

Expand Down Expand Up @@ -72,6 +73,7 @@ def user_api(self):
)
)

@with_spinner()
def collapse_to_spectrum(self, add_data=True, **kwargs):
"""
Collapse over the spectral axis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
:add_to_viewer_selected.sync="add_to_viewer_selected"
action_label="Extract"
action_tooltip="Run spectral extraction with error and mask propagation"
:action_spinner="spinner"
@click:action="spectral_extraction"
></plugin-add-results>

Expand Down
4 changes: 3 additions & 1 deletion jdaviz/configs/default/plugins/collapse/collapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
DatasetSelectMixin,
SelectPluginComponent,
SpectralSubsetSelectMixin,
AddResultsMixin)
AddResultsMixin,
with_spinner)
from jdaviz.core.user_api import PluginUserApi

__all__ = ['Collapse']
Expand Down Expand Up @@ -69,6 +70,7 @@ def _set_default_results_label(self, event={}):
label_comps += ["collapsed"]
self.results_label_default = " ".join(label_comps)

@with_spinner()
def collapse(self, add_data=True):
"""
Collapse over the spectral axis.
Expand Down
1 change: 1 addition & 0 deletions jdaviz/configs/default/plugins/collapse/collapse.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
:add_to_viewer_selected.sync="add_to_viewer_selected"
action_label="Collapse"
action_tooltip="Collapse data"
:action_spinner="spinner"
@click:action="collapse"
></plugin-add-results>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from jdaviz.core.events import SnackbarMessage
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import (PluginTemplateMixin, DatasetSelectMixin,
SelectPluginComponent, AddResultsMixin)
SelectPluginComponent, AddResultsMixin,
with_spinner)
from jdaviz.core.user_api import PluginUserApi

__all__ = ['GaussianSmooth']
Expand Down Expand Up @@ -176,6 +177,7 @@ def smooth(self, add_data=True):

return results

@with_spinner()
def spectral_smooth(self):
"""
Smooth the input spectrum along the spectral axis. To add the resulting spectrum into
Expand All @@ -199,6 +201,7 @@ def spectral_smooth(self):

return spec_smoothed

@with_spinner('spinner')
def spatial_smooth(self):
"""
Use astropy convolution machinery to smooth the spatial dimensions of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
:add_to_viewer_selected.sync="add_to_viewer_selected"
action_label="Smooth"
action_tooltip="Smooth data"
:action_spinner="spinner"
@click:action="apply"
></plugin-add-results>
</j-tray-plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
NonFiniteUncertaintyMismatchMixin,
AutoTextField,
AddResultsMixin,
TableMixin)
TableMixin,
with_spinner)
from jdaviz.core.custom_traitlets import IntHandleEmpty
from jdaviz.core.user_api import PluginUserApi
from jdaviz.configs.default.plugins.model_fitting.fitting_backend import fit_model_to_spectrum
Expand Down Expand Up @@ -767,6 +768,7 @@ def _update_viewer_filters(self, event={}):
# only want spectral viewers in the options
self.add_results.viewer.filters = ['is_spectrum_viewer']

@with_spinner()
def calculate_fit(self, add_data=True):
"""
Calculate the fit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@
action_label="Fit Model"
action_tooltip="Fit the model to the data"
:action_disabled="model_equation_invalid_msg.length > 0 || !spectral_subset_valid"
:action_spinner="spinner"
@click:action="apply"
>
<div v-if="config!=='cubeviz' || !cube_fit">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from jdaviz.core.region_translators import regions2aperture, _get_region_from_spatial_subset
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import (PluginTemplateMixin, DatasetMultiSelectMixin,
SubsetSelect, TableMixin, PlotMixin)
SubsetSelect, TableMixin, PlotMixin, with_spinner)
from jdaviz.core.tools import ICON_DIR
from jdaviz.utils import PRIHDR_KEY

Expand Down Expand Up @@ -305,6 +305,7 @@ def _background_selected_changed(self, event={}):
self.hub.broadcast(SnackbarMessage(
f"Failed to extract {background_selected}: {repr(e)}", color='error', sender=self))

@with_spinner()
def calculate_photometry(self, dataset=None, aperture=None, background=None,
background_value=None, pixel_area=None, counts_factor=None,
flux_scaling=None, add_to_table=True, update_plots=True):
Expand Down Expand Up @@ -726,6 +727,7 @@ def _unpack_dict_list(mult_values, single_values):

return _unpack_dict_list(mult_values, single_values)

@with_spinner()
def calculate_batch_photometry(self, options=[], add_to_table=True, update_plots=True,
full_exceptions=False):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
</v-row>

<v-row justify="end">
<v-btn color="primary" text @click="do_aper_phot" :disabled="aperture_selected === background_selected">Calculate</v-btn>
<v-btn color="primary" text @click="do_aper_phot" :disabled="aperture_selected === background_selected || spinner">Calculate</v-btn>
</v-row>
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion jdaviz/configs/imviz/plugins/catalogs/catalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from jdaviz.core.events import SnackbarMessage
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import (PluginTemplateMixin, ViewerSelectMixin,
FileImportSelectPluginComponent, HasFileImportSelect)
FileImportSelectPluginComponent, HasFileImportSelect,
with_spinner)

__all__ = ['Catalogs']

Expand Down Expand Up @@ -55,6 +56,7 @@ def _file_parser(path):

return '', {path: table}

@with_spinner()
def search(self):
"""
Search the catalog, display markers on the viewer, and return a table of results (or None
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/configs/imviz/plugins/catalogs/catalogs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<v-btn color="primary" text @click="do_clear">Clear</v-btn>
</v-col>
<v-col>
<v-btn color="primary" text @click="do_search">Search</v-btn>
<v-btn color="primary" text @click="do_search" :disabled="spinner">Search</v-btn>
</v-col>
</v-row>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
SelectPluginComponent,
DatasetSelect,
AddResults,
skip_if_no_updates_since_last_active)
skip_if_no_updates_since_last_active,
with_spinner)
from jdaviz.core.user_api import PluginUserApi
from jdaviz.core.custom_traitlets import IntHandleEmpty, FloatHandleEmpty
from jdaviz.core.marks import PluginLine
Expand Down Expand Up @@ -130,6 +131,7 @@ class SpectralExtraction(PluginTemplateMixin):
trace_results_label_overwrite = Bool().tag(sync=True)
trace_add_to_viewer_items = List().tag(sync=True)
trace_add_to_viewer_selected = Unicode().tag(sync=True)
trace_spinner = Bool(False).tag(sync=True)

# BACKGROUND
bg_dataset_items = List().tag(sync=True)
Expand All @@ -156,6 +158,7 @@ class SpectralExtraction(PluginTemplateMixin):
bg_results_label_overwrite = Bool().tag(sync=True)
bg_add_to_viewer_items = List().tag(sync=True)
bg_add_to_viewer_selected = Unicode().tag(sync=True)
bg_img_spinner = Bool(False).tag(sync=True)

bg_spec_results_label = Unicode().tag(sync=True)
bg_spec_results_label_default = Unicode().tag(sync=True)
Expand All @@ -164,6 +167,7 @@ class SpectralExtraction(PluginTemplateMixin):
bg_spec_results_label_overwrite = Bool().tag(sync=True)
bg_spec_add_to_viewer_items = List().tag(sync=True)
bg_spec_add_to_viewer_selected = Unicode().tag(sync=True)
bg_spec_spinner = Bool(False).tag(sync=True)

bg_sub_results_label = Unicode().tag(sync=True)
bg_sub_results_label_default = Unicode().tag(sync=True)
Expand All @@ -172,6 +176,7 @@ class SpectralExtraction(PluginTemplateMixin):
bg_sub_results_label_overwrite = Bool().tag(sync=True)
bg_sub_add_to_viewer_items = List().tag(sync=True)
bg_sub_add_to_viewer_selected = Unicode().tag(sync=True)
bg_sub_spinner = Bool(False).tag(sync=True)

# EXTRACT
ext_dataset_items = List().tag(sync=True)
Expand All @@ -195,6 +200,7 @@ class SpectralExtraction(PluginTemplateMixin):
ext_results_label_overwrite = Bool().tag(sync=True)
ext_add_to_viewer_items = List().tag(sync=True)
ext_add_to_viewer_selected = Unicode().tag(sync=True)
# uses default "spinner"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -682,6 +688,7 @@ def import_trace(self, trace):
else: # pragma: no cover
raise NotImplementedError(f"trace of type {trace.__class__.__name__} not supported")

@with_spinner('trace_spinner')
def export_trace(self, add_data=False, **kwargs):
"""
Create a specreduce Trace object from the input parameters
Expand Down Expand Up @@ -780,6 +787,7 @@ def import_bg(self, bg):

self.bg_width = bg.width

@with_spinner('bg_spinner')
def export_bg(self, **kwargs):
"""
Create a specreduce Background object from the input parameters defined in the plugin.
Expand Down Expand Up @@ -817,6 +825,7 @@ def export_bg(self, **kwargs):

return bg

@with_spinner('bg_img_spinner')
def export_bg_img(self, add_data=False, **kwargs):
"""
Create a background 2D spectrum from the input parameters defined in the plugin.
Expand All @@ -843,6 +852,7 @@ def vue_create_bg_img(self, *args):
color='error', sender=self)
)

@with_spinner('bg_spec_spinner')
def export_bg_spectrum(self, add_data=False, **kwargs):
"""
Create a background 1D spectrum from the input parameters defined in the plugin.
Expand All @@ -863,6 +873,7 @@ def export_bg_spectrum(self, add_data=False, **kwargs):
def vue_create_bg_spec(self, *args):
self.export_bg_spectrum(add_data=True)

@with_spinner('bg_sub_spinner')
def export_bg_sub(self, add_data=False, **kwargs):
"""
Create a background-subtracted 2D spectrum from the input parameters defined in the plugin.
Expand Down Expand Up @@ -936,6 +947,7 @@ def export_extract(self, **kwargs):

return ext

@with_spinner('spinner')
def export_extract_spectrum(self, add_data=False, **kwargs):
"""
Create an extracted 1D spectrum from the input parameters defined in the plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
:add_to_viewer_selected.sync="trace_add_to_viewer_selected"
action_label="Create"
action_tooltip="Create Trace"
:action_spinner="trace_spinner"
@click:action="create_trace"
></plugin-add-results>
</v-expansion-panel-content>
Expand Down Expand Up @@ -293,6 +294,7 @@
:add_to_viewer_selected.sync="bg_add_to_viewer_selected"
action_label="Export"
action_tooltip="Create Background Image"
:action_spinner="bg_img_spinner"
@click:action="create_bg_img"
></plugin-add-results>
</v-expansion-panel-content>
Expand Down Expand Up @@ -415,6 +417,7 @@
action_label="Extract"
action_tooltip="Extract 1D Spectrum"
:action_disabled="ext_specreduce_err.length > 0"
:action_spinner="spinner"
@click:action="extract_spectrum"
></plugin-add-results>
</div>
Expand Down
25 changes: 24 additions & 1 deletion jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@


__all__ = ['show_widget', 'TemplateMixin', 'PluginTemplateMixin',
'skip_if_no_updates_since_last_active',
'skip_if_no_updates_since_last_active', 'with_spinner',
'ViewerPropertiesMixin',
'BasePluginComponent',
'SelectPluginComponent', 'UnitSelectPluginComponent', 'EditableSelectPluginComponent',
Expand Down Expand Up @@ -266,6 +266,28 @@ def wrapper(self, msg={}):
return decorator


def with_spinner(spinner_traitlet='spinner'):
"""
decorator on a plugin method to set a traitlet to True at the beginning
and False either on failure or successful completion. This traitlet can
then be used in the UI to disable elements or display a spinner during
operation. Each plugin gets a 'spinner' traitlet by default, but some plugins
may want different controls for different sections/actions within the plugin.
"""
def decorator(meth):
def wrapper(self, *args, **kwargs):
setattr(self, spinner_traitlet, True)
try:
ret_ = meth(self, *args, **kwargs)
except Exception:
setattr(self, spinner_traitlet, False)
raise
setattr(self, spinner_traitlet, False)
return ret_
return wrapper
return decorator


class PluginTemplateMixin(TemplateMixin):
"""
This base class can be inherited by all sidebar/tray plugins to expose common functionality.
Expand All @@ -276,6 +298,7 @@ class PluginTemplateMixin(TemplateMixin):
uses_active_status = Bool(False).tag(sync=True) # noqa whether the plugin has live-preview marks, set to True in plugins to expose keep_active switch
keep_active = Bool(False).tag(sync=True) # noqa whether the live-preview marks show regardless of active state, inapplicable unless uses_active_status is True
is_active = Bool(False).tag(sync=True) # noqa read-only: whether the previews should be shown according to plugin_opened and keep_active
spinner = Bool(False).tag(sync=True) # noqa use along-side @with_spinner() and <plugin-add-results :action_spinner="spinner">

def __init__(self, **kwargs):
self._viewer_callbacks = {}
Expand Down

0 comments on commit 6e8edf5

Please sign in to comment.