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

1from builtins import zip 

2from builtins import object 

3import numpy as np 

4import tempfile 

5import shutil 

6 

7import os 

8import unittest 

9import lsst 

10import lsst.utils.tests 

11from lsst.utils import getPackageDir 

12from lsst.sims.utils.CodeUtilities import sims_clean_up 

13from lsst.sims.utils import ObservationMetaData 

14from lsst.sims.catalogs.db import CatalogDBObject 

15from lsst.sims.catalogs.definitions import InstanceCatalog 

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

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

18from lsst.sims.catUtils.utils import setupPhotometryCatalog 

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

20 

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

22 

23 

24def setup_module(module): 

25 lsst.utils.tests.init() 

26 

27 

28class testStarCatalog(InstanceCatalog, AstrometryStars, PhotometryStars): 

29 """ 

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

31 where it will be given photometry columns 

32 """ 

33 catalog_type = __file__ + "test_star_catalog" 

34 column_outputs = ['raObserved', 'decObserved'] 

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

36 

37 

38class baselineStarCatalog(InstanceCatalog, AstrometryStars, PhotometryStars): 

39 """ 

40 Baseline photometry catalog against which to compare testStarCatalog 

41 """ 

42 catalog_type = __file__ + 'baseline_star_catalog' 

43 column_outputs = ['raObserved', 'decObserved'] 

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

45 

46 

47class testGalaxyCatalog(InstanceCatalog, AstrometryGalaxies, PhotometryGalaxies): 

48 """ 

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

50 where it will be given photometry columns 

51 """ 

52 catalog_type = __file__ + 'test_galaxy_catalog' 

53 column_outputs = ['raObserved', 'decObserved'] 

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

55 

56 

57class baselineGalaxyCatalog(InstanceCatalog, AstrometryGalaxies, PhotometryGalaxies): 

58 """ 

59 Baseline photometry catalog against which to compare testGalaxyCatalog 

60 """ 

61 catalog_type = __file__ + 'baseline_galaxy_catalog' 

62 column_outputs = ['raObserved', 'decObserved'] 

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

64 

65 

66class testStarDBObject(CatalogDBObject): 

67 """ 

68 CatalogDBObject to map our test database of stars 

69 """ 

70 tableid = 'StarAllForceseek' 

71 idColKey = 'id' 

72 raColName = 'ra' 

73 decColName = 'decl' 

74 objectTypeId = 49 

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

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

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

78 ('magNorm', None), 

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

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

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

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

83 ('radialVelocity', 'vrad'), 

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

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

86 

87 

88class testGalaxyDBObject(CatalogDBObject): 

89 

90 #: This is the base table for the galaxies 

91 # tableid = 'final_clone_db' 

92 tableid = 'galaxy' 

93 idColKey = 'galtileid' 

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

95 decColName = 'dec' 

96 objectTypeId = 51 

97 

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

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

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

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

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

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

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

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

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

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

108 ('magNormBulge', 'magnorm_bulge'), 

109 ('magNormDisk', 'magnorm_disk'), 

110 ('magNormAgn', 'magnorm_agn'), 

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

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

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

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

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

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

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

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

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

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

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

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

123 ('internalAvBulge', 'av_b'), 

124 ('internalRvBulge', 'rv_b'), 

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

126 ('internalAvDisk', 'av_d'), 

127 ('internalRvDisk', 'rv_d'), 

128 ('lsst_u', 'u_ab'), 

129 ('lsst_g', 'g_ab'), 

130 ('lsst_r', 'r_ab'), 

131 ('lsst_i', 'i_ab'), 

132 ('lsst_z', 'z_ab'), 

133 ('lsst_y', 'y_ab')] 

134 

135 

136class InstanceCatalogSetupUnittest(unittest.TestCase): 

137 

138 @classmethod 

139 def setUpClass(cls): 

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

141 

142 @classmethod 

143 def tearDownClass(cls): 

144 sims_clean_up() 

145 if os.path.exists(cls.scratch_dir): 

146 shutil.rmtree(cls.scratch_dir, ignore_errors=True) 

147 

148 def setUp(self): 

149 self.driver = 'sqlite' 

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

151 'testSetup_setupTestStars.db') 

152 

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

154 'testSetup_setupTestGalaxies.db') 

155 

156 self.pointingRA = 50.0 

157 self.pointingDec = -5.0 

158 self.radius = 1.0 

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

160 pointingRA=self.pointingRA, 

161 pointingDec=self.pointingDec, 

162 radius=self.radius) 

163 

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

165 pointingRA=self.pointingRA, 

166 pointingDec=self.pointingDec, 

167 radius=self.radius) 

168 

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

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

171 

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

173 pointingDec=self.pointingDec, 

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

175 bandpassName='g', mjd=57000.0, 

176 m5=24.5) 

177 

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

179 pointingDec=self.pointingDec, 

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

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

182 m5=[24.5, 17.5]) 

183 

184 def tearDown(self): 

185 if os.path.exists(self.StarDBName): 

186 os.unlink(self.StarDBName) 

187 

188 if os.path.exists(self.GalaxyDBName): 

189 os.unlink(self.GalaxyDBName) 

190 

191 del self.starDBObj 

192 del self.galaxyDBObj 

193 del self.StarDBName 

194 del self.GalaxyDBName 

195 del self.pointingRA 

196 del self.pointingDec 

197 del self.radius 

198 del self.obs_metadata 

199 

200 def testExceptions(self): 

201 """ 

202 Make sure that setupPhotometryCatalog throws errors when it is supposed to 

203 """ 

204 

205 class dummyClass(object): 

206 def __init__(self): 

207 pass 

208 

209 xx = dummyClass() 

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

211 dbConnection=self.starDBObj, catalogClass=testStarCatalog) 

212 

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

214 dbConnection=xx, catalogClass=testStarCatalog) 

215 

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

217 dbConnection=self.starDBObj, catalogClass=dummyClass) 

218 

219 def testSetupPhotometry(self): 

220 """ 

221 Make sure that catalogs instantiated by setupPhotometryCatalog contain the 

222 correct columns. 

223 """ 

224 

225 # test case with a single bandpass 

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

227 catalogClass=testStarCatalog) 

228 

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

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

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

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

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

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

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

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

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

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

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

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

241 

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

243 catalogClass=testStarCatalog, uncertainty=True) 

244 

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

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

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

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

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

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

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

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

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

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

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

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

257 

258 # test case with two bandpasses 

259 cat = setupPhotometryCatalog(obs_metadata=self.obs_metadata_compound, 

260 dbConnection=self.starDBObj, catalogClass=testStarCatalog) 

261 

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

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

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

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

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

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

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

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

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

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

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

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

274 

275 cat = setupPhotometryCatalog(obs_metadata=self.obs_metadata_compound, 

276 dbConnection=self.starDBObj, catalogClass=testStarCatalog, 

277 uncertainty=True) 

278 

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

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

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

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

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

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

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

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

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

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

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

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

291 

292 # make sure that class default columns did not get overwritten 

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

294 

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

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

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

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

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

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

301 

302 def testActualCatalog(self): 

303 """ 

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

305 setupPhotometryCatalog are correct 

306 """ 

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

308 

309 testCatClasses = [testStarCatalog, testGalaxyCatalog] 

310 testCatDBs = [self.starDBObj, self.galaxyDBObj] 

311 baselineCats = [] 

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

313 column_outputs=['lsst_g', 'sigma_lsst_g', 

314 'lsst_i', 'sigma_lsst_i'])) 

315 

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

317 column_outputs=['lsst_g', 'sigma_lsst_g', 

318 'lsst_i', 'sigma_lsst_i'])) 

319 

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

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

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

323 

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

325 

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

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

328 

329 testCat = setupPhotometryCatalog(obs_metadata=self.obs_metadata, 

330 dbConnection=dbo, 

331 catalogClass=testCatClass) 

332 

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

334 testCat.write_catalog(testName) 

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

336 

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

338 baselineCat.write_catalog(baseName) 

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

340 

341 self.assertGreater(len(testData), 0) 

342 self.assertGreater(len(baseData), 0) 

343 

344 ct = 0 

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

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

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

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

349 ct += 1 

350 

351 self.assertGreater(ct, 0) 

352 

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

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

355 

356 testCat = setupPhotometryCatalog(obs_metadata=self.obs_metadata_compound, 

357 dbConnection=dbo, 

358 catalogClass=testCatClass) 

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

360 testCat.write_catalog(testName) 

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

362 self.assertGreater(len(testData), 0) 

363 ct = 0 

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

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

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

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

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

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

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

371 ct += 1 

372 

373 self.assertGreater(ct, 0) 

374 

375 def testActualCatalogWithUncertainty(self): 

376 """ 

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

378 setupPhotometryCatalog are correct (include photometric uncertainty) 

379 """ 

380 

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

382 

383 testCatClasses = [testStarCatalog, testGalaxyCatalog] 

384 testCatDBs = [self.starDBObj, self.galaxyDBObj] 

385 baselineCats = [] 

386 

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

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

389 # disagree with our cartoon test case) 

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

391 column_outputs=['lsst_g', 'lsst_i', 

392 'sigma_lsst_g', 'sigma_lsst_i'])) 

393 

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

395 column_outputs=['lsst_g', 'lsst_i', 

396 'sigma_lsst_g', 'sigma_lsst_i'])) 

397 

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

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

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

401 

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

403 

404 testCat = setupPhotometryCatalog(obs_metadata=self.obs_metadata, 

405 dbConnection=dbo, 

406 catalogClass=testCatClass, 

407 uncertainty=True) 

408 

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

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

411 

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

413 testCat.write_catalog(testName) 

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

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

416 baselineCat.write_catalog(baseName) 

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

418 self.assertGreater(len(testData), 0) 

419 self.assertGreater(len(baseData), 0) 

420 

421 ct = 0 

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

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

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

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

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

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

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

429 ct += 1 

430 

431 self.assertGreater(ct, 0) 

432 

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

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

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

436 

437 testCat = setupPhotometryCatalog(obs_metadata=self.obs_metadata_compound, 

438 dbConnection=dbo, 

439 catalogClass=testCatClass, 

440 uncertainty=True) 

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

442 testCat.write_catalog(testName) 

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

444 self.assertGreater(len(testData), 0) 

445 ct = 0 

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

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

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

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

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

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

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

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

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

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

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

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

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

459 ct += 1 

460 

461 self.assertGreater(ct, 0) 

462 

463 

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

465 pass 

466 

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

468 lsst.utils.tests.init() 

469 unittest.main()