Skip to content

Commit

Permalink
update to use skip_if_no_updates_since_last_active (#38)
Browse files Browse the repository at this point in the history
* update flattening plugin to use skip_if_no_updates_since_last_active
* update binning plugin to use skip_if_no_updates_since_last_active
* fix failing tests
  • Loading branch information
kecnry authored Oct 19, 2023
1 parent ad40f2a commit 93d5a14
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
44 changes: 30 additions & 14 deletions lcviz/plugins/binning/binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from jdaviz.core.events import (ViewerAddedMessage, ViewerRemovedMessage)
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import (PluginTemplateMixin,
DatasetSelectMixin, AddResultsMixin)
DatasetSelectMixin, AddResultsMixin,
skip_if_no_updates_since_last_active)
from jdaviz.core.user_api import PluginUserApi

from lcviz.events import EphemerisChangedMessage
Expand Down Expand Up @@ -125,15 +126,37 @@ def viewer_filter(viewer):

self.add_results.viewer.filters = [viewer_filter]

@observe('show_live_preview', 'is_active',
'dataset_selected', 'ephemeris_selected',
@observe('is_active', 'show_live_preview')
def _toggle_marks(self, event={}):
visible = self.show_live_preview and self.is_active

for viewer_id, mark in self.marks.items():
if not visible:
this_visible = False
elif self.ephemeris_selected == 'No ephemeris':
this_visible = True
else:
this_visible = viewer_id.split(':')[-1] == self.ephemeris_selected

mark.visible = this_visible

if visible and event.get('name', '') in ('is_active', 'show_live_preview'):
# then the marks themselves need to be updated
self._live_update(event)

@observe('dataset_selected', 'ephemeris_selected',
'n_bins')
@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 event.get('name', '') not in ('is_active', 'show_live_preview'):
# mark visibility hasn't been handled yet
self._toggle_marks()

try:
lc = self.bin(add_data=False)
except Exception:
Expand All @@ -155,23 +178,16 @@ def _live_update(self, event={}):

for viewer_id, mark in self.marks.items():
if self.ephemeris_selected == 'No ephemeris':
visible = True
# TODO: fix this to be general and not rely on ugly id
do_phase = viewer_id != 'lcviz-0'
else:
# TODO: try to fix flashing as traitlets update
visible = viewer_id.split(':')[-1] == self.ephemeris_selected
do_phase = False

if visible:
if do_phase:
mark.update_ty(times, lc.flux.value)
else:
mark.times = []
mark.update_xy(times, lc.flux.value)
if do_phase:
mark.update_ty(times, lc.flux.value)
else:
mark.clear()
mark.visible = visible
mark.times = []
mark.update_xy(times, lc.flux.value)

def _on_ephemeris_update(self, msg):
if not self.show_live_preview or not self.is_active:
Expand Down
32 changes: 22 additions & 10 deletions lcviz/plugins/flatten/flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from jdaviz.core.events import ViewerAddedMessage
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import (PluginTemplateMixin,
DatasetSelectMixin, AddResultsMixin)
DatasetSelectMixin, AddResultsMixin,
skip_if_no_updates_since_last_active)
from jdaviz.core.user_api import PluginUserApi

from lcviz.marks import LivePreviewTrend, LivePreviewFlattened
Expand Down Expand Up @@ -159,16 +160,25 @@ def _clear_marks(self):
mark.clear()
mark.visible = False

@observe('show_live_preview', 'is_active',
'dataset_selected',
@observe('is_active', 'show_live_preview')
def _toggle_marks(self, event={}):
visible = self.show_live_preview and self.is_active

trend_marks, flattened_marks = self.marks
for mark in trend_marks.values():
mark.visible = visible
for mark in flattened_marks.values():
mark.visible = visible

if visible and event.get('name') in ('is_active', 'show_live_preview'):
# then the marks themselves need to be updated
self._live_update(event)

@observe('dataset_selected',
'window_length', 'polyorder', 'break_tolerance',
'niters', 'sigma')
@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.flatten_err = ''
return

try:
output_lc, trend_lc = self.flatten(add_data=False)
except Exception as e:
Expand All @@ -177,6 +187,10 @@ def _live_update(self, event={}):
return
self.flatten_err = ''

if event.get('name') not in ('is_active', 'show_live_preview'):
# mark visibility hasn't been handled yet
self._toggle_marks(event)

if self.unnormalize:
output_flux = output_lc.flux.value
else:
Expand All @@ -188,10 +202,8 @@ def _live_update(self, event={}):
for mark in trend_marks.values():
# TODO: need to account for phasing
mark.update_ty(times.value, trend_lc.flux.value)
mark.visible = True
for mark in flattened_marks.values():
mark.update_ty(times.value, output_flux)
mark.visible = True

def vue_apply(self, *args, **kwargs):
try:
Expand Down
4 changes: 3 additions & 1 deletion lcviz/tests/test_plugin_flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def test_plugin_flatten(helper, light_curve_like_kepler_quarter):
# update polyorder (live-preview should re-appear and have changed from before)
f.polyorder = before_polyorder + 1
assert f._obj.flatten_err == ''
after_update = _get_marks_from_viewer(tv)[0].y
marks = _get_marks_from_viewer(tv)
assert len(marks) == 2
after_update = marks[0].y
assert not np.allclose(before_update, after_update)

orig_label = f.dataset.selected
Expand Down

0 comments on commit 93d5a14

Please sign in to comment.