LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
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:93
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:354