22from collections
import namedtuple
27from lsst.utils.timer
import timeMethod
29from .pluginsBase
import BasePlugin, BasePluginConfig
30from .pluginRegistry
import PluginRegistry, PluginMap
31from ._measBaseLib
import FatalAlgorithmError, MeasurementError
35FATAL_EXCEPTIONS = (MemoryError, FatalAlgorithmError)
37__all__ = (
"CatalogCalculationPluginConfig",
"CatalogCalculationPlugin",
"CatalogCalculationConfig",
38 "CatalogCalculationTask")
42 """Default configuration class for catalog calcuation plugins.
48 """Base class for catalog calculation plugins.
52 config : `CatalogCalculationPlugin.ConfigClass`
55 The string the plugin was registered with.
56 schema : `lsst.afw.table.Schema`
57 The source schema, New fields should be added here to
58 hold output produced by this plugin.
59 metadata : `lsst.daf.base.PropertySet`
60 Plugin metadata that will be attached to the output catalog
63 ConfigClass = CatalogCalculationPluginConfig
66 """List of available plugins (`lsst.meas.base.PluginRegistry`).
70 """Does the plugin operate on a single source or the whole catalog (`str`)?
72 If the plugin operates on a single source at a time, this should be set to
73 ``"single"``; if it expects the whoe catalog, to ``"multi"``. If the
74 plugin is of type ``"multi"``, the `fail` method must be implemented to
75 accept the whole catalog. If the plugin is of type ``"single"``, `fail`
76 should accept a single source record.
79 def __init__(self, config, name, schema, metadata):
80 BasePlugin.__init__(self, config, name)
84 r"""Used to set the relative order of plugin execution.
86 The values returned by `getExecutionOrder` are compared across all
87 plugins, and smaller numbers run first.
91 `CatalogCalculationPlugin`\s must run with
92 `BasePlugin.DEFAULT_CATALOGCALCULATION` or higher.
94 All plugins must implement this method with an appropriate run level
96 raise NotImplementedError()
99 """Perform the calculation specified by this plugin.
101 This method can either be used to operate on a single catalog record
102 or a whole catalog, populating it with the output defined by this
105 Note that results may be added to catalog records as new columns, or
106 may result in changes to existing values.
110 cat : `lsst.afw.table.SourceCatalog` or `lsst.afw.table.SourceRecord`
111 May either be a `~lsst.afw.table.SourceCatalog` or a single
112 `~lsst.afw.table.SourceRecord`, depending on the plugin type. Will
113 be updated in place to contain the results of plugin execution.
115 Any additional keyword arguments that may be passed to the plugin.
117 raise NotImplementedError()
121 """Handle errors that are thrown by catalog calculation plugins.
123 This is a context manager.
127 plugin : `CatalogCalculationPlugin`
128 The plugin that is to be run.
129 cat : `lsst.afw.table.SourceCatalog` or `lsst.afw.table.SourceRecord`
130 May either be a `~lsst.afw.table.SourceCatalog` or a single
131 `~lsst.afw.table.SourceRecord`, depending on the plugin type.
132 log : `lsst.log.Log` or `logging.Logger`
133 A logger. Generally, this should be the logger of the object in which
134 the context manager is being used.
144 def __exit__(self, exc_type, exc_value, traceback):
147 if exc_type
in FATAL_EXCEPTIONS:
149 elif exc_type
is MeasurementError:
150 self.plugin.fail(self.cat, exc_value)
151 elif issubclass(exc_type, Exception):
152 self.log.warning(
"Error in %s.calculate: %s", self.plugin.name, exc_value)
160 """Config class for the catalog calculation driver task.
162 Specifies which plugins will execute when the `CatalogCalculationTask`
163 associated with this configuration is run.
166 plugins = CatalogCalculationPlugin.registry.makeField(
168 default=[
"base_ClassificationExtendedness",
169 "base_FootprintArea"],
170 doc=
"Plugins to be run and their configuration")
174 """Run plugins which operate on a catalog of sources.
176 This task facilitates running plugins which will operate on a source
177 catalog. These plugins may do things such as classifying an object based
178 on source record entries inserted during a measurement task.
182 plugMetaData : `lsst.daf.base.PropertyList` or `None`
183 Will be modified in-place to contain metadata about the plugins being
184 run. If `None`, an empty `~lsst.daf.base.PropertyList` will be
187 Additional arguments passed to the superclass constructor.
191 Plugins may either take an entire catalog to work on at a time, or work on
194 ConfigClass = CatalogCalculationConfig
195 _DefaultName =
"catalogCalculation"
197 def __init__(self, schema, plugMetadata=None, **kwargs):
198 lsst.pipe.base.Task.__init__(self, **kwargs)
200 if plugMetadata
is None:
208 """Initialize the plugins according to the configuration.
211 pluginType = namedtuple(
'pluginType',
'single multi')
217 for executionOrder, name, config, PluginClass
in sorted(self.config.plugins.apply()):
219 self.
executionDict[executionOrder] = pluginType(single=[], multi=[])
220 if PluginClass.getExecutionOrder() >= BasePlugin.DEFAULT_CATALOGCALCULATION:
223 if plug.plugType ==
'single':
225 elif plug.plugType ==
'multi':
228 errorTuple = (PluginClass, PluginClass.getExecutionOrder(),
229 BasePlugin.DEFAULT_CATALOGCALCULATION)
230 raise ValueError(
"{} has an execution order less than the minimum for an catalogCalculation "
231 "plugin. Value {} : Minimum {}".format(*errorTuple))
235 """The entry point for the catalog calculation task.
239 meascat : `lsst.afw.table.SourceCatalog`
240 Catalog for measurement.
245 """Run each of the plugins on the catalog.
249 catalog : `lsst.afw.table.SourceCatalog`
250 The catalog on which the plugins will operate.
256 plug.calculate(catalog)
258 for measRecord
in catalog:
261 plug.calculate(measRecord)
__exit__(self, exc_type, exc_value, traceback)
__init__(self, plugin, cat, log)
__init__(self, config, name, schema, metadata)
calculate(self, cat, **kwargs)
callCompute(self, catalog)
__init__(self, schema, plugMetadata=None, **kwargs)