Coverage for tests/testCompoundCatalogDBObject.py : 18%

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
11from lsst.sims.utils.CodeUtilities import sims_clean_up
12from lsst.sims.utils import ObservationMetaData
13from lsst.sims.catalogs.db import fileDBObject, CompoundCatalogDBObject, CatalogDBObject
15ROOT = os.path.abspath(os.path.dirname(__file__))
18def setup_module(module):
19 lsst.utils.tests.init()
22class dbClass1(CatalogDBObject):
23 objid = 'class1'
24 idColKey = 'id'
25 tableid = 'test'
26 columns = [('aa', 'a'),
27 ('bb', 'd', str, 20)]
29 dbDefaultValues = {'ee': -1}
32class dbClass2(CatalogDBObject):
33 objid = 'class2'
34 idColKey = 'id'
35 tableid = 'test'
36 columns = [('aa', '2.0*b'),
37 ('bb', 'a')]
39 dbDefaultValues = {'ee': -2}
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')]
50 dbDefaultValues = {'ee': -3}
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')]
61 dbDefaultValues = {'ee': -3}
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')]
72 dbDefaultValues = {'ee': -3}
75class dbClass6(CatalogDBObject):
76 objid = 'class6'
77 idColKey = 'id'
78 tableid = 'test'
79 columns = [('a', None),
80 ('b', None)]
83class specificCompoundObj_otherTest(CompoundCatalogDBObject):
84 _table_restriction = ['otherTest']
87class specificCompoundObj_test(CompoundCatalogDBObject):
88 _table_restriction = ['test']
91class universalCompoundObj(CompoundCatalogDBObject):
92 _table_restriction = ['test', 'otherTest']
95class CompoundCatalogDBObjectTestCase(unittest.TestCase):
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)])
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)
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)
119 cls.baseDir = tempfile.mkdtemp(dir=ROOT, prefix='scratchSpace-')
121 cls.textFileName = os.path.join(cls.baseDir, 'compound_test_data.txt')
122 if os.path.exists(cls.textFileName):
123 os.unlink(cls.textFileName)
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))
130 cls.dbName = os.path.join(cls.baseDir, 'compoundCatalogTestDB.db')
131 if os.path.exists(cls.dbName):
132 os.unlink(cls.dbName)
134 cls.otherDbName = os.path.join(cls.baseDir, 'otherDb.db')
135 if os.path.exists(cls.otherDbName):
136 os.unlink(cls.otherDbName)
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 ])
146 fileDBObject(cls.textFileName, runtable='test',
147 database=cls.dbName, dtype=dtype,
148 idColKey='id')
150 fileDBObject(cls.textFileName, runtable='test',
151 database=cls.otherDbName, dtype=dtype,
152 idColKey='id')
154 fileDBObject(cls.textFileName, runtable='otherTest',
155 database=cls.dbName, dtype=dtype,
156 idColKey='id')
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)
170 def testExceptions(self):
171 """
172 Verify that CompoundCatalogDBObject raises an exception
173 when you violate its API
174 """
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'
182 class testDbClass2(dbClass2):
183 database = self.dbName
184 driver = 'sqlite'
186 db1 = testDbClass1()
187 db2 = testDbClass2()
189 with self.assertRaises(RuntimeError) as context:
190 CompoundCatalogDBObject([db1, db2])
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])
201 # test case where they are querying the same database, but different
202 # tables
204 class testDbClass3(dbClass4):
205 database = self.dbName
206 driver = 'sqlite'
208 class testDbClass4(dbClass2):
209 database = self.dbName
210 driver = 'sqlite'
212 with self.assertRaises(RuntimeError) as context:
213 CompoundCatalogDBObject([testDbClass3, testDbClass4])
215 self.assertIn("['otherTest', 'test']", context.exception.args[0])
217 # test case where the CatalogDBObjects have the same objid
218 class testDbClass5(dbClass4):
219 database = self.dbName
220 driver = 'sqlite'
221 objid = 'dummy'
223 class testDbClass6(dbClass5):
224 database = self.dbName
225 driver = 'sqlite'
226 objid = 'dummy'
228 with self.assertRaises(RuntimeError) as context:
229 CompoundCatalogDBObject([testDbClass5, testDbClass6])
231 self.assertIn("objid dummy is duplicated", context.exception.args[0])
233 # test case where CompoundCatalogDBObject does not support the
234 # tables being queried
235 class testDbClass7(dbClass1):
236 database = self.dbName
237 driver = 'sqlite'
239 class testDbClass8(dbClass2):
240 database = self.dbName
241 driver = 'sqlite'
243 with self.assertRaises(RuntimeError) as context:
244 specificCompoundObj_otherTest([testDbClass7, testDbClass8])
246 msg = "This CompoundCatalogDBObject does not support the table 'test'"
247 self.assertIn(msg, context.exception.args[0])
249 def testCompoundCatalogDBObject_method(self):
250 """
251 Verify that CompoundCatalogDBObject returns the expected
252 columns.
253 """
255 class testDbClass9(dbClass1):
256 database = self.dbName
257 driver = 'sqlite'
259 class testDbClass10(dbClass2):
260 database = self.dbName
261 driver = 'sqlite'
263 class testDbClass11(dbClass3):
264 database = self.dbName
265 driver = 'sqlite'
267 db1 = testDbClass9
268 db2 = testDbClass10
269 db3 = testDbClass11
271 dbList = [db1, db2, db3]
272 compoundDb = CompoundCatalogDBObject(dbList)
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]
279 colNames = numpy.unique([compoundDb.name_map(name) for name in prefixed_colNames])
281 results = compoundDb.query_columns(colnames=colNames)
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)
288 numpy.testing.assert_array_equal(chunk[compoundDb.name_map('%s_bb' % db1.objid)],
289 self.controlArray['d'])
291 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db2.objid)],
292 2.0*self.controlArray['b'],
293 decimal=6)
295 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db2.objid)],
296 self.controlArray['a'],
297 decimal=6)
299 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db3.objid)],
300 self.controlArray['c']-3.0,
301 decimal=6)
303 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db3.objid)],
304 self.controlArray['a'],
305 decimal=6)
307 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_cc' % db3.objid)],
308 3.0*self.controlArray['b'],
309 decimal=6)
311 def testTableRestriction(self):
312 """
313 Verify that _table_restriction works the way it should in CompoundCatalogDBObject
314 """
316 class testDbClass12(dbClass1):
317 database = self.dbName
318 driver = 'sqlite'
320 class testDbClass13(dbClass2):
321 database = self.dbName
322 driver = 'sqlite'
324 db1 = testDbClass12
325 db2 = testDbClass13
326 dbList = [db1, db2]
327 compoundDb = specificCompoundObj_test(dbList)
329 prefix_colNames = ['%s_aa' % db1.objid, '%s_bb' % db1.objid,
330 '%s_aa' % db2.objid, '%s_bb' % db2.objid]
332 colNames = numpy.unique([compoundDb.name_map(name) for name in prefix_colNames])
334 results = compoundDb.query_columns(colnames=colNames)
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)
341 numpy.testing.assert_array_equal(chunk[compoundDb.name_map('%s_bb' % db1.objid)],
342 self.controlArray['d'])
344 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db2.objid)],
345 2.0*self.controlArray['b'],
346 decimal=6)
348 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db2.objid)],
349 self.controlArray['a'],
350 decimal=6)
352 def testUniversalTableRestriction(self):
353 """
354 Verify that _table_restriction with multiple tables also works
355 """
357 class testDbClass14(dbClass1):
358 database = self.dbName
359 driver = 'sqlite'
361 class testDbClass15(dbClass2):
362 database = self.dbName
363 driver = 'sqlite'
365 class testDbClass16(dbClass3):
366 database = self.dbName
367 driver = 'sqlite'
369 db1 = testDbClass14
370 db2 = testDbClass15
371 db3 = testDbClass16
372 dbList = [db1, db2, db3]
373 compoundDb = universalCompoundObj(dbList)
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]
380 colNames = numpy.unique([compoundDb.name_map(name) for name in prefix_colNames])
382 results = compoundDb.query_columns(colnames=colNames)
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)
389 numpy.testing.assert_array_equal(chunk[compoundDb.name_map('%s_bb' % db1.objid)],
390 self.controlArray['d'])
392 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db2.objid)],
393 2.0*self.controlArray['b'],
394 decimal=6)
396 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db2.objid)],
397 self.controlArray['a'],
398 decimal=6)
400 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db3.objid)],
401 self.controlArray['c']-3.0,
402 decimal=6)
404 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db3.objid)],
405 self.controlArray['a'],
406 decimal=6)
408 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_cc' % db3.objid)],
409 3.0*self.controlArray['b'],
410 decimal=6)
412 def testChunks(self):
413 """
414 Verify that CompoundCatalogDBObject handles chunk_size correctly
415 """
417 class testDbClass17(dbClass1):
418 database = self.dbName
419 driver = 'sqlite'
421 class testDbClass18(dbClass2):
422 database = self.dbName
423 driver = 'sqlite'
425 class testDbClass19(dbClass3):
426 database = self.dbName
427 driver = 'sqlite'
429 db1 = testDbClass17
430 db2 = testDbClass18
431 db3 = testDbClass19
432 dbList = [db1, db2, db3]
433 compoundDb = CompoundCatalogDBObject(dbList)
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]
441 colNames = numpy.unique([compoundDb.name_map(name) for name in prefix_colNames])
443 results = compoundDb.query_columns(colnames=colNames, chunk_size=10)
445 ct = 0
447 for chunk in results:
448 ct += len(chunk['%s_aa' % db1.objid])
449 rows = chunk['id']
450 self.assertLessEqual(len(rows), 10)
452 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_aa' % db1.objid)],
453 self.controlArray['a'][rows],
454 decimal=6)
456 numpy.testing.assert_array_equal(chunk[compoundDb.name_map('%s_bb' % db1.objid)],
457 self.controlArray['d'][rows])
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)
463 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db2.objid)],
464 self.controlArray['a'][rows],
465 decimal=6)
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)
471 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_bb' % db3.objid)],
472 self.controlArray['a'][rows],
473 decimal=6)
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)
479 self.assertEqual(ct, 100)
481 def testNoneMapping(self):
482 """
483 Test that Nones are handled correctly in the CatalogDBObject
484 column mappings
485 """
487 class testDbClass20(dbClass1):
488 database = self.dbName
489 driver = 'sqlite'
491 class testDbClass21(dbClass6):
492 database = self.dbName
493 driver = 'sqlite'
495 db1 = testDbClass20
496 db2 = testDbClass21
498 compoundDb = CompoundCatalogDBObject([db1, db2])
500 prefix_colNames = ['%s_aa' % db1.objid, '%s_bb' % db1.objid,
501 '%s_a' % db2.objid, '%s_b' % db2.objid]
503 colNames = numpy.unique([compoundDb.name_map(name) for name in prefix_colNames])
505 results = compoundDb.query_columns(colnames=colNames)
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)
512 numpy.testing.assert_array_equal(chunk[compoundDb.name_map('%s_bb' % db1.objid)],
513 self.controlArray['d'])
515 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_a' % db2.objid)],
516 self.controlArray['a'],
517 decimal=6)
519 numpy.testing.assert_array_almost_equal(chunk[compoundDb.name_map('%s_b' % db2.objid)],
520 self.controlArray['b'],
521 decimal=6)
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')]
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')]
548class CompoundWithObsMetaData(unittest.TestCase):
550 longMessage = True
552 @classmethod
553 def setUpClass(cls):
554 cls.baseDir = tempfile.mkdtemp(dir=ROOT, prefix='scratchSpace-')
556 cls.textFileName = os.path.join(cls.baseDir, 'compound_obs_metadata_text_data.txt')
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
564 dtype = numpy.dtype([
565 ('ra', numpy.float),
566 ('dec', numpy.float),
567 ('mag', numpy.float)
568 ])
570 cls.controlArray = numpy.rec.fromrecords([(r, d, m) for r, d, m in zip(raList, decList, magList)],
571 dtype=dtype)
573 dbDtype = numpy.dtype([
574 ('id', numpy.int),
575 ('ra', numpy.float),
576 ('dec', numpy.float),
577 ('mag', numpy.float)
578 ])
580 if os.path.exists(cls.textFileName):
581 os.unlink(cls.textFileName)
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))
588 cls.dbName = os.path.join(cls.baseDir, 'compound_obs_metadata_db.db')
590 if os.path.exists(cls.dbName):
591 os.unlink(cls.dbName)
593 fileDBObject(cls.textFileName, runtable='test',
594 database=cls.dbName, dtype=dbDtype,
595 idColKey='id')
597 @classmethod
598 def tearDownClass(cls):
599 sims_clean_up()
600 if os.path.exists(cls.textFileName):
601 os.unlink(cls.textFileName)
603 if os.path.exists(cls.dbName):
604 os.unlink(cls.dbName)
606 if os.path.exists(cls.baseDir):
607 shutil.rmtree(cls.baseDir)
609 def testObsMetaData(self):
610 """
611 Test that CompoundCatalogDBObject can handle ObservationMetaData
612 properly
613 """
615 obs = ObservationMetaData(pointingRA = 180.0,
616 pointingDec = 0.0,
617 boundType = 'box',
618 boundLength = (80.0, 25.0),
619 mjd=53580.0)
621 class testDbClass22(testStarDB1):
622 database = self.dbName
623 driver = 'sqlite'
625 class testDbClass23(testStarDB2):
626 database = self.dbName
627 driver = 'sqlite'
629 db1 = testDbClass22
630 db2 = testDbClass23
632 compoundDb = CompoundCatalogDBObject([db1, db2])
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]
640 colnames = numpy.unique([compoundDb.name_map(name) for name in prefix_colnames])
642 results = compoundDb.query_columns(colnames=colnames,
643 obs_metadata=obs)
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)
653 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db1.objid)],
654 self.controlArray['dec'][ix], 10)
656 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db1.objid)],
657 self.controlArray['mag'][ix], 10)
659 self.assertAlmostEqual(line[compoundDb.name_map('%s_raJ2000' % db2.objid)],
660 2.0*self.controlArray['ra'][ix], 10)
662 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db2.objid)],
663 2.0*self.controlArray['dec'][ix], 10)
665 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db2.objid)],
666 2.0*self.controlArray['mag'][ix], 10)
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)
673 bad_rows = [ii for ii in range(self.controlArray.shape[0]) if ii not in good_rows]
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])]
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)
683 def testConstraint(self):
684 """
685 Test that CompoundCatalogDBObject runs correctly with a constraint
686 """
688 class testDbClass24(testStarDB1):
689 database = self.dbName
690 driver = 'sqlite'
692 class testDbClass25(testStarDB2):
693 database = self.dbName
694 driver = 'sqlite'
696 db1 = testDbClass24
697 db2 = testDbClass25
699 compoundDb = CompoundCatalogDBObject([db1, db2])
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]
707 colnames = numpy.unique([compoundDb.name_map(name) for name in prefix_colnames])
709 results = compoundDb.query_columns(colnames=colnames,
710 constraint='mag<11.0')
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)
720 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db1.objid)],
721 self.controlArray['dec'][ix], 10)
723 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db1.objid)],
724 self.controlArray['mag'][ix], 10)
726 self.assertAlmostEqual(line[compoundDb.name_map('%s_raJ2000' % db2.objid)],
727 2.0*self.controlArray['ra'][ix], 10)
729 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db2.objid)],
730 2.0*self.controlArray['dec'][ix], 10)
732 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db2.objid)],
733 2.0*self.controlArray['mag'][ix], 10)
735 self.assertLess(self.controlArray['mag'][ix], 11.0)
737 bad_rows = [ii for ii in range(self.controlArray.shape[0]) if ii not in good_rows]
739 in_bounds = [mm < 11.0 for mm in self.controlArray['mag'][bad_rows]]
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])
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)
757 class testDbClass26(testStarDB1):
758 database = self.dbName
759 driver = 'sqlite'
761 class testDbClass27(testStarDB2):
762 database = self.dbName
763 driver = 'sqlite'
765 db1 = testDbClass26
766 db2 = testDbClass27
768 compoundDb = CompoundCatalogDBObject([db1, db2])
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]
776 colnames = numpy.unique([compoundDb.name_map(name) for name in prefix_colnames])
778 results = compoundDb.query_columns(colnames=colnames,
779 obs_metadata=obs,
780 constraint='mag>15.0')
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)
790 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db1.objid)],
791 self.controlArray['dec'][ix], 10)
793 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db1.objid)],
794 self.controlArray['mag'][ix], 10)
796 self.assertAlmostEqual(line[compoundDb.name_map('%s_raJ2000' % db2.objid)],
797 2.0*self.controlArray['ra'][ix], 10)
799 self.assertAlmostEqual(line[compoundDb.name_map('%s_decJ2000' % db2.objid)],
800 2.0*self.controlArray['dec'][ix], 10)
802 self.assertAlmostEqual(line[compoundDb.name_map('%s_magMod' % db2.objid)],
803 2.0*self.controlArray['mag'][ix], 10)
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)
811 bad_rows = [ii for ii in range(self.controlArray.shape[0]) if ii not in good_rows]
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])]
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])
824class MemoryTestClass(lsst.utils.tests.MemoryTestCase):
825 pass
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()