Coverage for python / lsst / pipe / base / pipelineTask.py: 76%

17 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-18 01:04 -0800

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"""Define `PipelineTask` class and related methods.""" 

29 

30from __future__ import annotations 

31 

32__all__ = ["PipelineTask"] # Classes in this module 

33 

34from collections.abc import Callable 

35from typing import TYPE_CHECKING, Any, ClassVar 

36 

37from .connections import InputQuantizedConnection, OutputQuantizedConnection 

38from .task import Task 

39 

40if TYPE_CHECKING: 

41 import logging 

42 

43 from lsst.utils.logging import LsstLogAdapter 

44 

45 from ._quantumContext import QuantumContext 

46 from .config import PipelineTaskConfig 

47 from .struct import Struct 

48 

49 

50class PipelineTask(Task): 

51 """Base class for all pipeline tasks. 

52 

53 This is an abstract base class for PipelineTasks which represents an 

54 algorithm executed by framework(s) on data which comes from data butler, 

55 resulting data is also stored in a data butler. 

56 

57 PipelineTask inherits from a `~lsst.pipe.base.Task` and uses the same 

58 configuration mechanism based on :ref:`lsst.pex.config`. `PipelineTask` 

59 classes also have a `PipelineTaskConnections` class associated with their 

60 config which defines all of the IO a `PipelineTask` will need to do. 

61 PipelineTask sub-class typically implements `run()` method which receives 

62 Python-domain data objects and returns `lsst.pipe.base.Struct` object with 

63 resulting data. `run()` method is not supposed to perform any I/O, it 

64 operates entirely on in-memory objects. `runQuantum()` is the method (can 

65 be re-implemented in sub-class) where all necessary I/O is performed, it 

66 reads all input data from data butler into memory, calls `run()` method 

67 with that data, examines returned `Struct` object and saves some or all of 

68 that data back to data butler. `runQuantum()` method receives a 

69 `QuantumContext` instance to facilitate I/O, a `InputQuantizedConnection` 

70 instance which defines all input `lsst.daf.butler.DatasetRef`, and a 

71 `OutputQuantizedConnection` instance which defines all the output 

72 `lsst.daf.butler.DatasetRef` for a single invocation of PipelineTask. 

73 

74 Subclasses must be constructable with exactly the arguments taken by the 

75 PipelineTask base class constructor, but may support other signatures as 

76 well. 

77 

78 Attributes 

79 ---------- 

80 canMultiprocess : bool, True by default (class attribute) 

81 This class attribute is checked by execution framework, sub-classes 

82 can set it to ``False`` in case task does not support multiprocessing. 

83 

84 Parameters 

85 ---------- 

86 config : `~lsst.pex.config.Config`, optional 

87 Configuration for this task (an instance of ``self.ConfigClass``, 

88 which is a task-specific subclass of `PipelineTaskConfig`). 

89 If not specified then it defaults to ``self.ConfigClass()``. 

90 log : `logging.Logger`, optional 

91 Logger instance whose name is used as a log name prefix, or ``None`` 

92 for no prefix. 

93 initInputs : `dict`, optional 

94 A dictionary of objects needed to construct this PipelineTask, with 

95 keys matching the keys of the dictionary returned by 

96 `getInitInputDatasetTypes` and values equivalent to what would be 

97 obtained by calling `~lsst.daf.butler.Butler.get` with those 

98 DatasetTypes and no data IDs. While it is optional for the base class, 

99 subclasses are permitted to require this argument. 

100 **kwargs : `~typing.Any` 

101 Arbitrary parameters, passed to base class constructor. 

102 """ 

103 

104 ConfigClass: ClassVar[type[PipelineTaskConfig]] 

105 canMultiprocess: ClassVar[bool] = True 

106 

107 def __init__( 

108 self, 

109 *, 

110 config: PipelineTaskConfig | None = None, 

111 log: logging.Logger | LsstLogAdapter | None = None, 

112 initInputs: dict[str, Any] | None = None, 

113 **kwargs: Any, 

114 ): 

115 super().__init__(config=config, log=log, **kwargs) 

116 

117 run: Callable[..., Struct] # The 'run' method for subclasses will have a different signature 

118 

119 def run(self, **kwargs: Any) -> Struct: # type: ignore 

120 """Run task algorithm on in-memory data. 

121 

122 This method should be implemented in a subclass. This method will 

123 receive keyword-only arguments whose names will be the same as names of 

124 connection fields describing input dataset types. Argument values will 

125 be data objects retrieved from data butler. If a dataset type is 

126 configured with ``multiple`` field set to ``True`` then the argument 

127 value will be a list of objects, otherwise it will be a single object. 

128 

129 If the task needs to know its input or output DataIds then it also has 

130 to override the `runQuantum` method. 

131 

132 This method should return a `Struct` whose attributes share the same 

133 name as the connection fields describing output dataset types. 

134 

135 Parameters 

136 ---------- 

137 **kwargs : `~typing.Any` 

138 Arbitrary parameters accepted by subclasses. 

139 

140 Returns 

141 ------- 

142 struct : `Struct` 

143 Struct with attribute names corresponding to output connection 

144 fields. 

145 

146 Examples 

147 -------- 

148 Typical implementation of this method may look like: 

149 

150 .. code-block:: python 

151 

152 def run(self, *, input, calib): 

153 # "input", "calib", and "output" are the names of the 

154 # connection fields. 

155 

156 # Assuming that input/calib datasets are `scalar` they are 

157 # simple objects, do something with inputs and calibs, produce 

158 # output image. 

159 image = self.makeImage(input, calib) 

160 

161 # If output dataset is `scalar` then return object, not list 

162 return Struct(output=image) 

163 """ 

164 raise NotImplementedError("run() is not implemented") 

165 

166 def runQuantum( 

167 self, 

168 butlerQC: QuantumContext, 

169 inputRefs: InputQuantizedConnection, 

170 outputRefs: OutputQuantizedConnection, 

171 ) -> None: 

172 """Do butler IO and transform to provide in memory 

173 objects for tasks `~Task.run` method. 

174 

175 Parameters 

176 ---------- 

177 butlerQC : `QuantumContext` 

178 A butler which is specialized to operate in the context of a 

179 `lsst.daf.butler.Quantum`. 

180 inputRefs : `InputQuantizedConnection` 

181 Datastructure whose attribute names are the names that identify 

182 connections defined in corresponding `PipelineTaskConnections` 

183 class. The values of these attributes are the 

184 `lsst.daf.butler.DatasetRef` objects associated with the defined 

185 input/prerequisite connections. 

186 outputRefs : `OutputQuantizedConnection` 

187 Datastructure whose attribute names are the names that identify 

188 connections defined in corresponding `PipelineTaskConnections` 

189 class. The values of these attributes are the 

190 `lsst.daf.butler.DatasetRef` objects associated with the defined 

191 output connections. 

192 """ 

193 inputs = butlerQC.get(inputRefs) 

194 outputs = self.run(**inputs) 

195 butlerQC.put(outputs, outputRefs)