LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
exampleCmdLineTask.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2014 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 from lsst.afw.display.ds9 import mtv
23 import lsst.pex.config as pexConfig
24 import lsst.pipe.base as pipeBase
25 from .exampleStatsTasks import ExampleSigmaClippedStatsTask
26 
27 __all__ = ["ExampleCmdLineConfig", "ExampleCmdLineTask"]
28 
29 # The following block adds links to this task from the Task Documentation page.
30 # This works even for task(s) that are not in lsst.pipe.tasks.
31 
37 
38 
39 class ExampleCmdLineConfig(pexConfig.Config):
40  """!Configuration for ExampleCmdLineTask
41  """
42  stats = pexConfig.ConfigurableField(
43  doc="Subtask to compute statistics of an image",
44  target=ExampleSigmaClippedStatsTask,
45  )
46  doFail = pexConfig.Field(
47  doc="Raise an lsst.base.TaskError exception when processing each image? "
48  "This allows one to see the effects of the --doraise command line flag",
49  dtype=bool,
50  default=False,
51  )
52 
53 
54 class ExampleCmdLineTask(pipeBase.CmdLineTask):
55  r"""!Example command-line task that computes simple statistics on an image
56 
57  \section pipeTasks_ExampleCmdLineTask_Contents Contents
58 
59  - \ref pipeTasks_ExampleCmdLineTask_Purpose
60  - \ref pipeTasks_ExampleCmdLineTask_Config
61  - \ref pipeTasks_ExampleCmdLineTask_Debug
62  - \ref pipeTasks_ExampleCmdLineTask_Example
63 
64  \section pipeTasks_ExampleCmdLineTask_Purpose Description
65 
66  \copybrief ExampleCmdLineTask
67 
68  This task was written as an example for the documents \ref pipeTasks_writeTask
69  and \ref pipeTasks_writeCmdLineTask.
70  The task reads in a "calexp" (a calibrated science \ref lsst::afw::image::Exposure "exposure"),
71  computes statistics on the image plane, and logs and returns the statistics.
72  In addition, if debugging is enabled, it displays the image in ds9.
73 
74  The image statistics are computed using a subtask, in order to show how to call subtasks and how to
75  \ref pipeBase_argumentParser_retargetSubtasks "retarget" (replace) them with variant subtasks.
76 
77  The main method is \ref ExampleCmdLineTask.runDataRef "runDataRef".
78 
79  \section pipeTasks_ExampleCmdLineTask_Config Configuration parameters
80 
81  See \ref ExampleCmdLineConfig
82 
83  \section pipeTasks_ExampleCmdLineTask_Debug Debug variables
84 
85  This task supports the following debug variables:
86  <dl>
87  <dt>`display`
88  <dd>If True then display the exposure in ds9
89  </dl>
90 
91  To enable debugging, see \ref baseDebug.
92 
93  \section pipeTasks_ExampleCmdLineTask_Example A complete example of using ExampleCmdLineTask
94 
95  This code is in examples/exampleCmdLineTask.py, and can be run as follows:
96  \code
97  examples/exampleCmdLineTask.py $OBS_TEST_DIR/data/input --id
98  # that will process all data; you can also try any combination of these flags:
99  --id filter=g
100  --config doFail=True --doraise
101  --show config data
102  \endcode
103  """
104  ConfigClass = ExampleCmdLineConfig
105  _DefaultName = "exampleTask"
106 
107  def __init__(self, *args, **kwargs):
108  """Construct an ExampleCmdLineTask
109 
110  Call the parent class constructor and make the "stats" subtask from the config field of the same name.
111  """
112  pipeBase.CmdLineTask.__init__(self, *args, **kwargs)
113  self.makeSubtask("stats")
114 
115  @pipeBase.timeMethod
116  def runDataRef(self, dataRef):
117  """!Compute a few statistics on the image plane of an exposure
118 
119  @param dataRef: data reference for a calibrated science exposure ("calexp")
120  @return a pipeBase Struct containing:
121  - mean: mean of image plane
122  - meanErr: uncertainty in mean
123  - stdDev: standard deviation of image plane
124  - stdDevErr: uncertainty in standard deviation
125  """
126  self.log.info("Processing data ID %s" % (dataRef.dataId,))
127  if self.config.doFail:
128  raise pipeBase.TaskError("Raising TaskError by request (config.doFail=True)")
129 
130  # Unpersist the raw exposure pointed to by the data reference
131  rawExp = dataRef.get("raw")
132  maskedImage = rawExp.getMaskedImage()
133 
134  # Support extra debug output.
135  # -
136  import lsstDebug
137  display = lsstDebug.Info(__name__).display
138  if display:
139  frame = 1
140  mtv(rawExp, frame=frame, title="exposure")
141 
142  # return the pipe_base Struct that is returned by self.stats.run
143  return self.stats.run(maskedImage)
144 
145  def _getConfigName(self):
146  """!Get the name prefix for the task config's dataset type, or None to prevent persisting the config
147 
148  This override returns None to avoid persisting metadata for this trivial task.
149 
150  However, if the method returns a name, then the full name of the dataset type will be <name>_config.
151  The default CmdLineTask._getConfigName returns _DefaultName,
152  which for this task would result in a dataset name of "exampleTask_config".
153 
154  Normally you can use the default CmdLineTask._getConfigName, but here are two reasons
155  why you might want to override it:
156  - If you do not want your task to write its config, then have the override return None.
157  That is done for this example task, because I didn't want to clutter up the
158  repository with config information for a trivial task.
159  - If the default name would not be unique. An example is
160  \ref lsst.pipe.tasks.makeSkyMap.MakeSkyMapTask "MakeSkyMapTask": it makes a
161  \ref lsst.skymap.SkyMap "sky map" (sky pixelization for a coadd)
162  for any of several different types of coadd, such as deep or goodSeeing.
163  As such, the name of the persisted config must include the coadd type in order to be unique.
164 
165  Normally if you override _getConfigName then you override _getMetadataName to match.
166  """
167  return None
168 
169  def _getMetadataName(self):
170  """!Get the name prefix for the task metadata's dataset type, or None to prevent persisting metadata
171 
172  This override returns None to avoid persisting metadata for this trivial task.
173 
174  However, if the method returns a name, then the full name of the dataset type will be <name>_metadata.
175  The default CmdLineTask._getConfigName returns _DefaultName,
176  which for this task would result in a dataset name of "exampleTask_metadata".
177 
178  See the description of _getConfigName for reasons to override this method.
179  """
180  return None
def runDataRef(self, dataRef)
Compute a few statistics on the image plane of an exposure.
def mtv(data, frame=None, title="", wcs=None, args, kwargs)
Definition: ds9.py:93
Example command-line task that computes simple statistics on an image.