Skip to content

Commit

Permalink
Add subtractTask metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
isullivan committed Sep 19, 2023
1 parent e8a5b81 commit 2e5f4c3
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions python/lsst/ip/diffim/subtractImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ def run(self, template, science, sources, finalizedPsfApCorrCatalog=None,
)
self.log.info("Science PSF FWHM: %f pixels", sciencePsfSize)
self.log.info("Template PSF FWHM: %f pixels", templatePsfSize)
self.metadata.add("sciencePsfSize", sciencePsfSize)
self.metadata.add("templatePsfSize", templatePsfSize)
selectSources = self._sourceSelector(sources, science.mask)

if self.config.mode == "auto":
Expand All @@ -397,8 +399,10 @@ def run(self, template, science, sources, finalizedPsfApCorrCatalog=None,
raise RuntimeError("Cannot handle AlardLuptonSubtract mode: %s", self.config.mode)

if convolveTemplate:
self.metadata.add("convolvedExposure", "Template")
subtractResults = self.runConvolveTemplate(template, science, selectSources)
else:
self.metadata.add("convolvedExposure", "Science")
subtractResults = self.runConvolveScience(template, science, selectSources)

return subtractResults
Expand Down Expand Up @@ -608,13 +612,11 @@ def _validateExposures(template, science):
assert templateBBox.contains(scienceBBox),\
"Template bbox does not contain all of the science image."

@staticmethod
def _convolveExposure(exposure, kernel, convolutionControl,
def _convolveExposure(self, exposure, kernel, convolutionControl,
bbox=None,
psf=None,
photoCalib=None,
interpolateBadMaskPlanes=False,
badMaskPlanes=None,
):
"""Convolve an exposure with the given kernel.
Expand Down Expand Up @@ -643,9 +645,10 @@ def _convolveExposure(exposure, kernel, convolutionControl,
convolvedExposure.setPsf(psf)
if photoCalib is not None:
convolvedExposure.setPhotoCalib(photoCalib)
if interpolateBadMaskPlanes and badMaskPlanes is not None:
_interpolateImage(convolvedExposure.maskedImage,
badMaskPlanes)
if interpolateBadMaskPlanes and self.config.badMaskPlanes is not None:
nInterp = _interpolateImage(convolvedExposure.maskedImage,
self.config.badMaskPlanes)
self.metadata.add("nInterpolated", nInterp)
convolvedImage = lsst.afw.image.MaskedImageF(convolvedExposure.getBBox())
lsst.afw.math.convolve(convolvedImage, convolvedExposure.maskedImage, kernel, convolutionControl)
convolvedExposure.setMaskedImage(convolvedImage)
Expand Down Expand Up @@ -694,6 +697,7 @@ def _sourceSelector(self, sources, mask):
"%i selected but %i needed for the calculation.",
len(selectSources), self.config.makeKernel.nStarPerCell)
raise RuntimeError("Cannot compute PSF matching kernel: too few sources selected.")
self.metadata.add("nPsfSources", len(selectSources))

return selectSources.copy(deep=True)

Expand Down Expand Up @@ -856,9 +860,9 @@ def run(self, template, science, sources, finalizedPsfApCorrCatalog=None, visitS
# TODO: DM-37212 we need to mirror the kernel in order to get correct cross correlation
scienceKernel = science.psf.getKernel()
matchedScience = self._convolveExposure(science, scienceKernel, self.convolutionControl,
badMaskPlanes=self.config.badMaskPlanes,
interpolateBadMaskPlanes=True)
selectSources = self._sourceSelector(sources, matchedScience.mask)
self.metadata.add("convolvedExposure", "Preconvolution")

subtractResults = self.runPreconvolve(template, science, matchedScience, selectSources, scienceKernel)

Expand Down Expand Up @@ -918,7 +922,6 @@ def runPreconvolve(self, template, science, matchedScience, selectSources, preCo
self.convolutionControl,
bbox=bbox,
psf=science.psf,
badMaskPlanes=self.config.badMaskPlanes,
interpolateBadMaskPlanes=True,
photoCalib=science.photoCalib)
score = _subtractImages(matchedScience, matchedTemplate,
Expand Down Expand Up @@ -1048,6 +1051,11 @@ def _interpolateImage(maskedImage, badMaskPlanes, fallbackValue=None):
List of mask planes to interpolate over.
fallbackValue : `float`, optional
Value to set when interpolation fails.
Returns
-------
result: `float`
The number of masked pixels that were replaced.
"""
image = maskedImage.image.array
badPixels = (maskedImage.mask.array & maskedImage.mask.getPlaneBitMask(badMaskPlanes)) > 0
Expand All @@ -1057,3 +1065,4 @@ def _interpolateImage(maskedImage, badMaskPlanes, fallbackValue=None):
# For this initial implementation, skip the interpolation and just fill with
# the median value.
image[badPixels] = fallbackValue
return np.sum(badPixels)

0 comments on commit 2e5f4c3

Please sign in to comment.