LSSTApplications  16.0-1-gce273f5+18,16.0-10-gc1446dd+17,16.0-12-g569485f+1,16.0-12-g6065a46,16.0-13-g5e87145+1,16.0-13-g80874fd,16.0-13-gb122224+8,16.0-13-gd9b1b71+17,16.0-14-g08f9460,16.0-15-g77ef378+3,16.0-16-gf910c00+1,16.0-17-g0bdc215+10,16.0-17-g6a7bfb3b+17,16.0-2-g0febb12+14,16.0-2-g839ba83+55,16.0-2-g9d5294e+43,16.0-2-gc6e0ed0+1,16.0-21-g8fa7d5a4,16.0-3-g404ea43+10,16.0-3-gbc759ec+15,16.0-3-gcfd6c53+41,16.0-4-g03cf288+32,16.0-4-g13a27c5+18,16.0-4-g5f3a788+13,16.0-4-g8a0f11a+39,16.0-4-ga3eb747+3,16.0-5-g1991253+17,16.0-5-g1e9226d+1,16.0-5-g865efd9+18,16.0-5-gb3f8a4b+49,16.0-5-gd0f1235+7,16.0-51-gb4b4a8566,16.0-6-gf9cb114+19,16.0-7-g6043bfc+5,16.0-7-ga8e1655+13,16.0-7-gde5bd64,16.0-8-g4dec96c+30,16.0-8-gfd407c0+3,16.0-9-g2f60796,master-g5768c874b9+2,w.2018.41 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).

# Introduction

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.

# Configuration

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:

• Supply useful defaults for all config parameters if at all possible. Your task will be much easier to use if the default configuration usually suffices.
• Document each field of the configuration carefully. Pretend you don't know anything about your task and ask yourself what you would need to be told to change the parameter. What does the parameter do and why might you change it? What units does it have?

The configuration class is specified as ExampleSigmaClippedStatsTask class variable ConfigClass, as described in Class Variables.

Here is the config for ExampleCmdLineTask, a task that calls one subtask named "stats"; notice the lsst.pex.config.ConfigurableField:

# 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(...).

• 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.

# Methods

Tasks have the following important methods:

• __init__: construct and initialize a task
• run: process one item of data

These methods are described in more depth below:

## The __init__ Method

Use the __init__ method (task constructor) to do the following:

• Call the parent task's __init__ method
• Make subtasks by calling self.makeSubtask(<name>), where <name> is the name of a field of type lsst.pex.config.ConfigurableField in your task's pipeTasks_writeTask_configuration "configuration".

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:

This creates a binary mask identifying bad pixels in the mask plane and an lsst.afw.math.StatisticsControl, specifying how statistics are computed. Both of these are constants, and thus are the same for each invocation of the run method; this is strongly recommended, as explained in the next section.

## The run Method

Most tasks have a run method which perform's the task's data processing operation. In addition, command-line tasks require a runDataRef method that fetches the appropriate data products specified by Gen-2 Butler DataRefs. 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 and runDataRef methods 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; runDataRef does everything:

# Debug Variables

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":

import lsstDebug
display = lsstDebug.Info(__name__).display
if 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.

# Documentation

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:

• An overview of the important task methods, including run and runDataRef (if present), including links to the detailed documentation for each method.
• The detailed documentation for each method must carefully describe its parameters, including data type (unless blindingly obvious). If a method takes a data reference or a butler, describe which dataset types are read and written.
• A link to the config class, which should have clear help strings for each parameter.
• A description of debug options.
• A complete working example.

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.

## \addtogroup LSST_task_documentation
## \{
##      An example intended to show how to write a command-line task.
## \}


Each subtask is specified in the configuration as a field of type lsst.pex.config.ConfigurableField or (less commonly) lsst.pex.config.RegistryField. There are advantages to each:

• It is easier for the user to override settings of the subtask; simply use dotted name notation:

config.configurableSubtask.subtaskParam1 = ...


In contrast, to override configuration for a subtask specified as an lsst.pex.config.RegistryField you must either specify the name of the subtask to configure:

config.registrySubtask[nameOfSelectedSubtask].subtaskParam1 = ...


or use the active attribute to modify the configuration of the currently selected (active) subtask:

config.registrySubtask.active.subtaskParam1 = ...

• You can specify overrides for any registered subtask and they are remembered if you retarget subtasks. In comparison if the subtask is specified as an lsst.pex.config.ConfigurableField then you can only override parameters for the currently retargeted subtask, and all overrides are lost each time you retarget. Thus using an lsst.pex.config.RegistryField offers the opportunity to specify suitable overrides for more than one variant subtask, making it safer for the user to use those variants. Of course this can get out of hand if there are many variants, so users should not assume that all variants have suitable overrides.
• Retargeting a subtask can be done using --config on the command line, as long as the module containing the desired subtask has been imported:

config.registrySubtask.name = "foo"


By comparison, a subtask specified as an lsst.pex.config.ConfigurableField can only be retargeted from a config override file (e.g. using --configfile, never --config):

from ... import FooTask