Coverage for python/lsst/verify/output.py: 22%

17 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-12 10:50 +0000

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__all__ = ['output_quantities'] 

22 

23from .job import Job 

24from .measurement import Measurement 

25from .naming import Name 

26 

27 

28def output_quantities(package_name, quantities, suffix=None, 

29 include_metrics=False, metrics_package='verify_metrics'): 

30 r"""Output measurements, as `astropy.units.Quantity` objects, from a 

31 pipeline task execution to a `lsst.verify`-formatted JSON file. 

32 

33 Parameters 

34 ---------- 

35 package_name : `str` 

36 Name of the package producing measurements. This name is used two ways: 

37 

38 1. Make fully-qualified metric names from keys in the ``quantities`` 

39 dictionary. For example, if a ``quantities`` dict has a key-value 

40 pair ``{'PA1': 5 * u.mmag}`` and ``package_name='validate_drp'``, 

41 the fully-qualified metric name is ``'validate_drp.PA1'``. 

42 2. As a filename prefix for the output JSON file. 

43 

44 quantities : `dict` of `astropy.units.Quantity` values 

45 Dictionary of measurements as plain `astropy.units.Quantity` 

46 instances. Each key is the name of a metric. If metric names are 

47 not fully-specified (in ``package.metric`` format), the package 

48 name can be provided with the ``package_name`` argument. 

49 

50 suffix : `str`, optional 

51 Additional suffix to add to the output JSON filename:: 

52 

53 {package_name}_{suffix}.verify.json 

54 

55 The suffix may be used to distinguish measurement output files from 

56 different tasks in the same package. 

57 

58 include_metrics : `bool`, optional 

59 Metric and specification definitions are included in the JSON output 

60 if set to `True`. The metric and specification definitions are 

61 loaded from a metric package indicated by the ``metrics_package`` 

62 argument. Normally tasks do not need to include metric definitions if 

63 a post-processing step is used. Default: `False`. 

64 

65 metrics_package : `str`, optional 

66 Name of the metrics package to obtain metrics from if 

67 ``include_metrics`` is `True`. Default is ``'verify_metrics'``. 

68 

69 Returns 

70 ------- 

71 filename : `str` 

72 Filename where the JSON file was written. 

73 

74 See also 

75 -------- 

76 lsst.verify.Job.write 

77 

78 Notes 

79 ----- 

80 This function is designed for lightweight `lsst.verify` framework usage. 

81 Rather than maintaining `Job` and `Measurement` objects, 

82 a task can simply record metric measurements as `astropy.units.Quantity` 

83 objects. With `output_quantities`, the task can output these measurements 

84 in a standardized `lsst.verify` JSON format. Post-processing tools 

85 can load this data for local analysis, or submit it to the 

86 https://squash.lsst.codes dashboard service. 

87 

88 Tasks that need to include `Blob`\ s, `Measurement.extras` or query 

89 `Metric` objects should create a `Job` instance and use 

90 `Job.write` instead. 

91 """ 

92 if include_metrics: 

93 job = Job.load_metrics_package(metrics_package, subset=package_name) 

94 else: 

95 job = Job() 

96 

97 for name, quantity in quantities.items(): 

98 metric_name = Name(package=package_name, metric=name) 

99 measurement = Measurement(metric_name, quantity=quantity) 

100 job.measurements.insert(measurement) 

101 

102 if suffix is not None: 

103 filename = '{package}_{suffix}.verify.json'.format( 

104 package=package_name, suffix=suffix) 

105 else: 

106 filename = '{package}.verify.json'.format(package=package_name) 

107 

108 job.write(filename) 

109 

110 return filename