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 

2import os 

3import numpy as np 

4import unittest 

5import lsst.utils 

6import lsst.utils.tests 

7 

8from lsst.sims.photUtils import Bandpass, Sed, PhotometricParameters, PhysicalParameters 

9 

10 

11def setup_module(module): 

12 lsst.utils.tests.init() 

13 

14 

15class PhotometricParametersUnitTest(unittest.TestCase): 

16 

17 def testInit(self): 

18 """ 

19 Test that the init and getters of PhotometricParameters work 

20 properly 

21 """ 

22 defaults = PhotometricParameters() 

23 params = ['exptime', 'nexp', 'effarea', 

24 'gain', 'readnoise', 'darkcurrent', 

25 'othernoise', 'platescale', 'sigmaSys'] 

26 

27 for attribute in params: 

28 kwargs = {} 

29 kwargs[attribute] = -100.0 

30 testCase = PhotometricParameters(**kwargs) 

31 

32 for pp in params: 

33 if pp != attribute: 

34 self.assertEqual(defaults.__getattribute__(pp), 

35 testCase.__getattribute__(pp)) 

36 else: 

37 self.assertNotEqual(defaults.__getattribute__(pp), 

38 testCase.__getattribute__(pp)) 

39 

40 self.assertEqual(testCase.__getattribute__(pp), -100.0) 

41 

42 def testExceptions(self): 

43 """ 

44 Test that exceptions get raised when they ought to by the 

45 PhotometricParameters constructor 

46 

47 We will instantiate PhotometricParametrs with different incomplete 

48 lists of parameters set. We will verify that the returned 

49 error messages correctly point out which parameters were ignored. 

50 """ 

51 

52 expectedMessage = {'exptime': 'did not set exptime', 

53 'nexp': 'did not set nexp', 

54 'effarea': 'did not set effarea', 

55 'gain': 'did not set gain', 

56 'platescale': 'did not set platescale', 

57 'sigmaSys': 'did not set sigmaSys', 

58 'readnoise': 'did not set readnoise', 

59 'darkcurrent': 'did not set darkcurrent', 

60 'othernoise': 'did not set othernoise'} 

61 

62 with self.assertRaises(RuntimeError) as context: 

63 PhotometricParameters(bandpass='x') 

64 

65 for name in expectedMessage: 

66 self.assertIn(expectedMessage[name], context.exception.args[0]) 

67 

68 for name1 in expectedMessage: 

69 for name2 in expectedMessage: 

70 setParameters = {name1: 2.0, name2: 2.0} 

71 with self.assertRaises(RuntimeError) as context: 

72 PhotometricParameters(bandpass='x', **setParameters) 

73 

74 for name3 in expectedMessage: 

75 if name3 not in setParameters: 

76 self.assertIn(expectedMessage[name3], context.exception.args[0]) 

77 else: 

78 self.assertNotIn(expectedMessage[name3], context.exception.args[0]) 

79 

80 def testDefaults(self): 

81 """ 

82 Test that PhotometricParameters are correctly assigned to defaults 

83 """ 

84 bandpassNames = ['u', 'g', 'r', 'i', 'z', 'y', None] 

85 for bp in bandpassNames: 

86 photParams = PhotometricParameters(bandpass=bp) 

87 self.assertEqual(photParams.bandpass, bp) 

88 self.assertAlmostEqual(photParams.exptime, 15.0, 7) 

89 self.assertAlmostEqual(photParams.nexp, 2, 7) 

90 self.assertAlmostEqual(photParams.effarea/(np.pi*(6.423*100/2.0)**2), 1.0, 7) 

91 self.assertAlmostEqual(photParams.gain, 2.3, 7) 

92 self.assertAlmostEqual(photParams.darkcurrent, 0.2, 7) 

93 self.assertAlmostEqual(photParams.readnoise, 8.8, 7) 

94 self.assertAlmostEqual(photParams.othernoise, 0, 7) 

95 self.assertAlmostEqual(photParams.platescale, 0.2, 7) 

96 if bp not in ['u', 'z', 'y']: 

97 self.assertAlmostEqual(photParams.sigmaSys, 0.005, 7) 

98 else: 

99 self.assertAlmostEqual(photParams.sigmaSys, 0.0075, 7) 

100 

101 def testNoBandpass(self): 

102 """ 

103 Test that if no bandpass is set, bandpass stays 'None' even after all other 

104 parameters are assigned. 

105 """ 

106 photParams = PhotometricParameters() 

107 self.assertEqual(photParams.bandpass, None) 

108 self.assertAlmostEqual(photParams.exptime, 15.0, 7) 

109 self.assertAlmostEqual(photParams.nexp, 2, 7) 

110 self.assertAlmostEqual(photParams.effarea/(np.pi*(6.423*100/2.0)**2), 1.0, 7) 

111 self.assertAlmostEqual(photParams.gain, 2.3, 7) 

112 self.assertAlmostEqual(photParams.darkcurrent, 0.2, 7) 

113 self.assertAlmostEqual(photParams.readnoise, 8.8, 7) 

114 self.assertAlmostEqual(photParams.othernoise, 0, 7) 

115 self.assertAlmostEqual(photParams.platescale, 0.2, 7) 

116 self.assertAlmostEqual(photParams.sigmaSys, 0.005, 7) 

117 

118 def testAssignment(self): 

119 """ 

120 Test that it is impossible to set PhotometricParameters on the fly 

121 """ 

122 testCase = PhotometricParameters() 

123 controlCase = PhotometricParameters() 

124 success = 0 

125 

126 msg = '' 

127 try: 

128 testCase.exptime = -1.0 

129 success += 1 

130 msg += 'was able to assign exptime; ' 

131 except: 

132 self.assertEqual(testCase.exptime, controlCase.exptime) 

133 

134 try: 

135 testCase.nexp = -1.0 

136 success += 1 

137 msg += 'was able to assign nexp; ' 

138 except: 

139 self.assertEqual(testCase.nexp, controlCase.nexp) 

140 

141 try: 

142 testCase.effarea = -1.0 

143 success += 1 

144 msg += 'was able to assign effarea; ' 

145 except: 

146 self.assertEqual(testCase.effarea, controlCase.effarea) 

147 

148 try: 

149 testCase.gain = -1.0 

150 success += 1 

151 msg += 'was able to assign gain; ' 

152 except: 

153 self.assertEqual(testCase.gain, controlCase.gain) 

154 

155 try: 

156 testCase.readnoise = -1.0 

157 success += 1 

158 msg += 'was able to assign readnoise; ' 

159 except: 

160 self.assertEqual(testCase.readnoise, controlCase.readnoise) 

161 

162 try: 

163 testCase.darkcurrent = -1.0 

164 success += 1 

165 msg += 'was able to assign darkcurrent; ' 

166 except: 

167 self.assertEqual(testCase.darkcurrent, controlCase.darkcurrent) 

168 

169 try: 

170 testCase.othernoise = -1.0 

171 success += 1 

172 msg += 'was able to assign othernoise; ' 

173 except: 

174 self.assertEqual(testCase.othernoise, controlCase.othernoise) 

175 

176 try: 

177 testCase.platescale = -1.0 

178 success += 1 

179 msg += 'was able to assign platescale; ' 

180 except: 

181 self.assertEqual(testCase.platescale, controlCase.platescale) 

182 

183 try: 

184 testCase.sigmaSys = -1.0 

185 success += 1 

186 msg += 'was able to assign sigmaSys; ' 

187 except: 

188 self.assertEqual(testCase.sigmaSys, controlCase.sigmaSys) 

189 

190 try: 

191 testCase.bandpass = 'z' 

192 success += 1 

193 msg += 'was able to assign bandpass; ' 

194 except: 

195 self.assertEqual(testCase.bandpass, controlCase.bandpass) 

196 

197 self.assertEqual(success, 0, msg=msg) 

198 

199 def testApplication(self): 

200 """ 

201 Test that PhotometricParameters get properly propagated into 

202 Sed methods. We will test this using Sed.calcADU, since the ADU 

203 scale linearly with the appropriate parameter. 

204 """ 

205 

206 testSed = Sed() 

207 testSed.setFlatSED() 

208 

209 testBandpass = Bandpass() 

210 testBandpass.readThroughput(os.path.join(lsst.utils.getPackageDir('throughputs'), 

211 'baseline', 'total_g.dat')) 

212 

213 control = testSed.calcADU(testBandpass, 

214 photParams=PhotometricParameters()) 

215 

216 testCase = PhotometricParameters(exptime=30.0) 

217 

218 test = testSed.calcADU(testBandpass, photParams=testCase) 

219 

220 self.assertGreater(control, 0.0) 

221 self.assertEqual(control, 0.5*test) 

222 

223 

224class PhysicalParametersUnitTest(unittest.TestCase): 

225 

226 def testAssignment(self): 

227 """ 

228 Make sure it is impossible to change the values stored in 

229 PhysicalParameters 

230 """ 

231 

232 pp = PhysicalParameters() 

233 control = PhysicalParameters() 

234 success = 0 

235 msg = '' 

236 

237 try: 

238 pp.minwavelen = 2.0 

239 success += 1 

240 msg += 'was able to assign minwavelen; ' 

241 except: 

242 self.assertEqual(pp.minwavelen, control.minwavelen) 

243 

244 try: 

245 pp.maxwavelen = 2.0 

246 success += 1 

247 msg += 'was able to assign maxwavelen; ' 

248 except: 

249 self.assertEqual(pp.maxwavelen, control.maxwavelen) 

250 

251 try: 

252 pp.wavelenstep = 2.0 

253 success += 1 

254 msg += 'was able to assign wavelenstep; ' 

255 except: 

256 self.assertEqual(pp.wavelenstep, control.wavelenstep) 

257 

258 try: 

259 pp.lightspeed = 2.0 

260 success += 1 

261 msg += 'was able to assign lightspeed; ' 

262 except: 

263 self.assertEqual(pp.lightspeed, control.lightspeed) 

264 

265 try: 

266 pp.planck = 2.0 

267 success += 1 

268 msg += 'was able to assign planck; ' 

269 except: 

270 self.assertEqual(pp.planck, control.planck) 

271 

272 try: 

273 pp.nm2m = 2.0 

274 success += 1 

275 msg += 'was able to assign nm2m; ' 

276 except: 

277 self.assertEqual(pp.nm2m, control.nm2m) 

278 

279 try: 

280 pp.ergsetc2jansky = 2.0 

281 msg += 'was able to assign ergsetc2jansky; ' 

282 success += 1 

283 except: 

284 self.assertEqual(pp.ergsetc2jansky, control.ergsetc2jansky) 

285 

286 self.assertEqual(success, 0, msg=msg) 

287 

288 

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

290 pass 

291 

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

293 lsst.utils.tests.init() 

294 unittest.main()