Coverage for tests/testBandpassDict.py : 6%

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
13def setup_module(module):
14 lsst.utils.tests.init()
17class BandpassDictTest(unittest.TestCase):
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)
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)]
32 def getListOfBandpasses(self, nBp):
33 """
34 Generate a list of nBp bandpass names and bandpasses
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)
51 return bandpassNameList, bandpassList
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 """
59 for nBp in range(3, 10, 1):
60 nameList, bpList = self.getListOfBandpasses(nBp)
61 testDict = BandpassDict(bpList, nameList)
63 self.assertEqual(len(testDict), nBp)
65 for controlName, testName in zip(nameList, testDict):
66 self.assertEqual(controlName, testName)
68 for controlName, testName in zip(nameList, testDict.keys()):
69 self.assertEqual(controlName, testName)
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)
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)
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)
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))
101 testDict = BandpassDict(bpList, bpNameList)
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))
111 def testPhiArray(self):
112 """
113 Test that the phi array is correctly calculated by BandpassDict
114 upon construction.
115 """
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)
127 def testExceptions(self):
128 """
129 Test that the correct exceptions are thrown by BandpassDict
130 """
132 nameList, bpList = self.getListOfBandpasses(4)
133 dummyNameList = copy.deepcopy(nameList)
134 dummyNameList[1] = dummyNameList[0]
136 with self.assertRaises(RuntimeError) as context:
137 testDict = BandpassDict(bpList, dummyNameList)
139 self.assertIn('occurs twice', context.exception.args[0])
141 testDict = BandpassDict(bpList, nameList)
143 with self.assertRaises(AttributeError) as context:
144 testDict.phiArray = None
146 with self.assertRaises(AttributeError) as context:
147 testDict.wavelenStep = 0.9
149 with self.assertRaises(AttributeError) as context:
150 testDict.wavelenMatch = np.arange(10.0, 100.0, 1.0)
152 def testMagListForSed(self):
153 """
154 Test that magListForSed calculates the correct magnitude
155 """
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)
161 for nBp in range(3, 10, 1):
163 nameList, bpList = self.getListOfBandpasses(nBp)
164 testDict = BandpassDict(bpList, nameList)
165 self.assertNotEqual(len(testDict.values()[0].wavelen), len(spectrum.wavelen))
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)
174 def testMagDictForSed(self):
175 """
176 Test that magDictForSed calculates the correct magnitude
177 """
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)
183 for nBp in range(3, 10, 1):
185 nameList, bpList = self.getListOfBandpasses(nBp)
186 testDict = BandpassDict(bpList, nameList)
187 self.assertNotEqual(len(testDict.values()[0].wavelen),
188 len(spectrum.wavelen))
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)
195 def testMagListForSedList(self):
196 """
197 Test that magListForSedList calculates the correct magnitude
198 """
200 nBandpasses = 7
201 bpNameList, bpList = self.getListOfBandpasses(nBandpasses)
202 testBpDict = BandpassDict(bpList, bpNameList)
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
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)
218 magList = testBpDict.magListForSedList(testSedList)
219 self.assertEqual(magList.shape[0], nSed)
220 self.assertEqual(magList.shape[1], nBandpasses)
222 for ix, sedObj in enumerate(testSedList):
223 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
224 flambda=copy.deepcopy(sedObj.flambda))
226 for iy, bp in enumerate(testBpDict):
227 mag = dummySed.calcMag(bpList[iy])
228 self.assertAlmostEqual(mag, magList[ix][iy], 2)
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)
238 magList = testBpDict.magListForSedList(testSedList)
239 self.assertEqual(magList.shape[0], nSed)
240 self.assertEqual(magList.shape[1], nBandpasses)
242 for ix, sedObj in enumerate(testSedList):
243 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
244 flambda=copy.deepcopy(sedObj.flambda))
246 for iy, bp in enumerate(testBpDict):
247 mag = dummySed.calcMag(bpList[iy])
248 self.assertAlmostEqual(mag, magList[ix][iy], 2)
250 def testMagArrayForSedList(self):
251 """
252 Test that magArrayForSedList calculates the correct magnitude
253 """
255 nBandpasses = 7
256 bpNameList, bpList = self.getListOfBandpasses(nBandpasses)
257 testBpDict = BandpassDict(bpList, bpNameList)
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
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)
273 magArray = testBpDict.magArrayForSedList(testSedList)
275 for ix, sedObj in enumerate(testSedList):
276 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
277 flambda=copy.deepcopy(sedObj.flambda))
279 for iy, bp in enumerate(bpNameList):
280 mag = dummySed.calcMag(bpList[iy])
281 self.assertAlmostEqual(mag, magArray[bp][ix], 2)
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)
291 magArray = testBpDict.magArrayForSedList(testSedList)
293 for ix, sedObj in enumerate(testSedList):
294 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
295 flambda=copy.deepcopy(sedObj.flambda))
297 for iy, bp in enumerate(bpNameList):
298 mag = dummySed.calcMag(bpList[iy])
299 self.assertAlmostEqual(mag, magArray[bp][ix], 2)
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 """
307 nBandpasses = 7
308 nameList, bpList = self.getListOfBandpasses(nBandpasses)
309 testBpDict = BandpassDict(bpList, nameList)
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]
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)
329 self.assertEqual(ctNaN, 4)
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
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)
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)
353 for ix, sedObj in enumerate(testSedList):
354 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
355 flambda=copy.deepcopy(sedObj.flambda))
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)
370 self.assertEqual(ctNaN, 4)
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)
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)
388 for ix, sedObj in enumerate(testSedList):
389 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
390 flambda=copy.deepcopy(sedObj.flambda))
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)
405 self.assertEqual(ctNaN, 4)
407 def testFluxListForSed(self):
408 """
409 Test that fluxListForSed calculates the correct fluxes
410 """
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)
416 for nBp in range(3, 10, 1):
418 nameList, bpList = self.getListOfBandpasses(nBp)
419 testDict = BandpassDict(bpList, nameList)
420 self.assertNotEqual(len(testDict.values()[0].wavelen),
421 len(spectrum.wavelen))
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)
430 def testFluxDictForSed(self):
431 """
432 Test that fluxDictForSed calculates the correct fluxes
433 """
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)
439 for nBp in range(3, 10, 1):
441 nameList, bpList = self.getListOfBandpasses(nBp)
442 testDict = BandpassDict(bpList, nameList)
443 self.assertNotEqual(len(testDict.values()[0].wavelen),
444 len(spectrum.wavelen))
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)
451 def testFluxListForSedList(self):
452 """
453 Test that fluxListForSedList calculates the correct fluxes
454 """
456 nBandpasses = 7
457 bpNameList, bpList = self.getListOfBandpasses(nBandpasses)
458 testBpDict = BandpassDict(bpList, bpNameList)
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
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)
474 fluxList = testBpDict.fluxListForSedList(testSedList)
475 self.assertEqual(fluxList.shape[0], nSed)
476 self.assertEqual(fluxList.shape[1], nBandpasses)
478 for ix, sedObj in enumerate(testSedList):
479 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
480 flambda=copy.deepcopy(sedObj.flambda))
482 for iy, bp in enumerate(testBpDict):
483 flux = dummySed.calcFlux(bpList[iy])
484 self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2)
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)
494 fluxList = testBpDict.fluxListForSedList(testSedList)
495 self.assertEqual(fluxList.shape[0], nSed)
496 self.assertEqual(fluxList.shape[1], nBandpasses)
498 for ix, sedObj in enumerate(testSedList):
499 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
500 flambda=copy.deepcopy(sedObj.flambda))
502 for iy, bp in enumerate(testBpDict):
503 flux = dummySed.calcFlux(bpList[iy])
504 self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2)
506 def testFluxArrayForSedList(self):
507 """
508 Test that fluxArrayForSedList calculates the correct fluxes
509 """
511 nBandpasses = 7
512 bpNameList, bpList = self.getListOfBandpasses(nBandpasses)
513 testBpDict = BandpassDict(bpList, bpNameList)
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
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)
529 fluxArray = testBpDict.fluxArrayForSedList(testSedList)
531 for ix, sedObj in enumerate(testSedList):
532 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
533 flambda=copy.deepcopy(sedObj.flambda))
535 for iy, bp in enumerate(bpNameList):
536 flux = dummySed.calcFlux(bpList[iy])
537 self.assertAlmostEqual(flux/fluxArray[bp][ix], 1.0, 2)
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)
547 fluxArray = testBpDict.fluxArrayForSedList(testSedList)
549 for ix, sedObj in enumerate(testSedList):
550 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
551 flambda=copy.deepcopy(sedObj.flambda))
553 for iy, bp in enumerate(bpNameList):
554 flux = dummySed.calcFlux(bpList[iy])
555 self.assertAlmostEqual(flux/fluxArray[bp][ix], 1.0, 2)
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 """
563 nBandpasses = 7
564 nameList, bpList = self.getListOfBandpasses(nBandpasses)
565 testBpDict = BandpassDict(bpList, nameList)
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]
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)
585 self.assertEqual(ctNaN, 4)
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
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)
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)
610 for ix, sedObj in enumerate(testSedList):
611 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
612 flambda=copy.deepcopy(sedObj.flambda))
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)
627 self.assertEqual(ctNaN, 4)
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)
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)
646 for ix, sedObj in enumerate(testSedList):
647 dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
648 flambda=copy.deepcopy(sedObj.flambda))
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)
663 self.assertEqual(ctNaN, 4)
665 def testLoadTotalBandpassesFromFiles(self):
666 """
667 Test that the class method loadTotalBandpassesFromFiles produces the
668 expected result
669 """
671 bandpassDir = os.path.join(getPackageDir('sims_photUtils'),
672 'tests', 'cartoonSedTestData')
673 bandpassNames = ['g', 'r', 'u']
674 bandpassRoot = 'test_bandpass_'
676 bandpassDict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames=bandpassNames,
677 bandpassDir=bandpassDir,
678 bandpassRoot = bandpassRoot)
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)
687 wMin = controlBandpassList[0].wavelen[0]
688 wMax = controlBandpassList[0].wavelen[-1]
689 wStep = controlBandpassList[0].wavelen[1]-controlBandpassList[0].wavelen[0]
691 for bp in controlBandpassList:
692 bp.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax,
693 wavelen_step=wStep)
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)
700 def testLoadBandpassesFromFiles(self):
701 """
702 Test that running the classmethod loadBandpassesFromFiles produces
703 expected result
704 """
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')
713 bandpassDict, hardwareDict = BandpassDict.loadBandpassesFromFiles(bandpassNames=bandpassNames,
714 filedir=fileDir,
715 bandpassRoot=bandpassRoot,
716 componentList=componentList,
717 atmoTransmission=atmo)
719 controlBandpassList = []
720 controlHardwareList = []
722 for bpn in bandpassNames:
723 componentList = [os.path.join(fileDir, bandpassRoot+bpn+'.dat'),
724 os.path.join(fileDir, 'toy_mirror.dat')]
726 dummyBp = Bandpass()
727 dummyBp.readThroughputList(componentList)
728 controlHardwareList.append(dummyBp)
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')]
734 dummyBp = Bandpass()
735 dummyBp.readThroughputList(componentList)
736 controlBandpassList.append(dummyBp)
738 wMin = controlBandpassList[0].wavelen[0]
739 wMax = controlBandpassList[0].wavelen[-1]
740 wStep = controlBandpassList[0].wavelen[1]-controlBandpassList[0].wavelen[0]
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)
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)
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)
759class MemoryTestClass(lsst.utils.tests.MemoryTestCase):
760 pass
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()