Skip to content

Commit

Permalink
Use the kernel to shrink bbox when convolving
Browse files Browse the repository at this point in the history
  • Loading branch information
isullivan committed Nov 24, 2022
1 parent 5bc64e4 commit ecc3c2c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
38 changes: 22 additions & 16 deletions python/lsst/ip/diffim/subtractImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,13 @@ def runConvolveTemplate(self, template, science, selectSources):
bbox=science.getBBox(),
psf=science.psf,
photoCalib=science.getPhotoCalib())
difference = _subtractImages(science, matchedTemplate,

bbox = science.getBBox().clippedTo(matchedTemplate.getBBox())
difference = _subtractImages(science[bbox], matchedTemplate[bbox],
backgroundModel=(kernelResult.backgroundModel
if self.config.doSubtractBackground else None))
correctedExposure = self.finalize(template, science, difference, kernelResult.psfMatchingKernel,
correctedExposure = self.finalize(template[bbox], science[bbox], difference,
kernelResult.psfMatchingKernel,
templateMatched=True)

return lsst.pipe.base.Struct(difference=correctedExposure,
Expand Down Expand Up @@ -442,18 +445,20 @@ def runConvolveScience(self, template, science, selectSources):
matchedScience = self._convolveExposure(science, kernelResult.psfMatchingKernel,
self.convolutionControl,
psf=template.psf)
bbox = matchedScience.getBBox().clippedTo(template.getBBox())

# Place back on native photometric scale
matchedScience.maskedImage /= norm
matchedTemplate = template.clone()[science.getBBox()]
matchedTemplate = template.clone()[bbox]
matchedTemplate.maskedImage /= norm
matchedTemplate.setPhotoCalib(science.getPhotoCalib())

difference = _subtractImages(matchedScience, matchedTemplate,
difference = _subtractImages(matchedScience[bbox], matchedTemplate,
backgroundModel=(kernelResult.backgroundModel
if self.config.doSubtractBackground else None))

correctedExposure = self.finalize(template, science, difference, kernelResult.psfMatchingKernel,
correctedExposure = self.finalize(template[bbox], science[bbox], difference,
kernelResult.psfMatchingKernel,
templateMatched=False)

return lsst.pipe.base.Struct(difference=correctedExposure,
Expand Down Expand Up @@ -575,10 +580,12 @@ def _convolveExposure(exposure, kernel, convolutionControl,
convolvedImage = lsst.afw.image.MaskedImageF(exposure.getBBox())
lsst.afw.math.convolve(convolvedImage, exposure.maskedImage, kernel, convolutionControl)
convolvedExposure.setMaskedImage(convolvedImage)
expBBox = kernel.shrinkBBox(convolvedExposure.getBBox())
convolvedExposure = convolvedExposure[expBBox]
if bbox is None:
return convolvedExposure
else:
return convolvedExposure[bbox]
return convolvedExposure[bbox.clippedTo(expBBox)]

def _sourceSelector(self, sources):
"""Select sources from a catalog that meet the selection criteria.
Expand Down Expand Up @@ -789,30 +796,29 @@ def runConvolveTemplate(self, template, science, selectSources):
Kernel used to PSF-match the template to the science image.
"""

bbox = lsst.geom.Box2I(science.getBBox().getBegin(), science.getBBox().getDimensions())
bbox.grow(-20)
kernelSources = self.makeKernel.selectKernelSources(template[bbox], science[bbox],
candidateList=selectSources,
preconvolved=True)

# creating a kernel to run preconvolution with its own psf
# TODO: we need to mirror the kernel in order to get correct cross correlation
scienceKernel = science.psf.getKernel()

matchedScience = self._convolveExposure(science, scienceKernel, self.convolutionControl)
bbox = matchedScience.getBBox()

kernelResult = self.makeKernel.run(template[bbox], matchedScience[bbox], kernelSources,
kernelSources = self.makeKernel.selectKernelSources(template[bbox], science[bbox],
candidateList=selectSources,
preconvolved=True)
kernelResult = self.makeKernel.run(template[bbox], matchedScience, kernelSources,
preconvolved=True)

matchedTemplate = self._convolveExposure(template, kernelResult.psfMatchingKernel,
self.convolutionControl,
bbox=science.getBBox(),
bbox=bbox,
psf=science.psf,
photoCalib=science.getPhotoCalib())
score = _subtractImages(matchedScience, matchedTemplate,
bbox.clip(matchedTemplate.getBBox())
score = _subtractImages(matchedScience[bbox], matchedTemplate[bbox],
backgroundModel=(kernelResult.backgroundModel
if self.config.doSubtractBackground else None))
correctedScore = self.finalize(template, science, score,
correctedScore = self.finalize(template[bbox], science[bbox], score,
kernelResult.psfMatchingKernel,
templateMatched=True, preConvMode=True,
preConvKernel=scienceKernel)
Expand Down
27 changes: 15 additions & 12 deletions tests/test_subtractTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,21 @@ def test_order_equal_images(self):
config2.doSubtractBackground = False
task2 = subtractImages.AlardLuptonSubtractTask(config=config2)
results_convolveScience = task2.run(template2, science2, sources2)
diff1 = science1.maskedImage.clone()
diff1 -= template1.maskedImage
diff2 = science2.maskedImage.clone()
diff2 -= template2.maskedImage
self.assertFloatsAlmostEqual(results_convolveTemplate.difference.image.array,
bbox = results_convolveTemplate.difference.getBBox().clippedTo(
results_convolveScience.difference.getBBox())
diff1 = science1.maskedImage.clone()[bbox]
diff1 -= template1.maskedImage[bbox]
diff2 = science2.maskedImage.clone()[bbox]
diff2 -= template2.maskedImage[bbox]
self.assertFloatsAlmostEqual(results_convolveTemplate.difference[bbox].image.array,
diff1.image.array,
atol=noiseLevel*5.)
self.assertFloatsAlmostEqual(results_convolveScience.difference.image.array,
self.assertFloatsAlmostEqual(results_convolveScience.difference[bbox].image.array,
diff2.image.array,
atol=noiseLevel*5.)
diffErr = noiseLevel*2
self.assertMaskedImagesAlmostEqual(results_convolveTemplate.difference.maskedImage,
results_convolveScience.difference.maskedImage,
self.assertMaskedImagesAlmostEqual(results_convolveTemplate.difference[bbox].maskedImage,
results_convolveScience.difference[bbox].maskedImage,
atol=diffErr*5.)

def test_background_subtraction(self):
Expand Down Expand Up @@ -785,11 +787,12 @@ def test_agnostic_template_psf(self):

science_better = task.run(template1, science.clone(), sources)
template_better = task.run(template2, science, sources)
bbox = science_better.scoreExposure.getBBox().clippedTo(template_better.scoreExposure.getBBox())

delta = template_better.scoreExposure.clone()
delta.image -= science_better.scoreExposure.image
delta.variance -= science_better.scoreExposure.variance
delta.mask.array &= science_better.scoreExposure.mask.array
delta = template_better.scoreExposure[bbox].clone()
delta.image -= science_better.scoreExposure[bbox].image
delta.variance -= science_better.scoreExposure[bbox].variance
delta.mask.array &= science_better.scoreExposure[bbox].mask.array

statsCtrl = _makeStats(badMaskPlanes=("EDGE", "BAD", "NO_DATA"))
# Mean of delta should be very close to zero.
Expand Down

0 comments on commit ecc3c2c

Please sign in to comment.