Skip to content

Commit

Permalink
add offending ranges to error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Jan 3, 2023
1 parent 700608c commit f26ac6f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,8 @@ def calculate_fit(self, add_data=True):
residuals (if ``residuals_calculate`` is set to ``True``)
"""
if not self.spectral_subset_valid:
raise ValueError("spectral subset is outside data range")
valid, spec_range, subset_range = self._check_dataset_spectral_subset_valid(return_ranges=True) # noqa
raise ValueError(f"spectral subset '{self.spectral_subset.selected}' {subset_range} is outside data range of '{self.dataset.selected}' {spec_range}") # noqa

if self.cube_fit:
return self._fit_model_to_cube(add_data=add_data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def test_invalid_subset(specviz_helper, spectrum1d):
plugin.spectral_subset = 'Subset 1'
assert not plugin._obj.spectral_subset_valid

with pytest.raises(ValueError, match='spectral subset is outside data range'):
with pytest.raises(ValueError, match=r"spectral subset 'Subset 1' \(5000.0, 5888.888888888889\) is outside data range of 'right_spectrum' \(6000.0, 8000.0\)"): # noqa
plugin.calculate_fit()

plugin.dataset = 'left_spectrum'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ def get_results(self):
# is closed

if not self.spectral_subset_valid:
raise ValueError("spectral subset is outside data range")
valid, spec_range, subset_range = self._check_dataset_spectral_subset_valid(return_ranges=True) # noqa
raise ValueError(f"spectral subset '{self.spectral_subset.selected}' {subset_range} is outside data range of '{self.dataset.selected}' {spec_range}") # noqa

self._calculate_statistics(ignore_plugin_closed=True)
return self.results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def test_invalid_subset(specviz_helper, spectrum1d):
plugin.spectral_subset = 'Subset 1'
assert not plugin._obj.spectral_subset_valid

with pytest.raises(ValueError, match='spectral subset is outside data range'):
with pytest.raises(ValueError, match=r"spectral subset 'Subset 1' \(5000.0, 5888.888888888889\) is outside data range of 'right_spectrum' \(6000.0, 8000.0\)"): # noqa
plugin.get_results()

plugin.dataset = 'left_spectrum'
Expand Down
9 changes: 7 additions & 2 deletions jdaviz/core/template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,15 +1071,20 @@ class DatasetSpectralSubsetValidMixin(VuetifyTemplate, HubListener):
spectral_subset_valid = Bool(True).tag(sync=True)

@observe("dataset_selected", "spectral_subset_selected")
def _check_dataset_spectral_subset_valid(self, event={}):
def _check_dataset_spectral_subset_valid(self, event={}, return_ranges=False):
if self.spectral_subset_selected == "Entire Spectrum":
self.spectral_subset_valid = True
else:
spec = self.dataset.selected_obj
spec_min, spec_max = np.nanmin(spec.spectral_axis), np.nanmax(spec.spectral_axis)
subset_min, subset_max = self.spectral_subset.selected_min_max(spec)
self.spectral_subset_valid = bool(subset_min < spec_max and subset_max > spec_min)
return self.spectral_subset_valid
if return_ranges:
return (self.spectral_subset_valid,
(spec_min.value, spec_max.value),
(subset_min.value, subset_max.value))
else:
return self.spectral_subset_valid


class ViewerSelect(SelectPluginComponent):
Expand Down

0 comments on commit f26ac6f

Please sign in to comment.