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 os 

6import copy 

7import numpy as np 

8import lsst.utils.tests 

9from lsst.utils import getPackageDir 

10from lsst.sims.photUtils import Bandpass, Sed, BandpassDict, SedList 

11 

12 

13def setup_module(module): 

14 lsst.utils.tests.init() 

15 

16 

17class BandpassDictTest(unittest.TestCase): 

18 

19 def setUp(self): 

20 self.rng = np.random.RandomState(32) 

21 self.bandpassPossibilities = ['u', 'g', 'r', 'i', 'z', 'y'] 

22 self.bandpassDir = os.path.join(getPackageDir('throughputs'), 'baseline') 

23 self.sedDir = os.path.join(getPackageDir('sims_photUtils'), 

24 'tests/cartoonSedTestData/galaxySed') 

25 self.sedPossibilities = os.listdir(self.sedDir) 

26 

27 def getListOfSedNames(self, nNames): 

28 return [self.sedPossibilities[ii].replace('.gz', '') 

29 for ii in 

30 self.rng.randint(0, len(self.sedPossibilities)-1, nNames)] 

31 

32 def getListOfBandpasses(self, nBp): 

33 """ 

34 Generate a list of nBp bandpass names and bandpasses 

35 

36 Intentionally do so a nonsense order so that we can test 

37 that order is preserved in the BandpassDict 

38 """ 

39 dexList = self.rng.randint(0, len(self.bandpassPossibilities)-1, nBp) 

40 bandpassNameList = [] 

41 bandpassList = [] 

42 for dex in dexList: 

43 name = self.bandpassPossibilities[dex] 

44 bp = Bandpass() 

45 bp.readThroughput(os.path.join(self.bandpassDir, 'total_%s.dat' % name)) 

46 while name in bandpassNameList: 

47 name += '0' 

48 bandpassNameList.append(name) 

49 bandpassList.append(bp) 

50 

51 return bandpassNameList, bandpassList 

52 

53 def testInitialization(self): 

54 """ 

55 Test that all of the member variables of BandpassDict are set 

56 to the correct value upon construction. 

57 """ 

58 

59 for nBp in range(3, 10, 1): 

60 nameList, bpList = self.getListOfBandpasses(nBp) 

61 testDict = BandpassDict(bpList, nameList) 

62 

63 self.assertEqual(len(testDict), nBp) 

64 

65 for controlName, testName in zip(nameList, testDict): 

66 self.assertEqual(controlName, testName) 

67 

68 for controlName, testName in zip(nameList, testDict.keys()): 

69 self.assertEqual(controlName, testName) 

70 

71 for name, bp in zip(nameList, bpList): 

72 np.testing.assert_array_almost_equal(bp.wavelen, testDict[name].wavelen, 10) 

73 np.testing.assert_array_almost_equal(bp.sb, testDict[name].sb, 10) 

74 

75 for bpControl, bpTest in zip(bpList, testDict.values()): 

76 np.testing.assert_array_almost_equal(bpControl.wavelen, bpTest.wavelen, 10) 

77 np.testing.assert_array_almost_equal(bpControl.sb, bpTest.sb, 10) 

78 

79 def testWavelenMatch(self): 

80 """ 

81 Test that when you load bandpasses sampled over different 

82 wavelength grids, they all get sampled to the same wavelength 

83 grid. 

84 """ 

85 dwavList = np.arange(5.0, 25.0, 5.0) 

86 bpList = [] 

87 bpNameList = [] 

88 for ix, dwav in enumerate(dwavList): 

89 name = 'bp_%d' % ix 

90 wavelen = np.arange(10.0, 1500.0, dwav) 

91 sb = np.exp(-0.5*(np.power((wavelen-100.0*ix)/100.0, 2))) 

92 bp = Bandpass(wavelen=wavelen, sb=sb) 

93 bpList.append(bp) 

94 bpNameList.append(name) 

95 

96 # First make sure that we have created distinct wavelength grids 

97 for ix in range(len(bpList)): 

98 for iy in range(ix+1, len(bpList)): 

99 self.assertNotEqual(len(bpList[ix].wavelen), len(bpList[iy].wavelen)) 

100 

101 testDict = BandpassDict(bpList, bpNameList) 

102 

103 # Now make sure that the wavelength grids in the dict were resampled, but that 

104 # the original wavelength grids were not changed 

105 for ix in range(len(bpList)): 

106 np.testing.assert_array_almost_equal(testDict.values()[ix].wavelen, testDict.wavelenMatch, 19) 

107 if ix != 0: 

108 self.assertNotEqual(len(testDict.wavelenMatch), 

109 len(bpList[ix].wavelen)) 

110 

111 def testPhiArray(self): 

112 """ 

113 Test that the phi array is correctly calculated by BandpassDict 

114 upon construction. 

115 """ 

116 

117 for nBp in range(3, 10, 1): 

118 nameList, bpList = self.getListOfBandpasses(nBp) 

119 testDict = BandpassDict(bpList, nameList) 

120 dummySed = Sed() 

121 controlPhi, controlWavelenStep = dummySed.setupPhiArray(bpList) 

122 np.testing.assert_array_almost_equal(controlPhi, 

123 testDict.phiArray, 19) 

124 self.assertAlmostEqual(controlWavelenStep, 

125 testDict.wavelenStep, 10) 

126 

127 def testExceptions(self): 

128 """ 

129 Test that the correct exceptions are thrown by BandpassDict 

130 """ 

131 

132 nameList, bpList = self.getListOfBandpasses(4) 

133 dummyNameList = copy.deepcopy(nameList) 

134 dummyNameList[1] = dummyNameList[0] 

135 

136 with self.assertRaises(RuntimeError) as context: 

137 testDict = BandpassDict(bpList, dummyNameList) 

138 

139 self.assertIn('occurs twice', context.exception.args[0]) 

140 

141 testDict = BandpassDict(bpList, nameList) 

142 

143 with self.assertRaises(AttributeError) as context: 

144 testDict.phiArray = None 

145 

146 with self.assertRaises(AttributeError) as context: 

147 testDict.wavelenStep = 0.9 

148 

149 with self.assertRaises(AttributeError) as context: 

150 testDict.wavelenMatch = np.arange(10.0, 100.0, 1.0) 

151 

152 def testMagListForSed(self): 

153 """ 

154 Test that magListForSed calculates the correct magnitude 

155 """ 

156 

157 wavelen = np.arange(10.0, 2000.0, 1.0) 

158 flux = (wavelen*2.0-5.0)*1.0e-6 

159 spectrum = Sed(wavelen=wavelen, flambda=flux) 

160 

161 for nBp in range(3, 10, 1): 

162 

163 nameList, bpList = self.getListOfBandpasses(nBp) 

164 testDict = BandpassDict(bpList, nameList) 

165 self.assertNotEqual(len(testDict.values()[0].wavelen), len(spectrum.wavelen)) 

166 

167 magList = testDict.magListForSed(spectrum) 

168 for ix, (name, bp, magTest) in enumerate(zip(nameList, 

169 bpList, 

170 magList)): 

171 magControl = spectrum.calcMag(bp) 

172 self.assertAlmostEqual(magTest, magControl, 5) 

173 

174 def testMagDictForSed(self): 

175 """ 

176 Test that magDictForSed calculates the correct magnitude 

177 """ 

178 

179 wavelen = np.arange(10.0, 2000.0, 1.0) 

180 flux = (wavelen*2.0-5.0)*1.0e-6 

181 spectrum = Sed(wavelen=wavelen, flambda=flux) 

182 

183 for nBp in range(3, 10, 1): 

184 

185 nameList, bpList = self.getListOfBandpasses(nBp) 

186 testDict = BandpassDict(bpList, nameList) 

187 self.assertNotEqual(len(testDict.values()[0].wavelen), 

188 len(spectrum.wavelen)) 

189 

190 magDict = testDict.magDictForSed(spectrum) 

191 for ix, (name, bp) in enumerate(zip(nameList, bpList)): 

192 magControl = spectrum.calcMag(bp) 

193 self.assertAlmostEqual(magDict[name], magControl, 5) 

194 

195 def testMagListForSedList(self): 

196 """ 

197 Test that magListForSedList calculates the correct magnitude 

198 """ 

199 

200 nBandpasses = 7 

201 bpNameList, bpList = self.getListOfBandpasses(nBandpasses) 

202 testBpDict = BandpassDict(bpList, bpNameList) 

203 

204 nSed = 20 

205 sedNameList = self.getListOfSedNames(nSed) 

206 magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 

207 internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

208 redshiftList = self.rng.random_sample(nSed)*5.0 

209 galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

210 

211 # first, test on an SedList without a wavelenMatch 

212 testSedList = SedList(sedNameList, magNormList, 

213 fileDir=self.sedDir, 

214 internalAvList=internalAvList, 

215 redshiftList=redshiftList, 

216 galacticAvList=galacticAvList) 

217 

218 magList = testBpDict.magListForSedList(testSedList) 

219 self.assertEqual(magList.shape[0], nSed) 

220 self.assertEqual(magList.shape[1], nBandpasses) 

221 

222 for ix, sedObj in enumerate(testSedList): 

223 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

224 flambda=copy.deepcopy(sedObj.flambda)) 

225 

226 for iy, bp in enumerate(testBpDict): 

227 mag = dummySed.calcMag(bpList[iy]) 

228 self.assertAlmostEqual(mag, magList[ix][iy], 2) 

229 

230 # now use wavelenMatch 

231 testSedList = SedList(sedNameList, magNormList, 

232 fileDir=self.sedDir, 

233 internalAvList=internalAvList, 

234 redshiftList=redshiftList, 

235 galacticAvList=galacticAvList, 

236 wavelenMatch=testBpDict.wavelenMatch) 

237 

238 magList = testBpDict.magListForSedList(testSedList) 

239 self.assertEqual(magList.shape[0], nSed) 

240 self.assertEqual(magList.shape[1], nBandpasses) 

241 

242 for ix, sedObj in enumerate(testSedList): 

243 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

244 flambda=copy.deepcopy(sedObj.flambda)) 

245 

246 for iy, bp in enumerate(testBpDict): 

247 mag = dummySed.calcMag(bpList[iy]) 

248 self.assertAlmostEqual(mag, magList[ix][iy], 2) 

249 

250 def testMagArrayForSedList(self): 

251 """ 

252 Test that magArrayForSedList calculates the correct magnitude 

253 """ 

254 

255 nBandpasses = 7 

256 bpNameList, bpList = self.getListOfBandpasses(nBandpasses) 

257 testBpDict = BandpassDict(bpList, bpNameList) 

258 

259 nSed = 20 

260 sedNameList = self.getListOfSedNames(nSed) 

261 magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 

262 internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

263 redshiftList = self.rng.random_sample(nSed)*5.0 

264 galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

265 

266 # first, test on an SedList without a wavelenMatch 

267 testSedList = SedList(sedNameList, magNormList, 

268 fileDir=self.sedDir, 

269 internalAvList=internalAvList, 

270 redshiftList=redshiftList, 

271 galacticAvList=galacticAvList) 

272 

273 magArray = testBpDict.magArrayForSedList(testSedList) 

274 

275 for ix, sedObj in enumerate(testSedList): 

276 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

277 flambda=copy.deepcopy(sedObj.flambda)) 

278 

279 for iy, bp in enumerate(bpNameList): 

280 mag = dummySed.calcMag(bpList[iy]) 

281 self.assertAlmostEqual(mag, magArray[bp][ix], 2) 

282 

283 # now use wavelenMatch 

284 testSedList = SedList(sedNameList, magNormList, 

285 fileDir=self.sedDir, 

286 internalAvList=internalAvList, 

287 redshiftList=redshiftList, 

288 galacticAvList=galacticAvList, 

289 wavelenMatch=testBpDict.wavelenMatch) 

290 

291 magArray = testBpDict.magArrayForSedList(testSedList) 

292 

293 for ix, sedObj in enumerate(testSedList): 

294 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

295 flambda=copy.deepcopy(sedObj.flambda)) 

296 

297 for iy, bp in enumerate(bpNameList): 

298 mag = dummySed.calcMag(bpList[iy]) 

299 self.assertAlmostEqual(mag, magArray[bp][ix], 2) 

300 

301 def testIndicesOnMagnitudes(self): 

302 """ 

303 Test that, when you pass a list of indices into the calcMagList 

304 methods, you get the correct magnitudes out. 

305 """ 

306 

307 nBandpasses = 7 

308 nameList, bpList = self.getListOfBandpasses(nBandpasses) 

309 testBpDict = BandpassDict(bpList, nameList) 

310 

311 # first try it with a single Sed 

312 wavelen = np.arange(10.0, 2000.0, 1.0) 

313 flux = (wavelen*2.0-5.0)*1.0e-6 

314 spectrum = Sed(wavelen=wavelen, flambda=flux) 

315 indices = [1, 2, 5] 

316 

317 magList = testBpDict.magListForSed(spectrum, indices=indices) 

318 ctNaN = 0 

319 for ix, (name, bp, magTest) in enumerate(zip(nameList, 

320 bpList, 

321 magList)): 

322 if ix in indices: 

323 magControl = spectrum.calcMag(bp) 

324 self.assertAlmostEqual(magTest, magControl, 5) 

325 else: 

326 ctNaN += 1 

327 np.testing.assert_equal(magTest, np.NaN) 

328 

329 self.assertEqual(ctNaN, 4) 

330 

331 nSed = 20 

332 sedNameList = self.getListOfSedNames(nSed) 

333 magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 

334 internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

335 redshiftList = self.rng.random_sample(nSed)*5.0 

336 galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

337 

338 # now try a SedList without a wavelenMatch 

339 testSedList = SedList(sedNameList, magNormList, 

340 fileDir=self.sedDir, 

341 internalAvList=internalAvList, 

342 redshiftList=redshiftList, 

343 galacticAvList=galacticAvList) 

344 

345 magList = testBpDict.magListForSedList(testSedList, indices=indices) 

346 magArray = testBpDict.magArrayForSedList(testSedList, indices=indices) 

347 self.assertEqual(magList.shape[0], nSed) 

348 self.assertEqual(magList.shape[1], nBandpasses) 

349 self.assertEqual(magArray.shape[0], nSed) 

350 for bpname in testBpDict: 

351 self.assertEqual(len(magArray[bpname]), nSed) 

352 

353 for ix, sedObj in enumerate(testSedList): 

354 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

355 flambda=copy.deepcopy(sedObj.flambda)) 

356 

357 ctNaN = 0 

358 for iy, bp in enumerate(testBpDict): 

359 if iy in indices: 

360 mag = dummySed.calcMag(testBpDict[bp]) 

361 self.assertAlmostEqual(mag, magList[ix][iy], 2) 

362 self.assertAlmostEqual(mag, magArray[ix][iy], 2) 

363 self.assertAlmostEqual(mag, magArray[bp][ix], 2) 

364 else: 

365 ctNaN += 1 

366 np.testing.assert_equal(magList[ix][iy], np.NaN) 

367 np.testing.assert_equal(magArray[ix][iy], np.NaN) 

368 np.testing.assert_equal(magArray[bp][ix], np.NaN) 

369 

370 self.assertEqual(ctNaN, 4) 

371 

372 # now use wavelenMatch 

373 testSedList = SedList(sedNameList, magNormList, 

374 fileDir=self.sedDir, 

375 internalAvList=internalAvList, 

376 redshiftList=redshiftList, 

377 galacticAvList=galacticAvList, 

378 wavelenMatch=testBpDict.wavelenMatch) 

379 

380 magList = testBpDict.magListForSedList(testSedList, indices=indices) 

381 magArray = testBpDict.magArrayForSedList(testSedList, indices=indices) 

382 self.assertEqual(magList.shape[0], nSed) 

383 self.assertEqual(magList.shape[1], nBandpasses) 

384 self.assertEqual(magArray.shape[0], nSed) 

385 for bpname in testBpDict: 

386 self.assertEqual(len(magArray[bpname]), nSed) 

387 

388 for ix, sedObj in enumerate(testSedList): 

389 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

390 flambda=copy.deepcopy(sedObj.flambda)) 

391 

392 ctNaN = 0 

393 for iy, bp in enumerate(testBpDict): 

394 if iy in indices: 

395 mag = dummySed.calcMag(testBpDict[bp]) 

396 self.assertAlmostEqual(mag, magList[ix][iy], 2) 

397 self.assertAlmostEqual(mag, magArray[ix][iy], 2) 

398 self.assertAlmostEqual(mag, magArray[bp][ix], 2) 

399 else: 

400 ctNaN += 1 

401 np.testing.assert_equal(magList[ix][iy], np.NaN) 

402 np.testing.assert_equal(magArray[ix][iy], np.NaN) 

403 np.testing.assert_equal(magArray[bp][ix], np.NaN) 

404 

405 self.assertEqual(ctNaN, 4) 

406 

407 def testFluxListForSed(self): 

408 """ 

409 Test that fluxListForSed calculates the correct fluxes 

410 """ 

411 

412 wavelen = np.arange(10.0, 2000.0, 1.0) 

413 flux = (wavelen*2.0-5.0)*1.0e-6 

414 spectrum = Sed(wavelen=wavelen, flambda=flux) 

415 

416 for nBp in range(3, 10, 1): 

417 

418 nameList, bpList = self.getListOfBandpasses(nBp) 

419 testDict = BandpassDict(bpList, nameList) 

420 self.assertNotEqual(len(testDict.values()[0].wavelen), 

421 len(spectrum.wavelen)) 

422 

423 fluxList = testDict.fluxListForSed(spectrum) 

424 for ix, (name, bp, fluxTest) in enumerate(zip(nameList, 

425 bpList, 

426 fluxList)): 

427 fluxControl = spectrum.calcFlux(bp) 

428 self.assertAlmostEqual(fluxTest/fluxControl, 1.0, 2) 

429 

430 def testFluxDictForSed(self): 

431 """ 

432 Test that fluxDictForSed calculates the correct fluxes 

433 """ 

434 

435 wavelen = np.arange(10.0, 2000.0, 1.0) 

436 flux = (wavelen*2.0-5.0)*1.0e-6 

437 spectrum = Sed(wavelen=wavelen, flambda=flux) 

438 

439 for nBp in range(3, 10, 1): 

440 

441 nameList, bpList = self.getListOfBandpasses(nBp) 

442 testDict = BandpassDict(bpList, nameList) 

443 self.assertNotEqual(len(testDict.values()[0].wavelen), 

444 len(spectrum.wavelen)) 

445 

446 fluxDict = testDict.fluxDictForSed(spectrum) 

447 for ix, (name, bp) in enumerate(zip(nameList, bpList)): 

448 fluxControl = spectrum.calcFlux(bp) 

449 self.assertAlmostEqual(fluxDict[name]/fluxControl, 1.0, 2) 

450 

451 def testFluxListForSedList(self): 

452 """ 

453 Test that fluxListForSedList calculates the correct fluxes 

454 """ 

455 

456 nBandpasses = 7 

457 bpNameList, bpList = self.getListOfBandpasses(nBandpasses) 

458 testBpDict = BandpassDict(bpList, bpNameList) 

459 

460 nSed = 20 

461 sedNameList = self.getListOfSedNames(nSed) 

462 magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 

463 internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

464 redshiftList = self.rng.random_sample(nSed)*5.0 

465 galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

466 

467 # first, test on an SedList without a wavelenMatch 

468 testSedList = SedList(sedNameList, magNormList, 

469 fileDir=self.sedDir, 

470 internalAvList=internalAvList, 

471 redshiftList=redshiftList, 

472 galacticAvList=galacticAvList) 

473 

474 fluxList = testBpDict.fluxListForSedList(testSedList) 

475 self.assertEqual(fluxList.shape[0], nSed) 

476 self.assertEqual(fluxList.shape[1], nBandpasses) 

477 

478 for ix, sedObj in enumerate(testSedList): 

479 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

480 flambda=copy.deepcopy(sedObj.flambda)) 

481 

482 for iy, bp in enumerate(testBpDict): 

483 flux = dummySed.calcFlux(bpList[iy]) 

484 self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2) 

485 

486 # now use wavelenMatch 

487 testSedList = SedList(sedNameList, magNormList, 

488 fileDir=self.sedDir, 

489 internalAvList=internalAvList, 

490 redshiftList=redshiftList, 

491 galacticAvList=galacticAvList, 

492 wavelenMatch=testBpDict.wavelenMatch) 

493 

494 fluxList = testBpDict.fluxListForSedList(testSedList) 

495 self.assertEqual(fluxList.shape[0], nSed) 

496 self.assertEqual(fluxList.shape[1], nBandpasses) 

497 

498 for ix, sedObj in enumerate(testSedList): 

499 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

500 flambda=copy.deepcopy(sedObj.flambda)) 

501 

502 for iy, bp in enumerate(testBpDict): 

503 flux = dummySed.calcFlux(bpList[iy]) 

504 self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2) 

505 

506 def testFluxArrayForSedList(self): 

507 """ 

508 Test that fluxArrayForSedList calculates the correct fluxes 

509 """ 

510 

511 nBandpasses = 7 

512 bpNameList, bpList = self.getListOfBandpasses(nBandpasses) 

513 testBpDict = BandpassDict(bpList, bpNameList) 

514 

515 nSed = 20 

516 sedNameList = self.getListOfSedNames(nSed) 

517 magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 

518 internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

519 redshiftList = self.rng.random_sample(nSed)*5.0 

520 galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

521 

522 # first, test on an SedList without a wavelenMatch 

523 testSedList = SedList(sedNameList, magNormList, 

524 fileDir=self.sedDir, 

525 internalAvList=internalAvList, 

526 redshiftList=redshiftList, 

527 galacticAvList=galacticAvList) 

528 

529 fluxArray = testBpDict.fluxArrayForSedList(testSedList) 

530 

531 for ix, sedObj in enumerate(testSedList): 

532 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

533 flambda=copy.deepcopy(sedObj.flambda)) 

534 

535 for iy, bp in enumerate(bpNameList): 

536 flux = dummySed.calcFlux(bpList[iy]) 

537 self.assertAlmostEqual(flux/fluxArray[bp][ix], 1.0, 2) 

538 

539 # now use wavelenMatch 

540 testSedList = SedList(sedNameList, magNormList, 

541 fileDir=self.sedDir, 

542 internalAvList=internalAvList, 

543 redshiftList=redshiftList, 

544 galacticAvList=galacticAvList, 

545 wavelenMatch=testBpDict.wavelenMatch) 

546 

547 fluxArray = testBpDict.fluxArrayForSedList(testSedList) 

548 

549 for ix, sedObj in enumerate(testSedList): 

550 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

551 flambda=copy.deepcopy(sedObj.flambda)) 

552 

553 for iy, bp in enumerate(bpNameList): 

554 flux = dummySed.calcFlux(bpList[iy]) 

555 self.assertAlmostEqual(flux/fluxArray[bp][ix], 1.0, 2) 

556 

557 def testIndicesOnFlux(self): 

558 """ 

559 Test that, when you pass a list of indices into the calcFluxList 

560 methods, you get the correct fluxes out. 

561 """ 

562 

563 nBandpasses = 7 

564 nameList, bpList = self.getListOfBandpasses(nBandpasses) 

565 testBpDict = BandpassDict(bpList, nameList) 

566 

567 # first try it with a single Sed 

568 wavelen = np.arange(10.0, 2000.0, 1.0) 

569 flux = (wavelen*2.0-5.0)*1.0e-6 

570 spectrum = Sed(wavelen=wavelen, flambda=flux) 

571 indices = [1, 2, 5] 

572 

573 fluxList = testBpDict.fluxListForSed(spectrum, indices=indices) 

574 ctNaN = 0 

575 for ix, (name, bp, fluxTest) in enumerate(zip(nameList, 

576 bpList, 

577 fluxList)): 

578 if ix in indices: 

579 fluxControl = spectrum.calcFlux(bp) 

580 self.assertAlmostEqual(fluxTest/fluxControl, 1.0, 2) 

581 else: 

582 ctNaN += 1 

583 np.testing.assert_equal(fluxTest, np.NaN) 

584 

585 self.assertEqual(ctNaN, 4) 

586 

587 nSed = 20 

588 sedNameList = self.getListOfSedNames(nSed) 

589 magNormList = self.rng.random_sample(nSed)*5.0 + 15.0 

590 internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

591 redshiftList = self.rng.random_sample(nSed)*5.0 

592 galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1 

593 

594 # now try a SedList without a wavelenMatch 

595 testSedList = SedList(sedNameList, magNormList, 

596 fileDir=self.sedDir, 

597 internalAvList=internalAvList, 

598 redshiftList=redshiftList, 

599 galacticAvList=galacticAvList) 

600 

601 fluxList = testBpDict.fluxListForSedList(testSedList, indices=indices) 

602 fluxArray = testBpDict.fluxArrayForSedList(testSedList, 

603 indices=indices) 

604 self.assertEqual(fluxList.shape[0], nSed) 

605 self.assertEqual(fluxList.shape[1], nBandpasses) 

606 self.assertEqual(fluxArray.shape[0], nSed) 

607 for bpname in testBpDict: 

608 self.assertEqual(len(fluxArray[bpname]), nSed) 

609 

610 for ix, sedObj in enumerate(testSedList): 

611 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

612 flambda=copy.deepcopy(sedObj.flambda)) 

613 

614 ctNaN = 0 

615 for iy, bp in enumerate(testBpDict): 

616 if iy in indices: 

617 flux = dummySed.calcFlux(testBpDict[bp]) 

618 self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2) 

619 self.assertAlmostEqual(flux/fluxArray[ix][iy], 1.0, 2) 

620 self.assertAlmostEqual(flux/fluxArray[bp][ix], 1.0, 2) 

621 else: 

622 ctNaN += 1 

623 np.testing.assert_equal(fluxList[ix][iy], np.NaN) 

624 np.testing.assert_equal(fluxArray[ix][iy], np.NaN) 

625 np.testing.assert_equal(fluxArray[bp][ix], np.NaN) 

626 

627 self.assertEqual(ctNaN, 4) 

628 

629 # now use wavelenMatch 

630 testSedList = SedList(sedNameList, magNormList, 

631 fileDir=self.sedDir, 

632 internalAvList=internalAvList, 

633 redshiftList=redshiftList, 

634 galacticAvList=galacticAvList, 

635 wavelenMatch=testBpDict.wavelenMatch) 

636 

637 fluxList = testBpDict.fluxListForSedList(testSedList, indices=indices) 

638 fluxArray = testBpDict.fluxArrayForSedList(testSedList, 

639 indices=indices) 

640 self.assertEqual(fluxList.shape[0], nSed) 

641 self.assertEqual(fluxList.shape[1], nBandpasses) 

642 self.assertEqual(fluxArray.shape[0], nSed) 

643 for bpname in testBpDict: 

644 self.assertEqual(len(fluxArray[bpname]), nSed) 

645 

646 for ix, sedObj in enumerate(testSedList): 

647 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen), 

648 flambda=copy.deepcopy(sedObj.flambda)) 

649 

650 ctNaN = 0 

651 for iy, bp in enumerate(testBpDict): 

652 if iy in indices: 

653 flux = dummySed.calcFlux(testBpDict[bp]) 

654 self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2) 

655 self.assertAlmostEqual(flux/fluxArray[ix][iy], 1.0, 2) 

656 self.assertAlmostEqual(flux/fluxArray[bp][ix], 1.0, 2) 

657 else: 

658 ctNaN += 1 

659 np.testing.assert_equal(fluxList[ix][iy], np.NaN) 

660 np.testing.assert_equal(fluxArray[ix][iy], np.NaN) 

661 np.testing.assert_equal(fluxArray[bp][ix], np.NaN) 

662 

663 self.assertEqual(ctNaN, 4) 

664 

665 def testLoadTotalBandpassesFromFiles(self): 

666 """ 

667 Test that the class method loadTotalBandpassesFromFiles produces the 

668 expected result 

669 """ 

670 

671 bandpassDir = os.path.join(getPackageDir('sims_photUtils'), 

672 'tests', 'cartoonSedTestData') 

673 bandpassNames = ['g', 'r', 'u'] 

674 bandpassRoot = 'test_bandpass_' 

675 

676 bandpassDict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames=bandpassNames, 

677 bandpassDir=bandpassDir, 

678 bandpassRoot = bandpassRoot) 

679 

680 controlBandpassList = [] 

681 for bpn in bandpassNames: 

682 dummyBp = Bandpass() 

683 dummyBp.readThroughput(os.path.join(bandpassDir, 

684 bandpassRoot+bpn+'.dat')) 

685 controlBandpassList.append(dummyBp) 

686 

687 wMin = controlBandpassList[0].wavelen[0] 

688 wMax = controlBandpassList[0].wavelen[-1] 

689 wStep = controlBandpassList[0].wavelen[1]-controlBandpassList[0].wavelen[0] 

690 

691 for bp in controlBandpassList: 

692 bp.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax, 

693 wavelen_step=wStep) 

694 

695 for test, control in zip(bandpassDict.values(), controlBandpassList): 

696 np.testing.assert_array_almost_equal(test.wavelen, 

697 control.wavelen, 19) 

698 np.testing.assert_array_almost_equal(test.sb, control.sb, 19) 

699 

700 def testLoadBandpassesFromFiles(self): 

701 """ 

702 Test that running the classmethod loadBandpassesFromFiles produces 

703 expected result 

704 """ 

705 

706 fileDir = os.path.join(getPackageDir('sims_photUtils'), 

707 'tests', 'cartoonSedTestData') 

708 bandpassNames = ['g', 'z', 'i'] 

709 bandpassRoot = 'test_bandpass_' 

710 componentList = ['toy_mirror.dat'] 

711 atmo = os.path.join(fileDir, 'toy_atmo.dat') 

712 

713 bandpassDict, hardwareDict = BandpassDict.loadBandpassesFromFiles(bandpassNames=bandpassNames, 

714 filedir=fileDir, 

715 bandpassRoot=bandpassRoot, 

716 componentList=componentList, 

717 atmoTransmission=atmo) 

718 

719 controlBandpassList = [] 

720 controlHardwareList = [] 

721 

722 for bpn in bandpassNames: 

723 componentList = [os.path.join(fileDir, bandpassRoot+bpn+'.dat'), 

724 os.path.join(fileDir, 'toy_mirror.dat')] 

725 

726 dummyBp = Bandpass() 

727 dummyBp.readThroughputList(componentList) 

728 controlHardwareList.append(dummyBp) 

729 

730 componentList = [os.path.join(fileDir, bandpassRoot+bpn+'.dat'), 

731 os.path.join(fileDir, 'toy_mirror.dat'), 

732 os.path.join(fileDir, 'toy_atmo.dat')] 

733 

734 dummyBp = Bandpass() 

735 dummyBp.readThroughputList(componentList) 

736 controlBandpassList.append(dummyBp) 

737 

738 wMin = controlBandpassList[0].wavelen[0] 

739 wMax = controlBandpassList[0].wavelen[-1] 

740 wStep = controlBandpassList[0].wavelen[1]-controlBandpassList[0].wavelen[0] 

741 

742 for bp, hh in zip(controlBandpassList, controlHardwareList): 

743 bp.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax, 

744 wavelen_step=wStep) 

745 hh.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax, 

746 wavelen_step=wStep) 

747 

748 for test, control in zip(bandpassDict.values(), controlBandpassList): 

749 np.testing.assert_array_almost_equal(test.wavelen, 

750 control.wavelen, 19) 

751 np.testing.assert_array_almost_equal(test.sb, control.sb, 19) 

752 

753 for test, control in zip(hardwareDict.values(), controlHardwareList): 

754 np.testing.assert_array_almost_equal(test.wavelen, 

755 control.wavelen, 19) 

756 np.testing.assert_array_almost_equal(test.sb, control.sb, 19) 

757 

758 

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

760 pass 

761 

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

763 lsst.utils.tests.init() 

764 unittest.main()