Coverage for python/lsst/meas/base/wrappers.py : 59%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"GenericPlugin")
SingleFramePlugin.__init__(self, config, name, schema, metadata, logName=logName) if hasattr(self, "hasLogName") and self.hasLogName and logName is not None: self.cpp = self.factory(config, name, schema, metadata, logName=logName) else: self.cpp = self.factory(config, name, schema, metadata)
self.cpp.measure(measRecord, exposure)
self.cpp.measureN(measCat, exposure)
self.cpp.fail(measRecord, error.cpp if error is not None else None)
ForcedPlugin.__init__(self, config, name, schemaMapper, metadata, logName=logName) if hasattr(self, "hasLogName") and self.hasLogName and logName is not None: self.cpp = self.factory(config, name, schemaMapper, metadata, logName=logName) else: self.cpp = self.factory(config, name, schemaMapper, metadata)
self.cpp.measureForced(measRecord, exposure, refRecord, refWcs)
self.cpp.measureNForced(measCat, exposure, refCat, refWcs)
self.cpp.fail(measRecord, error.cpp if error is not None else None)
"""! Wrap a C++ algorithm's control class into a Python Config class.
@param[in] Base Base class for the returned ConfigClass; one of SingleFramePluginConfig or ForcedPluginConfig @param[in] Control Control class to be wrapped (a Swigged C++ class) @param[in] module Either a module object, a string specifying the name of the module, or an integer specifying how far back in the stack to look for the module to use: 0 is pex.config.wrap, 1 is lsst.meas.base.wrappers, 2 is the immediate caller, etc. This will be used to set __module__ for the new config class, and the class will also be added to the module. The default is to use the callers' module. @param[in] hasMeasureN Whether the plugin supports fitting multiple objects at once (if so, a config option to enable/disable this will be added).
@return a new subclass of lsst.pex.config.Config
This function is generally only called by wrapAlgorithm; it is unlikely users will have to call it directly. """ # We need to add a Config field to enable multi-object measurement, to replace # the simple bool class attribute that's on the base class. To do that, we # create the Config class dynamically here, then call makeControlClass to finish # it off by adding fields from the control object. cls = type( Control.__name__.replace("Control", "Config"), (Base,), {"doMeasureN": lsst.pex.config.Field(dtype=bool, default=True, doc="whether to run this plugin in multi-object mode")} ) ConfigClass = lsst.pex.config.makeConfigClass(Control, module=module, cls=cls) else: # If we don't have to add that Config field, we can delegate all of the work to # pex_config's makeControlClass
ConfigClass=None, TransformClass=None, doRegister=True, shouldApCorr=False, apCorrList=(), hasLogName=False, **kwds): """! Wrap a C++ Algorithm class into a Python Plugin class.
@param[in] Base Base class for the returned Plugin; one of SingleFramePlugin or ForcedPlugin @param[in] AlgClass Swigged C++ Algorithm class to convert; must be a subclass of SingleFrameAlgorithm or ForcedAlgorithm (matching the Base argument), or an unrelated class with the same measure() and measureN() signatures as those base classes. @param[in] factory A callable that is used to construct an instance of AlgClass. It must take four arguments, either (config, name, schema, metadata) or (config, name, schemaMapper, metadata), depending on whether the algorithm is single-frame or forced. @param[in] executionOrder The order this plugin should be run, relative to others (see BasePlugin.getExecutionOrder()). @param[in] name String to use when registering the algorithm. Ignored if doRegistry=False, set to generateAlgorithmName(AlgClass) if None. @param[in] Control Swigged C++ Control class for the algorithm; AlgClass.Control is used if None. Ignored if ConfigClass is not None. @param[in] ConfigClass Python Config class that wraps the C++ Algorithm's swigged Control class. If None, wrapAlgorithmControl is called to generate a Config class using the Control argument. @param[in] TransformClass Transformation which may be used to post-process the results of measurement. If None, the default (defined by BasePlugin) is used. @param[in] doRegister If True (the default), register the plugin with Base's registry, allowing it to be used by measurement Tasks. @param[in] shouldApCorr Does this algorithm measure a instFlux that can be aperture corrected? This is shorthand for apCorrList=[name] and is ignored if apCorrList is specified. @param[in] apCorrList List of field name prefixes for instFlux fields that to be aperture corrected. If an algorithm produces a single instFlux that should be aperture corrected then it is simpler to set shouldApCorr=True. But if an algorithm produces multiple such fields then it must specify apCorrList, instead. For example modelfit_CModel produces 3 such fields: apCorrList= ("modelfit_CModel_exp", "modelfit_CModel_exp", "modelfit_CModel_def") If apCorrList is non-empty then shouldApCorr is ignored. If non-empty and doRegister is True then the names are added to the set retrieved by getApCorrNameSet @param[in] hasLogName Plugin supports a logName as a constructor argument
@param[in] **kwds Additional keyword arguments passed to generateAlgorithmControl, including: - hasMeasureN: Whether the plugin supports fitting multiple objects at once (if so, a config option to enable/disable this will be added). - executionOrder: If not None, an override for the default executionOrder for this plugin (the default is 2.0, which is usually appropriate for fluxes).
@return the new Plugin class, a subclass of Base
This function is generally only called by the public wrapSingleFrameAlgorithm, wrapForcedAlgorithm, and wrapSimpleAlgorithm functions; it is unlikely users will have to call it directly. """ Control = AlgClass.Control
return executionOrder getExecutionOrder=staticmethod(getExecutionOrder))
hasLogName=False, **kwds): """! Wrap a C++ SingleFrameAlgorithm class into a Python SingleFramePlugin class.
@param[in] AlgClass Swigged C++ Algorithm class to convert; must be a subclass of SingleFrameAlgorithm, or an unrelated class with the same measure(), measureN(), and fail() signatures. @param[in] executionOrder The order this plugin should be run, relative to others (see BasePlugin.getExecutionOrder()). @param[in] name String to use when registering the algorithm. Ignored if doRegistry=False, set to generateAlgorithmName(AlgClass) if None. @param[in] needsMetadata Sets whether the AlgClass's constructor should be passed a PropertySet metadata argument. @param[in] hasMeasureN Whether the algorithm supports simultaneous measurement of multiple sources. If True, a bool doMeasureN field will be added to the generated Config class, and its value will be passed as the last argument when calling the AlgClass constructor. @param[in] hasLogName Plugin supports a logName as a constructor argument @param[in] **kwds Additional keyword arguments passed to the lower-level wrapAlgorithm and wrapAlgorithmControl classes. These include: - Control: Swigged C++ Control class for the algorithm; AlgClass.Control is used if None. Ignored if ConfigClass is not None. - ConfigClass: Python Config class that wraps the C++ Algorithm's swigged Control class. If None, wrapAlgorithmControl is called to generate a Config class using the Control argument. - doRegister: If True (the default), register the plugin with SingleFramePlugin.registry, allowing it to be used by SingleFrameMeasurementTask. - shouldApCorr: does this algorithm measure a instFlux that can be aperture corrected? This is shorthand for apCorrList=[name] and is ignored if apCorrList is specified. - apCorrList: list of field name prefixes for instFlux fields that should be aperture corrected. If an algorithm produces a single instFlux that should be aperture corrected then it is simpler to set shouldApCorr=True. But if an algorithm produces multiple such fields then it must specify apCorrList, instead. For example modelfit_CModel produces 3 such fields: apCorrList= ("modelfit_CModel_exp", "modelfit_CModel_exp", "modelfit_CModel_def") If apCorrList is non-empty then shouldApCorr is ignored. If non-empty and doRegister is True then the names are added to the set retrieved by getApCorrNameSet - executionOrder: If not None, an override for the default executionOrder for this plugin (the default is 2.0, which is usually appropriate for fluxes).
@return the new SingleFramePlugin subclass
The needsMetadata and hasMeasureN arguments combine to determine the expected constructor signature; we always expect the first three arguments to be: @verbatim Control const & ctrl, std::string const & name, Schema & schema @endverbatim If needsMetadata, we also append: @verbatim PropertySet & metadata @endverbatim If hasMeasureN, we also append: @verbatim bool doMeasureN @endverbatim If hasLogName, we also append: @verbatim std::string logName @endverbatim If more than one is True, the metadata PropertySet precedes the doMeasureN bool and the logName comes last of the three """ if needsMetadata: def factory(config, name, schema, metadata, **kwargs): return AlgClass(config.makeControl(), name, schema, metadata, config.doMeasureN, **kwargs) else: def factory(config, name, schema, metadata, **kwargs): return AlgClass(config.makeControl(), name, schema, config.doMeasureN, **kwargs) else: return AlgClass(config.makeControl(), name, schema, metadata, **kwargs) else: return AlgClass(config.makeControl(), name, schema, **kwargs)
factory=factory, hasMeasureN=hasMeasureN, hasLogName=hasLogName, **kwds)
hasMeasureN=False, needsSchemaOnly=False, hasLogName=False, **kwds): """! Wrap a C++ ForcedAlgorithm class into a Python ForcedPlugin class.
@param[in] AlgClass Swigged C++ Algorithm class to convert; must be a subclass of ForcedAlgorithm, or an unrelated class with the same measure(), measureN(), and fail() signatures. @param[in] executionOrder The order this plugin should be run, relative to others (see BasePlugin.getExecutionOrder()). @param[in] name String to use when registering the algorithm. Ignored if doRegistry=False, set to generateAlgorithmName(AlgClass) if None. @param[in] needsMetadata Sets whether the AlgClass's constructor should be passed a PropertySet metadata argument. @param[in] hasMeasureN Whether the algorithm supports simultaneous measurement of multiple sources. If True, a bool doMeasureN field will be added to the generated Config class, and its value will be passed as the last argument when calling the AlgClass constructor. @param[in] hasLogName Plugin supports a logName as a constructor argument @param[in] needsSchemaOnly Whether the algorithm constructor expects a Schema argument (representing the output Schema) rather than the full SchemaMapper (which provides access to both the reference Schema and the output Schema). @param[in] **kwds Additional keyword arguments passed to the lower-level wrapAlgorithm and wrapAlgorithmControl classes. These include: - Control: Swigged C++ Control class for the algorithm; AlgClass.Control is used if None. Ignored if ConfigClass is not None. - ConfigClass: Python Config class that wraps the C++ Algorithm's swigged Control class. If None, wrapAlgorithmControl is called to generate a Config class using the Control argument. - doRegister: If True (the default), register the plugin with ForcedPlugin.registry, allowing it to be used by ForcedMeasurementTask. - shouldApCorr: does this algorithm measure a instFlux that can be aperture corrected? This is shorthand for apCorrList=[name] and is ignored if apCorrList is specified. - apCorrList: list of field name prefixes for instFlux fields that should be aperture corrected. If an algorithm produces a single instFlux that should be aperture corrected then it is simpler to set shouldApCorr=True. But if an algorithm produces multiple such fields then it must specify apCorrList, instead. For example modelfit_CModel produces 3 such fields: apCorrList= ("modelfit_CModel_exp", "modelfit_CModel_exp", "modelfit_CModel_def") If apCorrList is non-empty then shouldApCorr is ignored. If non-empty and doRegister is True then the names are added to the set retrieved by getApCorrNameSet - executionOrder: If not None, an override for the default executionOrder for this plugin (the default is 2.0, which is usually appropriate for fluxes).
@return the new ForcedPlugin subclass
The needsMetadata, hasMeasureN, and needsSchemaOnly arguments combine to determine the expected constructor signature; we always expect the first two arguments to be: @verbatim Control const & ctrl, std::string const & name @endverbatim If needsSchemaOnly is True, then the third argument will be @verbatim Schema & schema @endverbatim otherwise, it will be: @verbatim SchemaMapper & schemaMapper @endverbatim If needsMetadata, we also append: @verbatim PropertySet & metadata @endverbatim If hasMeasureN, we also append: @verbatim bool doMeasureN @endverbatim If hasLogName, we also append: @verbatim std::string logName @endverbatim If more than one is True, the metadata PropertySet precedes the doMeasureN bool and the logName comes last of the three """ return m.editOutputSchema() else: def extractSchemaArg(m): return m if needsMetadata: def factory(config, name, schemaMapper, metadata, **kwargs): return AlgClass(config.makeControl(), name, extractSchemaArg(schemaMapper), metadata, config.doMeasureN, **kwargs) else: def factory(config, name, schemaMapper, metadata, **kwargs): return AlgClass(config.makeControl(), name, extractSchemaArg(schemaMapper), config.doMeasureN, **kwargs) else: return AlgClass(config.makeControl(), name, extractSchemaArg(schemaMapper), metadata, **kwargs) else: return AlgClass(config.makeControl(), name, extractSchemaArg(schemaMapper), **kwargs)
factory=factory, hasLogName=hasLogName, **kwds)
hasLogName=False, **kwds): """! Wrap a C++ SimpleAlgorithm class into both a Python SingleFramePlugin and ForcedPlugin classes
@param[in] AlgClass Swigged C++ Algorithm class to convert; must be a subclass of simpleAlgorithm, or an unrelated class with the same measure(), measureN(), and fail() signatures. @param[in] executionOrder The order this plugin should be run, relative to others (see BasePlugin.getExecutionOrder()). @param[in] name String to use when registering the algorithm. Ignored if doRegistry=False, set to generateAlgorithmName(AlgClass) if None. @param[in] needsMetadata Sets whether the AlgClass's constructor should be passed a PropertySet metadata argument. @param[in] hasMeasureN Whether the algorithm supports simultaneous measurement of multiple sources. If True, a bool doMeasureN field will be added to the generated Config class, and its value will be passed as the last argument when calling the AlgClass constructor. @param[in] hasLogName Plugin supports a logName as a constructor argument @param[in] **kwds Additional keyword arguments passed to the lower-level wrapAlgorithm and wrapAlgorithmControl classes. These include: - Control: Swigged C++ Control class for the algorithm; AlgClass.Control is used if None. Ignored if ConfigClass is not None. - ConfigClass: Python Config class that wraps the C++ Algorithm's swigged Control class. If None, wrapAlgorithmControl is called to generate a Config class using the Control argument. - doRegister: If True (the default), register the plugins with Base's registry, allowing it to be used by measurement Tasks. - shouldApCorr: does this algorithm measure a instFlux that can be aperture corrected? This is shorthand for apCorrList=[name] and is ignored if apCorrList is specified. - apCorrList: list of field name prefixes for instFlux fields that should be aperture corrected. If an algorithm produces a single instFlux that should be aperture corrected then it is simpler to set shouldApCorr=True. But if an algorithm produces multiple such fields then it must specify apCorrList, instead. For example modelfit_CModel produces 3 such fields: apCorrList= ("modelfit_CModel_exp", "modelfit_CModel_exp", "modelfit_CModel_def") If apCorrList is non-empty then shouldApCorr is ignored. If non-empty and doRegister is True then the names are added to the set retrieved by getApCorrNameSet - executionOrder: If not None, an override for the default executionOrder for this plugin (the default is 2.0, which is usually appropriate for fluxes).
@return a two-element tuple, containing the new SingleFramePlugin and ForcedPlugin subclasses
The needsMetadata and hasMeasureN arguments combine to determine the expected constructor signature; we always expect the first three arguments to be: @verbatim Control const & ctrl, std::string const & name, Schema & schema @endverbatim If needsMetadata, we also append: @verbatim PropertySet & metadata @endverbatim If hasMeasureN, we also append: @verbatim bool doMeasureN @endverbatim If hasLogName, we also append: @verbatim std::string logName @endverbatim If more than one is True, the metadata PropertySet precedes the doMeasureN bool and the logName comes last of the three """ needsMetadata=needsMetadata, hasLogName=hasLogName, **kwds), wrapForcedAlgorithm(AlgClass, executionOrder=executionOrder, name=name, needsMetadata=needsMetadata, hasLogName=hasLogName, needsSchemaOnly=True, **kwds))
"""Modify a C++ Transform class so that it can be configured with either a Config or a Control.
Parameters ---------- transformClass: class A Transform class. Its constructor must take a Control, a string, and a SchemaMapper, in that order. """
if hasattr(ctrl, "makeControl"): ctrl = ctrl.makeControl() # logName signature needs to be on this Class __init__, but is not needed by the C++ plugin oldInit(self, ctrl, name, mapper)
"""Abstract base class for a generic plugin
A generic plugin can be used with the `singleFramePluginFromGeneric` and/or `forcedPluginFromGeneric` wrappers to create classes that can be used for single frame measurement and/or forced measurement (as appropriate). The only real difference between `SingleFramePlugin` and `ForcedPlugin` is the `measure` method; this class introduces a shared signature for the `measure` method that, in combination with the afore-mentioned wrappers, allows both plugin styles to share a single implementation.
This doesn't use abc.ABCMeta because I couldn't get it to work with a superclass.
Sub-classes should set `ConfigClass` and implement the `measure` and `measureN` methods. They may optionally provide alternative implementations for the `__init__`, `fail` and `getExecutionOrder` methods.
Constructor parameters ---------------------- config : `lsst.pex.config.Config` An instance of this class' ConfigClass. name : `str` Name of this measurement plguin, for registering. schema : `lsst.afw.table.Schema` The catalog schema. New fields should be added here to hold measurements produced by this plugin. metadata : `lsst.daf.base.PropertySet` Metadata that will be attached to the output catalog. logName : `str` Name of log component. """
def getExecutionOrder(cls): return 0
"""Constructor
This default implementation simply adds a field for recording a fatal failure of the measurement plugin. """ BasePlugin.__init__(self, config, name, logName=logName) self._failKey = schema.addField(name + '_flag', type="Flag", doc="Set for any fatal failure")
"""Measure source
It is the responsibility of this method to perform the desired measurement and record the result in the `measRecord`.
Parameters ---------- measRecord : `lsst.afw.table.SourceRecord` Catalog record for the source being measured. exposure : `lsst.afw.image.Exposure` Exposure on which the source is being measured. center : `lsst.geom.Point2D` Pixel coordinates of the object.
Raises ------ `MeasurementError` If the measurement fails for a known/justifiable reason. """ raise NotImplementedError()
"""Measure multiple sources
It is the responsibility of this method to perform the desired measurement and record the result in the `measCat`.
Parameters ---------- measCat : `lsst.afw.table.SourceCatalog` Catalog for the sources being measured. exposure : `lsst.afw.image.Exposure` Exposure on which the source is being measured. refCat : `lsst.afw.table.SourceCatalog` Reference catalog. refWcs : `lsst.afw.image.Wcs` Astrometric solution for the reference image.
Raises ------ `MeasurementError` If the measurement fails for a known/justifiable reason. """ raise NotImplementedError()
"""Record failure
This default implementation simply records the failure in the source record.
Parameters ---------- measRecord : `lsst.afw.table.SourceRecord` Catalog record for the source being measured. error : `Exception` Error causing failure, or `None`. """ measRecord.set(self._failKey, True)
def makeSingleFramePlugin(cls, name): """Produce a SingleFramePlugin sub-class from this GenericPlugin class
The class is also registered.
Parameters ---------- name : `str` Name of plugin to register. """
SingleFramePlugin.__init__(self, config, name, schema, metadata, logName=logName) self._generic = cls(config, name, schema, metadata)
center = measRecord.getCentroid() return self._generic.measure(measRecord, exposure, center)
return self._generic.measureN(measCat, exposure, refCat, refWcs)
self._generic.fail(measRecord, error if error is not None else None)
def getExecutionOrder(): return cls.getExecutionOrder()
return self._generic.getTransformClass()
def makeForcedPlugin(cls, name): """Produce a ForcedPlugin sub-class from this GenericPlugin class
The class is also registered.
Parameters ---------- name : `str` Name of plugin to register. """
ForcedPlugin.__init__(self, config, name, schemaMapper, metadata, logName=logName) schema = schemaMapper.editOutputSchema() self._generic = cls(config, name, schema, metadata)
center = exposure.getWcs().skyToPixel(refWcs.pixelToSky(refRecord.getCentroid())) return self._generic.measure(measRecord, exposure, center)
return self._generic.measureN(measCat, exposure, refCat, refWcs)
self._generic.fail(measRecord, error if error is not None else None)
def getExecutionOrder(): return cls.getExecutionOrder()
return self._generic.getTransformClass()
|