LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
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_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 
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  """
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  )
Pass parameters to a Statistics object.
Definition: Statistics.h:92
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.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Definition: Statistics.h:359