Skip to content

Commit

Permalink
aperture photometry plugin listens to unit conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Aug 5, 2024
1 parent 453ecc8 commit 1a95ebd
Show file tree
Hide file tree
Showing 4 changed files with 400 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Cubeviz

- Background subtraction support within Spectral Extraction. [#2859]

- Aperture photometry plugin now listens to changes in display unit. [#3118]

Imviz
^^^^^

Expand Down
105 changes: 105 additions & 0 deletions jdaviz/configs/cubeviz/plugins/tests/test_cubeviz_aperphot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest
from astropy import units as u
from astropy.table import Table
from astropy.tests.helper import assert_quantity_allclose
from astropy.utils.exceptions import AstropyUserWarning
from numpy.testing import assert_allclose
Expand Down Expand Up @@ -82,6 +83,8 @@ def test_cubeviz_aperphot_cube_orig_flux(cubeviz_helper, image_cube_hdu_obj_micr
assert np.isnan(row["slice_wave"])


@pytest.mark.skip(reason="ap. phot. on moment maps not currently in scope " +
"to handle unit conversion, unskip once supported.")
def test_cubeviz_aperphot_generated_2d_moment(cubeviz_helper, image_cube_hdu_obj_microns):
cubeviz_helper.load_data(image_cube_hdu_obj_microns, data_label="test")
flux_unit = u.Unit("1E-17 erg*s^-1*cm^-2*Angstrom^-1")
Expand Down Expand Up @@ -192,3 +195,105 @@ def test_cubeviz_aperphot_cube_orig_flux_mjysr(cubeviz_helper, spectrum1d_cube_c
assert_allclose(row["mean"], 5 * (u.MJy / u.sr))
# TODO: check if slice plugin has value_unit set correctly
assert_quantity_allclose(row["slice_wave"], 0.46236 * u.um)


def _compare_table_units(orig_tab, new_tab):

# compare two photometry tables with different
# units and make sure they are equivilant

for i, row in enumerate(orig_tab):
new_unit = new_tab[i]['unit'] or '-'
orig_unit = row['unit'] or '-'
if new_unit != '-' and orig_unit != '-':

new_unit = u.Unit(new_unit)
new = float(new_tab[i]['result']) * new_unit

orig_unit = u.Unit(orig_unit)
orig = float(row['result']) * orig_unit

orig_converted = orig.to(new_unit)
assert_quantity_allclose(orig_converted, new)


def test_cubeviz_aperphot_unit_conversion(cubeviz_helper, spectrum1d_cube_custom_fluxunit):
"""Make sure outputs of the aperture photometry plugin in Cubeviz
reflect the correct choice of display units from the Unit
Conversion plugin.
"""

# create cube with units of MJy / sr
mjy_sr_cube = spectrum1d_cube_custom_fluxunit(fluxunit=u.MJy / u.sr,
shape=(5, 5, 4))

# create apertures for photometry and background
aper = RectanglePixelRegion(center=PixCoord(x=2, y=3), width=1, height=1)
bg = RectanglePixelRegion(center=PixCoord(x=1, y=2), width=1, height=1)

cubeviz_helper.load_data(mjy_sr_cube, data_label="test")
cubeviz_helper.load_regions([aper, bg])

ap = cubeviz_helper.plugins['Aperture Photometry']._obj

ap.dataset_selected = "test[FLUX]"
ap.aperture_selected = "Subset 1"
ap.background_selected = "Subset 2"
ap.vue_do_aper_phot()

uc = cubeviz_helper.plugins['Unit Conversion']._obj

# check that initial units are synced between plugins
assert uc.flux_unit.selected == 'MJy'
assert uc.angle_unit.selected == 'sr'
assert ap.display_flux_or_sb_unit == 'MJy / sr'
assert ap.flux_scaling_display_unit == 'MJy'

# and defaults for inputs are in the correct unit
assert ap.flux_scaling == 0.003631
assert ap.background_value == 46

# test output of default curve of growth plot
# not sure how to access this from the figure
# ap_phot.plot.figure.marks[0].???

# output table in original units to compare to
# outputs after converting units
orig_tab = Table(ap.results)

# change units, which will change the numerator of the current SB unit
uc.flux_unit.selected = 'Jy'

# make sure inputs were re-computed in new units
# after the unit change
assert ap.flux_scaling == 3631
assert ap.background_value == 4.6e7

# re-do photometry and make sure table is in new units
# and consists of the same results as before converting units
ap.vue_do_aper_phot()
new_tab = Table(ap.results)

_compare_table_units(orig_tab, new_tab)

# test manual background and flux scaling option input in current
# units (Jy / sr) will be used correctly and converted to data units
ap.background_selected == 'Manual'
ap.background_value = 1.0e7
ap.flux_scaling = 1000
ap.vue_do_aper_phot()
orig_tab = Table(ap.results)

# change units back to MJy/sr from Jy/sr
uc.flux_unit.selected = 'MJy'

# make sure background input in Jy/sr is now in MJy/sr
assert ap.background_value == 10.
assert ap.flux_scaling == 0.001

# and that photometry results match those before unit converson,
# but with units converted
ap.vue_do_aper_phot()
new_tab = Table(ap.results)

_compare_table_units(orig_tab, new_tab)
Loading

0 comments on commit 1a95ebd

Please sign in to comment.