Coverage for python/lsst/pipe/base/tests/util.py: 7%

44 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-03-26 02:50 -0700

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/>. 

27 

28"""Collection of common methods for use in unit tests.""" 

29 

30from __future__ import annotations 

31 

32from lsst.daf.butler import DatasetRef 

33 

34from ..graph import QuantumGraph 

35 

36 

37def check_output_run(graph: QuantumGraph, run: str) -> list[DatasetRef]: 

38 """Check that all output and intermediate datasets belong to a 

39 specified run. 

40 

41 Parameters 

42 ---------- 

43 graph : `QuantumGraph` 

44 Quantum graph. 

45 run : `str` 

46 Output run name. 

47 

48 Returns 

49 ------- 

50 refs : `list` [ `~lsst.daf.butler.DatasetRef` ] 

51 List of output/intermediate dataset references that do NOT belong to 

52 the specified run. 

53 """ 

54 # Collect all inputs/outputs, so that we can build intermediate refs. 

55 output_refs: list[DatasetRef] = [] 

56 input_refs: list[DatasetRef] = [] 

57 for node in graph: 

58 for refs in node.quantum.outputs.values(): 

59 output_refs += refs 

60 for refs in node.quantum.inputs.values(): 

61 input_refs += refs 

62 for task_def in graph.iterTaskGraph(): 

63 init_refs = graph.initOutputRefs(task_def) 

64 if init_refs: 

65 output_refs += init_refs 

66 init_refs = graph.initInputRefs(task_def) 

67 if init_refs: 

68 input_refs += init_refs 

69 output_refs += graph.globalInitOutputRefs() 

70 newRefs = [ref for ref in output_refs if ref.run != run] 

71 

72 output_ids = {ref.id for ref in output_refs} 

73 intermediates = [ref for ref in input_refs if ref.id in output_ids] 

74 newRefs += [ref for ref in intermediates if ref.run != run] 

75 

76 return newRefs 

77 

78 

79def get_output_refs(graph: QuantumGraph) -> list[DatasetRef]: 

80 """Return all output and intermediate references in a graph. 

81 

82 Parameters 

83 ---------- 

84 graph : `QuantumGraph` 

85 Quantum graph. 

86 

87 Returns 

88 ------- 

89 refs : `list` [ `~lsst.daf.butler.DatasetRef` ] 

90 List of all output/intermediate dataset references, intermediates 

91 will appear more than once in this list. 

92 """ 

93 output_refs: set[DatasetRef] = set() 

94 for node in graph: 

95 for refs in node.quantum.outputs.values(): 

96 output_refs.update(refs) 

97 for task_def in graph.iterTaskGraph(): 

98 init_refs = graph.initOutputRefs(task_def) 

99 if init_refs: 

100 output_refs.update(init_refs) 

101 output_refs.update(graph.globalInitOutputRefs()) 

102 

103 result = list(output_refs) 

104 

105 for node in graph: 

106 for refs in node.quantum.inputs.values(): 

107 result += [ref for ref in refs if ref in output_refs] 

108 for task_def in graph.iterTaskGraph(): 

109 init_refs = graph.initInputRefs(task_def) 

110 if init_refs: 

111 result += [ref for ref in init_refs if ref in output_refs] 

112 

113 return result