diff --git a/CHANGES.rst b/CHANGES.rst index 56b6a6167a..fa5ca94957 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,6 +6,9 @@ Bug Fixes - Improved performance and removed jittering for the matched box zoom tool. [#3215] +- Fixed Aperture Photometry radial profile fit crashing when NaN is present in + aperture data for Cubeviz and Imviz. [#3246] + Cubeviz ^^^^^^^ diff --git a/jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py b/jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py index 655ebefac6..e34b289cf3 100644 --- a/jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py +++ b/jdaviz/configs/imviz/plugins/aper_phot_simple/aper_phot_simple.py @@ -893,8 +893,8 @@ def calculate_photometry(self, dataset=None, aperture=None, background=None, # Fit Gaussian1D to radial profile data. if self.fit_radial_profile: fitter = TRFLSQFitter() - y_max = y_data.max() - x_mean = x_data[np.where(y_data == y_max)].mean() + y_max = np.nanmax(y_data) + x_mean = np.nanmean(x_data[np.where(y_data == y_max)]) std = 0.5 * (phot_table['semimajor_sigma'][0] + phot_table['semiminor_sigma'][0]) if isinstance(std, u.Quantity): diff --git a/jdaviz/configs/imviz/tests/test_simple_aper_phot.py b/jdaviz/configs/imviz/tests/test_simple_aper_phot.py index 685012264a..76bee54e0c 100644 --- a/jdaviz/configs/imviz/tests/test_simple_aper_phot.py +++ b/jdaviz/configs/imviz/tests/test_simple_aper_phot.py @@ -1,3 +1,5 @@ +import warnings + import pytest import numpy as np from astropy import units as u @@ -7,6 +9,7 @@ from numpy.testing import assert_allclose, assert_array_equal from photutils.aperture import (ApertureStats, CircularAperture, EllipticalAperture, RectangularAperture, EllipticalAnnulus) +from photutils.datasets import make_4gaussians_image from regions import (CircleAnnulusPixelRegion, CirclePixelRegion, EllipsePixelRegion, RectanglePixelRegion, PixCoord) @@ -333,8 +336,6 @@ def test_sky_background(self, data_label, fac, bg_label, expected_bg): def test_annulus_background(imviz_helper): - from photutils.datasets import make_4gaussians_image - gauss4 = make_4gaussians_image() # The background has a mean of 5 with noise ones = np.ones(gauss4.shape) @@ -421,6 +422,32 @@ def test_annulus_background(imviz_helper): assert_allclose(phot_plugin.background_value, bg_4gauss_4) +def test_fit_radial_profile_with_nan(imviz_helper): + gauss4 = make_4gaussians_image() # The background has a mean of 5 with noise + # Insert NaN + gauss4[25, 150] = np.nan + + imviz_helper.load_data(gauss4, data_label='four_gaussians') + + # Mark an object of interest + circle_1 = CirclePixelRegion(center=PixCoord(x=150, y=25), radius=7) + imviz_helper.plugins['Subset Tools']._obj.import_region( + [circle_1], combination_mode='new') + + phot_plugin = imviz_helper.app.get_tray_item_from_name('imviz-aper-phot-simple') + phot_plugin.dataset_selected = 'four_gaussians' + phot_plugin.aperture_selected = 'Subset 1' + phot_plugin.current_plot_type = 'Radial Profile' + phot_plugin.fit_radial_profile = True + with warnings.catch_warnings(): + warnings.simplefilter("ignore") # Fitter warnings do not matter, only want error. + phot_plugin.vue_do_aper_phot() + tbl = imviz_helper.get_aperture_photometry_results() + + assert phot_plugin.result_failed_msg == '' + assert_allclose(tbl['sum'][0], 8590.419296) + + # NOTE: Extracting the cutout for radial profile is aperture # shape agnostic, so we use ellipse as representative case. # NOTE: This test only tests the radial profile algorithm and does