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