From c390ebdde62854ebd0d0b098aaa1633a73ed847e Mon Sep 17 00:00:00 2001 From: Ian Sullivan Date: Sat, 9 May 2026 13:51:49 -0700 Subject: [PATCH] Improve diffim detection near the edge --- python/lsst/ip/diffim/detectAndMeasure.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/python/lsst/ip/diffim/detectAndMeasure.py b/python/lsst/ip/diffim/detectAndMeasure.py index 09cd6cb1..1840a363 100644 --- a/python/lsst/ip/diffim/detectAndMeasure.py +++ b/python/lsst/ip/diffim/detectAndMeasure.py @@ -331,7 +331,6 @@ class DetectAndMeasureConfig(pipeBase.PipelineTaskConfig, dtype=str, doc="Sources with any of these flags set are removed before writing the output catalog.", default=("base_PixelFlags_flag_offimage", - "base_PixelFlags_flag_edge", "base_PixelFlags_flag_interpolatedCenterAll", "base_PixelFlags_flag_badCenter", "base_PixelFlags_flag_edgeCenter", @@ -785,7 +784,12 @@ def _fitAndSubtractBackground(self, differenceExposure, scoreExposure=None): # Use the temporary detection mask for the final background subtraction. # The detection mask planes will be cleared before the final detection # step, so it is OK if they get set for differenceExposure. - differenceExposure.setMask(detectionExposure.mask) + detectedBit = differenceExposure.mask.getPlaneBitMask(["DETECTED"]) + detectedPix = detectionExposure.mask.array & detectedBit > 0 + detectedNegativeBit = differenceExposure.mask.getPlaneBitMask(["DETECTED_NEGATIVE"]) + detectedNegativePix = detectionExposure.mask.array & detectedNegativeBit > 0 + differenceExposure.mask.array[detectedPix] |= detectedBit + differenceExposure.mask.array[detectedNegativePix] |= detectedNegativeBit background = self.subtractFinalBackground.run(differenceExposure).background if scoreExposure is not None: # The preconvolution kernel is normalized to 1, so the same @@ -1552,8 +1556,19 @@ def run(self, science, matchedTemplate, difference, scoreExposure, kernelSources measurementResults.differenceBackground = background measurementResults.scoreMeasuredExposure = scoreExposure - # Copy the detection mask from the Score image to the difference image - difference.mask.assign(scoreExposure.mask, scoreExposure.getBBox()) + # Copy the final detection masks from the Score image to the difference. + # Do not copy all mask planes, since the convolution will grow them by + # the size of the convolution kernel. + detectedBit = difference.mask.getPlaneBitMask(["DETECTED"]) + detectedPix = scoreExposure.mask.array & detectedBit > 0 + detectedNegativeBit = difference.mask.getPlaneBitMask(["DETECTED_NEGATIVE"]) + detectedNegativePix = scoreExposure.mask.array & detectedNegativeBit > 0 + difference.mask.array[detectedPix] |= detectedBit + difference.mask.array[detectedNegativePix] |= detectedNegativeBit + # Also set the EDGE mask, since the science image was convolved + edgeBit = difference.mask.getPlaneBitMask(["EDGE"]) + edgePix = scoreExposure.mask.array & edgeBit > 0 + difference.mask.array[edgePix] |= edgeBit if self.config.doDeblend: sources, positives, negatives = self._deblend(difference,