Skip to content

Commit

Permalink
Update to photutils v2.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
vrodgom committed Oct 23, 2024
1 parent dbd82c4 commit 5550c00
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
50 changes: 27 additions & 23 deletions statmorph/statmorph.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
from astropy.utils.exceptions import (AstropyUserWarning,
AstropyDeprecationWarning)
from astropy.convolution import convolve
import photutils
from photutils.aperture import (CircularAperture, CircularAnnulus,
EllipticalAperture, EllipticalAnnulus)
from photutils.background import ModeEstimatorBackground
from photutils.segmentation import SegmentationImage


__all__ = [
'ConvolvedSersic2D',
Expand Down Expand Up @@ -161,7 +165,7 @@ def _fraction_of_total_function_circ(r, image, center, fraction, total_sum):
if r == 0:
cur_fraction = 0.0
else:
ap = photutils.aperture.CircularAperture(center, r)
ap = CircularAperture(center, r)
# Force flux sum to be positive:
ap_sum = np.abs(ap.do_photometry(image, method='exact')[0][0])
cur_fraction = ap_sum / total_sum
Expand All @@ -176,7 +180,7 @@ def _radius_at_fraction_of_total_circ(image, center, r_total, fraction):
"""
flag = 0

ap_total = photutils.aperture.CircularAperture(center, r_total)
ap_total = CircularAperture(center, r_total)

total_sum = ap_total.do_photometry(image, method='exact')[0][0]
if total_sum == 0:
Expand Down Expand Up @@ -220,7 +224,7 @@ def _fraction_of_total_function_ellip(a, image, center, elongation, theta,
cur_fraction = 0.0
else:
b = a / elongation
ap = photutils.aperture.EllipticalAperture(center, a, b, theta=theta)
ap = EllipticalAperture(center, a, b, theta=theta)
# Force flux sum to be positive:
ap_sum = np.abs(ap.do_photometry(image, method='exact')[0][0])
cur_fraction = ap_sum / total_sum
Expand All @@ -238,7 +242,7 @@ def _radius_at_fraction_of_total_ellip(image, center, elongation, theta,
flag = 0

b_total = a_total / elongation
ap_total = photutils.aperture.EllipticalAperture(
ap_total = EllipticalAperture(
center, a_total, b_total, theta=theta)

total_sum = ap_total.do_photometry(image, method='exact')[0][0]
Expand Down Expand Up @@ -595,8 +599,8 @@ def __init__(self, image, segmap, label, mask=None, weightmap=None,
# Measure runtime
start = time.time()

if not isinstance(self._segmap, photutils.segmentation.SegmentationImage):
self._segmap = photutils.segmentation.SegmentationImage(self._segmap)
if not isinstance(self._segmap, SegmentationImage):
self._segmap = SegmentationImage(self._segmap)

# Check sanity of input data
self._segmap.check_labels([self.label])
Expand Down Expand Up @@ -1110,8 +1114,8 @@ def _petrosian_function_circ(self, r, center):
r_in = r - 0.5 * self._annulus_width
r_out = r + 0.5 * self._annulus_width

circ_annulus = photutils.aperture.CircularAnnulus(center, r_in, r_out)
circ_aperture = photutils.aperture.CircularAperture(center, r)
circ_annulus = CircularAnnulus(center, r_in, r_out)
circ_aperture = CircularAperture(center, r)

# Force mean fluxes to be positive:
circ_annulus_mean_flux = np.abs(_aperture_mean_nomask(
Expand Down Expand Up @@ -1208,7 +1212,7 @@ def flux_circ(self):
"""
image = self._cutout_stamp_maskzeroed
r = self._petro_extent_flux * self.rpetro_circ
ap = photutils.aperture.CircularAperture(self._asymmetry_center, r)
ap = CircularAperture(self._asymmetry_center, r)
# Force flux sum to be positive:
ap_sum = np.abs(ap.do_photometry(image, method='exact')[0][0])
return ap_sum
Expand All @@ -1232,9 +1236,9 @@ def _petrosian_function_ellip(self, a, center, elongation, theta):

b_out = a_out / elongation

ellip_annulus = photutils.aperture.EllipticalAnnulus(
ellip_annulus = EllipticalAnnulus(
center, a_in, a_out, b_out, theta=theta)
ellip_aperture = photutils.aperture.EllipticalAperture(
ellip_aperture = EllipticalAperture(
center, a, b, theta=theta)

# Force mean fluxes to be positive:
Expand Down Expand Up @@ -1327,7 +1331,7 @@ def flux_ellip(self):
a = self._petro_extent_flux * self.rpetro_ellip
b = a / self.elongation_asymmetry
theta = self.orientation_asymmetry
ap = photutils.aperture.EllipticalAperture(
ap = EllipticalAperture(
self._asymmetry_center, a, b, theta=theta)
# Force flux sum to be positive:
ap_sum = np.abs(ap.do_photometry(image, method='exact')[0][0])
Expand All @@ -1352,7 +1356,7 @@ def _segmap_gini(self):
a_out = self.rpetro_ellip + 0.5 * self._annulus_width
b_out = a_out / self.elongation_asymmetry
theta = self.orientation_asymmetry
ellip_annulus = photutils.aperture.EllipticalAnnulus(
ellip_annulus = EllipticalAnnulus(
(self._xc_stamp, self._yc_stamp), a_in, a_out, b_out, theta=theta)
ellip_annulus_mean_flux = _aperture_mean_nomask(
ellip_annulus, cutout_smooth, method='exact')
Expand Down Expand Up @@ -1694,21 +1698,21 @@ def _asymmetry_function(self, center, image, kind):
# Create aperture for the chosen kind of asymmetry
if kind == 'cas' or kind == 'rms':
r = self._petro_extent_cas * self._rpetro_circ_centroid
ap = photutils.aperture.CircularAperture(center, r)
ap = CircularAperture(center, r)
elif kind == 'outer':
a_in = self.rhalf_ellip
a_out = self.rmax_ellip
b_out = a_out / self.elongation_asymmetry
theta = self.orientation_asymmetry
assert (a_in > 0) & (a_out > 0)
ap = photutils.aperture.EllipticalAnnulus(center, a_in, a_out, b_out, theta=theta)
ap = EllipticalAnnulus(center, a_in, a_out, b_out, theta=theta)
elif kind == 'shape':
if np.isnan(self.rmax_circ) or (self.rmax_circ <= 0):
warnings.warn('[shape_asym] Invalid rmax_circ value.',
AstropyUserWarning)
self.flag = 2
return -99.0 # invalid
ap = photutils.aperture.CircularAperture(center, self.rmax_circ)
ap = CircularAperture(center, self.rmax_circ)
else:
raise NotImplementedError('Asymmetry kind not understood:', kind)

Expand Down Expand Up @@ -1939,7 +1943,7 @@ def smoothness(self):
# Exclude central region during smoothness calculation:
r_in = self._petro_fraction_cas * self.rpetro_circ
r_out = self._petro_extent_cas * self.rpetro_circ
ap = photutils.aperture.CircularAnnulus(self._asymmetry_center, r_in, r_out)
ap = CircularAnnulus(self._asymmetry_center, r_in, r_out)

boxcar_size = int(self._petro_fraction_cas * self.rpetro_circ)
image_smooth = ndi.uniform_filter(image, size=boxcar_size)
Expand Down Expand Up @@ -2405,7 +2409,7 @@ def _segmap_shape_asym(self):
# that only contains background sky (hopefully).
r_in = self._petro_extent_flux * self.rpetro_ellip
r_out = 2.0 * self._petro_extent_flux * self.rpetro_ellip
circ_annulus = photutils.aperture.CircularAnnulus(center, r_in, r_out)
circ_annulus = CircularAnnulus(center, r_in, r_out)

# Convert circular annulus aperture to binary mask
circ_annulus_mask = circ_annulus.to_mask(method='center')
Expand All @@ -2431,7 +2435,7 @@ def _segmap_shape_asym(self):
return ~self._mask_stamp_no_bg

# Define the "mode" as in Bertin & Arnouts (1996):
bkg_estimator = photutils.background.ModeEstimatorBackground(
bkg_estimator = ModeEstimatorBackground(
median_factor=2.5, mean_factor=1.5)

# Do sigma-clipping until convergence
Expand Down Expand Up @@ -2624,7 +2628,7 @@ def _sersic_model(self):
self.flag_sersic = 2
a_in = guess_r_eff
b_out = (1 - guess_ellip) * a_out
ellip_annulus = photutils.aperture.EllipticalAnnulus(
ellip_annulus = EllipticalAnnulus(
guess_center, a_in, a_out, b_out, theta=guess_theta)
ellip_annulus_mean_flux = _aperture_mean_nomask(
ellip_annulus, image, method='exact')
Expand Down Expand Up @@ -3211,8 +3215,8 @@ def source_morphology(image, segmap, **kwargs):
See `README.rst` for a list of references.
"""
if not isinstance(segmap, photutils.segmentation.SegmentationImage):
segmap = photutils.segmentation.SegmentationImage(segmap)
if not isinstance(segmap, SegmentationImage):
segmap = SegmentationImage(segmap)

sources_morph = []
for label in segmap.labels:
Expand Down
1 change: 0 additions & 1 deletion statmorph/utils/image_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Licensed under a 3-Clause BSD License.

import numpy as np
import sys
import skimage.transform
import statmorph
from astropy.visualization import simple_norm
Expand Down

0 comments on commit 5550c00

Please sign in to comment.