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 str 

3import numpy as np 

4import unittest 

5import lsst.utils.tests 

6import warnings 

7import os 

8import gzip 

9from lsst.sims.photUtils.Sed import Sed 

10from lsst.sims.catUtils.IGM import ApplyIGM 

11from lsst.utils import getPackageDir 

12 

13 

14def setup_module(module): 

15 lsst.utils.tests.init() 

16 

17 

18class TestApplyIGM(unittest.TestCase): 

19 

20 def testInitializeIGM(self): 

21 

22 "Test Initialization Method" 

23 

24 # Make sure that if we initialize IGM with new inputs that it 

25 # is initializing with them 

26 testIGM = ApplyIGM() 

27 testSed = Sed() 

28 sedName = os.path.join(getPackageDir('sims_sed_library'), 'galaxySED') 

29 testSed.readSED_flambda(os.path.join(sedName, 

30 'Burst.10E08.002Z.spec.gz')) 

31 testIGM.applyIGM(1.8, testSed) 

32 testZmin = 1.8 

33 testZmax = 2.2 

34 # Want new values for testing, 

35 # so make sure we are not just putting in the same values 

36 # as are already there 

37 self.assertNotEqual(testZmin, testIGM.zMin) 

38 self.assertNotEqual(testZmax, testIGM.zMax) 

39 testIGM.initializeIGM(zMin = testZmin, zMax = testZmax) 

40 self.assertEqual(testZmin, testIGM.zMin) 

41 self.assertEqual(testZmax, testIGM.zMax) 

42 

43 def testLoadTables(self): 

44 

45 "Test Read-in of IGM Lookup Tables" 

46 

47 tableDirectory = str(getPackageDir('sims_catUtils') + 

48 '/python/lsst/sims/catUtils/IGM/igm_tables') 

49 # First make sure that if variance option is turned on but 

50 # there are no variance files that the correct error is raised 

51 testIGM = ApplyIGM() 

52 testIGM.initializeIGM(zMax = 1.5) 

53 with gzip.open('MeanLookupTable_zSource1.5.tbl.gz', 'wt') as testMeanLookupTable: 

54 testMeanLookupTable.write('300.0 0.9999\n') 

55 self.assertRaisesRegex(IOError, "Cannot find variance tables.", 

56 testIGM.loadTables, os.getcwd()) 

57 os.remove('MeanLookupTable_zSource1.5.tbl.gz') 

58 

59 # Then make sure that the mean lookup tables and var lookup 

60 # tables all get loaded into proper dicts 

61 testIGMDicts = ApplyIGM() 

62 testIGMDicts.initializeIGM() 

63 testIGMDicts.loadTables(tableDirectory) 

64 redshiftValues = ['1.5', '1.6', '1.7', '1.8', '1.9', '2.0', 

65 '2.1', '2.2', '2.3', '2.4', '2.5', 

66 '2.6', '2.7', '2.8', '2.9'] 

67 

68 # Python 3 replaces assertItemsEqual() with assertCountEqual() 

69 if hasattr(self, 'assertItemsEqual'): 

70 self.assertItemsEqual(list(testIGMDicts.meanLookups.keys()), redshiftValues) 

71 self.assertItemsEqual(list(testIGMDicts.varLookups.keys()), redshiftValues) 

72 else: 

73 self.assertCountEqual(list(testIGMDicts.meanLookups.keys()), redshiftValues) 

74 self.assertCountEqual(list(testIGMDicts.varLookups.keys()), redshiftValues) 

75 

76 # Finally make sure that if Variance Boolean is false that 

77 # nothing is passed in to varLookups 

78 testIGMVar = ApplyIGM() 

79 testIGMVar.initializeIGM() 

80 testIGMVar.loadTables(tableDirectory, varianceTbl = False) 

81 self.assertEqual(testIGMVar.varLookups, {}) 

82 

83 def testApplyIGM(self): 

84 

85 """Test application of IGM from Lookup Tables to SED objects""" 

86 

87 # Test that a warning comes up if input redshift is out 

88 # of range and that no changes occurs to SED 

89 testSed = Sed() 

90 sedName = os.path.join(getPackageDir('sims_sed_library'), 'galaxySED') 

91 testSed.readSED_flambda(os.path.join(sedName, 

92 'Burst.10E08.002Z.spec.gz')) 

93 testFlambda = [] 

94 for fVal in testSed.flambda: 

95 testFlambda.append(fVal) 

96 testIGM = ApplyIGM() 

97 testIGM.initializeIGM() 

98 with warnings.catch_warnings(record=True) as wa: 

99 testIGM.applyIGM(1.1, testSed) 

100 self.assertEqual(len(wa), 1) 

101 self.assertIn('IGM Lookup tables', str(wa[-1].message)) 

102 np.testing.assert_equal(testFlambda, testSed.flambda) 

103 

104 # Test that lookup table is read in correctly 

105 testTable15 = np.genfromtxt(str(getPackageDir('sims_catUtils') + 

106 '/python/lsst/sims/catUtils/IGM/igm_tables/' + 

107 'MeanLookupTable_zSource1.5.tbl.gz')) 

108 np.testing.assert_equal(testTable15, testIGM.meanLookups['1.5']) 

109 

110 # Test output by making sure that an incoming sed 

111 # with flambda = 1.0 everywhere will return the 

112 # transmission values of the lookup table as its 

113 # flambda output 

114 testSed.setSED(testSed.wavelen, flambda=np.ones(len(testSed.wavelen))) 

115 testIGM.applyIGM(1.5, testSed) 

116 testTable15Above300 = testTable15[np.where(testTable15[:, 0] >= 300.0)] 

117 testSed.resampleSED(wavelen_match = testTable15Above300[:, 0]) 

118 np.testing.assert_allclose(testTable15Above300[:, 1], 

119 testSed.flambda, 1e-4) 

120 

121 

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

123 pass 

124 

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

126 lsst.utils.tests.init() 

127 unittest.main()