Coverage for python/lsst/ap/verify/pipeline_driver.py : 20%

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# This file is part of ap_verify.
3#
4# Developed for the LSST Data Management System.
5# This product includes software developed by the LSST Project
6# (http://www.lsst.org).
7# See the COPYRIGHT file at the top-level directory of this distribution
8# for details of code ownership.
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program. If not, see <http://www.gnu.org/licenses/>.
22#
24"""Interface between `ap_verify` and `ap_pipe`.
26This module handles calling `ap_pipe` and converting any information
27as needed.
28"""
30__all__ = ["ApPipeParser", "runApPipeGen2", "runApPipeGen3"]
32import argparse
33import os
35import lsst.log
36from lsst.utils import getPackageDir
37import lsst.daf.butler as dafButler
38import lsst.pipe.base as pipeBase
39import lsst.ctrl.mpexec as ctrlMpexec
40import lsst.ap.pipe as apPipe
41from lsst.ap.pipe.make_apdb import makeApdb
44class ApPipeParser(argparse.ArgumentParser):
45 """An argument parser for data needed by ``ap_pipe`` activities.
47 This parser is not complete, and is designed to be passed to another parser
48 using the `parent` parameter.
49 """
51 def __init__(self):
52 defaultPipeline = os.path.join(getPackageDir("ap_verify"), "pipelines", "ApVerify.yaml")
54 # Help and documentation will be handled by main program's parser
55 argparse.ArgumentParser.__init__(self, add_help=False)
56 # namespace.dataIds will always be a list of 0 or more nonempty strings, regardless of inputs.
57 # TODO: in Python 3.8+, action='extend' handles nargs='?' more naturally than 'append'.
58 self.add_argument('--id', dest='dataIds', action=self.AppendOptional, nargs='?', default=[],
59 help='An identifier for the data to process.')
60 self.add_argument("-p", "--pipeline", default=defaultPipeline,
61 help="A custom version of the ap_verify pipeline (e.g., with different metrics).")
62 self.add_argument("-j", "--processes", default=1, type=int,
63 help="Number of processes to use.")
64 self.add_argument("--skip-pipeline", action="store_true",
65 help="Do not run the AP pipeline itself. This argument is useful "
66 "for testing metrics on a fixed data set.")
68 class AppendOptional(argparse.Action):
69 """A variant of the built-in "append" action that ignores None values
70 instead of appending them.
71 """
72 # This class can't safely inherit from the built-in "append" action
73 # because there is no public class that implements it.
74 def __call__(self, parser, namespace, values, option_string=None):
75 if values is not None:
76 try:
77 allValues = getattr(namespace, self.dest)
78 allValues.append(values)
79 except AttributeError:
80 setattr(namespace, self.dest, [values])
83def runApPipeGen2(workspace, parsedCmdLine):
84 """Run `ap_pipe` on this object's dataset.
86 Parameters
87 ----------
88 workspace : `lsst.ap.verify.workspace.WorkspaceGen2`
89 The abstract location containing input and output repositories.
90 parsedCmdLine : `argparse.Namespace`
91 Command-line arguments, including all arguments supported by `ApPipeParser`.
93 Returns
94 -------
95 apPipeReturn : `Struct`
96 The `Struct` returned from `~lsst.ap.pipe.ApPipeTask.parseAndRun` with
97 ``doReturnResults=False``. This object is valid even if
98 `~lsst.ap.pipe.ApPipeTask` was never run.
99 """
100 log = lsst.log.Log.getLogger('ap.verify.pipeline_driver.runApPipeGen2')
102 configArgs = _getConfigArguments(workspace)
103 makeApdb(configArgs)
105 pipelineArgs = [workspace.dataRepo,
106 "--output", workspace.outputRepo,
107 "--calib", workspace.calibRepo,
108 "--template", workspace.templateRepo]
109 pipelineArgs.extend(configArgs)
110 if parsedCmdLine.dataIds:
111 for singleId in parsedCmdLine.dataIds:
112 pipelineArgs.extend(["--id", *singleId.split(" ")])
113 else:
114 pipelineArgs.extend(["--id"])
115 pipelineArgs.extend(["--processes", str(parsedCmdLine.processes)])
116 pipelineArgs.extend(["--noExit"])
118 if not parsedCmdLine.skip_pipeline:
119 results = apPipe.ApPipeTask.parseAndRun(pipelineArgs)
120 log.info('Pipeline complete')
121 else:
122 log.info('Skipping AP pipeline entirely.')
123 apPipeParser = apPipe.ApPipeTask._makeArgumentParser()
124 apPipeParsed = apPipeParser.parse_args(config=apPipe.ApPipeTask.ConfigClass(), args=pipelineArgs)
125 results = pipeBase.Struct(
126 argumentParser=apPipeParser,
127 parsedCmd=apPipeParsed,
128 taskRunner=apPipe.ApPipeTask.RunnerClass(TaskClass=apPipe.ApPipeTask, parsedCmd=apPipeParsed),
129 resultList=[],
130 )
132 return results
135def runApPipeGen3(workspace, parsedCmdLine):
136 """Run `ap_pipe` on this object's dataset.
138 Parameters
139 ----------
140 workspace : `lsst.ap.verify.workspace.WorkspaceGen3`
141 The abstract location containing input and output repositories.
142 parsedCmdLine : `argparse.Namespace`
143 Command-line arguments, including all arguments supported by `ApPipeParser`.
144 """
145 log = lsst.log.Log.getLogger('ap.verify.pipeline_driver.runApPipeGen3')
147 # Currently makeApdb has different argument conventions from Gen 3; see DM-22663
148 makeApdb(_getConfigArguments(workspace))
150 pipelineArgs = ["run",
151 "--butler-config", workspace.repo,
152 "--pipeline", parsedCmdLine.pipeline,
153 ]
154 # TODO: collections should be determined exclusively by Workspace.workButler,
155 # but I can't find a way to hook that up to the graph builder. So use the CLI
156 # for now and revisit once DM-26239 is done.
157 pipelineArgs.extend(_getCollectionArguments(workspace))
158 pipelineArgs.extend(_getConfigArgumentsGen3(workspace))
159 if parsedCmdLine.dataIds:
160 for singleId in parsedCmdLine.dataIds:
161 pipelineArgs.extend(["--data-query", singleId])
162 pipelineArgs.extend(["--processes", str(parsedCmdLine.processes)])
163 pipelineArgs.extend(["--register-dataset-types"])
165 if not parsedCmdLine.skip_pipeline:
166 # TODO: generalize this code in DM-26028
167 activator = ctrlMpexec.CmdLineFwk()
168 # TODO: work off of workspace.workButler after DM-26239
169 results = activator.parseAndRun(pipelineArgs)
171 log.info('Pipeline complete.')
172 return results
173 else:
174 log.info('Skipping AP pipeline entirely.')
177def _getConfigArguments(workspace):
178 """Return the config options for running ApPipeTask on this workspace, as
179 command-line arguments.
181 Parameters
182 ----------
183 workspace : `lsst.ap.verify.workspace.WorkspaceGen2`
184 A Workspace whose config directory may contain an
185 `~lsst.ap.pipe.ApPipeTask` config.
187 Returns
188 -------
189 args : `list` of `str`
190 Command-line arguments calling ``--config`` or ``--configFile``,
191 following the conventions of `sys.argv`.
192 """
193 overrideFile = apPipe.ApPipeTask._DefaultName + ".py"
194 overridePath = os.path.join(workspace.configDir, overrideFile)
196 args = ["--configfile", overridePath]
197 # ApVerify will use the sqlite hooks for the Apdb.
198 args.extend(["--config", "diaPipe.apdb.db_url=sqlite:///" + workspace.dbLocation])
199 args.extend(["--config", "diaPipe.apdb.isolation_level=READ_UNCOMMITTED"])
200 # Put output alerts into the workspace.
201 args.extend(["--config", "diaPipe.alertPackager.alertWriteLocation=" + workspace.alertLocation])
202 args.extend(["--config", "diaPipe.doPackageAlerts=True"])
204 return args
207def _getConfigArgumentsGen3(workspace):
208 """Return the config options for running the Gen 3 AP Pipeline on this
209 workspace, as command-line arguments.
211 Parameters
212 ----------
213 workspace : `lsst.ap.verify.workspace.WorkspaceGen3`
214 A Workspace whose config directory may contain various configs.
216 Returns
217 -------
218 args : `list` of `str`
219 Command-line arguments calling ``--config`` or ``--configFile``,
220 following the conventions of `sys.argv`.
221 """
222 args = [
223 # ApVerify will use the sqlite hooks for the Apdb.
224 "--config", "diaPipe:apdb.db_url=sqlite:///" + workspace.dbLocation,
225 "--config", "diaPipe:apdb.isolation_level=READ_UNCOMMITTED",
226 # Put output alerts into the workspace.
227 "--config", "diaPipe:alertPackager.alertWriteLocation=" + workspace.alertLocation,
228 "--config", "diaPipe:doPackageAlerts=True",
229 # TODO: the configs below should not be needed after DM-26140
230 "--configfile", "calibrate:" + os.path.join(workspace.configDir, "calibrate.py"),
231 "--configfile", "imageDifference:" + os.path.join(workspace.configDir, "imageDifference.py"),
232 ]
233 # TODO: reverse-engineering the instrument should not be needed after DM-26140
234 # pipetask will crash if there is more than one instrument
235 for idRecord in workspace.workButler.registry.queryDataIds("instrument").expanded():
236 className = idRecord.records["instrument"].class_name
237 args.extend(["--instrument", className])
239 return args
242def _getCollectionArguments(workspace):
243 """Return the collections for running the Gen 3 AP Pipeline on this
244 workspace, as command-line arguments.
246 Parameters
247 ----------
248 workspace : `lsst.ap.verify.workspace.WorkspaceGen3`
249 A Workspace with a Gen 3 repository.
251 Returns
252 -------
253 args : `list` of `str`
254 Command-line arguments calling ``--input`` or ``--output``,
255 following the conventions of `sys.argv`.
256 """
257 butler = workspace.workButler
258 inputs = set(butler.registry.queryCollections(collectionType=dafButler.CollectionType.RUN))
259 inputs.discard(workspace.runName)
260 return ["--input", ",".join(inputs),
261 "--output-run", workspace.runName,
262 ]