Skip to content

Commit

Permalink
Debug Cubeviz batch photometry (#3163)
Browse files Browse the repository at this point in the history
* Debugging Cubeviz batch photometry

* Fix changing units when in batch mode

* Working on adding cubeviz batch photometry test

* Add simple test

* Codestyle

* Add comment about future changes

* Move changelog to 3.10.4

* Don't convert if display unit hasn't been initialized

* Update test

* Codestyle

* Update jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py

Co-authored-by: P. L. Lim <2090236+pllim@users.noreply.github.com>

---------

Co-authored-by: P. L. Lim <2090236+pllim@users.noreply.github.com>
(cherry picked from commit 19a8f56)
  • Loading branch information
rosteen committed Sep 5, 2024
1 parent 02ac080 commit 7474e49
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Bug Fixes
Cubeviz
^^^^^^^

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

Imviz
^^^^^

Expand Down
19 changes: 15 additions & 4 deletions jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def valid_cubeviz_datasets(data):
'photon flux density wav',
'spectral flux density',
'photon flux density',
'energy flux'] # Moment map 0
'energy flux', # Moment map 0
'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 @@ -143,10 +144,19 @@ def _on_dataset_selected_changed(self, event={}):
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

def _get_defaults_from_metadata(self, dataset=None):
defaults = {}
Expand Down Expand Up @@ -842,6 +852,7 @@ def calculate_batch_photometry(self, options=[], add_to_table=True, update_plots
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)

0 comments on commit 7474e49

Please sign in to comment.