Coverage for python/lsst/ctrl/mpexec/util.py: 16%

51 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-10 03:29 -0700

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 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"""Few utility methods used by the rest of a package. 

29""" 

30 

31__all__ = ["printTable", "filterTasks", "filterTaskNodes", "subTaskIter"] 

32 

33from collections.abc import Iterator 

34 

35import lsst.pex.config as pexConfig 

36from deprecated.sphinx import deprecated 

37from lsst.pipe.base import Pipeline, PipelineGraph, TaskDef 

38from lsst.pipe.base.pipeline_graph import TaskNode 

39 

40 

41def printTable(rows: list[tuple], header: tuple | None) -> None: 

42 """Nice formatting of 2-column table. 

43 

44 Parameters 

45 ---------- 

46 rows : `list` of `tuple` 

47 Each item in the list is a 2-tuple containg left and righ column 

48 values. 

49 header : `tuple` or `None` 

50 If `None` then table header are not prined, otherwise it's a 2-tuple 

51 with column headings. 

52 """ 

53 if not rows: 

54 return 

55 width = max(len(x[0]) for x in rows) 

56 if header: 

57 width = max(width, len(header[0])) 

58 print(header[0].ljust(width), header[1]) 

59 print("".ljust(width, "-"), "".ljust(len(header[1]), "-")) 

60 for col1, col2 in rows: 

61 print(col1.ljust(width), col2) 

62 

63 

64# TODO: remove on DM-40443. 

65@deprecated( 

66 "filterTasks is deprecated in favor of filterTaskNodes, and will be removed after v27.", 

67 version="v27.0", 

68 category=FutureWarning, 

69) 

70def filterTasks(pipeline: Pipeline, name: str | None) -> list[TaskDef]: 

71 """Find list of tasks matching given name. 

72 

73 For matching task either task label or task name after last dot should 

74 be identical to `name`. If task label is non-empty then task name is not 

75 checked. 

76 

77 Parameters 

78 ---------- 

79 pipeline : `Pipeline` 

80 Pipeline to examine. 

81 name : `str` or `None` 

82 If empty or `None` then all tasks are returned. 

83 

84 Returns 

85 ------- 

86 tasks : `list` [ `lsst.pipe.base.TaskDef`] 

87 List of `~lsst.pipe.base.TaskDef` instances that match. 

88 """ 

89 if not name: 

90 return list(pipeline.toExpandedPipeline()) 

91 tasks = [] 

92 for taskDef in pipeline.toExpandedPipeline(): 

93 if taskDef.label: 

94 if taskDef.label == name: 

95 tasks.append(taskDef) 

96 elif taskDef.taskName.split(".")[-1] == name: 

97 tasks.append(taskDef) 

98 return tasks 

99 

100 

101def filterTaskNodes(pipeline_graph: PipelineGraph, name: str | None) -> list[TaskNode]: 

102 """Find list of tasks matching given name. 

103 

104 For matching task either task label or task name after last dot should 

105 be identical to `name`. If task label is non-empty then task name is not 

106 checked. 

107 

108 Parameters 

109 ---------- 

110 pipeline_graph : `~lsst.pipe.base.PipelineGraph` 

111 Pipeline graph to examine. 

112 name : `str` or `None` 

113 If empty or `None` then all tasks are xreturned. 

114 

115 Returns 

116 ------- 

117 tasks : `list` [ `lsst.pipe.base.pipeline_graph.TaskNode`] 

118 List of task nodes that match. 

119 """ 

120 if not name: 

121 return list(pipeline_graph.tasks.values()) 

122 tasks = [] 

123 for task_node in pipeline_graph.tasks.values(): 

124 if task_node.label == name: 

125 tasks.append(task_node) 

126 elif task_node.task_class_name.split(".")[-1] == name: 

127 tasks.append(task_node) 

128 return tasks 

129 

130 

131def subTaskIter(config: pexConfig.Config) -> Iterator[tuple[str, str]]: 

132 """Recursively generates subtask names. 

133 

134 Parameters 

135 ---------- 

136 config : `lsst.pex.config.Config` 

137 Configuration of the task. 

138 

139 Returns 

140 ------- 

141 names : `collections.abc.Iterator` [ `tuple` [ `str`, `str` ] ] 

142 Iterator which returns tuples of (configFieldPath, taskName). 

143 """ 

144 for fieldName, field in sorted(config.items()): 

145 if hasattr(field, "value") and hasattr(field, "target"): 

146 subConfig = field.value 

147 if isinstance(subConfig, pexConfig.Config): 

148 try: 

149 taskName = f"{field.target.__module__}.{field.target.__name__}" 

150 except Exception: 

151 taskName = repr(field.target) 

152 yield fieldName, taskName 

153 for subFieldName, taskName in subTaskIter(subConfig): 

154 yield fieldName + "." + subFieldName, taskName