fit_2dgaussian#
- photutils.psf.fit_2dgaussian(data, *, xypos=None, fwhm=None, fix_fwhm=True, fit_shape=None, mask=None, error=None)[source]#
Fit a 2D Gaussian model to one or more sources in an image.
This convenience function uses a
CircularGaussianPRFmodel to fit the sources using thePSFPhotometryclass.Non-finite 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.- fix_fwhmbool, optional
Whether to fix the FWHM of the Gaussian PSF model during the fitting process.
- fit_shapeint or tuple of two ints, optional
The shape of the fitting region. If a scalar, then it is assumed to be a square. If
None, then the shape of the inputdatawill be used.- 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:
- result
PSFPhotometry The PSF-fitting photometry results.
- result
See also
fit_fwhmFit the FWHM of one or more sources in an image.
Notes
The source(s) are fit with a
CircularGaussianPRFmodel using 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.Examples
Fit a 2D Gaussian model to an image containing only one source (e.g., a cutout image):
>>> import numpy as np >>> from photutils.psf import CircularGaussianPRF, fit_2dgaussian >>> 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) >>> fit = fit_2dgaussian(data, fix_fwhm=False, fit_shape=7) >>> phot_tbl = fit.results >>> cols = ['x_fit', 'y_fit', 'fwhm_fit', 'flux_fit'] >>> for col in cols: ... phot_tbl[col].info.format = '.4f' # optional format >>> print(phot_tbl[['id'] + cols]) id x_fit y_fit fwhm_fit flux_fit --- ------- ------- -------- -------- 1 22.1700 28.8700 3.1230 9.7000
Fit a 2D Gaussian model to multiple sources in an image:
>>> import numpy as np >>> from photutils.detection import DAOStarFinder >>> from photutils.psf import (CircularGaussianPRF, fit_2dgaussian, ... 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]) >>> finder = DAOStarFinder(0.1, 5) >>> finder_tbl = finder(data) >>> xypos = zip(sources['x_0'], sources['y_0']) >>> psfphot = fit_2dgaussian(data, xypos=xypos, fit_shape=7, ... fix_fwhm=False) >>> phot_tbl = psfphot.results >>> len(phot_tbl) 5
Here we show only a few columns of the photometry table:
>>> cols = ['x_fit', 'y_fit', 'fwhm_fit', 'flux_fit'] >>> for col in cols: ... phot_tbl[col].info.format = '.4f' # optional format >>> print(phot_tbl[['id'] + cols]) id x_fit y_fit fwhm_fit flux_fit --- ------- ------- -------- -------- 1 61.7787 74.6905 5.6947 147.9988 2 30.2017 27.5858 5.2138 123.2373 3 10.5237 82.3776 7.6551 180.1881 4 8.4214 12.0369 3.2026 192.3530 5 76.9412 35.9061 6.6600 126.6130