LSST Applications 26.0.0,g0265f82a02+6660c170cc,g07994bdeae+30b05a742e,g0a0026dc87+17526d298f,g0a60f58ba1+17526d298f,g0e4bf8285c+96dd2c2ea9,g0ecae5effc+c266a536c8,g1e7d6db67d+6f7cb1f4bb,g26482f50c6+6346c0633c,g2bbee38e9b+6660c170cc,g2cc88a2952+0a4e78cd49,g3273194fdb+f6908454ef,g337abbeb29+6660c170cc,g337c41fc51+9a8f8f0815,g37c6e7c3d5+7bbafe9d37,g44018dc512+6660c170cc,g4a941329ef+4f7594a38e,g4c90b7bd52+5145c320d2,g58be5f913a+bea990ba40,g635b316a6c+8d6b3a3e56,g67924a670a+bfead8c487,g6ae5381d9b+81bc2a20b4,g93c4d6e787+26b17396bd,g98cecbdb62+ed2cb6d659,g98ffbb4407+81bc2a20b4,g9ddcbc5298+7f7571301f,ga1e77700b3+99e9273977,gae46bcf261+6660c170cc,gb2715bf1a1+17526d298f,gc86a011abf+17526d298f,gcf0d15dbbd+96dd2c2ea9,gdaeeff99f8+0d8dbea60f,gdb4ec4c597+6660c170cc,ge23793e450+96dd2c2ea9,gf041782ebf+171108ac67
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 Notes
58 -----
59 The init method may compute anything that that does not require data.
60 In this case we create a statistics control object using the config
61 (which cannot change once the task is created).
62 """
63 ConfigClass = ExampleSigmaClippedStatsConfig
64 _DefaultName = "exampleSigmaClippedStats"
65
66 def __init__(self, *args, **kwargs):
67 pipeBase.Task.__init__(self, *args, **kwargs)
68
69 self._badPixelMask = afwImage.Mask.getPlaneBitMask(self.config.badMaskPlanes)
70
72 self._statsControl.setNumSigmaClip(self.config.numSigmaClip)
73 self._statsControl.setNumIter(self.config.numIter)
74 self._statsControl.setAndMask(self._badPixelMask)
75
76 @timeMethod
77 def run(self, maskedImage):
78 """Compute and return statistics for a masked image.
79
80 Parameters
81 ----------
82 maskedImage : `lsst.afw.image.MaskedImage`
83 Masked image to compute statistics on.
84
85 Returns
86 -------
87 stats : `lsst.pipe.base.Struct`
88 Statistics as a struct with attributes:
89
90 ``mean``
91 Mean of image plane (`float`).
92 ``meanErr``
93 Uncertainty in mean (`float`).
94 ``stdDev``
95 Standard deviation of image plane (`float`).
96 ``stdDevErr``
97 Uncertainty in standard deviation (`float`).
98 """
99 statObj = afwMath.makeStatistics(maskedImage, afwMath.MEANCLIP | afwMath.STDEVCLIP | afwMath.ERRORS,
100 self._statsControl)
101 mean, meanErr = statObj.getResult(afwMath.MEANCLIP)
102 stdDev, stdDevErr = statObj.getResult(afwMath.STDEVCLIP)
103 self.log.info("clipped mean=%0.2f; meanErr=%0.2f; stdDev=%0.2f; stdDevErr=%0.2f",
104 mean, meanErr, stdDev, stdDevErr)
105 return pipeBase.Struct(
106 mean=mean,
107 meanErr=meanErr,
108 stdDev=stdDev,
109 stdDevErr=stdDevErr,
110 )
111
112
113class ExampleSimpleStatsTask(pipeBase.Task):
114 """Example task to compute mean and standard deviation of an image.
115
116 This was designed to be run as a subtask by ExampleCmdLineTask.
117 It is about as simple as a task can be; it has no configuration parameters
118 and requires no special initialization. See also
119 ExampleSigmaClippedStatsTask as a variant that is slightly more
120 complicated.
121
122 The main method is ExampleSimpleTask.run "run".
123
124 pipeTasks_ExampleSimpleStatsTask_Config Configuration parameters
125
126 This task has no configuration parameters.
127
128 pipeTasks_ExampleSimpleStatsTask_Debug Debug variables
129
130 This task has no debug variables.
131 """
132 # Even a task with no configuration requires setting ConfigClass
133 ConfigClass = pexConfig.Config
134 # Having a default name simplifies construction of the task, since the
135 # parent task need not specify a name. Note: having a default name is
136 # required for command-line tasks.
137 # The name can be simple and need not be unique (except for multiple
138 # subtasks that will be run by a parent task at the same time).
139 _DefaultName = "exampleSimpleStats"
140
141 # The `lsst.utils.timer.timeMethod` decorator measures how long a task
142 # method takes to run, and the resources needed to run it. The information
143 # is recorded in the task's `metadata` field.
144 # Most command-line tasks (not including the example below) save metadata
145 # for the task and all of its subtasks whenver the task is run.
146 @timeMethod
147 def run(self, maskedImage):
148 """Compute and return statistics for a masked image.
149
150 Parameters
151 ----------
152 maskedImage : `lsst.afw.MaskedImage`
153 Masked image to compute statistics on.
154
155 Returns
156 -------
157 stats : `lsst.pipe.base.Struct`
158 Statistics as a struct with attributes:
159
160 ``mean``
161 Mean of image plane (`float`).
162 ``meanErr``
163 Uncertainty in mean (`float`).
164 ``stdDev``
165 Standard deviation of image plane (`float`).
166 ``stdDevErr``
167 Uncertainty in standard deviation (`float`).
168 """
170 statObj = afwMath.makeStatistics(maskedImage, afwMath.MEAN | afwMath.STDEV | afwMath.ERRORS,
171 self._statsControl)
172 mean, meanErr = statObj.getResult(afwMath.MEAN)
173 stdDev, stdDevErr = statObj.getResult(afwMath.STDEV)
174 self.log.info("simple mean=%0.2f; meanErr=%0.2f; stdDev=%0.2f; stdDevErr=%0.2f",
175 mean, meanErr, stdDev, stdDevErr)
176
177 return pipeBase.Struct(
178 mean=mean,
179 meanErr=meanErr,
180 stdDev=stdDev,
181 stdDevErr=stdDevErr,
182 )
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