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

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

# 

# LSST Data Management System 

# Copyright 2008-2016 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/>. 

# 

 

""" 

Tests for matching SourceSets 

 

Run with: 

python SourceMatch.py 

or 

python 

>>> import SourceMatch; SourceMatch.run() 

""" 

import os 

import re 

import unittest 

 

import numpy as np 

 

import lsst.geom 

import lsst.afw.table as afwTable 

import lsst.daf.base as dafBase 

import lsst.utils.tests 

import lsst.pex.exceptions as pexExcept 

 

try: 

afwdataDir = lsst.utils.getPackageDir("afwdata") 

except pexExcept.NotFoundError: 

afwdataDir = None 

 

 

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

"""A test case for matching SourceSets 

""" 

 

def setUp(self): 

schema = afwTable.SourceTable.makeMinimalSchema() 

schema.addField("flux_flux", type=np.float64) 

schema.addField("flux_fluxSigma", type=np.float64) 

schema.addField("flux_flag", type="Flag") 

self.table = afwTable.SourceTable.make(schema) 

self.table.definePsfFlux("flux") 

self.ss1 = afwTable.SourceCatalog(self.table) 

self.ss2 = afwTable.SourceCatalog(self.table) 

self.metadata = dafBase.PropertyList() 

 

def tearDown(self): 

del self.table 

del self.metadata 

del self.ss1 

del self.ss2 

 

def testIdentity(self): 

nobj = 1000 

for i in range(nobj): 

s = self.ss1.addNew() 

s.setId(i) 

s.set(afwTable.SourceTable.getCoordKey().getRa(), 

(10 + 0.001*i) * lsst.geom.degrees) 

s.set(afwTable.SourceTable.getCoordKey().getDec(), 

(10 + 0.001*i) * lsst.geom.degrees) 

 

s = self.ss2.addNew() 

s.setId(2*nobj + i) 

# Give slight offsets for Coord testing of matches to/from catalog in checkMatchToFromCatalog() 

# Chosen such that the maximum offset (nobj*1E-7 deg = 0.36 arcsec) is within the maximum 

# distance (1 arcsec) in afwTable.matchRaDec. 

s.set(afwTable.SourceTable.getCoordKey().getRa(), 

(10 + 0.0010001*i) * lsst.geom.degrees) 

s.set(afwTable.SourceTable.getCoordKey().getDec(), 

(10 + 0.0010001*i) * lsst.geom.degrees) 

 

mc = afwTable.MatchControl() 

mc.findOnlyClosest = False 

mat = afwTable.matchRaDec( 

self.ss1, self.ss2, 1.0*lsst.geom.arcseconds, mc) 

self.assertEqual(len(mat), nobj) 

 

cat = afwTable.packMatches(mat) 

 

mat2 = afwTable.unpackMatches(cat, self.ss1, self.ss2) 

 

for m1, m2, c in zip(mat, mat2, cat): 

self.assertEqual(m1.first, m2.first) 

self.assertEqual(m1.second, m2.second) 

self.assertEqual(m1.distance, m2.distance) 

self.assertEqual(m1.first.getId(), c["first"]) 

self.assertEqual(m1.second.getId(), c["second"]) 

self.assertEqual(m1.distance, c["distance"]) 

 

self.checkMatchToFromCatalog(mat, cat) 

 

if False: 

s0 = mat[0][0] 

s1 = mat[0][1] 

print(s0.getRa(), s1.getRa(), s0.getId(), s1.getId()) 

 

def testNaNPositions(self): 

ss1 = afwTable.SourceCatalog(self.table) 

ss2 = afwTable.SourceCatalog(self.table) 

for ss in (ss1, ss2): 

ss.addNew().set(afwTable.SourceTable.getCoordKey().getRa(), 

float('nan') * lsst.geom.radians) 

 

ss.addNew().set(afwTable.SourceTable.getCoordKey().getDec(), 

float('nan') * lsst.geom.radians) 

 

s = ss.addNew() 

s.set(afwTable.SourceTable.getCoordKey().getRa(), 

0.0 * lsst.geom.radians) 

s.set(afwTable.SourceTable.getCoordKey().getDec(), 

0.0 * lsst.geom.radians) 

 

s = ss.addNew() 

s.set(afwTable.SourceTable.getCoordKey().getRa(), 

float('nan') * lsst.geom.radians) 

s.set(afwTable.SourceTable.getCoordKey().getDec(), 

float('nan') * lsst.geom.radians) 

 

mc = afwTable.MatchControl() 

mc.findOnlyClosest = False 

mat = afwTable.matchRaDec(ss1, ss2, 1.0*lsst.geom.arcseconds, mc) 

self.assertEqual(len(mat), 1) 

 

@unittest.skipIf(afwdataDir is None, "afwdata not setup") 

def testPhotometricCalib(self): 

"""Test matching the CFHT catalogue (as generated using LSST code) to the SDSS catalogue 

""" 

 

band = 2 # SDSS r 

 

# Read SDSS catalogue 

with open(os.path.join(afwdataDir, "CFHT", "D2", "sdss.dat"), "r") as ifd: 

 

sdss = afwTable.SourceCatalog(self.table) 

sdssSecondary = afwTable.SourceCatalog(self.table) 

 

PRIMARY, SECONDARY = 1, 2 # values of mode 

 

id = 0 

for line in ifd.readlines(): 

if re.search(r"^\s*#", line): 

continue 

 

fields = line.split() 

objId = int(fields[0]) 

fields[1] 

mode = int(fields[2]) 

ra, dec = [float(f) for f in fields[3:5]] 

psfMags = [float(f) for f in fields[5:]] 

 

if mode == PRIMARY: 

s = sdss.addNew() 

elif SECONDARY: 

s = sdssSecondary.addNew() 

 

s.setId(objId) 

s.setRa(ra * lsst.geom.degrees) 

s.setDec(dec * lsst.geom.degrees) 

s.set(self.table.getPsfFluxKey(), psfMags[band]) 

 

del ifd 

 

# Read catalalogue built from the template image 

# Read SDSS catalogue 

with open(os.path.join(afwdataDir, "CFHT", "D2", "template.dat"), "r") as ifd: 

 

template = afwTable.SourceCatalog(self.table) 

 

id = 0 

for line in ifd.readlines(): 

if re.search(r"^\s*#", line): 

continue 

 

fields = line.split() 

id, flags = [int(f) for f in fields[0:2]] 

ra, dec = [float(f) for f in fields[2:4]] 

flux = [float(f) for f in fields[4:]] 

 

if flags & 0x1: # EDGE 

continue 

 

s = template.addNew() 

s.setId(id) 

id += 1 

s.set(afwTable.SourceTable.getCoordKey().getRa(), 

ra * lsst.geom.degrees) 

s.set(afwTable.SourceTable.getCoordKey().getDec(), 

dec * lsst.geom.degrees) 

s.set(self.table.getPsfFluxKey(), flux[0]) 

 

del ifd 

 

# Actually do the match 

mc = afwTable.MatchControl() 

mc.findOnlyClosest = False 

 

matches = afwTable.matchRaDec( 

sdss, template, 1.0*lsst.geom.arcseconds, mc) 

 

self.assertEqual(len(matches), 901) 

 

if False: 

for mat in matches: 

s0 = mat[0] 

s1 = mat[1] 

d = mat[2] 

print(s0.getRa(), s0.getDec(), s1.getRa(), 

s1.getDec(), s0.getPsfFlux(), s1.getPsfFlux()) 

 

# Actually do the match 

for s in sdssSecondary: 

sdss.append(s) 

 

mc = afwTable.MatchControl() 

mc.symmetricMatch = False 

matches = afwTable.matchRaDec(sdss, 1.0*lsst.geom.arcseconds, mc) 

nmiss = 1 # one object doesn't match 

self.assertEqual(len(matches), len(sdssSecondary) - nmiss) 

 

# Find the one that didn't match 

if False: 

matchIds = set() 

for s0, s1, d in matches: 

matchIds.add(s0.getId()) 

matchIds.add(s1.getId()) 

 

for s in sdssSecondary: 

if s.getId() not in matchIds: 

print("RHL", s.getId()) 

 

matches = afwTable.matchRaDec(sdss, 1.0*lsst.geom.arcseconds) 

self.assertEqual(len(matches), 2*(len(sdssSecondary) - nmiss)) 

 

if False: 

for mat in matches: 

s0 = mat[0] 

s1 = mat[1] 

mat[2] 

print(s0.getId(), s1.getId(), s0.getRa(), s0.getDec(), end=' ') 

print(s1.getRa(), s1.getDec(), s0.getPsfFlux(), s1.getPsfFlux()) 

 

def testMismatches(self): 

""" Chech that matchRaDec works as expected when using 

the includeMismatches option 

""" 

cat1 = afwTable.SourceCatalog(self.table) 

cat2 = afwTable.SourceCatalog(self.table) 

nobj = 100 

for i in range(nobj): 

s1 = cat1.addNew() 

s2 = cat2.addNew() 

s1.setId(i) 

s2.setId(i) 

s1.set(afwTable.SourceTable.getCoordKey().getRa(), 

(10 + 0.0001*i) * lsst.geom.degrees) 

s2.set(afwTable.SourceTable.getCoordKey().getRa(), 

(10.005 + 0.0001*i) * lsst.geom.degrees) 

s1.set(afwTable.SourceTable.getCoordKey().getDec(), 

(10 + 0.0001*i) * lsst.geom.degrees) 

s2.set(afwTable.SourceTable.getCoordKey().getDec(), 

(10.005 + 0.0001*i) * lsst.geom.degrees) 

 

for closest in (True, False): 

mc = afwTable.MatchControl() 

mc.findOnlyClosest = closest 

mc.includeMismatches = False 

matches = afwTable.matchRaDec( 

cat1, cat2, 1.0*lsst.geom.arcseconds, mc) 

mc.includeMismatches = True 

matchesMismatches = afwTable.matchRaDec( 

cat1, cat2, 1.0*lsst.geom.arcseconds, mc) 

 

catMatches = afwTable.SourceCatalog(self.table) 

catMismatches = afwTable.SourceCatalog(self.table) 

for m in matchesMismatches: 

if m[1] is not None: 

if not any(x == m[0] for x in catMatches): 

catMatches.append(m[0]) 

else: 

catMismatches.append(m[0]) 

if closest: 

self.assertEqual(len(catMatches), len(matches)) 

matches2 = afwTable.matchRaDec( 

catMatches, cat2, 1.0*lsst.geom.arcseconds, mc) 

self.assertEqual(len(matches), len(matches2)) 

mc.includeMismatches = False 

noMatches = afwTable.matchRaDec( 

catMismatches, cat2, 1.0*lsst.geom.arcseconds, mc) 

self.assertEqual(len(noMatches), 0) 

 

def checkMatchToFromCatalog(self, matches, catalog): 

"""Check the conversion of matches to and from a catalog 

 

Test the functions in lsst.afw.table.catalogMatches.py 

Note that the return types and schemas of these functions do not necessarily match 

those of the catalogs passed to them, so value entries are compared as opposed to 

comparing the attributes as a whole. 

""" 

catalog.setMetadata(self.metadata) 

matchMeta = catalog.getTable().getMetadata() 

matchToCat = afwTable.catalogMatches.matchesToCatalog( 

matches, matchMeta) 

matchFromCat = afwTable.catalogMatches.matchesFromCatalog(matchToCat) 

self.assertEqual(len(matches), len(matchToCat)) 

self.assertEqual(len(matches), len(matchFromCat)) 

 

for mat, cat, catM, matchC in zip(matches, catalog, matchToCat, matchFromCat): 

self.assertEqual(mat.first.getId(), catM["ref_id"]) 

self.assertEqual(mat.first.getId(), matchC.first.getId()) 

self.assertEqual(mat.first.getCoord(), matchC.first.getCoord()) 

self.assertEqual(mat.second.getId(), cat["second"]) 

self.assertEqual(mat.second.getId(), catM["src_id"]) 

self.assertEqual(mat.second.getId(), matchC.second.getId()) 

self.assertEqual((mat.first.getRa(), mat.first.getDec()), 

(catM["ref_coord_ra"], catM["ref_coord_dec"])) 

self.assertEqual((mat.second.getRa(), mat.second.getDec()), 

(catM["src_coord_ra"], catM["src_coord_dec"])) 

self.assertEqual(mat.first.getCoord(), matchC.first.getCoord()) 

self.assertEqual(mat.second.getCoord(), matchC.second.getCoord()) 

self.assertEqual(mat.distance, matchC.distance) 

self.assertEqual(mat.distance, cat["distance"]) 

self.assertEqual(mat.distance, catM["distance"]) 

 

def assertEqualFloat(self, value1, value2): 

"""Compare floating point values, allowing for NAN 

""" 

self.assertTrue(value1 == value2 or 

(np.isnan(value1) and np.isnan(value2))) 

 

def testDistancePrecision(self): 

"""Test for precision of the calculated distance 

 

Check that the distance produced by matchRaDec is the same 

as the distance produced from calculating the separation 

between the matched coordinates. 

 

Based on DM-13891. 

""" 

num = 1000 # Number of points 

radius = 0.5*lsst.geom.arcseconds # Matching radius 

tol = 1.0e-10 # Absolute tolerance 

rng = np.random.RandomState(12345) # I have the same combination on my luggage 

coordKey = afwTable.SourceTable.getCoordKey() 

raKey = coordKey.getRa() 

decKey = coordKey.getDec() 

for ii in range(num): 

src1 = self.ss1.addNew() 

src1.setId(ii) 

src1.set(raKey, (10 + 0.001*ii) * lsst.geom.degrees) 

src1.set(decKey, (10 + 0.001*ii) * lsst.geom.degrees) 

 

src2 = self.ss2.addNew() 

src2.setId(2*num + ii) 

src2.set(coordKey, 

src1.getCoord().offset(rng.uniform(high=360)*lsst.geom.degrees, 

rng.uniform(high=radius.asArcseconds())*lsst.geom.arcseconds)) 

 

matches = afwTable.matchRaDec(self.ss1, self.ss2, radius) 

dist1 = np.array([(mm.distance*lsst.geom.radians).asArcseconds() for mm in matches]) 

dist2 = np.array([mm.first.getCoord().separation(mm.second.getCoord()).asArcseconds() 

for mm in matches]) 

diff = dist1 - dist2 

self.assertLess(diff.std(), tol) # I get 4e-12 

self.assertFloatsAlmostEqual(dist1, dist2, atol=tol) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()