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

37 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-12 02:52 -0800

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 Notes 

58 ----- 

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

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

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

62 """ 

63 ConfigClass = ExampleSigmaClippedStatsConfig 

64 _DefaultName = "exampleSigmaClippedStats" 

65 

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

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

68 

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

70 

71 self._statsControl = afwMath.StatisticsControl() 

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

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

74 self._statsControl.setAndMask(self._badPixelMask) 

75 

76 @timeMethod 

77 def run(self, maskedImage): 

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

79 

80 Parameters 

81 ---------- 

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

83 Masked image to compute statistics on. 

84 

85 Returns 

86 ------- 

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

88 Statistics as a struct with attributes: 

89 

90 ``mean`` 

91 Mean of image plane (`float`). 

92 ``meanErr`` 

93 Uncertainty in mean (`float`). 

94 ``stdDev`` 

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

96 ``stdDevErr`` 

97 Uncertainty in standard deviation (`float`). 

98 """ 

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

100 self._statsControl) 

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

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

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

104 mean, meanErr, stdDev, stdDevErr) 

105 return pipeBase.Struct( 

106 mean=mean, 

107 meanErr=meanErr, 

108 stdDev=stdDev, 

109 stdDevErr=stdDevErr, 

110 ) 

111 

112 

113class ExampleSimpleStatsTask(pipeBase.Task): 

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

115 

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

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

118 and requires no special initialization. See also 

119 ExampleSigmaClippedStatsTask as a variant that is slightly more 

120 complicated. 

121 

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

123 

124 pipeTasks_ExampleSimpleStatsTask_Config Configuration parameters 

125 

126 This task has no configuration parameters. 

127 

128 pipeTasks_ExampleSimpleStatsTask_Debug Debug variables 

129 

130 This task has no debug variables. 

131 """ 

132 # Even a task with no configuration requires setting ConfigClass 

133 ConfigClass = pexConfig.Config 

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

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

136 # required for command-line tasks. 

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

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

139 _DefaultName = "exampleSimpleStats" 

140 

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

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

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

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

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

146 @timeMethod 

147 def run(self, maskedImage): 

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

149 

150 Parameters 

151 ---------- 

152 maskedImage : `lsst.afw.MaskedImage` 

153 Masked image to compute statistics on. 

154 

155 Returns 

156 ------- 

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

158 Statistics as a struct with attributes: 

159 

160 ``mean`` 

161 Mean of image plane (`float`). 

162 ``meanErr`` 

163 Uncertainty in mean (`float`). 

164 ``stdDev`` 

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

166 ``stdDevErr`` 

167 Uncertainty in standard deviation (`float`). 

168 """ 

169 self._statsControl = afwMath.StatisticsControl() 

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

171 self._statsControl) 

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

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

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

175 mean, meanErr, stdDev, stdDevErr) 

176 

177 return pipeBase.Struct( 

178 mean=mean, 

179 meanErr=meanErr, 

180 stdDev=stdDev, 

181 stdDevErr=stdDevErr, 

182 )