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

Untranslatable unit handling #3139

Merged
merged 14 commits into from
Aug 19, 2024

Conversation

gibsongreen
Copy link
Contributor

@gibsongreen gibsongreen commented Aug 7, 2024

Description

This pull request is to address implementing a solution to handle units that cannot be translated using the MJy <-> MJy / sr custom equivalency (and subsequent surface brightness conversions between these units). The following units are able to be converted between flux units and other flux units, but issues specifically arise in translations and in surface brightness to surface brightness conversions. Custom equivalencies have been added in this PR that enable conversions and translations to solve this issue, and they work in a notebook cell setting. However, attempting to utilize this solution in Cubeviz UI causes tracebacks. It is likely due to the native units from the data collection. The native units will be used for the Spectrum1D object used in flux_conversion, and the native unit will also be used as either the original_unit or the target_unit (see jdaviz/utils.py). Translation(s) and/or conversion(s) needs to take place before the next translation/conversion occurs.

flux_units = [
    'erg / (s cm2 Angstrom)',
    'ph / (Angstrom s cm2)',
    'ph / (Hz s cm2)', 'ph / (Hz s cm2)'
    ]

sb_units = [
    'erg / (s cm2 Angstrom sr)',
    'ph / (Angstrom s cm2 sr)',
    'ph / (Hz s cm2 sr)', 'ph / (Hz s cm2 sr)'
    ]

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 cubeviz specviz plugin Label for plugins common to multiple configurations labels Aug 7, 2024
@gibsongreen gibsongreen force-pushed the untranslatable_unit_handling branch 2 times, most recently from dd8189c to 14a5b9b Compare August 8, 2024 21:03
Copy link

codecov bot commented Aug 8, 2024

Codecov Report

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

Project coverage is 88.81%. Comparing base (3976983) to head (d3ec377).
Report is 1 commits behind head on main.

Files Patch % Lines
jdaviz/utils.py 90.19% 5 Missing ⚠️
...specviz/plugins/unit_conversion/unit_conversion.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3139      +/-   ##
==========================================
- Coverage   88.82%   88.81%   -0.01%     
==========================================
  Files         112      112              
  Lines       17586    17624      +38     
==========================================
+ Hits        15621    15653      +32     
- Misses       1965     1971       +6     

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

@gibsongreen gibsongreen added this to the 4.0 milestone Aug 13, 2024
@gibsongreen gibsongreen marked this pull request as ready for review August 13, 2024 20:52
@pllim
Copy link
Contributor

pllim commented Aug 14, 2024

Patch coverage is only 67.64%. Any chance you can increase that?

Copy link
Collaborator

@rosteen rosteen left a comment

Choose a reason for hiding this comment

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

Works in testing, I left a few comments/suggestions/questions. I also think that the code handling the untranslatable units in utils.py could be reorganized a bit to reduce repeated code, I may make a PR to your branch as a suggestion since it's a bit too much to do in a comment here.

jdaviz/configs/imviz/plugins/coords_info/coords_info.py Outdated Show resolved Hide resolved
jdaviz/core/validunits.py Outdated Show resolved Hide resolved
else:
spectral_values = spec.spectral_axis

spec_unit = str(spec.flux.unit)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was going to say this should probably be flux_unit, but I guess the point is that it could be surface brightness so calling it that would also be confusing. How about spec_y_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.

For this spec unit, it's specifically looking at the Spectrum1D object. We want to see what the native units of the input data are, since that ultimately dictate the conversions/translations that take place. We can't just look at what's displayed along the spectrum viewers y-axis. I added a comment above the line that should clarify that the unit can be flux or sb, but let me know if I should add or explain this better

Copy link
Member

Choose a reason for hiding this comment

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

we just want to avoid confusion with spectral_unit/spectral_axis used in many places that are referring to wavelength/frequency.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Exactly. fluxish_unit? 😆

I didn't realize when I wrote the original comment that this variable is only used once later in the method, so it's not a huge deal.

@rosteen
Copy link
Collaborator

rosteen commented Aug 15, 2024

Patch coverage is only 67.64%. Any chance you can increase that?

I see it at 79.62...would still be nice to increase it a bit if possible.

@gibsongreen
Copy link
Contributor Author

I changed the term 'untranslatable' to 'transitive' for those specific units since they now are translatable. I was also thinking 'indirect', but if there are any other suggestions I can update the name

jdaviz/core/validunits.py Outdated Show resolved Hide resolved
jdaviz/core/validunits.py Outdated Show resolved Hide resolved
jdaviz/utils.py Outdated Show resolved Hide resolved
jdaviz/utils.py Show resolved Hide resolved
Copy link
Collaborator

@rosteen rosteen left a comment

Choose a reason for hiding this comment

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

Left a couple more small comments, I think this is close but I want to think about the _transitive_conversion function a little bit more, I think that's where some consolidation could happen but I'm not sure (and it works, so in some sense it doesn't necessarily need to be prettier).

jdaviz/utils.py Outdated Show resolved Hide resolved
jdaviz/utils.py Outdated Show resolved Hide resolved
jdaviz/utils.py Outdated Show resolved Hide resolved
@pllim
Copy link
Contributor

pllim commented Aug 16, 2024

How is the drop-down order controlled? Jy and MJy is at the top, and the list is not completely alphabetical order, yet two other Jy prefixes are scattered as such:

Screenshot 2024-08-16 082818

Should they be grouped together at the top? If so, is it in scope here or a separate ticket?

@pllim

This comment was marked as resolved.

@gibsongreen
Copy link
Contributor Author

Spectral extraction failed when I tried to grab "mean" instead of "sum". Is fixing this in scope here?

I reproduced and believe I know what the issue is, the spectra if you use min/max/mean are in Surface Brightness units and there needs to be an extra case in _indirect_conversions to handle going from A->D (just as it is done in the image-viewers) and not just A->C.

@gibsongreen
Copy link
Contributor Author

How is the drop-down order controlled? Jy and MJy is at the top, and the list is not completely alphabetical order, yet two other Jy prefixes are scattered as such:

Screenshot 2024-08-16 082818 Should they be grouped together at the top? If so, is it in scope here or a separate ticket?

This will also have to be done for the Spectral Unit and the Angle Unit (when fully populated) drop downs. Is the preference here alphabetical order or groupings of units with the same numerator base?

Copy link
Contributor

@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.

Mostly minor comments.

Side note: If performance becomes a problem in the future, we can take shortcuts for certain cases because when a /sr converts to another /sr, you don't really need all the fancy indirect stuff. It is basically the same as silently removing /sr, do the normal flux conversion, and then silently reapply it.

jdaviz/configs/imviz/plugins/coords_info/coords_info.py Outdated Show resolved Hide resolved
jdaviz/configs/imviz/plugins/coords_info/coords_info.py Outdated Show resolved Hide resolved
jdaviz/core/helpers.py Outdated Show resolved Hide resolved
jdaviz/tests/test_utils.py Outdated Show resolved Hide resolved
jdaviz/tests/test_utils.py Outdated Show resolved Hide resolved
jdaviz/tests/test_utils.py Outdated Show resolved Hide resolved
jdaviz/tests/test_utils.py Outdated Show resolved Hide resolved
jdaviz/utils.py Outdated Show resolved Hide resolved
@pllim
Copy link
Contributor

pllim commented Aug 16, 2024

Hmm looks like there are merge conflicts now, so needs a rebase too.

Is the preference here alphabetical order or groupings of units with the same numerator base?

Personally, I prefer logical grouping but I cannot speak for everyone.

@kecnry
Copy link
Member

kecnry commented Aug 16, 2024

Can ordering/grouping be a follow-up (and probably not necessary for 4.0)?

@gibsongreen
Copy link
Contributor Author

gibsongreen commented Aug 16, 2024

Spectral extraction failed when I tried to grab "mean" instead of "sum". Is fixing this in scope here?

I reproduced and believe I know what the issue is, the spectra if you use min/max/mean are in Surface Brightness units and there needs to be an extra case in _indirect_conversions to handle going from A->D (just as it is done in the image-viewers) and not just A->C.

Took me a bit but I realized since I split the _indirect_conversion into its own function and I was updating orig_units in the new function but that wasn't getting updated in flux_conversion, with that now updating spectral extraction should be working now!

Copy link
Contributor

@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.

My remaining unresolved comments are minor and spectral extraction bug seems fixed, so approving. Thanks!

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.

Just a few minor comments from looking through the diff, but I think they can be addressed as a follow-up since @gibsongreen is away for a while.

Comment on lines +325 to +327
slice : `astropy.units.Quantity`, optional
The current slice of a data cube, with units. Necessary for complex unit
conversions/translations that require spectral density equivalencies.
Copy link
Member

Choose a reason for hiding this comment

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

could this be unitless and just documented as required to be in the same units as original_units/values? That would then save a call to app._get_display_units when calling this.

Comment on lines +183 to +186
if self.angle_unit.selected == 'pix':
mouseover_unit = self.flux_unit.selected
else:
mouseover_unit = self.sb_unit_selected
Copy link
Member

Choose a reason for hiding this comment

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

@cshanahan1 - this logic will probably need to change for your effort once images/cubes are guaranteed to always be in SB units

Comment on lines +190 to +192
# if cube was loaded in flux units, we still need to broadcast
# a 'sb' message for mouseover info. this should be removed when
# unit change messaging is improved and is a temporary fix
Copy link
Member

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cubeviz 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