LSSTApplications
10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
|
This document describes how to write a data processing task. For an introduction to data processing tasks, please read pipe_base introduction. It also helps to have a basic understanding of data repositories and how to use the butler to read and write data (to be written; for now read existing tasks to see how it is done).
After reading this document you may wish to read How to Write a Command-Line Task for additional information needed to write a command-line task.
Tasks are subclasses of lsst.pipe.base.Task or (command-line tasks) lsst.pipe.base.CmdLineTask.
Tasks are constructed by calling __init__
with the task configuration. Occasionally additional arguments are required (see the task's documentation for details). lsst.pipe.base.Task.__init__ has a few other arguments that are usually only specified when a task is created as a subtask of another task; you will probably never have to specify them yourself. (Command-line tasks are often constructed and run by calling parseAndRun, as described in How to Write a Command-Line Task.)
Tasks typically have a run
method that executes the task's main function. See Methods for more information.
Every task requires a configuration: a task-specific set of configuration parameters. The configuration is read-only; once you construct a task, the same configuration will be used to process all data with that task. This makes the data processing more predictable: it does not depend on the order in which items of data are processed.
The task's configuration is specified using the pex_config package, as a task-specific subclass of lsst.pex.config.Config. The task class specifies its configuration class using class variable ConfigClass
. If the task has no configuration parameters then it may use lsst.pex.config.Config as its configuration class.
Some important details of configurations:
ExampleSigmaClippedStatsTask uses configuration class ExampleSigmaClippedStatsConfig:
The configuration class is specified as ExampleSigmaClippedStatsTask class variable ConfigClass
, as described in Class Variables.
Tasks require several class variables to function:
ConfigClass
: the configuration class used by the task._DefaultName
: a string used as the default name for the task. This is required for a command-line task, and strongly recommended for other tasks because it makes them easier to construct for unit tests. Note that when a task creates a subtask, it ignores the subtask's _DefaultName
and assigns the name of the config parameter as the subtask's name. For example exampleCmdLineTask.ExampleCmdLineConfig creates the statistics subtask with name "stats" because the config field for that subtask is stats = lsst.pex.config.ConfigurableField(...)
.
Task names are used for the hierarchy of task and subtask metadata. Also, for command-line tasks the name is used as a component of the of the dataset type for saving the task's configuration and metadata; see Persisting Config and Metadata for more information.
RunnerClass
: only needed for command-line tasks; see task runner for more information.canMultiprocess
: only needed for command-line tasks; see canMultiprocess for more information.Here are the class variables for exampleCmdLineTask.ExampleCmdLineTask:
Tasks have the following important methods:
__init__
: construct and initialize a taskrun
: process one item of dataThese methods are described in more depth below:
Use the __init__
method (task constructor) to do the following:
__init__
methodself.makeSubtask(<name>)
, where <name>
is the name of a field of type lsst.pex.config.ConfigurableField in your task's pipeTasks_writeTask_configuration "configuration".Here is exampleCmdLineTask.ExampleCmdLineTask.__init__:
That task creates a subtask named stats
to compute image statistics. Here is the __init__ method for the default version of the stats
subtask: exampleTask.ExampleSigmaClippedStatsTask, which is slightly more interesting:
run
method; this is strongly recommended, as explained in the next section.
Most tasks have a run
method which perform's the task's data processing operation. This is required for command-line tasks and strongly recommended for most other tasks. One exception is if your task needs different methods to handle different data types (C++ handles this using overloaded functions, but the standard technique is Python is to provide different methods for different call signatures).
If your task's processing can be divided into logical units, then we recommend that you provide methods for each unit. run
can then call each method to do its work. This allows your task to be more easily adapted: a subclass can override just a few methods.
We strongly recommend that you make your task stateless, by not using instance variables as part of your data processing. Pass data between methods by calling and returning it. This makes the task much easier to reason about, since processing one item of data cannot affect future items of data.
The run
method should always return its results in an lsst.pipe.base.struct.Struct object, with a named field for each item of data. This is safer than returning a tuple of items, and allows adding fields without affecting existing code. Other methods should also return Structs if they return more than one or two items.
Any method that is likely to take significant time or memory should be preceded by this python decorator: @lsst.pipe.base.timeMethod
. This automatically records the execution time and memory of the method in the task's metadata
attribute.
The example exampleCmdLineTask.ExampleCmdLineTask is so simple that it needs no other methods; run
does everything:
The statistics are actually computed by the "stats" subtask. Here is the run method for the default version of that task: exampleTask.ExampleSigmaClippedStatsTask.run:
Debug variables are variables the user may set while running your task, to enable additional debug output. To have your task support debug variables, have it import lsstDebug
and call lsstDebug.Info(__name__).
varname to get the debug variable varname specific to your task. If you look for a variable the user has not specified, it will have a value of False
. For example, to look for a debug variable named "display":
See Using lsstDebug to control debugging output for more information about debug variables, including how to specify them while running a command-line task.
For others to use your task, it must be clearly documented. pipe/tasks/exampleStatsTasks.py
and pipe/tasks/exampleCmdLineTask.py
provide useful examples and documentation templates.
Content should include:
run
(if present), including links to the detailed documentation for each method.Use """! instead of """ to start doc strings (i.e. include an exclamation mark). This causes Doxygen to parse Doxygen commands in the doc string, which is almost always what you want.
Include a section such as the following in your task's documentation. This will make it appear on the page Task Documentation, even if your task is not in the pipe_tasks package.
## \addtogroup LSST_task_documentation ## \{ ## \page exampleCmdLineTask ## \ref ExampleCmdLineTask "ExampleCmdLineTask" ## An example intended to show how to write a command-line task. ## \}