Skip to content

Commit

Permalink
flatten/binning: handle slow live-updates for large data (#56)
Browse files Browse the repository at this point in the history
* flatten: disable live-updating when slow

* raise a warning alert in the plugin telling the user the live-previews aren't updating and prompt them to manually update the preview or to disable the preview markers

* binning: disable live-updating when slow

* make use of action_spinner

* doesn't require upstream PR, but won't have any affect without it
  • Loading branch information
kecnry authored Nov 27, 2023
1 parent fbdfbd4 commit 989e282
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
22 changes: 19 additions & 3 deletions lcviz/plugins/binning/binning.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import numpy as np
from time import time
from astropy.time import Time
from traitlets import Bool, observe
from traitlets import Bool, Float, observe
from glue.config import data_translator

from jdaviz.core.custom_traitlets import IntHandleEmpty
Expand Down Expand Up @@ -46,6 +48,10 @@ class Binning(PluginTemplateMixin, DatasetSelectMixin, EphemerisSelectMixin, Add
n_bins = IntHandleEmpty(100).tag(sync=True)
bin_enabled = Bool(True).tag(sync=True)

last_live_time = Float(0).tag(sync=True)
previews_temp_disable = Bool(False).tag(sync=True)
spinner = Bool(False).tag(sync=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -145,14 +151,19 @@ def _toggle_marks(self, event={}):
self._live_update(event)

@observe('dataset_selected', 'ephemeris_selected',
'n_bins')
'n_bins', 'previews_temp_disable')
@skip_if_no_updates_since_last_active()
def _live_update(self, event={}):
if not self.show_live_preview or not self.is_active:
self._clear_marks()
self.bin_enabled = self.n_bins != '' and self.n_bins > 0
return

if self.previews_temp_disable:
return

start = time()

if event.get('name', '') not in ('is_active', 'show_live_preview'):
# mark visibility hasn't been handled yet
self._toggle_marks()
Expand Down Expand Up @@ -189,6 +200,10 @@ def _live_update(self, event={}):
mark.times = []
mark.update_xy(times, lc.flux.value)

self.last_live_time = np.round(time() - start, 2)
if self.last_live_time > 0.3:
self.previews_temp_disable = True

def _on_ephemeris_update(self, msg):
if not self.show_live_preview or not self.is_active:
return
Expand All @@ -199,7 +214,7 @@ def _on_ephemeris_update(self, msg):
self._live_update()

def bin(self, add_data=True):

self.spinner = True
if self.n_bins == '' or self.n_bins <= 0:
raise ValueError("n_bins must be a positive integer")

Expand Down Expand Up @@ -245,6 +260,7 @@ def bin(self, add_data=True):
# by resetting x_att, the preview marks may have dissappeared
self._live_update()

self.spinner = False
return lc

def vue_apply(self, event={}):
Expand Down
19 changes: 19 additions & 0 deletions lcviz/plugins/binning/binning.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@
</v-text-field>
</v-row>

<v-alert v-if="previews_temp_disable && show_live_preview" type='warning' style="margin-left: -12px; margin-right: -12px">
Live-updating is temporarily disabled (last update took {{last_live_time}}s)
<v-row justify='center'>
<j-tooltip tooltipcontent='hide live preview (can be re-enabled from the settings section in the plugin).' span_style="width: 100%">
<v-btn style='width: 100%' @click="show_live_preview = false">
disable previews
</v-btn>
</j-tooltip>
</v-row>
<v-row justify='center'>
<j-tooltip tooltipcontent='manually update live-previews based on current plugin inputs.' span_style="width: 100%">
<v-btn style='width: 100%' @click="previews_temp_disable = false">
update preview
</v-btn>
</j-tooltip>
</v-row>
</v-alert>

<plugin-add-results
:label.sync="results_label"
:label_default="results_label_default"
Expand All @@ -69,6 +87,7 @@
action_label="Bin"
action_tooltip="Bin data"
:action_disabled="!bin_enabled"
:action_spinner="spinner"
@click:action="apply"
></plugin-add-results>

Expand Down
19 changes: 17 additions & 2 deletions lcviz/plugins/flatten/flatten.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
from time import time

from traitlets import Bool, Unicode, observe
from traitlets import Bool, Float, Unicode, observe

from jdaviz.core.custom_traitlets import FloatHandleEmpty, IntHandleEmpty
from jdaviz.core.events import ViewerAddedMessage
Expand Down Expand Up @@ -57,6 +58,10 @@ class Flatten(PluginTemplateMixin, DatasetSelectMixin, AddResultsMixin):
sigma = FloatHandleEmpty(3).tag(sync=True)
unnormalize = Bool(False).tag(sync=True)

last_live_time = Float(0).tag(sync=True)
previews_temp_disable = Bool(False).tag(sync=True)
spinner = Bool(False).tag(sync=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -133,6 +138,7 @@ def flatten(self, add_data=True):
trend_lc : `~lightkurve.LightCurve`
The trend used to flatten the light curve.
"""
self.spinner = True
input_lc = self.dataset.selected_obj
if input_lc is None: # pragma: no cover
raise ValueError("no input dataset selected")
Expand All @@ -155,6 +161,7 @@ def flatten(self, add_data=True):
data = _data_with_reftime(self.app, output_lc)
self.add_results.add_results_from_plugin(data)

self.spinner = False
return output_lc, trend_lc

def _clear_marks(self):
Expand Down Expand Up @@ -182,9 +189,13 @@ def _toggle_marks(self, event={}):

@observe('dataset_selected',
'window_length', 'polyorder', 'break_tolerance',
'niters', 'sigma')
'niters', 'sigma', 'previews_temp_disable')
@skip_if_no_updates_since_last_active()
def _live_update(self, event={}):
if self.previews_temp_disable:
return

start = time()
try:
output_lc, trend_lc = self.flatten(add_data=False)
except Exception as e:
Expand All @@ -211,6 +222,10 @@ def _live_update(self, event={}):
for mark in flattened_marks.values():
mark.update_ty(times.value, output_flux)

self.last_live_time = np.round(time() - start, 2)
if self.last_live_time > 0.3:
self.previews_temp_disable = True

def vue_apply(self, *args, **kwargs):
try:
self.flatten(add_data=True)
Expand Down
19 changes: 19 additions & 0 deletions lcviz/plugins/flatten/flatten.vue
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@
<v-alert type="warning">Live preview is unnormalized, but flattening will normalize.</v-alert>
</v-row>

<v-alert v-if="previews_temp_disable && (show_live_preview || show_trend_preview)" type='warning' style="margin-left: -12px; margin-right: -12px">
Live-updating is temporarily disabled (last update took {{last_live_time}}s)
<v-row justify='center'>
<j-tooltip tooltipcontent='hide live trend and flattened previews (can be re-enabled from the settings section in the plugin).' span_style="width: 100%">
<v-btn style='width: 100%' @click="() => {show_live_preview = false; show_trend_preview = false}">
disable previews
</v-btn>
</j-tooltip>
</v-row>
<v-row justify='center'>
<j-tooltip tooltipcontent='manually update live-previews based on current plugin inputs.' span_style="width: 100%">
<v-btn style='width: 100%' @click="previews_temp_disable = false">
update preview
</v-btn>
</j-tooltip>
</v-row>
</v-alert>

<plugin-add-results
:label.sync="results_label"
:label_default="results_label_default"
Expand All @@ -150,6 +168,7 @@
action_label="Flatten"
action_tooltip="Flatten data"
:action_disabled="flatten_err.length > 0"
:action_spinner="spinner"
@click:action="apply"
></plugin-add-results>

Expand Down

0 comments on commit 989e282

Please sign in to comment.