LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
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