LSST Applications g180d380827+0f66a164bb,g2079a07aa2+86d27d4dc4,g2305ad1205+7d304bc7a0,g29320951ab+500695df56,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+e42ea45bea,g48712c4677+36a86eeaa5,g487adcacf7+2dd8f347ac,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+c70619cc9d,g5a732f18d5+53520f316c,g5ea96fc03c+341ea1ce94,g64a986408d+f7cd9c7162,g858d7b2824+f7cd9c7162,g8a8a8dda67+585e252eca,g99cad8db69+469ab8c039,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+c92fc63c7e,gbd866b1f37+f7cd9c7162,gc120e1dc64+02c66aa596,gc28159a63d+0e5473021a,gc3e9b769f7+b0068a2d9f,gcf0d15dbbd+e42ea45bea,gdaeeff99f8+f9a426f77a,ge6526c86ff+84383d05b3,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+f7cd9c7162,w.2024.17
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