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).
38 __all__ = (
"SingleFramePluginConfig",
"SingleFramePlugin",
39 "SingleFrameMeasurementConfig",
"SingleFrameMeasurementTask")
43 Base class for configs of single-frame plugin algorithms.
49 Base class for single-frame plugin algorithms.
51 New Plugins can be created in Python by inheriting directly from this class
52 and implementing measure(), fail() (from BasePlugin), and optionally __init__
53 and measureN(). Plugins can also be defined in C++ via the WrappedSingleFramePlugin
58 registry = PluginRegistry(SingleFramePluginConfig)
59 ConfigClass = SingleFramePluginConfig
61 def __init__(self, config, name, schema, metadata):
63 Initialize the measurement object.
65 @param[in] config An instance of this class's ConfigClass.
66 @param[in] name The string the plugin was registered with.
67 @param[in,out] schema The Source schema. New fields should be added here to
68 hold measurements produced by this plugin.
69 @param[in] metadata Plugin metadata that will be attached to the output catalog
71 BasePlugin.__init__(self, config, name)
75 Measure the properties of a source on a single image (single-epoch image or coadd).
77 @param[in,out] measRecord lsst.afw.table.SourceRecord to be filled with outputs,
78 and from which previously-measured quantities can be
81 @param[in] exposure lsst.afw.image.ExposureF, containing the pixel data to
82 be measured and the associated Psf, Wcs, etc. All
83 other sources in the image will have been replaced by
84 noise according to deblender outputs.
87 raise NotImplementedError()
91 Measure the properties of a group of blended sources on a single image
92 (single-epoch image or coadd).
94 @param[in,out] measCat lsst.afw.table.SourceCatalog to be filled with outputs,
95 and from which previously-measured quantities can be
96 retrieved, containing only the sources that should be
97 measured together in this call.
99 @param[in] exposure lsst.afw.image.ExposureF, containing the pixel data to
100 be measured and the associated Psf, Wcs, etc. Sources
101 not in the blended hierarchy to be measured will have
102 been replaced with noise using deblender outputs.
104 Derived classes that do not implement measureN() should just inherit this
105 disabled version. Derived classes that do implement measureN() should additionally
106 add a bool doMeasureN config field to their config class to signal that measureN-mode
109 raise NotImplementedError()
113 Config class for single frame measurement driver task.
116 plugins = SingleFramePlugin.registry.makeField(
118 default=[
"base_PixelFlags",
120 "base_GaussianCentroid",
121 "base_NaiveCentroid",
125 "base_CircularApertureFlux",
126 "base_ClassificationExtendedness",
129 doc=
"Plugins to be run and their configuration"
131 algorithms = property(
lambda self: self.
plugins, doc=
"backwards-compatibility alias for plugins")
142 A subtask for measuring the properties of sources on a single exposure.
144 The task is configured with a list of "plugins": each plugin defines the values it
145 measures (i.e. the columns in a table it will fill) and conducts that measurement
146 on each detected source (see SingleFramePlugin). The job of the
147 measurement task is to initialize the set of plugins (which includes setting up the
148 catalog schema) from their configuration, and then invoke each plugin on each
151 When run after the deblender (see lsst.meas.deblender.SourceDeblendTask),
152 SingleFrameMeasurementTask also replaces each source's neighbors with noise before
153 measuring each source, utilizing the HeavyFootprints created by the deblender (see
156 SingleFrameMeasurementTask has only two methods: __init__() and run(). For configuration
157 options, see SingleFrameMeasurementConfig.
159 @section meas_base_sfm_Example A complete example of using SingleFrameMeasurementTask
161 The code below is in examples/runSingleFrameTask.py
163 @dontinclude runSingleFrameTask.py
165 See meas_algorithms_detection_Example for more information on SourceDetectionTask.
167 First, import the required tasks (there are some other standard imports;
168 read the file if you're confused):
170 @skip SourceDetectionTask
171 @until SingleFrameMeasurementTask
173 We need to create our tasks before processing any data as the task constructors
174 can add extra columns to the schema. The most important argument we pass these to these
175 is an lsst.afw.table.Schema object, which contains information about the fields (i.e. columns) of the
176 measurement catalog we'll create, including names, types, and additional documentation.
177 Tasks that operate on a catalog are typically passed a Schema upon construction, to which
178 they add the fields they'll fill later when run. We construct a mostly empty Schema that
179 contains just the fields required for a SourceCatalog like this:
183 Now we can configure and create the SourceDetectionTask:
187 We then move on to configuring the measurement task:
191 While a reasonable set of plugins is configured by default, we'll customize the list.
192 We also need to unset one of the slots at the same time, because we're
193 not running the algorithm that it's set to by default, and that would cause problems later:
197 Now, finally, we can construct the measurement task:
199 @skipline measureTask
201 After constructing all the tasks, we can inspect the Schema we've created:
203 @skipline print schema
205 All of the fields in the
206 schema can be accessed via the get() method on a record object. See afwTable for more
209 We're now ready to process the data (we could loop over multiple exposures/catalogs using the same
210 task objects). First create the output table and process the image to find sources:
220 We then might plot the results (@em e.g. if you set `--ds9` on the command line)
225 and end up with something like
227 @image html runSingleFrameTask-ds9.png
230 ConfigClass = SingleFrameMeasurementConfig
232 def __init__(self, schema, algMetadata=None, **kwds):
234 Initialize the task. Set up the execution order of the plugins and initialize
235 the plugins, giving each plugin an opportunity to add its measurement fields to
236 the output schema and to record information in the task metadata.
238 @param[in,out] schema lsst.afw.table.Schema, to be initialized to include the
239 measurement fields from the plugins already
240 @param[in,out] algMetadata lsst.daf.base.PropertyList used to record information about
241 each algorithm. An empty PropertyList will be created if None.
242 @param[in] **kwds Keyword arguments forwarded to lsst.pipe.base.Task.__init__
244 super(SingleFrameMeasurementTask, self).
__init__(algMetadata=algMetadata, **kwds)
245 if schema.getVersion() == 0:
246 raise lsst.pex.exceptions.LogicError(
"schema must have version 1 or greater")
248 self.initializePlugins(schema=self.
schema)
249 self.config.slots.setupSchema(self.
schema)
252 def run(self, measCat, exposure, noiseImage=None, exposureId=None):
254 Run single frame measurement over an exposure and source catalog
256 @param[in,out] measCat lsst.afw.table.SourceCatalog to be filled with outputs. Must
257 contain all the SourceRecords to be measured (with Footprints
258 attached), and have a schema that is a superset of self.schema.
260 @param[in] exposure lsst.afw.image.ExposureF, containing the pixel data to
261 be measured and the associated Psf, Wcs, etc.
262 @param[in] noiseImage optional lsst.afw.image.ImageF for test which need to control
264 @param[in] exposureId optional unique exposureId used to calculate random number
265 generator seed in the NoiseReplacer.
269 if exposure.__class__.__name__ ==
"SourceCatalog":
273 assert measCat.getSchema().contains(self.
schema)
274 footprints = {measRecord.getId(): (measRecord.getParent(), measRecord.getFootprint())
275 for measRecord
in measCat}
281 if self.config.doReplaceWithNoise:
282 noiseReplacer = NoiseReplacer(self.config.noiseReplacer, exposure, footprints,
283 noiseImage=noiseImage, log=self.log, exposureId=exposureId)
284 algMetadata = measCat.getMetadata()
285 if not algMetadata
is None:
286 algMetadata.addInt(
"NOISE_SEED_MULTIPLIER", self.config.noiseReplacer.noiseSeedMultiplier)
287 algMetadata.addString(
"NOISE_SOURCE", self.config.noiseReplacer.noiseSource)
288 algMetadata.addDouble(
"NOISE_OFFSET", self.config.noiseReplacer.noiseOffset)
289 if not exposureId
is None:
290 algMetadata.addLong(
"NOISE_EXPOSURE_ID", exposureId)
292 noiseReplacer = DummyNoiseReplacer()
296 measParentCat = measCat.getChildren(0)
298 self.log.info(
"Measuring %d sources (%d parents, %d children) "
299 % (len(measCat), len(measParentCat), len(measCat) - len(measParentCat)))
301 for parentIdx, measParentRecord
in enumerate(measParentCat):
303 measChildCat = measCat.getChildren(measParentRecord.getId())
305 for measChildRecord
in measChildCat:
306 noiseReplacer.insertSource(measChildRecord.getId())
307 self.callMeasure(measChildRecord, exposure)
308 noiseReplacer.removeSource(measChildRecord.getId())
310 noiseReplacer.insertSource(measParentRecord.getId())
311 self.callMeasure(measParentRecord, exposure)
313 self.callMeasureN(measParentCat[parentIdx:parentIdx+1], exposure)
314 self.callMeasureN(measChildCat, exposure)
315 noiseReplacer.removeSource(measParentRecord.getId())
321 Backwards-compatibility alias for run()
323 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).