LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
exampleCmdLineTask.py
Go to the documentation of this file.
1 from __future__ import division, absolute_import
2 #
3 # LSST Data Management System
4 # Copyright 2014 LSST Corporation.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
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 LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 from lsst.afw.display.ds9 import mtv
24 import lsst.pex.config as pexConfig
25 import lsst.pipe.base as pipeBase
26 from .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 ## \addtogroup LSST_task_documentation
33 ## \{
34 ## \page pipeTasks_exampleTask
35 ## \ref ExampleCmdLineTask "ExampleCmdLineTask"
36 ## An example intended to show how to write a command-line task.
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 class ExampleCmdLineTask(pipeBase.CmdLineTask):
54  """!Example command-line task that computes simple statistics on an image
55 
56  \section pipeTasks_ExampleCmdLineTask_Contents Contents
57 
58  - \ref pipeTasks_ExampleCmdLineTask_Purpose
59  - \ref pipeTasks_ExampleCmdLineTask_Config
60  - \ref pipeTasks_ExampleCmdLineTask_Debug
61  - \ref pipeTasks_ExampleCmdLineTask_Example
62 
63  \section pipeTasks_ExampleCmdLineTask_Purpose Description
64 
65  \copybrief ExampleCmdLineTask
66 
67  This task was written as an example for the documents \ref pipeTasks_writeTask
68  and \ref pipeTasks_writeCmdLineTask.
69  The task reads in a "calexp" (a calibrated science \ref lsst::afw::image::Exposure "exposure"),
70  computes statistics on the image plane, and logs and returns the statistics.
71  In addition, if debugging is enabled, it displays the image in ds9.
72 
73  The image statistics are computed using a subtask, in order to show how to call subtasks and how to
74  \ref pipeBase_argumentParser_retargetSubtasks "retarget" (replace) them with variant subtasks.
75 
76  The main method is \ref ExampleCmdLineTask.run "run".
77 
78  \section pipeTasks_ExampleCmdLineTask_Config Configuration parameters
79 
80  See \ref ExampleCmdLineConfig
81 
82  \section pipeTasks_ExampleCmdLineTask_Debug Debug variables
83 
84  This task supports the following debug variables:
85  <dl>
86  <dt>`display`
87  <dd>If True then display the exposure in ds9
88  </dl>
89 
90  To enable debugging, see \ref baseDebug.
91 
92  \section pipeTasks_ExampleCmdLineTask_Example A complete example of using ExampleCmdLineTask
93 
94  This code is in examples/exampleCmdLineTask.py, and can be run as follows:
95  \code
96  examples/exampleCmdLineTask.py $OBS_TEST_DIR/data/input --id
97  # that will process all data; you can also try any combination of these flags:
98  --id filter=g
99  --config doFail=True --doraise
100  --show config data
101  \endcode
102  """
103  ConfigClass = ExampleCmdLineConfig
104  _DefaultName = "exampleTask"
105 
106  def __init__(self, *args, **kwargs):
107  """Construct an ExampleCmdLineTask
108 
109  Call the parent class constructor and make the "stats" subtask from the config field of the same name.
110  """
111  pipeBase.CmdLineTask.__init__(self, *args, **kwargs)
112  self.makeSubtask("stats")
113 
114  @pipeBase.timeMethod
115  def run(self, dataRef):
116  """!Compute a few statistics on the image plane of an exposure
117 
118  @param dataRef: data reference for a calibrated science exposure ("calexp")
119  @return a pipeBase Struct containing:
120  - mean: mean of image plane
121  - meanErr: uncertainty in mean
122  - stdDev: standard deviation of image plane
123  - stdDevErr: uncertainty in standard deviation
124  """
125  self.log.info("Processing data ID %s" % (dataRef.dataId,))
126  if self.config.doFail:
127  raise pipeBase.TaskError("Raising TaskError by request (config.doFail=True)")
128 
129  # Unpersist the raw exposure pointed to by the data reference
130  rawExp = dataRef.get("raw")
131  maskedImage = rawExp.getMaskedImage()
132 
133  # Support extra debug output.
134  # -
135  import lsstDebug
136  display = lsstDebug.Info(__name__).display
137  if display:
138  frame = 1
139  mtv(rawExp, frame=frame, title="exposure")
140 
141  # return the pipe_base Struct that is returned by self.stats.run
142  return self.stats.run(maskedImage)
143 
144  def _getConfigName(self):
145  """!Get the name prefix for the task config's dataset type, or None to prevent persisting the config
146 
147  This override returns None to avoid persisting metadata for this trivial task.
148 
149  However, if the method returns a name, then the full name of the dataset type will be <name>_config.
150  The default CmdLineTask._getConfigName returns _DefaultName,
151  which for this task would result in a dataset name of "exampleTask_config".
152 
153  Normally you can use the default CmdLineTask._getConfigName, but here are two reasons
154  why you might want to override it:
155  - If you do not want your task to write its config, then have the override return None.
156  That is done for this example task, because I didn't want to clutter up the
157  repository with config information for a trivial task.
158  - If the default name would not be unique. An example is
159  \ref lsst.pipe.tasks.makeSkyMap.MakeSkyMapTask "MakeSkyMapTask": it makes a
160  \ref lsst.skymap.SkyMap "sky map" (sky pixelization for a coadd)
161  for any of several different types of coadd, such as deep or goodSeeing.
162  As such, the name of the persisted config must include the coadd type in order to be unique.
163 
164  Normally if you override _getConfigName then you override _getMetadataName to match.
165  """
166  return None
167 
168  def _getMetadataName(self):
169  """!Get the name prefix for the task metadata's dataset type, or None to prevent persisting metadata
170 
171  This override returns None to avoid persisting metadata for this trivial task.
172 
173  However, if the method returns a name, then the full name of the dataset type will be <name>_metadata.
174  The default CmdLineTask._getConfigName returns _DefaultName,
175  which for this task would result in a dataset name of "exampleTask_metadata".
176 
177  See the description of _getConfigName for reasons to override this method.
178  """
179  return None
def _getConfigName
Get the name prefix for the task config&#39;s dataset type, or None to prevent persisting the config...
def _getMetadataName
Get the name prefix for the task metadata&#39;s dataset type, or None to prevent persisting metadata...
Example command-line task that computes simple statistics on an image.
def run
Compute a few statistics on the image plane of an exposure.