Skip to content

Commit

Permalink
don't crash if line analysis fails in specutils
Browse files Browse the repository at this point in the history
* mosviz.test_parsers was failing with an interpolation error on initial load
  • Loading branch information
kecnry committed Jul 14, 2023
1 parent a0a7635 commit 46ce9e4
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions jdaviz/configs/specviz/plugins/line_analysis/line_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
SpectralMarksChangedMessage,
LineIdentifyMessage,
RedshiftMessage,
GlobalDisplayUnitChanged)
GlobalDisplayUnitChanged,
SnackbarMessage)
from jdaviz.core.marks import (LineAnalysisContinuum,
LineAnalysisContinuumCenter,
LineAnalysisContinuumLeft,
Expand Down Expand Up @@ -457,10 +458,20 @@ def _uncertainty(result):
flux=spec_subtracted.flux,
uncertainty=spec_subtracted.uncertainty)

raw_result = analysis.line_flux(freq_spec)
try:
raw_result = analysis.line_flux(freq_spec)
except ValueError as e:
# can happen if interpolation out-of-bounds or any error from specutils
# let's avoid the whole app crashing and instead expose the error to the
# user
self.hub.broadcast(SnackbarMessage(
f"failed to calculate line analysis statistics: {e}", sender=self,
color="warning"))
self.update_results(None)
return

# When flux is equivalent to Jy, lineflux result should be shown in W/m2
if flux_unit.is_equivalent(u.Jy/u.sr):

final_unit = u.Unit('W/(m2 sr)')
else:
final_unit = u.Unit('W/m2')
Expand All @@ -479,7 +490,17 @@ def _uncertainty(result):
equivalencies=u.spectral()),
flux=spec_subtracted.flux,
uncertainty=spec_subtracted.uncertainty)
raw_result = analysis.line_flux(wave_spec)
try:
raw_result = analysis.line_flux(wave_spec)
except ValueError as e:

Check warning on line 495 in jdaviz/configs/specviz/plugins/line_analysis/line_analysis.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/specviz/plugins/line_analysis/line_analysis.py#L495

Added line #L495 was not covered by tests
# can happen if interpolation out-of-bounds or any error from specutils
# let's avoid the whole app crashing and instead expose the error to the
# user
self.hub.broadcast(SnackbarMessage(

Check warning on line 499 in jdaviz/configs/specviz/plugins/line_analysis/line_analysis.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/specviz/plugins/line_analysis/line_analysis.py#L499

Added line #L499 was not covered by tests
f"failed to calculate line analysis statistics: {e}", sender=self,
color="warning"))
self.update_results(None)
return

Check warning on line 503 in jdaviz/configs/specviz/plugins/line_analysis/line_analysis.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/specviz/plugins/line_analysis/line_analysis.py#L502-L503

Added lines #L502 - L503 were not covered by tests
# When flux is equivalent to Jy, lineflux result should be shown in W/m2
if flux_unit.is_equivalent(u.Unit('W/(m2 m)'/u.sr)):
final_unit = u.Unit('W/(m2 sr)')
Expand Down

0 comments on commit 46ce9e4

Please sign in to comment.