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

347

348

349

350

351

352

353

354

355

356

357

358

# 

# LSST Data Management System 

# Copyright 2008-2017 LSST Corporation. 

# 

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

# 

""" Unit tests for Python Plugin FlagHandlers and Sample Plugin Example.""" 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.meas.base 

import lsst.meas.base.tests 

import lsst.afw.table 

from lsst.meas.base import FlagDefinitionList, FlagHandler, MeasurementError 

from lsst.meas.base.tests import AlgorithmTestCase 

 

import lsst.pex.exceptions 

from lsst.meas.base.pluginRegistry import register 

from lsst.meas.base.sfm import SingleFramePluginConfig, SingleFramePlugin 

 

 

class PythonPluginConfig(SingleFramePluginConfig): 

""" 

Configuration for Sample Plugin with a FlagHandler. 

""" 

 

edgeLimit = lsst.pex.config.Field(dtype=int, default=0, optional=False, 

doc="How close to the edge can the object be?") 

size = lsst.pex.config.Field(dtype=int, default=1, optional=False, 

doc="size of aperture to measure around the center?") 

flux0 = lsst.pex.config.Field(dtype=float, default=None, optional=False, 

doc="Flux for zero mag, used to set mag if defined") 

 

 

@register("test_PythonPlugin") 

class PythonPlugin(SingleFramePlugin): 

""" 

This is a sample Python plugin which shows how to create and use a FlagHandler. 

The FlagHandler defines the known failures which can occur when the plugin is 

called, and should be tested after measure() to detect any potential problems. 

 

This plugin is a very simple flux measurement algorithm which sums 

the pixel values in a square box of dimension config.size around the center point. 

 

Note that to properly set the error flags when a MeasurementError occurs, the plugin 

must implement the fail() method as shown below. The fail method should set both 

the general error flag, and any specific flag as designated in the MeasurementError. 

 

This example also demonstrates the use of the SafeCentroidExtractor. The 

SafeCentroidEextractor and SafeShapeExtractor can be used to get some reasonable 

estimate of the centroid or shape in cases where the centroid or shape slot 

has failed on a particular source. 

""" 

 

ConfigClass = PythonPluginConfig 

 

@classmethod 

def getExecutionOrder(cls): 

return cls.FLUX_ORDER 

 

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

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

flagDefs = FlagDefinitionList() 

self.FAILURE = flagDefs.addFailureFlag() 

self.CONTAINS_NAN = flagDefs.add("flag_containsNan", "Measurement area contains a nan") 

self.EDGE = flagDefs.add("flag_edge", "Measurement area over edge") 

self.flagHandler = FlagHandler.addFields(schema, name, flagDefs) 

self.centroidExtractor = lsst.meas.base.SafeCentroidExtractor(schema, name) 

self.fluxKey = schema.addField(name + "_flux", "F", doc="flux") 

self.magKey = schema.addField(name + "_mag", "F", doc="mag") 

 

def measure(self, measRecord, exposure): 

""" 

The measure method is called by the measurement framework when task.run is called 

If a MeasurementError is raised during this method, the fail() method will be 

called to set the error flags. 

""" 

# Call the SafeCentroidExtractor to get a centroid, even if one has 

# not been supplied by the centroid slot. Normally, the centroid is supplied 

# by the centroid slot, but if that fails, the footprint is used as a fallback. 

# If the fallback is needed, the fail flag will be set on this record. 

center = self.centroidExtractor(measRecord, self.flagHandler) 

 

# create a square bounding box of size = config.size around the center 

centerPoint = lsst.geom.Point2I(int(center.getX()), int(center.getY())) 

bbox = lsst.geom.Box2I(centerPoint, lsst.geom.Extent2I(1, 1)) 

bbox.grow(self.config.size) 

 

# If the measurement box falls outside the exposure, raise the edge MeasurementError 

if not exposure.getBBox().contains(bbox): 

raise MeasurementError(self.EDGE.doc, self.EDGE.number) 

 

# Sum the pixels inside the bounding box 

flux = lsst.afw.image.ImageF(exposure.getMaskedImage().getImage(), bbox).getArray().sum() 

measRecord.set(self.fluxKey, flux) 

 

# If there was a nan inside the bounding box, the flux will still be nan 

if np.isnan(flux): 

raise MeasurementError(self.CONTAINS_NAN.doc, self.CONTAINS_NAN.number) 

 

if self.config.flux0 is not None: 

120 ↛ 122line 120 didn't jump to line 122, because the condition on line 120 was never false if self.config.flux0 == 0: 

raise ZeroDivisionError("self.config.flux0 is zero in divisor") 

mag = -2.5 * np.log10(flux/self.config.flux0) 

measRecord.set(self.magKey, mag) 

 

def fail(self, measRecord, error=None): 

""" 

This routine responds to the standard failure call in baseMeasurement 

If the exception is a MeasurementError, the error will be passed to the 

fail method by the MeasurementFramework. If error is not none, error.cpp 

should correspond to a specific error and the appropriate 

error flag will be set. 

""" 

if error is None: 

self.flagHandler.handleFailure(measRecord) 

else: 

self.flagHandler.handleFailure(measRecord, error.cpp) 

 

 

class FlagHandlerTestCase(AlgorithmTestCase, lsst.utils.tests.TestCase): 

# Setup a configuration and datasource to be used by the plugin tests 

 

def setUp(self): 

self.algName = "test_PythonPlugin" 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Point2I(100, 100)) 

self.dataset = lsst.meas.base.tests.TestDataset(bbox) 

self.dataset.addSource(flux=1E5, centroid=lsst.geom.Point2D(25, 26)) 

config = lsst.meas.base.SingleFrameMeasurementConfig() 

config.plugins = [self.algName] 

config.slots.centroid = None 

config.slots.apFlux = None 

config.slots.calibFlux = None 

config.slots.instFlux = None 

config.slots.modelFlux = None 

config.slots.psfFlux = None 

config.slots.shape = None 

config.slots.psfShape = None 

self.config = config 

 

def tearDown(self): 

del self.config 

del self.dataset 

 

def testFlagHandler(self): 

""" 

Standalone test to create a flaghandler and call it 

This is not a real world example, just a simple unit test 

""" 

schema = lsst.afw.table.SourceTable.makeMinimalSchema() 

 

# This is a FlagDefinition structure like a plugin might have 

flagDefs = FlagDefinitionList() 

FAILURE = flagDefs.addFailureFlag() 

FIRST = flagDefs.add("1st error", "this is the first failure type") 

SECOND = flagDefs.add("2nd error", "this is the second failure type") 

fh = FlagHandler.addFields(schema, "test", flagDefs) 

# Check to be sure that the FlagHandler was correctly initialized 

for index in range(len(flagDefs)): 

self.assertEqual(flagDefs.getDefinition(index).name, fh.getFlagName(index)) 

 

catalog = lsst.afw.table.SourceCatalog(schema) 

 

# Now check to be sure that all of the known failures set the bits correctly 

record = catalog.addNew() 

fh.handleFailure(record) 

self.assertTrue(fh.getValue(record, FAILURE.number)) 

self.assertFalse(fh.getValue(record, FIRST.number)) 

self.assertFalse(fh.getValue(record, SECOND.number)) 

record = catalog.addNew() 

 

error = MeasurementError(FAILURE.doc, FAILURE.number) 

fh.handleFailure(record, error.cpp) 

self.assertTrue(fh.getValue(record, FAILURE.number)) 

self.assertFalse(fh.getValue(record, FIRST.number)) 

self.assertFalse(fh.getValue(record, SECOND.number)) 

 

record = catalog.addNew() 

error = MeasurementError(FIRST.doc, FIRST.number) 

fh.handleFailure(record, error.cpp) 

self.assertTrue(fh.getValue(record, FAILURE.number)) 

self.assertTrue(fh.getValue(record, FIRST.number)) 

self.assertFalse(fh.getValue(record, SECOND.number)) 

 

record = catalog.addNew() 

error = MeasurementError(SECOND.doc, SECOND.number) 

fh.handleFailure(record, error.cpp) 

self.assertTrue(fh.getValue(record, FAILURE.number)) 

self.assertFalse(fh.getValue(record, FIRST.number)) 

self.assertTrue(fh.getValue(record, SECOND.number)) 

 

# Test with no failure flag 

def testNoFailureFlag(self): 

""" 

Standalone test to create a flaghandler and call it 

This is not a real world example, just a simple unit test 

""" 

schema = lsst.afw.table.SourceTable.makeMinimalSchema() 

 

# This is a FlagDefinition structure like a plugin might have 

flagDefs = FlagDefinitionList() 

FIRST = flagDefs.add("1st error", "this is the first failure type") 

SECOND = flagDefs.add("2nd error", "this is the second failure type") 

fh = FlagHandler.addFields(schema, "test", flagDefs) 

# Check to be sure that the FlagHandler was correctly initialized 

for index in range(len(flagDefs)): 

self.assertEqual(flagDefs.getDefinition(index).name, fh.getFlagName(index)) 

 

catalog = lsst.afw.table.SourceCatalog(schema) 

 

# Now check to be sure that all of the known failures set the bits correctly 

record = catalog.addNew() 

fh.handleFailure(record) 

self.assertFalse(fh.getValue(record, FIRST.number)) 

self.assertFalse(fh.getValue(record, SECOND.number)) 

record = catalog.addNew() 

 

record = catalog.addNew() 

error = MeasurementError(FIRST.doc, FIRST.number) 

fh.handleFailure(record, error.cpp) 

self.assertTrue(fh.getValue(record, FIRST.number)) 

self.assertFalse(fh.getValue(record, SECOND.number)) 

 

record = catalog.addNew() 

error = MeasurementError(SECOND.doc, SECOND.number) 

fh.handleFailure(record, error.cpp) 

self.assertFalse(fh.getValue(record, FIRST.number)) 

self.assertTrue(fh.getValue(record, SECOND.number)) 

 

# This and the following tests using the toy plugin, and demonstrate how 

# the flagHandler is used. 

 

def testPluginNoError(self): 

""" 

Test that the sample plugin can be run without errors 

""" 

schema = self.dataset.makeMinimalSchema() 

task = lsst.meas.base.SingleFrameMeasurementTask(schema=schema, config=self.config) 

exposure, cat = self.dataset.realize(noise=100.0, schema=schema, randomSeed=0) 

task.run(cat, exposure) 

source = cat[0] 

self.assertFalse(source.get(self.algName + "_flag")) 

self.assertFalse(source.get(self.algName + "_flag_containsNan")) 

self.assertFalse(source.get(self.algName + "_flag_edge")) 

 

def testPluginUnexpectedError(self): 

""" 

An unexpected error is a non-fatal error which is not caught by the algorithm itself. 

However, such errors are caught by the measurement framework in task.run, and result 

in the failure flag being set, but no other specific flags 

""" 

self.config.plugins[self.algName].flux0 = 0.0 # this causes a divide by zero 

schema = self.dataset.makeMinimalSchema() 

task = lsst.meas.base.SingleFrameMeasurementTask(schema=schema, config=self.config) 

exposure, cat = self.dataset.realize(noise=100.0, schema=schema, randomSeed=1) 

task.log.setLevel(task.log.FATAL) 

task.run(cat, exposure) 

source = cat[0] 

self.assertTrue(source.get(self.algName + "_flag")) 

self.assertFalse(source.get(self.algName + "_flag_containsNan")) 

self.assertFalse(source.get(self.algName + "_flag_edge")) 

 

def testPluginContainsNan(self): 

""" 

Test that the containsNan error can be triggered. 

""" 

schema = self.dataset.makeMinimalSchema() 

task = lsst.meas.base.SingleFrameMeasurementTask(schema=schema, config=self.config) 

exposure, cat = self.dataset.realize(noise=100.0, schema=schema, randomSeed=2) 

source = cat[0] 

exposure.getMaskedImage().getImage().getArray()[int(source.getY()), int(source.getX())] = np.nan 

task.run(cat, exposure) 

self.assertTrue(source.get(self.algName + "_flag")) 

self.assertTrue(source.get(self.algName + "_flag_containsNan")) 

self.assertFalse(source.get(self.algName + "_flag_edge")) 

 

def testPluginEdgeError(self): 

""" 

Test that the Edge error can be triggered. 

""" 

schema = self.dataset.makeMinimalSchema() 

task = lsst.meas.base.SingleFrameMeasurementTask(schema=schema, config=self.config) 

exposure, cat = self.dataset.realize(noise=100.0, schema=schema, randomSeed=3) 

# Set the size large enough to trigger the edge error 

self.config.plugins[self.algName].size = exposure.getDimensions()[1]//2 

task.log.setLevel(task.log.FATAL) 

task.run(cat, exposure) 

source = cat[0] 

self.assertTrue(source.get(self.algName + "_flag")) 

self.assertFalse(source.get(self.algName + "_flag_containsNan")) 

self.assertTrue(source.get(self.algName + "_flag_edge")) 

 

def testSafeCentroider(self): 

""" 

Test that the SafeCentroidExtractor works and sets the flags correctly 

""" 

# Normal case should use the centroid slot to get the center, which should succeed 

schema = self.dataset.makeMinimalSchema() 

task = lsst.meas.base.SingleFrameMeasurementTask(schema=schema, config=self.config) 

task.log.setLevel(task.log.FATAL) 

exposure, cat = self.dataset.realize(noise=0.0, schema=schema, randomSeed=4) 

source = cat[0] 

task.run(cat, exposure) 

self.assertFalse(source.get(self.algName + "_flag")) 

flux = source.get("test_PythonPlugin_flux") 

self.assertFalse(np.isnan(flux)) 

 

# If one of the center coordinates is nan and the centroid slot error flag has 

# not been set, the SafeCentroidExtractor will fail. 

source.set('truth_x', np.nan) 

source.set('truth_flag', False) 

source.set("test_PythonPlugin_flux", np.nan) 

source.set(self.algName + "_flag", False) 

task.run(cat, exposure) 

self.assertTrue(source.get(self.algName + "_flag")) 

self.assertTrue(np.isnan(source.get("test_PythonPlugin_flux"))) 

 

# But if the same conditions occur and the centroid slot error flag is set 

# to true, the SafeCentroidExtractor will succeed and the algorithm will complete. 

# However, the failure flag will also be set. 

source.set('truth_x', np.nan) 

source.set('truth_flag', True) 

source.set("test_PythonPlugin_flux", np.nan) 

source.set(self.algName + "_flag", False) 

task.run(cat, exposure) 

self.assertTrue(source.get(self.algName + "_flag")) 

self.assertEqual(source.get("test_PythonPlugin_flux"), flux) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()