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