Skip to content

Commit

Permalink
Merge pull request #349 from lsst/tickets/DM-47257
Browse files Browse the repository at this point in the history
DM-47257: matching kernel metadata metrics
  • Loading branch information
isullivan authored Oct 31, 2024
2 parents ebdfc2c + f787559 commit 376feae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
24 changes: 12 additions & 12 deletions python/lsst/ip/diffim/detectAndMeasure.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def processResults(self, science, matchedTemplate, difference, sources, idFactor
``diaSources`` : `lsst.afw.table.SourceCatalog`
The catalog of detected sources.
"""
self.metadata.add("nUnmergedDiaSources", len(sources))
self.metadata["nUnmergedDiaSources"] = len(sources)
if self.config.doMerge:
fpSet = positiveFootprints
fpSet.merge(negativeFootprints, self.config.growFootprint,
Expand All @@ -416,7 +416,7 @@ def processResults(self, science, matchedTemplate, difference, sources, idFactor
initialDiaSources.getTable().setIdFactory(idFactory)
initialDiaSources.setMetadata(self.algMetadata)

self.metadata.add("nMergedDiaSources", len(initialDiaSources))
self.metadata["nMergedDiaSources"] = len(initialDiaSources)

if self.config.doMaskStreaks:
streakInfo = self._runStreakMasking(difference.maskedImage)
Expand Down Expand Up @@ -514,7 +514,7 @@ def _removeBadSources(self, diaSources):
self.log.debug("Found %d unphysical sources with flag %s.", nBad, flag)
selector &= ~flags
nBadTotal = np.count_nonzero(~selector)
self.metadata.add("nRemovedBadFlaggedSources", nBadTotal)
self.metadata["nRemovedBadFlaggedSources"] = nBadTotal
self.log.info("Removed %d unphysical sources.", nBadTotal)
return diaSources[selector].copy(deep=True)

Expand All @@ -535,7 +535,7 @@ def addSkySources(self, diaSources, mask, seed,
if subtask is None:
subtask = self.skySources
skySourceFootprints = subtask.run(mask=mask, seed=seed, catalog=diaSources)
self.metadata.add(f"n_{subtask.getName()}", len(skySourceFootprints))
self.metadata[f"n_{subtask.getName()}"] = len(skySourceFootprints)

def measureDiaSources(self, diaSources, science, difference, matchedTemplate):
"""Use (matched) template and science image to constrain dipole fitting.
Expand Down Expand Up @@ -613,23 +613,23 @@ def calculateMetrics(self, difference):
"""
mask = difference.mask
badPix = (mask.array & mask.getPlaneBitMask(self.config.detection.excludeMaskPlanes)) > 0
self.metadata.add("nGoodPixels", np.sum(~badPix))
self.metadata.add("nBadPixels", np.sum(badPix))
self.metadata["nGoodPixels"] = np.sum(~badPix)
self.metadata["nBadPixels"] = np.sum(badPix)
detPosPix = (mask.array & mask.getPlaneBitMask("DETECTED")) > 0
detNegPix = (mask.array & mask.getPlaneBitMask("DETECTED_NEGATIVE")) > 0
self.metadata.add("nPixelsDetectedPositive", np.sum(detPosPix))
self.metadata.add("nPixelsDetectedNegative", np.sum(detNegPix))
self.metadata["nPixelsDetectedPositive"] = np.sum(detPosPix)
self.metadata["nPixelsDetectedNegative"] = np.sum(detNegPix)
detPosPix &= badPix
detNegPix &= badPix
self.metadata.add("nBadPixelsDetectedPositive", np.sum(detPosPix))
self.metadata.add("nBadPixelsDetectedNegative", np.sum(detNegPix))
self.metadata["nBadPixelsDetectedPositive"] = np.sum(detPosPix)
self.metadata["nBadPixelsDetectedNegative"] = np.sum(detNegPix)

metricsMaskPlanes = list(mask.getMaskPlaneDict().keys())
for maskPlane in metricsMaskPlanes:
try:
self.metadata.add("%s_mask_fraction"%maskPlane.lower(), evaluateMaskFraction(mask, maskPlane))
self.metadata["%s_mask_fraction"%maskPlane.lower()] = evaluateMaskFraction(mask, maskPlane)
except InvalidParameterError:
self.metadata.add("%s_mask_fraction"%maskPlane.lower(), -1)
self.metadata["%s_mask_fraction"%maskPlane.lower()] = -1
self.log.info("Unable to calculate metrics for mask plane %s: not in image"%maskPlane)

def _runStreakMasking(self, maskedImage):
Expand Down
26 changes: 13 additions & 13 deletions python/lsst/ip/diffim/subtractImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ def run(self, template, science, sources, visitSummary=None):
)
self.log.info("Science PSF FWHM: %f pixels", sciencePsfSize)
self.log.info("Template PSF FWHM: %f pixels", templatePsfSize)
self.metadata.add("sciencePsfSize", sciencePsfSize)
self.metadata.add("templatePsfSize", templatePsfSize)
self.metadata["sciencePsfSize"] = sciencePsfSize
self.metadata["templatePsfSize"] = templatePsfSize

# Calculate estimated image depths, i.e., limiting magnitudes
maglim_science = self._calculateMagLim(science, fallbackPsfSize=sciencePsfSize)
Expand All @@ -429,9 +429,9 @@ def run(self, template, science, sources, visitSummary=None):
else:
fluxlim_template = (maglim_template*u.ABmag).to_value(u.nJy)
maglim_diffim = (np.sqrt(fluxlim_science**2 + fluxlim_template**2)*u.nJy).to(u.ABmag).value
self.metadata.add("scienceLimitingMagnitude", maglim_science)
self.metadata.add("templateLimitingMagnitude", maglim_template)
self.metadata.add("diffimLimitingMagnitude", maglim_diffim)
self.metadata["scienceLimitingMagnitude"] = maglim_science
self.metadata["templateLimitingMagnitude"] = maglim_template
self.metadata["diffimLimitingMagnitude"] = maglim_diffim

if self.config.mode == "auto":
convolveTemplate = _shapeTest(template,
Expand Down Expand Up @@ -460,10 +460,10 @@ def run(self, template, science, sources, visitSummary=None):
sourceMask.array |= template[science.getBBox()].mask.array
selectSources = self._sourceSelector(sources, sourceMask)
if convolveTemplate:
self.metadata.add("convolvedExposure", "Template")
self.metadata["convolvedExposure"] = "Template"
subtractResults = self.runConvolveTemplate(template, science, selectSources)
else:
self.metadata.add("convolvedExposure", "Science")
self.metadata["convolvedExposure"] = "Science"
subtractResults = self.runConvolveScience(template, science, selectSources)

except (RuntimeError, lsst.pex.exceptions.Exception) as e:
Expand Down Expand Up @@ -766,7 +766,7 @@ def _convolveExposure(self, exposure, kernel, convolutionControl,
if interpolateBadMaskPlanes and self.config.badMaskPlanes is not None:
nInterp = _interpolateImage(convolvedExposure.maskedImage,
self.config.badMaskPlanes)
self.metadata.add("nInterpolated", nInterp)
self.metadata["nInterpolated"] = nInterp
convolvedImage = lsst.afw.image.MaskedImageF(convolvedExposure.getBBox())
lsst.afw.math.convolve(convolvedImage, convolvedExposure.maskedImage, kernel, convolutionControl)
convolvedExposure.setMaskedImage(convolvedImage)
Expand Down Expand Up @@ -823,7 +823,7 @@ def _sourceSelector(self, sources, mask):
"%i selected but %i needed for the calculation.",
len(selectSources), self.config.minKernelSources)
raise RuntimeError("Cannot compute PSF matching kernel: too few sources selected.")
self.metadata.add("nPsfSources", len(selectSources))
self.metadata["nPsfSources"] = len(selectSources)

return selectSources

Expand Down Expand Up @@ -893,7 +893,7 @@ def _prepareInputs(self, template, science, visitSummary=None):
exceptionMessage="Not attempting subtraction. To force subtraction,"
" set config requiredTemplateFraction=0"
)
self.metadata.add("templateCoveragePercent", 100*templateCoverageFraction)
self.metadata["templateCoveragePercent"] = 100*templateCoverageFraction

if self.config.doScaleVariance:
# Scale the variance of the template and science images before
Expand All @@ -902,9 +902,9 @@ def _prepareInputs(self, template, science, visitSummary=None):
templateVarFactor = self.scaleVariance.run(template.maskedImage)
sciVarFactor = self.scaleVariance.run(science.maskedImage)
self.log.info("Template variance scaling factor: %.2f", templateVarFactor)
self.metadata.add("scaleTemplateVarianceFactor", templateVarFactor)
self.metadata["scaleTemplateVarianceFactor"] = templateVarFactor
self.log.info("Science variance scaling factor: %.2f", sciVarFactor)
self.metadata.add("scaleScienceVarianceFactor", sciVarFactor)
self.metadata["scaleScienceVarianceFactor"] = sciVarFactor

# Erase existing detection mask planes.
# We don't want the detection mask from the science image
Expand Down Expand Up @@ -1055,7 +1055,7 @@ def run(self, template, science, sources, visitSummary=None):
scienceKernel = science.psf.getKernel()
matchedScience = self._convolveExposure(science, scienceKernel, self.convolutionControl,
interpolateBadMaskPlanes=True)
self.metadata.add("convolvedExposure", "Preconvolution")
self.metadata["convolvedExposure"] = "Preconvolution"
try:
selectSources = self._sourceSelector(sources, matchedScience.mask)
subtractResults = self.runPreconvolve(template, science, matchedScience,
Expand Down

0 comments on commit 376feae

Please sign in to comment.