Hide keyboard shortcuts

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

# 

# 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. 

""" 

import contextlib 

import os 

import shutil 

import tempfile 

import unittest 

 

import lsst.utils 

import lsst.afw.table as afwTable 

import lsst.afw.geom as afwGeom 

import lsst.daf.persistence as dafPersist 

import lsst.meas.base as measBase 

import lsst.utils.tests 

from lsst.pipe.tasks.multiBand import MeasureMergedCoaddSourcesConfig 

from lsst.pipe.tasks.processCcd import ProcessCcdTask, ProcessCcdConfig 

from lsst.pipe.tasks.transformMeasurement import (TransformConfig, TransformTask, SrcTransformTask, 

RunTransformConfig, CoaddSrcTransformTask) 

 

PLUGIN_NAME = "base_TrivialMeasurement" 

 

# 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. 

 

 

class Placeholder: 

 

def __init__(self): 

self.count = 0 

 

def increment(self): 

self.count += 1 

 

 

class TrivialMeasurementTransform(measBase.transforms.MeasurementTransform): 

 

def __init__(self, config, name, mapper): 

"""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") 

 

def __call__(self, inputCatalog, outputCatalog, wcs, photoCalib): 

"""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] 

 

 

class TrivialMeasurementBase: 

 

"""Default values for a trivial measurement plugin, subclassed below""" 

@staticmethod 

def getExecutionOrder(): 

return 0 

 

@staticmethod 

def getTransformClass(): 

return TrivialMeasurementTransform 

 

def measure(self, measRecord, exposure): 

measRecord.set(self.key, 1.0) 

 

 

@measBase.register(PLUGIN_NAME) 

class SFTrivialMeasurement(TrivialMeasurementBase, measBase.sfm.SingleFramePlugin): 

 

"""Single frame version of the trivial measurement""" 

 

def __init__(self, config, name, schema, metadata): 

measBase.sfm.SingleFramePlugin.__init__(self, config, name, schema, metadata) 

self.key = schema.addField(name, type="D", doc="dummy field") 

 

 

@measBase.register(PLUGIN_NAME) 

class ForcedTrivialMeasurement(TrivialMeasurementBase, measBase.forcedMeasurement.ForcedPlugin): 

 

"""Forced frame version of the trivial measurement""" 

 

def __init__(self, config, name, schemaMapper, metadata): 

measBase.forcedMeasurement.ForcedPlugin.__init__(self, config, name, schemaMapper, metadata) 

self.key = schemaMapper.editOutputSchema().addField(name, type="D", doc="dummy field") 

 

 

class TransformTestCase(lsst.utils.tests.TestCase): 

 

def _transformAndCheck(self, measConf, schema, transformTask): 

"""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)) 

 

def testSingleFrameMeasurementTransform(self): 

"""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) 

 

def testForcedMeasurementTransform(self): 

"""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) 

 

 

@contextlib.contextmanager 

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) 

 

 

class RunTransformTestCase(lsst.utils.tests.TestCase): 

 

def testInterface(self): 

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()) 

 

 

class CoaddTransformTestCase(lsst.utils.tests.TestCase): 

"""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. 

""" 

MEASUREMENT_CONFIG_DATASET = "measureCoaddSources_config" 

 

# The following are hard-coded in lsst.pipe.tasks.multiBand: 

SCHEMA_SUFFIX = "Coadd_meas_schema" 

SOURCE_SUFFIX = "Coadd_meas" 

CALEXP_SUFFIX = "Coadd_calexp" 

 

def setUp(self): 

# 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) 

 

def tearDown(self): 

del self.butler 

del self.transformTask 

shutil.rmtree(self.repo) 

 

def testCoaddName(self): 

"""Check that we have correctly derived the coadd name.""" 

self.assertEqual(self.transformTask.coaddName, self.coaddName) 

 

def testSourceType(self): 

"""Check that we have correctly derived the type of the measured sources.""" 

self.assertEqual(self.transformTask.sourceType, self.coaddName + self.SOURCE_SUFFIX) 

 

def testCalexpType(self): 

"""Check that we have correctly derived the type of the measurement images.""" 

self.assertEqual(self.transformTask.calexpType, self.coaddName + self.CALEXP_SUFFIX) 

 

 

class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

344 ↛ 345line 344 didn't jump to line 345, because the condition on line 344 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()