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 __future__ import with_statement 

2from builtins import zip 

3from builtins import range 

4import unittest 

5import numpy 

6import os 

7import tempfile 

8import shutil 

9import lsst.utils.tests 

10 

11from lsst.sims.utils.CodeUtilities import sims_clean_up 

12from lsst.sims.utils import ObservationMetaData 

13from lsst.sims.catalogs.db import fileDBObject, CompoundCatalogDBObject, CatalogDBObject 

14 

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

16 

17 

18def setup_module(module): 

19 lsst.utils.tests.init() 

20 

21 

22class dbClass1(CatalogDBObject): 

23 objid = 'class1' 

24 idColKey = 'id' 

25 tableid = 'test' 

26 columns = [('aa', 'a'), 

27 ('bb', 'd', str, 20)] 

28 

29 dbDefaultValues = {'ee': -1} 

30 

31 

32class dbClass2(CatalogDBObject): 

33 objid = 'class2' 

34 idColKey = 'id' 

35 tableid = 'test' 

36 columns = [('aa', '2.0*b'), 

37 ('bb', 'a')] 

38 

39 dbDefaultValues = {'ee': -2} 

40 

41 

42class dbClass3(CatalogDBObject): 

43 objid = 'class3' 

44 idColKey = 'id' 

45 tableid = 'test' 

46 columns = [('aa', 'c-3.0'), 

47 ('bb', 'a'), 

48 ('cc', '3.0*b')] 

49 

50 dbDefaultValues = {'ee': -3} 

51 

52 

53class dbClass4(CatalogDBObject): 

54 objid = 'class4' 

55 idColKey = 'id' 

56 tableid = 'otherTest' 

57 columns = [('aa', 'c-3.0'), 

58 ('bb', 'a'), 

59 ('cc', '3.0*b')] 

60 

61 dbDefaultValues = {'ee': -3} 

62 

63 

64class dbClass5(CatalogDBObject): 

65 objid = 'class5' 

66 idColKey = 'id' 

67 tableid = 'otherTest' 

68 columns = [('aa', 'c-3.0'), 

69 ('bb', 'a'), 

70 ('cc', '3.0*b')] 

71 

72 dbDefaultValues = {'ee': -3} 

73 

74 

75class dbClass6(CatalogDBObject): 

76 objid = 'class6' 

77 idColKey = 'id' 

78 tableid = 'test' 

79 columns = [('a', None), 

80 ('b', None)] 

81 

82 

83class specificCompoundObj_otherTest(CompoundCatalogDBObject): 

84 _table_restriction = ['otherTest'] 

85 

86 

87class specificCompoundObj_test(CompoundCatalogDBObject): 

88 _table_restriction = ['test'] 

89 

90 

91class universalCompoundObj(CompoundCatalogDBObject): 

92 _table_restriction = ['test', 'otherTest'] 

93 

94 

95class CompoundCatalogDBObjectTestCase(unittest.TestCase): 

96 

97 @classmethod 

98 def setUpClass(cls): 

99 numpy.random.seed(42) 

100 dtype = numpy.dtype([('a', numpy.float), 

101 ('b', numpy.float), 

102 ('c', numpy.float), 

103 ('d', str, 20)]) 

104 

105 nSamples = 100 

106 aList = numpy.random.random_sample(nSamples)*10.0 

107 bList = numpy.random.random_sample(nSamples)*(-1.0) 

108 cList = numpy.random.random_sample(nSamples)*10.0-5.0 

109 ww = 'a' 

110 dList = [] 

111 for ix in range(nSamples): 

112 ww += 'b' 

113 dList.append(ww) 

114 

115 cls.controlArray = numpy.rec.fromrecords([(aa, bb, cc, dd) 

116 for aa, bb, cc, dd in zip(aList, bList, cList, dList)], 

117 dtype=dtype) 

118 

119 cls.baseDir = tempfile.mkdtemp(dir=ROOT, prefix='scratchSpace-') 

120 

121 cls.textFileName = os.path.join(cls.baseDir, 'compound_test_data.txt') 

122 if os.path.exists(cls.textFileName): 

123 os.unlink(cls.textFileName) 

124 

125 with open(cls.textFileName, 'w') as output: 

126 output.write('# id a b c d\n') 

127 for ix, (aa, bb, cc, dd) in enumerate(zip(aList, bList, cList, dList)): 

128 output.write('%d %e %e %e %s\n' % (ix, aa, bb, cc, dd)) 

129 

130 cls.dbName = os.path.join(cls.baseDir, 'compoundCatalogTestDB.db') 

131 if os.path.exists(cls.dbName): 

132 os.unlink(cls.dbName) 

133 

134 cls.otherDbName = os.path.join(cls.baseDir, 'otherDb.db') 

135 if os.path.exists(cls.otherDbName): 

136 os.unlink(cls.otherDbName) 

137 

138 dtype = numpy.dtype([ 

139 ('id', numpy.int), 

140 ('a', numpy.float), 

141 ('b', numpy.float), 

142 ('c', numpy.float), 

143 ('d', str, 20) 

144 ]) 

145 

146 fileDBObject(cls.textFileName, runtable='test', 

147 database=cls.dbName, dtype=dtype, 

148 idColKey='id') 

149 

150 fileDBObject(cls.textFileName, runtable='test', 

151 database=cls.otherDbName, dtype=dtype, 

152 idColKey='id') 

153 

154 fileDBObject(cls.textFileName, runtable='otherTest', 

155 database=cls.dbName, dtype=dtype, 

156 idColKey='id') 

157 

158 @classmethod 

159 def tearDownClass(cls): 

160 sims_clean_up() 

161 if os.path.exists(cls.textFileName): 

162 os.unlink(cls.textFileName) 

163 if os.path.exists(cls.dbName): 

164 os.unlink(cls.dbName) 

165 if os.path.exists(cls.otherDbName): 

166 os.unlink(cls.otherDbName) 

167 if os.path.exists(cls.baseDir): 

168 shutil.rmtree(cls.baseDir) 

169 

170 def testExceptions(self): 

171 """ 

172 Verify that CompoundCatalogDBObject raises an exception 

173 when you violate its API 

174 """ 

175 

176 # test case where they are querying the same table, but different 

177 # databases 

178 class testDbClass1(dbClass1): 

179 database = self.otherDbName 

180 driver = 'sqlite' 

181 

182 class testDbClass2(dbClass2): 

183 database = self.dbName 

184 driver = 'sqlite' 

185 

186 db1 = testDbClass1() 

187 db2 = testDbClass2() 

188 

189 with self.assertRaises(RuntimeError) as context: 

190 CompoundCatalogDBObject([db1, db2]) 

191 

192 try: 

193 self.assertIn("['%s', '%s']" % (self.otherDbName, self.dbName), 

194 context.exception.args[0]) 

195 except AssertionError: 

196 # in the pybind11 stack, the database names get added to the 

197 # exception message as unicode 

198 self.assertIn("[u'%s', u'%s']" % (self.otherDbName, self.dbName), 

199 context.exception.args[0]) 

200 

201 # test case where they are querying the same database, but different 

202 # tables 

203 

204 class testDbClass3(dbClass4): 

205 database = self.dbName 

206 driver = 'sqlite' 

207 

208 class testDbClass4(dbClass2): 

209 database = self.dbName 

210 driver = 'sqlite' 

211 

212 with self.assertRaises(RuntimeError) as context: 

213 CompoundCatalogDBObject([testDbClass3, testDbClass4]) 

214 

215 self.assertIn("['otherTest', 'test']", context.exception.args[0]) 

216 

217 # test case where the CatalogDBObjects have the same objid 

218 class testDbClass5(dbClass4): 

219 database = self.dbName 

220 driver = 'sqlite' 

221 objid = 'dummy' 

222 

223 class testDbClass6(dbClass5): 

224 database = self.dbName 

225 driver = 'sqlite' 

226 objid = 'dummy' 

227 

228 with self.assertRaises(RuntimeError) as context: 

229 CompoundCatalogDBObject([testDbClass5, testDbClass6]) 

230 

231 self.assertIn("objid dummy is duplicated", context.exception.args[0]) 

232 

233 # test case where CompoundCatalogDBObject does not support the 

234 # tables being queried 

235 class testDbClass7(dbClass1): 

236 database = self.dbName 

237 driver = 'sqlite' 

238 

239 class testDbClass8(dbClass2): 

240 database = self.dbName 

241 driver = 'sqlite' 

242 

243 with self.assertRaises(RuntimeError) as context: 

244 specificCompoundObj_otherTest([testDbClass7, testDbClass8]) 

245 

246 msg = "This CompoundCatalogDBObject does not support the table 'test'" 

247 self.assertIn(msg, context.exception.args[0]) 

248 

249 def testCompoundCatalogDBObject_method(self): 

250 """ 

251 Verify that CompoundCatalogDBObject returns the expected 

252 columns. 

253 """ 

254 

255 class testDbClass9(dbClass1): 

256 database = self.dbName 

257 driver = 'sqlite' 

258 

259 class testDbClass10(dbClass2): 

260 database = self.dbName 

261 driver = 'sqlite' 

262 

263 class testDbClass11(dbClass3): 

264 database = self.dbName 

265 driver = 'sqlite' 

266 

267 db1 = testDbClass9 

268 db2 = testDbClass10 

269 db3 = testDbClass11 

270 

271 dbList = [db1, db2, db3] 

272 compoundDb = CompoundCatalogDBObject(dbList) 

273 

274 prefixed_colNames = ['%s_aa' % db1.objid, '%s_bb' % db1.objid, 

275 '%s_aa' % db2.objid, '%s_bb' % db2.objid, 

276 '%s_aa' % db3.objid, '%s_bb' % db3.objid, 

277 '%s_cc' % db3.objid] 

278 

279 colNames = numpy.unique([compoundDb.name_map(name) for name in prefixed_colNames]) 

280 

281 results = compoundDb.query_columns(colnames=colNames) 

282 

283 for chunk in results: 

284 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db1.objid)], 

285 self.controlArray['a'], 

286 decimal=6) 

287 

288 numpy.testing.assert_array_equal(chunk[compoundDb.name_map('%s_bb' % db1.objid)], 

289 self.controlArray['d']) 

290 

291 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db2.objid)], 

292 2.0*self.controlArray['b'], 

293 decimal=6) 

294 

295 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db2.objid)], 

296 self.controlArray['a'], 

297 decimal=6) 

298 

299 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db3.objid)], 

300 self.controlArray['c']-3.0, 

301 decimal=6) 

302 

303 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db3.objid)], 

304 self.controlArray['a'], 

305 decimal=6) 

306 

307 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_cc' % db3.objid)], 

308 3.0*self.controlArray['b'], 

309 decimal=6) 

310 

311 def testTableRestriction(self): 

312 """ 

313 Verify that _table_restriction works the way it should in CompoundCatalogDBObject 

314 """ 

315 

316 class testDbClass12(dbClass1): 

317 database = self.dbName 

318 driver = 'sqlite' 

319 

320 class testDbClass13(dbClass2): 

321 database = self.dbName 

322 driver = 'sqlite' 

323 

324 db1 = testDbClass12 

325 db2 = testDbClass13 

326 dbList = [db1, db2] 

327 compoundDb = specificCompoundObj_test(dbList) 

328 

329 prefix_colNames = ['%s_aa' % db1.objid, '%s_bb' % db1.objid, 

330 '%s_aa' % db2.objid, '%s_bb' % db2.objid] 

331 

332 colNames = numpy.unique([compoundDb.name_map(name) for name in prefix_colNames]) 

333 

334 results = compoundDb.query_columns(colnames=colNames) 

335 

336 for chunk in results: 

337 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db1.objid)], 

338 self.controlArray['a'], 

339 decimal=6) 

340 

341 numpy.testing.assert_array_equal(chunk[compoundDb.name_map('%s_bb' % db1.objid)], 

342 self.controlArray['d']) 

343 

344 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db2.objid)], 

345 2.0*self.controlArray['b'], 

346 decimal=6) 

347 

348 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db2.objid)], 

349 self.controlArray['a'], 

350 decimal=6) 

351 

352 def testUniversalTableRestriction(self): 

353 """ 

354 Verify that _table_restriction with multiple tables also works 

355 """ 

356 

357 class testDbClass14(dbClass1): 

358 database = self.dbName 

359 driver = 'sqlite' 

360 

361 class testDbClass15(dbClass2): 

362 database = self.dbName 

363 driver = 'sqlite' 

364 

365 class testDbClass16(dbClass3): 

366 database = self.dbName 

367 driver = 'sqlite' 

368 

369 db1 = testDbClass14 

370 db2 = testDbClass15 

371 db3 = testDbClass16 

372 dbList = [db1, db2, db3] 

373 compoundDb = universalCompoundObj(dbList) 

374 

375 prefix_colNames = ['%s_aa' % db1.objid, '%s_bb' % db1.objid, 

376 '%s_aa' % db2.objid, '%s_bb' % db2.objid, 

377 '%s_aa' % db3.objid, '%s_bb' % db3.objid, 

378 '%s_cc' % db3.objid] 

379 

380 colNames = numpy.unique([compoundDb.name_map(name) for name in prefix_colNames]) 

381 

382 results = compoundDb.query_columns(colnames=colNames) 

383 

384 for chunk in results: 

385 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db1.objid)], 

386 self.controlArray['a'], 

387 decimal=6) 

388 

389 numpy.testing.assert_array_equal(chunk[compoundDb.name_map('%s_bb' % db1.objid)], 

390 self.controlArray['d']) 

391 

392 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db2.objid)], 

393 2.0*self.controlArray['b'], 

394 decimal=6) 

395 

396 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db2.objid)], 

397 self.controlArray['a'], 

398 decimal=6) 

399 

400 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db3.objid)], 

401 self.controlArray['c']-3.0, 

402 decimal=6) 

403 

404 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db3.objid)], 

405 self.controlArray['a'], 

406 decimal=6) 

407 

408 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_cc' % db3.objid)], 

409 3.0*self.controlArray['b'], 

410 decimal=6) 

411 

412 def testChunks(self): 

413 """ 

414 Verify that CompoundCatalogDBObject handles chunk_size correctly 

415 """ 

416 

417 class testDbClass17(dbClass1): 

418 database = self.dbName 

419 driver = 'sqlite' 

420 

421 class testDbClass18(dbClass2): 

422 database = self.dbName 

423 driver = 'sqlite' 

424 

425 class testDbClass19(dbClass3): 

426 database = self.dbName 

427 driver = 'sqlite' 

428 

429 db1 = testDbClass17 

430 db2 = testDbClass18 

431 db3 = testDbClass19 

432 dbList = [db1, db2, db3] 

433 compoundDb = CompoundCatalogDBObject(dbList) 

434 

435 prefix_colNames = ['id', 

436 '%s_aa' % db1.objid, '%s_bb' % db1.objid, 

437 '%s_aa' % db2.objid, '%s_bb' % db2.objid, 

438 '%s_aa' % db3.objid, '%s_bb' % db3.objid, 

439 '%s_cc' % db3.objid] 

440 

441 colNames = numpy.unique([compoundDb.name_map(name) for name in prefix_colNames]) 

442 

443 results = compoundDb.query_columns(colnames=colNames, chunk_size=10) 

444 

445 ct = 0 

446 

447 for chunk in results: 

448 ct += len(chunk['%s_aa' % db1.objid]) 

449 rows = chunk['id'] 

450 self.assertLessEqual(len(rows), 10) 

451 

452 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db1.objid)], 

453 self.controlArray['a'][rows], 

454 decimal=6) 

455 

456 numpy.testing.assert_array_equal(chunk[compoundDb.name_map('%s_bb' % db1.objid)], 

457 self.controlArray['d'][rows]) 

458 

459 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db2.objid)], 

460 2.0*self.controlArray['b'][rows], 

461 decimal=6) 

462 

463 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db2.objid)], 

464 self.controlArray['a'][rows], 

465 decimal=6) 

466 

467 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db3.objid)], 

468 self.controlArray['c'][rows]-3.0, 

469 decimal=6) 

470 

471 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db3.objid)], 

472 self.controlArray['a'][rows], 

473 decimal=6) 

474 

475 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_cc' % db3.objid)], 

476 3.0*self.controlArray['b'][rows], 

477 decimal=6) 

478 

479 self.assertEqual(ct, 100) 

480 

481 def testNoneMapping(self): 

482 """ 

483 Test that Nones are handled correctly in the CatalogDBObject 

484 column mappings 

485 """ 

486 

487 class testDbClass20(dbClass1): 

488 database = self.dbName 

489 driver = 'sqlite' 

490 

491 class testDbClass21(dbClass6): 

492 database = self.dbName 

493 driver = 'sqlite' 

494 

495 db1 = testDbClass20 

496 db2 = testDbClass21 

497 

498 compoundDb = CompoundCatalogDBObject([db1, db2]) 

499 

500 prefix_colNames = ['%s_aa' % db1.objid, '%s_bb' % db1.objid, 

501 '%s_a' % db2.objid, '%s_b' % db2.objid] 

502 

503 colNames = numpy.unique([compoundDb.name_map(name) for name in prefix_colNames]) 

504 

505 results = compoundDb.query_columns(colnames=colNames) 

506 

507 for chunk in results: 

508 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db1.objid)], 

509 self.controlArray['a'], 

510 decimal=6) 

511 

512 numpy.testing.assert_array_equal(chunk[compoundDb.name_map('%s_bb' % db1.objid)], 

513 self.controlArray['d']) 

514 

515 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_a' % db2.objid)], 

516 self.controlArray['a'], 

517 decimal=6) 

518 

519 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_b' % db2.objid)], 

520 self.controlArray['b'], 

521 decimal=6) 

522 

523 

524class testStarDB1(CatalogDBObject): 

525 tableid = 'test' 

526 raColName = 'ra' 

527 decColName = 'dec' 

528 idColKey = 'id' 

529 objid = 'testStar1' 

530 columns = [('id', None), 

531 ('raJ2000', 'ra'), 

532 ('decJ2000', 'dec'), 

533 ('magMod', 'mag')] 

534 

535 

536class testStarDB2(CatalogDBObject): 

537 tableid = 'test' 

538 raColName = 'ra' 

539 decColName = 'dec' 

540 idColKey = 'id' 

541 objid = 'testStar2' 

542 columns = [('id', None), 

543 ('raJ2000', '2.0*ra'), 

544 ('decJ2000', '2.0*dec'), 

545 ('magMod', '2.0*mag')] 

546 

547 

548class CompoundWithObsMetaData(unittest.TestCase): 

549 

550 longMessage = True 

551 

552 @classmethod 

553 def setUpClass(cls): 

554 cls.baseDir = tempfile.mkdtemp(dir=ROOT, prefix='scratchSpace-') 

555 

556 cls.textFileName = os.path.join(cls.baseDir, 'compound_obs_metadata_text_data.txt') 

557 

558 numpy.random.seed(42) 

559 nSamples = 100 

560 raList = numpy.random.random_sample(nSamples)*360.0 

561 decList = numpy.random.random_sample(nSamples)*180.0 - 90.0 

562 magList = numpy.random.random_sample(nSamples)*15.0 + 7.0 

563 

564 dtype = numpy.dtype([ 

565 ('ra', numpy.float), 

566 ('dec', numpy.float), 

567 ('mag', numpy.float) 

568 ]) 

569 

570 cls.controlArray = numpy.rec.fromrecords([(r, d, m) for r, d, m in zip(raList, decList, magList)], 

571 dtype=dtype) 

572 

573 dbDtype = numpy.dtype([ 

574 ('id', numpy.int), 

575 ('ra', numpy.float), 

576 ('dec', numpy.float), 

577 ('mag', numpy.float) 

578 ]) 

579 

580 if os.path.exists(cls.textFileName): 

581 os.unlink(cls.textFileName) 

582 

583 with open(cls.textFileName, 'w') as output: 

584 output.write('# id ra dec mag\n') 

585 for ix, (r, d, m) in enumerate(zip(raList, decList, magList)): 

586 output.write('%d %.20f %.20f %.20f\n' % (ix, r, d, m)) 

587 

588 cls.dbName = os.path.join(cls.baseDir, 'compound_obs_metadata_db.db') 

589 

590 if os.path.exists(cls.dbName): 

591 os.unlink(cls.dbName) 

592 

593 fileDBObject(cls.textFileName, runtable='test', 

594 database=cls.dbName, dtype=dbDtype, 

595 idColKey='id') 

596 

597 @classmethod 

598 def tearDownClass(cls): 

599 sims_clean_up() 

600 if os.path.exists(cls.textFileName): 

601 os.unlink(cls.textFileName) 

602 

603 if os.path.exists(cls.dbName): 

604 os.unlink(cls.dbName) 

605 

606 if os.path.exists(cls.baseDir): 

607 shutil.rmtree(cls.baseDir) 

608 

609 def testObsMetaData(self): 

610 """ 

611 Test that CompoundCatalogDBObject can handle ObservationMetaData 

612 properly 

613 """ 

614 

615 obs = ObservationMetaData(pointingRA = 180.0, 

616 pointingDec = 0.0, 

617 boundType = 'box', 

618 boundLength = (80.0, 25.0), 

619 mjd=53580.0) 

620 

621 class testDbClass22(testStarDB1): 

622 database = self.dbName 

623 driver = 'sqlite' 

624 

625 class testDbClass23(testStarDB2): 

626 database = self.dbName 

627 driver = 'sqlite' 

628 

629 db1 = testDbClass22 

630 db2 = testDbClass23 

631 

632 compoundDb = CompoundCatalogDBObject([db1, db2]) 

633 

634 prefix_colnames = ['%s_id' % db1.objid, 

635 '%s_raJ2000' % db1.objid, '%s_decJ2000' % db1.objid, 

636 '%s_magMod' % db1.objid, 

637 '%s_raJ2000' % db2.objid, '%s_decJ2000' % db2.objid, 

638 '%s_magMod' % db2.objid] 

639 

640 colnames = numpy.unique([compoundDb.name_map(name) for name in prefix_colnames]) 

641 

642 results = compoundDb.query_columns(colnames=colnames, 

643 obs_metadata=obs) 

644 

645 good_rows = [] 

646 for chunk in results: 

647 for line in chunk: 

648 ix = int(line['id']) 

649 good_rows.append(ix) 

650 self.assertAlmostEqual(line[compoundDb.name_map('%s_raJ2000' % db1.objid)], 

651 self.controlArray['ra'][ix], 10) 

652 

653 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db1.objid)], 

654 self.controlArray['dec'][ix], 10) 

655 

656 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db1.objid)], 

657 self.controlArray['mag'][ix], 10) 

658 

659 self.assertAlmostEqual(line[compoundDb.name_map('%s_raJ2000' % db2.objid)], 

660 2.0*self.controlArray['ra'][ix], 10) 

661 

662 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db2.objid)], 

663 2.0*self.controlArray['dec'][ix], 10) 

664 

665 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db2.objid)], 

666 2.0*self.controlArray['mag'][ix], 10) 

667 

668 self.assertGreater(self.controlArray['ra'][ix], 100.0) 

669 self.assertLess(self.controlArray['ra'][ix], 260.0) 

670 self.assertGreater(self.controlArray['dec'][ix], -25.0) 

671 self.assertLess(self.controlArray['dec'][ix], 25.0) 

672 

673 bad_rows = [ii for ii in range(self.controlArray.shape[0]) if ii not in good_rows] 

674 

675 in_bounds = [rr > 100.0 and rr < 260.0 and dd > -25.0 and dd < 25.0 

676 for (rr, dd) in zip(self.controlArray['ra'][bad_rows], 

677 self.controlArray['dec'][bad_rows])] 

678 

679 self.assertNotIn(True, in_bounds, msg='failed to build bad_rows') 

680 self.assertGreater(len(good_rows), 0) 

681 self.assertGreater(len(bad_rows), 0) 

682 

683 def testConstraint(self): 

684 """ 

685 Test that CompoundCatalogDBObject runs correctly with a constraint 

686 """ 

687 

688 class testDbClass24(testStarDB1): 

689 database = self.dbName 

690 driver = 'sqlite' 

691 

692 class testDbClass25(testStarDB2): 

693 database = self.dbName 

694 driver = 'sqlite' 

695 

696 db1 = testDbClass24 

697 db2 = testDbClass25 

698 

699 compoundDb = CompoundCatalogDBObject([db1, db2]) 

700 

701 prefix_colnames = ['%s_id' % db1.objid, 

702 '%s_raJ2000' % db1.objid, '%s_decJ2000' % db1.objid, 

703 '%s_magMod' % db1.objid, 

704 '%s_raJ2000' % db2.objid, '%s_decJ2000' % db2.objid, 

705 '%s_magMod' % db2.objid] 

706 

707 colnames = numpy.unique([compoundDb.name_map(name) for name in prefix_colnames]) 

708 

709 results = compoundDb.query_columns(colnames=colnames, 

710 constraint='mag<11.0') 

711 

712 good_rows = [] 

713 for chunk in results: 

714 for line in chunk: 

715 ix = int(line['id']) 

716 good_rows.append(ix) 

717 self.assertAlmostEqual(line[compoundDb.name_map('%s_raJ2000' % db1.objid)], 

718 self.controlArray['ra'][ix], 10) 

719 

720 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db1.objid)], 

721 self.controlArray['dec'][ix], 10) 

722 

723 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db1.objid)], 

724 self.controlArray['mag'][ix], 10) 

725 

726 self.assertAlmostEqual(line[compoundDb.name_map('%s_raJ2000' % db2.objid)], 

727 2.0*self.controlArray['ra'][ix], 10) 

728 

729 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db2.objid)], 

730 2.0*self.controlArray['dec'][ix], 10) 

731 

732 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db2.objid)], 

733 2.0*self.controlArray['mag'][ix], 10) 

734 

735 self.assertLess(self.controlArray['mag'][ix], 11.0) 

736 

737 bad_rows = [ii for ii in range(self.controlArray.shape[0]) if ii not in good_rows] 

738 

739 in_bounds = [mm < 11.0 for mm in self.controlArray['mag'][bad_rows]] 

740 

741 self.assertNotIn(True, in_bounds, msg='failed to build bad_rows') 

742 self.assertGreater(len(good_rows), 0) 

743 self.assertGreater(len(bad_rows), 0) 

744 self.assertEqual(len(good_rows)+len(bad_rows), self.controlArray.shape[0]) 

745 

746 def testObsMetadataAndConstraint(self): 

747 """ 

748 Test that CompoundCatalogDBObject correctly handles an ObservationMetaData 

749 and a constraint at the same time 

750 """ 

751 obs = ObservationMetaData(pointingRA = 180.0, 

752 pointingDec = 0.0, 

753 boundType = 'box', 

754 boundLength = (80.0, 25.0), 

755 mjd=53580.0) 

756 

757 class testDbClass26(testStarDB1): 

758 database = self.dbName 

759 driver = 'sqlite' 

760 

761 class testDbClass27(testStarDB2): 

762 database = self.dbName 

763 driver = 'sqlite' 

764 

765 db1 = testDbClass26 

766 db2 = testDbClass27 

767 

768 compoundDb = CompoundCatalogDBObject([db1, db2]) 

769 

770 prefix_colnames = ['%s_id' % db1.objid, 

771 '%s_raJ2000' % db1.objid, '%s_decJ2000' % db1.objid, 

772 '%s_magMod' % db1.objid, 

773 '%s_raJ2000' % db2.objid, '%s_decJ2000' % db2.objid, 

774 '%s_magMod' % db2.objid] 

775 

776 colnames = numpy.unique([compoundDb.name_map(name) for name in prefix_colnames]) 

777 

778 results = compoundDb.query_columns(colnames=colnames, 

779 obs_metadata=obs, 

780 constraint='mag>15.0') 

781 

782 good_rows = [] 

783 for chunk in results: 

784 for line in chunk: 

785 ix = int(line['id']) 

786 good_rows.append(ix) 

787 self.assertAlmostEqual(line[compoundDb.name_map('%s_raJ2000' % db1.objid)], 

788 self.controlArray['ra'][ix], 10) 

789 

790 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db1.objid)], 

791 self.controlArray['dec'][ix], 10) 

792 

793 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db1.objid)], 

794 self.controlArray['mag'][ix], 10) 

795 

796 self.assertAlmostEqual(line[compoundDb.name_map('%s_raJ2000' % db2.objid)], 

797 2.0*self.controlArray['ra'][ix], 10) 

798 

799 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db2.objid)], 

800 2.0*self.controlArray['dec'][ix], 10) 

801 

802 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db2.objid)], 

803 2.0*self.controlArray['mag'][ix], 10) 

804 

805 self.assertGreater(self.controlArray['ra'][ix], 100.0) 

806 self.assertLess(self.controlArray['ra'][ix], 260.0) 

807 self.assertGreater(self.controlArray['dec'][ix], -25.0) 

808 self.assertLess(self.controlArray['dec'][ix], 25.0) 

809 self.assertGreater(self.controlArray['mag'][ix], 15.0) 

810 

811 bad_rows = [ii for ii in range(self.controlArray.shape[0]) if ii not in good_rows] 

812 

813 in_bounds = [rr > 100.0 and rr < 260.0 and dd > -25.0 and dd < 25.0 and mm > 150.0 

814 for (rr, dd, mm) in zip(self.controlArray['ra'][bad_rows], 

815 self.controlArray['dec'][bad_rows], 

816 self.controlArray['mag'][bad_rows])] 

817 

818 self.assertNotIn(True, in_bounds, msg='failed to build bad_rows') 

819 self.assertGreater(len(good_rows), 0) 

820 self.assertGreater(len(bad_rows), 0) 

821 self.assertEqual(len(good_rows)+len(bad_rows), self.controlArray.shape[0]) 

822 

823 

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

825 pass 

826 

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

828 lsst.utils.tests.init() 

829 unittest.main()