Skip to content

Commit

Permalink
Replace masked image pixels with median values before convolution
Browse files Browse the repository at this point in the history
Only use with preconvolution and the Score image used for detection for now.
  • Loading branch information
isullivan committed Jul 21, 2023
1 parent 357ee4a commit addaa0e
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions python/lsst/ip/diffim/subtractImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,10 @@ def _validateExposures(template, science):
def _convolveExposure(exposure, kernel, convolutionControl,
bbox=None,
psf=None,
photoCalib=None):
photoCalib=None,
interpolateBadMaskPlanes=False,
badMaskPlanes=None,
):
"""Convolve an exposure with the given kernel.
Parameters
Expand Down Expand Up @@ -640,8 +643,11 @@ def _convolveExposure(exposure, kernel, convolutionControl,
convolvedExposure.setPsf(psf)
if photoCalib is not None:
convolvedExposure.setPhotoCalib(photoCalib)
convolvedImage = lsst.afw.image.MaskedImageF(exposure.getBBox())
lsst.afw.math.convolve(convolvedImage, exposure.maskedImage, kernel, convolutionControl)
if interpolateBadMaskPlanes and badMaskPlanes is not None:
_interpolateImage(convolvedExposure.maskedImage,
badMaskPlanes)
convolvedImage = lsst.afw.image.MaskedImageF(convolvedExposure.getBBox())
lsst.afw.math.convolve(convolvedImage, convolvedExposure.maskedImage, kernel, convolutionControl)
convolvedExposure.setMaskedImage(convolvedImage)
if bbox is None:
return convolvedExposure
Expand Down Expand Up @@ -849,7 +855,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)
matchedScience = self._convolveExposure(science, scienceKernel, self.convolutionControl,
badMaskPlanes=self.config.badMaskPlanes,
interpolateBadMaskPlanes=True)
selectSources = self._sourceSelector(sources, matchedScience.mask)

subtractResults = self.runPreconvolve(template, science, matchedScience, selectSources, scienceKernel)
Expand Down Expand Up @@ -910,6 +918,8 @@ 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,
backgroundModel=(kernelResult.backgroundModel
Expand Down Expand Up @@ -1025,3 +1035,25 @@ def _shapeTest(exp1, exp2, fwhmExposureBuffer, fwhmExposureGrid):
xTest = shape1[0] <= shape2[0]
yTest = shape1[1] <= shape2[1]
return xTest | yTest


def _interpolateImage(maskedImage, badMaskPlanes, fallbackValue=None):
"""Replace masked image pixels with interpolated values.
Parameters
----------
maskedImage : `lsst.afw.image.MaskedImage`
Image on which to perform interpolation.
badMaskPlanes : `list` of `str`
List of mask planes to interpolate over.
fallbackValue : `float`, optional
Value to set when interpolation fails.
"""
image = maskedImage.image.array
badPixels = (maskedImage.mask.array & maskedImage.mask.getPlaneBitMask(badMaskPlanes)) > 0
image[badPixels] = np.nan
if fallbackValue is None:
fallbackValue = np.nanmedian(image)
# For this initial implementation, skip the interpolation and just fill with
# the median value.
image[badPixels] = fallbackValue

0 comments on commit addaa0e

Please sign in to comment.