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

Debug Cubeviz batch photometry #3163

Merged
merged 11 commits into from
Sep 5, 2024
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ Bug Fixes
Cubeviz
^^^^^^^

- Fixed multiple select handling for batch mode aperture photometry in Cubeviz. [#3163]

Imviz
^^^^^

Expand Down
64 changes: 39 additions & 25 deletions jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py
Copy link
Contributor

Choose a reason for hiding this comment

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

Changes in this file are mainly changing things that @cshanahan1 added earlier, so I would prefer she review as well.

Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@
acceptable_types = ['spectral flux density wav',
'photon flux density wav',
'spectral flux density',
'photon flux density']
'photon flux density',
'surface brightness']
return ((data.ndim in (2, 3)) and
((img_unit == (u.MJy / u.sr)) or
(img_unit.physical_type in acceptable_types)))
Expand Down Expand Up @@ -145,10 +146,19 @@
if self.config != "cubeviz":
return
# self.dataset might not exist when app is setting itself up.
if hasattr(self, "dataset") and self.dataset.selected_dc_item.ndim > 2:
self.is_cube = True
else:
if hasattr(self, "dataset"):
if isinstance(self.dataset.selected_dc_item, list):
datasets = self.dataset.selected_dc_item
else:
datasets = [self.dataset.selected_dc_item]

self.is_cube = False
for dataset in datasets:
# This assumes all cubes, or no cubes. If we allow photometry on collapsed cubes
# or images this will need to change.
if dataset.ndim > 2:
self.is_cube = True
break
kecnry marked this conversation as resolved.
Show resolved Hide resolved

def _on_display_units_changed(self, event={}):

Expand Down Expand Up @@ -208,6 +218,8 @@
return

data = self.dataset.selected_dc_item
if isinstance(data, list):
data = data[0]
Comment on lines +221 to +222
Copy link
Member

Choose a reason for hiding this comment

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

don't love this, but this whole method will probably go away with #3156, and I can't think of a better way to do it with the current setup.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

this will go away at some point (im trying to consolidate everything that listens to unit changes to one method in that PR), but it also seems like its necessary here for now

comp = data.get_component(data.main_components[0])
if comp.units:
# if data is something-per-solid-angle, its a SB unit and we should
Expand Down Expand Up @@ -557,7 +569,7 @@

# cubeviz: background_value set in plugin is in display units
# convert temporarily to image units for calculations
if (self.config == 'cubeviz') and (img_unit is not None):
if (self.config == 'cubeviz') and (img_unit is not None) and display_unit != '':
background_value = (background_value * display_unit).to_value(
img_unit, u.spectral_density(self._cube_wave))

Expand All @@ -568,7 +580,7 @@

# cubeviz: background_value set in plugin is in display units
# convert temporarily to image units for calculations
if (self.config == 'cubeviz') and (img_unit is not None):
if (self.config == 'cubeviz') and (img_unit is not None) and display_unit != '':
background_value = (background_value * display_unit).to_value(
img_unit, u.spectral_density(self._cube_wave))
else:
Expand All @@ -578,7 +590,7 @@

# cubeviz: computed background median will be in display units,
# convert temporarily back to image units for calculations
if (self.config == 'cubeviz') and (img_unit is not None):
if (self.config == 'cubeviz') and (img_unit is not None) and display_unit != '':

Check warning on line 593 in jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py#L593

Added line #L593 was not covered by tests
background_value = (background_value * display_unit).to_value(
img_unit, u.spectral_density(self._cube_wave))
try:
Expand Down Expand Up @@ -726,25 +738,26 @@
# convert units of certain columns in aperture phot. output table
# to reflect display units (i.e if data units are MJy / sr, but
# Jy / sr is selected in Unit Conversion plugin)
phot_table['background'] = phot_table['background'].to(
display_unit, u.spectral_density(self._cube_wave))

if include_pixarea_fac:
phot_table['sum'] = phot_table['sum'].to(
(display_unit * pixarea_fac).unit, u.spectral_density(self._cube_wave))
else:
phot_table['sum'] = phot_table['sum'].to(
if display_unit != '':
phot_table['background'] = phot_table['background'].to(
display_unit, u.spectral_density(self._cube_wave))
for key in ['min', 'max', 'mean', 'median', 'mode', 'std',
'mad_std', 'biweight_location']:
phot_table[key] = phot_table[key].to(
display_unit, u.spectral_density(self._cube_wave))
for key in ['var', 'biweight_midvariance']:
try:
phot_table[key] = phot_table[key].to(display_unit**2)
# FIXME: Can fail going between per-wave and per-freq
except u.UnitConversionError:
pass

if include_pixarea_fac:
phot_table['sum'] = phot_table['sum'].to(
(display_unit * pixarea_fac).unit, u.spectral_density(self._cube_wave))
else:
phot_table['sum'] = phot_table['sum'].to(
display_unit, u.spectral_density(self._cube_wave))
for key in ['min', 'max', 'mean', 'median', 'mode', 'std',
'mad_std', 'biweight_location']:
phot_table[key] = phot_table[key].to(
display_unit, u.spectral_density(self._cube_wave))
for key in ['var', 'biweight_midvariance']:
try:
phot_table[key] = phot_table[key].to(display_unit**2)
# FIXME: Can fail going between per-wave and per-freq
except u.UnitConversionError:
pass

Check warning on line 760 in jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py#L759-L760

Added lines #L759 - L760 were not covered by tests

if add_to_table:
try:
Expand Down Expand Up @@ -1060,6 +1073,7 @@
option.setdefault('pixel_area', defaults.get('pixel_area', 0))
if self.flux_scaling_multi_auto:
option.setdefault('flux_scaling', defaults.get('flux_scaling', 0))

try:
self.calculate_photometry(add_to_table=add_to_table,
update_plots=this_update_plots,
Expand Down
30 changes: 30 additions & 0 deletions jdaviz/configs/imviz/tests/test_simple_aper_phot.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,33 @@ def test_curve_of_growth(with_unit):
with pytest.raises(TypeError, match='Unsupported aperture'):
_curve_of_growth(data, cen, EllipticalAnnulus(cen, 3, 8, 5), 100,
pixarea_fac=pixarea_fac)


def test_cubeviz_batch(cubeviz_helper, spectrum1d_cube_fluxunit_jy_per_steradian):
cubeviz_helper.load_data(spectrum1d_cube_fluxunit_jy_per_steradian, data_label='test')
phot_plugin = cubeviz_helper.plugins['Aperture Photometry']._obj
uc_plugin = cubeviz_helper.plugins['Unit Conversion']

cubeviz_helper.load_regions(CirclePixelRegion(center=PixCoord(x=5, y=5), radius=2))
cubeviz_helper.load_regions(CirclePixelRegion(center=PixCoord(x=3, y=3), radius=2))

phot_plugin.dataset_selected = 'test[FLUX]'
phot_plugin.multiselect = True
phot_plugin.aperture.selected = ['Subset 1', 'Subset 2']

phot_plugin.calculate_batch_photometry(full_exceptions=True)
assert len(phot_plugin.table) == 2
tbl = cubeviz_helper.get_aperture_photometry_results()
assert_quantity_allclose(tbl['sum'], [5.980836e-12, 2.037396e-10] * u.Jy, rtol=1e-4)

# Test that it still works after unit conversion
uc_plugin.flux_unit = 'MJy'

phot_plugin.calculate_batch_photometry(full_exceptions=True)

assert len(phot_plugin.table) == 4
tbl = cubeviz_helper.get_aperture_photometry_results()
# get_aperture_photometry_results converts all to the same units
assert_quantity_allclose(tbl['sum'],
[5.980836e-12, 2.037396e-10, 5.980836e-12, 2.037396e-10] * u.Jy,
rtol=1e-4)