Skip to content

Commit

Permalink
Binning: handle n_bins being empty or <= 0 (#51)
Browse files Browse the repository at this point in the history
* avoid traceback in preview when n_bins is invalid form validation shows error message for widget already
* disable "bin" button for invalid inputs
* test coverage
  • Loading branch information
kecnry authored Sep 22, 2023
1 parent 585a341 commit f81459a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lcviz/plugins/binning/binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Binning(PluginTemplateMixin, DatasetSelectMixin, EphemerisSelectMixin, Add
show_live_preview = Bool(True).tag(sync=True)

n_bins = IntHandleEmpty(100).tag(sync=True)
bin_enabled = Bool(True).tag(sync=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -130,9 +131,18 @@ def viewer_filter(viewer):
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

lc = self.bin(add_data=False)
try:
lc = self.bin(add_data=False)
except Exception:
self._clear_marks()
self.bin_enabled = False
return
else:
self.bin_enabled = True

# TODO: remove the need for this (inconsistent quantity vs value setting in lc object)
lc_time = getattr(lc.time, 'value', lc.time)

Expand Down Expand Up @@ -173,6 +183,10 @@ def _on_ephemeris_update(self, msg):
self._live_update()

def bin(self, add_data=True):

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

input_lc = self.input_lc

lc = input_lc.bin(time_bin_size=(input_lc.time[-1]-input_lc.time[0]).value/self.n_bins)
Expand Down
1 change: 1 addition & 0 deletions lcviz/plugins/binning/binning.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
:add_to_viewer_selected.sync="add_to_viewer_selected"
action_label="Bin"
action_tooltip="Bin data"
:action_disabled="!bin_enabled"
@click:action="apply"
></plugin-add-results>

Expand Down
18 changes: 18 additions & 0 deletions lcviz/tests/test_plugin_binning.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from lcviz.marks import LivePreviewBinning


Expand Down Expand Up @@ -47,3 +49,19 @@ def test_plugin_binning(helper, light_curve_like_kepler_quarter):
ephem.period = 1.222

b.bin(add_data=True)

# setting to invalid n_bins will raise error
b.n_bins = 0
assert b._obj.bin_enabled is False
with pytest.raises(ValueError):
b.bin(add_data=False)

# the enabled state of the button should work with or without live previews enabled
b.show_live_preview = False
assert len(_get_marks_from_viewer(tv)) == 0
assert len(_get_marks_from_viewer(pv)) == 0
assert b._obj.bin_enabled is False
b.n_bins = 1
assert b._obj.bin_enabled is True
b.n_bins = ''
assert b._obj.bin_enabled is False

0 comments on commit f81459a

Please sign in to comment.