23 """Base measurement task, which subclassed by the single frame and forced measurement tasks.
25 from lsst.log
import Log
27 import lsst.pex.config
29 from .pluginRegistry
import PluginMap
30 from .exceptions
import FatalAlgorithmError, MeasurementError
31 from .pluginsBase
import BasePluginConfig, BasePlugin
32 from .noiseReplacer
import NoiseReplacerConfig
34 __all__ = (
"BaseMeasurementPluginConfig",
"BaseMeasurementPlugin",
35 "BaseMeasurementConfig",
"BaseMeasurementTask")
38 FATAL_EXCEPTIONS = (MemoryError, FatalAlgorithmError)
43 Base config class for all measurement plugins
45 Most derived classes will want to override setDefaults() in order to customize
46 the default exceutionOrder.
48 A derived class whose corresponding Plugin class implements measureN() should
49 additionally add a bool doMeasureN field to replace the bool class attribute
53 doMeasure = lsst.pex.config.Field(dtype=bool, default=
True,
54 doc=
"whether to run this plugin in single-object mode")
61 Base class for all measurement plugins
63 This is class is a placeholder for future behavior which will be shared only between
64 measurement plugins and is implemented for symmetry with the measurement base plugin
70 class SourceSlotConfig(lsst.pex.config.Config):
72 Slot configuration which assigns a particular named plugin to each of a set of
73 slots. Each slot allows a type of measurement to be fetched from the SourceTable
74 without knowing which algorithm was used to produced the data.
76 NOTE: the default algorithm for each slot must be registered, even if the default is not used.
79 centroid = lsst.pex.config.Field(dtype=str, default=
"base_SdssCentroid", optional=
True,
80 doc=
"the name of the centroiding algorithm used to set source x,y")
81 shape = lsst.pex.config.Field(dtype=str, default=
"base_SdssShape", optional=
True,
82 doc=
"the name of the algorithm used to set source moments parameters")
83 apFlux = lsst.pex.config.Field(dtype=str, default=
"base_CircularApertureFlux_12_0", optional=
True,
84 doc=
"the name of the algorithm used to set the source aperture flux slot")
85 modelFlux = lsst.pex.config.Field(dtype=str, default=
"base_GaussianFlux", optional=
True,
86 doc=
"the name of the algorithm used to set the source model flux slot")
87 psfFlux = lsst.pex.config.Field(dtype=str, default=
"base_PsfFlux", optional=
True,
88 doc=
"the name of the algorithm used to set the source psf flux slot")
89 instFlux = lsst.pex.config.Field(dtype=str, default=
"base_GaussianFlux", optional=
True,
90 doc=
"the name of the algorithm used to set the source inst flux slot")
91 calibFlux = lsst.pex.config.Field(dtype=str, default=
"base_CircularApertureFlux_12_0", optional=
True,
92 doc=
"the name of the flux measurement algorithm used for calibration")
95 """Convenience method to setup a Schema's slots according to the config definition.
97 This is defined in the Config class to support use in unit tests without needing
98 to construct a Task object.
100 aliases = schema.getAliasMap()
102 aliases.set(
"slot_Centroid", self.
centroid)
103 if self.
shape is not None:
104 aliases.set(
"slot_Shape", self.
shape)
105 if self.
apFlux is not None:
106 aliases.set(
"slot_ApFlux", self.
apFlux)
108 aliases.set(
"slot_ModelFlux", self.
modelFlux)
110 aliases.set(
"slot_PsfFlux", self.
psfFlux)
112 aliases.set(
"slot_InstFlux", self.
instFlux)
114 aliases.set(
"slot_CalibFlux", self.
calibFlux)
119 Base config class for all measurement driver tasks.
121 Subclasses should define the 'plugins' and 'undeblended' registries, e.g.:
123 plugins = PluginBaseClass.registry.makeField(
126 doc="Plugins to be run and their configuration"
128 undeblended = PluginBaseClass.registry.makeField(
131 doc="Plugins to run on undeblended image"
134 where PluginBaseClass is the appropriate base class of the plugin
135 (e.g., SingleFramePlugin or ForcedPlugin).
138 slots = lsst.pex.config.ConfigField(
139 dtype=SourceSlotConfig,
140 doc=
"Mapping from algorithms to special aliases in Source."
143 doReplaceWithNoise = lsst.pex.config.Field(
144 dtype=bool, default=
True, optional=
False,
145 doc=
'When measuring, replace other detected footprints with noise?')
147 noiseReplacer = lsst.pex.config.ConfigField(
148 dtype=NoiseReplacerConfig,
149 doc=
"configuration that sets how to replace neighboring sources with noise"
151 undeblendedPrefix = lsst.pex.config.Field(
152 dtype=str, default=
"undeblended_",
153 doc=
"Prefix to give undeblended plugins"
157 lsst.pex.config.Config.validate(self)
158 if self.slots.centroid
is not None and self.slots.centroid
not in self.plugins.names:
159 raise ValueError(
"source centroid slot algorithm is not being run.")
160 if self.slots.shape
is not None and self.slots.shape
not in self.plugins.names:
161 raise ValueError(
"source shape slot algorithm '%s' is not being run." % self.slots.shape)
162 for slot
in (self.slots.psfFlux, self.slots.apFlux, self.slots.modelFlux,
163 self.slots.instFlux, self.slots.calibFlux):
165 for name
in self.plugins.names:
166 if len(name) <= len(slot)
and name == slot[:len(name)]:
169 raise ValueError(
"source flux slot algorithm '%s' is not being run." % slot)
180 Ultimate base class for all measurement tasks.
182 This base class for SingleFrameMeasurementTask and ForcedMeasurementTask mostly exists to share
183 code between the two, and generally should not be used directly.
186 ConfigClass = BaseMeasurementConfig
187 _DefaultName =
"measurement"
191 Constructor; only called by derived classes.
193 @param[in] algMetadata An lsst.daf.base.PropertyList that will be filled with metadata
194 about the plugins being run. If None, an empty PropertyList will
196 @param[in] **kwds Additional arguments passed to lsst.pipe.base.Task.__init__.
198 This attaches two public attributes to the class for use by derived classes and parent tasks:
199 - plugins: an empty PluginMap, which will eventually contain all active plugins that will by
200 invoked by the run() method (to be filled by subclasses). This should be considered read-only.
201 - algMetadata: a lsst.daf.base.PropertyList that will contain additional information about the
202 active plugins to be saved with the output catalog (to be filled by subclasses).
204 super(BaseMeasurementTask, self).
__init__(**kwds)
207 if algMetadata
is None:
208 algMetadata = lsst.daf.base.PropertyList()
212 return self.log.getName() +
'.' + pluginName
215 """Initialize the plugins (and slots) according to the configuration.
217 Derived class constructors should call this method to fill the self.plugins
218 attribute and add correspond output fields and slot aliases to the output schema.
220 In addition to the attributes added by BaseMeasurementTask.__init__, a self.schema
221 attribute holding the output schema must also be present before this method is called, .
223 Keyword arguments are forwarded directly to plugin constructors, allowing derived
224 classes to use plugins with different signatures.
229 if self.config.slots.centroid
is not None:
230 self.
plugins[self.config.slots.centroid] =
None
232 for executionOrder, name, config, PluginClass
in sorted(self.config.plugins.apply()):
235 if hasattr(PluginClass,
"hasLogName")
and PluginClass.hasLogName:
244 if self.config.slots.centroid
is not None and self.
plugins[self.config.slots.centroid]
is None:
245 del self.
plugins[self.config.slots.centroid]
247 for executionOrder, name, config, PluginClass
in sorted(self.config.undeblended.apply()):
248 undeblendedName = self.config.undeblendedPrefix + name
254 Call the measure() method on all plugins, handling exceptions in a consistent way.
256 @param[in,out] measRecord lsst.afw.table.SourceRecord that corresponds to the object being
257 measured, and where outputs should be written.
258 @param[in] *args Positional arguments forwarded to Plugin.measure()
259 @param[in] **kwds Keyword arguments. Two are handled locally:
260 - beginOrder: beginning execution order (inclusive): measurements with
261 executionOrder < beginOrder are not executed. None for no limit.
262 - endOrder: ending execution order (exclusive): measurements with
263 executionOrder >= endOrder are not executed. None for no limit.
264 the rest are forwarded to Plugin.measure()
266 This method can be used with plugins that have different signatures; the only requirement is that
267 'measRecord' be the first argument. Subsequent positional arguments and keyword arguments are
268 forwarded directly to the plugin.
270 This method should be considered "protected"; it is intended for use by derived classes, not users.
272 beginOrder = kwds.pop(
"beginOrder",
None)
273 endOrder = kwds.pop(
"endOrder",
None)
274 for plugin
in self.plugins.iter():
275 if beginOrder
is not None and plugin.getExecutionOrder() < beginOrder:
277 if endOrder
is not None and plugin.getExecutionOrder() >= endOrder:
283 Call the measure() method on the nominated plugin, handling exceptions in a consistent way.
285 @param[in] plugin Plugin that will measure
286 @param[in,out] measRecord lsst.afw.table.SourceRecord that corresponds to the object being
287 measured, and where outputs should be written.
288 @param[in] *args Positional arguments forwarded to plugin.measure()
289 @param[in] **kwds Keyword arguments forwarded to plugin.measure()
291 This method can be used with plugins that have different signatures; the only requirement is that
292 the 'plugin' and 'measRecord' be the first two arguments. Subsequent positional arguments and
293 keyword arguments are forwarded directly to the plugin.
295 This method should be considered "protected"; it is intended for use by derived classes, not users.
298 plugin.measure(measRecord, *args, **kwds)
299 except FATAL_EXCEPTIONS:
301 except MeasurementError
as error:
303 "MeasurementError in %s.measure on record %s: %s"
304 % (plugin.name, measRecord.getId(), error))
305 plugin.fail(measRecord, error)
306 except Exception
as error:
308 "Exception in %s.measure on record %s: %s"
309 % (plugin.name, measRecord.getId(), error))
310 plugin.fail(measRecord)
314 Call the measureN() method on all plugins, handling exceptions in a consistent way.
316 @param[in,out] measCat lsst.afw.table.SourceCatalog containing records for just
317 the source family to be measured, and where outputs should
319 @param[in] beginOrder beginning execution order (inclusive): measurements with
320 executionOrder < beginOrder are not executed. None for no limit.
321 @param[in] endOrder ending execution order (exclusive): measurements with
322 executionOrder >= endOrder are not executed. None for no limit.
323 @param[in] *args Positional arguments forwarded to Plugin.measure()
324 @param[in] **kwds Keyword arguments. Two are handled locally:
325 - beginOrder: beginning execution order (inclusive): measurements with
326 executionOrder < beginOrder are not executed. None for no limit.
327 - endOrder: ending execution order (exclusive): measurements with
328 executionOrder >= endOrder are not executed. None for no limit.
329 the rest are forwarded to Plugin.measure()
331 This method can be used with plugins that have different signatures; the only requirement is that
332 'measRecord' be the first argument. Subsequent positional arguments and keyword arguments are
333 forwarded directly to the plugin.
335 This method should be considered "protected"; it is intended for use by derived classes, not users.
337 beginOrder = kwds.pop(
"beginOrder",
None)
338 endOrder = kwds.pop(
"endOrder",
None)
339 for plugin
in self.plugins.iterN():
340 if beginOrder
is not None and plugin.getExecutionOrder() < beginOrder:
342 if endOrder
is not None and plugin.getExecutionOrder() >= endOrder:
348 Call the measureN() method on the nominated plugin, handling exceptions in a consistent way.
350 @param[in] plugin Plugin that will measure
351 @param[in,out] measCat lsst.afw.table.SourceCatalog containing records for just
352 the source family to be measured, and where outputs should
354 @param[in] *args Positional arguments forwarded to plugin.measureN()
355 @param[in] **kwds Keyword arguments forwarded to plugin.measureN()
357 This method can be used with plugins that have different signatures; the only requirement is that
358 the 'plugin' and 'measCat' be the first two arguments. Subsequent positional arguments and
359 keyword arguments are forwarded directly to the plugin.
361 This method should be considered "protected"; it is intended for use by derived classes, not users.
364 plugin.measureN(measCat, *args, **kwds)
365 except FATAL_EXCEPTIONS:
368 except MeasurementError
as error:
369 for measRecord
in measCat:
371 "MeasurementError in %s.measureN on records %s-%s: %s"
372 % (plugin.name, measCat[0].getId(), measCat[-1].getId(), error))
373 plugin.fail(measRecord, error)
374 except Exception
as error:
375 for measRecord
in measCat:
376 plugin.fail(measRecord)
378 "Exception in %s.measureN on records %s-%s: %s"
379 % (plugin.name, measCat[0].getId(), measCat[-1].getId(), error))
Base config class for all measurement plugins.
def __init__
Constructor; only called by derived classes.
def callMeasure
Call the measure() method on all plugins, handling exceptions in a consistent way.
def doMeasurement
Call the measure() method on the nominated plugin, handling exceptions in a consistent way...
def doMeasurementN
Call the measureN() method on the nominated plugin, handling exceptions in a consistent way...
Ultimate base class for all measurement tasks.
Base config class for all measurement driver tasks.
def callMeasureN
Call the measureN() method on all plugins, handling exceptions in a consistent way.