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

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

pllim
Copy link
Contributor

@pllim pllim commented Sep 30, 2024

Description

This pull request is to unit conversion when equivalency needed breaks contours in cubeviz. Unskips a test added in #3204 .

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

Change log entry

  • Is a change log needed? If yes, is it added to CHANGES.rst? If you want to avoid merge conflicts,
    list the proposed change log here for review and add to CHANGES.rst before merge. If no, maintainer
    should add a no-changelog-entry-needed label.

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.

  • Are two approvals required? Branch protection rule does not check for the second approval. If a second approval is not necessary, please apply the trivial label.
  • Do the proposed changes actually accomplish desired goals? Also manually run the affected example notebooks, if necessary.
  • Do the proposed changes follow the STScI Style Guides?
  • Are tests added/updated as required? If so, do they follow the STScI Style Guides?
  • Are docs added/updated as required? If so, do they follow the STScI Style Guides?
  • Did the CI pass? If not, are the failures related?
  • Is a milestone set? Set this to bugfix milestone if this is a bug fix and needs to be released ASAP; otherwise, set this to the next major release milestone. Bugfix milestone also needs an accompanying backport label.
  • After merge, any internal documentations need updating (e.g., JIRA, Innerspace)? 🐱

@pllim pllim added bug Something isn't working no-changelog-entry-needed changelog bot directive labels Sep 30, 2024
@pllim pllim added this to the 4.0 milestone Sep 30, 2024
@github-actions github-actions bot added cubeviz plugin Label for plugins common to multiple configurations labels Sep 30, 2024
@pllim
Copy link
Contributor Author

pllim commented Sep 30, 2024

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

to make sure it still fails before I fix it.
@pllim
Copy link
Contributor Author

pllim commented Oct 1, 2024

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)

@pllim pllim marked this pull request as ready for review October 1, 2024 22:22
Copy link

codecov bot commented Oct 1, 2024

Codecov Report

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

Project coverage is 88.45%. Comparing base (fc88465) to head (45aa120).

Files with missing lines Patch % Lines
...specviz/plugins/unit_conversion/unit_conversion.py 85.71% 2 Missing ⚠️
...imviz/plugins/aper_phot_simple/aper_phot_simple.py 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3207      +/-   ##
==========================================
- Coverage   88.46%   88.45%   -0.01%     
==========================================
  Files         125      125              
  Lines       18677    18699      +22     
==========================================
+ Hits        16522    16541      +19     
- Misses       2155     2158       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor Author

@pllim pllim left a comment

Choose a reason for hiding this comment

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

In a nutshell, the way all this stuff is set up, it goes through the glue unit conversion machinery that has no access to app data (like cube slice or spectral axis). So the least painful way I can think of is to define the equivalencies (again) where things are broken (as far as this JIRA ticket is concerned). For the background unit conversion, I cannot find any existing equivalencies that would work on the / pix2 stuff so I put in a hack to remove it. Ideas welcome.

if len(values) == spectral_values.size:
eqv = u.spectral_density(spectral_values)
else:
eqv = u.spectral_density(spec.spectral_axis[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually not sure if grabbing [0] is entirely correct but I also don't know what is really happening when values are only an array of 7 elements. I think that is the downsampled version from the viewer but really hard to tell for sure.

# FIXME: Kinda hacky, better solution welcomed.
# Basically we want to forget about PIX2 and do normal conversion.
# Since traitlet does not keep unit, we do not need to reapply PIX2.
if "pix2" in prev_display_unit and "pix2" in self.display_unit:
Copy link
Contributor

@cshanahan1 cshanahan1 Oct 2, 2024

Choose a reason for hiding this comment

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

can you check if this is the case for steradian as well? i believe you cant convert flux/sr <> flux/sr if there is a u.spectral_density equivalency needed.

you could also leave a note here to pass this through utils.flux_conversion eventually - i'm working on generalizing this function to handle cases like this and it might simplify this code if we want to swap it later on

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think steradian is already covered by existing equivalencies combo because steradian is a recognized physical unit in astronomy, whereas pixel is very much tied to instrumentation and not well defined in astropy.units. CI passes without such treatment for u.sr. Unless you mean somehow the existing test suite does not cover the steradian case already?

def _eqv_pixar_sr(pixar_sr):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see the test I unskipped is about Cubeviz unit conversion plugin only. And this code is inside aperture photometry. I can think of how I can expand your test case to go through this line as well tomorrow, maybe. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cubeviz no-changelog-entry-needed changelog bot directive plugin Label for plugins common to multiple configurations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants