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-2017 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 <https://www.lsstcorp.org/LegalNotices/>. 

# 

 

import unittest 

import numpy as np 

 

import lsst.afw.table 

import lsst.meas.algorithms 

import lsst.meas.base.tests 

import lsst.pipe.base 

import lsst.utils.tests 

 

from lsst.meas.algorithms import ColorLimit 

 

 

class SourceSelectorTester: 

"""Mixin for testing 

 

This provides a base class for doing tests common to the 

ScienceSourceSelectorTask and ReferenceSourceSelectorTask. 

""" 

Task = None 

 

def setUp(self): 

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

schema.addField("flux", float, "Flux value") 

schema.addField("flux_flag", "Flag", "Bad flux?") 

schema.addField("other_flux", float, "Flux value 2") 

schema.addField("other_flux_flag", "Flag", "Bad flux 2?") 

schema.addField("other_fluxErr", float, "Flux error 2") 

schema.addField("goodFlag", "Flag", "Flagged if good") 

schema.addField("badFlag", "Flag", "Flagged if bad") 

schema.addField("starGalaxy", float, "0=star, 1=galaxy") 

schema.addField("nChild", np.int32, "Number of children") 

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

self.catalog.reserve(10) 

self.config = self.Task.ConfigClass() 

 

def tearDown(self): 

del self.catalog 

 

def check(self, expected): 

task = self.Task(config=self.config) 

results = task.run(self.catalog) 

self.assertListEqual(results.selected.tolist(), expected) 

self.assertListEqual([src.getId() for src in results.sourceCat], 

[src.getId() for src, ok in zip(self.catalog, expected) if ok]) 

 

def testFlags(self): 

bad1 = self.catalog.addNew() 

bad1.set("goodFlag", False) 

bad1.set("badFlag", False) 

bad2 = self.catalog.addNew() 

bad2.set("goodFlag", True) 

bad2.set("badFlag", True) 

bad3 = self.catalog.addNew() 

bad3.set("goodFlag", False) 

bad3.set("badFlag", True) 

good = self.catalog.addNew() 

good.set("goodFlag", True) 

good.set("badFlag", False) 

self.catalog["flux"] = 1.0 

self.catalog["other_flux"] = 1.0 

self.config.flags.good = ["goodFlag"] 

self.config.flags.bad = ["badFlag"] 

self.check([False, False, False, True]) 

 

def testSignalToNoise(self): 

low = self.catalog.addNew() 

low.set("other_flux", 1.0) 

low.set("other_fluxErr", 1.0) 

good = self.catalog.addNew() 

good.set("other_flux", 1.0) 

good.set("other_fluxErr", 0.1) 

high = self.catalog.addNew() 

high.set("other_flux", 1.0) 

high.set("other_fluxErr", 0.001) 

self.config.doSignalToNoise = True 

self.config.signalToNoise.fluxField = "other_flux" 

self.config.signalToNoise.errField = "other_fluxErr" 

self.config.signalToNoise.minimum = 5.0 

self.config.signalToNoise.maximum = 100.0 

self.check([False, True, False]) 

 

 

class ScienceSourceSelectorTaskTest(SourceSelectorTester, lsst.utils.tests.TestCase): 

Task = lsst.meas.algorithms.ScienceSourceSelectorTask 

 

def setUp(self): 

SourceSelectorTester.setUp(self) 

self.config.fluxLimit.fluxField = "flux" 

self.config.flags.bad = [] 

self.config.doFluxLimit = True 

self.config.doFlags = True 

self.config.doUnresolved = False 

self.config.doIsolated = False 

 

def testFluxLimit(self): 

tooBright = self.catalog.addNew() 

tooBright.set("flux", 1.0e10) 

tooBright.set("flux_flag", False) 

good = self.catalog.addNew() 

good.set("flux", 1000.0) 

good.set("flux_flag", False) 

bad = self.catalog.addNew() 

bad.set("flux", good.get("flux")) 

bad.set("flux_flag", True) 

tooFaint = self.catalog.addNew() 

tooFaint.set("flux", 1.0) 

tooFaint.set("flux_flag", False) 

self.config.fluxLimit.minimum = 10.0 

self.config.fluxLimit.maximum = 1.0e6 

self.config.fluxLimit.fluxField = "flux" 

self.check([False, True, False, False]) 

 

# Works with no maximum set? 

self.config.fluxLimit.maximum = None 

self.check([True, True, False, False]) 

 

# Works with no minimum set? 

self.config.fluxLimit.minimum = None 

self.check([True, True, False, True]) 

 

def testUnresolved(self): 

num = 5 

for _ in range(num): 

self.catalog.addNew() 

self.catalog["flux"] = 1.0 

starGalaxy = np.linspace(0.0, 1.0, num, False) 

self.catalog["starGalaxy"] = starGalaxy 

self.config.doUnresolved = True 

self.config.unresolved.name = "starGalaxy" 

minimum, maximum = 0.3, 0.7 

self.config.unresolved.minimum = minimum 

self.config.unresolved.maximum = maximum 

self.check(((starGalaxy > minimum) & (starGalaxy < maximum)).tolist()) 

 

# Works with no minimum set? 

self.config.unresolved.minimum = None 

self.check((starGalaxy < maximum).tolist()) 

 

# Works with no maximum set? 

self.config.unresolved.minimum = minimum 

self.config.unresolved.maximum = None 

self.check((starGalaxy > minimum).tolist()) 

 

def testIsolated(self): 

num = 5 

for _ in range(num): 

self.catalog.addNew() 

self.catalog["flux"] = 1.0 

parent = np.array([0, 0, 10, 0, 0], dtype=int) 

nChild = np.array([2, 0, 0, 0, 0], dtype=int) 

self.catalog["parent"] = parent 

self.catalog["nChild"] = nChild 

self.config.doIsolated = True 

self.config.isolated.parentName = "parent" 

self.config.isolated.nChildName = "nChild" 

self.check(((parent == 0) & (nChild == 0)).tolist()) 

 

 

class ReferenceSourceSelectorTaskTest(SourceSelectorTester, lsst.utils.tests.TestCase): 

Task = lsst.meas.algorithms.ReferenceSourceSelectorTask 

 

def setUp(self): 

SourceSelectorTester.setUp(self) 

self.config.magLimit.fluxField = "flux" 

self.config.doMagLimit = True 

self.config.doFlags = True 

self.config.doUnresolved = False 

 

def testMagnitudeLimit(self): 

tooBright = self.catalog.addNew() 

tooBright.set("flux", 1.0e10) 

tooBright.set("flux_flag", False) 

good = self.catalog.addNew() 

good.set("flux", 1000.0) 

good.set("flux_flag", False) 

bad = self.catalog.addNew() 

bad.set("flux", good.get("flux")) 

bad.set("flux_flag", True) 

tooFaint = self.catalog.addNew() 

tooFaint.set("flux", 1.0) 

tooFaint.set("flux_flag", False) 

# Note: magnitudes are backwards, so the minimum flux is the maximum magnitude 

self.config.magLimit.minimum = lsst.afw.image.abMagFromFlux(1.0e6) 

self.config.magLimit.maximum = lsst.afw.image.abMagFromFlux(10.0) 

self.config.magLimit.fluxField = "flux" 

self.check([False, True, False, False]) 

 

# Works with no minimum set? 

self.config.magLimit.minimum = None 

self.check([True, True, False, False]) 

 

# Works with no maximum set? 

self.config.magLimit.maximum = None 

self.check([True, True, False, True]) 

 

def testMagErrorLimit(self): 

# Using an arbitrary field as if it was a magnitude error to save adding a new field 

field = "other_fluxErr" 

tooFaint = self.catalog.addNew() 

tooFaint.set(field, 0.5) 

tooBright = self.catalog.addNew() 

tooBright.set(field, 0.00001) 

good = self.catalog.addNew() 

good.set(field, 0.2) 

 

self.config.doMagError = True 

self.config.magError.minimum = 0.01 

self.config.magError.maximum = 0.3 

self.config.magError.magErrField = field 

self.check([False, False, True]) 

 

def testColorLimits(self): 

num = 10 

for _ in range(num): 

self.catalog.addNew() 

color = np.linspace(-0.5, 0.5, num, True) 

flux = 1000.0 

# Definition: color = mag(flux) - mag(otherFlux) 

otherFlux = lsst.afw.image.fluxFromABMag(lsst.afw.image.abMagFromFlux(flux) - color) 

self.catalog["flux"] = flux 

self.catalog["other_flux"] = otherFlux 

minimum, maximum = -0.1, 0.2 

self.config.colorLimits = {"test": ColorLimit(primary="flux", secondary="other_flux", 

minimum=minimum, maximum=maximum)} 

self.check(((color > minimum) & (color < maximum)).tolist()) 

 

# Works with no minimum set? 

self.config.colorLimits["test"].minimum = None 

self.check((color < maximum).tolist()) 

 

# Works with no maximum set? 

self.config.colorLimits["test"].maximum = None 

self.config.colorLimits["test"].minimum = minimum 

self.check((color > minimum).tolist()) 

 

# Multiple limits 

self.config.colorLimits = {"test": ColorLimit(primary="flux", secondary="other_flux", 

minimum=minimum), 

"other": ColorLimit(primary="flux", secondary="other_flux", 

maximum=maximum)} 

assert maximum > minimum # To be non-mutually-exclusive 

self.check(((color > minimum) & (color < maximum)).tolist()) 

 

# Multiple mutually-exclusive limits 

self.config.colorLimits["test"] = ColorLimit(primary="flux", secondary="other_flux", maximum=-0.1) 

self.config.colorLimits["other"] = ColorLimit(primary="flux", secondary="other_flux", minimum=0.1) 

self.check([False]*num) 

 

def testUnresolved(self): 

num = 5 

for _ in range(num): 

self.catalog.addNew() 

self.catalog["flux"] = 1.0 

starGalaxy = np.linspace(0.0, 1.0, num, False) 

self.catalog["starGalaxy"] = starGalaxy 

self.config.doUnresolved = True 

self.config.unresolved.name = "starGalaxy" 

minimum, maximum = 0.3, 0.7 

self.config.unresolved.minimum = minimum 

self.config.unresolved.maximum = maximum 

self.check(((starGalaxy > minimum) & (starGalaxy < maximum)).tolist()) 

 

# Works with no minimum set? 

self.config.unresolved.minimum = None 

self.check((starGalaxy < maximum).tolist()) 

 

# Works with no maximum set? 

self.config.unresolved.minimum = minimum 

self.config.unresolved.maximum = None 

self.check((starGalaxy > minimum).tolist()) 

 

 

class TrivialSourceSelector(lsst.meas.algorithms.BaseSourceSelectorTask): 

"""Return true for every source. Purely for testing.""" 

def selectSources(self, sourceCat, matches=None, exposure=None): 

return lsst.pipe.base.Struct(selected=np.ones(len(sourceCat), dtype=bool)) 

 

 

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

"""Test the API of the Abstract Base Class with a trivial example.""" 

def setUp(self): 

schema = lsst.meas.base.tests.TestDataset.makeMinimalSchema() 

self.selectedKeyName = "is_selected" 

schema.addField(self.selectedKeyName, type="Flag") 

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

for i in range(4): 

self.catalog.addNew() 

 

self.sourceSelector = TrivialSourceSelector() 

 

def testRun(self): 

"""Test that run() returns a catalog and boolean selected array.""" 

result = self.sourceSelector.run(self.catalog) 

for i, x in enumerate(self.catalog['id']): 

self.assertIn(x, result.sourceCat['id']) 

self.assertTrue(result.selected[i]) 

 

def testRunSourceSelectedField(self): 

"""Test that the selected flag is set in the original catalog.""" 

self.sourceSelector.run(self.catalog, sourceSelectedField=self.selectedKeyName) 

np.testing.assert_array_equal(self.catalog[self.selectedKeyName], True) 

 

def testRunNonContiguousRaises(self): 

"""Cannot do source selection on non-contiguous catalogs.""" 

del self.catalog[1] # take one out of the middle to make it non-contiguous. 

self.assertFalse(self.catalog.isContiguous(), "Catalog is contiguous: the test won't work.") 

 

with self.assertRaises(RuntimeError): 

self.sourceSelector.run(self.catalog) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

import sys 

setup_module(sys.modules[__name__]) 

unittest.main()