DynamicDetectionTask¶
-
class
lsst.meas.algorithms.
DynamicDetectionTask
(*args, **kwargs)¶ Bases:
lsst.meas.algorithms.SourceDetectionTask
Detection of sources on an image with a dynamic threshold
We first detect sources using a lower threshold than normal (see config parameter
prelimThresholdFactor
) in order to identify good sky regions (configurableskyObjects
). Then we perform forced PSF photometry on those sky regions. Using those PSF flux measurements and estimated errors, we set the threshold so that the stdev of the measurements matches the median estimated error.Besides the usual initialisation of configurables, we also set up the forced measurement which is deliberately not represented in this Task’s configuration parameters because we’re using it as part of the algorithm and we don’t want to allow it to be modified.
Methods Summary
applyTempLocalBackground
(exposure, middle, …)Apply a temporary local background subtraction applyThreshold
(middle, bbox[, factor])Apply thresholds to the convolved image calculateKernelSize
(sigma)Calculate size of smoothing kernel calculateThreshold
(exposure, seed[, sigma])Calculate new threshold clearMask
(mask)Clear the DETECTED and DETECTED_NEGATIVE mask planes clearUnwantedResults
(mask, results)Clear unwanted results from the Struct of results convolveImage
(maskedImage, psf[, doSmooth])Convolve the image with the PSF detectFootprints
(exposure[, doSmooth, …])Detect footprints with a dynamic threshold display
(exposure, results[, convolvedImage])Display detections if so configured emptyMetadata
()Empty (clear) the metadata for this Task and all sub-Tasks. finalizeFootprints
(mask, results, sigma[, …])Finalize the detected footprints getAllSchemaCatalogs
()Get schema catalogs for all tasks in the hierarchy, combining the results into a single dict. getFullMetadata
()Get metadata for all tasks. getFullName
()Get the task name as a hierarchical name including parent task names. getName
()Get the name of the task. getPsf
(exposure[, sigma])Retrieve the PSF for an exposure getSchemaCatalogs
()Get the schemas generated by this task. getTaskDict
()Get a dictionary of all tasks as a shallow copy. makeField
(doc)Make a lsst.pex.config.ConfigurableField
for this task.makeSubtask
(name, **keyArgs)Create a subtask as a new instance as the name
attribute of this task.makeThreshold
(image, thresholdParity[, factor])Make an afw.detection.Threshold object corresponding to the task’s configuration and the statistics of the given image. reEstimateBackground
(maskedImage, backgrounds)Estimate the background after detection run
(table, exposure[, doSmooth, sigma, …])Run source detection and create a SourceCatalog of detections. setEdgeBits
(maskedImage, goodBBox, edgeBitmask)Set the edgeBitmask bits for all of maskedImage outside goodBBox tempWideBackgroundContext
(exposure)Context manager for removing wide (large-scale) background timer
(name, logLevel)Context manager to log performance data for an arbitrary block of code. tweakBackground
(exposure, bgLevel[, bgList])Modify the background by a constant value updatePeaks
(fpSet, image, threshold)Update the Peaks in a FootprintSet by detecting new Footprints and Peaks in an image and using the new Peaks instead of the old ones. Methods Documentation
-
applyTempLocalBackground
(exposure, middle, results)¶ Apply a temporary local background subtraction
This temporary local background serves to suppress noise fluctuations in the wings of bright objects.
Peaks in the footprints will be updated.
Parameters: - exposure :
lsst.afw.image.Exposure
Exposure for which to fit local background.
- middle :
lsst.afw.image.MaskedImage
Convolved image on which detection will be performed (typically smaller than
exposure
because the half-kernel has been removed around the edges).- results :
lsst.pipe.base.Struct
Results of the ‘detectFootprints’ method, containing positive and negative footprints (which contain the peak positions that we will plot). This is a
Struct
withpositive
andnegative
elements that are of typelsst.afw.detection.FootprintSet
.
- exposure :
-
applyThreshold
(middle, bbox, factor=1.0)¶ Apply thresholds to the convolved image
Identifies ``Footprint``s, both positive and negative.
The threshold can be modified by the provided multiplication
factor
.Parameters: - middle :
lsst.afw.image.MaskedImage
Convolved image to threshold.
- bbox :
lsst.geom.Box2I
Bounding box of unconvolved image.
- factor :
float
Multiplier for the configured threshold.
- middle :
-
calculateKernelSize
(sigma)¶ Calculate size of smoothing kernel
Uses the
nSigmaForKernel
configuration parameter. Note that that is the full width of the kernel bounding box (so a value of 7 means 3.5 sigma on either side of center). The value will be rounded up to the nearest odd integer.Parameters: - sigma :
float
Gaussian sigma of smoothing kernel.
Returns: - size :
int
Size of the smoothing kernel.
- sigma :
-
calculateThreshold
(exposure, seed, sigma=None)¶ Calculate new threshold
This is the main functional addition to the vanilla
SourceDetectionTask
.We identify sky objects and perform forced PSF photometry on them. Using those PSF flux measurements and estimated errors, we set the threshold so that the stdev of the measurements matches the median estimated error.
Parameters: Returns: - result :
lsst.pipe.base.Struct
Result struct with components:
- result :
-
clearMask
(mask)¶ Clear the DETECTED and DETECTED_NEGATIVE mask planes
Removes any previous detection mask in preparation for a new detection pass.
Parameters: - mask :
lsst.afw.image.Mask
Mask to be cleared.
- mask :
-
clearUnwantedResults
(mask, results)¶ Clear unwanted results from the Struct of results
If we specifically want only positive or only negative detections, drop the ones we don’t want, and its associated mask plane.
Parameters: - mask :
lsst.afw.image.Mask
Mask image.
- results :
lsst.pipe.base.Struct
Detection results, with
positive
andnegative
elements; modified.
- mask :
-
convolveImage
(maskedImage, psf, doSmooth=True)¶ Convolve the image with the PSF
We convolve the image with a Gaussian approximation to the PSF, because this is separable and therefore fast. It’s technically a correlation rather than a convolution, but since we use a symmetric Gaussian there’s no difference.
The convolution can be disabled with
doSmooth=False
. If we do convolve, we mask the edges asEDGE
and return the convolved image with the edges removed. This is because we can’t convolve the edges because the kernel would extend off the image.Parameters: - maskedImage :
lsst.afw.image.MaskedImage
Image to convolve.
- psf :
lsst.afw.detection.Psf
PSF to convolve with (actually with a Gaussian approximation to it).
- doSmooth :
bool
Actually do the convolution? Set to False when running on e.g. a pre-convolved image, or a mask plane.
- maskedImage :
-
detectFootprints
(exposure, doSmooth=True, sigma=None, clearMask=True, expId=None)¶ Detect footprints with a dynamic threshold
This varies from the vanilla
detectFootprints
method because we do detection twice: one with a low threshold so that we can find sky uncontaminated by objects, then one more with the new calculated threshold.Parameters: - exposure :
lsst.afw.image.Exposure
Exposure to process; DETECTED{,_NEGATIVE} mask plane will be set in-place.
- doSmooth :
bool
, optional If True, smooth the image before detection using a Gaussian of width
sigma
.- sigma :
float
, optional Gaussian Sigma of PSF (pixels); used for smoothing and to grow detections; if
None
then measure the sigma of the PSF of theexposure
.- clearMask :
bool
, optional Clear both DETECTED and DETECTED_NEGATIVE planes before running detection.
- expId :
int
, optional Exposure identifier, used as a seed for the random number generator. If absent, the seed will be the sum of the image.
- exposure :
-
display
(exposure, results, convolvedImage=None)¶ Display detections if so configured
Displays the
exposure
in frame 0, overlays the detection peaks.Requires that
lsstDebug
has been set up correctly, so thatlsstDebug.Info("lsst.meas.algorithms.detection")
evaluatesTrue
.If the
convolvedImage
is non-None
andlsstDebug.Info("lsst.meas.algorithms.detection") > 1
, theconvolvedImage
will be displayed in frame 1.Parameters: - exposure :
lsst.afw.image.Exposure
Exposure to display, on which will be plotted the detections.
- results :
lsst.pipe.base.Struct
Results of the ‘detectFootprints’ method, containing positive and negative footprints (which contain the peak positions that we will plot). This is a
Struct
withpositive
andnegative
elements that are of typelsst.afw.detection.FootprintSet
.- convolvedImage :
lsst.afw.image.Image
, optional Convolved image used for thresholding.
- exposure :
-
emptyMetadata
() → None¶ Empty (clear) the metadata for this Task and all sub-Tasks.
-
finalizeFootprints
(mask, results, sigma, factor=1.0)¶ Finalize the detected footprints
Grows the footprints, sets the
DETECTED
andDETECTED_NEGATIVE
mask planes, and logs the results.numPos
(number of positive footprints),numPosPeaks
(number of positive peaks),numNeg
(number of negative footprints),numNegPeaks
(number of negative peaks) entries are added to the detection results.Parameters: - mask :
lsst.afw.image.Mask
Mask image on which to flag detected pixels.
- results :
lsst.pipe.base.Struct
Struct of detection results, including
positive
andnegative
entries; modified.- sigma :
float
Gaussian sigma of PSF.
- factor :
float
Multiplier for the configured threshold.
- mask :
-
getAllSchemaCatalogs
() → Dict[str, Any]¶ Get schema catalogs for all tasks in the hierarchy, combining the results into a single dict.
Returns: - schemacatalogs :
dict
Keys are butler dataset type, values are a empty catalog (an instance of the appropriate
lsst.afw.table
Catalog type) for all tasks in the hierarchy, from the top-level task down through all subtasks.
Notes
This method may be called on any task in the hierarchy; it will return the same answer, regardless.
The default implementation should always suffice. If your subtask uses schemas the override
Task.getSchemaCatalogs
, not this method.- schemacatalogs :
-
getFullMetadata
() → lsst.pipe.base._task_metadata.TaskMetadata¶ Get metadata for all tasks.
Returns: - metadata :
TaskMetadata
The keys are the full task name. Values are metadata for the top-level task and all subtasks, sub-subtasks, etc.
Notes
The returned metadata includes timing information (if
@timer.timeMethod
is used) and any metadata set by the task. The name of each item consists of the full task name with.
replaced by:
, followed by.
and the name of the item, e.g.:topLevelTaskName:subtaskName:subsubtaskName.itemName
using
:
in the full task name disambiguates the rare situation that a task has a subtask and a metadata item with the same name.- metadata :
-
getFullName
() → str¶ Get the task name as a hierarchical name including parent task names.
Returns: - fullName :
str
The full name consists of the name of the parent task and each subtask separated by periods. For example:
- The full name of top-level task “top” is simply “top”.
- The full name of subtask “sub” of top-level task “top” is “top.sub”.
- The full name of subtask “sub2” of subtask “sub” of top-level task “top” is “top.sub.sub2”.
- fullName :
-
getPsf
(exposure, sigma=None)¶ Retrieve the PSF for an exposure
If
sigma
is provided, we make aGaussianPsf
with that, otherwise use the one from theexposure
.Parameters: - exposure :
lsst.afw.image.Exposure
Exposure from which to retrieve the PSF.
- sigma :
float
, optional Gaussian sigma to use if provided.
Returns: - psf :
lsst.afw.detection.Psf
PSF to use for detection.
- exposure :
-
getSchemaCatalogs
() → Dict[str, Any]¶ Get the schemas generated by this task.
Returns: - schemaCatalogs :
dict
Keys are butler dataset type, values are an empty catalog (an instance of the appropriate
lsst.afw.table
Catalog type) for this task.
See also
Task.getAllSchemaCatalogs
Notes
Warning
Subclasses that use schemas must override this method. The default implementation returns an empty dict.
This method may be called at any time after the Task is constructed, which means that all task schemas should be computed at construction time, not when data is actually processed. This reflects the philosophy that the schema should not depend on the data.
Returning catalogs rather than just schemas allows us to save e.g. slots for SourceCatalog as well.
- schemaCatalogs :
-
getTaskDict
() → Dict[str, weakref.ReferenceType[Task]]¶ Get a dictionary of all tasks as a shallow copy.
Returns: - taskDict :
dict
Dictionary containing full task name: task object for the top-level task and all subtasks, sub-subtasks, etc.
- taskDict :
-
classmethod
makeField
(doc: str) → lsst.pex.config.configurableField.ConfigurableField¶ Make a
lsst.pex.config.ConfigurableField
for this task.Parameters: - doc :
str
Help text for the field.
Returns: - configurableField :
lsst.pex.config.ConfigurableField
A
ConfigurableField
for this task.
Examples
Provides a convenient way to specify this task is a subtask of another task.
Here is an example of use:
class OtherTaskConfig(lsst.pex.config.Config): aSubtask = ATaskClass.makeField("brief description of task")
- doc :
-
makeSubtask
(name: str, **keyArgs) → None¶ Create a subtask as a new instance as the
name
attribute of this task.Parameters: - name :
str
Brief name of the subtask.
- keyArgs
Extra keyword arguments used to construct the task. The following arguments are automatically provided and cannot be overridden:
- “config”.
- “parentTask”.
Notes
The subtask must be defined by
Task.config.name
, an instance ofConfigurableField
orRegistryField
.- name :
-
makeThreshold
(image, thresholdParity, factor=1.0)¶ Make an afw.detection.Threshold object corresponding to the task’s configuration and the statistics of the given image.
Parameters: - image :
afw.image.MaskedImage
Image to measure noise statistics from if needed.
- thresholdParity: `str`
One of “positive” or “negative”, to set the kind of fluctuations the Threshold will detect.
- factor :
float
Factor by which to multiply the configured detection threshold. This is useful for tweaking the detection threshold slightly.
Returns: - threshold :
lsst.afw.detection.Threshold
Detection threshold.
- image :
-
reEstimateBackground
(maskedImage, backgrounds)¶ Estimate the background after detection
Parameters: - maskedImage :
lsst.afw.image.MaskedImage
Image on which to estimate the background.
- backgrounds :
lsst.afw.math.BackgroundList
List of backgrounds; modified.
Returns: - bg :
lsst.afw.math.backgroundMI
Empirical background model.
- maskedImage :
-
run
(table, exposure, doSmooth=True, sigma=None, clearMask=True, expId=None)¶ Run source detection and create a SourceCatalog of detections.
Parameters: - table :
lsst.afw.table.SourceTable
Table object that will be used to create the SourceCatalog.
- exposure :
lsst.afw.image.Exposure
Exposure to process; DETECTED mask plane will be set in-place.
- doSmooth :
bool
If True, smooth the image before detection using a Gaussian of width
sigma
, or the measured PSF width. Set to False when running on e.g. a pre-convolved image, or a mask plane.- sigma :
float
Sigma of PSF (pixels); used for smoothing and to grow detections; if None then measure the sigma of the PSF of the exposure
- clearMask :
bool
Clear DETECTED{,_NEGATIVE} planes before running detection.
- expId :
int
Exposure identifier; unused by this implementation, but used for RNG seed by subclasses.
Returns: - result :
lsst.pipe.base.Struct
sources
The detected sources (
lsst.afw.table.SourceCatalog
)fpSets
The result resturned by
detectFootprints
(lsst.pipe.base.Struct
).
Raises: - ValueError
If flags.negative is needed, but isn’t in table’s schema.
- lsst.pipe.base.TaskError
If sigma=None, doSmooth=True and the exposure has no PSF.
Notes
If you want to avoid dealing with Sources and Tables, you can use detectFootprints() to just get the `lsst.afw.detection.FootprintSet`s.
- table :
-
static
setEdgeBits
(maskedImage, goodBBox, edgeBitmask)¶ Set the edgeBitmask bits for all of maskedImage outside goodBBox
Parameters: - maskedImage :
lsst.afw.image.MaskedImage
Image on which to set edge bits in the mask.
- goodBBox :
lsst.geom.Box2I
Bounding box of good pixels, in
LOCAL
coordinates.- edgeBitmask :
lsst.afw.image.MaskPixel
Bit mask to OR with the existing mask bits in the region outside
goodBBox
.
- maskedImage :
-
tempWideBackgroundContext
(exposure)¶ Context manager for removing wide (large-scale) background
Removing a wide (large-scale) background helps to suppress the detection of large footprints that may overwhelm the deblender. It does, however, set a limit on the maximum scale of objects.
The background that we remove will be restored upon exit from the context manager.
Parameters: - exposure :
lsst.afw.image.Exposure
Exposure on which to remove large-scale background.
Returns: - context : context manager
Context manager that will ensure the temporary wide background is restored.
- exposure :
-
timer
(name: str, logLevel: int = 10) → Iterator[None]¶ Context manager to log performance data for an arbitrary block of code.
Parameters: See also
timer.logInfo
Examples
Creating a timer context:
with self.timer("someCodeToTime"): pass # code to time
-
tweakBackground
(exposure, bgLevel, bgList=None)¶ Modify the background by a constant value
Parameters: - exposure :
lsst.afw.image.Exposure
Exposure for which to tweak background.
- bgLevel :
float
Background level to remove
- bgList :
lsst.afw.math.BackgroundList
, optional List of backgrounds to append to.
Returns: - bg :
lsst.afw.math.BackgroundMI
Constant background model.
- exposure :
-
updatePeaks
(fpSet, image, threshold)¶ Update the Peaks in a FootprintSet by detecting new Footprints and Peaks in an image and using the new Peaks instead of the old ones.
Parameters: - fpSet :
afw.detection.FootprintSet
Set of Footprints whose Peaks should be updated.
- image :
afw.image.MaskedImage
Image to detect new Footprints and Peak in.
- threshold :
afw.detection.Threshold
Threshold object for detection.
- Input Footprints with fewer Peaks than self.config.nPeaksMaxSimple
- are not modified, and if no new Peaks are detected in an input
- Footprint, the brightest original Peak in that Footprint is kept.
- fpSet :
-