22 from __future__
import absolute_import, division
25 from builtins
import object
31 from .timer
import logInfo
33 __all__ = [
"Task",
"TaskError"]
37 """Use to report errors for which a traceback is not useful. 41 Examples of such errors: 43 - processCcd is asked to run detection, but not calibration, and no calexp is found. 44 - coadd finds no valid images in the specified patch. 50 """Base class for data processing tasks. 52 See :ref:`task-framework-overview` to learn what tasks are, and :ref:`creating-a-task` for more 53 information about writing tasks. 57 config : `Task.ConfigClass` instance, optional 58 Configuration for this task (an instance of Task.ConfigClass, which is a task-specific subclass of 59 `lsst.pex.config.Config`, or `None`. If `None`: 61 - If parentTask specified then defaults to parentTask.config.<name> 62 - If parentTask is None then defaults to self.ConfigClass() 64 name : `str`, optional 65 Brief name of task, or `None`; if `None` then defaults to `Task._DefaultName` 66 parentTask : `Task`-type, optional 67 The parent task of this subtask, if any. 69 - If `None` (a top-level task) then you must specify config and name is ignored. 70 - If not `None` (a subtask) then you must specify name. 71 log : `lsst.log.Log`, optional 72 Log whose name is used as a log name prefix, or `None` for no prefix. Ignored if is parentTask 73 specified, in which case ``parentTask.log``\ 's name is used as a prefix. The task's log name is 74 ``prefix + "." + name`` if a prefix exists, else ``name``. The task's log is then a child logger of 75 ``parentTask.log`` (if ``parentTask`` specified), or a child logger of the log from the argument 76 (if ``log`` is not `None`). 81 Raised under these circumstances: 83 - If ``parentTask`` is `None` and ``config`` is `None`. 84 - If ``parentTask`` is not `None` and ``name`` is `None`. 85 - If ``name`` is `None` and ``_DefaultName`` does not exist. 89 Useful attributes include: 91 - ``log``: an lsst.log.Log 92 - ``config``: task-specific configuration; an instance of ``ConfigClass`` (see below). 93 - ``metadata``: an `lsst.daf.base.PropertyList` for collecting task-specific metadata, 94 e.g. data quality and performance metrics. This is data that is only meant to be 95 persisted, never to be used by the task. 97 Subclasses typically have a method named ``run`` to perform the main data processing. Details: 99 - ``run`` should process the minimum reasonable amount of data, typically a single CCD. 100 Iteration, if desired, is performed by a caller of the run method. This is good design and allows 101 multiprocessing without the run method having to support it directly. 102 - If ``run`` can persist or unpersist data: 104 - ``run`` should accept a butler data reference (or a collection of data references, if appropriate, 106 - There should be a way to run the task without persisting data. Typically the run method returns all 107 data, even if it is persisted, and the task's config method offers a flag to disable persistence. 109 **Deprecated:** Tasks other than cmdLineTask.CmdLineTask%s should *not* accept a blob such as a butler 110 data reference. How we will handle data references is still TBD, so don't make changes yet! 113 Subclasses must also have an attribute ``ConfigClass`` that is a subclass of `lsst.pex.config.Config` 114 which configures the task. Subclasses should also have an attribute ``_DefaultName``: 115 the default name if there is no parent task. ``_DefaultName`` is required for subclasses of 116 `~lsst.pipe.base.CmdLineTask` and recommended for subclasses of Task because it simplifies construction 117 (e.g. for unit tests). 119 Tasks intended to be run from the command line should be subclasses of `~lsst.pipe.base.CmdLineTask` 123 def __init__(self, config=None, name=None, parentTask=None, log=None):
127 if parentTask
is not None:
129 raise RuntimeError(
"name is required for a subtask")
131 self.
_fullName = parentTask._computeFullName(name)
133 config = getattr(parentTask.config, name)
135 loggerName = parentTask.log.getName() +
'.' + name
138 name = getattr(self,
"_DefaultName",
None)
140 raise RuntimeError(
"name is required for a task unless it has attribute _DefaultName")
141 name = self._DefaultName
145 config = self.ConfigClass()
148 if log
is not None and log.getName():
149 loggerName = log.getName() +
'.' + loggerName
151 self.
log = Log.getLogger(loggerName)
157 """Empty (clear) the metadata for this Task and all sub-Tasks. 160 subtask.metadata = dafBase.PropertyList()
163 """Get the schemas generated by this task. 167 schemaCatalogs : `dict` 168 Keys are butler dataset type, values are an empty catalog (an instance of the appropriate 169 `lsst.afw.table` Catalog type) for this task. 176 Subclasses that use schemas must override this method. The default implemenation returns 179 This method may be called at any time after the Task is constructed, which means that all task 180 schemas should be computed at construction time, *not* when data is actually processed. This 181 reflects the philosophy that the schema should not depend on the data. 183 Returning catalogs rather than just schemas allows us to save e.g. slots for SourceCatalog as well. 187 Task.getAllSchemaCatalogs 192 """Get schema catalogs for all tasks in the hierarchy, combining the results into a single dict. 196 schemacatalogs : `dict` 197 Keys are butler dataset type, values are a empty catalog (an instance of the appropriate 198 lsst.afw.table Catalog type) for all tasks in the hierarchy, from the top-level task down 199 through all subtasks. 203 This method may be called on any task in the hierarchy; it will return the same answer, regardless. 205 The default implementation should always suffice. If your subtask uses schemas the override 206 `Task.getSchemaCatalogs`, not this method. 210 schemaDict.update(subtask.getSchemaCatalogs())
214 """Get metadata for all tasks. 218 metadata : `lsst.daf.base.PropertySet` 219 The `~lsst.daf.base.PropertySet` keys are the full task name. Values are metadata 220 for the top-level task and all subtasks, sub-subtasks, etc.. 224 The returned metadata includes timing information (if ``@timer.timeMethod`` is used) 225 and any metadata set by the task. The name of each item consists of the full task name 226 with ``.`` replaced by ``:``, followed by ``.`` and the name of the item, e.g.:: 228 topLevelTaskName:subtaskName:subsubtaskName.itemName 230 using ``:`` in the full task name disambiguates the rare situation that a task has a subtask 231 and a metadata item with the same name. 233 fullMetadata = dafBase.PropertySet()
235 fullMetadata.set(fullName.replace(
".",
":"), task.metadata)
239 """Get the task name as a hierarchical name including parent task names. 244 The full name consists of the name of the parent task and each subtask separated by periods. 247 - The full name of top-level task "top" is simply "top". 248 - The full name of subtask "sub" of top-level task "top" is "top.sub". 249 - The full name of subtask "sub2" of subtask "sub" of top-level task "top" is "top.sub.sub2". 254 """Get the name of the task. 268 """Get a dictionary of all tasks as a shallow copy. 273 Dictionary containing full task name: task object for the top-level task and all subtasks, 279 """Create a subtask as a new instance as the ``name`` attribute of this task. 284 Brief name of the subtask. 286 Extra keyword arguments used to construct the task. The following arguments are automatically 287 provided and cannot be overridden: 294 The subtask must be defined by ``Task.config.name``, an instance of pex_config ConfigurableField 297 taskField = getattr(self.
config, name,
None)
298 if taskField
is None:
299 raise KeyError(
"%s's config does not have field %r" % (self.
getFullName(), name))
300 subtask = taskField.apply(name=name, parentTask=self, **keyArgs)
301 setattr(self, name, subtask)
303 @contextlib.contextmanager
304 def timer(self, name, logLevel=Log.DEBUG):
305 """Context manager to log performance data for an arbitrary block of code. 310 Name of code being timed; data will be logged using item name: ``Start`` and ``End``. 312 A `lsst.log` level constant. 316 Creating a timer context:: 318 with self.timer("someCodeToTime"): 325 logInfo(obj=self, prefix=name +
"Start", logLevel=logLevel)
329 logInfo(obj=self, prefix=name +
"End", logLevel=logLevel)
333 """Make a `lsst.pex.config.ConfigurableField` for this task. 338 Help text for the field. 342 configurableField : `lsst.pex.config.ConfigurableField` 343 A `~ConfigurableField` for this task. 347 Provides a convenient way to specify this task is a subtask of another task. 349 Here is an example of use:: 351 class OtherTaskConfig(lsst.pex.config.Config) 352 aSubtask = ATaskClass.makeField("a brief description of what this task does") 354 return ConfigurableField(doc=doc, target=cls)
356 def _computeFullName(self, name):
357 """Compute the full name of a subtask or metadata item, given its brief name. 362 Brief name of subtask or metadata item. 367 The full name: the ``name`` argument prefixed by the full task name and a period. 371 For example: if the full name of this task is "top.sub.sub2" 372 then ``_computeFullName("subname")`` returns ``"top.sub.sub2.subname"``.
def makeSubtask(self, name, keyArgs)
def getFullMetadata(self)
def getAllSchemaCatalogs(self)
def logInfo(obj, prefix, logLevel=Log.DEBUG)
def getSchemaCatalogs(self)
def timer(self, name, logLevel=Log.DEBUG)
def __init__(self, config=None, name=None, parentTask=None, log=None)