Skip to content

Commit

Permalink
Merge branch 'tickets/DM-45318'
Browse files Browse the repository at this point in the history
  • Loading branch information
mrawls committed Oct 30, 2024
2 parents a1f12eb + 1d4010f commit f8bc214
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
15 changes: 9 additions & 6 deletions python/lsst/meas/algorithms/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def detectFootprints(self, exposure, doSmooth=True, sigma=None, clearMask=True,
convolveResults = self.convolveImage(maskedImage, psf, doSmooth=doSmooth)
middle = convolveResults.middle
sigma = convolveResults.sigma
self.removeBadPixels(middle)
self.removeBadPixels(middle, exposure.maskedImage.mask)

results = self.applyThreshold(middle, maskedImage.getBBox())
results.background = background if background is not None else afwMath.BackgroundList()
Expand All @@ -799,17 +799,20 @@ def detectFootprints(self, exposure, doSmooth=True, sigma=None, clearMask=True,

return results

def removeBadPixels(self, middle):
def removeBadPixels(self, middle, mask):
"""Set the significance of flagged pixels to zero.
Parameters
----------
middle : `lsst.afw.image.ExposureF`
Score or maximum likelihood difference image.
middle : `lsst.afw.image.Exposure` or `lsst.afw.image.MaskedImage`
Convolved image to zero certain mask planes from.
The image plane will be modified in place.
mask : `lsst.afw.image.Mask`
Mask to select planes from to zero the image; will be restricted
to the image's bounding box.
"""
badPixelMask = lsst.afw.image.Mask.getPlaneBitMask(self.config.excludeMaskPlanes)
badPixels = middle.mask.array & badPixelMask > 0
badPixelMask = mask.getPlaneBitMask(self.config.excludeMaskPlanes)
badPixels = mask.subset(middle.getBBox()).array & badPixelMask > 0
middle.image.array[badPixels] = 0

def setPeakSignificance(self, exposure, footprints, threshold, negative=False):
Expand Down
24 changes: 24 additions & 0 deletions tests/test_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,30 @@ def checkExposure(original, doTempLocalBackground, doTempWideBackground):
checkExposure(original, False, True)
checkExposure(original, True, True)

def test_removeBadPixels(self):
"""Test that if we set a NO_DATA region on top of a source,
One fewer sources is detected than when we don't do that."""
badMaskPlanes = ["NO_DATA", ]
exposure_regular, schema, numX, numY, starSigma = self._create_exposure()
exposure_removed = exposure_regular.clone()

# Define a region on one test exposure we will set to NO_DATA, blocking out one source
region = lsst.geom.Box2I(exposure_removed.getXY0(), lsst.geom.Extent2I(25, 35))
exposure_removed[region].mask.array |= exposure_removed.mask.getPlaneBitMask(badMaskPlanes)

config = SourceDetectionTask.ConfigClass()
config.reEstimateBackground = False
config.thresholdType = "stdev"
config.excludeMaskPlanes = badMaskPlanes
task = SourceDetectionTask(config=config, schema=schema)

# The regular test exposure finds 25 sources
self._check_detectFootprints(exposure_regular, numX, numY, starSigma, task, config, doSmooth=True)

# Modify numx and numy, which check_detectFootprints multiplies, to yield 24 instead of 25
self.assertEqual(numX, numY)
self._check_detectFootprints(exposure_removed, numX-1, numY+1, starSigma, task, config, doSmooth=True)


class TestMemory(lsst.utils.tests.MemoryTestCase):
pass
Expand Down

0 comments on commit f8bc214

Please sign in to comment.