Coverage for python/lsst/verify/bin/inspectjob.py : 16%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of verify.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21"""Print the measurements and metadata in lsst.verify JSON files.
23This script takes as arguments one or more lsst.verify JSON files, and prints
24the top-level metadata and a summary of any measurements.
26This script does not print information about metrics or specifications.
27"""
29__all__ = ["main", "inspect_job"]
31import argparse
32import json
34from lsst.verify import Job
37def _is_measurement_metadata(key, metrics):
38 """Test whether a job-level metadata key is really measurement metadata.
40 Parameters
41 ----------
42 key : `str`
43 The metadata key to test.
44 metrics : iterable of `lsst.verify.Name` or of `str`
45 The metrics recorded in the job.
47 Returns
48 -------
49 result : `bool`
50 `True` if ``key`` represents measurement metadata, `False` if it
51 represents purely job-level metadata.
52 """
53 for metric in metrics:
54 if str(metric) in key:
55 return True
56 return False
59def _simplify_key(key, prefix):
60 """Remove a prefix from a key, if it's present.
62 Parameters
63 ----------
64 key : `str`
65 The key to simplify.
66 prefix : `str`
67 The prefix to remove from ``key``.
69 Returns
70 -------
71 simplifiedKey : `str`
72 ``key`` with any initial ``prefix`` removed
73 """
74 if key.startswith(prefix):
75 return key.replace(prefix, "", 1)
76 else:
77 return key
80def inspect_job(job):
81 """Present the measurements in a Job object.
83 The measurements and any metadata are printed to standard output.
85 Parameters
86 ----------
87 job : `lsst.verify.Job`
88 The Job to examine.
89 """
90 # Leave enough space for output so that all '=' characters are aligned
91 max_metric_length = max([len(str(metric)) for metric in job.measurements])
93 print("Common metadata:")
94 for key, value in job.meta.items():
95 if _is_measurement_metadata(key, job.measurements.keys()):
96 continue
97 print("%*s = %s" % (max_metric_length, key, value))
99 print("\nMeasurements:")
100 for metric, measurement in job.measurements.items():
101 pretty_quantity = measurement.quantity.round(4)
102 if measurement.notes:
103 prefix = str(measurement.metric_name) + "."
104 # Raw representation of measurement.notes hard to read
105 simple_notes = {_simplify_key(key, prefix): value
106 for key, value in measurement.notes.items()}
107 print("%*s = %10s (%s)"
108 % (max_metric_length, metric, pretty_quantity, simple_notes))
109 else:
110 print("%*s = %10s" % (max_metric_length, metric, pretty_quantity))
113def build_argparser():
114 """Construct an argument parser for the ``inspect_job.py`` script.
116 Returns
117 -------
118 argparser : `argparse.ArgumentParser`
119 The argument parser that defines the ``inspect_job.py`` command-line
120 interface.
121 """
122 parser = argparse.ArgumentParser(
123 description=__doc__,
124 formatter_class=argparse.RawDescriptionHelpFormatter,
125 epilog='More information is available at https://pipelines.lsst.io.')
126 parser.add_argument(
127 'json_paths',
128 nargs='+',
129 metavar='json',
130 help='lsst.verify JSON file, or files (``*verify.json``).')
131 return parser
134def main():
135 """Present all Job files.
136 """
137 args = build_argparser().parse_args()
138 for filename in args.json_paths:
139 if len(args.json_paths) > 1:
140 print("\n%s:" % filename)
141 with open(filename) as f:
142 job = Job.deserialize(**json.load(f))
143 inspect_job(job)