LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
exampleCmdLineTask.py
Go to the documentation of this file.
1 #!/usr/bin/env python
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 # The following block adds links to this task from the Task Documentation page.
29 # This works even for task(s) that are not in lsst.pipe.tasks.
30 ## \addtogroup LSST_task_documentation
31 ## \{
32 ## \page pipeTasks_exampleTask
33 ## \ref ExampleCmdLineTask "ExampleCmdLineTask"
34 ## An example intended to show how to write a command-line task.
35 ## \}
36 
37 class ExampleCmdLineConfig(pexConfig.Config):
38  """!Configuration for ExampleCmdLineTask
39  """
40  stats = pexConfig.ConfigurableField(
41  doc = "Subtask to compute statistics of an image",
42  target = ExampleSigmaClippedStatsTask,
43  )
44  doFail = pexConfig.Field(
45  doc = "Raise an lsst.base.TaskError exception when processing each image? " \
46  + "This allows one to see the effects of the --doraise command line flag",
47  dtype = bool,
48  default = False,
49  )
50 
51 class ExampleCmdLineTask(pipeBase.CmdLineTask):
52  """!Example command-line task that computes simple statistics on an image
53 
54  \section pipeTasks_ExampleCmdLineTask_Contents Contents
55 
56  - \ref pipeTasks_ExampleCmdLineTask_Purpose
57  - \ref pipeTasks_ExampleCmdLineTask_Config
58  - \ref pipeTasks_ExampleCmdLineTask_Debug
59  - \ref pipeTasks_ExampleCmdLineTask_Example
60 
61  \section pipeTasks_ExampleCmdLineTask_Purpose Description
62 
63  \copybrief ExampleCmdLineTask
64 
65  This task was written as an example for the documents \ref pipeTasks_writeTask
66  and \ref pipeTasks_writeCmdLineTask.
67  The task reads in a "calexp" (a calibrated science \ref lsst::afw::image::Exposure "exposure"),
68  computes statistics on the image plane, and logs and returns the statistics.
69  In addition, if debugging is enabled, it displays the image in ds9.
70 
71  The image statistics are computed using a subtask, in order to show how to call subtasks and how to
72  \ref pipeBase_argumentParser_retargetSubtasks "retarget" (replace) them with variant subtasks.
73 
74  The main method is \ref ExampleCmdLineTask.run "run".
75 
76  \section pipeTasks_ExampleCmdLineTask_Config Configuration parameters
77 
78  See \ref ExampleCmdLineConfig
79 
80  \section pipeTasks_ExampleCmdLineTask_Debug Debug variables
81 
82  This task supports the following debug variables:
83  <dl>
84  <dt>`display`
85  <dd>If True then display the exposure in ds9
86  </dl>
87 
88  To enable debugging, see \ref baseDebug.
89 
90  \section pipeTasks_ExampleCmdLineTask_Example A complete example of using ExampleCmdLineTask
91 
92  This code is in examples/exampleCmdLineTask.py, and can be run as _e.g._
93  \code
94  examples/exampleCmdLineTask.py <path_to_data_repo> --id <data_id>
95  # The following will work on an NCSA lsst* computer:
96  examples/exampleCmdLineTask.py /lsst8/krughoff/diffim_data/sparse_diffim_output_v7_2 --id visit=6866601
97  # also try these flags:
98  --config doFail=True --doraise
99  --show config data
100  \endcode
101  """
102  ConfigClass = ExampleCmdLineConfig
103  _DefaultName = "exampleTask"
104 
105  def __init__(self, *args, **kwargs):
106  """Construct an ExampleCmdLineTask
107 
108  Call the parent class constructor and make the "stats" subtask from the config field of the same name.
109  """
110  pipeBase.CmdLineTask.__init__(self, *args, **kwargs)
111  self.makeSubtask("stats")
112 
113  @pipeBase.timeMethod
114  def run(self, dataRef):
115  """!Compute a few statistics on the image plane of an exposure
116 
117  @param dataRef: data reference for a calibrated science exposure ("calexp")
118  @return a pipeBase Struct containing:
119  - mean: mean of image plane
120  - meanErr: uncertainty in mean
121  - stdDev: standard deviation of image plane
122  - stdDevErr: uncertainty in standard deviation
123  """
124  if self.config.doFail:
125  raise pipeBase.TaskError("Raising TaskError by request (config.doFail=True)")
126 
127  # Unpersist the data. In this case the data reference will retrieve a "calexp" by default,
128  # so the the string "calexp" is optiona, but the same data reference can be used
129  # to retrieve other dataset types that use the same data ID, so it is nice to be explicit
130  calExp = dataRef.get("calexp")
131  maskedImage = calExp.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(calExp, frame=frame, title="photocal")
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.