PSFPhotometry#

class photutils.psf.PSFPhotometry(psf_model, fit_shape, *, finder=None, grouper=None, fitter=None, fitter_maxiters=100, xy_bounds=None, aperture_radius=None, local_bkg_estimator=None, group_warning_threshold=25, n_threads=1, progress_bar=False)[source]#

Bases: object

Class to perform PSF photometry.

This class implements a flexible PSF photometry algorithm that can find sources in an image, group overlapping sources, fit the PSF model to the sources, and subtract the fit PSF models from the image.

Parameters:
psf_model2D astropy.modeling.Model

The PSF model to fit to the data. The model must have parameters named x_0, y_0, and flux, corresponding to the center (x, y) position and flux, or it must have ‘x_name’, ‘y_name’, and ‘flux_name’ attributes that map to the x, y, and flux parameters. The model must be two-dimensional such that it accepts 2 inputs (e.g., x and y) and provides 1 output.

The model is evaluated at the center of each pixel, and the result is compared with the pixel value. The value of the model at a position must therefore be the flux in a pixel centered at that position, as for the analytic models whose names end in PRF and for an image-based model made from an ePSF (see Terminology). A model that is not integrated over the pixels gives biased fluxes and positions for undersampled data.

The analytic models whose names end in PSF (e.g., CircularGaussianPSF and MoffatPSF) are not integrated over the pixels and should not be used here. Each has a counterpart whose name ends in PRF (e.g., CircularGaussianPRF and MoffatPRF). With a PSF model whose shape parameters are fixed, the fitted flux is biased by a few percent even for a FWHM of 2 to 3 pixels.

The fitted source positions are the fitted values of the model position parameters. For an image-based model, they give the position of the center of the model image, or of the origin of an ImagePSF. The PSF must therefore be centered on that point of the model image. Otherwise, the fitted positions are all offset by the distance of the PSF center from that point.

fit_shapeint or length-2 array_like

The rectangular shape around the initial source position that will be used to define the PSF-fitting data. If fit_shape is a scalar then a square shape of size fit_shape will be used. If fit_shape has two elements, they must be in (ny, nx) order. Each element of fit_shape must be a positive odd number. In general, fit_shape should be set to a small size (e.g., (5, 5)) that covers the region with the highest flux signal-to-noise.

findercallable or StarFinderBase or None, optional

A callable used to identify sources in an image. The finder must accept a 2D image as its first argument and a mask keyword, and return a Table containing the x and y centroid positions (or None if no sources are found). These positions are used as the starting points for the PSF fitting. The allowed x column names are (same suffix for y): 'x_init', 'xinit', 'x', 'x_0', 'x0', 'xcentroid', 'x_centroid', 'x_peak', 'xcen', 'x_cen', 'xpos', 'x_pos', 'x_fit', and 'xfit'. If None, then the initial (x, y) model positions must be input using the init_params keyword when calling the class. The (x, y) values in init_params override this keyword. If this class is run on an image that has units (i.e., a Quantity array), then certain finder keywords (e.g., threshold) must have the same units. Please see the documentation for the specific finder class for more information.

grouperSourceGrouper or callable or None, optional

A callable used to group sources. Typically, grouped sources are those that overlap with their neighbors. Sources that are grouped are fit simultaneously. The grouper must accept the x and y coordinates of the sources and return an integer array of the group ID numbers (starting from 1) indicating the group in which a given source belongs. If None, then no grouping is performed, i.e., each source is fit independently. The group_id values in init_params override this keyword. A warning is raised if any group size is larger than group_warning_threshold sources.

fitterFitter or None, optional

The fitter object used to perform the fit of the model to the data. If None, then the default astropy.modeling.fitting.TRFLSQFitter is used.

fitter_maxitersint, optional

The maximum number of iterations in which the fitter is called for each source. The value can be increased if the fit is not converging for sources. This parameter is passed to the fitter if it supports the maxiter parameter and ignored otherwise. In the latter case, a warning is issued when the class is created.

xy_boundsNone, float, or 2-tuple of float, optional

The maximum distance in pixels that a fitted source can be from the initial (x, y) position. If a single float, then the same maximum distance is used for both x and y. If a 2-tuple of floats, then the distances are in (x, y) order. If None, then no bounds are applied. Either value can also be None to indicate no bound along that axis.

aperture_radiusfloat or None, optional

The radius of the circular aperture used to estimate the initial flux of each source. If None, then the initial flux values must be provided in the init_params table. The aperture radius must be a strictly positive scalar. If initial flux values are present in the init_params table, they will override this keyword.

local_bkg_estimatorLocalBackground or None, optional

The object used to estimate the local background around each source. If None, then no local background is subtracted. The local_bkg values in init_params override this keyword. This option should be used with care, especially in crowded fields where the fit_shape of sources overlap (see Notes below).

group_warning_thresholdint, optional

The maximum number of sources in a group before a warning is raised. If the number of sources in a group exceeds this value, a warning is raised to inform the user that fitting such large groups may take a long time and be error-prone. The default is 25 sources.

n_threadsint, optional

The number of threads used to fit the sources. The default is 1 (no multithreading). When n_threads > 1, the source groups are divided into chunks that are fitted concurrently. Each group is fitted independently, so the results are identical to the single-threaded computation. Every thread fits with its own copy of the PSF model and the fitter, so the input fitter is not called and its fit_info is not updated. The fitting runs mostly in Python code that holds the global interpreter lock (GIL), so multithreading speeds up the fitting only on a free-threaded Python build. When the GIL is enabled, a warning is issued and the sources are fitted in a single thread.

progress_barbool, optional

Whether to display a progress bar when fitting the sources (or groups). The progress bar requires that the tqdm optional dependency be installed.

Attributes:
resultsQTable or None

The table of fit results from the most recent call, or None if the instance has not yet been run. The rows are sorted by the source id column.

fit_infolist of dict

A list of dictionaries, one per source in the same row order as results, containing information about each source fit with keys such as param_cov, ierr, message, and status (the exact keys depend on the fitter).

finder_resultsTable or None

The table of sources returned by the finder during the most recent call, or None if the finder was not used.

init_paramsTable or None

The table of initial parameters used in the most recent call, or None if the instance has not yet been run.

data_unitUnit or None

The unit of the input data from the most recent call, or None if the data did not have units.

Notes

The data that will be fit for each source is defined by the fit_shape parameter. A cutout will be made around the initial center of each source with a shape defined by fit_shape. The PSF model will be fit to the data in this region. The cutout region that is fit does not shift if the source center shifts during the fit iterations. Therefore, the initial source positions should be close to the true source positions. One way to ensure this is to use a finder to identify sources in the data.

If the fitted positions are significantly different from the initial positions, one can rerun the PSFPhotometry class using the fit results as the input init_params, which will change the fitted cutout region for each source. After running PSFPhotometry, you can use the results_to_init_params method to generate a table of initial parameters that can be used in a subsequent call to PSFPhotometry. This table will contain the fitted (x, y) positions, fluxes, and any other model parameters that were fit.

If the fitted model parameters are NaN, then the source was not valid, likely due to not enough valid data pixels in the fit_shape region. The flags column in the output results table indicates the reason why a source was not valid.

If the fitted model parameter errors are NaN, then either the fit did not converge, the model parameter was fixed, or the input fitter did not return parameter errors. For the latter case, one can try a different Astropy fitter that returns parameter errors.

The local background value around each source is optionally estimated using the local_bkg_estimator or obtained from the local_bkg column in the input init_params table. This local background is then subtracted from the data over the fit_shape region for each source before fitting the PSF model. For sources where their fit_shape regions overlap, the local background will effectively be subtracted twice in the overlapping fit_shape regions, even if the source grouper is input. This is not an issue if the sources are well-separated. However, for crowded fields, please use the local_bkg_estimator (or local_bkg column in init_params) with care.

Care should be taken in defining the source groups. Simultaneously fitting very large source groups is computationally expensive and error-prone, because the number of fitted parameters grows with the group size. A warning will be raised if the number of sources in a group exceeds the group_warning_threshold value.

This class stores per-call state on the instance (e.g., results and fit_info), so a single instance must not be called concurrently from multiple threads. Create one instance per thread for concurrent use. Sharing a single Astropy fitter instance across concurrently-used objects is also unsafe because Astropy fitters store fit_info on themselves.

Attributes Summary

Methods Summary

__call__(data, *[, mask, error, init_params])

Perform PSF photometry.

decode_flags([return_bit_values])

Decode the PSF photometry flags from the results table.

make_model_image(shape, *[, psf_shape, ...])

Create a 2D image from the fit PSF models and optional local background.

make_residual_image(data, *[, psf_shape, ...])

Create a 2D residual image from the fit PSF models and local background.

results_to_init_params(*[, remove_invalid, ...])

Create a table of initial parameters from the fitted results.

results_to_model_params(*[, remove_invalid, ...])

Create a table of the fitted model parameters from the results.

Attributes Documentation

fit_params#

Deprecated since version 2.3.0: The fit_params function is deprecated and may be removed in a future version. Use results instead.

The table of fit parameters and their errors.

This table is a subset of the results table, containing only the fit parameters and their errors. It can be used as the init_params for subsequent PSFPhotometry fits.

Methods Documentation

__call__(data, *, mask=None, error=None, init_params=None)[source]#

Perform PSF photometry.

Parameters:
data2D ndarray, Quantity, or NDData

The 2D array on which to perform photometry. Invalid data values (i.e., NaN or inf) are automatically masked. If an NDData object is input, its data, mask, uncertainty, and unit attributes are used, and the mask and error keywords must not be set.

mask2D bool ndarray, optional

A boolean mask with the same shape as data, where a True value indicates the corresponding element of data is masked. Nonboolean mask arrays are coerced with astype, so any nonzero value is treated as masked.

error2D ndarray, optional

The pixel-wise 1-sigma errors of the input data. error is assumed to include all sources of error, including the Poisson error of the sources. error must have the same shape as the input data. If data is a Quantity array, then error must also be a Quantity array with the same units. The error values of the unmasked pixels that are fit must be finite and strictly positive. Otherwise, a ValueError is raised.

init_paramsTable or None, optional

A table containing the initial guesses of the model parameters (e.g., x, y, flux) for each source. If input, the table must include the initial x and y values. If None, the finder keyword must be defined and is used to find the sources. If the initial flux values are not included, then the aperture_radius keyword must be defined to measure the initial flux values. Note that the initial flux values refer to the model flux parameters and are not corrected for local background values (computed using local_bkg_estimator or input in a local_bkg column). The allowed column names are:

  • 'x_init', 'xinit', 'x', 'x_0', 'x0', 'xcentroid', 'x_centroid', 'x_peak', 'xcen', 'x_cen', 'xpos', 'x_pos', 'x_fit', and 'xfit'.

  • 'y_init', 'yinit', 'y', 'y_0', 'y0', 'ycentroid', 'y_centroid', 'y_peak', 'ycen', 'y_cen', 'ypos', 'y_pos', 'y_fit', and 'yfit'.

  • 'flux_init', 'fluxinit', 'flux', 'flux_0', 'flux0', 'flux_fit', 'fluxfit', 'source_sum', 'segment_flux', and 'kron_flux'.

  • If the PSF model has additional free parameters that are fit, they can be included in the table. The column names must match the parameter names in the PSF model. They can also be suffixed with either the '_init' or '_fit' suffix. The suffix search order is '_init', '' (no suffix), and '_fit'. For example, if the PSF model has an additional parameter named “sigma”, then the allowed column names are: 'sigma_init', 'sigma', and 'sigma_fit'. If the column name is not found in the table, then the default value from the PSF model will be used.

The parameter names are searched in the input table in the above order, stopping at the first match.

If data is a Quantity array, then the initial flux values in this table must also have compatible units.

The table can also have 'group_id' and 'local_bkg' columns. If 'group_id' is input, the values will be used and grouper keyword will be ignored. If local_bkg is input, those values will be used and the local_bkg_estimator will be ignored. If data has units, then the local_bkg values must have compatible units.

The table can also have an 'id' column of unique source identification numbers. If it is not input, the sources are numbered from 1 in row order. The output table is sorted by 'id'.

Returns:
tableQTable or None

An astropy table with the PSF-fitting results. None is returned if no sources are found. If data has units, the 'local_bkg' and flux columns have the same units. The columns are ordered as listed below, except that all of the _init columns come first, followed by all of the _fit columns and then all of the _err columns. The table will contain the following columns:

  • 'id' : unique identification number for the source

  • 'group_id' : unique identification number for the source group

  • 'group_size' : the total number of sources in the group. This number includes sources that are in the group, but were not fit due to being masked, having no overlap with the input data, or having too few pixels for a fit.

  • 'local_bkg' : the local background value subtracted from the data before the fit (zero if no local background was used)

  • 'x_init', 'x_fit', 'x_err' : the initial, fit, and error of the source x center

  • 'y_init', 'y_fit', 'y_err' : the initial, fit, and error of the source y center

  • 'flux_init', 'flux_fit', 'flux_err' : the initial, fit, and error of the source flux

  • '<name>_init', '<name>_fit', '<name>_err' : the initial, fit, and error of any additional free model parameter, where <name> is the parameter name

  • 'n_pixels_fit' : the number of unmasked pixels used to fit the source

    Deprecated since version 3.0: The 'npixfit' column name is deprecated and will be removed in version 4.0. Use 'n_pixels_fit' instead.

  • 'qfit' : a quality-of-fit metric defined as the sum of the absolute value of the fit residuals divided by the fit flux. 'qfit' is zero for sources that are perfectly fit by the PSF model.

  • 'cfit' : a quality-of-fit metric defined as the fit residual (data - model) in the initial central pixel value divided by the fit flux. NaN values indicate that the central pixel was masked or outside the image, or that the source was not fit. Large positive values indicate sources that are sharper than the PSF model (e.g., cosmic ray, hot pixel, etc.). Large negative values indicate sources that are broader than the PSF model.

  • 'reduced_chi2' : the reduced chi-squared statistic. If no error array is provided, 'reduced_chi2' values will be NaN.

  • 'flags' : The bitwise quality flags for the source. A warning is issued if one or more fits may not have converged. See decode_psf_flags for decoding flag values. The flags are:

    • 0 : No flags set.

    • 1 ('n_pixels_fit_partial') : The number of fitted pixels (n_pixels_fit) is smaller than the full fit_shape region, indicating partial PSF fitting.

    • 2 ('position_outside_image') : The fitted source position is outside the bounds of the input image.

    • 4 ('non_positive_flux') : The fitted flux value is negative or zero, which is nonphysical.

    • 8 ('no_convergence') : The PSF fitting algorithm may not have converged to a stable solution.

    • 16 ('no_covariance') : Parameter covariance matrix is not available, preventing error estimation.

    • 32 ('parameter_near_bound') : One or more fitted parameters are very close to their imposed bounds.

    • 64 ('no_overlap') : The source fitting region has no overlap with the input image.

    • 128 ('fully_masked') : All pixels in the source fitting region are masked.

    • 256 ('too_few_pixels') : Insufficient unmasked pixels available for reliable PSF fitting.

    • 512 ('non_finite_position') : The fitted x or y position is NaN or inf, indicating an invalid or failed fit.

    • 1024 ('non_finite_flux') : The fitted flux value is NaN or inf, indicating an invalid or failed fit.

    • 2048 ('non_finite_local_bkg') : The local background value is NaN or inf, so it was not subtracted before fitting.

    • 4096 ('position_outside_fit_region') : The fitted source position is outside the region of fitted data, which is the fit_shape box around the initial position, trimmed to the input image. The fit is constrained only by the PSF wings, often because it moved toward a neighboring source. For grouped fits, the region is the box of each source, not the union of the boxes of the group.

Notes

The qfit and cfit metrics are equivalent to the q and C fit metrics defined by the HST PSF photometry hst1pass software. They are also similar to the chi (qfit) and sharp (cfit) metrics used by DAOPHOT.

decode_flags(return_bit_values=False)[source]#

Decode the PSF photometry flags from the results table.

This is a convenience method that calls decode_psf_flags with the ‘flags’ column from the results table.

Parameters:
return_bit_valuesbool, optional

If True, return the decoded bit flags (integers) instead of the flag names (strings). Default is False.

Returns:
decodeddict

A dictionary mapping each source id from the results table to the list of its active flag names (or bit values). If no flags are set for a source, its value is an empty list. The entries follow the results-table order.

Changed in version 3.1: The result is now a dictionary keyed by source id. Previously, a positional list of lists was returned.

Raises:
ValueError

If no results are available. Please run the PSFPhotometry instance first.

Examples

Decode flags from PSF photometry results:

>>> import numpy as np
>>> from astropy.table import Table
>>> from photutils.psf import CircularGaussianPRF, PSFPhotometry
>>> yy, xx = np.mgrid[:21, :21]
>>> psf_model = CircularGaussianPRF(flux=1, x_0=10, y_0=10, fwhm=2)
>>> # Create a source with negative flux to trigger a flag
>>> m1 = CircularGaussianPRF(flux=100, x_0=10, y_0=10, fwhm=2)
>>> m2 = CircularGaussianPRF(flux=-50, x_0=5, y_0=5, fwhm=2)
>>> data = m1(xx, yy) + m2(xx, yy)
>>> init_params = Table({'x': [10, 5], 'y': [10, 5],
...                      'flux': [100, 100]})
>>> photometry = PSFPhotometry(psf_model, (3, 3))
>>> results = photometry(data, init_params=init_params)
>>> for source_id, flags in photometry.decode_flags().items():
...     print(f'Source {source_id}: {flags}')
Source 1: []
Source 2: ['non_positive_flux']
make_model_image(shape, *, psf_shape=None, include_local_bkg=False)[source]#

Create a 2D image from the fit PSF models and optional local background.

Parameters:
shape2-tuple of int

The shape of the output array.

psf_shapeint, 2-tuple of int, or None, optional

The shape of the region around the center of the fit model to render in the output image. If psf_shape is a scalar integer, then a square shape of size psf_shape will be used. If None, then the bounding box of the model will be used. This keyword must be specified if the model does not have a bounding_box attribute.

include_local_bkgbool, optional

Whether to include the local background in the rendered output image. Note that the local background level is included around each source over the region defined by psf_shape. Thus, regions where the psf_shape of sources overlap will have the local background added multiple times. Nonfinite local background values (NaN or inf) are treated as zero and not included in the output image.

Returns:
array2D ndarray

The rendered image from the fit PSF models. If the fit was performed on data with units, the image will be a Quantity with the same units.

Raises:
ValueError

If no results are available because the instance has not been run or no sources were found.

make_residual_image(data, *, psf_shape=None, include_local_bkg=False)[source]#

Create a 2D residual image from the fit PSF models and local background.

Parameters:
data2D ndarray, Quantity, or NDData

The 2D array on which photometry was performed. This should be the same array input when calling the PSF-photometry class.

psf_shapeint, 2-tuple of int, or None, optional

The shape of the region around the center of the fit model to subtract. If psf_shape is a scalar integer, then a square shape of size psf_shape will be used. If None, then the bounding box of the model will be used. This keyword must be specified if the model does not have a bounding_box attribute.

include_local_bkgbool, optional

Whether to include the local background in the subtracted model. Note that the local background level is subtracted around each source over the region defined by psf_shape. Thus, regions where the psf_shape of sources overlap will have the local background subtracted multiple times. Nonfinite local background values (NaN or inf) are not subtracted from the residual image.

Returns:
array2D ndarray, Quantity, or NDData

The residual image of the data minus the fit PSF models minus the optional local_bkg. The output has the same type as the input data. If data is an NDData object, a copy is returned with the residual image as its data.

Raises:
ValueError

If no results are available because the instance has not been run or no sources were found.

results_to_init_params(*, remove_invalid=True, reset_ids=True)[source]#

Create a table of initial parameters from the fitted results.

The table columns are named according to those expected for the initial parameters table. It can be used as the init_params for subsequent PSFPhotometry fits.

Parameters:
remove_invalidbool, optional

If True, rows that contain nonfinite fitted values are removed.

reset_idsbool, optional

If True, the ‘id’ column will be reset to a sequential numbering starting from 1. If False, the ‘id’ column will remain unchanged from the results table. This option is ignored if remove_invalid is False.

Returns:
init_params_tblQTable or None

The table of initial parameters, or None if the instance has not yet been run.

results_to_model_params(*, remove_invalid=True, reset_ids=True)[source]#

Create a table of the fitted model parameters from the results.

The table columns are named according to the PSF model parameter names. It can also be used to reconstruct the fitted PSF models for visualization or further analysis.

Parameters:
remove_invalidbool, optional

If True, rows that contain nonfinite fitted values are removed.

reset_idsbool, optional

If True, the ‘id’ column will be reset to a sequential numbering starting from 1. If False, the ‘id’ column will remain unchanged from the results table. This option is ignored if remove_invalid is False.

Returns:
model_params_tblQTable or None

The table of model parameters, or None if the instance has not yet been run.