lsst.pipe.tasks g0099ee1360+6048f86b6d
exampleStatsTasks.py
Go to the documentation of this file.
2# LSST Data Management System
3# Copyright 2014 LSST Corporation.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <http://www.lsstcorp.org/LegalNotices/>.
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
26
27__all__ = ["ExampleSigmaClippedStatsConfig", "ExampleSigmaClippedStatsTask", "ExampleSimpleStatsTask"]
28
29# The following block adds links to these tasks from the Task Documentation page.
30# This works even for task(s) that are not in lsst.pipe.tasks.
31
40
41
42class ExampleSigmaClippedStatsConfig(pexConfig.Config):
43 """!Configuration for ExampleSigmaClippedStatsTask
44 """
45 badMaskPlanes = pexConfig.ListField(
46 dtype=str,
47 doc="Mask planes that, if set, indicate the associated pixel should "
48 "not be included when the calculating statistics.",
49 default=("EDGE",),
50 )
51 numSigmaClip = pexConfig.Field(
52 doc="number of sigmas at which to clip data",
53 dtype=float,
54 default=3.0,
55 )
56 numIter = pexConfig.Field(
57 doc="number of iterations of sigma clipping",
58 dtype=int,
59 default=2,
60 )
61
62
63class ExampleSigmaClippedStatsTask(pipeBase.Task):
64 r"""!Example task to compute sigma-clipped mean and standard deviation of an image
65
66 \section pipeTasks_ExampleSigmaClippedStatsTask_Contents Contents
67
68 - \ref pipeTasks_ExampleSigmaClippedStatsTask_Purpose
69 - \ref pipeTasks_ExampleSigmaClippedStatsTask_Config
70 - \ref pipeTasks_ExampleSigmaClippedStatsTask_Debug
71 - \ref pipeTasks_ExampleSigmaClippedStatsTask_Example
72
73 \section pipeTasks_ExampleSigmaClippedStatsTask_Purpose Description
74
75 \copybrief ExampleSigmaClippedStatsTask
76
77 This is a simple example task designed to be run as a subtask by ExampleCmdLineTask.
78 See also ExampleSimpleStatsTask as a variant that is even simpler.
79
80 The main method is \ref ExampleSigmaClippedStatsTask.run "run".
81
82 \section pipeTasks_ExampleSigmaClippedStatsTask_Config Configuration parameters
83
84 See \ref ExampleSigmaClippedStatsConfig
85
86 \section pipeTasks_ExampleSigmaClippedStatsTask_Debug Debug variables
87
88 This task has no debug variables.
89
90 \section pipeTasks_ExampleSigmaClippedStatsTask_Example A complete example
91 of using ExampleSigmaClippedStatsTask
92
93 This code is in examples/exampleStatsTask.py (this one example runs both
94 ExampleSigmaClippedStatsTask and ExampleSimpleStatsTask), and can be run as:
95 \code
96 examples/exampleStatsTask.py [fitsFile]
97 \endcode
98 """
99 ConfigClass = ExampleSigmaClippedStatsConfig
100 _DefaultName = "exampleSigmaClippedStats"
101
102 def __init__(self, *args, **kwargs):
103 """!Construct an ExampleSigmaClippedStatsTask
104
105 The init method may compute anything that that does not require data.
106 In this case we create a statistics control object using the config
107 (which cannot change once the task is created).
108 """
109 pipeBase.Task.__init__(self, *args, **kwargs)
110
111 self._badPixelMask_badPixelMask = afwImage.Mask.getPlaneBitMask(self.config.badMaskPlanes)
112
113 self._statsControl_statsControl = afwMath.StatisticsControl()
114 self._statsControl_statsControl.setNumSigmaClip(self.config.numSigmaClip)
115 self._statsControl_statsControl.setNumIter(self.config.numIter)
116 self._statsControl_statsControl.setAndMask(self._badPixelMask_badPixelMask)
117
118 @pipeBase.timeMethod
119 def run(self, maskedImage):
120 """!Compute and return statistics for a masked image
121
122 @param[in] maskedImage: masked image (an lsst::afw::MaskedImage)
123 @return a pipeBase Struct containing:
124 - mean: mean of image plane
125 - meanErr: uncertainty in mean
126 - stdDev: standard deviation of image plane
127 - stdDevErr: uncertainty in standard deviation
128 """
129 statObj = afwMath.makeStatistics(maskedImage, afwMath.MEANCLIP | afwMath.STDEVCLIP | afwMath.ERRORS,
130 self._statsControl_statsControl)
131 mean, meanErr = statObj.getResult(afwMath.MEANCLIP)
132 stdDev, stdDevErr = statObj.getResult(afwMath.STDEVCLIP)
133 self.log.info("clipped mean=%0.2f; meanErr=%0.2f; stdDev=%0.2f; stdDevErr=%0.2f",
134 mean, meanErr, stdDev, stdDevErr)
135 return pipeBase.Struct(
136 mean=mean,
137 meanErr=meanErr,
138 stdDev=stdDev,
139 stdDevErr=stdDevErr,
140 )
141
142
143class ExampleSimpleStatsTask(pipeBase.Task):
144 r"""!Example task to compute mean and standard deviation of an image
145
146 \section pipeTasks_ExampleSimpleStatsTask_Contents Contents
147
148 - \ref pipeTasks_ExampleSimpleStatsTask_Purpose
149 - \ref pipeTasks_ExampleSimpleStatsTask_Config
150 - \ref pipeTasks_ExampleSimpleStatsTask_Debug
151 - \ref pipeTasks_ExampleSimpleStatsTask_Example
152
153 \section pipeTasks_ExampleSimpleStatsTask_Purpose Description
154
155 \copybrief ExampleSimpleStatsTask
156
157 This was designed to be run as a subtask by ExampleCmdLineTask.
158 It is about as simple as a task can be; it has no configuration parameters and requires no special
159 initialization. See also ExampleSigmaClippedStatsTask as a variant that is slightly more complicated.
160
161 The main method is \ref ExampleSimpleTask.run "run".
162
163 \section pipeTasks_ExampleSimpleStatsTask_Config Configuration parameters
164
165 This task has no configuration parameters.
166
167 \section pipeTasks_ExampleSimpleStatsTask_Debug Debug variables
168
169 This task has no debug variables.
170
171 \section pipeTasks_ExampleSimpleStatsTask_Example A complete example of using ExampleSimpleStatsTask
172
173 This code is in examples/exampleStatsTask.py (this one example runs both
174 ExampleSigmaClippedStatsTask and ExampleSimpleStatsTask), and can be run as:
175 \code
176 examples/exampleStatsTask.py [fitsFile]
177 \endcode
178 """
179 # Even a task with no configuration requires setting ConfigClass
180 ConfigClass = pexConfig.Config
181 # Having a default name simplifies construction of the task, since the parent task
182 # need not specify a name. Note: having a default name is required for command-line tasks.
183 # The name can be simple and need not be unique (except for multiple subtasks that will
184 # be run by a parent task at the same time).
185 _DefaultName = "exampleSimpleStats"
186
187 # The `lsst.pipe.timeMethod` decorator measures how long a task method takes to run,
188 # and the resources needed to run it. The information is recorded in the task's `metadata` field.
189 # Most command-line tasks (not including the example below) save metadata for the task
190 # and all of its subtasks whenver the task is run.
191 @pipeBase.timeMethod
192 def run(self, maskedImage):
193 """!Compute and return statistics for a masked image
194
195 @param[in] maskedImage: masked image (an lsst::afw::MaskedImage)
196 @return a pipeBase Struct containing:
197 - mean: mean of image plane
198 - meanErr: uncertainty in mean
199 - stdDev: standard deviation of image plane
200 - stdDevErr: uncertainty in standard deviation
201 """
202 self._statsControl_statsControl = afwMath.StatisticsControl()
203 statObj = afwMath.makeStatistics(maskedImage, afwMath.MEAN | afwMath.STDEV | afwMath.ERRORS,
204 self._statsControl_statsControl)
205 mean, meanErr = statObj.getResult(afwMath.MEAN)
206 stdDev, stdDevErr = statObj.getResult(afwMath.STDEV)
207 self.log.info("simple mean=%0.2f; meanErr=%0.2f; stdDev=%0.2f; stdDevErr=%0.2f",
208 mean, meanErr, stdDev, stdDevErr)
209
210 return pipeBase.Struct(
211 mean=mean,
212 meanErr=meanErr,
213 stdDev=stdDev,
214 stdDevErr=stdDevErr,
215 )
Configuration for ExampleSigmaClippedStatsTask.
Example task to compute sigma-clipped mean and standard deviation of an image.
def run(self, maskedImage)
Compute and return statistics for a masked image.
def __init__(self, *args, **kwargs)
Construct an ExampleSigmaClippedStatsTask.
Example task to compute mean and standard deviation of an image.
def run(self, maskedImage)
Compute and return statistics for a masked image.