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 | Static Public Attributes | Private Member Functions | List of all members
lsst.pipe.base.cmdLineTask.CmdLineTask Class Reference

Base class for command-line tasks: tasks that may be executed from the command line. More...

Inheritance diagram for lsst.pipe.base.cmdLineTask.CmdLineTask:

Public Member Functions

def applyOverrides
 A hook to allow a task to change the values of its config after the camera-specific overrides are loaded but before any command-line overrides are applied. More...
 
def parseAndRun
 Parse an argument list and run the command. More...
 
def writeConfig
 Write the configuration used for processing the data, or check that an existing one is equal to the new one if present. More...
 
def writeSchemas
 Write the schemas returned by getAllSchemaCatalogs. More...
 
def writeMetadata
 Write the metadata produced from processing the data. More...
 

Static Public Attributes

 RunnerClass = TaskRunner
 
 canMultiprocess = True
 

Private Member Functions

def _makeArgumentParser
 Create and return an argument parser. More...
 
def _getConfigName
 Return the name of the config dataset type, or None if config is not to be persisted. More...
 
def _getMetadataName
 Return the name of the metadata dataset type, or None if metadata is not to be persisted. More...
 

Detailed Description

Base class for command-line tasks: tasks that may be executed from the command line.

See pipe_base introduction to learn what tasks are, and how to write a command-line task for more information about writing command-line 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.

Subclasses must specify the following class variables:

Subclasses may also specify the following class variables:

Subclasses must specify a method named "run":

Definition at line 356 of file cmdLineTask.py.

Member Function Documentation

def lsst.pipe.base.cmdLineTask.CmdLineTask._getConfigName (   self)
private

Return the name of the config dataset type, or None if config is not to be persisted.

Note
The name may depend on the config; that is why this is not a class method.

Definition at line 529 of file cmdLineTask.py.

530  def _getConfigName(self):
531  """!Return the name of the config dataset type, or None if config is not to be persisted
532 
533  @note The name may depend on the config; that is why this is not a class method.
534  """
535  return self._DefaultName + "_config"
def _getConfigName
Return the name of the config dataset type, or None if config is not to be persisted.
Definition: cmdLineTask.py:529
def lsst.pipe.base.cmdLineTask.CmdLineTask._getMetadataName (   self)
private

Return the name of the metadata dataset type, or None if metadata is not to be persisted.

Note
The name may depend on the config; that is why this is not a class method.

Definition at line 536 of file cmdLineTask.py.

537  def _getMetadataName(self):
538  """!Return the name of the metadata dataset type, or None if metadata is not to be persisted
539 
540  @note The name may depend on the config; that is why this is not a class method.
541  """
542  return self._DefaultName + "_metadata"
def _getMetadataName
Return the name of the metadata dataset type, or None if metadata is not to be persisted.
Definition: cmdLineTask.py:536
def lsst.pipe.base.cmdLineTask.CmdLineTask._makeArgumentParser (   cls)
private

Create and return an argument parser.

Parameters
[in]clsthe class object
Returns
the argument parser for this task.

By default this returns an ArgumentParser with one ID argument named --id of dataset type "raw".

Your task subclass may need to override this method to change the dataset type or data ref level, or to add additional data ID arguments. If you add additional data ID arguments or your task's run method takes more than a single data reference then you will also have to provide a task-specific task runner (see TaskRunner for more information).

Definition at line 444 of file cmdLineTask.py.

445  def _makeArgumentParser(cls):
446  """!Create and return an argument parser
447 
448  @param[in] cls the class object
449  @return the argument parser for this task.
450 
451  By default this returns an ArgumentParser with one ID argument named `--id` of dataset type "raw".
452 
453  Your task subclass may need to override this method to change the dataset type or data ref level,
454  or to add additional data ID arguments. If you add additional data ID arguments or your task's
455  run method takes more than a single data reference then you will also have to provide a task-specific
456  task runner (see TaskRunner for more information).
457  """
458  parser = ArgumentParser(name=cls._DefaultName)
459  parser.add_id_argument(name="--id", datasetType="raw", help="data ID, e.g. --id visit=12345 ccd=1,2")
460  return parser
def _makeArgumentParser
Create and return an argument parser.
Definition: cmdLineTask.py:444
def lsst.pipe.base.cmdLineTask.CmdLineTask.applyOverrides (   cls,
  config 
)

A hook to allow a task to change the values of its config after the camera-specific overrides are loaded but before any command-line overrides are applied.

This is necessary in some cases because the camera-specific overrides may retarget subtasks, wiping out changes made in ConfigClass.setDefaults. See LSST Trac ticket #2282 for more discussion.

Warning
This is called by CmdLineTask.parseAndRun; other ways of constructing a config will not apply these overrides.
Parameters
[in]clsthe class object
[in]configtask configuration (an instance of cls.ConfigClass)

Definition at line 390 of file cmdLineTask.py.

391  def applyOverrides(cls, config):
392  """!A hook to allow a task to change the values of its config *after* the camera-specific
393  overrides are loaded but before any command-line overrides are applied.
394 
395  This is necessary in some cases because the camera-specific overrides may retarget subtasks,
396  wiping out changes made in ConfigClass.setDefaults. See LSST Trac ticket #2282 for more discussion.
397 
398  @warning This is called by CmdLineTask.parseAndRun; other ways of constructing a config
399  will not apply these overrides.
400 
401  @param[in] cls the class object
402  @param[in] config task configuration (an instance of cls.ConfigClass)
403  """
404  pass
def applyOverrides
A hook to allow a task to change the values of its config after the camera-specific overrides are loa...
Definition: cmdLineTask.py:390
def lsst.pipe.base.cmdLineTask.CmdLineTask.parseAndRun (   cls,
  args = None,
  config = None,
  log = None,
  doReturnResults = False 
)

Parse an argument list and run the command.

Calling this method with no arguments specified is the standard way to run a command-line task from the command line. For an example see pipe_tasks bin/makeSkyMap.py or almost any other file in that directory.

Parameters
clsthe class object
argslist of command-line arguments; if None use sys.argv
configconfig for task (instance of pex_config Config); if None use cls.ConfigClass()
loglog (instance of lsst.pex.logging.Log); if None use the default log
doReturnResultsReturn the collected results from each invocation of the task? This is only intended for unit tests and similar use. It can easily exhaust memory (if the task returns enough data and you call it enough times) and it will fail when using multiprocessing if the returned data cannot be pickled.
Returns
a Struct containing:
  • argumentParser: the argument parser
  • parsedCmd: the parsed command returned by the argument parser's parse_args method
  • taskRunner: the task runner used to run the task (an instance of cls.RunnerClass)
  • resultList: results returned by the task runner's run method, one entry per invocation. This will typically be a list of None unless doReturnResults is True; see cls.RunnerClass (TaskRunner by default) for more information.

Definition at line 406 of file cmdLineTask.py.

407  def parseAndRun(cls, args=None, config=None, log=None, doReturnResults=False):
408  """!Parse an argument list and run the command
409 
410  Calling this method with no arguments specified is the standard way to run a command-line task
411  from the command line. For an example see pipe_tasks `bin/makeSkyMap.py` or almost any other
412  file in that directory.
413 
414  @param cls the class object
415  @param args list of command-line arguments; if `None` use sys.argv
416  @param config config for task (instance of pex_config Config); if `None` use cls.ConfigClass()
417  @param log log (instance of lsst.pex.logging.Log); if `None` use the default log
418  @param doReturnResults Return the collected results from each invocation of the task?
419  This is only intended for unit tests and similar use.
420  It can easily exhaust memory (if the task returns enough data and you call it enough times)
421  and it will fail when using multiprocessing if the returned data cannot be pickled.
422 
423  @return a Struct containing:
424  - argumentParser: the argument parser
425  - parsedCmd: the parsed command returned by the argument parser's parse_args method
426  - taskRunner: the task runner used to run the task (an instance of cls.RunnerClass)
427  - resultList: results returned by the task runner's run method, one entry per invocation.
428  This will typically be a list of `None` unless doReturnResults is `True`;
429  see cls.RunnerClass (TaskRunner by default) for more information.
430  """
431  argumentParser = cls._makeArgumentParser()
432  if config is None:
433  config = cls.ConfigClass()
434  parsedCmd = argumentParser.parse_args(config=config, args=args, log=log, override=cls.applyOverrides)
435  taskRunner = cls.RunnerClass(TaskClass=cls, parsedCmd=parsedCmd, doReturnResults=doReturnResults)
436  resultList = taskRunner.run(parsedCmd)
437  return Struct(
438  argumentParser = argumentParser,
439  parsedCmd = parsedCmd,
440  taskRunner = taskRunner,
441  resultList = resultList,
442  )
def parseAndRun
Parse an argument list and run the command.
Definition: cmdLineTask.py:406
def lsst.pipe.base.cmdLineTask.CmdLineTask.writeConfig (   self,
  butler,
  clobber = False 
)

Write the configuration used for processing the data, or check that an existing one is equal to the new one if present.

Parameters
[in]butlerdata butler used to write the config. The config is written to dataset type self._getConfigName()
[in]clobbera boolean flag that controls what happens if a config already has been saved:
  • True: overwrite the existing config
  • False: raise TaskError if this config does not match the existing config

Definition at line 461 of file cmdLineTask.py.

462  def writeConfig(self, butler, clobber=False):
463  """!Write the configuration used for processing the data, or check that an existing
464  one is equal to the new one if present.
465 
466  @param[in] butler data butler used to write the config.
467  The config is written to dataset type self._getConfigName()
468  @param[in] clobber a boolean flag that controls what happens if a config already has been saved:
469  - True: overwrite the existing config
470  - False: raise TaskError if this config does not match the existing config
471  """
472  configName = self._getConfigName()
473  if configName is None:
474  return
475  if clobber:
476  butler.put(self.config, configName, doBackup=True)
477  elif butler.datasetExists(configName):
478  # this may be subject to a race condition; see #2789
479  oldConfig = butler.get(configName, immediate=True)
480  output = lambda msg: self.log.fatal("Comparing configuration: " + msg)
481  if not self.config.compare(oldConfig, shortcut=False, output=output):
482  raise TaskError(
483  ("Config does not match existing task config %r on disk; tasks configurations " + \
484  "must be consistent within the same output repo (override with --clobber-config)") % \
485  (configName,))
486  else:
487  butler.put(self.config, configName)
def writeConfig
Write the configuration used for processing the data, or check that an existing one is equal to the n...
Definition: cmdLineTask.py:461
def _getConfigName
Return the name of the config dataset type, or None if config is not to be persisted.
Definition: cmdLineTask.py:529
def lsst.pipe.base.cmdLineTask.CmdLineTask.writeMetadata (   self,
  dataRef 
)

Write the metadata produced from processing the data.

Parameters
[in]dataRefbutler data reference used to write the metadata. The metadata is written to dataset type self._getMetadataName()

Definition at line 516 of file cmdLineTask.py.

517  def writeMetadata(self, dataRef):
518  """!Write the metadata produced from processing the data
519 
520  @param[in] dataRef butler data reference used to write the metadata.
521  The metadata is written to dataset type self._getMetadataName()
522  """
523  try:
524  metadataName = self._getMetadataName()
525  if metadataName is not None:
526  dataRef.put(self.getFullMetadata(), metadataName)
527  except Exception, e:
528  self.log.warn("Could not persist metadata for dataId=%s: %s" % (dataRef.dataId, e,))
def _getMetadataName
Return the name of the metadata dataset type, or None if metadata is not to be persisted.
Definition: cmdLineTask.py:536
def writeMetadata
Write the metadata produced from processing the data.
Definition: cmdLineTask.py:516
def lsst.pipe.base.cmdLineTask.CmdLineTask.writeSchemas (   self,
  butler,
  clobber = False 
)

Write the schemas returned by getAllSchemaCatalogs.

Parameters
[in]butlerdata butler used to write the schema. Each schema is written to the dataset type specified as the key in the dict returned by getAllSchemaCatalogs.
[in]clobbera boolean flag that controls what happens if a schema already has been saved:
  • True: overwrite the existing schema
  • False: raise TaskError if this schema does not match the existing schema
Warning
if clobber is False and an existing schema does not match a current schema, then some schemas may have been saved successfully and others may not, and there is no easy way to tell which is which.

Definition at line 488 of file cmdLineTask.py.

489  def writeSchemas(self, butler, clobber=False):
490  """!Write the schemas returned by \ref task.Task.getAllSchemaCatalogs "getAllSchemaCatalogs"
491 
492  @param[in] butler data butler used to write the schema.
493  Each schema is written to the dataset type specified as the key in the dict returned by
494  \ref task.Task.getAllSchemaCatalogs "getAllSchemaCatalogs".
495  @param[in] clobber a boolean flag that controls what happens if a schema already has been saved:
496  - True: overwrite the existing schema
497  - False: raise TaskError if this schema does not match the existing schema
498 
499  @warning if clobber is False and an existing schema does not match a current schema,
500  then some schemas may have been saved successfully and others may not, and there is no easy way to
501  tell which is which.
502  """
503  for dataset, catalog in self.getAllSchemaCatalogs().iteritems():
504  schemaDataset = dataset + "_schema"
505  if clobber:
506  butler.put(catalog, schemaDataset, doBackup=True)
507  elif butler.datasetExists(schemaDataset):
508  oldSchema = butler.get(schemaDataset, immediate=True).getSchema()
509  if not oldSchema.compare(catalog.getSchema(), afwTable.Schema.IDENTICAL):
510  raise TaskError(
511  ("New schema does not match schema %r on disk; schemas must be " + \
512  " consistent within the same output repo (override with --clobber-config)") % \
513  (dataset,))
514  else:
515  butler.put(catalog, schemaDataset)
def writeSchemas
Write the schemas returned by getAllSchemaCatalogs.
Definition: cmdLineTask.py:488

Member Data Documentation

lsst.pipe.base.cmdLineTask.CmdLineTask.canMultiprocess = True
static

Definition at line 387 of file cmdLineTask.py.

lsst.pipe.base.cmdLineTask.CmdLineTask.RunnerClass = TaskRunner
static

Definition at line 386 of file cmdLineTask.py.


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