Skip to content

Commit

Permalink
fix untranslatable units for per pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Sep 6, 2024
1 parent 3ea11dc commit 7588350
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion jdaviz/core/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def set_y_unit(self, unit=None):
y = (self.y * self.yunit).to_value(unit, equivalencies=eqv)

# for flux <> flux/pix2
eqv += _eqv_flux_to_sb_pixel(u.Jy) # extend to untranslatable, Jy is placeholder
eqv += _eqv_flux_to_sb_pixel()
else:
y = (self.y * self.yunit).to_value(unit)
self.yunit = unit
Expand Down
19 changes: 10 additions & 9 deletions jdaviz/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_spec_sb_flux_conversion():
spec.meta["_pixel_scale_factor"] = 0.1
assert_allclose(flux_conversion(values, u.Jy / u.sr, u.Jy, spec), [1, 2, 3])
assert_allclose(flux_conversion(values, u.Jy, u.Jy / u.sr, spec), [100, 200, 300])

# conversions with eq pixels
assert_allclose(flux_conversion(values, u.Jy / PIX2, u.Jy, spec), [10, 20, 30])
assert_allclose(flux_conversion(values, u.Jy, u.Jy / PIX2, spec), [10, 20, 30])
Expand All @@ -53,14 +53,15 @@ def test_spec_sb_flux_conversion():
# test spectrum when target unit in untranslatable unit list
target_values = [5.03411657e-05, 2.01364663e-04, 4.53070491e-04]
expected_units = (u.ph / (u.Hz * u.s * u.cm**2))
returned_values, return_units, unit_flag = _indirect_conversion(
values=values, orig_units=(u.MJy),
targ_units=(u.ph / (u.s * u.cm**2 * u.Hz * u.sr)), # noqa
eqv=eqv, spec_unit=spec_unit, image_data=None
)
assert_allclose(returned_values, target_values)
assert (return_units == expected_units)
assert (unit_flag == 'orig')
for solid_angle in [u.sr, u.pix*u.pix]:
returned_values, return_units, unit_flag = _indirect_conversion(
values=values, orig_units=(u.MJy),
targ_units=(u.ph / (u.s * u.cm**2 * u.Hz * solid_angle)), # noqa
eqv=eqv, spec_unit=spec_unit, image_data=None
)
assert_allclose(returned_values, target_values)
assert (return_units == expected_units)
assert (unit_flag == 'orig')

# test spectrum when original unit in untranslatable unit list
target_values = [1., 2., 3.]
Expand Down
22 changes: 14 additions & 8 deletions jdaviz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ def flux_conversion(values, original_units, target_units, spec=None, eqv=None, s
orig_units = u.Unit(original_units)
targ_units = u.Unit(target_units)

solid_angle_in_orig = check_if_unit_is_per_solid_angle(orig_units)
solid_angle_in_targ = check_if_unit_is_per_solid_angle(targ_units)
solid_angle_in_orig = check_if_unit_is_per_solid_angle(orig_units, return_unit=True)
solid_angle_in_targ = check_if_unit_is_per_solid_angle(targ_units, return_unit=True)

# Ensure a spectrum passed through Spectral Extraction plugin
if (('_pixel_scale_factor' in spec.meta) and
Expand All @@ -431,9 +431,7 @@ def flux_conversion(values, original_units, target_units, spec=None, eqv=None, s
eqv += _eqv_pixar_sr(np.array(eqv_in))

# may need equivalencies between flux and flux per square pixel
# NOTE need to extend this to other flux types that aren't equiv with u.Jy,
# this is a placeholder
eqv += _eqv_flux_to_sb_pixel(u.Jy)
eqv += _eqv_flux_to_sb_pixel()

# when angle<>pixel translations are enabled
# eqv += _eqv_sb_per_pixel_to_per_angle(u.Jy)
Expand Down Expand Up @@ -461,14 +459,23 @@ def flux_conversion(values, original_units, target_units, spec=None, eqv=None, s
values=values, orig_units=orig_units, targ_units=targ_units,
eqv=eqv, image_data=image_data
)
elif solid_angle_in_orig == solid_angle_in_targ == u.pix * u.pix:
# in the case where we have 2 SBs per solid pixel that need
# u.spectral_density equivalency, they can't be directly converted
# for whatever reason (i.e 'Jy / pix2' and 'erg / (Angstrom s cm2 pix2)'
# are not convertible). In this case, multiply out the factor of pix2 for
# conversion (same kind of thing _indirect_conversion is
# doing but we already know the exact angle units.
orig_units *= u.pix * u.pix
targ_units *= u.pix * u.pix

return (values * orig_units).to_value(targ_units, equivalencies=eqv)


def _indirect_conversion(values, orig_units, targ_units, eqv,
spec_unit=None, image_data=None):

# Note: is there a way we could write this to not require 'spec_unit'? It
# Note: is there a way we could write this to not require 'spec_unit'? It
# seems like it falls back on this to get a solid angle unit, but can we
# assume pix2 now if there are none? or use the display units?

Expand Down Expand Up @@ -516,7 +523,6 @@ def _indirect_conversion(values, orig_units, targ_units, eqv,
return values, orig_units



def _eqv_pixar_sr(pixar_sr):
"""
Return Equivalencies to convert from flux to flux per solid
Expand All @@ -537,7 +543,7 @@ def iconverter_flux(x): # Flux -> Surface Brightness
]


def _eqv_flux_to_sb_pixel(flux_unit):
def _eqv_flux_to_sb_pixel():
"""
Returns an Equivalency between `flux_unit` and `flux_unit`/pix**2. This
allows conversion between flux and flux-per-square-pixel surface brightness
Expand Down

0 comments on commit 7588350

Please sign in to comment.