fit_fwhm#
- photutils.psf.fit_fwhm(data, *, xypos=None, fwhm=None, fit_shape=None, mask=None, error=None)[source]#
Fit the FWHM of one or more sources in an image.
This convenience function uses a
CircularGaussianPRFmodel to fit the sources using thePSFPhotometryclass.Nonfinite values (e.g., NaN or inf) in the
dataarray are automatically masked.- Parameters:
- data2D array
The 2D array of the image. The input array must be background subtracted.
- xyposarray-like, optional
The initial (x, y) pixel coordinates of the sources as a list of tuples or a 2D array. If
None, then one source will be fit with an initial position using the center-of-mass centroid of thedataarray.- fwhmfloat, optional
The initial guess for the FWHM of the Gaussian PSF model. If
None, then the initial guess is half the mean of the x and y sizes of thefit_shapevalues.- fit_shapeint or tuple of two ints, optional
The shape of the fitting region. Each element must be a positive odd number. If a scalar, then it is assumed to be a square. If
None, then the shape of the inputdatais used, with any even dimension reduced by one pixel, and a warning is issued.fit_shapemust be given when fitting more than one source.- maskarray-like (bool), optional
A boolean mask with the same shape as the input
data, where aTruevalue indicates the corresponding element ofdatais masked.- error2D array, optional
The pixel-wise Gaussian 1-sigma errors of the input
data.erroris assumed to include all sources of error, including the Poisson error of the sources (seecalc_total_error).errormust have the same shape as the inputdata. If aQuantityarray, thendatamust also be aQuantityarray with the same units.
- Returns:
- fwhm
ndarray The FWHM of the sources. The fitted FWHM values are constrained to be nonnegative. The value is NaN for a source that could not be fit (e.g., a fully masked source).
- fwhm
- Warns:
AstropyUserWarningIf one or more fits may not have converged, or if
fit_shapeisNone.
See also
fit_2dgaussianFit a 2D Gaussian model to one or more sources in an image.
Notes
The source(s) are fit using the
fit_2dgaussian()function, which uses aCircularGaussianPRFmodel with thePSFPhotometryclass. The initial guess for the flux is the sum of the pixel values within the fitting region. IffwhmisNone, then the initial guess for the FWHM is half the mean of the x and y sizes of thefit_shapevalues.The
CircularGaussianPRFmodel is a Gaussian that is integrated over the pixels. The returned FWHM is the FWHM of the Gaussian before that integration, which is smaller than the FWHM of the pixelated image of the source. The integration over a pixel adds 1/12 pixel**2 to the variance of the profile, so the FWHM of the pixelated image is approximatelysqrt(fwhm**2 + 8 * ln(2) / 12), orsqrt(fwhm**2 + 0.462), in pixels. The two widths differ by about 3% for a FWHM of 3 pixels, 6% for 2 pixels, and 21% for 1 pixel.The fitter warnings are suppressed during the fits using the process-global warning filters, so warnings raised by other threads while this function runs may also be suppressed on non-free-threaded Python builds.
Examples
Fit the FWHM of a single source (e.g., a cutout image):
>>> import numpy as np >>> from photutils.psf import CircularGaussianPRF, fit_fwhm >>> yy, xx = np.mgrid[:51, :51] >>> model = CircularGaussianPRF(x_0=22.17, y_0=28.87, fwhm=3.123, flux=9.7) >>> data = model(xx, yy) >>> fwhm = fit_fwhm(data, fit_shape=7) >>> fwhm array([3.123])
Fit the FWHMs of multiple sources in an image:
>>> import numpy as np >>> from photutils.psf import (CircularGaussianPRF, fit_fwhm, ... make_psf_model_image) >>> model = CircularGaussianPRF() >>> data, sources = make_psf_model_image((100, 100), model, 5, ... min_separation=25, ... model_shape=(15, 15), ... flux=(100, 200), fwhm=[3, 8]) >>> xypos = zip(sources['x_0'], sources['y_0']) >>> fwhms = fit_fwhm(data, xypos=xypos, fit_shape=7) >>> fwhms array([5.69467204, 5.21376414, 7.65508658, 3.20255356, 6.66003098])