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

from __future__ import with_statement 

from builtins import range 

import os 

import unittest 

import numpy as np 

import sqlite3 

import json 

import tempfile 

import shutil 

import lsst.utils.tests 

from lsst.utils import getPackageDir 

from lsst.sims.utils.CodeUtilities import sims_clean_up 

from lsst.sims.utils import ObservationMetaData 

from lsst.sims.catalogs.db import CatalogDBObject 

from lsst.sims.catalogs.definitions import InstanceCatalog 

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

from lsst.sims.catUtils.mixins import VariabilityStars, VariabilityGalaxies 

from lsst.sims.catUtils.mixins import ExtraGalacticVariabilityModels 

from lsst.sims.catUtils.utils import TestVariabilityMixin 

 

from lsst.sims.catUtils.mixins import Variability 

 

VARIABILITY_DB = 'VariabilityTestDatabase.db' 

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

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

def makeRRlyTable(size=100, database=VARIABILITY_DB, **kwargs): 

""" 

Make a test database to serve information to the rrlyrae test 

""" 

 

# a haphazard sample of stellar SEDs 

sedFiles = ['kp10_8750.fits_g35_8950', 'kp03_10500.fits_g45_10600', 'km50_6750.fits_g20_6750'] 

 

# a haphazard sample of RRLyrae light curves 

lcFiles = ['rrly_lc/RRc/959802_per.txt', 'rrly_lc/RRc/1078860_per.txt', 'rrly_lc/RRab/98874_per.txt', 

'rrly_lc/RRab/3879827_per.txt'] 

 

conn = sqlite3.connect(database) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE RRly 

(varsimobjid int, variability text, sedfilename text, parallax real, ebv real)''') 

conn.commit() 

except: 

return 

 

rng = np.random.RandomState(32) 

mjDisplacement = (rng.random_sample(size)-50.0)*50.0 

for i in range(size): 

sedFile = sedFiles[rng.randint(0, len(sedFiles))] 

varParam = {'varMethodName': 'applyRRly', 

'pars': {'tStartMjd': 48000.0+mjDisplacement[i], 

'filename': lcFiles[rng.randint(0, len(lcFiles))]}} 

paramStr = json.dumps(varParam) 

 

qstr = '''INSERT INTO RRly VALUES (%i, '%s', '%s', 0.01, 0.7)''' % (i, paramStr, sedFile) 

c.execute(qstr) 

conn.commit() 

conn.close() 

 

 

def makeCepheidTable(size=100, database=VARIABILITY_DB, **kwargs): 

""" 

Make a test database to serve information to the cepheid test 

""" 

 

# a haphazard sample of stellar SEDs 

sedFiles = ['kp10_8750.fits_g35_8950', 'kp03_10500.fits_g45_10600', 'km50_6750.fits_g20_6750'] 

 

# a haphazard sample of cepheid light curves 

lcFiles = ['cepheid_lc/classical_longPer_specfile', 'cepheid_lc/classical_medPer_specfile', 

'cepheid_lc/classical_shortPer_specfile', 'cepheid_lc/classical_shortPer_specfile', 

'cepheid_lc/popII_longPer_specfile', 'cepheid_lc/popII_shortPer_specfile'] 

 

conn = sqlite3.connect(database) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE cepheid 

(varsimobjid int, variability text, sedfilename text, parallax real, ebv real)''') 

conn.commit() 

except: 

return 

 

rng = np.random.RandomState(32) 

periods = rng.random_sample(size)*50.0 

mjDisplacement = (rng.random_sample(size)-0.5)*50.0 

for i in range(size): 

sedFile = sedFiles[rng.randint(0, len(sedFiles))] 

varParam = {'varMethodName': 'applyCepheid', 

'pars': {'period': periods[i], 'lcfile': lcFiles[rng.randint(0, len(lcFiles))], 

't0': 48000.0+mjDisplacement[i]}} 

paramStr = json.dumps(varParam) 

 

qstr = '''INSERT INTO cepheid VALUES (%i, '%s', '%s', 0.01, 0.7)''' % (i, paramStr, sedFile) 

c.execute(qstr) 

conn.commit() 

conn.close() 

 

 

def makeEbTable(size=100, database=VARIABILITY_DB, **kwargs): 

""" 

Make a test database to serve information to the Eb test 

""" 

 

# a haphazard sample of eclipsing binary light curves 

lcFiles = ['eb_lc/EB.2294.inp', 'eb_lc/EB.1540.inp', 'eb_lc/EB.2801.inp'] 

 

conn = sqlite3.connect(database) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE eb 

(varsimobjid int, variability text, sedfilename text, parallax real, ebv real)''') 

conn.commit() 

except: 

return 

 

rng = np.random.RandomState(32) 

periods = rng.random_sample(size)*50.0 

mjDisplacement = (rng.random_sample(size)-0.5)*50.0 

for i in range(size): 

sedFile = 'sed_flat_norm.txt' 

varParam = {'varMethodName': 'applyEb', 

'pars': {'period': periods[i], 'lcfile': lcFiles[rng.randint(0, len(lcFiles))], 

't0': 48000.0+mjDisplacement[i]}} 

paramStr = json.dumps(varParam) 

 

qstr = '''INSERT INTO eb VALUES (%i, '%s', '%s', 0.01, 0.7)''' % (i, paramStr, sedFile) 

c.execute(qstr) 

conn.commit() 

conn.close() 

 

 

def makeMicrolensingTable(size=100, database=VARIABILITY_DB, **kwargs): 

""" 

Make a test database to serve information to the microlensing test 

""" 

 

# a haphazard sample of stellar SEDs 

sedFiles = ['kp10_8750.fits_g35_8950', 'kp03_10500.fits_g45_10600', 'km50_6750.fits_g20_6750'] 

 

# there are two microlensing methods; they should be equivalent 

method = ['applyMicrolensing', 'applyMicrolens'] 

conn = sqlite3.connect(database) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE microlensing 

(varsimobjid int, variability text, sedfilename text, parallax real, ebv real)''') 

conn.commit() 

except: 

return 

 

rng = np.random.RandomState(32) 

that = rng.random_sample(size)*40.0+40.0 

umin = rng.random_sample(size) 

mjDisplacement = rng.random_sample(size)*50.0 

for i in range(size): 

sedFile = sedFiles[0] 

varParam = {'varMethodName': method[i%len(method)], 

'pars': {'that': that[i], 'umin': umin[i], 't0': 52000.0+mjDisplacement[i]}} 

paramStr = json.dumps(varParam) 

 

qstr = '''INSERT INTO microlensing VALUES (%i, '%s', '%s', 0.01, 0.7)''' % (i, paramStr, sedFile) 

c.execute(qstr) 

conn.commit() 

conn.close() 

 

 

def makeBHMicrolensingTable(size=100, database=VARIABILITY_DB, **kwargs): 

""" 

Make a test database to serve information to the BHmicrolensing test 

""" 

 

# a haphazard sample of stellar SEDs 

sedFiles = ['kp10_8750.fits_g35_8950', 'kp03_10500.fits_g45_10600', 'km50_6750.fits_g20_6750'] 

 

# a sample of black hole microlensing light curves that do not repeat time steps 

# (repeating time steps causes the scipy spline interpolation routine to return Nan) 

lcFiles = ['microlens/bh_binary_source/lc_14_25_75_8000_0_0.05_316', 

'microlens/bh_binary_source/lc_14_25_4000_8000_0_phi1.09_0.005_100', 

'microlens/bh_binary_source/lc_14_25_75_8000_0_tets2.09_0.005_316'] 

 

conn = sqlite3.connect(database) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE bhmicrolensing 

(varsimobjid int, variability text, sedfilename text, parallax real, ebv real)''') 

conn.commit() 

except: 

return 

 

rng = np.random.RandomState(32) 

mjDisplacement = rng.random_sample(size)*5.0*365.25 

for i in range(size): 

sedFile = sedFiles[rng.randint(0, len(sedFiles))] 

varParam = {'varMethodName': 'applyBHMicrolens', 

'pars': {'filename': lcFiles[rng.randint(0, len(lcFiles))], 

't0': 52000.0-mjDisplacement[i]}} 

paramStr = json.dumps(varParam) 

 

qstr = '''INSERT INTO bhmicrolensing VALUES (%i, '%s', '%s', 0.01, 0.7)''' % (i, paramStr, sedFile) 

c.execute(qstr) 

conn.commit() 

conn.close() 

 

 

def makeAmcvnTable(size=100, database=VARIABILITY_DB, **kwargs): 

""" 

Make a test database to serve information to the AMCVN test 

""" 

 

# a haphazard sample of white dwarf SEDs 

sedFiles = ['bergeron_He_4750_70.dat_4950', 'bergeron_50000_85.dat_54000'] 

 

conn = sqlite3.connect(database) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE amcvn 

(varsimobjid int, variability text, sedfilename text, parallax real, ebv real)''') 

conn.commit() 

except: 

return 

 

rng = np.random.RandomState(32) 

doesBurst = rng.randint(0, 2, size=size) 

burst_freq = rng.randint(10, 150, size=size) 

burst_scale = 115.0 

amp_burst = rng.random_sample(size)*8.0 

color_excess_during_burst = rng.random_sample(size)*0.2-0.4 

amplitude = rng.random_sample(size)*0.2 

period = rng.random_sample(size)*200.0 

mjDisplacement = rng.random_sample(size)*500.0 

for i in range(size): 

sedFile = sedFiles[rng.randint(0, len(sedFiles))] 

varParam = {'varMethodName': 'applyAmcvn', 

'pars': {'does_burst': int(doesBurst[i]), # have to cast to int from np.int for json 

'burst_freq': int(burst_freq[i]), 

'burst_scale': burst_scale, 

'amp_burst': amp_burst[i], 

'color_excess_during_burst': color_excess_during_burst[i], 

'amplitude': amplitude[i], 

'period': period[i], 

't0': 51500.0-mjDisplacement[i]}} 

 

paramStr = json.dumps(varParam) 

 

qstr = '''INSERT INTO amcvn VALUES (%i, '%s', '%s', 0.01, 0.7)''' % (i, paramStr, sedFile) 

c.execute(qstr) 

conn.commit() 

conn.close() 

 

 

def makeAgnTable(size=100, database=VARIABILITY_DB, **kwargs): 

""" 

Make a test database to serve information to the microlensing test 

""" 

 

# a haphazard sample of galaxy SEDs 

sedFiles = ['Exp.31E06.0005Z.spec', 'Inst.79E06.1Z.spec', 'Const.50E07.0005Z.spec'] 

conn = sqlite3.connect(database) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE agn 

(galid int, varsimobjid int, 

internalAvBulge real, internalAvDisk real, redshift real, 

variability text, 

sedFilenameBulge text, sedFilenameDisk text, sedFilenameAgn text)''') 

conn.commit() 

except: 

return 

 

rng = np.random.RandomState(32) 

agn_tau = rng.random_sample(size)*100.0+100.0 

agn_sfu = rng.random_sample(size)*2.0 

agn_sfg = rng.random_sample(size)*2.0 

agn_sfr = rng.random_sample(size)*2.0 

agn_sfi = rng.random_sample(size)*2.0 

agn_sfz = rng.random_sample(size)*2.0 

agn_sfy = rng.random_sample(size)*2.0 

mjDisplacement = rng.random_sample(size)*5.0 

avBulge = rng.random_sample(size)*0.5+2.6 

avDisk = rng.random_sample(size)*0.5+2.6 

redshift = rng.random_sample(size)*0.5 

for i in range(size): 

varParam = {'varMethodName': 'applyAgn', 

'pars': {'agn_tau': agn_tau[i], 'agn_sfu': agn_sfu[i], 'agn_sfg': agn_sfg[i], 

'agn_sfr': agn_sfr[i], 'agn_sfi': agn_sfi[i], 'agn_sfz': agn_sfz[i], 

'agn_sfy': agn_sfy[i], 't0_mjd': 48000.0+mjDisplacement[i], 

'seed': rng.randint(0, 200000)}} 

 

paramStr = json.dumps(varParam) 

 

qstr = '''INSERT INTO agn VALUES (%i, %i, %f, %f, %f, '%s', '%s', '%s', '%s')''' % \ 

(i, i, avBulge[i], avDisk[i], redshift[i], 

paramStr, 

sedFiles[rng.randint(0, len(sedFiles))], 

sedFiles[rng.randint(0, len(sedFiles))], 

'agn.spec') 

 

c.execute(qstr) 

conn.commit() 

conn.close() 

 

 

def makeHybridTable(size=100, database='VariabilityTestDatabase.db', **kwargs): 

""" 

Make a test database that contains a mix of Cepheid variables 

and 'testVar' variables (variables that use the applySineVar 

method defined in the TestVariabilityMixin) 

""" 

 

# a haphazard sample of stellar SEDs 

sedFiles = ['kp10_8750.fits_g35_8950', 'kp03_10500.fits_g45_10600', 'km50_6750.fits_g20_6750'] 

 

# a haphazard sample of cepheid light curves 

lcFiles = ['cepheid_lc/classical_longPer_specfile', 'cepheid_lc/classical_medPer_specfile', 

'cepheid_lc/classical_shortPer_specfile', 'cepheid_lc/classical_shortPer_specfile', 

'cepheid_lc/popII_longPer_specfile', 'cepheid_lc/popII_shortPer_specfile'] 

 

conn = sqlite3.connect(database) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE hybrid 

(varsimobjid int, variability text, sedfilename text, parallax real, ebv real)''') 

conn.commit() 

except: 

return 

 

rng = np.random.RandomState(32) 

periods = rng.random_sample(size)*50.0 

mjDisplacement = (rng.random_sample(size)-0.5)*50.0 

for i in range(size): 

sedFile = sedFiles[rng.randint(0, len(sedFiles))] 

if i%3 == 0: 

# just to make sure that Variability mixins no how to andle 

# objects with no variability 

varParam = None 

paramStr = None 

elif i%2 == 0: 

varParam = {'varMethodName': 'applyCepheid', 

'pars': {'period': periods[i], 

'lcfile': lcFiles[rng.randint(0, len(lcFiles))], 

't0': 48000.0+mjDisplacement[i]}} 

else: 

varParam = {'varMethodName': 'testVar', 

'pars': {'period': rng.random_sample()*100.0, 'amplitude': 2.0}} 

 

if varParam is not None: 

paramStr = json.dumps(varParam) 

 

qstr = '''INSERT INTO hybrid VALUES (%i, '%s', '%s', 0.01, 0.7)''' % (i, paramStr, sedFile) 

c.execute(qstr) 

conn.commit() 

conn.close() 

 

 

class variabilityDB(CatalogDBObject): 

driver = 'sqlite' 

database = VARIABILITY_DB 

idColKey = 'varsimobjid' 

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

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

('varParamStr', 'variability', str, 600)] 

 

 

 

class hybridDB(variabilityDB): 

objid = 'hybridTest' 

tableid = 'hybrid' 

objectTypeId = 54 

 

 

class rrlyDB(variabilityDB): 

objid = 'rrlyTest' 

tableid = 'RRly' 

objectTypeId = 55 

 

 

class cepheidDB(variabilityDB): 

objid = 'cepheidTest' 

tableid = 'cepheid' 

objectTypeId = 56 

 

 

class ebDB(variabilityDB): 

objid = 'ebTest' 

tableid = 'eb' 

objectTypeId = 57 

 

 

class microlensDB(variabilityDB): 

objid = 'microlensTest' 

tableid = 'microlensing' 

objectTypeId = 58 

 

 

class BHmicrolensDB(variabilityDB): 

objid = 'bhmicrolensTest' 

tableid = 'bhmicrolensing' 

objectTypeId = 59 

 

 

class amcvnDB(variabilityDB): 

objid = 'amcvnTest' 

tableid = 'amcvn' 

objectTypeId = 60 

 

 

class agnDB(variabilityDB): 

objid = 'agnTest' 

tableid = 'agn' 

objectTypeId = 61 

 

 

class StellarVariabilityCatalog(InstanceCatalog, PhotometryStars, VariabilityStars): 

catalog_type = __file__ + 'stellarVariabilityCatalog' 

column_outputs = ['varsimobjid', 'sedFilename', 'delta_lsst_u'] 

default_columns = [('magNorm', 14.0, float)] 

 

 

class StellarVariabilityCatalogWithTest(InstanceCatalog, PhotometryStars, 

VariabilityStars, TestVariabilityMixin): 

catalog_type = __file__ + 'testVariabilityCatalog' 

column_outputs = ['varsimobjid', 'sedFilename', 'delta_lsst_u'] 

default_columns = [('magNorm', 14.0, float)] 

 

 

class OtherVariabilityCatalogWithTest(InstanceCatalog, PhotometryStars, 

TestVariabilityMixin, VariabilityStars): 

catalog_type = __file__ + 'other_variability_catalog' 

column_outputs = ['varsimobjid', 'sedFilename', 'delta_lsst_u'] 

default_columns = [('magNorm', 14.0, float)] 

 

 

class GalaxyVariabilityCatalog(InstanceCatalog, PhotometryGalaxies, VariabilityGalaxies): 

catalog_type = __file__ + 'galaxyVariabilityCatalog' 

column_outputs = ['varsimobjid', 'sedFilenameAgn', 'lsstUdiff', 'delta_uAgn'] 

default_columns = [('magNormAgn', 14.0, float), 

('magNormDisk', 14.0, float), 

('magNormBulge', 14.0, float)] 

 

def get_lsstUdiff(self): 

lsstUvar = self.column_by_name('lsst_u') 

 

bulge = self.column_by_name('uBulge') 

disk = self.column_by_name('uDisk') 

agn = self.column_by_name('uAgn') - self.column_by_name('delta_uAgn') 

lsstU = self.sum_magnitudes(bulge=bulge, disk=disk, agn=agn) 

 

return lsstUvar - lsstU 

 

def get_agnUdiff(self): 

lsstU = self.column_by_name('uAgn') 

lsstUvar = self.column_by_name('uAgn_var') 

return lsstUvar - lsstU 

 

 

class VariabilityTest(unittest.TestCase): 

 

longMessage = True 

 

@classmethod 

def setUpClass(cls): 

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

cls.variability_db = os.path.join(cls.scratch_dir, VARIABILITY_DB) 

 

@classmethod 

def tearDownClass(cls): 

sims_clean_up() 

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

os.unlink(cls.variability_db) 

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

shutil.rmtree(cls.scratch_dir) 

 

def setUp(self): 

self.obs_metadata = ObservationMetaData(mjd=52000.0) 

 

def tearDown(self): 

del self.obs_metadata 

 

def verify_catalogs(self, cat_name): 

""" 

Verify that a catalog generated by the unit tests below contains 

the rows it ought to. 

 

This is done by looking for a corresponding catalog in 

tests/testData and verifying that the two catalogs have identical 

rows. 

 

Parameters 

---------- 

cat_name is the full path to the test-generated catalog. The 

comparison catalog will be found in tests/testData 

""" 

 

control_dir = os.path.join(getPackageDir('sims_catUtils'), 

'tests', 'testData') 

_, control_name = os.path.split(cat_name) 

control_name = os.path.join(control_dir, control_name) 

with open(control_name, 'r') as control_file: 

control_lines = control_file.readlines() 

 

with open(cat_name, 'r') as test_file: 

test_lines = test_file.readlines() 

 

for tt in test_lines: 

self.assertIn(tt, control_lines) 

 

for cc in control_lines: 

self.assertIn(cc, test_lines) 

 

def testHybridVariability(self): 

""" 

Test that we can generate a catalog which inherits from multiple variability mixins 

(in this case, TestVariability and VariabilityStars). This is to make sure that 

the register_method and register_class decorators do not mangle inheritance of 

methods from mixins. 

""" 

cat_name = os.path.join(self.scratch_dir, 'hybridTestCatalog.dat') 

makeHybridTable(database=self.variability_db) 

myDB = CatalogDBObject.from_objid('hybridTest', database=self.variability_db) 

myCatalog = StellarVariabilityCatalogWithTest(myDB, obs_metadata=self.obs_metadata) 

 

myCatalog.write_catalog(cat_name, chunk_size=1000) 

self.verify_catalogs(cat_name) 

 

if os.path.exists(cat_name): 

os.unlink(cat_name) 

 

# make sure order of mixin inheritance does not matter 

myCatalog = OtherVariabilityCatalogWithTest(myDB, obs_metadata=self.obs_metadata) 

myCatalog.write_catalog(cat_name, chunk_size=1000) 

self.verify_catalogs(cat_name) 

 

if os.path.exists(cat_name): 

os.unlink(cat_name) 

 

# make sure that, if a catalog does not contain a variability method, 

# an error is thrown; verify that it contains the correct error message 

myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata) 

 

with self.assertRaises(RuntimeError) as context: 

myCatalog.write_catalog(cat_name) 

 

if os.path.exists(cat_name): 

os.unlink(cat_name) 

 

expectedMessage = "Your InstanceCatalog does not contain a variability method" 

expectedMessage += " corresponding to 'testVar'" 

self.assertEqual(context.exception.args[0], expectedMessage) 

 

def testApplyVariabilityWithManyMJD(self): 

""" 

Use the hybrid variability catalog class from testHybridVariability to 

verify that, if we pass an array of expmjd into applyVariability, we get 

the expected result out. 

""" 

 

makeHybridTable(database=self.variability_db) 

hybrid_db = hybridDB(database=self.variability_db) 

hybrid_cat = StellarVariabilityCatalogWithTest(hybrid_db, obs_metadata=self.obs_metadata, 

column_outputs=['varParamStr']) 

rng = np.random.RandomState(88) 

mjd_arr = rng.random_sample(14)*3653.0+59580.0 

n_time = len(mjd_arr) 

varparams =[] 

n_ceph = 0 

n_test = 0 

for line in hybrid_cat.iter_catalog(): 

varparams.append(line[-1]) 

if 'Cepheid' in line[-1]: 

n_ceph += 1 

if 'testVar' in line[-1]: 

n_test +=1 

 

self.assertGreater(n_ceph, 0) 

self.assertGreater(n_test, 0) 

 

n_obj=len(varparams) 

 

new_hybrid_cat = StellarVariabilityCatalogWithTest(hybrid_db, obs_metadata=self.obs_metadata) 

delta_mag_vector = new_hybrid_cat.applyVariability(varparams, expmjd=mjd_arr) 

self.assertEqual(delta_mag_vector.shape, (6, len(varparams), len(mjd_arr))) 

 

n_mags = 0 

for i_time, mjd in enumerate(mjd_arr): 

obs = ObservationMetaData(mjd=mjd) 

control_hybrid_cat = StellarVariabilityCatalogWithTest(hybrid_db, obs_metadata=obs, 

column_outputs=['delta_lsst_u', 

'delta_lsst_g', 

'delta_lsst_r', 

'delta_lsst_i', 

'delta_lsst_z', 

'delta_lsst_y']) 

 

for i_obj, star_obj in enumerate(control_hybrid_cat.iter_catalog()): 

for i_band in range(6): 

n_mags += 1 

self.assertEqual(delta_mag_vector[i_band][i_obj][i_time], 

star_obj[-6+i_band], 

msg = 'failed on time %d; obj %d; band %d' % (i_time, i_obj, i_band)) 

 

self.assertEqual(n_mags, 6*n_obj*n_time) 

 

def testRRlyrae(self): 

cat_name = os.path.join(self.scratch_dir, 'rrlyTestCatalog.dat') 

makeRRlyTable(database=self.variability_db) 

myDB = CatalogDBObject.from_objid('rrlyTest', database=self.variability_db) 

myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata) 

myCatalog.write_catalog(cat_name, chunk_size=1000) 

 

self.verify_catalogs(cat_name) 

if os.path.exists(cat_name): 

os.unlink(cat_name) 

 

def testCepheids(self): 

cat_name = os.path.join(self.scratch_dir, 'cepheidTestCatalog.dat') 

makeCepheidTable(database=self.variability_db) 

myDB = CatalogDBObject.from_objid('cepheidTest', database=self.variability_db) 

myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata) 

myCatalog.write_catalog(cat_name, chunk_size=1000) 

 

self.verify_catalogs(cat_name) 

if os.path.exists(cat_name): 

os.unlink(cat_name) 

 

def testEb(self): 

cat_name = os.path.join(self.scratch_dir, 'ebTestCatalog.dat') 

makeEbTable(database=self.variability_db) 

myDB = CatalogDBObject.from_objid('ebTest', database=self.variability_db) 

myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata) 

myCatalog.write_catalog(cat_name, chunk_size=1000) 

 

self.verify_catalogs(cat_name) 

if os.path.exists(cat_name): 

os.unlink(cat_name) 

 

def testMicrolensing(self): 

# Note: this test assumes that the parameters for the microlensing variability 

# model occur in a standard varParamStr column in the database. 

# Actually, the current database of microlensing events simply store the variability 

# parameters as independent columns in the database. 

# The varParamStr formalism is how the applyMicrolensing methods are written, however, 

# so that is what we will test. 

 

cat_name = os.path.join(self.scratch_dir, 'microlensTestCatalog.dat') 

makeMicrolensingTable(database=self.variability_db) 

myDB = CatalogDBObject.from_objid('microlensTest', database=self.variability_db) 

myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata) 

myCatalog.write_catalog(cat_name, chunk_size=1000) 

 

self.verify_catalogs(cat_name) 

if os.path.exists(cat_name): 

os.unlink(cat_name) 

 

def testBHMicrolensing(self): 

# Note: this test assumes that the parameters for the BHmicrolensing variability 

# model occur in a standard varParamStr column in the database. 

# Actually, the current database of BHmicrolensing events simply store the variability 

# parameters as independent columns in the database. 

# The varParamStr formalism is how the applyBHMicrolens method is written, however, 

# so that is what we will test. 

 

cat_name = os.path.join(self.scratch_dir, 'bhmicrolensTestCatalog.dat') 

makeBHMicrolensingTable(database=self.variability_db) 

myDB = CatalogDBObject.from_objid('bhmicrolensTest', database=self.variability_db) 

myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata) 

myCatalog.write_catalog(cat_name, chunk_size=1000) 

 

self.verify_catalogs(cat_name) 

if os.path.exists(cat_name): 

os.unlink(cat_name) 

 

def testAmcvn(self): 

# Note: this test assumes that the parameters for the Amcvn variability 

# model occur in a standard varParamStr column in the database. 

# Actually, the current database of Amcvn events simply store the variability 

# parameters as independent columns in the database. 

# The varParamStr formalism is how the applyAmcvn method is written, however, 

# so that is what we will test. 

 

cat_name = os.path.join(self.scratch_dir, 'amcvnTestCatalog.dat') 

makeAmcvnTable(database=self.variability_db) 

myDB = CatalogDBObject.from_objid('amcvnTest', database=self.variability_db) 

myCatalog = StellarVariabilityCatalog(myDB, obs_metadata=self.obs_metadata) 

myCatalog.write_catalog(cat_name, chunk_size=1000) 

 

self.verify_catalogs(cat_name) 

if os.path.exists(cat_name): 

os.unlink(cat_name) 

 

def testAgn(self): 

""" 

Just verify that the catalog generation code runs in this case 

""" 

 

cat_name = os.path.join(self.scratch_dir, 'agnTestCatalog.dat') 

makeAgnTable(database=self.variability_db) 

myDB = CatalogDBObject.from_objid('agnTest', database=self.variability_db) 

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

pointingDec=self.obs_metadata.pointingDec, 

boundType=self.obs_metadata.boundType, 

boundLength=self.obs_metadata.boundLength, 

mjd=60000.0) 

myCatalog = GalaxyVariabilityCatalog(myDB, obs_metadata=obs) 

myCatalog.write_catalog(cat_name, chunk_size=1000) 

 

with open(cat_name,'r') as input_file: 

lines = input_file.readlines() 

self.assertGreater(len(lines), 10) 

 

if os.path.exists(cat_name): 

os.unlink(cat_name) 

 

 

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

pass 

 

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

lsst.utils.tests.init() 

unittest.main()