-
Notifications
You must be signed in to change notification settings - Fork 74
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
Improve spaxel unit handling for cubes #3307
Conversation
It seems like MaNGA files might have changed format somewhat in the last few years. The test file we have is from 2020 and loads fine on |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3307 +/- ##
=======================================
Coverage 88.80% 88.80%
=======================================
Files 125 125
Lines 19118 19118
=======================================
Hits 16978 16978
Misses 2140 2140 ☔ View full report in Codecov by Sentry. |
Btw, I don't think we were really handling this correctly before #3221 either, that just exposed the problem in a way that errors out. |
@rosteen Regarding There haven't been any changes to the MaNGA files since their last data release in 2021, reduced in 2020. You can check the header keyword The flux units have always been set to the following in the headers, for both new and older files.
It's impossible to change the MaNGA data itself now so a fix has to be either in |
It looks like we are using an older file version in our test:
In regards to I think the fix belongs here in Jdaviz, this PR already lets us load the data properly, it was just a matter of whether I add a little more Edit: from the edit I made in the test, looks like the old file was version 2_1_2 |
Gah, I see now what you were saying that |
Ahh then in that case, if it's easier to switch to pix2, then I think that's ok. Do we even need an angular unit? How did Jdaviz handle manga data before before, say last year or so? |
We've had a whole push to handle surface brightness vs flux better in the last year (amidst the bigger Unit Conversion work), which included adding |
Thanks for looking into this @rosteen - I tried your changes in a patch to let me read in MaNGA cubes - this works for me now (thanks!) though I came across a unit error that may be related when trying to fit a model to the spectrum (when I trigger
Details
|
Thanks for testing this, I was aware of a couple things that weren't working but not this specifically - I'm working on refactoring a bit in a way that I think will fix this. I'll take this PR out of draft once I think I have it all working. |
Ok, this PR fixes the actual data loading of the MaNGA cubes, but exposed two related bugs/problems that I'm going to make a follow up for:
|
I think the docs build failure is related to Astropy release, not this PR. Not 100% sure though. |
if bad_locs is not None: | ||
# Prefer 0 over inf for the masked out values here | ||
flux[bad_locs] = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we just always replace infs (maybe with nans instead of zeros) regardless of the uncertainty type? Or should that be handled in individual plugins so that we do not modify original data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, let's discuss offline.
jdaviz/core/custom_units.py
Outdated
@@ -6,6 +6,13 @@ | |||
PIX2 = u.pix * u.pix | |||
|
|||
|
|||
# Add spaxel to enabled units | |||
def enable_spaxel_unit(): | |||
spaxel = spaxel = u.Unit('spaxel', represents=u.pixel, parse_strict='silent') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already in specutils parser (but not exposed). Should this be moved upstream eventually?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, for now I just made our spaxel definition match the specutils one so they would actually divide properly. And whoops, looks like I have a double spaxel =
there.
This is probably gonna conflict with #3228 . Should @cshanahan1 review? |
Use intersphinx :ref:`astropy:unit_equivalencies` |
Upon further investigation, 2 is just a manifestation of 1, not due to the values being too small. So only one follow up issue! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks good to me here!
I did notice one bug that I confirmed was not created by this PR. When a manga (or similarly counts) cube is loaded, if you translate, the the values do not update according to the scale factor. This PR made this more visible when changing flux units of the manga cube after translating (resulting in values not updating and zoom/other features no longer functioning until reverting to the original spectrum-y value).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the aperture photometry plugin, there is some logic specifically referring to PIX2 that should also probably be applied to spaxel. the pixel area is fixed to 1.0 for pix2 cubes, which I think should also be the case for spaxel? (the PIX2 logic in lines ~210-220 in aper_phot_simple.py are fixed in #3228).
There is also some logic for PIX2 done in coords_info when filtering out certain data types so their units aren't converted for mouseover, and spaxel should be added to that too, but I would do this off #3228 since there are changes to this part of the code there as well.
jdaviz/core/custom_units.py
Outdated
@@ -6,6 +6,13 @@ | |||
PIX2 = u.pix * u.pix | |||
|
|||
|
|||
# Add spaxel to enabled units | |||
def enable_spaxel_unit(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should probably define the PIX2 unit like this right? should i make a ticket?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the best order of operations here is since these changes will overlap with some of the changes made in #3228. It might make more sense for that to go in first? That way, you can add 'spaxel' to the parameterized tests on that branch that test unit conversion in plugins. Additionally, the same logic added to utils.flux_conversion here will need to be added to unit_conversion_utils.flux_conversion_general on my branch so spaxel will work in plugins with unit conversions. (I hope these two functions can be unified at some point, but for now utils.flux_conversion_general handles the spectral axis and unit_conversion_utils.flux_conversion_general handles all other flux unit conversions).
Another idea would be to merge this since it fixes loading for now, with the caveat that unit conversion is broken. Then merge my PR, then make a follow up ticket to add spaxel to the logic that my PR introduced.
@cshanahan1 I'm happy to merge yours first and rebase this, that might be the easier way. I'll respond to the other comments shortly. |
elif check_if_unit_is_per_solid_angle(flux.unit, return_unit=True) == "spaxel": | ||
# We need to convert spaxel to pixel squared, since spaxel isn't fully supported by astropy | ||
# This is horribly ugly but just multiplying by u.Unit("spaxel") doesn't work | ||
target_flux_unit = flux.unit * u.Unit('spaxel') / PIX2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cshanahan1 just pointing out that this is where spaxel
is getting converted to PIX2
in the parser - as discussed offline we shouldn't need spaxel handling elsewhere.
c0c6914
to
8e50ce7
Compare
#3221 broke MaNGA data cube loading, because we didn't account for
spaxel
being in the flux units anywhere in our code. I added that to the list of recognized solid angle units incheck_if_unit_is_per_solid_angle
, but this probably needs a little more thought about whether to exposespaxel
in the unit conversion plugin along withpix^2
, or whether to always convertspaxel
topix^2
on data load and just havepix^2
in the unit conversion plugin. @havok2063 @camipacifici any thoughts on those two options?Note that I milestoned this as 4.1, loading still works in 4.0.x because #3221 was milestoned to 4.1.
Still investigating why the current MaNGA loading test didn't catch this.