Coverage for tests/test_transform.py : 32%

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
# # LSST Data Management System # Copyright 2008-2015 AURA/LSST. # # This product includes software developed by the # LSST Project (http://www.lsst.org/). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the LSST License Statement and # the GNU General Public License along with this program. If not, # see <http://www.lsstcorp.org/LegalNotices/>. # Test the basic operation of measurement transformations.
We test measurement transforms in two ways:
First, we construct and run a simple TransformTask on the (mocked) results of measurement tasks. The same test is carried out against both SingleFrameMeasurementTask and ForcedMeasurementTask, on the basis that the transformation system should be agnostic as to the origin of the source catalog it is transforming.
Secondly, we use data from the obs_test package to demonstrate that the transformtion system and its interface package are capable of operating on data processed by the rest of the stack.
For the purposes of testing, we define a "TrivialMeasurement" plugin and associated transformation. Rather than building a catalog by measuring genuine SourceRecords, we directly populate a catalog following the TrivialMeasurement schema, then check that it is transformed properly by the TrivialMeasurementTransform. """
RunTransformConfig, CoaddSrcTransformTask)
# Rather than providing real WCS and calibration objects to the # transformation, we use this simple placeholder to keep track of the number # of times it is accessed.
self.count = 0
self.count += 1
"""Pass through all input fields to the output, and add a new field named after the measurement with the suffix "_transform". """ measBase.transforms.MeasurementTransform.__init__(self, config, name, mapper) for key, field in mapper.getInputSchema().extract(name + "*").values(): mapper.addMapping(key) self.key = mapper.editOutputSchema().addField(name + "_transform", type="D", doc="transformed dummy")
"""Transform inputCatalog to outputCatalog.
We update the wcs and photoCalib placeholders to indicate that they have been seen in the transformation, but do not use their values.
@param[in] inputCatalog SourceCatalog of measurements for transformation. @param[out] outputCatalog BaseCatalog of transformed measurements. @param[in] wcs Dummy WCS information; an instance of Placeholder. @param[in] photoCalib Dummy calibration information; an instance of Placeholder. """ if hasattr(wcs, "increment"): wcs.increment() if hasattr(photoCalib, "increment"): photoCalib.increment() inColumns = inputCatalog.getColumnView() outColumns = outputCatalog.getColumnView() outColumns[self.key] = -1.0 * inColumns[self.name]
"""Default values for a trivial measurement plugin, subclassed below""" def getExecutionOrder(): return 0
def getTransformClass(): return TrivialMeasurementTransform
measRecord.set(self.key, 1.0)
"""Single frame version of the trivial measurement"""
measBase.sfm.SingleFramePlugin.__init__(self, config, name, schema, metadata) self.key = schema.addField(name, type="D", doc="dummy field")
"""Forced frame version of the trivial measurement"""
measBase.forcedMeasurement.ForcedPlugin.__init__(self, config, name, schemaMapper, metadata) self.key = schemaMapper.editOutputSchema().addField(name, type="D", doc="dummy field")
"""Check the results of applying transformTask to a SourceCatalog.
@param[in] measConf Measurement plugin configuration. @param[in] schema Input catalog schema. @param[in] transformTask Instance of TransformTask to be applied.
For internal use by this test case. """ # There should now be one transformation registered per measurement plugin. self.assertEqual(len(measConf.plugins.names), len(transformTask.transforms))
# Rather than do a real measurement, we use a dummy source catalog # containing a source at an arbitrary position. inCat = afwTable.SourceCatalog(schema) r = inCat.addNew() r.setCoord(afwGeom.SpherePoint(0.0, 11.19, afwGeom.degrees)) r[PLUGIN_NAME] = 1.0
wcs, photoCalib = Placeholder(), Placeholder() outCat = transformTask.run(inCat, wcs, photoCalib)
# Check that all sources have been transformed appropriately. for inSrc, outSrc in zip(inCat, outCat): self.assertEqual(outSrc[PLUGIN_NAME], inSrc[PLUGIN_NAME]) self.assertEqual(outSrc[PLUGIN_NAME + "_transform"], inSrc[PLUGIN_NAME] * -1.0) for field in transformTask.config.toDict()['copyFields']: self.assertEqual(outSrc.get(field), inSrc.get(field))
# Check that the wcs and photoCalib objects were accessed once per transform. self.assertEqual(wcs.count, len(transformTask.transforms)) self.assertEqual(photoCalib.count, len(transformTask.transforms))
"""Test applying a transform task to the results of single frame measurement.""" schema = afwTable.SourceTable.makeMinimalSchema() sfmConfig = measBase.SingleFrameMeasurementConfig(plugins=[PLUGIN_NAME]) # We don't use slots in this test for key in sfmConfig.slots: setattr(sfmConfig.slots, key, None) sfmTask = measBase.SingleFrameMeasurementTask(schema, config=sfmConfig) transformTask = TransformTask(measConfig=sfmConfig, inputSchema=sfmTask.schema, outputDataset="src") self._transformAndCheck(sfmConfig, sfmTask.schema, transformTask)
"""Test applying a transform task to the results of forced measurement.""" schema = afwTable.SourceTable.makeMinimalSchema() forcedConfig = measBase.ForcedMeasurementConfig(plugins=[PLUGIN_NAME]) # We don't use slots in this test for key in forcedConfig.slots: setattr(forcedConfig.slots, key, None) forcedConfig.copyColumns = {"id": "objectId", "parent": "parentObjectId"} forcedTask = measBase.ForcedMeasurementTask(schema, config=forcedConfig) transformConfig = TransformConfig(copyFields=("objectId", "coord_ra", "coord_dec")) transformTask = TransformTask(measConfig=forcedConfig, inputSchema=forcedTask.schema, outputDataset="forced_src", config=transformConfig) self._transformAndCheck(forcedConfig, forcedTask.schema, transformTask)
def tempDirectory(*args, **kwargs): """A context manager which provides a temporary directory and automatically cleans up when done.""" dirname = tempfile.mkdtemp(*args, **kwargs) try: yield dirname finally: shutil.rmtree(dirname, ignore_errors=True)
obsTestDir = lsst.utils.getPackageDir('obs_test') inputDir = os.path.join(obsTestDir, "data", "input")
# Configure a ProcessCcd task such that it will return a minimal # number of measurements plus our test plugin. cfg = ProcessCcdConfig() cfg.calibrate.measurement.plugins.names = ["base_SdssCentroid", "base_SkyCoord", PLUGIN_NAME] cfg.calibrate.measurement.slots.shape = None cfg.calibrate.measurement.slots.psfFlux = None cfg.calibrate.measurement.slots.apFlux = None cfg.calibrate.measurement.slots.gaussianFlux = None cfg.calibrate.measurement.slots.modelFlux = None cfg.calibrate.measurement.slots.calibFlux = None # no reference catalog, so... cfg.calibrate.doAstrometry = False cfg.calibrate.doPhotoCal = False # disable aperture correction because we aren't measuring aperture flux cfg.calibrate.doApCorr = False # Extendedness requires modelFlux, disabled above. cfg.calibrate.catalogCalculation.plugins.names.discard("base_ClassificationExtendedness")
# Process the test data with ProcessCcd then perform a transform. with tempDirectory() as tempDir: measResult = ProcessCcdTask.parseAndRun(args=[inputDir, "--output", tempDir, "--id", "visit=1"], config=cfg, doReturnResults=True) trArgs = [tempDir, "--output", tempDir, "--id", "visit=1", "-c", "inputConfigType=processCcd_config"] trResult = SrcTransformTask.parseAndRun(args=trArgs, doReturnResults=True)
# It should be possible to reprocess the data through a new transform task with exactly # the same configuration without throwing. This check is useful since we are # constructing the task on the fly, which could conceivably cause problems with # configuration/metadata persistence. trResult = SrcTransformTask.parseAndRun(args=trArgs, doReturnResults=True)
measSrcs = measResult.resultList[0].result.calibRes.sourceCat trSrcs = trResult.resultList[0].result
# The length of the measured and transformed catalogs should be the same. self.assertEqual(len(measSrcs), len(trSrcs))
# Each source should have been measured & transformed appropriately. for measSrc, trSrc in zip(measSrcs, trSrcs): # The TrivialMeasurement should be transformed as defined above. self.assertEqual(trSrc[PLUGIN_NAME], measSrc[PLUGIN_NAME]) self.assertEqual(trSrc[PLUGIN_NAME + "_transform"], -1.0 * measSrc[PLUGIN_NAME])
# The SdssCentroid should be transformed to celestial coordinates. # Checking that the full transformation has been done correctly is # out of scope for this test case; we just ensure that there's # plausible position in the transformed record. trCoord = afwTable.CoordKey(trSrcs.schema["base_SdssCentroid"]).get(trSrc) self.assertAlmostEqual(measSrc.getCoord().getLongitude(), trCoord.getLongitude()) self.assertAlmostEqual(measSrc.getCoord().getLatitude(), trCoord.getLatitude())
"""Check that CoaddSrcTransformTask is set up properly.
RunTransformTestCase, above, has tested the basic RunTransformTask mechanism. Here, we just check that it is appropriately adapted for coadds. """
# The following are hard-coded in lsst.pipe.tasks.multiBand:
# We need a temporary repository in which we can store test configs. self.repo = tempfile.mkdtemp() with open(os.path.join(self.repo, "_mapper"), "w") as f: f.write("lsst.obs.test.TestMapper") self.butler = dafPersist.Butler(self.repo)
# Persist a coadd measurement config. # We disable all measurement plugins so that there's no actual work # for the TransformTask to do. measCfg = MeasureMergedCoaddSourcesConfig() measCfg.measurement.plugins.names = [] self.butler.put(measCfg, self.MEASUREMENT_CONFIG_DATASET)
# Record the type of coadd on which our supposed measurements have # been carried out: we need to check this was propagated to the # transformation task. self.coaddName = measCfg.coaddName
# Since we disabled all measurement plugins, our catalog can be # simple. c = afwTable.SourceCatalog(afwTable.SourceTable.makeMinimalSchema()) self.butler.put(c, self.coaddName + self.SCHEMA_SUFFIX)
# Our transformation config needs to know the type of the measurement # configuration. trCfg = RunTransformConfig() trCfg.inputConfigType = self.MEASUREMENT_CONFIG_DATASET
self.transformTask = CoaddSrcTransformTask(config=trCfg, log=None, butler=self.butler)
del self.butler del self.transformTask shutil.rmtree(self.repo)
"""Check that we have correctly derived the coadd name.""" self.assertEqual(self.transformTask.coaddName, self.coaddName)
"""Check that we have correctly derived the type of the measured sources.""" self.assertEqual(self.transformTask.sourceType, self.coaddName + self.SOURCE_SUFFIX)
"""Check that we have correctly derived the type of the measurement images.""" self.assertEqual(self.transformTask.calexpType, self.coaddName + self.CALEXP_SUFFIX)
lsst.utils.tests.init()
lsst.utils.tests.init() unittest.main() |