Skip to content

Commit

Permalink
smarter default behavior when deleting selected entry
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Mar 28, 2022
1 parent a10cfad commit feadd75
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _get_region_from_subset(self, subset):
@observe('subset_selected')
def _subset_selected_changed(self, event={}):
subset_selected = event.get('new', self.subset_selected)
if self._selected_data is None:
if self._selected_data is None or subset_selected == '':
self.reset_results()
return

Expand Down
7 changes: 3 additions & 4 deletions jdaviz/configs/imviz/tests/test_simple_aper_phot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def test_plugin_wcs_dithered(self):
with pytest.raises(ValueError):
# will raise an error and revert to first entry
phot_plugin.subset_selected = 'no_such_subset'
assert phot_plugin.subset_selected == phot_plugin.subset.labels[0]
assert phot_plugin.subset_selected == ''
phot_plugin.subset_selected = phot_plugin.subset.labels[0]
assert_allclose(phot_plugin.background_value, 0)
phot_plugin.vue_do_aper_phot()
assert not phot_plugin.result_available
Expand All @@ -35,9 +36,7 @@ def test_plugin_wcs_dithered(self):
assert phot_plugin.current_plot_type == 'Radial Profile' # Software default

phot_plugin.data_selected = 'has_wcs_1[SCI,1]'
with pytest.raises(ValueError):
phot_plugin.subset_selected = 'no_such_subset'
assert phot_plugin.subset_selected == phot_plugin.subset.labels[0]
phot_plugin.subset_selected = phot_plugin.subset.labels[0]
with pytest.raises(ValueError):
phot_plugin.bg_subset_selected = 'no_such_subset'
assert phot_plugin.bg_subset_selected == 'Manual'
Expand Down
16 changes: 11 additions & 5 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ def _subset_type(subset):
else:
return 'spectral'

def _apply_default_selection(self):
# default to the default_text, if available, otherwise empty
if self._default_text:
self.selected = self._default_text
else:
self.selected = ''

def _subset_to_dict(self, subset):
# find layer artist in default spectrum-viewer
for viewer in self.viewers:
Expand All @@ -248,8 +255,8 @@ def _delete_subset(self, subset):
# NOTE: calling .remove will not trigger traitlet update
self.items = [s for s in self.items
if s['label'] != subset.label]
if self.selected not in self.labels and len(self.labels):
self.selected = self.labels[0]
if self.selected not in self.labels:
self._apply_default_selection()

def _update_subset(self, subset, attribute=None):
if self._allowed_type is not None and self._subset_type(subset) != self._allowed_type:
Expand Down Expand Up @@ -278,9 +285,8 @@ def _update_subset(self, subset, attribute=None):
self._update_has_subregions()

def _selected_changed(self, event):
if event['new'] not in self.labels:
if len(self.labels):
self.selected = self.labels[0]
if event['new'] not in self.labels + ['']:
self._apply_default_selection()
raise ValueError(f"{event['new']} not one of {self.labels}")
self._clear_cache("selected_obj", "selected_item")
self._update_has_subregions()
Expand Down

0 comments on commit feadd75

Please sign in to comment.