From c6874a267b2b31fa66215266746fb9c7c43a969d Mon Sep 17 00:00:00 2001 From: Meredith Rawls Date: Wed, 29 Jan 2025 15:54:11 -0800 Subject: [PATCH] Fix bug in deblending diaSources with negative fluxes. This does not fix a separate footprint merging bug. --- python/lsst/ip/diffim/detectAndMeasure.py | 28 ++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/python/lsst/ip/diffim/detectAndMeasure.py b/python/lsst/ip/diffim/detectAndMeasure.py index 7d2b0bd6..8119f279 100644 --- a/python/lsst/ip/diffim/detectAndMeasure.py +++ b/python/lsst/ip/diffim/detectAndMeasure.py @@ -526,13 +526,35 @@ def makeFootprints(sources): footprints.setFootprints([src.getFootprint() for src in sources]) return footprints - def deblend(footprints): + def deblend(footprints, negative=False): """Deblend a positive or negative footprint set, and return the deblended children. + + Parameters + ---------- + footprints : `lsst.afw.detection.FootprintSet` + negative : `bool` + Set True if the footprints contain negative fluxes + + Returns + ------- + sources : `lsst.afw.table.SourceCatalog` """ sources = afwTable.SourceCatalog(self.schema) footprints.makeSources(sources) - self.deblend.run(exposure=difference, sources=sources) + if negative: + # Invert the image so the deblender can run on positive peaks + difference_inverted = difference.clone() + difference_inverted.image *= -1 + self.deblend.run(exposure=difference_inverted, sources=sources) + children = sources[sources["parent"] != 0] + # Set the heavy footprint pixel values back to reality + for child in children: + footprint = child.getFootprint() + array = footprint.getImageArray() + array *= -1 + else: + self.deblend.run(exposure=difference, sources=sources) self.setPrimaryFlags.run(sources) children = sources["detect_isDeblendedSource"] == 1 sources = sources[children].copy(deep=True) @@ -541,7 +563,7 @@ def deblend(footprints): return sources.copy(deep=True) positives = deblend(positiveFootprints) - negatives = deblend(negativeFootprints) + negatives = deblend(negativeFootprints, negative=True) sources = afwTable.SourceCatalog(self.schema) sources.reserve(len(positives) + len(negatives))