Skip to content

Commit

Permalink
Fix --metrics-file argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
kfindeisen committed Mar 1, 2019
1 parent 90e0368 commit 0a659fa
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 98 deletions.
2 changes: 0 additions & 2 deletions config/default_dataset_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# Import these modules to ensure the metrics are registered
import lsst.ap.association.metrics # noqa: F401

config.jobFileTemplate = "ap_verify.metricTask{id}.{dataId}.verify.json"

ppdbConfigs = ["totalUnassociatedDiaObjects"]
config.measurers = ppdbConfigs

Expand Down
2 changes: 0 additions & 2 deletions config/default_image_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import lsst.ip.diffim.metrics # noqa: F401
import lsst.ap.association.metrics # noqa: F401

config.jobFileTemplate = "ap_verify.metricTask{id}.{dataId}.verify.json"

metadataConfigs = ["numNewDiaObjects",
"numUnassociatedDiaObjects",
"fracUpdatedDiaObjects"]
Expand Down
95 changes: 2 additions & 93 deletions python/lsst/ap/verify/ap_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@
__all__ = ["runApVerify", "runIngestion"]

import argparse
import copy
import os
import re

import lsst.log
import lsst.utils
from lsst.verify.gen2tasks import MetricsControllerTask

from .dataset import Dataset
from .ingestion import ingestDataset
from .metrics import MetricsParser
from .metrics import MetricsParser, computeMetrics
from .pipeline_driver import ApPipeParser, runApPipe
from .workspace import Workspace

Expand All @@ -59,12 +55,6 @@ def __init__(self):
required=True, help='The source of data to pass through the pipeline.')
self.add_argument('--output', required=True,
help='The location of the workspace to use for pipeline repositories.')
self.add_argument('--dataset-metrics-config',
help='The config file specifying the dataset-level metrics to measure. '
'Defaults to config/default_dataset_metrics.py.')
self.add_argument('--image-metrics-config',
help='The config file specifying the image-level metrics to measure. '
'Defaults to config/default_image_metrics.py.')


class _ApVerifyParser(argparse.ArgumentParser):
Expand Down Expand Up @@ -139,87 +129,6 @@ def __call__(self, _parser, namespace, values, _option_string=None):
setattr(namespace, self.dest, Dataset(values))


def _measureFinalProperties(workspace, dataIds, args):
"""Measure any metrics that apply to the final result of the AP pipeline,
rather than to a particular processing stage.
Parameters
----------
workspace : `lsst.ap.verify.workspace.Workspace`
The abstract location containing input and output repositories.
dataIds : `lsst.pipe.base.DataIdContainer`
The data IDs ap_pipe was run on. Each data ID must be complete.
args : `argparse.Namespace`
Command-line arguments, including arguments controlling output.
"""
imageConfig = _getMetricsConfig(args.image_metrics_config, "default_image_metrics.py")
_runMetricTasks(imageConfig, dataIds.refList)

datasetConfig = _getMetricsConfig(args.dataset_metrics_config, "default_dataset_metrics.py")
_runMetricTasks(datasetConfig, [workspace.workButler.dataRef("apPipe_config")])


def _getMetricsConfig(userFile, defaultFile):
"""Load a metrics config based on program settings.
Parameters
----------
userFile : `str` or `None`
The path provided by the user for this config file.
defaultFile : `str`
The filename (not a path) of the default config file.
Returns
-------
config : `lsst.verify.gen2tasks.MetricsControllerConfig`
The config from ``userFile`` if the user provided one, otherwise the
default config.
"""
timingConfig = MetricsControllerTask.ConfigClass()

if userFile is not None:
timingConfig.load(userFile)
else:
timingConfig.load(os.path.join(lsst.utils.getPackageDir("ap_verify"), "config", defaultFile))
return timingConfig


def _runMetricTasks(config, dataRefs):
"""Run MetricControllerTask on a single dataset.
Parameters
----------
config : `lsst.verify.gen2tasks.MetricsControllerConfig`
The config for running `~lsst.verify.gen2tasks.MetricsControllerTask`.
dataRefs : `list` [`lsst.daf.persistence.ButlerDataRef`]
The data references over which to compute metrics. The granularity
determines the metric granularity; see
`MetricsControllerTask.runDataRef` for more details.
"""
allMetricTasks = MetricsControllerTask(config)
allMetricTasks.runDataRefs([_sanitizeRef(ref) for ref in dataRefs])


def _sanitizeRef(dataRef):
"""Remove data ID tags that can cause problems when loading arbitrary
dataset types.
Parameters
----------
dataRef : `lsst.daf.persistence.ButlerDataRef`
The dataref to sanitize.
Returns
-------
clean : `lsst.daf.persistence.ButlerDataRef`
A dataref that is safe to use.
"""
newDataRef = copy.deepcopy(dataRef)
if "hdu" in newDataRef.dataId:
del newDataRef.dataId["hdu"]
return newDataRef


def runApVerify(cmdLine=None):
"""Execute the AP pipeline while handling metrics.
Expand All @@ -244,7 +153,7 @@ def runApVerify(cmdLine=None):

log.info('Running pipeline...')
expandedDataIds = runApPipe(workspace, args)
_measureFinalProperties(workspace, expandedDataIds, args)
computeMetrics(workspace, expandedDataIds, args)


def runIngestion(cmdLine=None):
Expand Down
103 changes: 102 additions & 1 deletion python/lsst/ap/verify/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@
``ap_verify`` module or in the appropriate pipeline step, as appropriate.
"""

__all__ = ["MetricsParser"]
__all__ = ["MetricsParser", "computeMetrics"]

import argparse
import copy
import os
import warnings

import lsst.utils
from lsst.verify.gen2tasks import MetricsControllerTask


class MetricsParser(argparse.ArgumentParser):
"""An argument parser for data needed by metrics activities.
Expand All @@ -54,6 +59,12 @@ def __init__(self):
action=DeprecatedAction,
deprecationReason="SQuaSH upload is no longer supported",
help='Do NOT submit metrics to SQuaSH.')
self.add_argument('--dataset-metrics-config',
help='The config file specifying the dataset-level metrics to measure. '
'Defaults to config/default_dataset_metrics.py.')
self.add_argument('--image-metrics-config',
help='The config file specifying the image-level metrics to measure. '
'Defaults to config/default_image_metrics.py.')


class DeprecatedAction(argparse.Action):
Expand All @@ -78,3 +89,93 @@ def __call__(self, _parser, _namespace, _values, option_string=None):
message = "%s has been deprecated, because %s. It will be removed in a future version." \
% (option_string, self.reason)
warnings.warn(message, category=FutureWarning)


def computeMetrics(workspace, dataIds, args):
"""Measure any metrics that apply to the final result of the AP pipeline,
rather than to a particular processing stage.
Parameters
----------
workspace : `lsst.ap.verify.workspace.Workspace`
The abstract location containing input and output repositories.
dataIds : `lsst.pipe.base.DataIdContainer`
The data IDs ap_pipe was run on. Each data ID must be complete.
args : `argparse.Namespace`
Command-line arguments, including arguments controlling output.
"""
imageConfig = _getMetricsConfig(args.image_metrics_config,
"default_image_metrics.py",
args.metrics_file)
_runMetricTasks(imageConfig, dataIds.refList)

datasetConfig = _getMetricsConfig(args.dataset_metrics_config,
"default_dataset_metrics.py",
args.metrics_file)
_runMetricTasks(datasetConfig, [workspace.workButler.dataRef("apPipe_config")])


def _getMetricsConfig(userFile, defaultFile, metricsOutputTemplate=None):
"""Load a metrics config based on program settings.
Parameters
----------
userFile : `str` or `None`
The path provided by the user for this config file.
defaultFile : `str`
The filename (not a path) of the default config file.
metricsOutputTemplate : `str` or `None`
The files to which to write metrics. If not `None`, this argument
overrides any output files set by either config file.
Returns
-------
config : `lsst.verify.gen2tasks.MetricsControllerConfig`
The config from ``userFile`` if the user provided one, otherwise the
default config.
"""
timingConfig = MetricsControllerTask.ConfigClass()

if userFile is not None:
timingConfig.load(userFile)
else:
timingConfig.load(os.path.join(lsst.utils.getPackageDir("ap_verify"), "config", defaultFile))
if metricsOutputTemplate:
timingConfig.jobFileTemplate = metricsOutputTemplate
return timingConfig


def _runMetricTasks(config, dataRefs):
"""Run MetricControllerTask on a single dataset.
Parameters
----------
config : `lsst.verify.gen2tasks.MetricsControllerConfig`
The config for running `~lsst.verify.gen2tasks.MetricsControllerTask`.
dataRefs : `list` [`lsst.daf.persistence.ButlerDataRef`]
The data references over which to compute metrics. The granularity
determines the metric granularity; see
`MetricsControllerTask.runDataRef` for more details.
"""
allMetricTasks = MetricsControllerTask(config)
allMetricTasks.runDataRefs([_sanitizeRef(ref) for ref in dataRefs])


def _sanitizeRef(dataRef):
"""Remove data ID tags that can cause problems when loading arbitrary
dataset types.
Parameters
----------
dataRef : `lsst.daf.persistence.ButlerDataRef`
The dataref to sanitize.
Returns
-------
clean : `lsst.daf.persistence.ButlerDataRef`
A dataref that is safe to use.
"""
newDataRef = copy.deepcopy(dataRef)
if "hdu" in newDataRef.dataId:
del newDataRef.dataId["hdu"]
return newDataRef

0 comments on commit 0a659fa

Please sign in to comment.