Skip to content

Commit

Permalink
Store diffim QA metrics in the Task metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
isullivan committed Oct 11, 2023
1 parent a0cc7aa commit d882fda
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion python/lsst/ip/diffim/detectAndMeasure.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from deprecated.sphinx import deprecated
import numpy as np

import lsst.afw.table as afwTable
import lsst.daf.base as dafBase
Expand Down Expand Up @@ -148,7 +149,7 @@ def setDefaults(self):
self.detection.thresholdValue = 5.0
self.detection.reEstimateBackground = False
self.detection.thresholdType = "pixel_stdev"
self.detection.excludeMaskPlanes = ["EDGE"]
self.detection.excludeMaskPlanes = ["EDGE", "SAT", "BAD", "INTRP"]

# Add filtered flux measurement, the correct measurement for pre-convolved images.
self.measurement.algorithms.names.add('base_PeakLikelihoodFlux')
Expand Down Expand Up @@ -343,6 +344,7 @@ def processResults(self, science, matchedTemplate, difference, sources, table,
``diaSources`` : `lsst.afw.table.SourceCatalog`
The catalog of detected sources.
"""
self.metadata.add("nUnmergedDiaSources", len(sources))
if self.config.doMerge:
fpSet = positiveFootprints
fpSet.merge(negativeFootprints, self.config.growFootprint,
Expand All @@ -352,6 +354,7 @@ def processResults(self, science, matchedTemplate, difference, sources, table,
self.log.info("Merging detections into %d sources", len(diaSources))
else:
diaSources = sources
self.metadata.add("nMergedDiaSources", len(diaSources))

if self.config.doSkySources:
self.addSkySources(diaSources, difference.mask, difference.info.id)
Expand All @@ -365,6 +368,7 @@ def processResults(self, science, matchedTemplate, difference, sources, table,
subtractedMeasuredExposure=difference,
diaSources=diaSources,
)
self.calculateMetrics(difference)

return measurementResults

Expand Down Expand Up @@ -449,6 +453,27 @@ def measureForcedSources(self, diaSources, science, wcs):
for diaSource, forcedSource in zip(diaSources, forcedSources):
diaSource.assign(forcedSource, mapper)

def calculateMetrics(self, difference):
"""Add image QA metrics to the Task metadata.
Parameters
----------
difference : `lsst.afw.image.Exposure`
The target image to calculate metrics for.
"""
mask = difference.mask
goodPix = (mask.array & mask.getPlaneBitMask(self.config.detection.excludeMaskPlanes)) == 0
self.metadata.add("nGoodPixels", np.sum(goodPix))
self.metadata.add("nBadPixels", np.sum(~goodPix))
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))
detPosPix *= ~goodPix
detNegPix *= ~goodPix
self.metadata.add("nBadPixelsDetectedPositive", np.sum(detPosPix))
self.metadata.add("nBadPixelsDetectedNegative", np.sum(detNegPix))


class DetectAndMeasureScoreConnections(DetectAndMeasureConnections):
scoreExposure = pipeBase.connectionTypes.Input(
Expand Down

0 comments on commit d882fda

Please sign in to comment.