Coverage for python / lsst / ctrl / mpexec / util.py: 14%
37 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-28 08:45 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-28 08:45 +0000
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/>.
28"""Few utility methods used by the rest of a package."""
30__all__ = ["filterTaskNodes", "printTable", "subTaskIter"]
32from collections.abc import Iterator
34import lsst.pex.config as pexConfig
35from lsst.pipe.base import PipelineGraph
36from lsst.pipe.base.pipeline_graph import TaskNode
39def printTable(rows: list[tuple], header: tuple | None) -> None:
40 """Nice formatting of 2-column table.
42 Parameters
43 ----------
44 rows : `list` of `tuple`
45 Each item in the list is a 2-tuple containg left and righ column
46 values.
47 header : `tuple` or `None`
48 If `None` then table header are not prined, otherwise it's a 2-tuple
49 with column headings.
50 """
51 if not rows:
52 return
53 width = max(len(x[0]) for x in rows)
54 if header:
55 width = max(width, len(header[0]))
56 print(header[0].ljust(width), header[1])
57 print("".ljust(width, "-"), "".ljust(len(header[1]), "-"))
58 for col1, col2 in rows:
59 print(col1.ljust(width), col2)
62def filterTaskNodes(pipeline_graph: PipelineGraph, name: str | None) -> list[TaskNode]:
63 """Find list of tasks matching given name.
65 For matching task either task label or task name after last dot should
66 be identical to `name`. If task label is non-empty then task name is not
67 checked.
69 Parameters
70 ----------
71 pipeline_graph : `~lsst.pipe.base.PipelineGraph`
72 Pipeline graph to examine.
73 name : `str` or `None`
74 If empty or `None` then all tasks are xreturned.
76 Returns
77 -------
78 tasks : `list` [ `lsst.pipe.base.pipeline_graph.TaskNode`]
79 List of task nodes that match.
80 """
81 if not name:
82 return list(pipeline_graph.tasks.values())
83 tasks = []
84 for task_node in pipeline_graph.tasks.values():
85 if task_node.label == name:
86 tasks.append(task_node)
87 elif task_node.task_class_name.split(".")[-1] == name:
88 tasks.append(task_node)
89 return tasks
92def subTaskIter(config: pexConfig.Config) -> Iterator[tuple[str, str]]:
93 """Recursively generates subtask names.
95 Parameters
96 ----------
97 config : `lsst.pex.config.Config`
98 Configuration of the task.
100 Returns
101 -------
102 names : `collections.abc.Iterator` [ `tuple` [ `str`, `str` ] ]
103 Iterator which returns tuples of (configFieldPath, taskName).
104 """
105 for fieldName, field in sorted(config.items()):
106 if hasattr(field, "value") and hasattr(field, "target"):
107 subConfig = field.value
108 if isinstance(subConfig, pexConfig.Config):
109 try:
110 taskName = f"{field.target.__module__}.{field.target.__name__}"
111 except Exception:
112 taskName = repr(field.target)
113 yield fieldName, taskName
114 for subFieldName, taskName in subTaskIter(subConfig):
115 yield fieldName + "." + subFieldName, taskName