spacetelescope / jdaviz

JWST astronomical data analysis tools in the Jupyter platform
https://jdaviz.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
138 stars 73 forks source link

Fix "Unit conversion when equivalency needed breaks contours in cubeviz" #3207

Open pllim opened 4 days ago

pllim commented 4 days ago

Description

This pull request is to unit conversion when equivalency needed breaks contours in cubeviz. Unskips a test added in https://github.com/spacetelescope/jdaviz/pull/3204 .

Probably no need for change log since this is patching unreleased feature, I assume.

Change log entry

Checklist for package maintainer(s)

This checklist is meant to remind the package maintainer(s) who will review this pull request of some common things to look for. This list is not exhaustive.

pllim commented 4 days ago

Somehow this one test makes 2 very different tracebacks. Funky.

x = array([0., 0., 0., 0., 0., 0., 0.])

    def f_la_from_f_nu(x):
>       return x / (wav.to_value(si.AA, spectral()) ** 2 / c_Aps)
E       ValueError: operands could not be broadcast together with shapes (7,) (3001,)
E       astropy.units.core.UnitConversionError: 'erg / (Hz s cm2 pix2)' and 'erg / (Angstrom s cm2 pix2)' are not convertible
pllim commented 3 days ago

The original exception reported in JIRA also produces ValueError: operands could not be broadcast together with shapes (7,) (3438,) (one of the exceptions from unskipped test)

codecov[bot] commented 3 days ago

Codecov Report

Attention: Patch coverage is 91.66667% with 3 lines in your changes missing coverage. Please review.

Project coverage is 88.56%. Comparing base (21e9b87) to head (f6bdc8a).

Files with missing lines Patch % Lines
...specviz/plugins/unit_conversion/unit_conversion.py 85.71% 2 Missing :warning:
...imviz/plugins/aper_phot_simple/aper_phot_simple.py 92.30% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #3207 +/- ## ========================================== + Coverage 88.51% 88.56% +0.04% ========================================== Files 125 125 Lines 18694 18719 +25 ========================================== + Hits 16547 16578 +31 + Misses 2147 2141 -6 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

pllim commented 11 hours ago

@cshanahan1 , I tried to explain at https://github.com/spacetelescope/jdaviz/pull/3207#pullrequestreview-2341568057 but in case that is not clear enough, when one sets uc_plg.flux_unit = flux_unit, glue invokes this:

https://github.com/spacetelescope/jdaviz/blob/21e9b87310bfa366c38ddfc944202b41fc80ffff/jdaviz/app.py#L94

You can see at https://github.com/glue-viz/glue/blob/main/glue/core/units.py that the converter has no access to app state or data. Therefore, the fix I used is to use https://docs.astropy.org/en/stable/api/astropy.units.set_enabled_equivalencies.html#astropy.units.set_enabled_equivalencies to pass in the equivalencies to global Python state so Jdaviz unit conversion can pick them up. It is used as context manager to hopefully only enable those equivalencies when needed.

Does this make sense?

pllim commented 11 hours ago

Ignore devdeps failure, it is unrelated.