Hide keyboard shortcuts

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

# 

# This file is part of ap_verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

# 

 

"""Command-line program for running and analyzing AP pipeline. 

 

In addition to containing ap_verify's main function, this module manages 

command-line argument parsing. 

""" 

 

__all__ = ["runApVerify", "runIngestion"] 

 

import argparse 

import re 

 

import lsst.log 

from .dataset import Dataset 

from .ingestion import ingestDataset 

from .metrics import MetricsParser, checkSquashReady, AutoJob 

from .pipeline_driver import ApPipeParser, runApPipe, _getConfig 

from .measurements import measureFromButlerRepo, measureFromPpdb 

from .workspace import Workspace 

 

 

class _InputOutputParser(argparse.ArgumentParser): 

"""An argument parser for program-wide input and output. 

 

This parser is not complete, and is designed to be passed to another parser 

using the `parent` parameter. 

""" 

 

def __init__(self): 

# Help and documentation will be handled by main program's parser 

argparse.ArgumentParser.__init__(self, add_help=False) 

self.add_argument('--dataset', action=_DatasetAction, choices=Dataset.getSupportedDatasets(), 

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.') 

 

 

class _ApVerifyParser(argparse.ArgumentParser): 

"""An argument parser for data needed by the main ap_verify program. 

""" 

 

def __init__(self): 

argparse.ArgumentParser.__init__( 

self, 

description='Executes the LSST DM AP pipeline and analyzes its performance using metrics.', 

epilog='', 

parents=[_InputOutputParser(), ApPipeParser(), MetricsParser()], 

add_help=True) 

 

 

class _IngestOnlyParser(argparse.ArgumentParser): 

"""An argument parser for data needed by dataset ingestion. 

""" 

 

def __init__(self): 

argparse.ArgumentParser.__init__( 

self, 

description='Ingests a dataset into a pair of Butler repositories.' 

'The program will create a data repository in <OUTPUT>/ingested and a calib repository ' 

'in <OUTPUT>/calibingested. ' 

'These repositories may be used directly by ap_verify.py by ' 

'passing the same --output argument, or by other programs that accept ' 

'Butler repositories as input.', 

epilog='', 

parents=[_InputOutputParser()], 

add_help=True) 

 

 

class _FormattedType: 

"""An argparse type converter that requires strings in a particular format. 

 

Leaves the input as a string if it matches, else raises `argparse.ArgumentTypeError`. 

 

Parameters 

---------- 

fmt : `str` 

A regular expression that values must satisfy to be accepted. The *entire* string must match the 

expression in order to pass. 

msg : `str` 

An error string to display for invalid values. The first "%s" shall be filled with the 

invalid argument. 

""" 

def __init__(self, fmt, msg='"%s" does not have the expected format.'): 

fullFormat = fmt 

if not fullFormat.startswith('^'): 

fullFormat = '^' + fullFormat 

if not fullFormat.endswith('$'): 

fullFormat += '$' 

self._format = re.compile(fullFormat) 

self._message = msg 

 

def __call__(self, value): 

if self._format.match(value): 

return value 

else: 

raise argparse.ArgumentTypeError(self._message % value) 

 

 

class _DatasetAction(argparse.Action): 

"""A converter for dataset arguments. 

 

Not an argparse type converter so that the ``choices`` parameter can be 

expressed using strings; ``choices`` checks happen after type conversion 

but before actions. 

""" 

def __call__(self, _parser, namespace, values, _option_string=None): 

setattr(namespace, self.dest, Dataset(values)) 

 

 

def _measureFinalProperties(metricsJob, workspace, args): 

"""Measure any metrics that apply to the final result of the AP pipeline, 

rather than to a particular processing stage. 

 

Parameters 

---------- 

metricsJob : `lsst.verify.Job` 

The Job object to which to add any metric measurements made. 

workspace : `lsst.ap.verify.workspace.Workspace` 

The abstract location containing input and output repositories. 

args : `argparse.Namespace` 

All command-line arguments passed to this program, including those 

supported by `lsst.ap.verify.pipeline_driver.ApPipeParser`. 

""" 

measurements = [] 

measurements.extend(measureFromButlerRepo(workspace.outputRepo, args.dataId)) 

# TODO: Add butler storage and retrieval of the Ppdb config. DM-16645 

measurements.extend(measureFromPpdb(_getConfig(workspace).ppdb)) 

 

for measurement in measurements: 

metricsJob.measurements.insert(measurement) 

 

 

def runApVerify(cmdLine=None): 

"""Execute the AP pipeline while handling metrics. 

 

This is the main function for ``ap_verify``, and handles logging, 

command-line argument parsing, pipeline execution, and metrics 

generation. 

 

Parameters 

---------- 

cmdLine : `list` of `str` 

an optional command line used to execute `runApVerify` from other 

Python code. If `None`, `sys.argv` will be used. 

""" 

lsst.log.configure() 

log = lsst.log.Log.getLogger('ap.verify.ap_verify.main') 

# TODO: what is LSST's policy on exceptions escaping into main()? 

args = _ApVerifyParser().parse_args(args=cmdLine) 

checkSquashReady(args) 

log.debug('Command-line arguments: %s', args) 

 

workspace = Workspace(args.output) 

ingestDataset(args.dataset, workspace) 

 

with AutoJob(args) as job: 

log.info('Running pipeline...') 

runApPipe(job, workspace, args) 

_measureFinalProperties(job, workspace, args) 

 

 

def runIngestion(cmdLine=None): 

"""Ingest a dataset, but do not process it. 

 

This is the main function for ``ingest_dataset``, and handles logging, 

command-line argument parsing, and ingestion. 

 

Parameters 

---------- 

cmdLine : `list` of `str` 

an optional command line used to execute `runIngestion` from other 

Python code. If `None`, `sys.argv` will be used. 

""" 

lsst.log.configure() 

log = lsst.log.Log.getLogger('ap.verify.ap_verify.ingest') 

# TODO: what is LSST's policy on exceptions escaping into main()? 

args = _IngestOnlyParser().parse_args(args=cmdLine) 

log.debug('Command-line arguments: %s', args) 

 

workspace = Workspace(args.output) 

ingestDataset(args.dataset, workspace)