Coverage for python/lsst/pipe/base/tests/util.py: 7%
44 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-23 10:31 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-08-23 10:31 +0000
1# This file is part of pipe_base.
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/>.
22"""Collection of common methods for use in unit tests."""
24from __future__ import annotations
26from lsst.daf.butler import DatasetRef
28from ..graph import QuantumGraph
31def check_output_run(graph: QuantumGraph, run: str) -> list[DatasetRef]:
32 """Check that all output and intermediate datasets belong to a
33 specified run.
35 Parameters
36 ----------
37 graph : `QuantumGraph`
38 Quantum graph.
39 run : `str`
40 Output run name.
42 Returns
43 -------
44 refs : `list` [ `~lsst.daf.butler.DatasetRef` ]
45 List of output/intermediate dataset references that do NOT belong to
46 the specified run.
47 """
48 # Collect all inputs/outputs, so that we can build intermediate refs.
49 output_refs: list[DatasetRef] = []
50 input_refs: list[DatasetRef] = []
51 for node in graph:
52 for refs in node.quantum.outputs.values():
53 output_refs += refs
54 for refs in node.quantum.inputs.values():
55 input_refs += refs
56 for task_def in graph.iterTaskGraph():
57 init_refs = graph.initOutputRefs(task_def)
58 if init_refs:
59 output_refs += init_refs
60 init_refs = graph.initInputRefs(task_def)
61 if init_refs:
62 input_refs += init_refs
63 output_refs += graph.globalInitOutputRefs()
64 newRefs = [ref for ref in output_refs if ref.run != run]
66 output_ids = {ref.id for ref in output_refs}
67 intermediates = [ref for ref in input_refs if ref.id in output_ids]
68 newRefs += [ref for ref in intermediates if ref.run != run]
70 return newRefs
73def get_output_refs(graph: QuantumGraph) -> list[DatasetRef]:
74 """Return all output and intermediate references in a graph.
76 Parameters
77 ----------
78 graph : `QuantumGraph`
79 Quantum graph.
81 Returns
82 -------
83 refs : `list` [ `~lsst.daf.butler.DatasetRef` ]
84 List of all output/intermediate dataset references, intermediates
85 will appear more than once in this list.
86 """
87 output_refs: set[DatasetRef] = set()
88 for node in graph:
89 for refs in node.quantum.outputs.values():
90 output_refs.update(refs)
91 for task_def in graph.iterTaskGraph():
92 init_refs = graph.initOutputRefs(task_def)
93 if init_refs:
94 output_refs.update(init_refs)
95 output_refs.update(graph.globalInitOutputRefs())
97 result = list(output_refs)
99 for node in graph:
100 for refs in node.quantum.inputs.values():
101 result += [ref for ref in refs if ref in output_refs]
102 for task_def in graph.iterTaskGraph():
103 init_refs = graph.initInputRefs(task_def)
104 if init_refs:
105 result += [ref for ref in init_refs if ref in output_refs]
107 return result