28from lsst.meas.algorithms
import ScaleVarianceTask
32from .
import MakeKernelTask, DecorrelateALKernelTask
33from lsst.utils.timer
import timeMethod
35__all__ = [
"AlardLuptonSubtractConfig",
"AlardLuptonSubtractTask"]
37_dimensions = (
"instrument",
"visit",
"detector")
38_defaultTemplates = {
"coaddName":
"deep",
"fakesType":
""}
42 dimensions=_dimensions,
43 defaultTemplates=_defaultTemplates):
44 template = connectionTypes.Input(
45 doc=
"Input warped template to subtract.",
46 dimensions=(
"instrument",
"visit",
"detector"),
47 storageClass=
"ExposureF",
48 name=
"{fakesType}{coaddName}Diff_templateExp"
50 science = connectionTypes.Input(
51 doc=
"Input science exposure to subtract from.",
52 dimensions=(
"instrument",
"visit",
"detector"),
53 storageClass=
"ExposureF",
54 name=
"{fakesType}calexp"
56 sources = connectionTypes.Input(
57 doc=
"Sources measured on the science exposure; "
58 "used to select sources for making the matching kernel.",
59 dimensions=(
"instrument",
"visit",
"detector"),
60 storageClass=
"SourceCatalog",
63 finalizedPsfApCorrCatalog = connectionTypes.Input(
64 doc=(
"Per-visit finalized psf models and aperture correction maps. "
65 "These catalogs use the detector id for the catalog id, "
66 "sorted on id for fast lookup."),
67 dimensions=(
"instrument",
"visit"),
68 storageClass=
"ExposureCatalog",
69 name=
"finalized_psf_ap_corr_catalog",
74 dimensions=_dimensions,
75 defaultTemplates=_defaultTemplates):
76 difference = connectionTypes.Output(
77 doc=
"Result of subtracting convolved template from science image.",
78 dimensions=(
"instrument",
"visit",
"detector"),
79 storageClass=
"ExposureF",
80 name=
"{fakesType}{coaddName}Diff_differenceTempExp",
82 matchedTemplate = connectionTypes.Output(
83 doc=
"Warped and PSF-matched template used to create `subtractedExposure`.",
84 dimensions=(
"instrument",
"visit",
"detector"),
85 storageClass=
"ExposureF",
86 name=
"{fakesType}{coaddName}Diff_matchedExp",
94 if not config.doApplyFinalizedPsf:
95 self.inputs.remove(
"finalizedPsfApCorrCatalog")
99 pipelineConnections=AlardLuptonSubtractConnections):
100 mode = lsst.pex.config.ChoiceField(
102 default=
"convolveTemplate",
103 allowed={
"auto":
"Choose which image to convolve at runtime.",
104 "convolveScience":
"Only convolve the science image.",
105 "convolveTemplate":
"Only convolve the template image."},
106 doc=
"Choose which image to convolve at runtime, or require that a specific image is convolved."
108 makeKernel = lsst.pex.config.ConfigurableField(
109 target=MakeKernelTask,
110 doc=
"Task to construct a matching kernel for convolution.",
112 doDecorrelation = lsst.pex.config.Field(
115 doc=
"Perform diffim decorrelation to undo pixel correlation due to A&L "
116 "kernel convolution? If True, also update the diffim PSF."
118 decorrelate = lsst.pex.config.ConfigurableField(
119 target=DecorrelateALKernelTask,
120 doc=
"Task to decorrelate the image difference.",
122 requiredTemplateFraction = lsst.pex.config.Field(
125 doc=
"Abort task if template covers less than this fraction of pixels."
126 " Setting to 0 will always attempt image subtraction."
128 doScaleVariance = lsst.pex.config.Field(
131 doc=
"Scale variance of the image difference?"
133 scaleVariance = lsst.pex.config.ConfigurableField(
134 target=ScaleVarianceTask,
135 doc=
"Subtask to rescale the variance of the template to the statistically expected level."
137 doSubtractBackground = lsst.pex.config.Field(
138 doc=
"Subtract the background fit when solving the kernel?",
142 doApplyFinalizedPsf = lsst.pex.config.Field(
143 doc=
"Replace science Exposure's psf and aperture correction map"
144 " with those in finalizedPsfApCorrCatalog.",
149 forceCompatibility = lsst.pex.config.Field(
152 doc=
"Set up and run diffim using settings that ensure the results"
153 "are compatible with the old version in pipe_tasks.",
154 deprecated=
"This option is only for backwards compatibility purposes"
155 " and will be removed after v24.",
161 self.
makeKernel.kernel.active.spatialKernelOrder = 1
162 self.
makeKernel.kernel.active.spatialBgOrder = 2
166 msg = f
"forceCompatibility=True requires mode='convolveTemplate', but mode was '{self.mode}'."
167 raise lsst.pex.config.FieldValidationError(AlardLuptonSubtractConfig.forceCompatibility,
172 """Compute the image difference of a science and template image using
173 the Alard & Lupton (1998) algorithm.
175 ConfigClass = AlardLuptonSubtractConfig
176 _DefaultName = "alardLuptonSubtract"
180 self.makeSubtask(
"decorrelate")
181 self.makeSubtask(
"makeKernel")
182 if self.config.doScaleVariance:
183 self.makeSubtask(
"scaleVariance")
191 def _applyExternalCalibrations(self, exposure, finalizedPsfApCorrCatalog):
192 """Replace calibrations (psf, and ApCorrMap) on this exposure with external ones.".
196 exposure : `lsst.afw.image.exposure.Exposure`
197 Input exposure to adjust calibrations.
199 Exposure catalog with finalized psf models
and aperture correction
200 maps to be applied
if config.doApplyFinalizedPsf=
True. Catalog uses
201 the detector id
for the catalog id, sorted on id
for fast lookup.
205 exposure : `lsst.afw.image.exposure.Exposure`
206 Exposure
with adjusted calibrations.
208 detectorId = exposure.info.getDetector().getId()
210 row = finalizedPsfApCorrCatalog.find(detectorId)
212 self.log.warning(
"Detector id %s not found in finalizedPsfApCorrCatalog; "
213 "Using original psf.", detectorId)
216 apCorrMap = row.getApCorrMap()
218 self.log.warning(
"Detector id %s has None for psf in "
219 "finalizedPsfApCorrCatalog; Using original psf and aperture correction.",
221 elif apCorrMap
is None:
222 self.log.warning(
"Detector id %s has None for apCorrMap in "
223 "finalizedPsfApCorrCatalog; Using original psf and aperture correction.",
227 exposure.info.setApCorrMap(apCorrMap)
232 def run(self, template, science, sources, finalizedPsfApCorrCatalog=None):
233 """PSF match, subtract, and decorrelate two images.
237 template : `lsst.afw.image.ExposureF`
238 Template exposure, warped to match the science exposure.
239 science : `lsst.afw.image.ExposureF`
240 Science exposure to subtract from the template.
242 Identified sources on the science exposure. This catalog
is used to
243 select sources
in order to perform the AL PSF matching on stamp
246 Exposure catalog
with finalized psf models
and aperture correction
247 maps to be applied
if config.doApplyFinalizedPsf=
True. Catalog uses
248 the detector id
for the catalog id, sorted on id
for fast lookup.
252 results : `lsst.pipe.base.Struct`
253 ``difference`` : `lsst.afw.image.ExposureF`
254 Result of subtracting template
and science.
255 ``matchedTemplate`` : `lsst.afw.image.ExposureF`
256 Warped
and PSF-matched template exposure.
257 ``backgroundModel`` : `lsst.afw.math.Function2D`
258 Background model that was fit
while solving
for the PSF-matching kernel
260 Kernel used to PSF-match the convolved image.
265 If an unsupported convolution mode
is supplied.
266 lsst.pipe.base.NoWorkFound
267 Raised
if fraction of good pixels, defined
as not having NO_DATA
268 set,
is less then the configured requiredTemplateFraction
271 if self.config.doApplyFinalizedPsf:
273 finalizedPsfApCorrCatalog=finalizedPsfApCorrCatalog)
275 requiredTemplateFraction=self.config.requiredTemplateFraction)
276 if self.config.forceCompatibility:
279 self.log.warning(
"Running with `config.forceCompatibility=True`")
283 self.log.info(
"Science PSF size: %f", sciencePsfSize)
284 self.log.info(
"Template PSF size: %f", templatePsfSize)
285 if self.config.mode ==
"auto":
286 if sciencePsfSize < templatePsfSize:
287 self.log.info(
"Template PSF size is greater: convolving science image.")
288 convolveTemplate =
False
290 self.log.info(
"Science PSF size is greater: convolving template image.")
291 convolveTemplate =
True
292 elif self.config.mode ==
"convolveTemplate":
293 self.log.info(
"`convolveTemplate` is set: convolving template image.")
294 convolveTemplate =
True
295 elif self.config.mode ==
"convolveScience":
296 self.log.info(
"`convolveScience` is set: convolving science image.")
297 convolveTemplate =
False
299 raise RuntimeError(
"Cannot handle AlardLuptonSubtract mode: %s", self.config.mode)
301 if self.config.doScaleVariance
and not self.config.forceCompatibility:
305 templateVarFactor = self.scaleVariance.
run(template.maskedImage)
306 sciVarFactor = self.scaleVariance.
run(science.maskedImage)
307 self.log.info(
"Template variance scaling factor: %.2f", templateVarFactor)
308 self.metadata.add(
"scaleTemplateVarianceFactor", templateVarFactor)
309 self.log.info(
"Science variance scaling factor: %.2f", sciVarFactor)
310 self.metadata.add(
"scaleScienceVarianceFactor", sciVarFactor)
312 kernelSources = self.makeKernel.selectKernelSources(template, science,
313 candidateList=sources,
320 if self.config.doScaleVariance
and self.config.forceCompatibility:
322 diffimVarFactor = self.scaleVariance.
run(subtractResults.difference.maskedImage)
323 self.log.info(
"Diffim variance scaling factor: %.2f", diffimVarFactor)
324 self.metadata.add(
"scaleDiffimVarianceFactor", diffimVarFactor)
326 return subtractResults
329 """Convolve the template image with a PSF-matching kernel and subtract
330 from the science image.
334 template : `lsst.afw.image.ExposureF`
335 Template exposure, warped to match the science exposure.
336 science : `lsst.afw.image.ExposureF`
337 Science exposure to subtract
from the template.
339 Identified sources on the science exposure. This catalog
is used to
340 select sources
in order to perform the AL PSF matching on stamp
345 results : `lsst.pipe.base.Struct`
347 ``difference`` : `lsst.afw.image.ExposureF`
348 Result of subtracting template
and science.
349 ``matchedTemplate`` : `lsst.afw.image.ExposureF`
350 Warped
and PSF-matched template exposure.
351 ``backgroundModel`` : `lsst.afw.math.Function2D`
352 Background model that was fit
while solving
for the PSF-matching kernel
354 Kernel used to PSF-match the template to the science image.
356 if self.config.forceCompatibility:
359 template = template[science.getBBox()]
360 kernelResult = self.makeKernel.
run(template, science, sources, preconvolved=
False)
362 matchedTemplate = self.
_convolveExposure(template, kernelResult.psfMatchingKernel,
364 bbox=science.getBBox(),
366 photoCalib=science.getPhotoCalib())
367 difference = _subtractImages(science, matchedTemplate,
368 backgroundModel=(kernelResult.backgroundModel
369 if self.config.doSubtractBackground
else None))
370 correctedExposure = self.
finalize(template, science, difference, kernelResult.psfMatchingKernel,
371 templateMatched=
True)
373 return lsst.pipe.base.Struct(difference=correctedExposure,
374 matchedTemplate=matchedTemplate,
375 matchedScience=science,
376 backgroundModel=kernelResult.backgroundModel,
377 psfMatchingKernel=kernelResult.psfMatchingKernel)
380 """Convolve the science image with a PSF-matching kernel and subtract the template image.
384 template : `lsst.afw.image.ExposureF`
385 Template exposure, warped to match the science exposure.
386 science : `lsst.afw.image.ExposureF`
387 Science exposure to subtract from the template.
389 Identified sources on the science exposure. This catalog
is used to
390 select sources
in order to perform the AL PSF matching on stamp
395 results : `lsst.pipe.base.Struct`
397 ``difference`` : `lsst.afw.image.ExposureF`
398 Result of subtracting template
and science.
399 ``matchedTemplate`` : `lsst.afw.image.ExposureF`
400 Warped template exposure. Note that
in this case, the template
401 is not PSF-matched to the science image.
402 ``backgroundModel`` : `lsst.afw.math.Function2D`
403 Background model that was fit
while solving
for the PSF-matching kernel
405 Kernel used to PSF-match the science image to the template.
407 if self.config.forceCompatibility:
410 template = template[science.getBBox()]
411 kernelResult = self.makeKernel.
run(science, template, sources, preconvolved=
False)
412 modelParams = kernelResult.backgroundModel.getParameters()
414 kernelResult.backgroundModel.setParameters([-p
for p
in modelParams])
416 kernelImage = lsst.afw.image.ImageD(kernelResult.psfMatchingKernel.getDimensions())
417 norm = kernelResult.psfMatchingKernel.computeImage(kernelImage, doNormalize=
False)
424 matchedScience.maskedImage /= norm
425 matchedTemplate = template.clone()[science.getBBox()]
426 matchedTemplate.maskedImage /= norm
427 matchedTemplate.setPhotoCalib(science.getPhotoCalib())
429 difference = _subtractImages(matchedScience, matchedTemplate,
430 backgroundModel=(kernelResult.backgroundModel
431 if self.config.doSubtractBackground
else None))
433 correctedExposure = self.
finalize(template, science, difference, kernelResult.psfMatchingKernel,
434 templateMatched=
False)
436 return lsst.pipe.base.Struct(difference=correctedExposure,
437 matchedTemplate=matchedTemplate,
438 matchedScience=matchedScience,
439 backgroundModel=kernelResult.backgroundModel,
440 psfMatchingKernel=kernelResult.psfMatchingKernel,)
442 def finalize(self, template, science, difference, kernel,
443 templateMatched=True,
446 spatiallyVarying=False):
447 """Decorrelate the difference image to undo the noise correlations
448 caused by convolution.
452 template : `lsst.afw.image.ExposureF`
453 Template exposure, warped to match the science exposure.
454 science : `lsst.afw.image.ExposureF`
455 Science exposure to subtract from the template.
456 difference : `lsst.afw.image.ExposureF`
457 Result of subtracting template
and science.
459 An (optionally spatially-varying) PSF matching kernel
460 templateMatched : `bool`, optional
461 Was the template PSF-matched to the science image?
462 preConvMode : `bool`, optional
463 Was the science image preconvolved
with its own PSF
464 before PSF matching the template?
466 If
not `
None`, then the science image was pre-convolved
with
467 (the reflection of) this kernel. Must be normalized to sum to 1.
468 spatiallyVarying : `bool`, optional
469 Compute the decorrelation kernel spatially varying across the image?
473 correctedExposure : `lsst.afw.image.ExposureF`
474 The decorrelated image difference.
478 mask = difference.mask
479 mask &= ~(mask.getPlaneBitMask(
"DETECTED") | mask.getPlaneBitMask(
"DETECTED_NEGATIVE"))
481 if self.config.doDecorrelation:
482 self.log.info(
"Decorrelating image difference.")
483 correctedExposure = self.decorrelate.
run(science, template[science.getBBox()], difference, kernel,
484 templateMatched=templateMatched,
485 preConvMode=preConvMode,
486 preConvKernel=preConvKernel,
487 spatiallyVarying=spatiallyVarying).correctedExposure
489 self.log.info(
"NOT decorrelating image difference.")
490 correctedExposure = difference
491 return correctedExposure
494 def _validateExposures(template, science):
495 """Check that the WCS of the two Exposures match, and the template bbox
496 contains the science bbox.
500 template : `lsst.afw.image.ExposureF`
501 Template exposure, warped to match the science exposure.
502 science : `lsst.afw.image.ExposureF`
503 Science exposure to subtract from the template.
508 Raised
if the WCS of the template
is not equal to the science WCS,
509 or if the science image
is not fully contained
in the template
512 assert template.wcs == science.wcs,\
513 "Template and science exposure WCS are not identical."
514 templateBBox = template.getBBox()
515 scienceBBox = science.getBBox()
517 assert templateBBox.contains(scienceBBox),\
518 "Template bbox does not contain all of the science image."
521 def _convolveExposure(exposure, kernel, convolutionControl,
525 """Convolve an exposure with the given kernel.
529 exposure : `lsst.afw.Exposure`
530 exposure to convolve.
532 PSF matching kernel computed in the ``makeKernel`` subtask.
534 Configuration
for convolve algorithm.
536 Bounding box to trim the convolved exposure to.
538 Point spread function (PSF) to set
for the convolved exposure.
540 Photometric calibration of the convolved exposure.
544 convolvedExp : `lsst.afw.Exposure`
547 convolvedExposure = exposure.clone()
549 convolvedExposure.setPsf(psf)
550 if photoCalib
is not None:
551 convolvedExposure.setPhotoCalib(photoCalib)
552 convolvedImage = lsst.afw.image.MaskedImageF(exposure.getBBox())
554 convolvedExposure.setMaskedImage(convolvedImage)
556 return convolvedExposure
558 return convolvedExposure[bbox]
562 """Raise NoWorkFound if template coverage < requiredTemplateFraction
566 templateExposure : `lsst.afw.image.ExposureF`
567 The template exposure to check
569 Logger for printing output.
570 requiredTemplateFraction : `float`, optional
571 Fraction of pixels of the science image required to have coverage
576 lsst.pipe.base.NoWorkFound
577 Raised
if fraction of good pixels, defined
as not having NO_DATA
578 set,
is less then the configured requiredTemplateFraction
582 pixNoData = np.count_nonzero(templateExposure.mask.array
583 & templateExposure.mask.getPlaneBitMask(
'NO_DATA'))
584 pixGood = templateExposure.getBBox().getArea() - pixNoData
585 logger.info(
"template has %d good pixels (%.1f%%)", pixGood,
586 100*pixGood/templateExposure.getBBox().getArea())
588 if pixGood/templateExposure.getBBox().getArea() < requiredTemplateFraction:
589 message = (
"Insufficient Template Coverage. (%.1f%% < %.1f%%) Not attempting subtraction. "
590 "To force subtraction, set config requiredTemplateFraction=0." % (
591 100*pixGood/templateExposure.getBBox().getArea(),
592 100*requiredTemplateFraction))
593 raise lsst.pipe.base.NoWorkFound(message)
596def _subtractImages(science, template, backgroundModel=None):
597 """Subtract template from science, propagating relevant metadata.
601 science : `lsst.afw.Exposure`
602 The input science image.
603 template : `lsst.afw.Exposure`
604 The template to subtract from the science image.
605 backgroundModel : `lsst.afw.MaskedImage`, optional
606 Differential background model
610 difference : `lsst.afw.Exposure`
611 The subtracted image.
613 difference = science.clone()
614 if backgroundModel
is not None:
615 difference.maskedImage -= backgroundModel
616 difference.maskedImage -= template.maskedImage
def __init__(self, *config=None)
def finalize(self, template, science, difference, kernel, templateMatched=True, preConvMode=False, preConvKernel=None, spatiallyVarying=False)
def _convolveExposure(exposure, kernel, convolutionControl, bbox=None, psf=None, photoCalib=None)
def runConvolveScience(self, template, science, sources)
def _applyExternalCalibrations(self, exposure, finalizedPsfApCorrCatalog)
def _validateExposures(template, science)
def runConvolveTemplate(self, template, science, sources)
def __init__(self, **kwargs)
def run(self, template, science, sources, finalizedPsfApCorrCatalog=None)
void convolve(OutImageT &convolvedImage, InImageT const &inImage, KernelT const &kernel, ConvolutionControl const &convolutionControl=ConvolutionControl())
def checkTemplateIsSufficient(templateExposure, logger, requiredTemplateFraction=0.)