find_peaks#

photutils.detection.find_peaks(data, threshold, *, box_size=3, footprint=None, mask=None, border_width=None, n_peaks=inf, min_separation=None, centroid_func=None, error=None, wcs=None)[source]#

Find local peaks in an image that are above a specified threshold value.

Peaks are the maxima above the threshold within a local region. The local regions are defined by either the box_size or footprint parameters. box_size defines the local region around each pixel as a square box. footprint is a boolean array where True values specify the region shape.

If multiple pixels within a local region have identical intensities, then the coordinates of all such pixels are returned. Otherwise, there will be only one peak pixel per local region. Thus, the defined region effectively imposes a minimum separation between peaks unless there are identical peaks within the region.

When min_separation is set, a fast algorithm is used that finds local maxima within a circular region of the given radius. Pixels with identical intensities are then reduced so that all returned peaks are separated by more than min_separation pixels. When set, box_size and footprint are not used for peak detection.

If centroid_func is input, then it will be used to calculate a centroid within the defined local region centered on each detected peak pixel. In this case, the centroid will also be returned in the output table.

Parameters:
dataarray_like

The 2D array of the image. If data is a MaskedArray, its mask is combined with the input mask.

thresholdfloat, scalar Quantity or array_like

The data value or pixel-wise data values to be used for the detection threshold. A peak is detected only if it is strictly greater than the threshold. If data is a Quantity array, then threshold must have the same units as data. A 2D threshold must have the same shape as data. See detect_threshold for one way to create a threshold image.

box_sizescalar or tuple, optional

The size of the local region to search for peaks at every point in data. If box_size is a scalar, then the region shape will be (box_size, box_size). If box_size has two elements, they must be in (ny, nx) order. box_size must have odd values for both axes if centroid_func is input. Either box_size or footprint must be defined. If they are both defined, then footprint overrides box_size.

footprintndarray of bools, optional

A boolean array where True values describe the local footprint region within which to search for peaks at every point in data. box_size=(n, m) is equivalent to footprint=np.ones((n, m)). Either box_size or footprint must be defined. If they are both defined, then footprint overrides box_size.

maskarray_like, bool, optional

A boolean mask with the same shape as data, where a True value indicates the corresponding element of data is masked. Masked pixels can neither be detected as peaks nor suppress nearby peaks within the local region (they are treated the same as NaN pixels).

Changed in version 3.1: Masked pixels no longer suppress nearby peaks within the local region. Previously, a masked pixel with a value larger than a nearby true peak prevented that peak from being detected.

border_widthint, array_like of int, or None, optional

The width in pixels to exclude around the border of the data. If border_width is a scalar then border_width will be applied to all sides. If border_width has two elements, they must be in (ny, nx) order. If None, then no border is excluded. The border width values must be nonnegative integers.

n_peaksint or numpy.inf, optional

The maximum number of peaks to return. It must be a positive integer or numpy.inf (the default, meaning all peaks are returned). When the number of detected peaks exceeds n_peaks, the peaks with the highest peak intensities will be returned.

min_separationfloat or None, optional

The minimum allowed separation (in pixels) between detected peaks. When set to a positive value, a fast algorithm first identifies the local maxima within a circular region of this radius centered on each pixel. A group of connected peak pixels, which have identical values (e.g., a saturated star), is then replaced by its pixel nearest to the centroid of the group. Finally, only one of any remaining equal-valued peaks that are separated by min_separation or less is kept (see Notes). All returned peaks are therefore separated by more than min_separation pixels. Peaks separated by exactly min_separation are too close. This is much faster for large radii than using an explicit circular footprint of the given radius. When set to a positive value, box_size and footprint are not used for peak detection. If None (default) or 0, the peak detection uses box_size or footprint as specified. To return every pixel that is tied for the maximum, use a circular footprint instead.

Changed in version 3.1: Pixels with identical values that are within min_separation of each other now give only one peak. Previously, every pixel that was tied for the maximum was returned.

centroid_funccallable, optional

A callable object (e.g., function or class) that is used to calculate the centroid of a 2D array. The centroid_func must accept a 2D ndarray, have a mask keyword, and optionally an error keyword. The callable object must return two scalar values representing the (x, y) centroid.

errorarray_like, optional

The 2D array of the 1-sigma errors of the input data. error is used only if centroid_func is input (the error array is passed directly to the centroid_func). If the centroid_func does not accept an error keyword, the error array is ignored. If data is a Quantity array, then error must have the same units as data.

wcsNone or WCS object, optional

A world coordinate system (WCS) transformation that supports the astropy shared interface for WCS (e.g., astropy.wcs.WCS, gwcs.wcs.WCS). If None, then the sky coordinates will not be returned in the output QTable.

Returns:
outputQTable or None

A table containing the x and y pixel location of the peaks and their values, in the id, x_peak, y_peak, and peak_value columns. The table also contains a skycoord_peak column if wcs is input, x_centroid and y_centroid columns if centroid_func is input, and a skycoord_centroid column if both are input. If no peaks are found then None is returned.

Notes

By default, the returned pixel coordinates are the integer indices of the maximum pixel value within the input box_size or footprint (i.e., only the peak pixel is identified).

When min_separation is given, peaks are detected in three steps. First, the local maxima within a circular footprint of the given radius, centered on each pixel, are identified. For a radius of at least 4 pixels, the algorithm uses two fast O(N) separable box filters (inscribed and circumscribed squares of the circle) to classify most candidates, then verifies only the remaining few against the exact circular region. Smaller radii use the footprint filter directly.

The local-maximum step returns every pixel that is tied for the maximum, so the next two steps handle pixels with identical values. Second, each group of connected peak pixels (a plateau, e.g., the pixels of a saturated star) is replaced by its pixel nearest to the centroid of the group. A plateau therefore gives one peak, even if it is larger than min_separation. Adjacent peak pixels are connected only if they are within min_separation of each other. Diagonal neighbors are therefore connected only for a min_separation of at least sqrt(2) pixels, and this step is skipped for a min_separation smaller than 1 pixel, for which every pixel above the threshold is a peak.

Third, for any remaining equal-valued peaks that are separated by min_separation or less, only one is kept. This includes the peak of a plateau, which can be removed in favor of a nearby equal-valued peak. The peak of the plateau with the most pixels is kept, so that a saturated star is preferred over a nearby isolated saturated pixel. For plateaus of equal size, including single pixels, the first peak in row-major order (ascending y, then x) is kept.

A centroiding function can be input via the centroid_func keyword to compute centroid coordinates with subpixel precision within the input box_size or footprint. Note that when min_separation is used, the centroid region is determined by footprint if it is input and otherwise by box_size (default 3), not by min_separation.

The peak detection uses mode='constant' with cval=0.0 for maximum_filter, which means pixels outside the image boundary are treated as zero. For images with all-negative values, this may suppress legitimate peaks near the borders.

Masked pixels and any NaN values in the input data are replaced with the minimum non-NaN data value before peak detection, so they can neither be detected as peaks nor suppress nearby peaks. NaN pixels are automatically excluded from the results.

The output column names (x_peak, y_peak, peak_value) differ from the star finder classes (e.g., DAOStarFinder), which use x_centroid, y_centroid, and flux.