Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line Analysis Plugin now listens to unit conversion changes #3107

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions jdaviz/configs/specviz/plugins/line_analysis/line_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ def _uncertainty(result):
if getattr(result, 'uncertainty', None) is not None:
# we'll keep the uncertainty and result in the same unit (so
# we only have to show the unit at the end)
if np.isnan(result.uncertainty.value) or np.isinf(result.uncertainty.value):
if (np.any(np.isnan(result.uncertainty.value)) or
np.any(np.isinf(result.uncertainty.value))):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use np.all(np.isfinite()) combo?

return ''
return str(result.uncertainty.to_value(result.unit))
else:
Expand All @@ -287,14 +288,15 @@ def _uncertainty(result):
# TODO: update specutils to allow ALL analysis to take regions and continuum so we
# don't need these if statements
if function == "Line Flux":
flux_unit = spec_subtracted.flux.unit
flux_unit = self.app._get_display_unit('flux')
if flux_unit == u.dimensionless_unscaled:
add_flux = True
flux_unit = u.Unit(self.spectrum_viewer.state.y_display_unit)
else:
add_flux = False
# If the flux unit is equivalent to Jy, or Jy per spaxel for Cubeviz,
# enforce integration in frequency space
flux_unit = u.Unit(flux_unit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be useful if get_display_unit had an option to cast to the unit itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added to #3112

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although that is triggering a few test failures (sigh), so we may need to postpone that work, for now maybe it makes sense to just case to unit immediately so if/when we do implement that it will be easy to notice and remove the duplication

flux_unit = u.Unit(self.app._get_display_unit('flux'))

if (flux_unit.is_equivalent(u.Jy) or
flux_unit.is_equivalent(u.Jy/u.sr)):
# Perform integration in frequency space
Expand All @@ -319,16 +321,13 @@ def _uncertainty(result):
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')

temp_result = raw_result.to(final_unit)
temp_result = np.mean(raw_result.to(
flux_unit,
equivalencies=u.spectral_density(freq_spec.spectral_axis)))
if getattr(raw_result, 'uncertainty', None) is not None:
temp_result.uncertainty = raw_result.uncertainty.to(final_unit)

temp_result.uncertainty = np.mean(raw_result.uncertainty.to(
flux_unit,
equivalencies=u.spectral_density(freq_spec.spectral_axis)))
# If the flux unit is instead equivalent to power density
# (Jy, but defined in wavelength), enforce integration in wavelength space
elif (flux_unit.is_equivalent(u.Unit('W/(m2 m)')) or
Expand All @@ -353,15 +352,14 @@ def _uncertainty(result):
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.Unit('W/(m2 m)'/u.sr)):
final_unit = u.Unit('W/(m2 sr)')
else:
final_unit = u.Unit('W/m2')

temp_result = raw_result.to(final_unit)
temp_result = np.mean(raw_result.to(
flux_unit,
equivalencies=u.spectral_density(wave_spec.spectral_axis)))
if getattr(raw_result, 'uncertainty', None) is not None:
temp_result.uncertainty = raw_result.uncertainty.to(final_unit)
temp_result.uncertainty = np.mean(raw_result.uncertainty.to(
flux_unit,
equivalencies=u.spectral_density(wave_spec.spectral_axis)))

# Otherwise, just rely on the default specutils line_flux result
else:
Expand Down
Loading