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 flux unit conversions in plugins #3228

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

Conversation

cshanahan1
Copy link
Contributor

@cshanahan1 cshanahan1 commented Oct 18, 2024

Flux unit conversions done in plugins go through the 'flux_conversion_general' function that handles all equivalencies and special cases where the angle needs to be multiplied out before conversion. plugins that listen to unit conversion are tested with every combination of units to ensure all units that the spectral y axis can be translated to work in the plugin.

@github-actions github-actions bot added cubeviz testing imviz plugin Label for plugins common to multiple configurations labels Oct 18, 2024
@cshanahan1 cshanahan1 added this to the 4.1 milestone Nov 1, 2024
@pllim
Copy link
Contributor

pllim commented Nov 1, 2024

What is the warning about? Might be related.

UserWarning: Given trait value dtype "float64" does not match required type "float64". A coerced copy has been created.

@pllim
Copy link
Contributor

pllim commented Nov 1, 2024

Does this need a change log?

# but I'm keeping them since they were already here. Background
# should only be converted flux<>flux or sb<>sb so only a possible
# u.spectral_density would be needed. explore removing these as a follow up
equivs = u.spectral() +\
Copy link
Contributor

Choose a reason for hiding this comment

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

I think \ for continuation is discouraged in Python. Instead please use ().

@@ -308,7 +320,11 @@ def _get_defaults_from_metadata(self, dataset=None):
# if display unit is different, translate
if (self.config == 'cubeviz') and (self.display_unit != ''):
disp_unit = u.Unit(self.display_unit)
mjy2abmag = (mjy2abmag * u.Unit("MJy/sr")).to_value(disp_unit)
mjy2abmag = flux_conversion_general(mjy2abmag,
u.Unit("MJy/sr"),
Copy link
Contributor

Choose a reason for hiding this comment

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

This would save the overhead of string parsing.

Suggested change
u.Unit("MJy/sr"),
u.MJy / u.sr,

Comment on lines +357 to +361
Note: This is essentially a simplified version of the function utils.flux_conversion.
The difference is that all required equivalencies must be passed in (rather than being
generated from an input spectrum or slice value, and combined with input equivalences).
I didn't want to replace calls to that function entirely because there is some extra
logic there that i'm unsure of, but in theory we could/should.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be rendered in API docstring? Looks like a note meant for internal devs only. Should this be moved to code comment instead?

Since this sounds like such an important function, please also document Parameters and Returns as laid out by numpydoc.

units appear in the aperture photometry output table in the variance columns.
Units like (MJy / sr)**2 to (Jy / sr)**2 can convert directly using astropy
units , but if an equivalency is required this conversion needs
a workaround.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment about Parameters and Returns.

@cshanahan1
Copy link
Contributor Author

Does this need a change log?

yes waiting until the end so I don't get conflicts to resolve

Comment on lines +142 to +144
y = flux_conversion_general(self.y, self.yunit, unit, eqv)
else:
y = (self.y * self.yunit).to_value(unit)
y = flux_conversion_general(self.y, self.yunit, unit)
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch here, this originally was causing one of the tracebacks with some of the plugins in Specviz2d (in #3253), would only go away with this new flux_conversion_general.

Comment on lines 525 to +529
if 'PIXAR_SR' in self.app.data_collection[0].meta:
# Need current slice value and associated unit to use to compute
# spectral density equivalencies that enable Flux to Flux conversions.
# This is needed for units that are not directly convertible/translatable.
slice = viewer.slice_value * u.Unit(self.app._get_display_unit('spectral'))

value = flux_conversion(value, unit, self.image_unit,
eqv=_eqv_pixar_sr(self.app.data_collection[0].meta['PIXAR_SR']), # noqa: E501
slice=slice)
unit = self.image_unit

elif self.image_unit.is_equivalent(unit):
value = (value * u.Unit(unit)).to_value(u.Unit(self.image_unit))
unit = self.image_unit
pixar_sr = self.app.data_collection[0].meta
else:
pixar_sr = 1
eqv += _eqv_pixar_sr(pixar_sr)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a case here where we might need the _eqv_flux_to_sb_pixel equivalencies? This applies to Specviz, Specviz2d, (and I'm assuming Mosviz). This is one outcome in Specviz without it in the image below. In #3253 I introduce 'un'-exposing the SB and angle unit so they don't display in the API/UI, so it might not need to be address but until that is in this is the current state in non-Cubeviz configs.
Screenshot 2024-11-04 at 11 45 00 AM

Comment on lines +510 to +514
valid_types = ["spectral flux density", "surface brightness",
"power density/spectral flux density wav",
"photon flux density wav",
"photon flux density"]
if str(physical_type) not in valid_types:
Copy link
Contributor

Choose a reason for hiding this comment

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

Will make a note on the physical types JDAT-ticket and here (#3234).

equivs = _eqv_flux_to_sb_pixel() + u.spectral_density(init_x)
init_y = init_y.to(self._units['y'], equivs)

init_y = flux_conversion_general([init_y.value], init_y.unit, self._units['y'], equivs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Leaving another note here to retest model fitting in Specviz2d following merging this PR (#3253). Bug was preexisting unit conversion plugin but still worth retesting.

Comment on lines +566 to +568
eqv = _eqv_pixar_sr(self.dataset.selected_obj.meta.get('PIXAR_SR', 1.0))
eqv += _eqv_flux_to_sb_pixel() # for conversions between flux <> sb per pix2
eqv += u.spectral_density(self.dataset.selected_obj.spectral_axis)
Copy link
Contributor

@gibsongreen gibsongreen Nov 4, 2024

Choose a reason for hiding this comment

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

Suggested change
eqv = _eqv_pixar_sr(self.dataset.selected_obj.meta.get('PIXAR_SR', 1.0))
eqv += _eqv_flux_to_sb_pixel() # for conversions between flux <> sb per pix2
eqv += u.spectral_density(self.dataset.selected_obj.spectral_axis)
eqv = (
_eqv_pixar_sr(self.dataset.selected_obj.meta.get('PIXAR_SR', 1.0))
+ _eqv_flux_to_sb_pixel()
+ u.spectral_density(self.dataset.selected_obj.spectral_axis)
)

Comment on lines +347 to +348
def flux_conversion_general(values, original_unit, target_unit,
equivalencies=None, with_unit=True):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we keep the parameters the same here as in flux_conversion? Originally adding spec and slice had to do with the image-viewers (the one case I explicitly remember testing for is working with the current state here) and eventually image data. I'll need to test this out a bit more to, might not be necessary but I will add a note about this on the Imviz implementation ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cubeviz imviz plugin Label for plugins common to multiple configurations testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants