Coverage for python/lsst/ctrl/mpexec/cmdLineFwk.py: 11%

399 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-05 18:04 -0800

1# This file is part of ctrl_mpexec. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

21 

22"""Module defining CmdLineFwk class and related methods. 

23""" 

24 

25__all__ = ['CmdLineFwk'] 

26 

27# ------------------------------- 

28# Imports of standard modules -- 

29# ------------------------------- 

30import argparse 

31import copy 

32import datetime 

33import fnmatch 

34import getpass 

35import logging 

36import re 

37import sys 

38from typing import Iterable, Optional, Tuple 

39import warnings 

40 

41# ----------------------------- 

42# Imports for other modules -- 

43# ----------------------------- 

44from lsst.daf.butler import ( 

45 Butler, 

46 CollectionSearch, 

47 CollectionType, 

48 Registry, 

49) 

50from lsst.daf.butler.registry import MissingCollectionError, RegistryDefaults 

51import lsst.pex.config as pexConfig 

52from lsst.pipe.base import (buildExecutionButler, GraphBuilder, Pipeline, 

53 PipelineDatasetTypes, QuantumGraph, TaskDef) 

54from lsst.obs.base import Instrument 

55from .dotTools import graph2dot, pipeline2dot 

56from .executionGraphFixup import ExecutionGraphFixup 

57from .mpGraphExecutor import MPGraphExecutor 

58from .preExecInit import PreExecInit 

59from .singleQuantumExecutor import SingleQuantumExecutor 

60from . import util 

61from lsst.utils import doImport 

62 

63# ---------------------------------- 

64# Local non-exported definitions -- 

65# ---------------------------------- 

66 

67_LOG = logging.getLogger(__name__.partition(".")[2]) 

68 

69 

70class _OutputChainedCollectionInfo: 

71 """A helper class for handling command-line arguments related to an output 

72 `~lsst.daf.butler.CollectionType.CHAINED` collection. 

73 

74 Parameters 

75 ---------- 

76 registry : `lsst.daf.butler.Registry` 

77 Butler registry that collections will be added to and/or queried from. 

78 name : `str` 

79 Name of the collection given on the command line. 

80 """ 

81 def __init__(self, registry: Registry, name: str): 

82 self.name = name 

83 try: 

84 self.chain = tuple(registry.getCollectionChain(name)) 

85 self.exists = True 

86 except MissingCollectionError: 

87 self.chain = () 

88 self.exists = False 

89 

90 def __str__(self): 

91 return self.name 

92 

93 name: str 

94 """Name of the collection provided on the command line (`str`). 

95 """ 

96 

97 exists: bool 

98 """Whether this collection already exists in the registry (`bool`). 

99 """ 

100 

101 chain: Tuple[str, ...] 

102 """The definition of the collection, if it already exists (`tuple`[`str`]). 

103 

104 Empty if the collection does not already exist. 

105 """ 

106 

107 

108class _OutputRunCollectionInfo: 

109 """A helper class for handling command-line arguments related to an output 

110 `~lsst.daf.butler.CollectionType.RUN` collection. 

111 

112 Parameters 

113 ---------- 

114 registry : `lsst.daf.butler.Registry` 

115 Butler registry that collections will be added to and/or queried from. 

116 name : `str` 

117 Name of the collection given on the command line. 

118 """ 

119 def __init__(self, registry: Registry, name: str): 

120 self.name = name 

121 try: 

122 actualType = registry.getCollectionType(name) 

123 if actualType is not CollectionType.RUN: 

124 raise TypeError(f"Collection '{name}' exists but has type {actualType.name}, not RUN.") 

125 self.exists = True 

126 except MissingCollectionError: 

127 self.exists = False 

128 

129 name: str 

130 """Name of the collection provided on the command line (`str`). 

131 """ 

132 

133 exists: bool 

134 """Whether this collection already exists in the registry (`bool`). 

135 """ 

136 

137 

138class _ButlerFactory: 

139 """A helper class for processing command-line arguments related to input 

140 and output collections. 

141 

142 Parameters 

143 ---------- 

144 registry : `lsst.daf.butler.Registry` 

145 Butler registry that collections will be added to and/or queried from. 

146 

147 args : `types.SimpleNamespace` 

148 Parsed command-line arguments. The following attributes are used, 

149 either at construction or in later methods. 

150 

151 ``output`` 

152 The name of a `~lsst.daf.butler.CollectionType.CHAINED` 

153 input/output collection. 

154 

155 ``output_run`` 

156 The name of a `~lsst.daf.butler.CollectionType.RUN` input/output 

157 collection. 

158 

159 ``extend_run`` 

160 A boolean indicating whether ``output_run`` should already exist 

161 and be extended. 

162 

163 ``replace_run`` 

164 A boolean indicating that (if `True`) ``output_run`` should already 

165 exist but will be removed from the output chained collection and 

166 replaced with a new one. 

167 

168 ``prune_replaced`` 

169 A boolean indicating whether to prune the replaced run (requires 

170 ``replace_run``). 

171 

172 ``inputs`` 

173 Input collections of any type; may be any type handled by 

174 `lsst.daf.butler.registry.CollectionSearch.fromExpression`. 

175 

176 ``butler_config`` 

177 Path to a data repository root or configuration file. 

178 

179 writeable : `bool` 

180 If `True`, a `Butler` is being initialized in a context where actual 

181 writes should happens, and hence no output run is necessary. 

182 

183 Raises 

184 ------ 

185 ValueError 

186 Raised if ``writeable is True`` but there are no output collections. 

187 """ 

188 def __init__(self, registry: Registry, args: argparse.Namespace, writeable: bool): 

189 if args.output is not None: 

190 self.output = _OutputChainedCollectionInfo(registry, args.output) 

191 else: 

192 self.output = None 

193 if args.output_run is not None: 

194 self.outputRun = _OutputRunCollectionInfo(registry, args.output_run) 

195 elif self.output is not None: 

196 if args.extend_run: 

197 if not self.output.chain: 

198 raise ValueError("Cannot use --extend-run option with non-existing or empty output chain") 

199 runName = self.output.chain[0] 

200 else: 

201 runName = "{}/{}".format(self.output, Instrument.makeCollectionTimestamp()) 

202 self.outputRun = _OutputRunCollectionInfo(registry, runName) 

203 elif not writeable: 

204 # If we're not writing yet, ok to have no output run. 

205 self.outputRun = None 

206 else: 

207 raise ValueError("Cannot write without at least one of (--output, --output-run).") 

208 # Recursively flatten any input CHAINED collections. We do this up 

209 # front so we can tell if the user passes the same inputs on subsequent 

210 # calls, even though we also flatten when we define the output CHAINED 

211 # collection. 

212 self.inputs = tuple(registry.queryCollections(args.input, flattenChains=True)) if args.input else () 

213 

214 def check(self, args: argparse.Namespace): 

215 """Check command-line options for consistency with each other and the 

216 data repository. 

217 

218 Parameters 

219 ---------- 

220 args : `types.SimpleNamespace` 

221 Parsed command-line arguments. See class documentation for the 

222 construction parameter of the same name. 

223 """ 

224 assert not (args.extend_run and args.replace_run), "In mutually-exclusive group in ArgumentParser." 

225 if self.inputs and self.output is not None and self.output.exists: 

226 # Passing the same inputs that were used to initialize the output 

227 # collection is allowed; this means they must _end_ with the same 

228 # collections, because we push new runs to the front of the chain. 

229 for c1, c2 in zip(self.inputs[::-1], self.output.chain[::-1]): 

230 if c1 != c2: 

231 raise ValueError( 

232 f"Output CHAINED collection {self.output.name!r} exists, but it ends with " 

233 "a different sequence of input collections than those given: " 

234 f"{c1!r} != {c2!r} in inputs={self.inputs} vs " 

235 f"{self.output.name}={self.output.chain}." 

236 ) 

237 if len(self.inputs) > len(self.output.chain): 

238 nNew = len(self.inputs) - len(self.output.chain) 

239 raise ValueError( 

240 f"Cannot add new input collections {self.inputs[:nNew]} after " 

241 "output collection is first created." 

242 ) 

243 if args.extend_run and self.outputRun is None: 

244 raise ValueError("Cannot --extend-run when no output collection is given.") 

245 if args.extend_run and not self.outputRun.exists: 

246 raise ValueError(f"Cannot --extend-run; output collection " 

247 f"'{self.outputRun.name}' does not exist.") 

248 if not args.extend_run and self.outputRun is not None and self.outputRun.exists: 

249 raise ValueError(f"Output run '{self.outputRun.name}' already exists, but " 

250 f"--extend-run was not given.") 

251 if args.prune_replaced and not args.replace_run: 

252 raise ValueError("--prune-replaced requires --replace-run.") 

253 if args.replace_run and (self.output is None or not self.output.exists): 

254 raise ValueError("--output must point to an existing CHAINED collection for --replace-run.") 

255 

256 @classmethod 

257 def _makeReadParts(cls, args: argparse.Namespace): 

258 """Common implementation for `makeReadButler` and 

259 `makeRegistryAndCollections`. 

260 

261 Parameters 

262 ---------- 

263 args : `types.SimpleNamespace` 

264 Parsed command-line arguments. See class documentation for the 

265 construction parameter of the same name. 

266 

267 Returns 

268 ------- 

269 butler : `lsst.daf.butler.Butler` 

270 A read-only butler constructed from the repo at 

271 ``args.butler_config``, but with no default collections. 

272 inputs : `lsst.daf.butler.registry.CollectionSearch` 

273 A collection search path constructed according to ``args``. 

274 self : `_ButlerFactory` 

275 A new `_ButlerFactory` instance representing the processed version 

276 of ``args``. 

277 """ 

278 butler = Butler(args.butler_config, writeable=False) 

279 self = cls(butler.registry, args, writeable=False) 

280 self.check(args) 

281 if self.output and self.output.exists: 

282 if args.replace_run: 

283 replaced = self.output.chain[0] 

284 inputs = self.output.chain[1:] 

285 _LOG.debug("Simulating collection search in '%s' after removing '%s'.", 

286 self.output.name, replaced) 

287 else: 

288 inputs = [self.output.name] 

289 else: 

290 inputs = list(self.inputs) 

291 if args.extend_run: 

292 inputs.insert(0, self.outputRun.name) 

293 inputs = CollectionSearch.fromExpression(inputs) 

294 return butler, inputs, self 

295 

296 @classmethod 

297 def makeReadButler(cls, args: argparse.Namespace) -> Butler: 

298 """Construct a read-only butler according to the given command-line 

299 arguments. 

300 

301 Parameters 

302 ---------- 

303 args : `types.SimpleNamespace` 

304 Parsed command-line arguments. See class documentation for the 

305 construction parameter of the same name. 

306 

307 Returns 

308 ------- 

309 butler : `lsst.daf.butler.Butler` 

310 A read-only butler initialized with the collections specified by 

311 ``args``. 

312 """ 

313 butler, inputs, _ = cls._makeReadParts(args) 

314 _LOG.debug("Preparing butler to read from %s.", inputs) 

315 return Butler(butler=butler, collections=inputs) 

316 

317 @classmethod 

318 def makeRegistryAndCollections(cls, args: argparse.Namespace) -> \ 

319 Tuple[Registry, CollectionSearch, Optional[str]]: 

320 """Return a read-only registry, a collection search path, and the name 

321 of the run to be used for future writes. 

322 

323 Parameters 

324 ---------- 

325 args : `types.SimpleNamespace` 

326 Parsed command-line arguments. See class documentation for the 

327 construction parameter of the same name. 

328 

329 Returns 

330 ------- 

331 registry : `lsst.daf.butler.Registry` 

332 Butler registry that collections will be added to and/or queried 

333 from. 

334 inputs : `lsst.daf.butler.registry.CollectionSearch` 

335 Collections to search for datasets. 

336 run : `str` or `None` 

337 Name of the output `~lsst.daf.butler.CollectionType.RUN` collection 

338 if it already exists, or `None` if it does not. 

339 """ 

340 butler, inputs, self = cls._makeReadParts(args) 

341 run = self.outputRun.name if args.extend_run else None 

342 _LOG.debug("Preparing registry to read from %s and expect future writes to '%s'.", inputs, run) 

343 return butler.registry, inputs, run 

344 

345 @classmethod 

346 def makeWriteButler(cls, args: argparse.Namespace, 

347 taskDefs: Optional[Iterable[TaskDef]] = None) -> Butler: 

348 """Return a read-write butler initialized to write to and read from 

349 the collections specified by the given command-line arguments. 

350 

351 Parameters 

352 ---------- 

353 args : `types.SimpleNamespace` 

354 Parsed command-line arguments. See class documentation for the 

355 construction parameter of the same name. 

356 taskDefs : iterable of `TaskDef`, optional 

357 Definitions for tasks in a pipeline. This argument is only needed 

358 if ``args.replace_run`` is `True` and ``args.prune_replaced`` is 

359 "unstore". 

360 

361 Returns 

362 ------- 

363 butler : `lsst.daf.butler.Butler` 

364 A read-write butler initialized according to the given arguments. 

365 """ 

366 butler = Butler(args.butler_config, writeable=True) 

367 self = cls(butler.registry, args, writeable=True) 

368 self.check(args) 

369 if self.output is not None: 

370 chainDefinition = list(self.output.chain if self.output.exists else self.inputs) 

371 if args.replace_run: 

372 replaced = chainDefinition.pop(0) 

373 if args.prune_replaced == "unstore": 

374 # Remove datasets from datastore 

375 with butler.transaction(): 

376 refs = butler.registry.queryDatasets(..., collections=replaced) 

377 # we want to remove regular outputs but keep 

378 # initOutputs, configs, and versions. 

379 if taskDefs is not None: 

380 initDatasetNames = set(PipelineDatasetTypes.initOutputNames(taskDefs)) 

381 refs = [ref for ref in refs if ref.datasetType.name not in initDatasetNames] 

382 butler.pruneDatasets(refs, unstore=True, run=replaced, disassociate=False) 

383 elif args.prune_replaced == "purge": 

384 # Erase entire collection and all datasets, need to remove 

385 # collection from its chain collection first. 

386 with butler.transaction(): 

387 butler.registry.setCollectionChain(self.output.name, chainDefinition, flatten=True) 

388 butler.pruneCollection(replaced, purge=True, unstore=True) 

389 elif args.prune_replaced is not None: 

390 raise NotImplementedError( 

391 f"Unsupported --prune-replaced option '{args.prune_replaced}'." 

392 ) 

393 if not self.output.exists: 

394 butler.registry.registerCollection(self.output.name, CollectionType.CHAINED) 

395 if not args.extend_run: 

396 butler.registry.registerCollection(self.outputRun.name, CollectionType.RUN) 

397 chainDefinition.insert(0, self.outputRun.name) 

398 butler.registry.setCollectionChain(self.output.name, chainDefinition, flatten=True) 

399 _LOG.debug("Preparing butler to write to '%s' and read from '%s'=%s", 

400 self.outputRun.name, self.output.name, chainDefinition) 

401 butler.registry.defaults = RegistryDefaults(run=self.outputRun.name, collections=self.output.name) 

402 else: 

403 inputs = CollectionSearch.fromExpression((self.outputRun.name,) + self.inputs) 

404 _LOG.debug("Preparing butler to write to '%s' and read from %s.", self.outputRun.name, inputs) 

405 butler.registry.defaults = RegistryDefaults(run=self.outputRun.name, collections=inputs) 

406 return butler 

407 

408 output: Optional[_OutputChainedCollectionInfo] 

409 """Information about the output chained collection, if there is or will be 

410 one (`_OutputChainedCollectionInfo` or `None`). 

411 """ 

412 

413 outputRun: Optional[_OutputRunCollectionInfo] 

414 """Information about the output run collection, if there is or will be 

415 one (`_OutputRunCollectionInfo` or `None`). 

416 """ 

417 

418 inputs: Tuple[str, ...] 

419 """Input collections provided directly by the user (`tuple` [ `str` ]). 

420 """ 

421 

422 

423class _FilteredStream: 

424 """A file-like object that filters some config fields. 

425 

426 Note 

427 ---- 

428 This class depends on implementation details of ``Config.saveToStream`` 

429 methods, in particular that that method uses single call to write() 

430 method to save information about single config field, and that call 

431 combines comments string(s) for a field and field path and value. 

432 This class will not work reliably on the "import" strings, so imports 

433 should be disabled by passing ``skipImports=True`` to ``saveToStream()``. 

434 """ 

435 def __init__(self, pattern): 

436 # obey case if pattern isn't lowercase or requests NOIGNORECASE 

437 mat = re.search(r"(.*):NOIGNORECASE$", pattern) 

438 

439 if mat: 

440 pattern = mat.group(1) 

441 self._pattern = re.compile(fnmatch.translate(pattern)) 

442 else: 

443 if pattern != pattern.lower(): 

444 print(f"Matching \"{pattern}\" without regard to case " 

445 "(append :NOIGNORECASE to prevent this)", file=sys.stdout) 

446 self._pattern = re.compile(fnmatch.translate(pattern), re.IGNORECASE) 

447 

448 def write(self, showStr): 

449 # Strip off doc string line(s) and cut off at "=" for string matching 

450 matchStr = showStr.rstrip().split("\n")[-1].split("=")[0] 

451 if self._pattern.search(matchStr): 

452 sys.stdout.write(showStr) 

453 

454# ------------------------ 

455# Exported definitions -- 

456# ------------------------ 

457 

458 

459class CmdLineFwk: 

460 """PipelineTask framework which executes tasks from command line. 

461 

462 In addition to executing tasks this activator provides additional methods 

463 for task management like dumping configuration or execution chain. 

464 """ 

465 

466 MP_TIMEOUT = 9999 # Default timeout (sec) for multiprocessing 

467 

468 def __init__(self): 

469 pass 

470 

471 def makePipeline(self, args): 

472 """Build a pipeline from command line arguments. 

473 

474 Parameters 

475 ---------- 

476 args : `types.SimpleNamespace` 

477 Parsed command line 

478 

479 Returns 

480 ------- 

481 pipeline : `~lsst.pipe.base.Pipeline` 

482 """ 

483 if args.pipeline: 

484 pipeline = Pipeline.from_uri(args.pipeline) 

485 else: 

486 pipeline = Pipeline("anonymous") 

487 

488 # loop over all pipeline actions and apply them in order 

489 for action in args.pipeline_actions: 

490 if action.action == "add_instrument": 

491 

492 pipeline.addInstrument(action.value) 

493 

494 elif action.action == "new_task": 

495 

496 pipeline.addTask(action.value, action.label) 

497 

498 elif action.action == "delete_task": 

499 

500 pipeline.removeTask(action.label) 

501 

502 elif action.action == "config": 

503 

504 # action value string is "field=value", split it at '=' 

505 field, _, value = action.value.partition("=") 

506 pipeline.addConfigOverride(action.label, field, value) 

507 

508 elif action.action == "configfile": 

509 

510 pipeline.addConfigFile(action.label, action.value) 

511 

512 else: 

513 

514 raise ValueError(f"Unexpected pipeline action: {action.action}") 

515 

516 if args.save_pipeline: 

517 pipeline.write_to_uri(args.save_pipeline) 

518 

519 if args.pipeline_dot: 

520 pipeline2dot(pipeline, args.pipeline_dot) 

521 

522 return pipeline 

523 

524 def makeGraph(self, pipeline, args): 

525 """Build a graph from command line arguments. 

526 

527 Parameters 

528 ---------- 

529 pipeline : `~lsst.pipe.base.Pipeline` 

530 Pipeline, can be empty or ``None`` if graph is read from a file. 

531 args : `types.SimpleNamespace` 

532 Parsed command line 

533 

534 Returns 

535 ------- 

536 graph : `~lsst.pipe.base.QuantumGraph` or `None` 

537 If resulting graph is empty then `None` is returned. 

538 """ 

539 

540 # make sure that --extend-run always enables --skip-existing 

541 if args.extend_run: 

542 args.skip_existing = True 

543 

544 registry, collections, run = _ButlerFactory.makeRegistryAndCollections(args) 

545 

546 if args.skip_existing and run: 

547 args.skip_existing_in += (run,) 

548 

549 if args.qgraph: 

550 # click passes empty tuple as default value for qgraph_node_id 

551 nodes = args.qgraph_node_id or None 

552 qgraph = QuantumGraph.loadUri(args.qgraph, registry.dimensions, 

553 nodes=nodes, graphID=args.qgraph_id) 

554 

555 # pipeline can not be provided in this case 

556 if pipeline: 

557 raise ValueError("Pipeline must not be given when quantum graph is read from file.") 

558 

559 else: 

560 

561 # make execution plan (a.k.a. DAG) for pipeline 

562 graphBuilder = GraphBuilder(registry, 

563 skipExistingIn=args.skip_existing_in, 

564 clobberOutputs=args.clobber_outputs) 

565 # accumulate metadata 

566 metadata = {"input": args.input, "output": args.output, "butler_argument": args.butler_config, 

567 "output_run": args.output_run, "extend_run": args.extend_run, 

568 "skip_existing_in": args.skip_existing_in, "skip_existing": args.skip_existing, 

569 "data_query": args.data_query, "user": getpass.getuser(), 

570 "time": f"{datetime.datetime.now()}"} 

571 qgraph = graphBuilder.makeGraph(pipeline, collections, run, args.data_query, metadata=metadata) 

572 

573 # Count quanta in graph and give a warning if it's empty and return 

574 # None. 

575 nQuanta = len(qgraph) 

576 if nQuanta == 0: 

577 warnings.warn("QuantumGraph is empty", stacklevel=2) 

578 return None 

579 else: 

580 _LOG.info("QuantumGraph contains %d quanta for %d tasks, graph ID: %r", 

581 nQuanta, len(qgraph.taskGraph), qgraph.graphID) 

582 

583 if args.save_qgraph: 

584 qgraph.saveUri(args.save_qgraph) 

585 

586 if args.save_single_quanta: 

587 for quantumNode in qgraph: 

588 sqgraph = qgraph.subset(quantumNode) 

589 uri = args.save_single_quanta.format(quantumNode.nodeId.number) 

590 sqgraph.saveUri(uri) 

591 

592 if args.qgraph_dot: 

593 graph2dot(qgraph, args.qgraph_dot) 

594 

595 if args.execution_butler_location: 

596 butler = Butler(args.butler_config) 

597 newArgs = copy.deepcopy(args) 

598 

599 def builderShim(butler): 

600 newArgs.butler_config = butler._config 

601 # Calling makeWriteButler is done for the side effects of 

602 # calling that method, maining parsing all the args into 

603 # collection names, creating collections, etc. 

604 newButler = _ButlerFactory.makeWriteButler(newArgs) 

605 return newButler 

606 

607 # Include output collection in collections for input 

608 # files if it exists in the repo. 

609 all_inputs = args.input 

610 if args.output is not None: 

611 try: 

612 all_inputs += (next(iter(butler.registry.queryCollections(args.output))), ) 

613 except MissingCollectionError: 

614 pass 

615 

616 _LOG.debug("Calling buildExecutionButler with collections=%s", all_inputs) 

617 buildExecutionButler(butler, qgraph, args.execution_butler_location, run, 

618 butlerModifier=builderShim, collections=all_inputs, 

619 clobber=args.clobber_execution_butler) 

620 

621 return qgraph 

622 

623 def runPipeline(self, graph, taskFactory, args, butler=None): 

624 """Execute complete QuantumGraph. 

625 

626 Parameters 

627 ---------- 

628 graph : `QuantumGraph` 

629 Execution graph. 

630 taskFactory : `~lsst.pipe.base.TaskFactory` 

631 Task factory 

632 args : `types.SimpleNamespace` 

633 Parsed command line 

634 butler : `~lsst.daf.butler.Butler`, optional 

635 Data Butler instance, if not defined then new instance is made 

636 using command line options. 

637 """ 

638 # make sure that --extend-run always enables --skip-existing 

639 if args.extend_run: 

640 args.skip_existing = True 

641 

642 # make butler instance 

643 if butler is None: 

644 butler = _ButlerFactory.makeWriteButler(args, graph.iterTaskGraph()) 

645 

646 if args.skip_existing: 

647 args.skip_existing_in += (butler.run, ) 

648 

649 # Enable lsstDebug debugging. Note that this is done once in the 

650 # main process before PreExecInit and it is also repeated before 

651 # running each task in SingleQuantumExecutor (which may not be 

652 # needed if `multipocessing` always uses fork start method). 

653 if args.enableLsstDebug: 

654 try: 

655 _LOG.debug("Will try to import debug.py") 

656 import debug # noqa:F401 

657 except ImportError: 

658 _LOG.warn("No 'debug' module found.") 

659 

660 # Save all InitOutputs, configs, etc. 

661 preExecInit = PreExecInit(butler, taskFactory, extendRun=args.extend_run) 

662 preExecInit.initialize(graph, 

663 saveInitOutputs=not args.skip_init_writes, 

664 registerDatasetTypes=args.register_dataset_types, 

665 saveVersions=not args.no_versions) 

666 

667 if not args.init_only: 

668 graphFixup = self._importGraphFixup(args) 

669 quantumExecutor = SingleQuantumExecutor(taskFactory, 

670 skipExistingIn=args.skip_existing_in, 

671 clobberOutputs=args.clobber_outputs, 

672 enableLsstDebug=args.enableLsstDebug, 

673 exitOnKnownError=args.fail_fast) 

674 timeout = self.MP_TIMEOUT if args.timeout is None else args.timeout 

675 executor = MPGraphExecutor(numProc=args.processes, timeout=timeout, 

676 startMethod=args.start_method, 

677 quantumExecutor=quantumExecutor, 

678 failFast=args.fail_fast, 

679 executionGraphFixup=graphFixup) 

680 with util.profile(args.profile, _LOG): 

681 executor.execute(graph, butler) 

682 

683 def showInfo(self, args, pipeline, graph=None): 

684 """Display useful info about pipeline and environment. 

685 

686 Parameters 

687 ---------- 

688 args : `types.SimpleNamespace` 

689 Parsed command line 

690 pipeline : `Pipeline` 

691 Pipeline definition 

692 graph : `QuantumGraph`, optional 

693 Execution graph 

694 """ 

695 showOpts = args.show 

696 for what in showOpts: 

697 showCommand, _, showArgs = what.partition("=") 

698 

699 if showCommand in ["pipeline", "config", "history", "tasks"]: 

700 if not pipeline: 

701 _LOG.warning("Pipeline is required for --show=%s", showCommand) 

702 continue 

703 

704 if showCommand in ["graph", "workflow", "uri"]: 

705 if not graph: 

706 _LOG.warning("QuantumGraph is required for --show=%s", showCommand) 

707 continue 

708 

709 if showCommand == "pipeline": 

710 print(pipeline) 

711 elif showCommand == "config": 

712 self._showConfig(pipeline, showArgs, False) 

713 elif showCommand == "dump-config": 

714 self._showConfig(pipeline, showArgs, True) 

715 elif showCommand == "history": 

716 self._showConfigHistory(pipeline, showArgs) 

717 elif showCommand == "tasks": 

718 self._showTaskHierarchy(pipeline) 

719 elif showCommand == "graph": 

720 if graph: 

721 self._showGraph(graph) 

722 elif showCommand == "uri": 

723 if graph: 

724 self._showUri(graph, args) 

725 elif showCommand == "workflow": 

726 if graph: 

727 self._showWorkflow(graph, args) 

728 else: 

729 print("Unknown value for show: %s (choose from '%s')" % 

730 (what, "', '".join("pipeline config[=XXX] history=XXX tasks graph".split())), 

731 file=sys.stderr) 

732 sys.exit(1) 

733 

734 def _showConfig(self, pipeline, showArgs, dumpFullConfig): 

735 """Show task configuration 

736 

737 Parameters 

738 ---------- 

739 pipeline : `Pipeline` 

740 Pipeline definition 

741 showArgs : `str` 

742 Defines what to show 

743 dumpFullConfig : `bool` 

744 If true then dump complete task configuration with all imports. 

745 """ 

746 stream = sys.stdout 

747 if dumpFullConfig: 

748 # Task label can be given with this option 

749 taskName = showArgs 

750 else: 

751 # The argument can have form [TaskLabel::][pattern:NOIGNORECASE] 

752 matConfig = re.search(r"^(?:(\w+)::)?(?:config.)?(.+)?", showArgs) 

753 taskName = matConfig.group(1) 

754 pattern = matConfig.group(2) 

755 if pattern: 

756 stream = _FilteredStream(pattern) 

757 

758 tasks = util.filterTasks(pipeline, taskName) 

759 if not tasks: 

760 print("Pipeline has no tasks named {}".format(taskName), file=sys.stderr) 

761 sys.exit(1) 

762 

763 for taskDef in tasks: 

764 print("### Configuration for task `{}'".format(taskDef.label)) 

765 taskDef.config.saveToStream(stream, root="config", skipImports=not dumpFullConfig) 

766 

767 def _showConfigHistory(self, pipeline, showArgs): 

768 """Show history for task configuration 

769 

770 Parameters 

771 ---------- 

772 pipeline : `Pipeline` 

773 Pipeline definition 

774 showArgs : `str` 

775 Defines what to show 

776 """ 

777 

778 taskName = None 

779 pattern = None 

780 matHistory = re.search(r"^(?:(\w+)::)?(?:config[.])?(.+)", showArgs) 

781 if matHistory: 

782 taskName = matHistory.group(1) 

783 pattern = matHistory.group(2) 

784 if not pattern: 

785 print("Please provide a value with --show history (e.g. history=Task::param)", file=sys.stderr) 

786 sys.exit(1) 

787 

788 tasks = util.filterTasks(pipeline, taskName) 

789 if not tasks: 

790 print(f"Pipeline has no tasks named {taskName}", file=sys.stderr) 

791 sys.exit(1) 

792 

793 found = False 

794 for taskDef in tasks: 

795 

796 config = taskDef.config 

797 

798 # Look for any matches in the config hierarchy for this name 

799 for nmatch, thisName in enumerate(fnmatch.filter(config.names(), pattern)): 

800 if nmatch > 0: 

801 print("") 

802 

803 cpath, _, cname = thisName.rpartition(".") 

804 try: 

805 if not cpath: 

806 # looking for top-level field 

807 hconfig = taskDef.config 

808 else: 

809 hconfig = eval("config." + cpath, {}, {"config": config}) 

810 except AttributeError: 

811 print(f"Error: Unable to extract attribute {cpath} from task {taskDef.label}", 

812 file=sys.stderr) 

813 hconfig = None 

814 

815 # Sometimes we end up with a non-Config so skip those 

816 if isinstance(hconfig, (pexConfig.Config, pexConfig.ConfigurableInstance)) and \ 

817 hasattr(hconfig, cname): 

818 print(f"### Configuration field for task `{taskDef.label}'") 

819 print(pexConfig.history.format(hconfig, cname)) 

820 found = True 

821 

822 if not found: 

823 print(f"None of the tasks has field matching {pattern}", file=sys.stderr) 

824 sys.exit(1) 

825 

826 def _showTaskHierarchy(self, pipeline): 

827 """Print task hierarchy to stdout 

828 

829 Parameters 

830 ---------- 

831 pipeline: `Pipeline` 

832 """ 

833 for taskDef in pipeline.toExpandedPipeline(): 

834 print("### Subtasks for task `{}'".format(taskDef.taskName)) 

835 

836 for configName, taskName in util.subTaskIter(taskDef.config): 

837 print("{}: {}".format(configName, taskName)) 

838 

839 def _showGraph(self, graph): 

840 """Print quanta information to stdout 

841 

842 Parameters 

843 ---------- 

844 graph : `QuantumGraph` 

845 Execution graph. 

846 """ 

847 for taskNode in graph.taskGraph: 

848 print(taskNode) 

849 

850 for iq, quantum in enumerate(graph.getQuantaForTask(taskNode)): 

851 print(" Quantum {}:".format(iq)) 

852 print(" inputs:") 

853 for key, refs in quantum.inputs.items(): 

854 dataIds = ["DataId({})".format(ref.dataId) for ref in refs] 

855 print(" {}: [{}]".format(key, ", ".join(dataIds))) 

856 print(" outputs:") 

857 for key, refs in quantum.outputs.items(): 

858 dataIds = ["DataId({})".format(ref.dataId) for ref in refs] 

859 print(" {}: [{}]".format(key, ", ".join(dataIds))) 

860 

861 def _showWorkflow(self, graph, args): 

862 """Print quanta information and dependency to stdout 

863 

864 Parameters 

865 ---------- 

866 graph : `QuantumGraph` 

867 Execution graph. 

868 args : `types.SimpleNamespace` 

869 Parsed command line 

870 """ 

871 for node in graph: 

872 print(f"Quantum {node.nodeId.number}: {node.taskDef.taskName}") 

873 for parent in graph.determineInputsToQuantumNode(node): 

874 print(f"Parent Quantum {parent.nodeId.number} - Child Quantum {node.nodeId.number}") 

875 

876 def _showUri(self, graph, args): 

877 """Print input and predicted output URIs to stdout 

878 

879 Parameters 

880 ---------- 

881 graph : `QuantumGraph` 

882 Execution graph 

883 args : `types.SimpleNamespace` 

884 Parsed command line 

885 """ 

886 def dumpURIs(thisRef): 

887 primary, components = butler.getURIs(thisRef, predict=True, run="TBD") 

888 if primary: 

889 print(f" {primary}") 

890 else: 

891 print(" (disassembled artifact)") 

892 for compName, compUri in components.items(): 

893 print(f" {compName}: {compUri}") 

894 

895 butler = _ButlerFactory.makeReadButler(args) 

896 for node in graph: 

897 print(f"Quantum {node.nodeId.number}: {node.taskDef.taskName}") 

898 print(" inputs:") 

899 for key, refs in node.quantum.inputs.items(): 

900 for ref in refs: 

901 dumpURIs(ref) 

902 print(" outputs:") 

903 for key, refs in node.quantum.outputs.items(): 

904 for ref in refs: 

905 dumpURIs(ref) 

906 

907 def _importGraphFixup(self, args): 

908 """Import/instantiate graph fixup object. 

909 

910 Parameters 

911 ---------- 

912 args : `types.SimpleNamespace` 

913 Parsed command line. 

914 

915 Returns 

916 ------- 

917 fixup : `ExecutionGraphFixup` or `None` 

918 

919 Raises 

920 ------ 

921 ValueError 

922 Raised if import fails, method call raises exception, or returned 

923 instance has unexpected type. 

924 """ 

925 if args.graph_fixup: 

926 try: 

927 factory = doImport(args.graph_fixup) 

928 except Exception as exc: 

929 raise ValueError("Failed to import graph fixup class/method") from exc 

930 try: 

931 fixup = factory() 

932 except Exception as exc: 

933 raise ValueError("Failed to make instance of graph fixup") from exc 

934 if not isinstance(fixup, ExecutionGraphFixup): 

935 raise ValueError("Graph fixup is not an instance of ExecutionGraphFixup class") 

936 return fixup