Coverage for python / lsst / pipe / base / tests / util.py: 15%
58 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-17 08:59 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-17 08:59 +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 software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28"""Collection of common methods for use in unit tests."""
30from __future__ import annotations
32__all__ = ("check_output_run", "patch_deterministic_uuid4")
34import random
35import uuid
36from collections.abc import Iterator
37from contextlib import contextmanager
39from lsst.daf.butler import DatasetRef
41from ..graph import QuantumGraph
44def check_output_run(graph: QuantumGraph, run: str) -> list[DatasetRef]:
45 """Check that all output and intermediate datasets belong to a
46 specified run.
48 Parameters
49 ----------
50 graph : `QuantumGraph`
51 Quantum graph.
52 run : `str`
53 Output run name.
55 Returns
56 -------
57 refs : `list` [ `~lsst.daf.butler.DatasetRef` ]
58 List of output/intermediate dataset references that do NOT belong to
59 the specified run.
60 """
61 # Collect all inputs/outputs, so that we can build intermediate refs.
62 output_refs: list[DatasetRef] = []
63 input_refs: list[DatasetRef] = []
64 for node in graph:
65 for refs in node.quantum.outputs.values():
66 output_refs += refs
67 for refs in node.quantum.inputs.values():
68 input_refs += refs
69 for task_def in graph.iterTaskGraph():
70 init_refs = graph.initOutputRefs(task_def)
71 if init_refs:
72 output_refs += init_refs
73 init_refs = graph.initInputRefs(task_def)
74 if init_refs:
75 input_refs += init_refs
76 output_refs += graph.globalInitOutputRefs()
77 newRefs = [ref for ref in output_refs if ref.run != run]
79 output_ids = {ref.id for ref in output_refs}
80 intermediates = [ref for ref in input_refs if ref.id in output_ids]
81 newRefs += [ref for ref in intermediates if ref.run != run]
83 return newRefs
86def get_output_refs(graph: QuantumGraph) -> list[DatasetRef]:
87 """Return all output and intermediate references in a graph.
89 Parameters
90 ----------
91 graph : `QuantumGraph`
92 Quantum graph.
94 Returns
95 -------
96 refs : `list` [ `~lsst.daf.butler.DatasetRef` ]
97 List of all output/intermediate dataset references, intermediates
98 will appear more than once in this list.
99 """
100 output_refs: set[DatasetRef] = set()
101 for node in graph:
102 for refs in node.quantum.outputs.values():
103 output_refs.update(refs)
104 for task_def in graph.iterTaskGraph():
105 init_refs = graph.initOutputRefs(task_def)
106 if init_refs:
107 output_refs.update(init_refs)
108 output_refs.update(graph.globalInitOutputRefs())
110 result = list(output_refs)
112 for node in graph:
113 for refs in node.quantum.inputs.values():
114 result += [ref for ref in refs if ref in output_refs]
115 for task_def in graph.iterTaskGraph():
116 init_refs = graph.initInputRefs(task_def)
117 if init_refs:
118 result += [ref for ref in init_refs if ref in output_refs]
120 return result
123@contextmanager
124def patch_deterministic_uuid4(seed: int) -> Iterator[None]:
125 """Return a context manager that replaces the standard library's
126 `uuid.uuid4` function with one that generates random numbers with a
127 deterministic sequence.
129 Parameters
130 ----------
131 seed : `int
132 Integer seed for the random number generator.
133 """
134 random.seed(seed)
135 original = uuid.uuid4
136 uuid.uuid4 = _deterministic_uuid
137 try:
138 yield
139 finally:
140 uuid.uuid4 = original
143def _deterministic_uuid() -> uuid.UUID:
144 return uuid.UUID(int=random.getrandbits(128))