LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
exampleStatsTasks.py
Go to the documentation of this file.
1 #
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 #
22 import lsst.afw.image as afwImage
23 import lsst.afw.math as afwMath
24 import lsst.pex.config as pexConfig
25 import 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 
42 class 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 
63 class 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 = afwImage.Mask.getPlaneBitMask(self.config.badMaskPlanes)
112 
114  self._statsControl.setNumSigmaClip(self.config.numSigmaClip)
115  self._statsControl.setNumIter(self.config.numIter)
116  self._statsControl.setAndMask(self._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)
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 
143 class 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  """
203  statObj = afwMath.makeStatistics(maskedImage, afwMath.MEAN | afwMath.STDEV | afwMath.ERRORS,
204  self._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  )
Example task to compute sigma-clipped mean and standard deviation of an image.
def __init__(self, args, kwargs)
Construct an ExampleSigmaClippedStatsTask.
def run(self, maskedImage)
Compute and return statistics for a masked image.
def run(self, maskedImage)
Compute and return statistics for a masked image.
Statistics makeStatistics(lsst::afw::math::MaskedVector< EntryT > const &mv, std::vector< WeightPixel > const &vweights, int const flags, StatisticsControl const &sctrl=StatisticsControl())
The makeStatistics() overload to handle lsst::afw::math::MaskedVector<>
Definition: Statistics.h:520
Pass parameters to a Statistics object.
Definition: Statistics.h:93
Configuration for ExampleSigmaClippedStatsTask.
Example task to compute mean and standard deviation of an image.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...