22 __all__ = [
"Task", 
"TaskError"]
 
   27 from lsst.pex.config 
import ConfigurableField
 
   30 from .timer 
import logInfo
 
   34     """Use to report errors for which a traceback is not useful. 
   38     Examples of such errors: 
   40     - processCcd is asked to run detection, but not calibration, and no calexp is found. 
   41     - coadd finds no valid images in the specified patch. 
   47     r"""Base class for data processing tasks. 
   49     See :ref:`task-framework-overview` to learn what tasks are, and :ref:`creating-a-task` for more 
   50     information about writing tasks. 
   54     config : `Task.ConfigClass` instance, optional 
   55         Configuration for this task (an instance of Task.ConfigClass, which is a task-specific subclass of 
   56         `lsst.pex.config.Config`, or `None`. If `None`: 
   58         - If parentTask specified then defaults to parentTask.config.<name> 
   59         - If parentTask is None then defaults to self.ConfigClass() 
   61     name : `str`, optional 
   62         Brief name of task, or `None`; if `None` then defaults to `Task._DefaultName` 
   63     parentTask : `Task`-type, optional 
   64         The parent task of this subtask, if any. 
   66         - If `None` (a top-level task) then you must specify config and name is ignored. 
   67         - If not `None` (a subtask) then you must specify name. 
   68     log : `lsst.log.Log`, optional 
   69         Log whose name is used as a log name prefix, or `None` for no prefix. Ignored if is parentTask 
   70         specified, in which case ``parentTask.log``\ 's name is used as a prefix. The task's log name is 
   71         ``prefix + "." + name`` if a prefix exists, else ``name``. The task's log is then a child logger of 
   72         ``parentTask.log`` (if ``parentTask`` specified), or a child logger of the log from the argument 
   73         (if ``log`` is not `None`). 
   78         Raised under these circumstances: 
   80         - If ``parentTask`` is `None` and ``config`` is `None`. 
   81         - If ``parentTask`` is not `None` and ``name`` is `None`. 
   82         - If ``name`` is `None` and ``_DefaultName`` does not exist. 
   86     Useful attributes include: 
   88     - ``log``: an lsst.log.Log 
   89     - ``config``: task-specific configuration; an instance of ``ConfigClass`` (see below). 
   90     - ``metadata``: an `lsst.daf.base.PropertyList` for collecting task-specific metadata, 
   91         e.g. data quality and performance metrics. This is data that is only meant to be 
   92         persisted, never to be used by the task. 
   94     Subclasses typically have a method named ``runDataRef`` to perform the main data processing. Details: 
   96     - ``runDataRef`` should process the minimum reasonable amount of data, typically a single CCD. 
   97       Iteration, if desired, is performed by a caller of the method. This is good design and allows 
   98       multiprocessing without the run method having to support it directly. 
   99     - If ``runDataRef`` can persist or unpersist data: 
  101       - ``runDataRef`` should accept a butler data reference (or a collection of data references, 
  102         if appropriate, e.g. coaddition). 
  103       - There should be a way to run the task without persisting data. Typically the run method returns all 
  104         data, even if it is persisted, and the task's config method offers a flag to disable persistence. 
  106     **Deprecated:** Tasks other than cmdLineTask.CmdLineTask%s should *not* accept a blob such as a butler 
  107     data reference.  How we will handle data references is still TBD, so don't make changes yet! 
  110     Subclasses must also have an attribute ``ConfigClass`` that is a subclass of `lsst.pex.config.Config` 
  111     which configures the task. Subclasses should also have an attribute ``_DefaultName``: 
  112     the default name if there is no parent task. ``_DefaultName`` is required for subclasses of 
  113     `~lsst.pipe.base.CmdLineTask` and recommended for subclasses of Task because it simplifies construction 
  114     (e.g. for unit tests). 
  116     Tasks intended to be run from the command line should be subclasses of `~lsst.pipe.base.CmdLineTask` 
  120     def __init__(self, config=None, name=None, parentTask=None, log=None):
 
  124         if parentTask 
is not None:
 
  126                 raise RuntimeError(
"name is required for a subtask")
 
  128             self.
_fullName = parentTask._computeFullName(name)
 
  130                 config = getattr(parentTask.config, name)
 
  132             loggerName = parentTask.log.getName() + 
'.' + name
 
  135                 name = getattr(self, 
"_DefaultName", 
None)
 
  137                     raise RuntimeError(
"name is required for a task unless it has attribute _DefaultName")
 
  138                 name = self._DefaultName
 
  142                 config = self.ConfigClass()
 
  145             if log 
is not None and log.getName():
 
  146                 loggerName = log.getName() + 
'.' + loggerName
 
  148         self.
log = Log.getLogger(loggerName)
 
  154         """Empty (clear) the metadata for this Task and all sub-Tasks. 
  160         """Get the schemas generated by this task. 
  164         schemaCatalogs : `dict` 
  165             Keys are butler dataset type, values are an empty catalog (an instance of the appropriate 
  166             `lsst.afw.table` Catalog type) for this task. 
  173            Subclasses that use schemas must override this method. The default implemenation returns 
  176         This method may be called at any time after the Task is constructed, which means that all task 
  177         schemas should be computed at construction time, *not* when data is actually processed. This 
  178         reflects the philosophy that the schema should not depend on the data. 
  180         Returning catalogs rather than just schemas allows us to save e.g. slots for SourceCatalog as well. 
  184         Task.getAllSchemaCatalogs 
  189         """Get schema catalogs for all tasks in the hierarchy, combining the results into a single dict. 
  193         schemacatalogs : `dict` 
  194             Keys are butler dataset type, values are a empty catalog (an instance of the appropriate 
  195             lsst.afw.table Catalog type) for all tasks in the hierarchy, from the top-level task down 
  196             through all subtasks. 
  200         This method may be called on any task in the hierarchy; it will return the same answer, regardless. 
  202         The default implementation should always suffice. If your subtask uses schemas the override 
  203         `Task.getSchemaCatalogs`, not this method. 
  207             schemaDict.update(subtask.getSchemaCatalogs())
 
  211         """Get metadata for all tasks. 
  215         metadata : `lsst.daf.base.PropertySet` 
  216             The `~lsst.daf.base.PropertySet` keys are the full task name. Values are metadata 
  217             for the top-level task and all subtasks, sub-subtasks, etc.. 
  221         The returned metadata includes timing information (if ``@timer.timeMethod`` is used) 
  222         and any metadata set by the task. The name of each item consists of the full task name 
  223         with ``.`` replaced by ``:``, followed by ``.`` and the name of the item, e.g.:: 
  225             topLevelTaskName:subtaskName:subsubtaskName.itemName 
  227         using ``:`` in the full task name disambiguates the rare situation that a task has a subtask 
  228         and a metadata item with the same name. 
  232             fullMetadata.set(fullName.replace(
".", 
":"), task.metadata)
 
  236         """Get the task name as a hierarchical name including parent task names. 
  241             The full name consists of the name of the parent task and each subtask separated by periods. 
  244             - The full name of top-level task "top" is simply "top". 
  245             - The full name of subtask "sub" of top-level task "top" is "top.sub". 
  246             - The full name of subtask "sub2" of subtask "sub" of top-level task "top" is "top.sub.sub2". 
  251         """Get the name of the task. 
  265         """Get a dictionary of all tasks as a shallow copy. 
  270             Dictionary containing full task name: task object for the top-level task and all subtasks, 
  276         """Create a subtask as a new instance as the ``name`` attribute of this task. 
  281             Brief name of the subtask. 
  283             Extra keyword arguments used to construct the task. The following arguments are automatically 
  284             provided and cannot be overridden: 
  291         The subtask must be defined by ``Task.config.name``, an instance of pex_config ConfigurableField 
  294         taskField = getattr(self.
config, name, 
None)
 
  295         if taskField 
is None:
 
  296             raise KeyError(f
"{self.getFullName()}'s config does not have field {name!r}")
 
  297         subtask = taskField.apply(name=name, parentTask=self, **keyArgs)
 
  298         setattr(self, name, subtask)
 
  300     @contextlib.contextmanager
 
  301     def timer(self, name, logLevel=Log.DEBUG):
 
  302         """Context manager to log performance data for an arbitrary block of code. 
  307             Name of code being timed; data will be logged using item name: ``Start`` and ``End``. 
  309             A `lsst.log` level constant. 
  313         Creating a timer context:: 
  315             with self.timer("someCodeToTime"): 
  322         logInfo(obj=self, prefix=name + 
"Start", logLevel=logLevel)
 
  326             logInfo(obj=self, prefix=name + 
"End", logLevel=logLevel)
 
  330         """Make a `lsst.pex.config.ConfigurableField` for this task. 
  335             Help text for the field. 
  339         configurableField : `lsst.pex.config.ConfigurableField` 
  340             A `~ConfigurableField` for this task. 
  344         Provides a convenient way to specify this task is a subtask of another task. 
  346         Here is an example of use:: 
  348             class OtherTaskConfig(lsst.pex.config.Config) 
  349                 aSubtask = ATaskClass.makeField("a brief description of what this task does") 
  351         return ConfigurableField(doc=doc, target=cls)
 
  353     def _computeFullName(self, name):
 
  354         """Compute the full name of a subtask or metadata item, given its brief name. 
  359             Brief name of subtask or metadata item. 
  364             The full name: the ``name`` argument prefixed by the full task name and a period. 
  368         For example: if the full name of this task is "top.sub.sub2" 
  369         then ``_computeFullName("subname")`` returns ``"top.sub.sub2.subname"``. 
  371         return f
"{self._fullName}.{name}"