SourceFinder#
- class photutils.segmentation.SourceFinder(n_pixels, *, connectivity=8, deblend=True, n_levels=32, contrast=0.001, contrast_method=None, mode='exponential', relabel=True, n_threads=1, nproc=1, n_processes=1, progress_bar=True)[source]#
Bases:
objectClass to detect sources, including deblending, in an image using segmentation.
This is a convenience class that combines the functionality of
detect_sourcesanddeblend_sources.Sources are deblended using a combination of multithresholding and watershed segmentation. In order to deblend sources, they must be separated enough that there is a saddle point between them.
- Parameters:
- n_pixelsint or array_like of 2 int
The minimum number of connected pixels, each greater than a specified threshold, that an object must have to be detected. If
n_pixelsis an integer, then the value will be used for both source detection and deblending (which internally uses source detection at multiple thresholds). Ifn_pixelscontains two values, then the first value will be used for source detection and the second value used for source deblending.n_pixelsvalues must be positive integers.- connectivity{4, 8}, optional
The type of pixel connectivity used in determining how pixels are grouped into a detected source. The options are 4 or 8 (default). 4-connected pixels touch along their edges. 8-connected pixels touch along their edges or corners.
- deblendbool, optional
Whether to deblend overlapping sources.
- n_levelsint, optional
The number of multithresholding levels to use for deblending. Each source will be rethresholded at
n_levelslevels spaced between its minimum and maximum values (noninclusive). Themodekeyword determines how the levels are spaced. This keyword is ignored unlessdeblend=True.- contrastfloat, optional
The fraction of the total source flux that a local peak must have (at any one of the multithresholds) to be deblended as a separate object.
contrastmust be between 0 and 1, inclusive. Ifcontrast=0then every local peak will be made a separate object (maximum deblending). Ifcontrast=1then no deblending will occur. The default is 0.001, which will deblend sources with a 7.5 magnitude difference. This keyword is ignored unlessdeblend=True.- contrast_method{
None, ‘basin’, ‘saddle’}, optional The flux used by the contrast criterion. For
'basin', the fraction is the total flux in the source’s watershed basin. For'saddle', the fraction is the flux the source holds above the saddle level where it separates from its neighbors. IfNone(default), the'basin'method is currently used. The default may change to'saddle'in version 4.0. Seedeblend_sources()for details. This keyword is ignored unlessdeblend=True.- mode{‘exponential’, ‘linear’, ‘sinh’}, optional
The mode used in defining the spacing between the multithresholding levels (see the
n_levelskeyword) during deblending. The'exponential'and'sinh'modes have more threshold levels near the source minimum and fewer near the source maximum. The'linear'mode evenly spaces the threshold levels between the source minimum and maximum. The'exponential'and'sinh'modes differ in that the'exponential'levels are dependent on the source maximum/minimum ratio (smaller ratios are more linear and larger ratios are more exponential), while the'sinh'levels are not. Unlike the'exponential'mode, the'sinh'and'linear'modes are well defined for sources with nonpositive minimum data values. For such sources, the'exponential'mode will be changed to'sinh'. This keyword is ignored unlessdeblend=True.- relabelbool, optional
If
True(default), then the segmentation image will be relabeled after deblending such that the labels are in consecutive order starting from 1. This keyword is ignored unlessdeblend=True.- n_threadsint, optional
The number of threads to use to deblend the sources. The default is 1 (no multithreading). When
n_threads> 1, the sources are divided into chunks and processed concurrently, producing results identical to the single-threaded computation. This keyword is ignored unlessdeblend=True.- nprocint, optional
This keyword is deprecated and has no effect. It was the name of the
n_processeskeyword before version 3.0.Deprecated since version 3.0: The
nprockeyword is deprecated and will be removed in version 4.0.- n_processesint, optional
This keyword is deprecated and has no effect. Multiprocessing no longer provides any benefit for source deblending. Use the
n_threadskeyword instead.Deprecated since version 3.1: The
n_processeskeyword is deprecated and will be removed in version 4.0.- progress_barbool, optional
This keyword is deprecated and has no effect. Deblending no longer displays a progress bar.
Deprecated since version 3.1: The
progress_barkeyword is deprecated and will be removed in version 4.0.
Examples
import matplotlib.pyplot as plt from astropy.convolution import convolve from astropy.visualization import simple_norm from photutils.background import Background2D, MedianBackground from photutils.datasets import make_100gaussians_image from photutils.segmentation import (SourceFinder, make_2dgaussian_kernel) # Make a simulated image data = make_100gaussians_image() # Estimate the background using Background2D and subtract it bkg_estimator = MedianBackground() bkg = Background2D(data, (50, 50), filter_size=(3, 3), bkg_estimator=bkg_estimator) data -= bkg.background # subtract the background # Convolve the data kernel = make_2dgaussian_kernel(3.0, size=5) convolved_data = convolve(data, kernel) # Detect the sources threshold = 1.5 * bkg.background_rms # per-pixel detection threshold finder = SourceFinder(n_pixels=10) segment_map = finder(convolved_data, threshold) # Plot the image and the segmentation image fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(8, 10)) norm = simple_norm(data, 'sqrt', percent=99.5) ax1.imshow(data, norm=norm, origin='lower') segment_map.imshow(ax=ax2) fig.tight_layout()
(
Source code,png,hires.png,pdf,svg)
Methods Summary
__call__(data, threshold[, mask])Detect sources, including deblending, in an image using segmentation.
Methods Documentation
- __call__(data, threshold, mask=None)[source]#
Detect sources, including deblending, in an image using segmentation.
- Parameters:
- data2D
ndarrayorMaskedArray The 2D array from which to detect sources. Typically, this array should be an image that has been convolved with a smoothing kernel. If
datais aMaskedArray, its mask is combined with the inputmask.- threshold2D
ndarrayor float The data value or pixel-wise data values (as an array) to be used as the per-pixel detection threshold. If
datais aQuantityarray, thenthresholdmust have the same units asdata. A 2Dthresholdarray must have the same shape asdata. A warning is emitted if the threshold is negative (or, for an array, has any negative values), because pixels with zero or negative values can then be included in the detected sources.- mask2D bool
ndarray, optional A boolean mask with the same shape as
data, where aTruevalue indicates the corresponding element ofdatais masked. Masked pixels will not be included in any source.
- data2D
- Returns:
- segment_image
SegmentationImageorNone A 2D segmentation image, with the same shape as the input data, where sources are marked by different positive integer values. A value of zero is reserved for the background. If no sources are found then
Noneis returned.
- segment_image
- Warns:
- NoDetectionsWarning
If no sources are found.
- AstropyUserWarning
If
thresholdis negative or, for an array, has any negative values.- DeblendWarning
If
deblend=Trueand the deblending mode for one or more sources was changed frommodeto “sinh” due to nonpositive minimum data values or to “linear” due to too many potential deblended sources.