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

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

from builtins import zip 

from builtins import object 

import numpy as np 

import tempfile 

import shutil 

 

import os 

import unittest 

import lsst 

import lsst.utils.tests 

from lsst.utils import getPackageDir 

from lsst.sims.utils.CodeUtilities import sims_clean_up 

from lsst.sims.utils import ObservationMetaData 

from lsst.sims.catalogs.db import CatalogDBObject 

from lsst.sims.catalogs.definitions import InstanceCatalog 

from lsst.sims.catUtils.mixins import AstrometryStars, AstrometryGalaxies 

from lsst.sims.catUtils.mixins import PhotometryStars, PhotometryGalaxies 

from lsst.sims.catUtils.utils import setupPhotometryCatalog 

from lsst.sims.catUtils.utils import makeStarDatabase, makeGalaxyDatabase 

 

ROOT = os.path.abspath(os.path.dirname(__file__)) 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class testStarCatalog(InstanceCatalog, AstrometryStars, PhotometryStars): 

""" 

A class with no photometry columns. Meant to be passed to setupPhotometryCatalog 

where it will be given photometry columns 

""" 

catalog_type = __file__ + "test_star_catalog" 

column_outputs = ['raObserved', 'decObserved'] 

default_formats = {'f': '%.12e'} 

 

 

class baselineStarCatalog(InstanceCatalog, AstrometryStars, PhotometryStars): 

""" 

Baseline photometry catalog against which to compare testStarCatalog 

""" 

catalog_type = __file__ + 'baseline_star_catalog' 

column_outputs = ['raObserved', 'decObserved'] 

default_formats = {'f': '%.12e'} 

 

 

class testGalaxyCatalog(InstanceCatalog, AstrometryGalaxies, PhotometryGalaxies): 

""" 

A class with no photometry columns. Meant to be passed to setupPhotometryCatalog 

where it will be given photometry columns 

""" 

catalog_type = __file__ + 'test_galaxy_catalog' 

column_outputs = ['raObserved', 'decObserved'] 

default_formats = {'f': '%.12e'} 

 

 

class baselineGalaxyCatalog(InstanceCatalog, AstrometryGalaxies, PhotometryGalaxies): 

""" 

Baseline photometry catalog against which to compare testGalaxyCatalog 

""" 

catalog_type = __file__ + 'baseline_galaxy_catalog' 

column_outputs = ['raObserved', 'decObserved'] 

default_formats = {'f': '%.12e'} 

 

 

class testStarDBObject(CatalogDBObject): 

""" 

CatalogDBObject to map our test database of stars 

""" 

tableid = 'StarAllForceseek' 

idColKey = 'id' 

raColName = 'ra' 

decColName = 'decl' 

objectTypeId = 49 

columns = [('id', 'simobjid', int), 

('raJ2000', 'ra*PI()/180.'), 

('decJ2000', 'decl*PI()/180.'), 

('magNorm', None), 

('properMotionRa', '(mura/(1000.*3600.))*PI()/180.'), 

('properMotionDec', '(mudecl/(1000.*3600.))*PI()/180.'), 

('parallax', 'parallax*PI()/648000000.'), 

('galacticAv', '3.1*ebv'), 

('radialVelocity', 'vrad'), 

('variabilityParameters', 'varParamStr', str, 256), 

('sedFilename', 'sedfilename', str, 40)] 

 

 

class testGalaxyDBObject(CatalogDBObject): 

 

#: This is the base table for the galaxies 

# tableid = 'final_clone_db' 

tableid = 'galaxy' 

idColKey = 'galtileid' 

raColName = '((CAST(ra AS NUMERIC(9,6))%360.)+360.)%360.' 

decColName = 'dec' 

objectTypeId = 51 

 

columns = [('galtileid', None, np.int64), 

('galid', None, str, 30), 

('raJ2000', 'ra*PI()/180.'), 

('decJ2000', 'dec*PI()/180.'), 

('raJ2000Bulge', 'bra*PI()/180.'), 

('decJ2000Bulge', 'bdec*PI()/180.'), 

('raJ2000Disk', 'dra*PI()/180.'), 

('decJ2000Disk', 'ddec*PI()/180.'), 

('raJ2000Agn', 'agnra*PI()/180.'), 

('decJ2000Agn', 'agndec*PI()/180.'), 

('magNormBulge', 'magnorm_bulge'), 

('magNormDisk', 'magnorm_disk'), 

('magNormAgn', 'magnorm_agn'), 

('sedFilenameBulge', 'sedname_bulge', str, 40), 

('sedFilenameDisk', 'sedname_disk', str, 40), 

('sedFilenameAgn', 'sedname_agn', str, 40), 

('majorAxisBulge', 'a_b*PI()/648000.'), 

('minorAxisBulge', 'b_b*PI()/648000.'), 

('positionAngleBulge', 'pa_bulge*PI()/180.'), 

('sindexBulge', 'bulge_n', int), 

('majorAxisDisk', 'a_d*PI()/648000.'), 

('minorAxisDisk', 'b_d*PI()/648000.'), 

('positionAngleDisk', 'pa_disk*PI()/180.'), 

('sindexDisk', 'disk_n', int), 

('internalExtinctionModelBulge', 'ext_model_b', str, 3), 

('internalAvBulge', 'av_b'), 

('internalRvBulge', 'rv_b'), 

('internalExtinctionModelDisk', 'ext_model_d', str, 3), 

('internalAvDisk', 'av_d'), 

('internalRvDisk', 'rv_d'), 

('lsst_u', 'u_ab'), 

('lsst_g', 'g_ab'), 

('lsst_r', 'r_ab'), 

('lsst_i', 'i_ab'), 

('lsst_z', 'z_ab'), 

('lsst_y', 'y_ab')] 

 

 

class InstanceCatalogSetupUnittest(unittest.TestCase): 

 

@classmethod 

def setUpClass(cls): 

cls.scratch_dir = tempfile.mkdtemp(dir=ROOT, prefix='InstanceCatalogSetupUnittest-') 

 

@classmethod 

def tearDownClass(cls): 

sims_clean_up() 

145 ↛ exitline 145 didn't return from function 'tearDownClass', because the condition on line 145 was never false if os.path.exists(cls.scratch_dir): 

shutil.rmtree(cls.scratch_dir) 

 

def setUp(self): 

self.driver = 'sqlite' 

self.StarDBName = os.path.join(self.scratch_dir, 

'testSetup_setupTestStars.db') 

 

self.GalaxyDBName = os.path.join(self.scratch_dir, 

'testSetup_setupTestGalaxies.db') 

 

self.pointingRA = 50.0 

self.pointingDec = -5.0 

self.radius = 1.0 

makeStarDatabase(filename=self.StarDBName, size=100, 

pointingRA=self.pointingRA, 

pointingDec=self.pointingDec, 

radius=self.radius) 

 

makeGalaxyDatabase(filename=self.GalaxyDBName, size=100, 

pointingRA=self.pointingRA, 

pointingDec=self.pointingDec, 

radius=self.radius) 

 

self.starDBObj = testStarDBObject(driver=self.driver, database= self.StarDBName) 

self.galaxyDBObj = testGalaxyDBObject(driver=self.driver, database=self.GalaxyDBName) 

 

self.obs_metadata = ObservationMetaData(pointingRA=self.pointingRA, 

pointingDec=self.pointingDec, 

boundType='circle', boundLength=self.radius, 

bandpassName='g', mjd=57000.0, 

m5=24.5) 

 

self.obs_metadata_compound = ObservationMetaData(pointingRA=self.pointingRA, 

pointingDec=self.pointingDec, 

boundType='circle', boundLength=self.radius, 

bandpassName=['g', 'i'], mjd=57000.0, 

m5=[24.5, 17.5]) 

 

def tearDown(self): 

185 ↛ 188line 185 didn't jump to line 188, because the condition on line 185 was never false if os.path.exists(self.StarDBName): 

os.unlink(self.StarDBName) 

 

188 ↛ 191line 188 didn't jump to line 191, because the condition on line 188 was never false if os.path.exists(self.GalaxyDBName): 

os.unlink(self.GalaxyDBName) 

 

del self.starDBObj 

del self.galaxyDBObj 

del self.StarDBName 

del self.GalaxyDBName 

del self.pointingRA 

del self.pointingDec 

del self.radius 

del self.obs_metadata 

 

def testExceptions(self): 

""" 

Make sure that setupPhotometryCatalog throws errors when it is supposed to 

""" 

 

class dummyClass(object): 

def __init__(self): 

pass 

 

xx = dummyClass() 

self.assertRaises(RuntimeError, setupPhotometryCatalog, obs_metadata=xx, 

dbConnection=self.starDBObj, catalogClass=testStarCatalog) 

 

self.assertRaises(RuntimeError, setupPhotometryCatalog, obs_metadata=self.obs_metadata, 

dbConnection=xx, catalogClass=testStarCatalog) 

 

self.assertRaises(RuntimeError, setupPhotometryCatalog, obs_metadata=self.obs_metadata, 

dbConnection=self.starDBObj, catalogClass=dummyClass) 

 

def testSetupPhotometry(self): 

""" 

Make sure that catalogs instantiated by setupPhotometryCatalog contain the 

correct columns. 

""" 

 

# test case with a single bandpass 

cat = setupPhotometryCatalog(obs_metadata=self.obs_metadata, dbConnection=self.starDBObj, 

catalogClass=testStarCatalog) 

 

self.assertIn('lsst_g', cat.iter_column_names()) 

self.assertNotIn('lsst_u', cat.iter_column_names()) 

self.assertNotIn('lsst_r', cat.iter_column_names()) 

self.assertNotIn('lsst_i', cat.iter_column_names()) 

self.assertNotIn('lsst_z', cat.iter_column_names()) 

self.assertNotIn('lsst_y', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_g', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_u', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_r', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_i', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_z', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_y', cat.iter_column_names()) 

 

cat = setupPhotometryCatalog(obs_metadata=self.obs_metadata, dbConnection=self.starDBObj, 

catalogClass=testStarCatalog, uncertainty=True) 

 

self.assertIn('lsst_g', cat.iter_column_names()) 

self.assertNotIn('lsst_u', cat.iter_column_names()) 

self.assertNotIn('lsst_r', cat.iter_column_names()) 

self.assertNotIn('lsst_i', cat.iter_column_names()) 

self.assertNotIn('lsst_z', cat.iter_column_names()) 

self.assertNotIn('lsst_y', cat.iter_column_names()) 

self.assertIn('sigma_lsst_g', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_u', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_r', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_i', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_z', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_y', cat.iter_column_names()) 

 

# test case with two bandpasses 

cat = setupPhotometryCatalog(obs_metadata=self.obs_metadata_compound, 

dbConnection=self.starDBObj, catalogClass=testStarCatalog) 

 

self.assertIn('lsst_g', cat.iter_column_names()) 

self.assertIn('lsst_i', cat.iter_column_names()) 

self.assertNotIn('lsst_u', cat.iter_column_names()) 

self.assertNotIn('lsst_r', cat.iter_column_names()) 

self.assertNotIn('lsst_z', cat.iter_column_names()) 

self.assertNotIn('lsst_y', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_g', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_u', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_r', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_i', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_z', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_y', cat.iter_column_names()) 

 

cat = setupPhotometryCatalog(obs_metadata=self.obs_metadata_compound, 

dbConnection=self.starDBObj, catalogClass=testStarCatalog, 

uncertainty=True) 

 

self.assertIn('lsst_g', cat.iter_column_names()) 

self.assertIn('lsst_i', cat.iter_column_names()) 

self.assertNotIn('lsst_u', cat.iter_column_names()) 

self.assertNotIn('lsst_r', cat.iter_column_names()) 

self.assertNotIn('lsst_z', cat.iter_column_names()) 

self.assertNotIn('lsst_y', cat.iter_column_names()) 

self.assertIn('sigma_lsst_g', cat.iter_column_names()) 

self.assertIn('sigma_lsst_i', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_u', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_r', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_z', cat.iter_column_names()) 

self.assertNotIn('sigma_lsst_y', cat.iter_column_names()) 

 

# make sure that class default columns did not get overwritten 

cat = testStarCatalog(self.starDBObj, obs_metadata=self.obs_metadata) 

 

self.assertNotIn('lsst_u', cat.iter_column_names()) 

self.assertNotIn('lsst_g', cat.iter_column_names()) 

self.assertNotIn('lsst_r', cat.iter_column_names()) 

self.assertNotIn('lsst_i', cat.iter_column_names()) 

self.assertNotIn('lsst_z', cat.iter_column_names()) 

self.assertNotIn('lsst_y', cat.iter_column_names()) 

 

def testActualCatalog(self): 

""" 

Make sure that the values written to catalogs that are instantiated using 

setupPhotometryCatalog are correct 

""" 

msgroot = ['failed on stars; ', 'failed on galaxies; '] 

 

testCatClasses = [testStarCatalog, testGalaxyCatalog] 

testCatDBs = [self.starDBObj, self.galaxyDBObj] 

baselineCats = [] 

baselineCats.append(baselineStarCatalog(self.starDBObj, obs_metadata=self.obs_metadata_compound, 

column_outputs=['lsst_g', 'sigma_lsst_g', 

'lsst_i', 'sigma_lsst_i'])) 

 

baselineCats.append(baselineGalaxyCatalog(self.galaxyDBObj, obs_metadata=self.obs_metadata_compound, 

column_outputs=['lsst_g', 'sigma_lsst_g', 

'lsst_i', 'sigma_lsst_i'])) 

 

basedtype = np.dtype([('raObserved', np.float), ('decObserved', np.float), 

('lsst_g', np.float), ('sigma_lsst_g', np.float), 

('lsst_i', np.float), ('sigma_lsst_i', np.float)]) 

 

for (testCatClass, dbo, baselineCat, msgr) in zip(testCatClasses, testCatDBs, baselineCats, msgroot): 

 

testdtype = np.dtype([('raObserved', np.float), ('decObserved', np.float), 

('lsst_g', np.float)]) 

 

testCat = setupPhotometryCatalog(obs_metadata=self.obs_metadata, 

dbConnection=dbo, 

catalogClass=testCatClass) 

 

with lsst.utils.tests.getTempFilePath('.txt') as testName: 

testCat.write_catalog(testName) 

testData = np.genfromtxt(testName, dtype=testdtype, delimiter=',') 

 

with lsst.utils.tests.getTempFilePath('.txt') as baseName: 

baselineCat.write_catalog(baseName) 

baseData = np.genfromtxt(baseName, dtype=basedtype, delimiter=',') 

 

self.assertGreater(len(testData), 0) 

self.assertGreater(len(baseData), 0) 

 

ct = 0 

for b, t in zip(baseData, testData): 

self.assertAlmostEqual(b['lsst_g'], t['lsst_g'], 12, 

msg = '%s single column; %.12e != %.12e' % 

(msgr, b['lsst_g'], t['lsst_g'])) 

ct += 1 

 

self.assertGreater(ct, 0) 

 

testdtype = np.dtype([('raObserved', np.float), ('decObserved', np.float), 

('lsst_g', np.float), ('lsst_i', np.float)]) 

 

testCat = setupPhotometryCatalog(obs_metadata=self.obs_metadata_compound, 

dbConnection=dbo, 

catalogClass=testCatClass) 

with lsst.utils.tests.getTempFilePath('.txt') as testName: 

testCat.write_catalog(testName) 

testData = np.genfromtxt(testName, dtype=testdtype, delimiter=',') 

self.assertGreater(len(testData), 0) 

ct = 0 

for b, t in zip(baseData, testData): 

self.assertAlmostEqual(b['lsst_g'], t['lsst_g'], 12, 

msg = '%s double column; %.12e != %.12e ' % 

(msgr, b['lsst_g'], t['lsst_g'])) 

self.assertAlmostEqual(b['lsst_i'], t['lsst_i'], 12, 

msg = '%s double column; %.12e != %.12e ' % 

(msgr, b['lsst_i'], t['lsst_i'])) 

ct += 1 

 

self.assertGreater(ct, 0) 

 

def testActualCatalogWithUncertainty(self): 

""" 

Make sure that the values written to catalogs that are instantiated using 

setupPhotometryCatalog are correct (include photometric uncertainty) 

""" 

 

msgroot = ['failed on stars; ', 'failed on galaxies; '] 

 

testCatClasses = [testStarCatalog, testGalaxyCatalog] 

testCatDBs = [self.starDBObj, self.galaxyDBObj] 

baselineCats = [] 

 

# need to set up the baseline catalogs with the compound obs_metadata so that they get the 

# correct m5 values for both magnitudes (otherwise, they will use LSST defaults, which 

# disagree with our cartoon test case) 

baselineCats.append(baselineStarCatalog(self.starDBObj, obs_metadata=self.obs_metadata_compound, 

column_outputs=['lsst_g', 'lsst_i', 

'sigma_lsst_g', 'sigma_lsst_i'])) 

 

baselineCats.append(baselineGalaxyCatalog(self.galaxyDBObj, obs_metadata=self.obs_metadata_compound, 

column_outputs=['lsst_g', 'lsst_i', 

'sigma_lsst_g', 'sigma_lsst_i'])) 

 

basedtype = np.dtype([('raObserved', np.float), ('decObserved', np.float), 

('lsst_g', np.float), ('lsst_i', np.float), 

('sigma_lsst_g', np.float), ('sigma_lsst_i', np.float)]) 

 

for (testCatClass, dbo, baselineCat, msgr) in zip(testCatClasses, testCatDBs, baselineCats, msgroot): 

 

testCat = setupPhotometryCatalog(obs_metadata=self.obs_metadata, 

dbConnection=dbo, 

catalogClass=testCatClass, 

uncertainty=True) 

 

testdtype = np.dtype([('raObserved', np.float), ('decObserved', np.float), 

('lsst_g', np.float), ('sigma_lsst_g', np.float)]) 

 

with lsst.utils.tests.getTempFilePath('.txt') as testName: 

testCat.write_catalog(testName) 

testData = np.genfromtxt(testName, dtype=testdtype, delimiter=',') 

with lsst.utils.tests.getTempFilePath('.txt') as baseName: 

baselineCat.write_catalog(baseName) 

baseData = np.genfromtxt(baseName, dtype=basedtype, delimiter=',') 

self.assertGreater(len(testData), 0) 

self.assertGreater(len(baseData), 0) 

 

ct = 0 

for b, t in zip(baseData, testData): 

self.assertAlmostEqual(b['lsst_g'], t['lsst_g'], 12, 

msg = '%s single column; %.12e != %.12e ' % 

(msgr, b['lsst_g'], t['lsst_g'])) 

self.assertAlmostEqual(b['sigma_lsst_g'], t['sigma_lsst_g'], 12, 

msg = '%s sigle column; %.12e != %.12e ' % 

(msgr, b['sigma_lsst_i'], t['sigma_lsst_g'])) 

ct += 1 

 

self.assertGreater(ct, 0) 

 

testdtype = np.dtype([('raObserved', np.float), ('decObserved', np.float), 

('lsst_g', np.float), ('sigma_lsst_g', np.float), 

('lsst_i', np.float), ('sigma_lsst_i', np.float)]) 

 

testCat = setupPhotometryCatalog(obs_metadata=self.obs_metadata_compound, 

dbConnection=dbo, 

catalogClass=testCatClass, 

uncertainty=True) 

with lsst.utils.tests.getTempFilePath('.txt') as testName: 

testCat.write_catalog(testName) 

testData = np.genfromtxt(testName, dtype=testdtype, delimiter=',') 

self.assertGreater(len(testData), 0) 

ct = 0 

for b, t in zip(baseData, testData): 

self.assertAlmostEqual(b['lsst_g'], t['lsst_g'], 12, 

msg = '%s double column; %.12e != %.12e ' % 

(msgr, b['lsst_g'], t['lsst_g'])) 

self.assertAlmostEqual(b['lsst_i'], t['lsst_i'], 12, 

msg = '%s double column; %.12e != %.12e ' % 

(msgr, b['lsst_i'], t['lsst_i'])) 

self.assertAlmostEqual(b['sigma_lsst_g'], t['sigma_lsst_g'], 12, 

msg = '%s double column; %.12e != %.12e ' % 

(msgr, b['sigma_lsst_g'], t['lsst_g'])) 

self.assertAlmostEqual(b['sigma_lsst_i'], t['sigma_lsst_i'], 12, 

msg = '%s double column; %.12e != %.12e ' % 

(msgr, b['sigma_lsst_i'], t['sigma_lsst_i'])) 

ct += 1 

 

self.assertGreater(ct, 0) 

 

 

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

pass 

 

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

lsst.utils.tests.init() 

unittest.main()