Coverage for python/lsst/ctrl/mpexec/util.py: 12%
38 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-25 10:56 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-25 10:56 +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.
29"""
31__all__ = ["printTable", "filterTasks", "subTaskIter"]
33from collections.abc import Iterator
35import lsst.pex.config as pexConfig
36from lsst.pipe.base import Pipeline, TaskDef
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 filterTasks(pipeline: Pipeline, name: str | None) -> list[TaskDef]:
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 : `Pipeline`
72 Pipeline to examine.
73 name : `str` or `None`
74 If empty or `None` then all tasks are returned.
76 Returns
77 -------
78 tasks : `list` [ `lsst.pipe.base.TaskDef`]
79 List of `~lsst.pipe.base.TaskDef` instances that match.
80 """
81 if not name:
82 return list(pipeline.toExpandedPipeline())
83 tasks = []
84 for taskDef in pipeline.toExpandedPipeline():
85 if taskDef.label:
86 if taskDef.label == name:
87 tasks.append(taskDef)
88 elif taskDef.taskName.split(".")[-1] == name:
89 tasks.append(taskDef)
90 return tasks
93def subTaskIter(config: pexConfig.Config) -> Iterator[tuple[str, str]]:
94 """Recursively generates subtask names.
96 Parameters
97 ----------
98 config : `lsst.pex.config.Config`
99 Configuration of the task.
101 Returns
102 -------
103 names : `collections.abc.Iterator` [ `tuple` [ `str`, `str` ] ]
104 Iterator which returns tuples of (configFieldPath, taskName).
105 """
106 for fieldName, field in sorted(config.items()):
107 if hasattr(field, "value") and hasattr(field, "target"):
108 subConfig = field.value
109 if isinstance(subConfig, pexConfig.Config):
110 try:
111 taskName = f"{field.target.__module__}.{field.target.__name__}"
112 except Exception:
113 taskName = repr(field.target)
114 yield fieldName, taskName
115 for subFieldName, taskName in subTaskIter(subConfig):
116 yield fieldName + "." + subFieldName, taskName