LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
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 )
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