LSST Applications g070148d5b3+33e5256705,g0d53e28543+25c8b88941,g0da5cf3356+2dd1178308,g1081da9e2a+62d12e78cb,g17e5ecfddb+7e422d6136,g1c76d35bf8+ede3a706f7,g295839609d+225697d880,g2e2c1a68ba+cc1f6f037e,g2ffcdf413f+853cd4dcde,g38293774b4+62d12e78cb,g3b44f30a73+d953f1ac34,g48ccf36440+885b902d19,g4b2f1765b6+7dedbde6d2,g5320a0a9f6+0c5d6105b6,g56b687f8c9+ede3a706f7,g5c4744a4d9+ef6ac23297,g5ffd174ac0+0c5d6105b6,g6075d09f38+66af417445,g667d525e37+2ced63db88,g670421136f+2ced63db88,g71f27ac40c+2ced63db88,g774830318a+463cbe8d1f,g7876bc68e5+1d137996f1,g7985c39107+62d12e78cb,g7fdac2220c+0fd8241c05,g96f01af41f+368e6903a7,g9ca82378b8+2ced63db88,g9d27549199+ef6ac23297,gabe93b2c52+e3573e3735,gb065e2a02a+3dfbe639da,gbc3249ced9+0c5d6105b6,gbec6a3398f+0c5d6105b6,gc9534b9d65+35b9f25267,gd01420fc67+0c5d6105b6,geee7ff78d7+a14128c129,gf63283c776+ede3a706f7,gfed783d017+0c5d6105b6,w.2022.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
exampleStatsTasks.py
Go to the documentation of this file.
1# This file is part of pipe_tasks.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
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 GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22import lsst.afw.image as afwImage
23import lsst.afw.math as afwMath
24import lsst.pex.config as pexConfig
25import lsst.pipe.base as pipeBase
26from lsst.utils.timer import timeMethod
27
28
29class ExampleSigmaClippedStatsConfig(pexConfig.Config):
30 """Configuration for ExampleSigmaClippedStatsTask
31 """
32 badMaskPlanes = pexConfig.ListField(
33 dtype=str,
34 doc="Mask planes that, if set, indicate the associated pixel should "
35 "not be included when the calculating statistics.",
36 default=("EDGE",),
37 )
38 numSigmaClip = pexConfig.Field(
39 doc="number of sigmas at which to clip data",
40 dtype=float,
41 default=3.0,
42 )
43 numIter = pexConfig.Field(
44 doc="number of iterations of sigma clipping",
45 dtype=int,
46 default=2,
47 )
48
49
50class ExampleSigmaClippedStatsTask(pipeBase.Task):
51 """Example task to compute sigma-clipped mean and standard deviation of an image.
52
53 This is a simple example task designed to be run as a subtask by
54 ExampleCmdLineTask. See also ExampleSimpleStatsTask as a variant that is
55 even simpler.
56
57 Examples
58 --------
59 pipeTasks_ExampleSigmaClippedStatsTask_Example A complete example
60 of using ExampleSigmaClippedStatsTask
61
62 This code is in examples/exampleStatsTask.py (this one example runs both
63 ExampleSigmaClippedStatsTask and ExampleSimpleStatsTask), and can be run
64 as: examples/exampleStatsTask.py [fitsFile]
65
66 The init method may compute anything that that does not require data.
67 In this case we create a statistics control object using the config
68 (which cannot change once the task is created).
69 """
70 ConfigClass = ExampleSigmaClippedStatsConfig
71 _DefaultName = "exampleSigmaClippedStats"
72
73 def __init__(self, *args, **kwargs):
74 pipeBase.Task.__init__(self, *args, **kwargs)
75
76 self._badPixelMask = afwImage.Mask.getPlaneBitMask(self.config.badMaskPlanes)
77
79 self._statsControl.setNumSigmaClip(self.config.numSigmaClip)
80 self._statsControl.setNumIter(self.config.numIter)
81 self._statsControl.setAndMask(self._badPixelMask)
82
83 @timeMethod
84 def run(self, maskedImage):
85 """Compute and return statistics for a masked image.
86
87 Parameters
88 ----------
89 maskedImage : `lsst.afw.image.MaskedImage`
90 Masked image to compute statistics on.
91
92 Returns
93 -------
94 stats : `lsst.pipe.base.Struct`
95 Statistics as a struct with attributes:
96
97 ``mean``
98 Mean of image plane (`float`).
99 ``meanErr``
100 Uncertainty in mean (`float`).
101 ``stdDev``
102 Standard deviation of image plane (`float`).
103 ``stdDevErr``
104 Uncertainty in standard deviation (`float`).
105 """
106 statObj = afwMath.makeStatistics(maskedImage, afwMath.MEANCLIP | afwMath.STDEVCLIP | afwMath.ERRORS,
107 self._statsControl)
108 mean, meanErr = statObj.getResult(afwMath.MEANCLIP)
109 stdDev, stdDevErr = statObj.getResult(afwMath.STDEVCLIP)
110 self.log.info("clipped mean=%0.2f; meanErr=%0.2f; stdDev=%0.2f; stdDevErr=%0.2f",
111 mean, meanErr, stdDev, stdDevErr)
112 return pipeBase.Struct(
113 mean=mean,
114 meanErr=meanErr,
115 stdDev=stdDev,
116 stdDevErr=stdDevErr,
117 )
118
119
120class ExampleSimpleStatsTask(pipeBase.Task):
121 """Example task to compute mean and standard deviation of an image.
122
123 This was designed to be run as a subtask by ExampleCmdLineTask.
124 It is about as simple as a task can be; it has no configuration parameters
125 and requires no special initialization. See also
126 ExampleSigmaClippedStatsTask as a variant that is slightly more
127 complicated.
128
129 The main method is ExampleSimpleTask.run "run".
130
131 pipeTasks_ExampleSimpleStatsTask_Config Configuration parameters
132
133 This task has no configuration parameters.
134
135 pipeTasks_ExampleSimpleStatsTask_Debug Debug variables
136
137 This task has no debug variables.
138
139 pipeTasks_ExampleSimpleStatsTask_Example A complete example of using
140 ExampleSimpleStatsTask
141
142 This code is in examples/exampleStatsTask.py (this one example runs both
143 ExampleSigmaClippedStatsTask and ExampleSimpleStatsTask), and can be run
144 as: examples/exampleStatsTask.py [fitsFile]
145 """
146 # Even a task with no configuration requires setting ConfigClass
147 ConfigClass = pexConfig.Config
148 # Having a default name simplifies construction of the task, since the
149 # parent task need not specify a name. Note: having a default name is
150 # required for command-line tasks.
151 # The name can be simple and need not be unique (except for multiple
152 # subtasks that will be run by a parent task at the same time).
153 _DefaultName = "exampleSimpleStats"
154
155 # The `lsst.utils.timer.timeMethod` decorator measures how long a task
156 # method takes to run, and the resources needed to run it. The information
157 # is recorded in the task's `metadata` field.
158 # Most command-line tasks (not including the example below) save metadata
159 # for the task and all of its subtasks whenver the task is run.
160 @timeMethod
161 def run(self, maskedImage):
162 """Compute and return statistics for a masked image.
163
164 Parameters
165 ----------
166 maskedImage : `lsst.afw.MaskedImage`
167 Masked image to compute statistics on.
168
169 Returns
170 -------
171 stats : `lsst.pipe.base.Struct`
172 Statistics as a struct with attributes:
173
174 ``mean``
175 Mean of image plane (`float`).
176 ``meanErr``
177 Uncertainty in mean (`float`).
178 ``stdDev``
179 Standard deviation of image plane (`float`).
180 ``stdDevErr``
181 Uncertainty in standard deviation (`float`).
182 """
184 statObj = afwMath.makeStatistics(maskedImage, afwMath.MEAN | afwMath.STDEV | afwMath.ERRORS,
185 self._statsControl)
186 mean, meanErr = statObj.getResult(afwMath.MEAN)
187 stdDev, stdDevErr = statObj.getResult(afwMath.STDEV)
188 self.log.info("simple mean=%0.2f; meanErr=%0.2f; stdDev=%0.2f; stdDevErr=%0.2f",
189 mean, meanErr, stdDev, stdDevErr)
190
191 return pipeBase.Struct(
192 mean=mean,
193 meanErr=meanErr,
194 stdDev=stdDev,
195 stdDevErr=stdDevErr,
196 )
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:74
Pass parameters to a Statistics object.
Definition: Statistics.h:83
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:361