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

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

from __future__ import with_statement 

from builtins import zip 

from builtins import range 

import unittest 

import numpy 

import os 

import tempfile 

import shutil 

import lsst.utils.tests 

 

from lsst.sims.utils.CodeUtilities import sims_clean_up 

from lsst.sims.utils import ObservationMetaData 

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

 

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

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class dbClass1(CatalogDBObject): 

objid = 'class1' 

idColKey = 'id' 

tableid = 'test' 

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

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

 

dbDefaultValues = {'ee': -1} 

 

 

class dbClass2(CatalogDBObject): 

objid = 'class2' 

idColKey = 'id' 

tableid = 'test' 

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

('bb', 'a')] 

 

dbDefaultValues = {'ee': -2} 

 

 

class dbClass3(CatalogDBObject): 

objid = 'class3' 

idColKey = 'id' 

tableid = 'test' 

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

('bb', 'a'), 

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

 

dbDefaultValues = {'ee': -3} 

 

 

class dbClass4(CatalogDBObject): 

objid = 'class4' 

idColKey = 'id' 

tableid = 'otherTest' 

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

('bb', 'a'), 

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

 

dbDefaultValues = {'ee': -3} 

 

 

class dbClass5(CatalogDBObject): 

objid = 'class5' 

idColKey = 'id' 

tableid = 'otherTest' 

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

('bb', 'a'), 

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

 

dbDefaultValues = {'ee': -3} 

 

 

class dbClass6(CatalogDBObject): 

objid = 'class6' 

idColKey = 'id' 

tableid = 'test' 

columns = [('a', None), 

('b', None)] 

 

 

class specificCompoundObj_otherTest(CompoundCatalogDBObject): 

_table_restriction = ['otherTest'] 

 

 

class specificCompoundObj_test(CompoundCatalogDBObject): 

_table_restriction = ['test'] 

 

 

class universalCompoundObj(CompoundCatalogDBObject): 

_table_restriction = ['test', 'otherTest'] 

 

 

class CompoundCatalogDBObjectTestCase(unittest.TestCase): 

 

@classmethod 

def setUpClass(cls): 

numpy.random.seed(42) 

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

('b', numpy.float), 

('c', numpy.float), 

('d', str, 20)]) 

 

nSamples = 100 

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

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

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

ww = 'a' 

dList = [] 

for ix in range(nSamples): 

ww += 'b' 

dList.append(ww) 

 

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

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

dtype=dtype) 

 

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

 

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

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

os.unlink(cls.textFileName) 

 

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

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

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

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

 

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

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

os.unlink(cls.dbName) 

 

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

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

os.unlink(cls.otherDbName) 

 

dtype = numpy.dtype([ 

('id', numpy.int), 

('a', numpy.float), 

('b', numpy.float), 

('c', numpy.float), 

('d', str, 20) 

]) 

 

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

database=cls.dbName, dtype=dtype, 

idColKey='id') 

 

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

database=cls.otherDbName, dtype=dtype, 

idColKey='id') 

 

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

database=cls.dbName, dtype=dtype, 

idColKey='id') 

 

@classmethod 

def tearDownClass(cls): 

sims_clean_up() 

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

os.unlink(cls.textFileName) 

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

os.unlink(cls.dbName) 

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

os.unlink(cls.otherDbName) 

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

shutil.rmtree(cls.baseDir) 

 

def testExceptions(self): 

""" 

Verify that CompoundCatalogDBObject raises an exception 

when you violate its API 

""" 

 

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

# databases 

class testDbClass1(dbClass1): 

database = self.otherDbName 

driver = 'sqlite' 

 

class testDbClass2(dbClass2): 

database = self.dbName 

driver = 'sqlite' 

 

db1 = testDbClass1() 

db2 = testDbClass2() 

 

with self.assertRaises(RuntimeError) as context: 

CompoundCatalogDBObject([db1, db2]) 

 

try: 

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

context.exception.args[0]) 

except AssertionError: 

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

# exception message as unicode 

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

context.exception.args[0]) 

 

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

# tables 

 

class testDbClass3(dbClass4): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass4(dbClass2): 

database = self.dbName 

driver = 'sqlite' 

 

with self.assertRaises(RuntimeError) as context: 

CompoundCatalogDBObject([testDbClass3, testDbClass4]) 

 

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

 

# test case where the CatalogDBObjects have the same objid 

class testDbClass5(dbClass4): 

database = self.dbName 

driver = 'sqlite' 

objid = 'dummy' 

 

class testDbClass6(dbClass5): 

database = self.dbName 

driver = 'sqlite' 

objid = 'dummy' 

 

with self.assertRaises(RuntimeError) as context: 

CompoundCatalogDBObject([testDbClass5, testDbClass6]) 

 

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

 

# test case where CompoundCatalogDBObject does not support the 

# tables being queried 

class testDbClass7(dbClass1): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass8(dbClass2): 

database = self.dbName 

driver = 'sqlite' 

 

with self.assertRaises(RuntimeError) as context: 

specificCompoundObj_otherTest([testDbClass7, testDbClass8]) 

 

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

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

 

def testCompoundCatalogDBObject(self): 

""" 

Verify that CompoundCatalogDBObject returns the expected 

columns. 

""" 

 

class testDbClass9(dbClass1): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass10(dbClass2): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass11(dbClass3): 

database = self.dbName 

driver = 'sqlite' 

 

db1 = testDbClass9 

db2 = testDbClass10 

db3 = testDbClass11 

 

dbList = [db1, db2, db3] 

compoundDb = CompoundCatalogDBObject(dbList) 

 

colNames = ['%s_aa' % db1.objid, '%s_bb' % db1.objid, 

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

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

'%s_cc' % db3.objid] 

 

results = compoundDb.query_columns(colnames=colNames) 

 

for chunk in results: 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db1.objid], 

self.controlArray['a'], 

decimal=6) 

 

numpy.testing.assert_array_equal(chunk['%s_bb' % db1.objid], 

self.controlArray['d']) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db2.objid], 

2.0*self.controlArray['b'], 

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_bb' % db2.objid], 

self.controlArray['a'], 

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db3.objid], 

self.controlArray['c']-3.0, 

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_bb' % db3.objid], 

self.controlArray['a'], 

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_cc' % db3.objid], 

3.0*self.controlArray['b'], 

decimal=6) 

 

def testTableRestriction(self): 

""" 

Verify that _table_restriction works the way it should in CompoundCatalogDBObject 

""" 

 

class testDbClass12(dbClass1): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass13(dbClass2): 

database = self.dbName 

driver = 'sqlite' 

 

db1 = testDbClass12 

db2 = testDbClass13 

dbList = [db1, db2] 

compoundDb = specificCompoundObj_test(dbList) 

 

colNames = ['%s_aa' % db1.objid, '%s_bb' % db1.objid, 

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

 

results = compoundDb.query_columns(colnames=colNames) 

 

for chunk in results: 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db1.objid], 

self.controlArray['a'], 

decimal=6) 

 

numpy.testing.assert_array_equal(chunk['%s_bb' % db1.objid], 

self.controlArray['d']) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db2.objid], 

2.0*self.controlArray['b'], 

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_bb' % db2.objid], 

self.controlArray['a'], 

decimal=6) 

 

def testUniversalTableRestriction(self): 

""" 

Verify that _table_restriction with multiple tables also works 

""" 

 

class testDbClass14(dbClass1): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass15(dbClass2): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass16(dbClass3): 

database = self.dbName 

driver = 'sqlite' 

 

db1 = testDbClass14 

db2 = testDbClass15 

db3 = testDbClass16 

dbList = [db1, db2, db3] 

compoundDb = universalCompoundObj(dbList) 

 

colNames = ['%s_aa' % db1.objid, '%s_bb' % db1.objid, 

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

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

'%s_cc' % db3.objid] 

 

results = compoundDb.query_columns(colnames=colNames) 

 

for chunk in results: 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db1.objid], 

self.controlArray['a'], 

decimal=6) 

 

numpy.testing.assert_array_equal(chunk['%s_bb' % db1.objid], 

self.controlArray['d']) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db2.objid], 

2.0*self.controlArray['b'], 

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_bb' % db2.objid], 

self.controlArray['a'], 

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db3.objid], 

self.controlArray['c']-3.0, 

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_bb' % db3.objid], 

self.controlArray['a'], 

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_cc' % db3.objid], 

3.0*self.controlArray['b'], 

decimal=6) 

 

def testChunks(self): 

""" 

Verify that CompoundCatalogDBObject handles chunk_size correctly 

""" 

 

class testDbClass17(dbClass1): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass18(dbClass2): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass19(dbClass3): 

database = self.dbName 

driver = 'sqlite' 

 

db1 = testDbClass17 

db2 = testDbClass18 

db3 = testDbClass19 

dbList = [db1, db2, db3] 

compoundDb = CompoundCatalogDBObject(dbList) 

 

colNames = ['id', 

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

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

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

'%s_cc' % db3.objid] 

 

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

 

ct = 0 

 

for chunk in results: 

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

rows = chunk['id'] 

self.assertLessEqual(len(rows), 10) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db1.objid], 

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

decimal=6) 

 

numpy.testing.assert_array_equal(chunk['%s_bb' % db1.objid], 

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

 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db2.objid], 

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

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_bb' % db2.objid], 

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

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db3.objid], 

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

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_bb' % db3.objid], 

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

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_cc' % db3.objid], 

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

decimal=6) 

 

self.assertEqual(ct, 100) 

 

def testNoneMapping(self): 

""" 

Test that Nones are handled correctly in the CatalogDBObject 

column mappings 

""" 

 

class testDbClass20(dbClass1): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass21(dbClass6): 

database = self.dbName 

driver = 'sqlite' 

 

db1 = testDbClass20 

db2 = testDbClass21 

colNames = ['%s_aa' % db1.objid, '%s_bb' % db1.objid, 

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

 

compoundDb = CompoundCatalogDBObject([db1, db2]) 

results = compoundDb.query_columns(colnames=colNames) 

 

for chunk in results: 

numpy.testing.assert_array_almost_equal(chunk['%s_aa' % db1.objid], 

self.controlArray['a'], 

decimal=6) 

 

numpy.testing.assert_array_equal(chunk['%s_bb' % db1.objid], 

self.controlArray['d']) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_a' % db2.objid], 

self.controlArray['a'], 

decimal=6) 

 

numpy.testing.assert_array_almost_equal(chunk['%s_b' % db2.objid], 

self.controlArray['b'], 

decimal=6) 

 

 

class testStarDB1(CatalogDBObject): 

tableid = 'test' 

raColName = 'ra' 

decColName = 'dec' 

idColKey = 'id' 

objid = 'testStar1' 

columns = [('id', None), 

('raJ2000', 'ra'), 

('decJ2000', 'dec'), 

('magMod', 'mag')] 

 

 

class testStarDB2(CatalogDBObject): 

tableid = 'test' 

raColName = 'ra' 

decColName = 'dec' 

idColKey = 'id' 

objid = 'testStar2' 

columns = [('id', None), 

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

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

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

 

 

class CompoundWithObsMetaData(unittest.TestCase): 

 

longMessage = True 

 

@classmethod 

def setUpClass(cls): 

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

 

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

 

numpy.random.seed(42) 

nSamples = 100 

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

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

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

 

dtype = numpy.dtype([ 

('ra', numpy.float), 

('dec', numpy.float), 

('mag', numpy.float) 

]) 

 

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

dtype=dtype) 

 

dbDtype = numpy.dtype([ 

('id', numpy.int), 

('ra', numpy.float), 

('dec', numpy.float), 

('mag', numpy.float) 

]) 

 

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

os.unlink(cls.textFileName) 

 

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

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

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

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

 

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

 

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

os.unlink(cls.dbName) 

 

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

database=cls.dbName, dtype=dbDtype, 

idColKey='id') 

 

@classmethod 

def tearDownClass(cls): 

sims_clean_up() 

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

os.unlink(cls.textFileName) 

 

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

os.unlink(cls.dbName) 

 

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

shutil.rmtree(cls.baseDir) 

 

def testObsMetaData(self): 

""" 

Test that CompoundCatalogDBObject can handle ObservationMetaData 

properly 

""" 

 

obs = ObservationMetaData(pointingRA = 180.0, 

pointingDec = 0.0, 

boundType = 'box', 

boundLength = (80.0, 25.0), 

mjd=53580.0) 

 

class testDbClass22(testStarDB1): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass23(testStarDB2): 

database = self.dbName 

driver = 'sqlite' 

 

db1 = testDbClass22 

db2 = testDbClass23 

 

compoundDb = CompoundCatalogDBObject([db1, db2]) 

 

colnames = ['%s_id' % db1.objid, 

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

'%s_magMod' % db1.objid, 

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

'%s_magMod' % db2.objid] 

 

results = compoundDb.query_columns(colnames=colnames, 

obs_metadata=obs) 

 

good_rows = [] 

for chunk in results: 

for line in chunk: 

ix = int(line['id']) 

good_rows.append(ix) 

self.assertAlmostEqual(line['%s_raJ2000' % db1.objid], self.controlArray['ra'][ix], 10) 

self.assertAlmostEqual(line['%s_decJ2000' % db1.objid], self.controlArray['dec'][ix], 10) 

self.assertAlmostEqual(line['%s_magMod' % db1.objid], self.controlArray['mag'][ix], 10) 

self.assertAlmostEqual(line['%s_raJ2000' % db2.objid], 2.0*self.controlArray['ra'][ix], 10) 

self.assertAlmostEqual(line['%s_decJ2000' % db2.objid], 2.0*self.controlArray['dec'][ix], 10) 

self.assertAlmostEqual(line['%s_magMod' % db2.objid], 2.0*self.controlArray['mag'][ix], 10) 

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

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

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

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

 

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

 

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

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

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

 

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

self.assertGreater(len(good_rows), 0) 

self.assertGreater(len(bad_rows), 0) 

 

def testContraint(self): 

""" 

Test that CompoundCatalogDBObject runs correctly with a constraint 

""" 

 

class testDbClass24(testStarDB1): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass25(testStarDB2): 

database = self.dbName 

driver = 'sqlite' 

 

db1 = testDbClass24 

db2 = testDbClass25 

 

compoundDb = CompoundCatalogDBObject([db1, db2]) 

 

colnames = ['%s_id' % db1.objid, 

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

'%s_magMod' % db1.objid, 

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

'%s_magMod' % db2.objid] 

 

results = compoundDb.query_columns(colnames=colnames, 

constraint='mag<11.0') 

 

good_rows = [] 

for chunk in results: 

for line in chunk: 

ix = int(line['id']) 

good_rows.append(ix) 

self.assertAlmostEqual(line['%s_raJ2000' % db1.objid], self.controlArray['ra'][ix], 10) 

self.assertAlmostEqual(line['%s_decJ2000' % db1.objid], self.controlArray['dec'][ix], 10) 

self.assertAlmostEqual(line['%s_magMod' % db1.objid], self.controlArray['mag'][ix], 10) 

self.assertAlmostEqual(line['%s_raJ2000' % db2.objid], 2.0*self.controlArray['ra'][ix], 10) 

self.assertAlmostEqual(line['%s_decJ2000' % db2.objid], 2.0*self.controlArray['dec'][ix], 10) 

self.assertAlmostEqual(line['%s_magMod' % db2.objid], 2.0*self.controlArray['mag'][ix], 10) 

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

 

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

 

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

 

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

self.assertGreater(len(good_rows), 0) 

self.assertGreater(len(bad_rows), 0) 

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

 

def testObsMetadataAndConstraint(self): 

""" 

Test that CompoundCatalogDBObject correctly handles an ObservationMetaData 

and a constraint at the same time 

""" 

obs = ObservationMetaData(pointingRA = 180.0, 

pointingDec = 0.0, 

boundType = 'box', 

boundLength = (80.0, 25.0), 

mjd=53580.0) 

 

class testDbClass26(testStarDB1): 

database = self.dbName 

driver = 'sqlite' 

 

class testDbClass27(testStarDB2): 

database = self.dbName 

driver = 'sqlite' 

 

db1 = testDbClass26 

db2 = testDbClass27 

 

compoundDb = CompoundCatalogDBObject([db1, db2]) 

 

colnames = ['%s_id' % db1.objid, 

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

'%s_magMod' % db1.objid, 

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

'%s_magMod' % db2.objid] 

 

results = compoundDb.query_columns(colnames=colnames, 

obs_metadata=obs, 

constraint='mag>15.0') 

 

good_rows = [] 

for chunk in results: 

for line in chunk: 

ix = int(line['id']) 

good_rows.append(ix) 

self.assertAlmostEqual(line['%s_raJ2000' % db1.objid], self.controlArray['ra'][ix], 10) 

self.assertAlmostEqual(line['%s_decJ2000' % db1.objid], self.controlArray['dec'][ix], 10) 

self.assertAlmostEqual(line['%s_magMod' % db1.objid], self.controlArray['mag'][ix], 10) 

self.assertAlmostEqual(line['%s_raJ2000' % db2.objid], 2.0*self.controlArray['ra'][ix], 10) 

self.assertAlmostEqual(line['%s_decJ2000' % db2.objid], 2.0*self.controlArray['dec'][ix], 10) 

self.assertAlmostEqual(line['%s_magMod' % db2.objid], 2.0*self.controlArray['mag'][ix], 10) 

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

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

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

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

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

 

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

 

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

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

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

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

 

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

self.assertGreater(len(good_rows), 0) 

self.assertGreater(len(bad_rows), 0) 

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

 

 

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

pass 

 

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

lsst.utils.tests.init() 

unittest.main()