LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
lsst.pipe.base.task.Task Class Reference

Base class for data processing tasks. More...

Inheritance diagram for lsst.pipe.base.task.Task:

Public Member Functions

def __init__
 Create a Task. More...
 
def emptyMetadata
 Empty (clear) the metadata for this Task and all sub-Tasks. More...
 
def getSchemaCatalogs
 Return the schemas generated by this task. More...
 
def getAllSchemaCatalogs
 Call getSchemaCatalogs() on all tasks in the hiearchy, combining the results into a single dict. More...
 
def getFullMetadata
 Get metadata for all tasks. More...
 
def getFullName
 Return the task name as a hierarchical name including parent task names. More...
 
def getName
 Return the name of the task. More...
 
def getTaskDict
 Return a dictionary of all tasks as a shallow copy. More...
 
def makeSubtask
 Create a subtask as a new instance self. More...
 
def timer
 Context manager to log performance data for an arbitrary block of code. More...
 
def display
 Display an exposure and/or sources. More...
 
def makeField
 Make an lsst.pex.config.ConfigurableField for this task. More...
 

Public Attributes

 metadata
 
 config
 
 log
 

Private Member Functions

def _computeFullName
 Compute the full name of a subtask or metadata item, given its brief name. More...
 

Private Attributes

 _name
 
 _fullName
 
 _taskDict
 
 _display
 

Detailed Description

Base class for data processing tasks.

See pipe_base introduction to learn what tasks are, and how to write a task for more information about writing tasks. If the second link is broken (as it will be before the documentation is cross-linked) then look at the main page of pipe_tasks documentation for a link.

Useful attributes include:

Subclasses typically have a method named "run" to perform the main data processing. Details:

Deprecated:
Tasks other than cmdLineTask.CmdLineTasks should not accept a blob such as a butler data reference. How we will handle data references is still TBD, so don't make changes yet! RHL 2014-06-27

Subclasses must also have an attribute ConfigClass that is a subclass of lsst.pex.config.Config which configures the task. Subclasses should also have an attribute _DefaultName: the default name if there is no parent task. _DefaultName is required for subclasses of CmdLineTask and recommended for subclasses of Task because it simplifies construction (e.g. for unit tests).

Tasks intended to be run from the command line should be subclasses of CmdLineTask, not Task.

Definition at line 73 of file task.py.

Constructor & Destructor Documentation

def lsst.pipe.base.task.Task.__init__ (   self,
  config = None,
  name = None,
  parentTask = None,
  log = None 
)

Create a Task.

Parameters
[in]configconfiguration for this task (an instance of self.ConfigClass, which is a task-specific subclass of lsst.pex.config.Config), or None. If None:
  • If parentTask specified then defaults to parentTask.config.<name>
  • If parentTask is None then defaults to self.ConfigClass()
[in]namebrief name of task, or None; if None then defaults to self._DefaultName
[in]parentTaskthe parent task of this subtask, if any.
  • If None (a top-level task) then you must specify config and name is ignored.
  • If not None (a subtask) then you must specify name
[in]logpexLog log; if None then the default is used; in either case a copy is made using the full task name.
Exceptions
RuntimeErrorif parentTask is None and config is None.
RuntimeErrorif parentTask is not None and name is None.
RuntimeErrorif name is None and _DefaultName does not exist.

Definition at line 110 of file task.py.

111  def __init__(self, config=None, name=None, parentTask=None, log=None):
112  """!Create a Task
113 
114  @param[in] config configuration for this task (an instance of self.ConfigClass,
115  which is a task-specific subclass of lsst.pex.config.Config), or None. If None:
116  - If parentTask specified then defaults to parentTask.config.<name>
117  - If parentTask is None then defaults to self.ConfigClass()
118  @param[in] name brief name of task, or None; if None then defaults to self._DefaultName
119  @param[in] parentTask the parent task of this subtask, if any.
120  - If None (a top-level task) then you must specify config and name is ignored.
121  - If not None (a subtask) then you must specify name
122  @param[in] log pexLog log; if None then the default is used;
123  in either case a copy is made using the full task name.
124 
125  @throw RuntimeError if parentTask is None and config is None.
126  @throw RuntimeError if parentTask is not None and name is None.
127  @throw RuntimeError if name is None and _DefaultName does not exist.
128  """
130 
131  if parentTask != None:
132  if name is None:
133  raise RuntimeError("name is required for a subtask")
134  self._name = name
135  self._fullName = parentTask._computeFullName(name)
136  if config == None:
137  config = getattr(parentTask.config, name)
138  self._taskDict = parentTask._taskDict
139  else:
140  if name is None:
141  name = getattr(self, "_DefaultName", None)
142  if name is None:
143  raise RuntimeError("name is required for a task unless it has attribute _DefaultName")
144  name = self._DefaultName
145  self._name = name
146  self._fullName = self._name
147  if config == None:
148  config = self.ConfigClass()
149  self._taskDict = dict()
150 
151  self.config = config
152  if log == None:
153  log = pexLog.getDefaultLog()
154  self.log = pexLog.Log(log, self._fullName)
155  self._display = lsstDebug.Info(self.__module__).display
156  self._taskDict[self._fullName] = self
Class for storing ordered metadata with comments.
Definition: PropertyList.h:81
a place to record messages and descriptions of the state of processing.
Definition: Log.h:154
def __init__
Create a Task.
Definition: task.py:110

Member Function Documentation

def lsst.pipe.base.task.Task._computeFullName (   self,
  name 
)
private

Compute the full name of a subtask or metadata item, given its brief name.

For example: if the full name of this task is "top.sub.sub2" then _computeFullName("subname") returns "top.sub.sub2.subname".

Parameters
[in]namebrief name of subtask or metadata item
Returns
the full name: the "name" argument prefixed by the full task name and a period.

Definition at line 410 of file task.py.

411  def _computeFullName(self, name):
412  """!Compute the full name of a subtask or metadata item, given its brief name
413 
414  For example: if the full name of this task is "top.sub.sub2"
415  then _computeFullName("subname") returns "top.sub.sub2.subname".
416 
417  @param[in] name brief name of subtask or metadata item
418  @return the full name: the "name" argument prefixed by the full task name and a period.
419  """
420  return "%s.%s" % (self._fullName, name)
def _computeFullName
Compute the full name of a subtask or metadata item, given its brief name.
Definition: task.py:410
def lsst.pipe.base.task.Task.display (   self,
  name,
  exposure = None,
  sources = (),
  matches = None,
  ctypes = _DefaultDS9CTypes,
  ptypes = _DefaultDS9PTypes,
  sizes = (4,,
  pause = None,
  prompt = None 
)

Display an exposure and/or sources.

Warning
This method is deprecated. New code should call lsst.afw.display.ds9 directly.
Parameters
[in]namename of product to display
[in]exposureexposure to display (instance of lsst::afw::image::Exposure), or None
[in]sourceslist of Sources to display, as a single lsst.afw.table.SourceCatalog or a list of lsst.afw.table.SourceCatalog, or an empty list to not display sources
[in]matcheslist of source matches to display (instances of lsst.afw.table.ReferenceMatch), or None; if any matches are specified then exposure must be provided and have a lsst.afw.image.Wcs.
[in]ctypesarray of colors to use on ds9 for displaying sources and matches (in that order). ctypes is indexed as follows, where ctypes is repeatedly cycled through, if necessary:
  • ctypes[i] is used to display sources[i]
  • ctypes[len(sources) + 2i] is used to display matches[i][0]
  • ctypes[len(sources) + 2i + 1] is used to display matches[i][1]
[in]ptypesarray of ptypes to use on ds9 for displaying sources and matches; indexed like ctypes
[in]sizesarray of sizes to use on ds9 for displaying sources and matches; indexed like ctypes
[in]pausepause execution?
[in]promptprompt for user while paused (ignored if pause is False)
Warning
if matches are specified and exposure has no lsst.afw.image.Wcs then the matches are silently not shown.
Exceptions
Exceptionif matches specified and exposure is None

Definition at line 283 of file task.py.

284  pause=None, prompt=None):
285  """!Display an exposure and/or sources
286 
287  @warning This method is deprecated. New code should call lsst.afw.display.ds9 directly.
288 
289  @param[in] name name of product to display
290  @param[in] exposure exposure to display (instance of lsst::afw::image::Exposure), or None
291  @param[in] sources list of Sources to display, as a single lsst.afw.table.SourceCatalog
292  or a list of lsst.afw.table.SourceCatalog,
293  or an empty list to not display sources
294  @param[in] matches list of source matches to display (instances of
295  lsst.afw.table.ReferenceMatch), or None;
296  if any matches are specified then exposure must be provided and have a lsst.afw.image.Wcs.
297  @param[in] ctypes array of colors to use on ds9 for displaying sources and matches
298  (in that order).
299  ctypes is indexed as follows, where ctypes is repeatedly cycled through, if necessary:
300  - ctypes[i] is used to display sources[i]
301  - ctypes[len(sources) + 2i] is used to display matches[i][0]
302  - ctypes[len(sources) + 2i + 1] is used to display matches[i][1]
303  @param[in] ptypes array of ptypes to use on ds9 for displaying sources and matches;
304  indexed like ctypes
305  @param[in] sizes array of sizes to use on ds9 for displaying sources and matches;
306  indexed like ctypes
307  @param[in] pause pause execution?
308  @param[in] prompt prompt for user while paused (ignored if pause is False)
309 
310  @warning if matches are specified and exposure has no lsst.afw.image.Wcs then the matches are
311  silently not shown.
312 
313  @throw Exception if matches specified and exposure is None
314  """
315  # N.b. doxygen will complain about parameters like ds9 and RED not being documented. Bug ID 732356
316  if not self._display or self._display < 0:
317  return
318  if isinstance(self._display, dict):
319  if (name not in self._display) or not self._display[name] or self._display[name] < 0:
320  return
321 
322  if isinstance(self._display, int):
323  frame = self._display
324  elif isinstance(self._display, dict):
325  frame = self._display[name]
326  else:
327  frame = 1
328 
329  if exposure:
330  if isinstance(exposure, list):
331  raise RuntimeError("exposure may not be a list")
332  mi = exposure.getMaskedImage()
333  ds9.mtv(exposure, frame=frame, title=name)
334  x0, y0 = mi.getX0(), mi.getY0()
335  else:
336  x0, y0 = 0, 0
337 
338  try:
339  sources[0][0]
340  except IndexError: # empty list
341  pass
342  except (TypeError, NotImplementedError): # not a list of sets of sources
343  sources = [sources]
344 
345  with ds9.Buffering():
346  i = 0
347  for i, ss in enumerate(sources):
348  ctype = ctypes[i%len(ctypes)]
349  ptype = ptypes[i%len(ptypes)]
350  size = sizes[i%len(sizes)]
351 
352  for source in ss:
353  xc, yc = source.getX() - x0, source.getY() - y0
354  ds9.dot(ptype, xc, yc, size=size, frame=frame, ctype=ctype)
355  #try:
356  # mag = 25-2.5*math.log10(source.getPsfFlux())
357  # if mag > 15: continue
358  #except: continue
359  #ds9.dot("%.1f" % mag, xc, yc, frame=frame, ctype="red")
360 
361  if matches and exposure.getWcs() is not None:
362  wcs = exposure.getWcs()
363  with ds9.Buffering():
364  for first, second, d in matches:
365  i = len(sources) # counter for ptypes/ctypes, starting one after number of source lists
366  catPos = wcs.skyToPixel(first.getCoord())
367  x1, y1 = catPos.getX() - x0, catPos.getY() - y0
368 
369  ctype = ctypes[i%len(ctypes)]
370  ptype = ptypes[i%len(ptypes)]
371  size = 2*sizes[i%len(sizes)]
372  ds9.dot(ptype, x1, y1, size=size, frame=frame, ctype=ctype)
373  i += 1
374 
375  ctype = ctypes[i%len(ctypes)]
376  ptype = ptypes[i%len(ptypes)]
377  size = 2*sizes[i%len(sizes)]
378  x2, y2 = second.getX() - x0, second.getY() - y0
379  ds9.dot(ptype, x2, y2, size=size, frame=frame, ctype=ctype)
380  i += 1
381 
382  if pause:
383  if prompt is None:
384  prompt = "%s: Enter or c to continue [chp]: " % name
385  while True:
386  ans = raw_input(prompt).lower()
387  if ans in ("", "c",):
388  break
389  if ans in ("p",):
390  import pdb; pdb.set_trace()
391  elif ans in ("h", ):
392  print "h[elp] c[ontinue] p[db]"
def lsst.pipe.base.task.Task.emptyMetadata (   self)

Empty (clear) the metadata for this Task and all sub-Tasks.

Definition at line 157 of file task.py.

158  def emptyMetadata(self):
159  """!Empty (clear) the metadata for this Task and all sub-Tasks."""
160  for subtask in self._taskDict.itervalues():
161  subtask.metadata = dafBase.PropertyList()
Class for storing ordered metadata with comments.
Definition: PropertyList.h:81
def emptyMetadata
Empty (clear) the metadata for this Task and all sub-Tasks.
Definition: task.py:157
def lsst.pipe.base.task.Task.getAllSchemaCatalogs (   self)

Call getSchemaCatalogs() on all tasks in the hiearchy, combining the results into a single dict.

Returns
a dict of butler dataset type: empty catalog (an instance of the appropriate lsst.afw.table Catalog type) for all tasks in the hierarchy, from the top-level task down through all subtasks

This method may be called on any task in the hierarchy; it will return the same answer, regardless.

The default implementation should always suffice. If your subtask uses schemas the override Task.getSchemaCatalogs, not this method.

Definition at line 181 of file task.py.

182  def getAllSchemaCatalogs(self):
183  """!Call getSchemaCatalogs() on all tasks in the hiearchy, combining the results into a single dict.
184 
185  @return a dict of butler dataset type: empty catalog (an instance of the appropriate
186  lsst.afw.table Catalog type) for all tasks in the hierarchy, from the top-level task down
187  through all subtasks
188 
189  This method may be called on any task in the hierarchy; it will return the same answer, regardless.
190 
191  The default implementation should always suffice. If your subtask uses schemas the override
192  Task.getSchemaCatalogs, not this method.
193  """
194  schemaDict = self.getSchemaCatalogs()
195  for subtask in self._taskDict.itervalues():
196  schemaDict.update(subtask.getSchemaCatalogs())
197  return schemaDict
def getSchemaCatalogs
Return the schemas generated by this task.
Definition: task.py:162
def getAllSchemaCatalogs
Call getSchemaCatalogs() on all tasks in the hiearchy, combining the results into a single dict...
Definition: task.py:181
def lsst.pipe.base.task.Task.getFullMetadata (   self)

Get metadata for all tasks.

The returned metadata includes timing information (if @timer.timeMethod is used) and any metadata set by the task. The name of each item consists of the full task name with "." replaced by ":", followed by "." and the name of the item, e.g.: topLeveltTaskName:subtaskName:subsubtaskName.itemName using ":" in the full task name disambiguates the rare situation that a task has a subtask and a metadata item with the same name.

Returns
metadata: an lsst.daf.base.PropertySet containing full task name: metadata for the top-level task and all subtasks, sub-subtasks, etc.

Definition at line 198 of file task.py.

199  def getFullMetadata(self):
200  """!Get metadata for all tasks
201 
202  The returned metadata includes timing information (if \@timer.timeMethod is used)
203  and any metadata set by the task. The name of each item consists of the full task name
204  with "." replaced by ":", followed by "." and the name of the item, e.g.:
205  topLeveltTaskName:subtaskName:subsubtaskName.itemName
206  using ":" in the full task name disambiguates the rare situation that a task has a subtask
207  and a metadata item with the same name.
208 
209  @return metadata: an lsst.daf.base.PropertySet containing full task name: metadata
210  for the top-level task and all subtasks, sub-subtasks, etc.
211  """
212  fullMetadata = dafBase.PropertySet()
213  for fullName, task in self.getTaskDict().iteritems():
214  fullMetadata.set(fullName.replace(".", ":"), task.metadata)
215  return fullMetadata
def getTaskDict
Return a dictionary of all tasks as a shallow copy.
Definition: task.py:234
Class for storing generic metadata.
Definition: PropertySet.h:82
def getFullMetadata
Get metadata for all tasks.
Definition: task.py:198
def lsst.pipe.base.task.Task.getFullName (   self)

Return the task name as a hierarchical name including parent task names.

The full name consists of the name of the parent task and each subtask separated by periods. For example:

Definition at line 216 of file task.py.

217  def getFullName(self):
218  """!Return the task name as a hierarchical name including parent task names
219 
220  The full name consists of the name of the parent task and each subtask separated by periods.
221  For example:
222  - The full name of top-level task "top" is simply "top"
223  - The full name of subtask "sub" of top-level task "top" is "top.sub"
224  - The full name of subtask "sub2" of subtask "sub" of top-level task "top" is "top.sub.sub2".
225  """
226  return self._fullName
def getFullName
Return the task name as a hierarchical name including parent task names.
Definition: task.py:216
def lsst.pipe.base.task.Task.getName (   self)

Return the name of the task.

See getFullName to get a hierarchical name including parent task names

Definition at line 227 of file task.py.

228  def getName(self):
229  """!Return the name of the task
230 
231  See getFullName to get a hierarchical name including parent task names
232  """
233  return self._name
def getName
Return the name of the task.
Definition: task.py:227
def lsst.pipe.base.task.Task.getSchemaCatalogs (   self)

Return the schemas generated by this task.

Warning
Subclasses the use schemas must override this method. The default implemenation returns an empty dict.
Returns
a dict of butler dataset type: empty catalog (an instance of the appropriate lsst.afw.table Catalog type) for this task

This method may be called at any time after the Task is constructed, which means that all task schemas should be computed at construction time, not when data is actually processed. This reflects the philosophy that the schema should not depend on the data.

Returning catalogs rather than just schemas allows us to save e.g. slots for SourceCatalog as well.

See also Task.getAllSchemaCatalogs

Definition at line 162 of file task.py.

163  def getSchemaCatalogs(self):
164  """!Return the schemas generated by this task
165 
166  @warning Subclasses the use schemas must override this method. The default implemenation
167  returns an empty dict.
168 
169  @return a dict of butler dataset type: empty catalog (an instance of the appropriate
170  lsst.afw.table Catalog type) for this task
171 
172  This method may be called at any time after the Task is constructed, which means that
173  all task schemas should be computed at construction time, __not__ when data is actually
174  processed. This reflects the philosophy that the schema should not depend on the data.
175 
176  Returning catalogs rather than just schemas allows us to save e.g. slots for SourceCatalog as well.
177 
178  See also Task.getAllSchemaCatalogs
179  """
180  return {}
def getSchemaCatalogs
Return the schemas generated by this task.
Definition: task.py:162
def lsst.pipe.base.task.Task.getTaskDict (   self)

Return a dictionary of all tasks as a shallow copy.

Returns
taskDict: a dict containing full task name: task object for the top-level task and all subtasks, sub-subtasks, etc.

Definition at line 234 of file task.py.

235  def getTaskDict(self):
236  """!Return a dictionary of all tasks as a shallow copy.
237 
238  @return taskDict: a dict containing full task name: task object
239  for the top-level task and all subtasks, sub-subtasks, etc.
240  """
241  return self._taskDict.copy()
def getTaskDict
Return a dictionary of all tasks as a shallow copy.
Definition: task.py:234
def lsst.pipe.base.task.Task.makeField (   cls,
  doc 
)

Make an lsst.pex.config.ConfigurableField for this task.

Provides a convenient way to specify this task is a subtask of another task. Here is an example of use:

1 class OtherTaskConfig(lsst.pex.config.Config)
2  aSubtask = ATaskClass.makeField("a brief description of what this task does")
Parameters
[in]clsthis class
[in]dochelp text for the field
Returns
a lsst.pex.config.ConfigurableField for this task

Definition at line 394 of file task.py.

395  def makeField(cls, doc):
396  """!Make an lsst.pex.config.ConfigurableField for this task
397 
398  Provides a convenient way to specify this task is a subtask of another task.
399  Here is an example of use:
400  \code
401  class OtherTaskConfig(lsst.pex.config.Config)
402  aSubtask = ATaskClass.makeField("a brief description of what this task does")
403  \endcode
404 
405  @param[in] cls this class
406  @param[in] doc help text for the field
407  @return a lsst.pex.config.ConfigurableField for this task
408  """
409  return ConfigurableField(doc=doc, target=cls)
def makeField
Make an lsst.pex.config.ConfigurableField for this task.
Definition: task.py:394
def lsst.pipe.base.task.Task.makeSubtask (   self,
  name,
  keyArgs 
)

Create a subtask as a new instance self.

<name>

The subtask must be defined by self.config.<name>, an instance of pex_config ConfigurableField.

Parameters
namebrief name of subtask
**keyArgsextra keyword arguments used to construct the task. The following arguments are automatically provided and cannot be overridden: "config" and "parentTask".

Definition at line 242 of file task.py.

243  def makeSubtask(self, name, **keyArgs):
244  """!Create a subtask as a new instance self.<name>
245 
246  The subtask must be defined by self.config.<name>, an instance of pex_config ConfigurableField.
247 
248  @param name brief name of subtask
249  @param **keyArgs extra keyword arguments used to construct the task.
250  The following arguments are automatically provided and cannot be overridden:
251  "config" and "parentTask".
252  """
253  configurableField = getattr(self.config, name, None)
254  if configurableField is None:
255  raise KeyError("%s's config does not have field %r" % (self.getFullName, name))
256  subtask = configurableField.apply(name=name, parentTask=self, **keyArgs)
257  setattr(self, name, subtask)
def makeSubtask
Create a subtask as a new instance self.
Definition: task.py:242
def getFullName
Return the task name as a hierarchical name including parent task names.
Definition: task.py:216
def lsst.pipe.base.task.Task.timer (   self,
  name,
  logLevel = pexLog.Log.DEBUG 
)

Context manager to log performance data for an arbitrary block of code.

Parameters
[in]namename of code being timed; data will be logged using item name: <name>Start<item> and <name>End<item>
[in]logLevelone of the lsst.pex.logging.Log level constants

Example of use:

1 with self.timer("someCodeToTime"):
2  ...code to time...

See timer.logInfo for the information logged

Definition at line 259 of file task.py.

260  def timer(self, name, logLevel = pexLog.Log.DEBUG):
261  """!Context manager to log performance data for an arbitrary block of code
262 
263  @param[in] name name of code being timed;
264  data will be logged using item name: <name>Start<item> and <name>End<item>
265  @param[in] logLevel one of the lsst.pex.logging.Log level constants
266 
267  Example of use:
268  \code
269  with self.timer("someCodeToTime"):
270  ...code to time...
271  \endcode
272 
273  See timer.logInfo for the information logged
274  """
275  logInfo(obj = self, prefix = name + "Start", logLevel = logLevel)
276  try:
277  yield
278  finally:
279  logInfo(obj = self, prefix = name + "End", logLevel = logLevel)
def logInfo
Log timer information to obj.metadata and obj.log.
Definition: timer.py:53
def timer
Context manager to log performance data for an arbitrary block of code.
Definition: task.py:259

Member Data Documentation

lsst.pipe.base.task.Task._display
private

Definition at line 154 of file task.py.

lsst.pipe.base.task.Task._fullName
private

Definition at line 134 of file task.py.

lsst.pipe.base.task.Task._name
private

Definition at line 133 of file task.py.

lsst.pipe.base.task.Task._taskDict
private

Definition at line 137 of file task.py.

lsst.pipe.base.task.Task.config

Definition at line 150 of file task.py.

lsst.pipe.base.task.Task.log

Definition at line 153 of file task.py.

lsst.pipe.base.task.Task.metadata

Definition at line 128 of file task.py.


The documentation for this class was generated from the following file: