Skip to content

Commit

Permalink
Merge pull request #328 from lsst/tickets/DM-46098
Browse files Browse the repository at this point in the history
DM-46098: Add NoWorkFound error handling to metadata metrics
  • Loading branch information
isullivan authored Dec 16, 2024
2 parents 763851a + c048ae9 commit fa6e285
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
50 changes: 37 additions & 13 deletions pipelines/apDetectorVisitQualityCore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,59 @@ tasks:
python: |
from lsst.analysis.tools.atools import *
diffimTaskCore:
class: lsst.analysis.tools.tasks.DiffimDetectorVisitMetricsAnalysisTask
class: lsst.analysis.tools.tasks.TaskMetadataAnalysisTask
config:
connections.outputName: diffimMetadata

atools.diffimMetadataMetric: DiffimMetadataMetricTool
connections.inputName: subtractImages_metadata
connections.outputName: diffimMetadata # Will be appended with "_metrics"
connections.storageClass: TaskMetadata
raiseNoWorkFoundOnEmptyMetadata: true
inputDimensions: ["instrument", "visit", "detector"]
atools.diffimMetadataMetric: TaskMetadataMetricTool
atools.diffimMetadataMetric.taskName: subtractImages
atools.diffimMetadataMetric.metrics:
# Format is "metric name in the metadata": units
nUnmergedDiaSources: ct
nMergedDiaSources: ct
nGoodPixels: ct
nBadPixels: ct
nPixelsDetectedPositive: ct
nPixelsDetectedNegative: ct
nBadPixelsDetectedPositive: ct
nBadPixelsDetectedNegative: ct
sciencePsfSize: pixel
templatePsfSize: pixel
scaleScienceVarianceFactor: ''
scaleTemplateVarianceFactor: ''
spatialConditionNum: ''
spatialKernelSum: ''
templateCoveragePercent: percent
atools.diffimMetadataMetric.subTaskNames:
# Format is "metric name in metadata: subtask that produced it"
spatialConditionNum: makeKernel
spatialKernelSum: makeKernel
python: |
from lsst.analysis.tools.atools import TaskMetadataMetricTool
detectionTaskCore:
class: lsst.analysis.tools.tasks.TaskMetadataAnalysisTask
config:
connections.inputName: detectAndMeasure_metadata
connections.outputName: detectAndMeasure_metadata # Will be appended with "_metrics"
connections.storageClass: TaskMetadata
raiseNoWorkFoundOnEmptyMetadata: true
inputDimensions: ["instrument", "visit", "detector"]
atools.diffimMetadataMetric: TaskMetadataMetricTool
atools.diffimMetadataMetric.taskName: detectAndMeasure
atools.diffimMetadataMetric.metrics:
# Format is "metric name in the metadata": units
nGoodPixels: ct
nBadPixels: ct
nPixelsDetectedPositive: ct
nPixelsDetectedNegative: ct
nBadPixelsDetectedPositive: ct
nBadPixelsDetectedNegative: ct
nUnmergedDiaSources: ct
nMergedDiaSources: ct
python: |
from lsst.analysis.tools.atools import DiffimMetadataMetricTool
from lsst.analysis.tools.atools import TaskMetadataMetricTool
associationCore:
class: lsst.analysis.tools.tasks.TaskMetadataAnalysisTask
config:
connections.inputName: diaPipe_metadata
connections.outputName: diaPipe_metadata # Should be appended with "_metrics"
connections.storageClass: TaskMetadata
raiseNoWorkFoundOnEmptyMetadata: true
inputDimensions: ["instrument", "visit", "detector"]
atools.diaPipeMetadataMetrics: TaskMetadataMetricTool
atools.diaPipeMetadataMetrics.taskName: diaPipe
Expand Down Expand Up @@ -209,6 +232,7 @@ subsets:
- analyzeAssocDiaSrcCore
- analyzeTrailedDiaSrcCore
- diffimTaskCore
- detectionTaskCore
- associationCore
- diffimTaskPlots
- initialPviCore
Expand Down
6 changes: 6 additions & 0 deletions python/lsst/analysis/tools/atools/diffimMetadataMetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@

__all__ = ("DiffimMetadataMetricTool",)

from deprecated.sphinx import deprecated
from lsst.pex.config import DictField

from ..actions.scalar import ValueAction
from ..interfaces import AnalysisTool


@deprecated(
reason=("This tool is superceded by TaskMetadataMetricTool, which should be used instead."),
version="v29.0",
category=FutureWarning,
)
class DiffimMetadataMetricTool(AnalysisTool):
"""This tool is designed to extract values from diffim task metadata"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import lsst.pex.config
import pandas as pd
from deprecated.sphinx import deprecated
from lsst.pipe.base import NoWorkFound, connectionTypes

from ..interfaces import AnalysisBaseConfig, AnalysisBaseConnections, AnalysisPipelineTask
Expand Down Expand Up @@ -57,6 +58,11 @@ class DiffimDetectorVisitMetricsAnalysisConfig(
)


@deprecated(
reason=("TaskMetadataAnalysisTask should be used instead."),
version="v29.0",
category=FutureWarning,
)
class DiffimDetectorVisitMetricsAnalysisTask(AnalysisPipelineTask):
ConfigClass = DiffimDetectorVisitMetricsAnalysisConfig
_DefaultName = "DiffimDetectorVisitMetricsAnalysis"
Expand All @@ -73,9 +79,8 @@ def runQuantum(self, butlerQC, inputRefs, outputRefs):
subtractTaskName = inputRefs.metadataSubtract.datasetType.name
subtractTaskName = subtractTaskName[: subtractTaskName.find("_")]
metadata |= inputs["metadataSubtract"].metadata[subtractTaskName].to_dict()
kernelSubtaskLabel = subtractTaskName + ":" + self.config.kernelSubtaskName
metadata |= inputs["metadataSubtract"].metadata[kernelSubtaskLabel].to_dict()
inputs.pop("metadataSubtract")
self.taskName = subtractTaskName
# Some metadata entries might have different lengths or simply floats.
# Pass the dict in a list to tell Pandas that this is one row in the
# dataframe.
Expand Down
34 changes: 31 additions & 3 deletions python/lsst/analysis/tools/tasks/metadataAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"TaskMetadataAnalysisTask",
)

from lsst.pex.config import ListField
from lsst.pex.config import Field, ListField
from lsst.pipe.base import NoWorkFound, connectionTypes

from ..interfaces import AnalysisBaseConfig, AnalysisBaseConnections, AnalysisPipelineTask
Expand Down Expand Up @@ -72,6 +72,11 @@ class MetadataAnalysisConfig(
dtype=str,
doc="The dimensions of the input dataset.",
)
raiseNoWorkFoundOnEmptyMetadata = Field(
dtype=bool,
default=False,
doc="Raise a NoWorkFound error if none of the configured metrics are in the task metadata.",
)


class DatasetMetadataAnalysisTask(AnalysisPipelineTask):
Expand Down Expand Up @@ -118,9 +123,32 @@ def runQuantum(self, butlerQC, inputRefs, outputRefs):
inputs = butlerQC.get(inputRefs)
plotInfo = self.parsePlotInfo(inputs, dataId)
metadata = inputs["data"].get().to_dict()
taskName = inputRefs.data.datasetType.name
taskName = taskName[: taskName.find("_")]
if not metadata:
taskName = inputRefs.data.datasetType.name
taskName = taskName[: taskName.find("_")]
raise NoWorkFound(f"No metadata entries for {taskName}.")
if self.config.raiseNoWorkFoundOnEmptyMetadata:
self.validateMetrics(metadata, taskName)
outputs = self.run(data=metadata, plotInfo=plotInfo)
butlerQC.put(outputs, outputRefs)

def validateMetrics(self, metadata, taskName):
"""Raise NoWorkFound if there are no metrics in the task metadata.
Parameters
----------
metadata : `dict`
The task metadata converted to a dict.
taskName : `str`
The name of the task to extract metadata from
Raises
------
NoWorkFound
If none of the metrics are in the metadata.
"""
for fieldName in self.config.atools.fieldNames:
for key in getattr(self.config.atools, fieldName).metrics.keys():
if key in metadata[taskName].keys():
return
raise NoWorkFound(f"None of the specified metrics were found in the {taskName} metadata")

0 comments on commit fa6e285

Please sign in to comment.