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

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

from __future__ import with_statement 

import os 

import numpy as np 

import unittest 

import lsst.utils 

import lsst.utils.tests 

 

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

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class PhotometricParametersUnitTest(unittest.TestCase): 

 

def testInit(self): 

""" 

Test that the init and getters of PhotometricParameters work 

properly 

""" 

defaults = PhotometricParameters() 

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

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

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

 

for attribute in params: 

kwargs = {} 

kwargs[attribute] = -100.0 

testCase = PhotometricParameters(**kwargs) 

 

for pp in params: 

if pp != attribute: 

self.assertEqual(defaults.__getattribute__(pp), 

testCase.__getattribute__(pp)) 

else: 

self.assertNotEqual(defaults.__getattribute__(pp), 

testCase.__getattribute__(pp)) 

 

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

 

def testExceptions(self): 

""" 

Test that exceptions get raised when they ought to by the 

PhotometricParameters constructor 

 

We will instantiate PhotometricParametrs with different incomplete 

lists of parameters set. We will verify that the returned 

error messages correctly point out which parameters were ignored. 

""" 

 

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

'nexp': 'did not set nexp', 

'effarea': 'did not set effarea', 

'gain': 'did not set gain', 

'platescale': 'did not set platescale', 

'sigmaSys': 'did not set sigmaSys', 

'readnoise': 'did not set readnoise', 

'darkcurrent': 'did not set darkcurrent', 

'othernoise': 'did not set othernoise'} 

 

with self.assertRaises(RuntimeError) as context: 

PhotometricParameters(bandpass='x') 

 

for name in expectedMessage: 

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

 

for name1 in expectedMessage: 

for name2 in expectedMessage: 

setParameters = {name1: 2.0, name2: 2.0} 

with self.assertRaises(RuntimeError) as context: 

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

 

for name3 in expectedMessage: 

if name3 not in setParameters: 

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

else: 

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

 

def testDefaults(self): 

""" 

Test that PhotometricParameters are correctly assigned to defaults 

""" 

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

for bp in bandpassNames: 

photParams = PhotometricParameters(bandpass=bp) 

self.assertEqual(photParams.bandpass, bp) 

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

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

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

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

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

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

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

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

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

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

else: 

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

 

def testNoBandpass(self): 

""" 

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

parameters are assigned. 

""" 

photParams = PhotometricParameters() 

self.assertEqual(photParams.bandpass, None) 

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

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

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

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

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

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

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

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

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

 

def testAssignment(self): 

""" 

Test that it is impossible to set PhotometricParameters on the fly 

""" 

testCase = PhotometricParameters() 

controlCase = PhotometricParameters() 

success = 0 

 

msg = '' 

try: 

testCase.exptime = -1.0 

success += 1 

msg += 'was able to assign exptime; ' 

except: 

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

 

try: 

testCase.nexp = -1.0 

success += 1 

msg += 'was able to assign nexp; ' 

except: 

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

 

try: 

testCase.effarea = -1.0 

success += 1 

msg += 'was able to assign effarea; ' 

except: 

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

 

try: 

testCase.gain = -1.0 

success += 1 

msg += 'was able to assign gain; ' 

except: 

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

 

try: 

testCase.readnoise = -1.0 

success += 1 

msg += 'was able to assign readnoise; ' 

except: 

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

 

try: 

testCase.darkcurrent = -1.0 

success += 1 

msg += 'was able to assign darkcurrent; ' 

except: 

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

 

try: 

testCase.othernoise = -1.0 

success += 1 

msg += 'was able to assign othernoise; ' 

except: 

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

 

try: 

testCase.platescale = -1.0 

success += 1 

msg += 'was able to assign platescale; ' 

except: 

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

 

try: 

testCase.sigmaSys = -1.0 

success += 1 

msg += 'was able to assign sigmaSys; ' 

except: 

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

 

try: 

testCase.bandpass = 'z' 

success += 1 

msg += 'was able to assign bandpass; ' 

except: 

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

 

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

 

def testApplication(self): 

""" 

Test that PhotometricParameters get properly propagated into 

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

scale linearly with the appropriate parameter. 

""" 

 

testSed = Sed() 

testSed.setFlatSED() 

 

testBandpass = Bandpass() 

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

'baseline', 'total_g.dat')) 

 

control = testSed.calcADU(testBandpass, 

photParams=PhotometricParameters()) 

 

testCase = PhotometricParameters(exptime=30.0) 

 

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

 

self.assertGreater(control, 0.0) 

self.assertEqual(control, 0.5*test) 

 

 

class PhysicalParametersUnitTest(unittest.TestCase): 

 

def testAssignment(self): 

""" 

Make sure it is impossible to change the values stored in 

PhysicalParameters 

""" 

 

pp = PhysicalParameters() 

control = PhysicalParameters() 

success = 0 

msg = '' 

 

try: 

pp.minwavelen = 2.0 

success += 1 

msg += 'was able to assign minwavelen; ' 

except: 

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

 

try: 

pp.maxwavelen = 2.0 

success += 1 

msg += 'was able to assign maxwavelen; ' 

except: 

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

 

try: 

pp.wavelenstep = 2.0 

success += 1 

msg += 'was able to assign wavelenstep; ' 

except: 

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

 

try: 

pp.lightspeed = 2.0 

success += 1 

msg += 'was able to assign lightspeed; ' 

except: 

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

 

try: 

pp.planck = 2.0 

success += 1 

msg += 'was able to assign planck; ' 

except: 

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

 

try: 

pp.nm2m = 2.0 

success += 1 

msg += 'was able to assign nm2m; ' 

except: 

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

 

try: 

pp.ergsetc2jansky = 2.0 

msg += 'was able to assign ergsetc2jansky; ' 

success += 1 

except: 

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

 

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

 

 

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

pass 

 

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

lsst.utils.tests.init() 

unittest.main()