Skip to content

Commit

Permalink
BUG: Fix radial profile fitting when NaN in data (#3246)
Browse files Browse the repository at this point in the history
* BUG: Exclude NaNs in max and mean
calculations for radial profile fit

* TST: Add test that fails without this patch

* Add change log

* move import up
  • Loading branch information
pllim authored Oct 25, 2024
1 parent 236f75a commit a360087
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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
^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
31 changes: 29 additions & 2 deletions jdaviz/configs/imviz/tests/test_simple_aper_phot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import pytest
import numpy as np
from astropy import units as u
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a360087

Please # to comment.