24 Definitions and registration of pure-Python plugins with trivial implementations,
25 and automatic plugin-from-algorithm calls for those implemented in C++.
29 import lsst.pex.exceptions
30 import lsst.afw.detection
33 from .pluginRegistry
import register
34 from .pluginsBase
import BasePlugin
35 from .sfm
import SingleFramePluginConfig, SingleFramePlugin
36 from .forcedMeasurement
import ForcedPluginConfig, ForcedPlugin
37 from .wrappers
import wrapSimpleAlgorithm, wrapTransform
38 from .transforms
import SimpleCentroidTransform
40 from .apertureFlux
import ApertureFluxControl, ApertureFluxTransform
41 from .transform
import BaseTransform
42 from .blendedness
import BlendednessAlgorithm, BlendednessControl
43 from .circularApertureFlux
import CircularApertureFluxAlgorithm
44 from .gaussianCentroid
import GaussianCentroidAlgorithm, GaussianCentroidControl, GaussianCentroidTransform
45 from .gaussianFlux
import GaussianFluxAlgorithm, GaussianFluxControl, GaussianFluxTransform
46 from .exceptions
import MeasurementError
47 from .naiveCentroid
import NaiveCentroidAlgorithm, NaiveCentroidControl, NaiveCentroidTransform
48 from .peakLikelihoodFlux
import PeakLikelihoodFluxAlgorithm, PeakLikelihoodFluxControl, PeakLikelihoodFluxTransform
49 from .pixelFlags
import PixelFlagsAlgorithm, PixelFlagsControl
50 from .psfFlux
import PsfFluxAlgorithm, PsfFluxControl, PsfFluxTransform
51 from .scaledApertureFlux
import ScaledApertureFluxAlgorithm, ScaledApertureFluxControl, ScaledApertureFluxTransform
52 from .sdssCentroid
import SdssCentroidAlgorithm, SdssCentroidControl, SdssCentroidTransform
53 from .sdssShape
import SdssShapeAlgorithm, SdssShapeControl, SdssShapeTransform
56 "SingleFrameFPPositionConfig",
"SingleFrameFPPositionPlugin",
57 "SingleFrameJacobianConfig",
"SingleFrameJacobianPlugin",
58 "SingleFrameVarianceConfig",
"SingleFrameVariancePlugin",
59 "SingleFrameInputCountConfig",
"SingleFrameInputCountPlugin",
60 "SingleFramePeakCentroidConfig",
"SingleFramePeakCentroidPlugin",
61 "SingleFrameSkyCoordConfig",
"SingleFrameSkyCoordPlugin",
62 "ForcedPeakCentroidConfig",
"ForcedPeakCentroidPlugin",
63 "ForcedTransformedCentroidConfig",
"ForcedTransformedCentroidPlugin",
64 "ForcedTransformedShapeConfig",
"ForcedTransformedShapePlugin",
70 TransformClass=PsfFluxTransform, executionOrder=BasePlugin.FLUX_ORDER,
71 shouldApCorr=
True, hasLogName=
True)
73 TransformClass=PeakLikelihoodFluxTransform, executionOrder=BasePlugin.FLUX_ORDER)
75 TransformClass=GaussianFluxTransform, executionOrder=BasePlugin.FLUX_ORDER,
78 TransformClass=GaussianCentroidTransform, executionOrder=BasePlugin.CENTROID_ORDER)
80 TransformClass=NaiveCentroidTransform, executionOrder=BasePlugin.CENTROID_ORDER)
82 TransformClass=SdssCentroidTransform, executionOrder=BasePlugin.CENTROID_ORDER)
84 executionOrder=BasePlugin.FLUX_ORDER)
86 TransformClass=SdssShapeTransform, executionOrder=BasePlugin.SHAPE_ORDER)
88 TransformClass=ScaledApertureFluxTransform, executionOrder=BasePlugin.FLUX_ORDER)
90 wrapSimpleAlgorithm(CircularApertureFluxAlgorithm, needsMetadata=
True, Control=ApertureFluxControl,
91 TransformClass=ApertureFluxTransform, executionOrder=BasePlugin.FLUX_ORDER)
93 TransformClass=BaseTransform, executionOrder=BasePlugin.SHAPE_ORDER)
115 Algorithm to calculate the position of a centroid on the focal plane
118 ConfigClass = SingleFrameFPPositionConfig
122 return cls.SHAPE_ORDER
124 def __init__(self, config, name, schema, metadata):
125 SingleFramePlugin.__init__(self, config, name, schema, metadata)
126 self.
focalValue = lsst.afw.table.Point2DKey.addFields(schema, name,
"Position on the focal plane",
128 self.
focalFlag = schema.addField(name +
"_flag", type=
"Flag", doc=
"Set to True for any fatal failure")
129 self.
detectorFlag = schema.addField(name +
"_missingDetector_flag", type=
"Flag",
130 doc=
"Set to True if detector object is missing")
133 det = exposure.getDetector()
136 fp = lsst.afw.geom.Point2D(numpy.nan, numpy.nan)
138 center = measRecord.getCentroid()
139 posInPix = det.makeCameraPoint(center, lsst.afw.cameraGeom.PIXELS)
140 fp = det.transform(posInPix, lsst.afw.cameraGeom.FOCAL_PLANE).getPoint()
143 def fail(self, measRecord, error=None):
148 pixelScale = lsst.pex.config.Field(dtype=float, default=0.5, doc=
"Nominal pixel size (arcsec)")
154 Algorithm which computes the Jacobian about a source and computes its ratio with a nominal pixel area.
155 This allows one to compare relative instead of absolute areas of pixels.
158 ConfigClass = SingleFrameJacobianConfig
162 return cls.SHAPE_ORDER
164 def __init__(self, config, name, schema, metadata):
165 SingleFramePlugin.__init__(self, config, name, schema, metadata)
166 self.
jacValue = schema.addField(name +
'_value', type=
"D", doc=
"Jacobian correction")
167 self.
jacFlag = schema.addField(name +
'_flag', type=
"Flag", doc=
"Set to 1 for any fatal failure")
169 self.
scale = pow(self.config.pixelScale, -2)
172 center = measRecord.getCentroid()
175 result = numpy.abs(self.
scale*exposure.getWcs().linearizePixelToSky(
177 lsst.afw.geom.arcseconds).getLinear().computeDeterminant())
178 measRecord.set(self.
jacValue, result)
180 def fail(self, measRecord, error=None):
181 measRecord.set(self.
jacFlag,
True)
185 scale = lsst.pex.config.Field(dtype=float, default=5.0, optional=
True,
186 doc=
"Scale factor to apply to shape for aperture")
187 mask = lsst.pex.config.ListField(doc=
"Mask planes to ignore", dtype=str,
188 default=[
"DETECTED",
"DETECTED_NEGATIVE",
"BAD",
"SAT"])
194 Calculate the median variance within a Footprint scaled from the object shape so
195 the value is not terribly influenced by the object and instead represents the
196 variance in the background near the object.
198 ConfigClass = SingleFrameVarianceConfig
199 FAILURE_BAD_CENTROID = 1
200 FAILURE_EMPTY_FOOTPRINT = 2
204 return cls.FLUX_ORDER
206 def __init__(self, config, name, schema, metadata):
207 SingleFramePlugin.__init__(self, config, name, schema, metadata)
208 self.
varValue = schema.addField(name +
'_value', type=
"D", doc=
"Variance at object position")
209 self.
varFlag = schema.addField(name +
'_flag', type=
"Flag", doc=
"Set to True for any fatal failure")
211 doc=
"Set to True when the footprint has no usable pixels")
215 schema.getAliasMap().set(name +
'_flag_badCentroid', schema.getAliasMap().apply(
"slot_Centroid_flag"))
218 if measRecord.getCentroidFlag():
222 aperture = lsst.afw.geom.ellipses.Ellipse(measRecord.getShape(), measRecord.getCentroid())
223 aperture.scale(self.config.scale)
224 ellipse = lsst.afw.geom.SpanSet.fromShape(aperture)
225 foot = lsst.afw.detection.Footprint(ellipse)
226 foot.clipTo(exposure.getBBox(lsst.afw.image.PARENT))
229 maskedImage = exposure.getMaskedImage()
230 pixels = lsst.afw.detection.makeHeavyFootprint(foot, maskedImage)
231 maskBits = maskedImage.getMask().getPlaneBitMask(self.config.mask)
232 logicalMask = numpy.logical_not(pixels.getMaskArray() & maskBits)
236 if numpy.any(logicalMask):
237 medVar = numpy.median(pixels.getVarianceArray()[logicalMask])
238 measRecord.set(self.
varValue, medVar)
240 raise MeasurementError(
"Footprint empty, or all pixels are masked, can't compute median",
243 def fail(self, measRecord, error=None):
245 if isinstance(error, MeasurementError):
250 measRecord.set(self.
varValue, numpy.nan)
251 measRecord.set(self.
varFlag,
True)
261 Plugin to count how many input images contributed to each source. This information
262 is in the exposure's coaddInputs. Some limitations:
263 * This is only for the pixel containing the center, not for all the pixels in the
265 * This does not account for any clipping in the coadd
268 ConfigClass = SingleFrameInputCountConfig
269 FAILURE_BAD_CENTROID = 1
270 FAILURE_NO_INPUTS = 2
274 return cls.SHAPE_ORDER
276 def __init__(self, config, name, schema, metadata):
277 SingleFramePlugin.__init__(self, config, name, schema, metadata)
278 self.
numberKey = schema.addField(name +
'_value', type=
"I",
279 doc=
"Number of images contributing at center, not including any"
281 self.
numberFlag = schema.addField(name +
'_flag', type=
"Flag", doc=
"Set to True for fatal failure")
282 self.
noInputsFlag = schema.addField(name +
'_flag_noInputs', type=
"Flag",
283 doc=
"No coadd inputs available")
286 schema.getAliasMap().set(name +
'_flag_badCentroid', schema.getAliasMap().apply(
"slot_Centroid_flag"))
289 if not exposure.getInfo().getCoaddInputs():
291 if measRecord.getCentroidFlag():
294 center = measRecord.getCentroid()
295 ccds = exposure.getInfo().getCoaddInputs().ccds
296 measRecord.set(self.
numberKey, len(ccds.subsetContaining(center, exposure.getWcs())))
298 def fail(self, measRecord, error=None):
300 if error
is not None:
314 A centroid algorithm that simply uses the first (i.e. highest) Peak in the Source's
315 Footprint as the centroid. This is of course a relatively poor measure of the true
316 centroid of the object; this algorithm is provided mostly for testing and debugging.
319 ConfigClass = SingleFramePeakCentroidConfig
323 return cls.CENTROID_ORDER
325 def __init__(self, config, name, schema, metadata):
326 SingleFramePlugin.__init__(self, config, name, schema, metadata)
327 self.
keyX = schema.addField(name +
"_x", type=
"D", doc=
"peak centroid", units=
"pixel")
328 self.
keyY = schema.addField(name +
"_y", type=
"D", doc=
"peak centroid", units=
"pixel")
329 self.
flag = schema.addField(name +
"_flag", type=
"Flag", doc=
"Centroiding failed")
332 peak = measRecord.getFootprint().getPeaks()[0]
333 measRecord.set(self.
keyX, peak.getFx())
334 measRecord.set(self.
keyY, peak.getFy())
336 def fail(self, measRecord, error=None):
337 measRecord.set(self.
flag,
True)
341 return SimpleCentroidTransform
351 A measurement plugin that sets the "coord" field (part of the Source minimal schema)
352 using the slot centroid and the Wcs attached to the Exposure.
355 ConfigClass = SingleFrameSkyCoordConfig
359 return cls.SHAPE_ORDER
364 if not exposure.hasWcs():
365 raise Exception(
"Wcs not attached to exposure. Required for " + self.name +
" algorithm")
366 measRecord.updateCoord(exposure.getWcs())
368 def fail(self, measRecord, error=None):
377 class ForcedPeakCentroidConfig(ForcedPluginConfig):
384 The forced peak centroid is like the SFM peak centroid plugin, except that it must transform
385 the peak coordinate from the original (reference) coordinate system to the coordinate system
386 of the exposure being measured.
389 ConfigClass = ForcedPeakCentroidConfig
393 return cls.CENTROID_ORDER
395 def __init__(self, config, name, schemaMapper, metadata):
396 ForcedPlugin.__init__(self, config, name, schemaMapper, metadata)
397 schema = schemaMapper.editOutputSchema()
398 self.
keyX = schema.addField(name +
"_x", type=
"D", doc=
"peak centroid", units=
"pixel")
399 self.
keyY = schema.addField(name +
"_y", type=
"D", doc=
"peak centroid", units=
"pixel")
401 def measure(self, measRecord, exposure, refRecord, refWcs):
402 targetWcs = exposure.getWcs()
403 peak = refRecord.getFootprint().getPeaks()[0]
404 result = lsst.afw.geom.Point2D(peak.getFx(), peak.getFy())
405 if not refWcs == targetWcs:
406 result = targetWcs.skyToPixel(refWcs.pixelToSky(result))
407 measRecord.set(self.
keyX, result.getX())
408 measRecord.set(self.
keyY, result.getY())
412 return SimpleCentroidTransform
419 @
register(
"base_TransformedCentroid")
421 """A centroid pseudo-algorithm for forced measurement that simply transforms the centroid
422 from the reference catalog to the measurement coordinate system. This is used as
423 the slot centroid by default in forced measurement, allowing subsequent measurements
424 to simply refer to the slot value just as they would in single-frame measurement.
427 ConfigClass = ForcedTransformedCentroidConfig
431 return cls.CENTROID_ORDER
433 def __init__(self, config, name, schemaMapper, metadata):
434 ForcedPlugin.__init__(self, config, name, schemaMapper, metadata)
435 schema = schemaMapper.editOutputSchema()
437 xKey = schema.addField(name +
"_x", type=
"D", doc=
"transformed reference centroid column",
439 yKey = schema.addField(name +
"_y", type=
"D", doc=
"transformed reference centroid row",
445 if "slot_Centroid_flag" in schemaMapper.getInputSchema():
446 self.
flagKey = schema.addField(name +
"_flag", type=
"Flag",
447 doc=
"whether the reference centroid is marked as bad")
451 def measure(self, measRecord, exposure, refRecord, refWcs):
452 targetWcs = exposure.getWcs()
453 if not refWcs == targetWcs:
454 targetPos = targetWcs.skyToPixel(refWcs.pixelToSky(refRecord.getCentroid()))
457 measRecord.set(self.
centroidKey, refRecord.getCentroid())
459 measRecord.set(self.
flagKey, refRecord.getCentroidFlag())
468 """A shape pseudo-algorithm for forced measurement that simply transforms the shape
469 from the reference catalog to the measurement coordinate system. This is used as
470 the slot shape by default in forced measurement, allowing subsequent measurements
471 to simply refer to the slot value just as they would in single-frame measurement.
474 ConfigClass = ForcedTransformedShapeConfig
478 return cls.SHAPE_ORDER
480 def __init__(self, config, name, schemaMapper, metadata):
481 ForcedPlugin.__init__(self, config, name, schemaMapper, metadata)
482 schema = schemaMapper.editOutputSchema()
484 xxKey = schema.addField(name +
"_xx", type=
"D", doc=
"transformed reference shape x^2 moment",
486 yyKey = schema.addField(name +
"_yy", type=
"D", doc=
"transformed reference shape y^2 moment",
488 xyKey = schema.addField(name +
"_xy", type=
"D", doc=
"transformed reference shape xy moment",
490 self.
shapeKey = lsst.afw.table.QuadrupoleKey(xxKey, yyKey, xyKey)
494 if "slot_Shape_flag" in schemaMapper.getInputSchema():
495 self.
flagKey = schema.addField(name +
"_flag", type=
"Flag",
496 doc=
"whether the reference shape is marked as bad")
500 def measure(self, measRecord, exposure, refRecord, refWcs):
501 targetWcs = exposure.getWcs()
502 if not refWcs == targetWcs:
503 fullTransform = lsst.afw.image.XYTransformFromWcsPair(targetWcs, refWcs)
504 localTransform = fullTransform.linearizeForwardTransform(refRecord.getCentroid())
505 measRecord.set(self.
shapeKey, refRecord.getShape().transform(localTransform.getLinear()))
507 measRecord.set(self.
shapeKey, refRecord.getShape())
509 measRecord.set(self.
flagKey, refRecord.getShapeFlag())
int FAILURE_EMPTY_FOOTPRINT
def register
A Python decorator that registers a class, using the given name, in its base class's PluginRegistry...
def wrapSimpleAlgorithm
Wrap a C++ SimpleAlgorithm class into both a Python SingleFramePlugin and ForcedPlugin classes...