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

Suppress Units warnings from Line Analysis plugin #1648

Merged
merged 4 commits into from
Sep 23, 2022

Conversation

rosteen
Copy link
Collaborator

@rosteen rosteen commented Sep 13, 2022

Suppresses the W/m2/m warning from astropy.units when using the line analysis plugin.

@rosteen rosteen added the plugin Label for plugins common to multiple configurations label Sep 13, 2022
@rosteen rosteen added this to the 2.11 milestone Sep 13, 2022
Copy link
Member

@kecnry kecnry left a comment

Choose a reason for hiding this comment

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

We may want to move the _unit_ignore_warnings to a higher level in the code if we ever need to re-use it elsewhere, but here for now makes sense to me.

@codecov
Copy link

codecov bot commented Sep 13, 2022

Codecov Report

Base: 86.31% // Head: 86.76% // Increases project coverage by +0.45% 🎉

Coverage data is based on head (69c9cd6) compared to base (78b5d35).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1648      +/-   ##
==========================================
+ Coverage   86.31%   86.76%   +0.45%     
==========================================
  Files          95       95              
  Lines        9588     9652      +64     
==========================================
+ Hits         8276     8375      +99     
+ Misses       1312     1277      -35     
Impacted Files Coverage Δ
...igs/specviz/plugins/line_analysis/line_analysis.py 98.03% <100.00%> (ø)
jdaviz/configs/imviz/plugins/parsers.py 100.00% <0.00%> (ø)
...daviz/configs/default/plugins/collapse/collapse.py 100.00% <0.00%> (ø)
jdaviz/app.py 91.80% <0.00%> (+0.01%) ⬆️
jdaviz/configs/imviz/helper.py 96.66% <0.00%> (+0.04%) ⬆️
jdaviz/configs/imviz/plugins/catalogs/catalogs.py 95.65% <0.00%> (+0.06%) ⬆️
jdaviz/configs/default/plugins/viewers.py 95.65% <0.00%> (+0.11%) ⬆️
jdaviz/configs/cubeviz/helper.py 96.42% <0.00%> (+0.13%) ⬆️
jdaviz/configs/cubeviz/plugins/slice/slice.py 96.90% <0.00%> (+0.16%) ⬆️
...default/plugins/metadata_viewer/metadata_viewer.py 94.73% <0.00%> (+0.23%) ⬆️
... and 8 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@@ -276,6 +276,11 @@ def _on_identified_line_changed(self, msg):
# in which case we'll default to the identified line
self.selected_line = self.identified_line

def _unit_ignore_warnings(self, unit):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to be more specific here? This blanket ignore may hide real bugs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we do not even need this.

@@ -446,7 +452,7 @@ def _uncertainty(result):
raw_result = analysis.line_flux(wave_spec)
# 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')
final_unit = self._unit_ignore_warnings('W/m2/sr')
Copy link
Contributor

Choose a reason for hiding this comment

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

We have full control of how the unit string is written here. Why not just change the string so it stops emitting warning?

Suggested change
final_unit = self._unit_ignore_warnings('W/m2/sr')
final_unit = u.Unit('W / (m2 sr)')

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Huh, the original ticket said that warnings were being thrown from our use of "W / m2 / m", and I totally didn't even consider that the warning I was actually seeing was for "W / m2 / sr" which, as you said, can be rewritten. Let me think about this a little more, your point that we don't even need the filtering might be correct.

Copy link
Member

Choose a reason for hiding this comment

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

W / m2 / sr may still be the preferred format for any user-facing representation of the unit... but if it's all internal, then that would definitely be the "easier" fix.

Copy link
Contributor

Choose a reason for hiding this comment

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

p.s. We should not encourage users to write unit in a format that emits astropy warnings. 😉

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was going to make the same point Kyle, this is really about how it's displayed to the user, and it may be that the desired format is the one with more slashes that naturally parses to "Watts per square meter per steradian". @PatrickOgle can you chime in on this?

Copy link
Contributor

Choose a reason for hiding this comment

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

WARNING: UnitsWarning: 'W/m2/sr' contains multiple slashes, which is discouraged by the FITS standard 🤷

@@ -425,7 +430,8 @@ def _uncertainty(result):
raw_result = analysis.line_flux(freq_spec)
# 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')

final_unit = self._unit_ignore_warnings('W/m2/sr')
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
final_unit = self._unit_ignore_warnings('W/m2/sr')
final_unit = u.Unit('W / (m2 sr)')

@@ -276,6 +276,11 @@ def _on_identified_line_changed(self, msg):
# in which case we'll default to the identified line
self.selected_line = self.identified_line

def _unit_ignore_warnings(self, unit):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we do not even need this.

@rosteen
Copy link
Collaborator Author

rosteen commented Sep 22, 2022

@pllim I decided to remove the function and reformat things in a way that isn't "discouraged" by astropy, as you suggested.

@rosteen rosteen requested a review from kecnry September 23, 2022 12:30
Copy link
Collaborator

@duytnguyendtn duytnguyendtn left a comment

Choose a reason for hiding this comment

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

Thanks for cleaning this up! Tested in Specviz and warnings are absent, as expected

Copy link
Member

@kecnry kecnry left a comment

Choose a reason for hiding this comment

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

Might want to update the change log to better reflect the current solution. But I agree this is cleaner since it's only internal code and not exposed to the user. Other than the change log, I just have one small question, but then hopefully we can get this in!

if flux_unit.is_equivalent(u.Unit('W/m2/m'/u.sr)):
final_unit = u.Unit('W/m2/sr')
if flux_unit.is_equivalent(u.Unit('W/(m2 m)'/u.sr)):
final_unit = u.Unit('W/(m2 sr)')
Copy link
Member

Choose a reason for hiding this comment

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

does final_unit affect what is exposed to the user or is that logic handled elsewhere (do we need to override to show W/m2/sr in the UI)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is how it will show up in the UI, but I didn't get any objections from POs. I figure get rid of the warnings this way for now, and if down the line people really hate that format I can bring back _unit_ignore_warnings.

@rosteen rosteen merged commit 1730dfe into spacetelescope:main Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin Label for plugins common to multiple configurations Ready for final review specviz
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants