Coverage for python/lsst/pipe/tasks/exampleStatsTasks.py: 54%
37 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-05 12:14 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-05 12:14 -0700
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/>.
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
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 )
50class ExampleSigmaClippedStatsTask(pipeBase.Task):
51 """Example task to compute sigma-clipped mean and standard deviation of an image.
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.
57 Examples
58 --------
59 pipeTasks_ExampleSigmaClippedStatsTask_Example A complete example
60 of using ExampleSigmaClippedStatsTask
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]
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"
73 def __init__(self, *args, **kwargs):
74 pipeBase.Task.__init__(self, *args, **kwargs)
76 self._badPixelMask = afwImage.Mask.getPlaneBitMask(self.config.badMaskPlanes)
78 self._statsControl = afwMath.StatisticsControl()
79 self._statsControl.setNumSigmaClip(self.config.numSigmaClip)
80 self._statsControl.setNumIter(self.config.numIter)
81 self._statsControl.setAndMask(self._badPixelMask)
83 @timeMethod
84 def run(self, maskedImage):
85 """Compute and return statistics for a masked image.
87 Parameters
88 ----------
89 maskedImage : `lsst.afw.image.MaskedImage`
90 Masked image to compute statistics on.
92 Returns
93 -------
94 stats : `lsst.pipe.base.Struct`
95 Statistics as a struct with attributes:
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 )
120class ExampleSimpleStatsTask(pipeBase.Task):
121 """Example task to compute mean and standard deviation of an image.
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.
129 The main method is ExampleSimpleTask.run "run".
131 pipeTasks_ExampleSimpleStatsTask_Config Configuration parameters
133 This task has no configuration parameters.
135 pipeTasks_ExampleSimpleStatsTask_Debug Debug variables
137 This task has no debug variables.
139 pipeTasks_ExampleSimpleStatsTask_Example A complete example of using
140 ExampleSimpleStatsTask
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"
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.
164 Parameters
165 ----------
166 maskedImage : `lsst.afw.MaskedImage`
167 Masked image to compute statistics on.
169 Returns
170 -------
171 stats : `lsst.pipe.base.Struct`
172 Statistics as a struct with attributes:
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 """
183 self._statsControl = afwMath.StatisticsControl()
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)
191 return pipeBase.Struct(
192 mean=mean,
193 meanErr=meanErr,
194 stdDev=stdDev,
195 stdDevErr=stdDevErr,
196 )