23 """Base classes for single-frame measurement plugins and the driver task for these.
25 In single-frame measurement, we assume that detection and probably deblending have already been run on
26 the same frame, so a SourceCatalog has already been created with Footprints (which may be HeavyFootprints).
27 Measurements are generally recorded in the coordinate system of the image being measured (and all
28 slot-eligible fields must be), but non-slot fields may be recorded in other coordinate systems if necessary
29 to avoid information loss (this should, of course, be indicated in the field documentation).
34 from .pluginRegistry
import PluginRegistry, PluginMap
35 from .baseMeasurement
import (BaseMeasurementPluginConfig, BaseMeasurementPlugin,
36 BaseMeasurementConfig, BaseMeasurementTask)
37 from .noiseReplacer
import NoiseReplacer, DummyNoiseReplacer
39 __all__ = (
"SingleFramePluginConfig",
"SingleFramePlugin",
40 "SingleFrameMeasurementConfig",
"SingleFrameMeasurementTask")
45 Base class for configs of single-frame plugin algorithms.
52 Base class for single-frame plugin algorithms.
54 New Plugins can be created in Python by inheriting directly from this class
55 and implementing measure(), fail() (from BasePlugin), and optionally __init__
56 and measureN(). Plugins can also be defined in C++ via the WrappedSingleFramePlugin
61 registry = PluginRegistry(SingleFramePluginConfig)
62 ConfigClass = SingleFramePluginConfig
64 def __init__(self, config, name, schema, metadata):
66 Initialize the measurement object.
68 @param[in] config An instance of this class's ConfigClass.
69 @param[in] name The string the plugin was registered with.
70 @param[in,out] schema The Source schema. New fields should be added here to
71 hold measurements produced by this plugin.
72 @param[in] metadata Plugin metadata that will be attached to the output catalog
74 BaseMeasurementPlugin.__init__(self, config, name)
78 Measure the properties of a source on a single image (single-epoch image or coadd).
80 @param[in,out] measRecord lsst.afw.table.SourceRecord to be filled with outputs,
81 and from which previously-measured quantities can be
84 @param[in] exposure lsst.afw.image.ExposureF, containing the pixel data to
85 be measured and the associated Psf, Wcs, etc. All
86 other sources in the image will have been replaced by
87 noise according to deblender outputs.
90 raise NotImplementedError()
94 Measure the properties of a group of blended sources on a single image
95 (single-epoch image or coadd).
97 @param[in,out] measCat lsst.afw.table.SourceCatalog to be filled with outputs,
98 and from which previously-measured quantities can be
99 retrieved, containing only the sources that should be
100 measured together in this call.
102 @param[in] exposure lsst.afw.image.ExposureF, containing the pixel data to
103 be measured and the associated Psf, Wcs, etc. Sources
104 not in the blended hierarchy to be measured will have
105 been replaced with noise using deblender outputs.
107 Derived classes that do not implement measureN() should just inherit this
108 disabled version. Derived classes that do implement measureN() should additionally
109 add a bool doMeasureN config field to their config class to signal that measureN-mode
112 raise NotImplementedError()
117 Config class for single frame measurement driver task.
120 plugins = SingleFramePlugin.registry.makeField(
122 default=[
"base_PixelFlags",
124 "base_GaussianCentroid",
125 "base_NaiveCentroid",
129 "base_CircularApertureFlux",
133 doc=
"Plugins to be run and their configuration"
135 algorithms = property(
lambda self: self.
plugins, doc=
"backwards-compatibility alias for plugins")
136 undeblended = SingleFramePlugin.registry.makeField(
139 doc=
"Plugins to run on undeblended image"
152 \anchor SingleFrameMeasurementTask_
154 \brief A subtask for measuring the properties of sources on a single exposure.
156 The task is configured with a list of "plugins": each plugin defines the values it
157 measures (i.e. the columns in a table it will fill) and conducts that measurement
158 on each detected source (see SingleFramePlugin). The job of the
159 measurement task is to initialize the set of plugins (which includes setting up the
160 catalog schema) from their configuration, and then invoke each plugin on each
163 When run after the deblender (see lsst.meas.deblender.SourceDeblendTask),
164 SingleFrameMeasurementTask also replaces each source's neighbors with noise before
165 measuring each source, utilizing the HeavyFootprints created by the deblender (see
168 SingleFrameMeasurementTask has only two methods: __init__() and run(). For configuration
169 options, see SingleFrameMeasurementConfig.
171 @section meas_base_sfm_Example A complete example of using SingleFrameMeasurementTask
173 The code below is in examples/runSingleFrameTask.py
175 @dontinclude runSingleFrameTask.py
177 See meas_algorithms_detection_Example for more information on SourceDetectionTask.
179 First, import the required tasks (there are some other standard imports;
180 read the file if you're confused):
182 @skip SourceDetectionTask
183 @until SingleFrameMeasurementTask
185 We need to create our tasks before processing any data as the task constructors
186 can add extra columns to the schema. The most important argument we pass these to these
187 is an lsst.afw.table.Schema object, which contains information about the fields (i.e. columns) of the
188 measurement catalog we'll create, including names, types, and additional documentation.
189 Tasks that operate on a catalog are typically passed a Schema upon construction, to which
190 they add the fields they'll fill later when run. We construct a mostly empty Schema that
191 contains just the fields required for a SourceCatalog like this:
195 Now we can configure and create the SourceDetectionTask:
199 We then move on to configuring the measurement task:
203 While a reasonable set of plugins is configured by default, we'll customize the list.
204 We also need to unset one of the slots at the same time, because we're
205 not running the algorithm that it's set to by default, and that would cause problems later:
209 Now, finally, we can construct the measurement task:
211 @skipline measureTask
213 After constructing all the tasks, we can inspect the Schema we've created:
215 @skipline print schema
217 All of the fields in the
218 schema can be accessed via the get() method on a record object. See afwTable for more
221 We're now ready to process the data (we could loop over multiple exposures/catalogs using the same
222 task objects). First create the output table and process the image to find sources:
232 We then might plot the results (@em e.g. if you set `--ds9` on the command line)
237 and end up with something like
239 @image html runSingleFrameTask-ds9.png
242 ConfigClass = SingleFrameMeasurementConfig
244 def __init__(self, schema, algMetadata=None, **kwds):
246 Initialize the task. Set up the execution order of the plugins and initialize
247 the plugins, giving each plugin an opportunity to add its measurement fields to
248 the output schema and to record information in the task metadata.
250 @param[in,out] schema lsst.afw.table.Schema, to be initialized to include the
251 measurement fields from the plugins already
252 @param[in,out] algMetadata lsst.daf.base.PropertyList used to record information about
253 each algorithm. An empty PropertyList will be created if None.
254 @param[in] **kwds Keyword arguments forwarded to lsst.pipe.base.Task.__init__
256 super(SingleFrameMeasurementTask, self).
__init__(algMetadata=algMetadata, **kwds)
258 self.config.slots.setupSchema(self.
schema)
259 self.initializePlugins(schema=self.
schema)
262 if 'base_Blendedness' in self.plugins:
268 def run(self, measCat, exposure, noiseImage=None, exposureId=None, beginOrder=None, endOrder=None):
270 Run single frame measurement over an exposure and source catalog
272 @param[in,out] measCat lsst.afw.table.SourceCatalog to be filled with outputs. Must
273 contain all the SourceRecords to be measured (with Footprints
274 attached), and have a schema that is a superset of self.schema.
276 @param[in] exposure lsst.afw.image.ExposureF, containing the pixel data to
277 be measured and the associated Psf, Wcs, etc.
278 @param[in] noiseImage optional lsst.afw.image.ImageF for test which need to control
280 @param[in] exposureId optional unique exposureId used to calculate random number
281 generator seed in the NoiseReplacer.
282 @param[in] beginOrder beginning execution order (inclusive): measurements with
283 executionOrder < beginOrder are not executed. None for no limit.
284 @param[in] endOrder ending execution order (exclusive): measurements with
285 executionOrder >= endOrder are not executed. None for no limit.
287 assert measCat.getSchema().contains(self.
schema)
288 footprints = {measRecord.getId(): (measRecord.getParent(), measRecord.getFootprint())
289 for measRecord
in measCat}
295 if self.config.doReplaceWithNoise:
296 noiseReplacer = NoiseReplacer(self.config.noiseReplacer, exposure, footprints,
297 noiseImage=noiseImage, log=self.log, exposureId=exposureId)
298 algMetadata = measCat.getMetadata()
299 if algMetadata
is not None:
300 algMetadata.addInt(
"NOISE_SEED_MULTIPLIER", self.config.noiseReplacer.noiseSeedMultiplier)
301 algMetadata.addString(
"NOISE_SOURCE", self.config.noiseReplacer.noiseSource)
302 algMetadata.addDouble(
"NOISE_OFFSET", self.config.noiseReplacer.noiseOffset)
303 if exposureId
is not None:
304 algMetadata.addLong(
"NOISE_EXPOSURE_ID", exposureId)
306 noiseReplacer = DummyNoiseReplacer()
310 measParentCat = measCat.getChildren(0)
312 self.log.info(
"Measuring %d sources (%d parents, %d children) "
313 % (len(measCat), len(measParentCat), len(measCat) - len(measParentCat)))
315 for parentIdx, measParentRecord
in enumerate(measParentCat):
317 measChildCat = measCat.getChildren(measParentRecord.getId())
319 for measChildRecord
in measChildCat:
320 noiseReplacer.insertSource(measChildRecord.getId())
321 self.callMeasure(measChildRecord, exposure, beginOrder=beginOrder, endOrder=endOrder)
324 self.blendPlugin.cpp.measureChildPixels(exposure.getMaskedImage(), measChildRecord)
326 noiseReplacer.removeSource(measChildRecord.getId())
329 noiseReplacer.insertSource(measParentRecord.getId())
330 self.callMeasure(measParentRecord, exposure, beginOrder=beginOrder, endOrder=endOrder)
333 self.blendPlugin.cpp.measureChildPixels(exposure.getMaskedImage(), measParentRecord)
336 self.callMeasureN(measParentCat[parentIdx:parentIdx+1], exposure,
337 beginOrder=beginOrder, endOrder=endOrder)
338 self.callMeasureN(measChildCat, exposure, beginOrder=beginOrder, endOrder=endOrder)
339 noiseReplacer.removeSource(measParentRecord.getId())
345 for source
in measCat:
346 for plugin
in self.undeblendedPlugins.iter():
347 self.doMeasurement(plugin, source, exposure)
351 for source
in measCat:
352 self.blendPlugin.cpp.measureParentPixels(exposure.getMaskedImage(), source)
356 Backwards-compatibility alias for run()
358 self.
run(measCat, exposure)
def run
Run single frame measurement over an exposure and source catalog.
def __init__
Initialize the task.
A subtask for measuring the properties of sources on a single exposure.
Base class for configs of single-frame plugin algorithms.
Base class for single-frame plugin algorithms.
Config class for single frame measurement driver task.
def __init__
Initialize the measurement object.
def measureN
Measure the properties of a group of blended sources on a single image (single-epoch image or coadd)...
def measure
Backwards-compatibility alias for run()
def measure
Measure the properties of a source on a single image (single-epoch image or coadd).