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

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

# This file is part of afw. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

import math 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.daf.base as dafBase 

import lsst.pex.exceptions as pexExcept 

import lsst.afw.image as afwImage 

import lsst.afw.image.utils as imageUtils 

from lsst.afw.cameraGeom.testUtils import DetectorWrapper 

 

 

class CalibTestCase(lsst.utils.tests.TestCase): 

def setUp(self): 

self.calib = afwImage.Calib() 

self.detector = DetectorWrapper().detector 

 

def tearDown(self): 

del self.calib 

del self.detector 

 

def testPhotom(self): 

"""Test the zero-point information""" 

 

flux, fluxErr = 1000.0, 10.0 

flux0, flux0Err = 1e12, 1e10 

self.calib.setFluxMag0(flux0) 

 

self.assertEqual(flux0, self.calib.getFluxMag0()[0]) 

self.assertEqual(0.0, self.calib.getFluxMag0()[1]) 

self.assertEqual(22.5, self.calib.getMagnitude(flux)) 

# Error just in flux 

self.assertAlmostEqual(self.calib.getMagnitude(flux, fluxErr)[1], 

2.5/math.log(10)*fluxErr/flux) 

# Error just in flux0 

self.calib.setFluxMag0(flux0, flux0Err) 

self.assertEqual(flux0Err, self.calib.getFluxMag0()[1]) 

self.assertAlmostEqual(self.calib.getMagnitude(flux, 0)[1], 

2.5/math.log(10)*flux0Err/flux0) 

 

self.assertAlmostEqual(flux0, self.calib.getFlux(0)) 

self.assertAlmostEqual(flux, self.calib.getFlux(22.5)) 

 

# I don't know how to test round-trip if fluxMag0 is significant 

# compared to fluxErr 

self.calib.setFluxMag0(flux0, flux0 / 1e6) 

for fluxErr in (flux / 1e2, flux / 1e4): 

mag, magErr = self.calib.getMagnitude(flux, fluxErr) 

self.assertAlmostEqual(flux, self.calib.getFlux(mag, magErr)[0]) 

self.assertLess( 

abs(fluxErr - self.calib.getFlux(mag, magErr)[1]), 1.0e-4) 

 

# Test context manager; shouldn't raise an exception within the block, 

# should outside 

with imageUtils.CalibNoThrow(): 

self.assertTrue(np.isnan(self.calib.getMagnitude(-50.0))) 

with self.assertRaises(pexExcept.DomainError): 

self.calib.getMagnitude(-50.0) 

 

def testPhotomMulti(self): 

self.calib.setFluxMag0(1e12, 1e10) 

flux, fluxErr = 1000.0, 10.0 

num = 5 

 

# Result assumed to be true: tested elsewhere 

mag, magErr = self.calib.getMagnitude(flux, fluxErr) 

 

fluxList = np.array([flux for i in range(num)], dtype=float) 

fluxErrList = np.array([fluxErr for i in range(num)], dtype=float) 

 

magList = self.calib.getMagnitude(fluxList) 

for m in magList: 

self.assertEqual(m, mag) 

 

mags, magErrs = self.calib.getMagnitude(fluxList, fluxErrList) 

 

for m, dm in zip(mags, magErrs): 

self.assertEqual(m, mag) 

self.assertEqual(dm, magErr) 

 

# Result assumed to be true: tested elsewhere 

flux, fluxErr = self.calib.getFlux(mag, magErr) 

 

fluxList = self.calib.getFlux(magList) 

for f in fluxList: 

self.assertEqual(f, flux) 

 

fluxes = self.calib.getFlux(mags, magErrs) 

for f, df in zip(fluxes[0], fluxes[1]): 

self.assertAlmostEqual(f, flux) 

self.assertAlmostEqual(df, fluxErr) 

 

def testCtorFromMetadata(self): 

"""Test building a Calib from metadata""" 

 

flux0, flux0Err = 1e12, 1e10 

flux, fluxErr = 1000.0, 10.0 

 

metadata = dafBase.PropertySet() 

metadata.add("FLUXMAG0", flux0) 

metadata.add("FLUXMAG0ERR", flux0Err) 

 

self.calib = afwImage.Calib(metadata) 

 

self.assertEqual(flux0, self.calib.getFluxMag0()[0]) 

self.assertEqual(flux0Err, self.calib.getFluxMag0()[1]) 

self.assertEqual(22.5, self.calib.getMagnitude(flux)) 

# Error just in flux 

self.calib.setFluxMag0(flux0, 0) 

 

self.assertAlmostEqual(self.calib.getMagnitude(flux, fluxErr)[1], 

2.5/math.log(10)*fluxErr/flux) 

 

# Check that we can clean up metadata 

afwImage.stripCalibKeywords(metadata) 

self.assertEqual(len(metadata.names()), 0) 

 

def testCalibEquality(self): 

self.assertEqual(self.calib, self.calib) 

# using assertFalse to directly test != operator 

self.assertFalse(self.calib != self.calib) 

 

calib2 = afwImage.Calib() 

calib2.setFluxMag0(1200) 

 

self.assertNotEqual(calib2, self.calib) 

 

def testCalibFromCalibs(self): 

"""Test creating a Calib from an array of Calibs""" 

mag0, mag0Sigma = 1.0, 0.01 

 

calibs = [] 

ncalib = 3 

for i in range(ncalib): 

calib = afwImage.Calib() 

calib.setFluxMag0(mag0, mag0Sigma) 

 

calibs.append(calib) 

 

ocalib = afwImage.Calib(calibs) 

# the following is surely incorrect; see DM-7619 

self.assertEqual(ocalib.getFluxMag0(), (0.0, 0.0)) 

 

# Check that we can only merge Calibs with the same fluxMag0 values 

calibs[0].setFluxMag0(1.001*mag0, mag0Sigma) 

with self.assertRaises(pexExcept.InvalidParameterError): 

afwImage.Calib(calibs) 

 

def testCalibNegativeFlux(self): 

"""Check that we can control if negative fluxes raise exceptions""" 

self.calib.setFluxMag0(1e12) 

 

with self.assertRaises(pexExcept.DomainError): 

self.calib.getMagnitude(-10) 

with self.assertRaises(pexExcept.DomainError): 

self.calib.getMagnitude(-10, 1) 

 

afwImage.Calib.setThrowOnNegativeFlux(False) 

mags = self.calib.getMagnitude(-10) 

self.assertTrue(np.isnan(mags)) 

mags = self.calib.getMagnitude(-10, 1) 

self.assertTrue(np.isnan(mags[0])) 

self.assertTrue(np.isnan(mags[1])) 

 

afwImage.Calib.setThrowOnNegativeFlux(True) 

 

# Re-check that we raise after resetting ThrowOnNegativeFlux. 

with self.assertRaises(pexExcept.DomainError): 

self.calib.getMagnitude(-10) 

with self.assertRaises(pexExcept.DomainError): 

self.calib.getMagnitude(-10, 1) 

 

 

def defineSdssFilters(testCase): 

"""Initialise filters as used for our tests""" 

imageUtils.resetFilters() 

wavelengths = dict() 

testCase.aliases = dict(u=[], g=[], r=[], i=[], z=['zprime', "z'"]) 

for name, lambdaEff in (('u', 355.1), 

('g', 468.6), 

('r', 616.5), 

('i', 748.1), 

('z', 893.1)): 

wavelengths[name] = lambdaEff 

imageUtils.defineFilter(name, lambdaEff, alias=testCase.aliases[name]) 

return wavelengths 

 

 

class ColorTestCase(lsst.utils.tests.TestCase): 

def setUp(self): 

defineSdssFilters(self) 

 

def testCtor(self): 

afwImage.Color() 

afwImage.Color(1.2) 

 

def testLambdaEff(self): 

f = afwImage.Filter("g") 

g_r = 1.2 

c = afwImage.Color(g_r) 

 

# XXX Not a real implementation! 

self.assertEqual(c.getLambdaEff(f), 1000*g_r) 

 

def testIsIndeterminate(self): 

"""Test that a default-constructed Color tests True, but ones with a g-r value test False""" 

self.assertTrue(afwImage.Color().isIndeterminate()) 

self.assertFalse(afwImage.Color(1.2).isIndeterminate()) 

 

 

class FilterTestCase(lsst.utils.tests.TestCase): 

def setUp(self): 

# Initialise our filters 

# Start by forgetting that we may already have defined filters 

wavelengths = defineSdssFilters(self) 

self.filters = tuple(sorted(wavelengths.keys())) 

self.g_lambdaEff = [lambdaEff for name, 

lambdaEff in wavelengths.items() if name == "g"][0] # for tests 

 

def defineFilterProperty(self, name, lambdaEff, force=False): 

return afwImage.FilterProperty(name, lambdaEff, force=force) 

 

def testListFilters(self): 

self.assertEqual(afwImage.Filter.getNames(), list(self.filters)) 

 

def testCtorFromMetadata(self): 

"""Test building a Filter from metadata""" 

 

metadata = dafBase.PropertySet() 

metadata.add("FILTER", "g") 

 

f = afwImage.Filter(metadata) 

self.assertEqual(f.getName(), "g") 

# Check that we can clean up metadata 

afwImage.stripFilterKeywords(metadata) 

self.assertEqual(len(metadata.names()), 0) 

 

badFilter = "rhl" # an undefined filter 

metadata.add("FILTER", badFilter) 

# Not defined 

with self.assertRaises(pexExcept.NotFoundError): 

afwImage.Filter(metadata) 

 

# Force definition 

f = afwImage.Filter(metadata, True) 

self.assertEqual(f.getName(), badFilter) # name is correctly defined 

 

def testFilterEquality(self): 

"""Test a "g" filter comparison""" 

f = afwImage.Filter("g") 

g = afwImage.Filter("g") 

 

self.assertEqual(f, g) 

 

f = afwImage.Filter() # the unknown filter 

self.assertNotEqual(f, f) # ... doesn't equal itself 

 

def testFilterProperty(self): 

"""Test properties of a "g" filter""" 

f = afwImage.Filter("g") 

# The properties of a g filter 

g = afwImage.FilterProperty.lookup("g") 

 

self.assertEqual(f.getName(), "g") 

self.assertEqual(f.getId(), 1) 

self.assertEqual(f.getFilterProperty().getLambdaEff(), 

self.g_lambdaEff) 

self.assertEqual(f.getFilterProperty(), 

self.defineFilterProperty("gX", self.g_lambdaEff, force=True)) 

self.assertEqual(g.getLambdaEff(), self.g_lambdaEff) 

 

def testLambdaMinMax(self): 

"""Test additional properties for minimum and maximum wavelength for a filter.""" 

filt = afwImage.Filter("g") 

# LambdaMin and LambdaMax are undefined for the test SDSS filter, and should return nan 

self.assertTrue(np.isnan(filt.getFilterProperty().getLambdaMin())) 

self.assertTrue(np.isnan(filt.getFilterProperty().getLambdaMax())) 

lambdaEff = 476.31 

lambdaMin = 405 

lambdaMax = 552 

imageUtils.defineFilter("gNew", lambdaEff, lambdaMin=lambdaMin, lambdaMax=lambdaMax) 

filtNew = afwImage.Filter("gNew") 

self.assertEqual(lambdaMin, filtNew.getFilterProperty().getLambdaMin()) 

self.assertEqual(lambdaMax, filtNew.getFilterProperty().getLambdaMax()) 

 

def testFilterAliases(self): 

"""Test that we can provide an alias for a Filter""" 

for name0 in self.aliases: 

f0 = afwImage.Filter(name0) 

self.assertEqual(f0.getCanonicalName(), name0) 

self.assertEqual(sorted(f0.getAliases()), 

sorted(self.aliases[name0])) 

 

for name in self.aliases[name0]: 

f = afwImage.Filter(name) 

self.assertEqual(sorted(f.getAliases()), 

sorted(self.aliases[name0])) 

self.assertEqual(f.getId(), f0.getId()) 

self.assertEqual(f.getName(), name) 

self.assertEqual(afwImage.Filter(f.getId()).getName(), name0) 

self.assertEqual(f.getCanonicalName(), name0) 

self.assertNotEqual(f.getCanonicalName(), name) 

self.assertEqual(f.getFilterProperty().getLambdaEff(), 

f0.getFilterProperty().getLambdaEff()) 

 

def testReset(self): 

"""Test that we can reset filter IDs and properties if needs be""" 

g = afwImage.FilterProperty.lookup("g") 

 

# Can we add a filter property? 

with self.assertRaises(pexExcept.RuntimeError): 

self.defineFilterProperty("g", self.g_lambdaEff + 10) 

# should not raise 

self.defineFilterProperty("g", self.g_lambdaEff + 10, True) 

self.defineFilterProperty("g", self.g_lambdaEff, True) 

 

# Can we redefine properties? 

with self.assertRaises(pexExcept.RuntimeError): 

# changing definition is not allowed 

self.defineFilterProperty("g", self.g_lambdaEff + 10) 

 

# identical redefinition is allowed 

self.defineFilterProperty("g", self.g_lambdaEff) 

 

# OK if Id's the same 

afwImage.Filter.define(g, afwImage.Filter("g").getId()) 

# AUTO will assign the same ID 

afwImage.Filter.define(g, afwImage.Filter.AUTO) 

 

# different ID 

with self.assertRaises(pexExcept.RuntimeError): 

afwImage.Filter.define(g, afwImage.Filter("g").getId() + 10) 

 

def testUnknownFilter(self): 

"""Test that we can define, but not use, an unknown filter""" 

badFilter = "rhl" # an undefined filter 

with self.assertRaises(pexExcept.NotFoundError): 

afwImage.Filter(badFilter) 

# Force definition 

f = afwImage.Filter(badFilter, True) 

self.assertEqual(f.getName(), badFilter) # name is correctly defined 

 

# can't use Filter f 

with self.assertRaises(pexExcept.NotFoundError): 

f.getFilterProperty().getLambdaEff() 

 

# Now define badFilter 

lambdaEff = 666.0 

self.defineFilterProperty(badFilter, lambdaEff) 

 

# but now we can 

self.assertEqual(f.getFilterProperty().getLambdaEff(), lambdaEff) 

 

# Check that we didn't accidently define the unknown filter 

with self.assertRaises(pexExcept.NotFoundError): 

afwImage.Filter().getFilterProperty().getLambdaEff() 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()