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

16 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-19 10:39 +0000

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""" 

30 

31from __future__ import annotations 

32 

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

34 

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 """ 

101 

102 ConfigClass: ClassVar[type[PipelineTaskConfig]] 

103 canMultiprocess: ClassVar[bool] = True 

104 

105 def __init__( 

106 self, 

107 *, 

108 config: PipelineTaskConfig | None = None, 

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

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

111 **kwargs: Any, 

112 ): 

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

114 

115 def run(self, **kwargs: Any) -> Struct: 

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

117 

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

119 receive keyword arguments whose names will be the same as names of 

120 connection fields describing input dataset types. Argument values will 

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

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

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

124 

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

126 override `runQuantum` method instead. 

127 

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

129 name as the connection fields describing output dataset types. 

130 

131 Returns 

132 ------- 

133 struct : `Struct` 

134 Struct with attribute names corresponding to output connection 

135 fields 

136 

137 Examples 

138 -------- 

139 Typical implementation of this method may look like: 

140 

141 .. code-block:: python 

142 

143 def run(self, input, calib): 

144 # "input", "calib", and "output" are the names of the config 

145 # fields 

146 

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

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

149 # output image. 

150 image = self.makeImage(input, calib) 

151 

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

153 return Struct(output=image) 

154 

155 """ 

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

157 

158 def runQuantum( 

159 self, 

160 butlerQC: QuantumContext, 

161 inputRefs: InputQuantizedConnection, 

162 outputRefs: OutputQuantizedConnection, 

163 ) -> None: 

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

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

166 

167 Parameters 

168 ---------- 

169 butlerQC : `QuantumContext` 

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

171 `lsst.daf.butler.Quantum`. 

172 inputRefs : `InputQuantizedConnection` 

173 Datastructure whose attribute names are the names that identify 

174 connections defined in corresponding `PipelineTaskConnections` 

175 class. The values of these attributes are the 

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

177 input/prerequisite connections. 

178 outputRefs : `OutputQuantizedConnection` 

179 Datastructure whose attribute names are the names that identify 

180 connections defined in corresponding `PipelineTaskConnections` 

181 class. The values of these attributes are the 

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

183 output connections. 

184 """ 

185 inputs = butlerQC.get(inputRefs) 

186 outputs = self.run(**inputs) 

187 butlerQC.put(outputs, outputRefs)