Skip to content
New issue

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

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

Already on GitHub? # to your account

implement unit conversion in specviz2d #3253

Merged
merged 32 commits into from
Jan 30, 2025

Conversation

gibsongreen
Copy link
Contributor

@gibsongreen gibsongreen commented Oct 24, 2024

Description

This pull request is to address implementing the Unit Conversion plugin in Specviz2d.

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

Copy link

codecov bot commented Dec 19, 2024

Codecov Report

Attention: Patch coverage is 87.50000% with 10 lines in your changes missing coverage. Please review.

Project coverage is 87.53%. Comparing base (74e537f) to head (d2411c1).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
jdaviz/configs/mosviz/plugins/tools.py 60.00% 6 Missing ⚠️
...plugins/spectral_extraction/spectral_extraction.py 70.00% 3 Missing ⚠️
jdaviz/core/marks.py 92.30% 1 Missing ⚠️

❌ Your patch status has failed because the patch coverage (87.50%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3253      +/-   ##
==========================================
+ Coverage   87.51%   87.53%   +0.02%     
==========================================
  Files         128      128              
  Lines       19901    19957      +56     
==========================================
+ Hits        17416    17469      +53     
- Misses       2485     2488       +3     

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

@gibsongreen gibsongreen marked this pull request as ready for review December 19, 2024 15:37
self.has_angle = False
self.has_sb = False
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't append an angle unit in Specviz right now, see the screenshot when the native unit is Jy. This hides both from displaying in the UI and unexposes them from the API.

Screenshot 2024-12-19 at 4 40 57 PM

jdaviz/conftest.py Outdated Show resolved Hide resolved
CHANGES.rst Outdated Show resolved Hide resolved
jdaviz/configs/default/plugins/markers/markers.py Outdated Show resolved Hide resolved
spec = self.dataset.selected_spectrum
# Needs Attention:
# should conversion occur before or after call to _apply_subset_masks?
if spec.flux.unit.to_string != self.app._get_display_unit('flux'):
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this a method? Is it cheaper to compare units instead of casting to string?

Suggested change
if spec.flux.unit.to_string != self.app._get_display_unit('flux'):
if spec.flux.unit.to_string() != self.app._get_display_unit('flux'):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Is it worth converting the display units to astropy.Unit or keep to string comparison?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure, probably fine as is for now

Comment on lines +441 to +442
self._dict['spectral_axis'] = wave.value
self._dict['spectral_axis:unit'] = wave.unit.to_string()
Copy link
Member

Choose a reason for hiding this comment

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

where were these handled before (I don't see them in the else 🤔)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

X-axis conversions were not previously handled in Specviz2d, so they were previously always using the native units and value when mousing over

Comment on lines +226 to +227
# Note: spectra2d Wave loaded in meters, but we respect one spectral unit, so the meters in
# converted to Angstrom (the spectra1d spectral unit).
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain this a little more? Is it that a previous data entry was in angstroms or just that that is the default spectral display unit?

Suggested change
# Note: spectra2d Wave loaded in meters, but we respect one spectral unit, so the meters in
# converted to Angstrom (the spectra1d spectral unit).
# Note: spectra2d Wave loaded in meters, but we respect one spectral unit, so the meters is
# converted to Angstrom (the spectra1d spectral unit).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the loaded 1D and 2D spectra have different native spectral units, with one in meters and one in angstroms and app-wide we're respecting one choice for spectral unit.

@rosteen rosteen modified the milestones: 4.1, 4.2 Dec 23, 2024
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.

When testing this with the specviz2d example notebook, the option to change flux unit disappears when you load the data. The screen shots below show an empty specviz instance, with the 'Flux' option appearing under the unit conversion plugin, and then after loading data when that dropdown disappears

Screenshot 2024-12-23 at 1 59 21 PM Screenshot 2024-12-23 at 2 08 54 PM

@cshanahan1
Copy link
Contributor

What is included in the scope here for surface brightness logic, including coercing flux units to surface brightness, handling square pixel, and toggling the spectral axis? (Are images of 2d spectra even typically or commonly in surface brightness units?)

@gibsongreen
Copy link
Contributor Author

gibsongreen commented Dec 23, 2024

What is included in the scope here for surface brightness logic, including coercing flux units to surface brightness, handling square pixel, and toggling the spectral axis? (Are images of 2d spectra even typically or commonly in surface brightness units?)

  • There will be no coercing for pix^2 in Specviz2d, but I think it will be applicable for the image viewers in Mosviz and Imviz.
  • We only apply the scale factor in Cubeviz since it is stored in the metadata when we extract the cube using the plugin (so no translations here)
  • I did some research into 2d spectra before starting and to my knowledge, they should only be in Flux units. Specviz2d will still be able to load surface brightness units in steradian, if applicable.

@kecnry
Copy link
Member

kecnry commented Jan 24, 2025

Probably could be a follow-up - but do we want to invert the x-axis on the 2d spectrum to align with the 1d spectrum when switching between wavelength and frequency units?

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.

Huge effort - just a few small comments and then I think this is good to go in and build on and hopefully didn't miss too many bugs that might slip through 🤞

@@ -18,12 +19,43 @@ def _is_matched_viewer(self, viewer):
return isinstance(viewer, (MosvizProfile2DView, MosvizProfileView))

def _map_limits(self, from_viewer, to_viewer, limits={}):
Copy link
Member

Choose a reason for hiding this comment

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

any (reasonable) way to increase test coverage in this method?

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'm not sure if codecov will be satisfied, but, I should be able to add to an existing test where the data_collection entry doesn't have the cids that were looking for.

A second addition could be to manually recreate the conversion with the pixel_to_world_limits and vis versa in the test itself and assert whether the results are the same (but it will pretty much be identical to the logic in the method).

Copy link
Contributor Author

@gibsongreen gibsongreen Jan 24, 2025

Choose a reason for hiding this comment

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

I added a second test for when the data loaded doesn't have the Wave nor Wavelength component (I tried using data_collection[0].remove_component('Wave') to no avail). I'm open to adding the pixel_to_world_limits test if coverage doesn't increase much from this recent test addition.

jdaviz/configs/mosviz/plugins/tools.py Outdated Show resolved Hide resolved
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.

Great work! I tested this out as best as I could, and made follow up tickets for the existing issues on main that prevented me from testing some of the functionality. I think that it is OK to merge this with the expectation that things might come up once some of the blocking issues are fixed.

  1. The spectrum viewer and image viewer both pick up changes in flux units, and the mouseover displays the correct units in both viewers. Repeated translations of the spectral axis are buggy, but this is the case on main and I created a follow up ticket for this.
  2. Model fitting. Previews of fit models don't display in the spectrum viewer, which is a pre-existing issue. It looks like the test you wrote for this verifies the unit conversion functionality in the model fitting plugin, and I tested various cases, so I think that is sufficient and if something comes up with the previews after that's fixed we can follow up.
  3. Spectral extraction seems to work with unit conversions, I didn't test this super robustly but in the few cases I tested it with, it works.
  4. The markers table plugin accurately reflects the selected unit, and works between various conversions that I tried.
  5. Line Lists and Line Analysis would benefit from a follow up investigation to see if any unit conversion support is needed, in general.
  6. There is a blocking issue with creating subsets that prevented me from testing some of the plugin functionality on subsets. I am unable to create spectral subsets. If this is not fixed with your linking PR, i'll make a ticket

Approving, pending 3.13 test failure (is this unrelated?)

@kecnry
Copy link
Member

kecnry commented Jan 30, 2025

3.13 dev tests failure is unrelated and was fixed in the bugfix branch by #3402 and will be fixed on main by #3238 (pending upstream support for messaging priority)

@gibsongreen gibsongreen merged commit 4ffc3a0 into spacetelescope:main Jan 30, 2025
19 of 21 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
cubeviz imviz plugin Label for plugins common to multiple configurations specviz specviz2d
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants