-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtestBandpassDict.py
764 lines (616 loc) · 31.4 KB
/
testBandpassDict.py
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
from __future__ import with_statement
from builtins import zip
from builtins import range
import unittest
import os
import copy
import numpy as np
import lsst.utils.tests
from lsst.utils import getPackageDir
from lsst.sims.photUtils import Bandpass, Sed, BandpassDict, SedList
def setup_module(module):
lsst.utils.tests.init()
class BandpassDictTest(unittest.TestCase):
def setUp(self):
self.rng = np.random.RandomState(32)
self.bandpassPossibilities = ['u', 'g', 'r', 'i', 'z', 'y']
self.bandpassDir = os.path.join(getPackageDir('throughputs'), 'baseline')
self.sedDir = os.path.join(getPackageDir('sims_photUtils'),
'tests/cartoonSedTestData/galaxySed')
self.sedPossibilities = os.listdir(self.sedDir)
def getListOfSedNames(self, nNames):
return [self.sedPossibilities[ii].replace('.gz', '')
for ii in
self.rng.randint(0, len(self.sedPossibilities)-1, nNames)]
def getListOfBandpasses(self, nBp):
"""
Generate a list of nBp bandpass names and bandpasses
Intentionally do so a nonsense order so that we can test
that order is preserved in the BandpassDict
"""
dexList = self.rng.randint(0, len(self.bandpassPossibilities)-1, nBp)
bandpassNameList = []
bandpassList = []
for dex in dexList:
name = self.bandpassPossibilities[dex]
bp = Bandpass()
bp.readThroughput(os.path.join(self.bandpassDir, 'total_%s.dat' % name))
while name in bandpassNameList:
name += '0'
bandpassNameList.append(name)
bandpassList.append(bp)
return bandpassNameList, bandpassList
def testInitialization(self):
"""
Test that all of the member variables of BandpassDict are set
to the correct value upon construction.
"""
for nBp in range(3, 10, 1):
nameList, bpList = self.getListOfBandpasses(nBp)
testDict = BandpassDict(bpList, nameList)
self.assertEqual(len(testDict), nBp)
for controlName, testName in zip(nameList, testDict):
self.assertEqual(controlName, testName)
for controlName, testName in zip(nameList, testDict.keys()):
self.assertEqual(controlName, testName)
for name, bp in zip(nameList, bpList):
np.testing.assert_array_almost_equal(bp.wavelen, testDict[name].wavelen, 10)
np.testing.assert_array_almost_equal(bp.sb, testDict[name].sb, 10)
for bpControl, bpTest in zip(bpList, testDict.values()):
np.testing.assert_array_almost_equal(bpControl.wavelen, bpTest.wavelen, 10)
np.testing.assert_array_almost_equal(bpControl.sb, bpTest.sb, 10)
def testWavelenMatch(self):
"""
Test that when you load bandpasses sampled over different
wavelength grids, they all get sampled to the same wavelength
grid.
"""
dwavList = np.arange(5.0, 25.0, 5.0)
bpList = []
bpNameList = []
for ix, dwav in enumerate(dwavList):
name = 'bp_%d' % ix
wavelen = np.arange(10.0, 1500.0, dwav)
sb = np.exp(-0.5*(np.power((wavelen-100.0*ix)/100.0, 2)))
bp = Bandpass(wavelen=wavelen, sb=sb)
bpList.append(bp)
bpNameList.append(name)
# First make sure that we have created distinct wavelength grids
for ix in range(len(bpList)):
for iy in range(ix+1, len(bpList)):
self.assertNotEqual(len(bpList[ix].wavelen), len(bpList[iy].wavelen))
testDict = BandpassDict(bpList, bpNameList)
# Now make sure that the wavelength grids in the dict were resampled, but that
# the original wavelength grids were not changed
for ix in range(len(bpList)):
np.testing.assert_array_almost_equal(testDict.values()[ix].wavelen, testDict.wavelenMatch, 19)
if ix != 0:
self.assertNotEqual(len(testDict.wavelenMatch),
len(bpList[ix].wavelen))
def testPhiArray(self):
"""
Test that the phi array is correctly calculated by BandpassDict
upon construction.
"""
for nBp in range(3, 10, 1):
nameList, bpList = self.getListOfBandpasses(nBp)
testDict = BandpassDict(bpList, nameList)
dummySed = Sed()
controlPhi, controlWavelenStep = dummySed.setupPhiArray(bpList)
np.testing.assert_array_almost_equal(controlPhi,
testDict.phiArray, 19)
self.assertAlmostEqual(controlWavelenStep,
testDict.wavelenStep, 10)
def testExceptions(self):
"""
Test that the correct exceptions are thrown by BandpassDict
"""
nameList, bpList = self.getListOfBandpasses(4)
dummyNameList = copy.deepcopy(nameList)
dummyNameList[1] = dummyNameList[0]
with self.assertRaises(RuntimeError) as context:
testDict = BandpassDict(bpList, dummyNameList)
self.assertIn('occurs twice', context.exception.args[0])
testDict = BandpassDict(bpList, nameList)
with self.assertRaises(AttributeError) as context:
testDict.phiArray = None
with self.assertRaises(AttributeError) as context:
testDict.wavelenStep = 0.9
with self.assertRaises(AttributeError) as context:
testDict.wavelenMatch = np.arange(10.0, 100.0, 1.0)
def testMagListForSed(self):
"""
Test that magListForSed calculates the correct magnitude
"""
wavelen = np.arange(10.0, 2000.0, 1.0)
flux = (wavelen*2.0-5.0)*1.0e-6
spectrum = Sed(wavelen=wavelen, flambda=flux)
for nBp in range(3, 10, 1):
nameList, bpList = self.getListOfBandpasses(nBp)
testDict = BandpassDict(bpList, nameList)
self.assertNotEqual(len(testDict.values()[0].wavelen), len(spectrum.wavelen))
magList = testDict.magListForSed(spectrum)
for ix, (name, bp, magTest) in enumerate(zip(nameList,
bpList,
magList)):
magControl = spectrum.calcMag(bp)
self.assertAlmostEqual(magTest, magControl, 5)
def testMagDictForSed(self):
"""
Test that magDictForSed calculates the correct magnitude
"""
wavelen = np.arange(10.0, 2000.0, 1.0)
flux = (wavelen*2.0-5.0)*1.0e-6
spectrum = Sed(wavelen=wavelen, flambda=flux)
for nBp in range(3, 10, 1):
nameList, bpList = self.getListOfBandpasses(nBp)
testDict = BandpassDict(bpList, nameList)
self.assertNotEqual(len(testDict.values()[0].wavelen),
len(spectrum.wavelen))
magDict = testDict.magDictForSed(spectrum)
for ix, (name, bp) in enumerate(zip(nameList, bpList)):
magControl = spectrum.calcMag(bp)
self.assertAlmostEqual(magDict[name], magControl, 5)
def testMagListForSedList(self):
"""
Test that magListForSedList calculates the correct magnitude
"""
nBandpasses = 7
bpNameList, bpList = self.getListOfBandpasses(nBandpasses)
testBpDict = BandpassDict(bpList, bpNameList)
nSed = 20
sedNameList = self.getListOfSedNames(nSed)
magNormList = self.rng.random_sample(nSed)*5.0 + 15.0
internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1
redshiftList = self.rng.random_sample(nSed)*5.0
galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1
# first, test on an SedList without a wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList)
magList = testBpDict.magListForSedList(testSedList)
self.assertEqual(magList.shape[0], nSed)
self.assertEqual(magList.shape[1], nBandpasses)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
for iy, bp in enumerate(testBpDict):
mag = dummySed.calcMag(bpList[iy])
self.assertAlmostEqual(mag, magList[ix][iy], 2)
# now use wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList,
wavelenMatch=testBpDict.wavelenMatch)
magList = testBpDict.magListForSedList(testSedList)
self.assertEqual(magList.shape[0], nSed)
self.assertEqual(magList.shape[1], nBandpasses)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
for iy, bp in enumerate(testBpDict):
mag = dummySed.calcMag(bpList[iy])
self.assertAlmostEqual(mag, magList[ix][iy], 2)
def testMagArrayForSedList(self):
"""
Test that magArrayForSedList calculates the correct magnitude
"""
nBandpasses = 7
bpNameList, bpList = self.getListOfBandpasses(nBandpasses)
testBpDict = BandpassDict(bpList, bpNameList)
nSed = 20
sedNameList = self.getListOfSedNames(nSed)
magNormList = self.rng.random_sample(nSed)*5.0 + 15.0
internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1
redshiftList = self.rng.random_sample(nSed)*5.0
galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1
# first, test on an SedList without a wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList)
magArray = testBpDict.magArrayForSedList(testSedList)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
for iy, bp in enumerate(bpNameList):
mag = dummySed.calcMag(bpList[iy])
self.assertAlmostEqual(mag, magArray[bp][ix], 2)
# now use wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList,
wavelenMatch=testBpDict.wavelenMatch)
magArray = testBpDict.magArrayForSedList(testSedList)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
for iy, bp in enumerate(bpNameList):
mag = dummySed.calcMag(bpList[iy])
self.assertAlmostEqual(mag, magArray[bp][ix], 2)
def testIndicesOnMagnitudes(self):
"""
Test that, when you pass a list of indices into the calcMagList
methods, you get the correct magnitudes out.
"""
nBandpasses = 7
nameList, bpList = self.getListOfBandpasses(nBandpasses)
testBpDict = BandpassDict(bpList, nameList)
# first try it with a single Sed
wavelen = np.arange(10.0, 2000.0, 1.0)
flux = (wavelen*2.0-5.0)*1.0e-6
spectrum = Sed(wavelen=wavelen, flambda=flux)
indices = [1, 2, 5]
magList = testBpDict.magListForSed(spectrum, indices=indices)
ctNaN = 0
for ix, (name, bp, magTest) in enumerate(zip(nameList,
bpList,
magList)):
if ix in indices:
magControl = spectrum.calcMag(bp)
self.assertAlmostEqual(magTest, magControl, 5)
else:
ctNaN += 1
np.testing.assert_equal(magTest, np.NaN)
self.assertEqual(ctNaN, 4)
nSed = 20
sedNameList = self.getListOfSedNames(nSed)
magNormList = self.rng.random_sample(nSed)*5.0 + 15.0
internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1
redshiftList = self.rng.random_sample(nSed)*5.0
galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1
# now try a SedList without a wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList)
magList = testBpDict.magListForSedList(testSedList, indices=indices)
magArray = testBpDict.magArrayForSedList(testSedList, indices=indices)
self.assertEqual(magList.shape[0], nSed)
self.assertEqual(magList.shape[1], nBandpasses)
self.assertEqual(magArray.shape[0], nSed)
for bpname in testBpDict:
self.assertEqual(len(magArray[bpname]), nSed)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
ctNaN = 0
for iy, bp in enumerate(testBpDict):
if iy in indices:
mag = dummySed.calcMag(testBpDict[bp])
self.assertAlmostEqual(mag, magList[ix][iy], 2)
self.assertAlmostEqual(mag, magArray[ix][iy], 2)
self.assertAlmostEqual(mag, magArray[bp][ix], 2)
else:
ctNaN += 1
np.testing.assert_equal(magList[ix][iy], np.NaN)
np.testing.assert_equal(magArray[ix][iy], np.NaN)
np.testing.assert_equal(magArray[bp][ix], np.NaN)
self.assertEqual(ctNaN, 4)
# now use wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList,
wavelenMatch=testBpDict.wavelenMatch)
magList = testBpDict.magListForSedList(testSedList, indices=indices)
magArray = testBpDict.magArrayForSedList(testSedList, indices=indices)
self.assertEqual(magList.shape[0], nSed)
self.assertEqual(magList.shape[1], nBandpasses)
self.assertEqual(magArray.shape[0], nSed)
for bpname in testBpDict:
self.assertEqual(len(magArray[bpname]), nSed)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
ctNaN = 0
for iy, bp in enumerate(testBpDict):
if iy in indices:
mag = dummySed.calcMag(testBpDict[bp])
self.assertAlmostEqual(mag, magList[ix][iy], 2)
self.assertAlmostEqual(mag, magArray[ix][iy], 2)
self.assertAlmostEqual(mag, magArray[bp][ix], 2)
else:
ctNaN += 1
np.testing.assert_equal(magList[ix][iy], np.NaN)
np.testing.assert_equal(magArray[ix][iy], np.NaN)
np.testing.assert_equal(magArray[bp][ix], np.NaN)
self.assertEqual(ctNaN, 4)
def testFluxListForSed(self):
"""
Test that fluxListForSed calculates the correct fluxes
"""
wavelen = np.arange(10.0, 2000.0, 1.0)
flux = (wavelen*2.0-5.0)*1.0e-6
spectrum = Sed(wavelen=wavelen, flambda=flux)
for nBp in range(3, 10, 1):
nameList, bpList = self.getListOfBandpasses(nBp)
testDict = BandpassDict(bpList, nameList)
self.assertNotEqual(len(testDict.values()[0].wavelen),
len(spectrum.wavelen))
fluxList = testDict.fluxListForSed(spectrum)
for ix, (name, bp, fluxTest) in enumerate(zip(nameList,
bpList,
fluxList)):
fluxControl = spectrum.calcFlux(bp)
self.assertAlmostEqual(fluxTest/fluxControl, 1.0, 2)
def testFluxDictForSed(self):
"""
Test that fluxDictForSed calculates the correct fluxes
"""
wavelen = np.arange(10.0, 2000.0, 1.0)
flux = (wavelen*2.0-5.0)*1.0e-6
spectrum = Sed(wavelen=wavelen, flambda=flux)
for nBp in range(3, 10, 1):
nameList, bpList = self.getListOfBandpasses(nBp)
testDict = BandpassDict(bpList, nameList)
self.assertNotEqual(len(testDict.values()[0].wavelen),
len(spectrum.wavelen))
fluxDict = testDict.fluxDictForSed(spectrum)
for ix, (name, bp) in enumerate(zip(nameList, bpList)):
fluxControl = spectrum.calcFlux(bp)
self.assertAlmostEqual(fluxDict[name]/fluxControl, 1.0, 2)
def testFluxListForSedList(self):
"""
Test that fluxListForSedList calculates the correct fluxes
"""
nBandpasses = 7
bpNameList, bpList = self.getListOfBandpasses(nBandpasses)
testBpDict = BandpassDict(bpList, bpNameList)
nSed = 20
sedNameList = self.getListOfSedNames(nSed)
magNormList = self.rng.random_sample(nSed)*5.0 + 15.0
internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1
redshiftList = self.rng.random_sample(nSed)*5.0
galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1
# first, test on an SedList without a wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList)
fluxList = testBpDict.fluxListForSedList(testSedList)
self.assertEqual(fluxList.shape[0], nSed)
self.assertEqual(fluxList.shape[1], nBandpasses)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
for iy, bp in enumerate(testBpDict):
flux = dummySed.calcFlux(bpList[iy])
self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2)
# now use wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList,
wavelenMatch=testBpDict.wavelenMatch)
fluxList = testBpDict.fluxListForSedList(testSedList)
self.assertEqual(fluxList.shape[0], nSed)
self.assertEqual(fluxList.shape[1], nBandpasses)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
for iy, bp in enumerate(testBpDict):
flux = dummySed.calcFlux(bpList[iy])
self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2)
def testFluxArrayForSedList(self):
"""
Test that fluxArrayForSedList calculates the correct fluxes
"""
nBandpasses = 7
bpNameList, bpList = self.getListOfBandpasses(nBandpasses)
testBpDict = BandpassDict(bpList, bpNameList)
nSed = 20
sedNameList = self.getListOfSedNames(nSed)
magNormList = self.rng.random_sample(nSed)*5.0 + 15.0
internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1
redshiftList = self.rng.random_sample(nSed)*5.0
galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1
# first, test on an SedList without a wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList)
fluxArray = testBpDict.fluxArrayForSedList(testSedList)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
for iy, bp in enumerate(bpNameList):
flux = dummySed.calcFlux(bpList[iy])
self.assertAlmostEqual(flux/fluxArray[bp][ix], 1.0, 2)
# now use wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList,
wavelenMatch=testBpDict.wavelenMatch)
fluxArray = testBpDict.fluxArrayForSedList(testSedList)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
for iy, bp in enumerate(bpNameList):
flux = dummySed.calcFlux(bpList[iy])
self.assertAlmostEqual(flux/fluxArray[bp][ix], 1.0, 2)
def testIndicesOnFlux(self):
"""
Test that, when you pass a list of indices into the calcFluxList
methods, you get the correct fluxes out.
"""
nBandpasses = 7
nameList, bpList = self.getListOfBandpasses(nBandpasses)
testBpDict = BandpassDict(bpList, nameList)
# first try it with a single Sed
wavelen = np.arange(10.0, 2000.0, 1.0)
flux = (wavelen*2.0-5.0)*1.0e-6
spectrum = Sed(wavelen=wavelen, flambda=flux)
indices = [1, 2, 5]
fluxList = testBpDict.fluxListForSed(spectrum, indices=indices)
ctNaN = 0
for ix, (name, bp, fluxTest) in enumerate(zip(nameList,
bpList,
fluxList)):
if ix in indices:
fluxControl = spectrum.calcFlux(bp)
self.assertAlmostEqual(fluxTest/fluxControl, 1.0, 2)
else:
ctNaN += 1
np.testing.assert_equal(fluxTest, np.NaN)
self.assertEqual(ctNaN, 4)
nSed = 20
sedNameList = self.getListOfSedNames(nSed)
magNormList = self.rng.random_sample(nSed)*5.0 + 15.0
internalAvList = self.rng.random_sample(nSed)*0.3 + 0.1
redshiftList = self.rng.random_sample(nSed)*5.0
galacticAvList = self.rng.random_sample(nSed)*0.3 + 0.1
# now try a SedList without a wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList)
fluxList = testBpDict.fluxListForSedList(testSedList, indices=indices)
fluxArray = testBpDict.fluxArrayForSedList(testSedList,
indices=indices)
self.assertEqual(fluxList.shape[0], nSed)
self.assertEqual(fluxList.shape[1], nBandpasses)
self.assertEqual(fluxArray.shape[0], nSed)
for bpname in testBpDict:
self.assertEqual(len(fluxArray[bpname]), nSed)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
ctNaN = 0
for iy, bp in enumerate(testBpDict):
if iy in indices:
flux = dummySed.calcFlux(testBpDict[bp])
self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2)
self.assertAlmostEqual(flux/fluxArray[ix][iy], 1.0, 2)
self.assertAlmostEqual(flux/fluxArray[bp][ix], 1.0, 2)
else:
ctNaN += 1
np.testing.assert_equal(fluxList[ix][iy], np.NaN)
np.testing.assert_equal(fluxArray[ix][iy], np.NaN)
np.testing.assert_equal(fluxArray[bp][ix], np.NaN)
self.assertEqual(ctNaN, 4)
# now use wavelenMatch
testSedList = SedList(sedNameList, magNormList,
fileDir=self.sedDir,
internalAvList=internalAvList,
redshiftList=redshiftList,
galacticAvList=galacticAvList,
wavelenMatch=testBpDict.wavelenMatch)
fluxList = testBpDict.fluxListForSedList(testSedList, indices=indices)
fluxArray = testBpDict.fluxArrayForSedList(testSedList,
indices=indices)
self.assertEqual(fluxList.shape[0], nSed)
self.assertEqual(fluxList.shape[1], nBandpasses)
self.assertEqual(fluxArray.shape[0], nSed)
for bpname in testBpDict:
self.assertEqual(len(fluxArray[bpname]), nSed)
for ix, sedObj in enumerate(testSedList):
dummySed = Sed(wavelen=copy.deepcopy(sedObj.wavelen),
flambda=copy.deepcopy(sedObj.flambda))
ctNaN = 0
for iy, bp in enumerate(testBpDict):
if iy in indices:
flux = dummySed.calcFlux(testBpDict[bp])
self.assertAlmostEqual(flux/fluxList[ix][iy], 1.0, 2)
self.assertAlmostEqual(flux/fluxArray[ix][iy], 1.0, 2)
self.assertAlmostEqual(flux/fluxArray[bp][ix], 1.0, 2)
else:
ctNaN += 1
np.testing.assert_equal(fluxList[ix][iy], np.NaN)
np.testing.assert_equal(fluxArray[ix][iy], np.NaN)
np.testing.assert_equal(fluxArray[bp][ix], np.NaN)
self.assertEqual(ctNaN, 4)
def testLoadTotalBandpassesFromFiles(self):
"""
Test that the class method loadTotalBandpassesFromFiles produces the
expected result
"""
bandpassDir = os.path.join(getPackageDir('sims_photUtils'),
'tests', 'cartoonSedTestData')
bandpassNames = ['g', 'r', 'u']
bandpassRoot = 'test_bandpass_'
bandpassDict = BandpassDict.loadTotalBandpassesFromFiles(bandpassNames=bandpassNames,
bandpassDir=bandpassDir,
bandpassRoot = bandpassRoot)
controlBandpassList = []
for bpn in bandpassNames:
dummyBp = Bandpass()
dummyBp.readThroughput(os.path.join(bandpassDir,
bandpassRoot+bpn+'.dat'))
controlBandpassList.append(dummyBp)
wMin = controlBandpassList[0].wavelen[0]
wMax = controlBandpassList[0].wavelen[-1]
wStep = controlBandpassList[0].wavelen[1]-controlBandpassList[0].wavelen[0]
for bp in controlBandpassList:
bp.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax,
wavelen_step=wStep)
for test, control in zip(bandpassDict.values(), controlBandpassList):
np.testing.assert_array_almost_equal(test.wavelen,
control.wavelen, 19)
np.testing.assert_array_almost_equal(test.sb, control.sb, 19)
def testLoadBandpassesFromFiles(self):
"""
Test that running the classmethod loadBandpassesFromFiles produces
expected result
"""
fileDir = os.path.join(getPackageDir('sims_photUtils'),
'tests', 'cartoonSedTestData')
bandpassNames = ['g', 'z', 'i']
bandpassRoot = 'test_bandpass_'
componentList = ['toy_mirror.dat']
atmo = os.path.join(fileDir, 'toy_atmo.dat')
bandpassDict, hardwareDict = BandpassDict.loadBandpassesFromFiles(bandpassNames=bandpassNames,
filedir=fileDir,
bandpassRoot=bandpassRoot,
componentList=componentList,
atmoTransmission=atmo)
controlBandpassList = []
controlHardwareList = []
for bpn in bandpassNames:
componentList = [os.path.join(fileDir, bandpassRoot+bpn+'.dat'),
os.path.join(fileDir, 'toy_mirror.dat')]
dummyBp = Bandpass()
dummyBp.readThroughputList(componentList)
controlHardwareList.append(dummyBp)
componentList = [os.path.join(fileDir, bandpassRoot+bpn+'.dat'),
os.path.join(fileDir, 'toy_mirror.dat'),
os.path.join(fileDir, 'toy_atmo.dat')]
dummyBp = Bandpass()
dummyBp.readThroughputList(componentList)
controlBandpassList.append(dummyBp)
wMin = controlBandpassList[0].wavelen[0]
wMax = controlBandpassList[0].wavelen[-1]
wStep = controlBandpassList[0].wavelen[1]-controlBandpassList[0].wavelen[0]
for bp, hh in zip(controlBandpassList, controlHardwareList):
bp.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax,
wavelen_step=wStep)
hh.resampleBandpass(wavelen_min=wMin, wavelen_max=wMax,
wavelen_step=wStep)
for test, control in zip(bandpassDict.values(), controlBandpassList):
np.testing.assert_array_almost_equal(test.wavelen,
control.wavelen, 19)
np.testing.assert_array_almost_equal(test.sb, control.sb, 19)
for test, control in zip(hardwareDict.values(), controlHardwareList):
np.testing.assert_array_almost_equal(test.wavelen,
control.wavelen, 19)
np.testing.assert_array_almost_equal(test.sb, control.sb, 19)
class MemoryTestClass(lsst.utils.tests.MemoryTestCase):
pass
if __name__ == "__main__":
lsst.utils.tests.init()
unittest.main()