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

model fitting: ensure specutils has access to equivs & set targ solid angle #3343

Merged

Conversation

gibsongreen
Copy link
Contributor

@gibsongreen gibsongreen commented Dec 9, 2024

This draft PR resolves:

  1. UnitConversionError in Model Fitting plugin, specutils spec.with_flux_unit did not have access to pix2 custom unit equivalencies

To reproduce on main:

uri ='https://data.science.stsci.edu/redirect/JWST/jwst-data_analysis_tools/IFU_cube_continuum_fit/NGC4151_Hband.fits'

model = cubeviz.plugins['Model Fitting']
model.cube_fit = True
model.create_model_component()
model.calculate_fit()
  1. TypeError: unsupported operand type(s) for *: 'CompositeUnit' and 'NoneType'. When toggling 'Cube Fit' in Model Fitting plugin, solid_angle_in_targ was not being set after targ_units was update to include a solid angle unit. Setting 'targ_units' triggered subsequent indirect conversion logic. By setting the solid_angle_in_targ we follow the same logic route and ensure we maintain output units in surface brightness.

To reproduce:

# example notebook cube
uri = "mast:JWST/product/jw02732-o004_t004_miri_ch1-shortmediumlong_s3d.fits"

uc = cubeviz.plugins['Unit Conversion']
uc.flux_unit = 'erg / (Angstrom s cm2)'

model = cubeviz.plugins['Model Fitting']
model.cube_fit = True
model.create_model_component()
model.calculate_fit()

Description

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)?

@github-actions github-actions bot added the plugin Label for plugins common to multiple configurations label Dec 9, 2024
@gibsongreen gibsongreen added this to the 4.1 milestone Dec 9, 2024
@gibsongreen gibsongreen added bug Something isn't working cubeviz labels Dec 9, 2024
Copy link

codecov bot commented Dec 9, 2024

Codecov Report

Attention: Patch coverage is 85.71429% with 2 lines in your changes missing coverage. Please review.

Project coverage is 88.78%. Comparing base (6c946d3) to head (0a70c5f).
Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
...igs/default/plugins/model_fitting/model_fitting.py 84.61% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3343      +/-   ##
==========================================
- Coverage   88.80%   88.78%   -0.03%     
==========================================
  Files         125      125              
  Lines       19137    19174      +37     
==========================================
+ Hits        16995    17023      +28     
- Misses       2142     2151       +9     

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

@gibsongreen gibsongreen changed the title model fitting: ensure specutils has access to custom equivs and update targ solid angle model fitting: ensure specutils has access to equivs & set targ solid angle Dec 11, 2024
@gibsongreen gibsongreen marked this pull request as ready for review December 11, 2024 22:59
@gibsongreen gibsongreen added the trivial Only needs one approval instead of two label Dec 12, 2024
@camipacifici
Copy link
Contributor

Works for me, thank you!

Copy link
Contributor

@cshanahan1 cshanahan1 left a comment

Choose a reason for hiding this comment

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

Good catch, I didn't clock this as a unit conversion when I was working on unit conversions for model fitting!

I do wonder if this will ever encounter one of the special cases where a spectral_density AND a pix2 equivalency are needed, and if with_flux_unit will handle that. Have you tested anything like that at all?

@gibsongreen
Copy link
Contributor Author

I do wonder if this will ever encounter one of the special cases where a spectral_density AND a pix2 equivalency are needed, and if with_flux_unit will handle that. Have you tested anything like that at all?

The all_flux_unit_conversion_equivs calls _eqv_flux_to_sb_pixel which returns the PIX2 equivalency, and by passing pixar_sr if available and the spectral_axis to all_flux_unit_conversion_equivs in this call, should ensure that that special case is handled.

The manga cube in issue #3342 is in the 1e-17 (Erg / Angstrom s cm2) unit and this does get the PIX2 unit applied. I did make sure that cube was working so we could resolve the issue, but adding a test here for this specific edge base should be added here.

Comment on lines +991 to +1002
pix2_in_flux = 'pix2' in spec.flux.unit.to_string()
pix2_in_sb = 'pix2' in sb_unit
# Handle various cases when PIX2 angle unit is present in the conversion
if pix2_in_flux and pix2_in_sb:
spec = spec.with_flux_unit(u.Unit(spec.flux.unit)*PIX2, equivalencies=equivalencies) # noqa
spec = spec.with_flux_unit(u.Unit(sb_unit)*PIX2, equivalencies=equivalencies)
elif pix2_in_flux:
spec = spec.with_flux_unit(u.Unit(spec.flux.unit) * PIX2, equivalencies=equivalencies) # noqa
elif pix2_in_sb:
spec = spec.with_flux_unit(u.Unit(spec.flux.unit) / PIX2, equivalencies=equivalencies) # noqa

spec = spec.with_flux_unit(sb_unit, equivalencies=equivalencies)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe in a small effort before flux_conversion and flux_conversion_general, these both right now return the converted values as an array or spectral_axis as a Quantity object. It'd be worth adding the ability to return the spec Spectrum1D object as well and then we can update this logic block.

@cshanahan1 if you test with this cube:
cubeviz.load_data(f"https://data.sdss.org/sas/dr17/manga/spectro/redux/v3_1_1/7443/stack/manga-7443-12703-LOGCUBE.fits.gz")

and convert the flux_unit to Jy, this no longer throughs a UnitConversionError for the case you brought up. Test coverage is there now too!

This PR is a blocker for a notebook we need merged once 4.1 is released next week, so I will create a ticket and start working on the Spectrum1D compatibility when converting flux.

@gibsongreen gibsongreen merged commit a0d77f5 into spacetelescope:main Dec 19, 2024
17 of 19 checks passed
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 plugin Label for plugins common to multiple configurations trivial Only needs one approval instead of two
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants