LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
exampleCmdLineTask.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.display as afwDisplay
23import lsst.pex.config as pexConfig
24import lsst.pipe.base as pipeBase
25from lsst.utils.timer import timeMethod
26from .exampleStatsTasks import ExampleSigmaClippedStatsTask
27
28__all__ = ["ExampleCmdLineConfig", "ExampleCmdLineTask"]
29
30# The following block adds links to this task from the Task Documentation page.
31# This works even for task(s) that are not in lsst.pipe.tasks.
32
38
39
40class ExampleCmdLineConfig(pexConfig.Config):
41 """!Configuration for ExampleCmdLineTask
42 """
43 stats = pexConfig.ConfigurableField(
44 doc="Subtask to compute statistics of an image",
45 target=ExampleSigmaClippedStatsTask,
46 )
47 doFail = pexConfig.Field(
48 doc="Raise an lsst.base.TaskError exception when processing each image? "
49 "This allows one to see the effects of the --doraise command line flag",
50 dtype=bool,
51 default=False,
52 )
53
54
55class ExampleCmdLineTask(pipeBase.CmdLineTask):
56 r"""!
57 Example command-line task that computes simple statistics on an image
58
59 \section pipeTasks_ExampleCmdLineTask_Contents Contents
60
61 - \ref pipeTasks_ExampleCmdLineTask_Purpose
62 - \ref pipeTasks_ExampleCmdLineTask_Config
63 - \ref pipeTasks_ExampleCmdLineTask_Debug
64 - \ref pipeTasks_ExampleCmdLineTask_Example
65
66 \section pipeTasks_ExampleCmdLineTask_Purpose Description
67
68 \copybrief ExampleCmdLineTask
69
70 This task was written as an example for the documents
71 <a href="https://pipelines.lsst.io/modules/lsst.pipe.base/creating-a-task.html">Creating a task</a>
72 and <a href="https://pipelines.lsst.io/modules/lsst.pipe.base/creating-a-command-line-task.html">
73 Creating a command-line task</a>.
74 The task reads in a "calexp" (a calibrated science \ref lsst::afw::image::Exposure "exposure"),
75 computes statistics on the image plane, and logs and returns the statistics.
76 In addition, if debugging is enabled, it displays the image in current display backend.
77
78 The image statistics are computed using a subtask, in order to show how to call subtasks and how to
79 <a href="https://pipelines.lsst.io/modules/lsst.pipe.base/command-line-task-retargeting-howto.html">
80 retarget</a> (replace) them with variant subtasks.
81
82 The main method is \ref ExampleCmdLineTask.runDataRef "runDataRef".
83
84 \section pipeTasks_ExampleCmdLineTask_Config Configuration parameters
85
86 See \ref ExampleCmdLineConfig
87
88 \section pipeTasks_ExampleCmdLineTask_Debug Debug variables
89
90 This task supports the following debug variables:
91 <dl>
92 <dt>`display`
93 <dd>If True then display the exposure in current display backend
94 </dl>
95
96 To enable debugging, see
97 <a href="https://pipelines.lsst.io/modules/lsstDebug/">the lsstDebug documentation</a>.
98
99 \section pipeTasks_ExampleCmdLineTask_Example A complete example of using ExampleCmdLineTask
100
101 This code is in examples/exampleCmdLineTask.py, and can be run as follows:
102 \code
103 examples/exampleCmdLineTask.py $OBS_TEST_DIR/data/input --id
104 # that will process all data; you can also try any combination of these flags:
105 --id filter=g
106 --config doFail=True --doraise
107 --show config data
108 \endcode
109 """
110 ConfigClass = ExampleCmdLineConfig
111 _DefaultName = "exampleTask"
112
113 def __init__(self, *args, **kwargs):
114 """Construct an ExampleCmdLineTask
115
116 Call the parent class constructor and make the "stats" subtask from the config field of the same name.
117 """
118 pipeBase.CmdLineTask.__init__(self, *args, **kwargs)
119 self.makeSubtask("stats")
120
121 @timeMethod
122 def runDataRef(self, dataRef):
123 """!Compute a few statistics on the image plane of an exposure
124
125 @param dataRef: data reference for a calibrated science exposure ("calexp")
126 @return a pipeBase Struct containing:
127 - mean: mean of image plane
128 - meanErr: uncertainty in mean
129 - stdDev: standard deviation of image plane
130 - stdDevErr: uncertainty in standard deviation
131 """
132 self.log.info("Processing data ID %s", dataRef.dataId)
133 if self.config.doFail:
134 raise pipeBase.TaskError("Raising TaskError by request (config.doFail=True)")
135
136 # Unpersist the raw exposure pointed to by the data reference
137 rawExp = dataRef.get("raw")
138 maskedImage = rawExp.getMaskedImage()
139
140 # Support extra debug output.
141 # -
142 import lsstDebug
143 display = lsstDebug.Info(__name__).display
144 if display:
145 frame = 1
146 disp = afwDisplay.Display(frame=frame)
147 disp.mtv(rawExp, title="exposure")
148
149 # return the pipe_base Struct that is returned by self.stats.run
150 return self.stats.run(maskedImage)
151
152 def _getConfigName(self):
153 """!Get the name prefix for the task config's dataset type, or None to prevent persisting the config
154
155 This override returns None to avoid persisting metadata for this trivial task.
156
157 However, if the method returns a name, then the full name of the dataset type will be <name>_config.
158 The default CmdLineTask._getConfigName returns _DefaultName,
159 which for this task would result in a dataset name of "exampleTask_config".
160
161 Normally you can use the default CmdLineTask._getConfigName, but here are two reasons
162 why you might want to override it:
163 - If you do not want your task to write its config, then have the override return None.
164 That is done for this example task, because I didn't want to clutter up the
165 repository with config information for a trivial task.
166 - If the default name would not be unique. An example is
167 \ref lsst.pipe.tasks.makeSkyMap.MakeSkyMapTask "MakeSkyMapTask": it makes a
168 \ref lsst.skymap.SkyMap "sky map" (sky pixelization for a coadd)
169 for any of several different types of coadd, such as deep or goodSeeing.
170 As such, the name of the persisted config must include the coadd type in order to be unique.
171
172 Normally if you override _getConfigName then you override _getMetadataName to match.
173 """
174 return None
175
176 def _getMetadataName(self):
177 """!Get the name prefix for the task metadata's dataset type, or None to prevent persisting metadata
178
179 This override returns None to avoid persisting metadata for this trivial task.
180
181 However, if the method returns a name, then the full name of the dataset type will be <name>_metadata.
182 The default CmdLineTask._getConfigName returns _DefaultName,
183 which for this task would result in a dataset name of "exampleTask_metadata".
184
185 See the description of _getConfigName for reasons to override this method.
186 """
187 return None
table::Key< std::string > name
Definition: Amplifier.cc:116
table::Key< int > from
Example command-line task that computes simple statistics on an image.
def runDataRef(self, dataRef)
Compute a few statistics on the image plane of an exposure.
Make a sky map in a repository.
Definition: makeSkyMap.py:84
def run(self, coaddExposures, bbox, wcs, dataIds, **kwargs)
Definition: getTemplate.py:596