Coverage for python/lsst/pipe/tasks/exampleStatsTasks.py: 54%

37 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-14 15:47 -0700

1# This file is part of pipe_tasks. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://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 program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import lsst.afw.image as afwImage 

23import lsst.afw.math as afwMath 

24import lsst.pex.config as pexConfig 

25import lsst.pipe.base as pipeBase 

26from lsst.utils.timer import timeMethod 

27 

28 

29class ExampleSigmaClippedStatsConfig(pexConfig.Config): 

30 """Configuration for ExampleSigmaClippedStatsTask 

31 """ 

32 badMaskPlanes = pexConfig.ListField( 

33 dtype=str, 

34 doc="Mask planes that, if set, indicate the associated pixel should " 

35 "not be included when the calculating statistics.", 

36 default=("EDGE",), 

37 ) 

38 numSigmaClip = pexConfig.Field( 

39 doc="number of sigmas at which to clip data", 

40 dtype=float, 

41 default=3.0, 

42 ) 

43 numIter = pexConfig.Field( 

44 doc="number of iterations of sigma clipping", 

45 dtype=int, 

46 default=2, 

47 ) 

48 

49 

50class ExampleSigmaClippedStatsTask(pipeBase.Task): 

51 """Example task to compute sigma-clipped mean and standard deviation of an image. 

52 

53 This is a simple example task designed to be run as a subtask by 

54 ExampleCmdLineTask. See also ExampleSimpleStatsTask as a variant that is 

55 even simpler. 

56 

57 Examples 

58 -------- 

59 pipeTasks_ExampleSigmaClippedStatsTask_Example A complete example 

60 of using ExampleSigmaClippedStatsTask 

61 

62 This code is in examples/exampleStatsTask.py (this one example runs both 

63 ExampleSigmaClippedStatsTask and ExampleSimpleStatsTask), and can be run 

64 as: examples/exampleStatsTask.py [fitsFile] 

65 

66 The init method may compute anything that that does not require data. 

67 In this case we create a statistics control object using the config 

68 (which cannot change once the task is created). 

69 """ 

70 ConfigClass = ExampleSigmaClippedStatsConfig 

71 _DefaultName = "exampleSigmaClippedStats" 

72 

73 def __init__(self, *args, **kwargs): 

74 pipeBase.Task.__init__(self, *args, **kwargs) 

75 

76 self._badPixelMask = afwImage.Mask.getPlaneBitMask(self.config.badMaskPlanes) 

77 

78 self._statsControl = afwMath.StatisticsControl() 

79 self._statsControl.setNumSigmaClip(self.config.numSigmaClip) 

80 self._statsControl.setNumIter(self.config.numIter) 

81 self._statsControl.setAndMask(self._badPixelMask) 

82 

83 @timeMethod 

84 def run(self, maskedImage): 

85 """Compute and return statistics for a masked image. 

86 

87 Parameters 

88 ---------- 

89 maskedImage : `lsst.afw.image.MaskedImage` 

90 Masked image to compute statistics on. 

91 

92 Returns 

93 ------- 

94 stats : `lsst.pipe.base.Struct` 

95 Statistics as a struct with attributes: 

96 

97 ``mean`` 

98 Mean of image plane (`float`). 

99 ``meanErr`` 

100 Uncertainty in mean (`float`). 

101 ``stdDev`` 

102 Standard deviation of image plane (`float`). 

103 ``stdDevErr`` 

104 Uncertainty in standard deviation (`float`). 

105 """ 

106 statObj = afwMath.makeStatistics(maskedImage, afwMath.MEANCLIP | afwMath.STDEVCLIP | afwMath.ERRORS, 

107 self._statsControl) 

108 mean, meanErr = statObj.getResult(afwMath.MEANCLIP) 

109 stdDev, stdDevErr = statObj.getResult(afwMath.STDEVCLIP) 

110 self.log.info("clipped mean=%0.2f; meanErr=%0.2f; stdDev=%0.2f; stdDevErr=%0.2f", 

111 mean, meanErr, stdDev, stdDevErr) 

112 return pipeBase.Struct( 

113 mean=mean, 

114 meanErr=meanErr, 

115 stdDev=stdDev, 

116 stdDevErr=stdDevErr, 

117 ) 

118 

119 

120class ExampleSimpleStatsTask(pipeBase.Task): 

121 """Example task to compute mean and standard deviation of an image. 

122 

123 This was designed to be run as a subtask by ExampleCmdLineTask. 

124 It is about as simple as a task can be; it has no configuration parameters 

125 and requires no special initialization. See also 

126 ExampleSigmaClippedStatsTask as a variant that is slightly more 

127 complicated. 

128 

129 The main method is ExampleSimpleTask.run "run". 

130 

131 pipeTasks_ExampleSimpleStatsTask_Config Configuration parameters 

132 

133 This task has no configuration parameters. 

134 

135 pipeTasks_ExampleSimpleStatsTask_Debug Debug variables 

136 

137 This task has no debug variables. 

138 

139 pipeTasks_ExampleSimpleStatsTask_Example A complete example of using 

140 ExampleSimpleStatsTask 

141 

142 This code is in examples/exampleStatsTask.py (this one example runs both 

143 ExampleSigmaClippedStatsTask and ExampleSimpleStatsTask), and can be run 

144 as: examples/exampleStatsTask.py [fitsFile] 

145 """ 

146 # Even a task with no configuration requires setting ConfigClass 

147 ConfigClass = pexConfig.Config 

148 # Having a default name simplifies construction of the task, since the 

149 # parent task need not specify a name. Note: having a default name is 

150 # required for command-line tasks. 

151 # The name can be simple and need not be unique (except for multiple 

152 # subtasks that will be run by a parent task at the same time). 

153 _DefaultName = "exampleSimpleStats" 

154 

155 # The `lsst.utils.timer.timeMethod` decorator measures how long a task 

156 # method takes to run, and the resources needed to run it. The information 

157 # is recorded in the task's `metadata` field. 

158 # Most command-line tasks (not including the example below) save metadata 

159 # for the task and all of its subtasks whenver the task is run. 

160 @timeMethod 

161 def run(self, maskedImage): 

162 """Compute and return statistics for a masked image. 

163 

164 Parameters 

165 ---------- 

166 maskedImage : `lsst.afw.MaskedImage` 

167 Masked image to compute statistics on. 

168 

169 Returns 

170 ------- 

171 stats : `lsst.pipe.base.Struct` 

172 Statistics as a struct with attributes: 

173 

174 ``mean`` 

175 Mean of image plane (`float`). 

176 ``meanErr`` 

177 Uncertainty in mean (`float`). 

178 ``stdDev`` 

179 Standard deviation of image plane (`float`). 

180 ``stdDevErr`` 

181 Uncertainty in standard deviation (`float`). 

182 """ 

183 self._statsControl = afwMath.StatisticsControl() 

184 statObj = afwMath.makeStatistics(maskedImage, afwMath.MEAN | afwMath.STDEV | afwMath.ERRORS, 

185 self._statsControl) 

186 mean, meanErr = statObj.getResult(afwMath.MEAN) 

187 stdDev, stdDevErr = statObj.getResult(afwMath.STDEV) 

188 self.log.info("simple mean=%0.2f; meanErr=%0.2f; stdDev=%0.2f; stdDevErr=%0.2f", 

189 mean, meanErr, stdDev, stdDevErr) 

190 

191 return pipeBase.Struct( 

192 mean=mean, 

193 meanErr=meanErr, 

194 stdDev=stdDev, 

195 stdDevErr=stdDevErr, 

196 )