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

392

393

394

395

396

397

398

399

400

401

402

# 

# LSST Data Management System 

# Copyright 2017 LSST/AURA. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# 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 LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.pex.exceptions 

import lsst.geom 

import lsst.afw.geom 

import lsst.afw.image 

import lsst.afw.table 

 

 

def makeTestCurve(random, x0, x1, y0=0.0, y1=0.0): 

"""Return a piecewise callable appropriate for testing 

TransmissionCurve objects. 

 

The returned callable can be called with either scalar or numpy.ndarray 

arguments. 

 

Between x0 and x1, the returned function is a nonnegative 4th-order 

polynomial. At and below x0 it is constant with value y0, and at and 

above x1 it is equal to y1. At exactly x0 and x1 its first derivative 

is zero. 

 

y0 and y1 may be None to indicate a random value should be drawn 

(on the interval [0, 1]). 

""" 

50 ↛ 51line 50 didn't jump to line 51, because the condition on line 50 was never true if y0 is None: 

y0 = random.rand() 

52 ↛ 53line 52 didn't jump to line 53, because the condition on line 52 was never true if y1 is None: 

y1 = random.rand() 

assert y0 >= 0.0 

assert y1 >= 0.0 

alpha0 = np.abs(y0 - y1) 

if alpha0 == 0.0: 

alpha0 = 1.0 

mu = (x1 - x0)*(0.25 + 0.5*random.rand()) 

alpha = alpha0*(0.25 + 0.5*random.rand()) 

n = 5 

A = np.zeros([n, n], dtype=float) 

dx = x1 - x0 

A[0, 0] = 1.0 

A[1, :] = [dx**k for k in range(n)] 

A[2, 1] = 1.0 

A[3, :] = [k*dx**(k - 1) for k in range(n)] 

A[4, :] = [mu**k for k in range(n)] 

b = np.array([y0, y1, 0.0, 0.0, alpha], dtype=float) 

coeffs = np.linalg.solve(A, b) 

 

def curve(x): 

result = sum(c*(x - x0)**k for k, c in enumerate(coeffs)) 

result[x <= x0] = y0 

result[x >= x1] = y1 

return result 

 

return curve 

 

 

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

 

def setUp(self): 

self.random = np.random.RandomState(1) 

self.points = [lsst.geom.Point2D(self.random.rand(), self.random.rand()) for i in range(5)] 

self.minWavelength = 5000 + self.random.rand() 

self.maxWavelength = 5500 + self.random.rand() 

 

def randIfNone(self, v): 

"""Return a random number if the given input is None, but pass it through if it is not.""" 

if v is None: 

return self.random.rand() 

return v 

 

def checkEvaluation(self, tc, wavelengths, expected, rtol=0.0, atol=0.0): 

"""Test that evaluating a TransmissionCurve on the given wavelengths array yields the given 

expected throughputs. 

""" 

for point in self.points: 

throughput = tc.sampleAt(point, wavelengths) 

self.assertFloatsAlmostEqual(throughput, expected, rtol=rtol, atol=atol) 

throughput2 = np.zeros(wavelengths.size, dtype=float) 

tc.sampleAt(point, wavelengths, out=throughput2) 

self.assertFloatsEqual(throughput2, throughput) 

 

def assertTransmissionCurvesEqual(self, a, b, rtol=0.0, atol=0.0): 

"""Test whether two TransimssionCurves are equivalent.""" 

self.assertEqual(a.getWavelengthBounds(), b.getWavelengthBounds()) 

self.assertEqual(a.getThroughputAtBounds(), b.getThroughputAtBounds()) 

wavelengths = np.linspace(*(a.getWavelengthBounds() + (100,))) 

for point in self.points: 

self.assertFloatsAlmostEqual( 

a.sampleAt(point, wavelengths), 

b.sampleAt(point, wavelengths), 

rtol=rtol, atol=atol 

) 

 

def checkPersistence(self, tc, points=None): 

"""Test that a TransmissionCurve round-trips through persistence.""" 

120 ↛ 122line 120 didn't jump to line 122, because the condition on line 120 was never false if points is None: 

points = self.points 

with lsst.utils.tests.getTempFilePath(".fits") as filename: 

tc.writeFits(filename) 

tc2 = lsst.afw.image.TransmissionCurve.readFits(filename) 

self.assertTransmissionCurvesEqual(tc, tc2) 

 

def makeAndCheckSpatiallyConstant(self, curve, wavelengths, throughputAtMin, throughputAtMax): 

"""Construct a constant TransmissionCurve and apply basic tests to it.""" 

throughput = curve(wavelengths) 

tc = lsst.afw.image.TransmissionCurve.makeSpatiallyConstant(throughput, wavelengths, 

throughputAtMin, throughputAtMax) 

self.assertEqual(tc.getWavelengthBounds(), (wavelengths[0], wavelengths[-1])) 

self.assertEqual(tc.getThroughputAtBounds(), (throughputAtMin, throughputAtMax)) 

self.checkEvaluation(tc, wavelengths, throughput) 

self.checkEvaluation(tc, np.array([2*wavelengths[0] - wavelengths[1]]), throughputAtMin) 

self.checkEvaluation(tc, np.array([2*wavelengths[-1] - wavelengths[-2]]), throughputAtMax) 

# Check that we get decent results when interpolating to different wavelengths. 

wavelengths1 = np.linspace(wavelengths[0] - 10, wavelengths[-1] + 10, 200) 

self.checkEvaluation(tc, wavelengths1, curve(wavelengths1), rtol=1E-2, atol=1E-2) 

# Test that multiplication with identity is a no-op 

tc2 = tc * lsst.afw.image.TransmissionCurve.makeIdentity() 

self.assertTransmissionCurvesEqual(tc, tc2) 

return tc 

 

def checkSpatiallyConstantEvenSpacing(self, throughputAtMin, throughputAtMax): 

"""Test that we can construct and use a spatially-constant 

TransmissionCurve initialized with an evenly-spaced wavelength 

array. 

""" 

throughputAtMin = self.randIfNone(throughputAtMin) 

throughputAtMax = self.randIfNone(throughputAtMax) 

wavelengths = np.linspace(self.minWavelength, self.maxWavelength, 100) 

curve = makeTestCurve(self.random, self.minWavelength, self.maxWavelength, 

throughputAtMin, throughputAtMax) 

tc = self.makeAndCheckSpatiallyConstant(curve, wavelengths, throughputAtMin, throughputAtMax) 

self.checkPersistence(tc) 

 

def testSpatiallyConstantEvenSpacing(self): 

"""Invoke all SpatiallyConstantEvenSpacing tests. 

 

Should be updated to use subTest when Python < 3.4 support is ended. 

""" 

self.checkSpatiallyConstantEvenSpacing(0.0, 0.0) 

self.checkSpatiallyConstantEvenSpacing(0.0, None) 

self.checkSpatiallyConstantEvenSpacing(None, 0.0) 

self.checkSpatiallyConstantEvenSpacing(None, None) 

 

def checkSpatiallyConstantUnevenSpacing(self, throughputAtMin, throughputAtMax): 

"""Test that we can construct and use a spatially-constant 

TransmissionCurve initialized with an unevenly-spaced wavelength 

array. 

""" 

throughputAtMin = self.randIfNone(throughputAtMin) 

throughputAtMax = self.randIfNone(throughputAtMax) 

wavelengths = self.minWavelength + (self.maxWavelength - self.minWavelength)*self.random.rand(100) 

wavelengths.sort() 

curve = makeTestCurve(self.random, self.minWavelength, self.maxWavelength, 

throughputAtMin, throughputAtMax) 

tc = self.makeAndCheckSpatiallyConstant(curve, wavelengths, throughputAtMin, throughputAtMax) 

self.checkPersistence(tc) 

 

def testSpatiallyConstantUnevenSpacing(self): 

"""Invoke all SpatiallyConstantUnevenSpacing tests. 

 

Should be updated to use subTest when Python < 3.4 support is ended. 

""" 

self.checkSpatiallyConstantUnevenSpacing(0.0, 0.0) 

self.checkSpatiallyConstantUnevenSpacing(0.0, None) 

self.checkSpatiallyConstantUnevenSpacing(None, 0.0) 

self.checkSpatiallyConstantUnevenSpacing(None, None) 

 

def checkProduct(self, throughputAtMin1, throughputAtMax1, throughputAtMin2, throughputAtMax2): 

"""Test the product of two spatially-constant TransmissionCurves.""" 

throughputAtMin1 = self.randIfNone(throughputAtMin1) 

throughputAtMax1 = self.randIfNone(throughputAtMax1) 

throughputAtMin2 = self.randIfNone(throughputAtMin2) 

throughputAtMax2 = self.randIfNone(throughputAtMax2) 

wl1a = 5100 + self.random.rand() 

wl1b = 5500 + self.random.rand() 

wl2a = 5200 + self.random.rand() 

wl2b = 5600 + self.random.rand() 

wl1 = np.linspace(wl1a, wl1b, 100) 

wl2 = np.linspace(wl2a, wl2b, 100) 

curve1 = makeTestCurve(self.random, wl1a, wl1b, throughputAtMin1, throughputAtMax1) 

curve2 = makeTestCurve(self.random, wl2a, wl2b, throughputAtMin2, throughputAtMax2) 

op1 = self.makeAndCheckSpatiallyConstant(curve1, wl1, throughputAtMin1, throughputAtMax1) 

op2 = self.makeAndCheckSpatiallyConstant(curve2, wl2, throughputAtMin2, throughputAtMax2) 

product = op1 * op2 

 

lowest = np.linspace(wl1a - 10, wl1a - 1, 10) 

self.checkEvaluation(product, lowest, throughputAtMin1*throughputAtMin2) 

 

lower = np.linspace(wl1a + 1, wl2a - 1, 10) 

if throughputAtMin2 == 0.0: 

self.checkEvaluation(product, lower, 0.0) 

else: 

for point in self.points: 

self.assertFloatsEqual( 

op1.sampleAt(point, lower)*throughputAtMin2, 

product.sampleAt(point, lower) 

) 

 

inner = np.linspace(wl2a, wl1b, 10) 

for point in self.points: 

self.assertFloatsAlmostEqual( 

op1.sampleAt(point, inner)*op2.sampleAt(point, inner), 

product.sampleAt(point, inner) 

) 

 

upper = np.linspace(wl1b + 1, wl2b - 1, 10) 

if throughputAtMax1 == 0.0: 

self.checkEvaluation(product, upper, 0.0) 

else: 

for point in self.points: 

self.assertFloatsEqual( 

op2.sampleAt(point, upper)*throughputAtMax1, 

product.sampleAt(point, upper) 

) 

 

uppermost = np.linspace(wl2b + 1, wl2b + 10, 10) 

self.checkEvaluation(product, uppermost, throughputAtMax1*throughputAtMax2) 

 

self.checkPersistence(product) 

 

def testProduct(self): 

"""Invoke all checkProduct tests. 

 

Should be updated to use subTest when Python < 3.4 support is ended. 

""" 

self.checkProduct(0.0, 0.0, 0.0, 0.0) 

self.checkProduct(0.0, 0.0, 0.0, None) 

self.checkProduct(0.0, 0.0, None, 0.0) 

self.checkProduct(0.0, 0.0, None, None) 

self.checkProduct(0.0, None, 0.0, 0.0) 

self.checkProduct(0.0, None, 0.0, None) 

self.checkProduct(0.0, None, None, 0.0) 

self.checkProduct(0.0, None, None, None) 

self.checkProduct(None, 0.0, 0.0, 0.0) 

self.checkProduct(None, 0.0, 0.0, None) 

self.checkProduct(None, 0.0, None, 0.0) 

self.checkProduct(None, 0.0, None, None) 

self.checkProduct(None, None, 0.0, 0.0) 

self.checkProduct(None, None, 0.0, None) 

self.checkProduct(None, None, None, 0.0) 

self.checkProduct(None, None, None, None) 

 

def makeRadial(self): 

"""Construct a random radial TransmissionCurve and return it with 

the wavelengths, radii, and 2-d curve used to construct it. 

""" 

wavelengths = np.linspace(self.minWavelength, self.maxWavelength, 100) 

radii = np.linspace(0.0, 1.0, 200) 

 

# This curve will represent the TransmissionCurve at the origin; 

# we'll shift it to higher wavelengths and scale it linearly with radius 

curve = makeTestCurve(self.random, wavelengths[0], wavelengths[90]) 

delta = (wavelengths[1] - wavelengths[0])/(radii[1] - radii[0]) 

 

def curve2d(lam, r): 

return curve(lam + delta*r)*(1.0+r) 

 

throughput = np.zeros(wavelengths.shape + radii.shape, dtype=float) 

for i, radius in enumerate(radii): 

throughput[:, i] = curve2d(wavelengths, radius) 

 

tc = lsst.afw.image.TransmissionCurve.makeRadial(throughput, wavelengths, radii) 

 

return tc, wavelengths, radii, curve2d 

 

def testRadial(self): 

"""Test the functionality of radial TransmissionCurves.""" 

tc, wavelengths, radii, curve2d = self.makeRadial() 

 

# Test at exactly the radii and wavelengths we initialized with. 

for n, radius in enumerate(radii): 

rot = lsst.geom.LinearTransform.makeRotation( 

2.0*np.pi*self.random.rand()*lsst.geom.radians 

) 

p0 = lsst.geom.Point2D(0.0, radius) 

p1 = rot(p0) 

self.assertFloatsAlmostEqual( 

curve2d(wavelengths, radius), 

tc.sampleAt(p0, wavelengths), 

rtol=1E-13 

) 

self.assertFloatsAlmostEqual( 

curve2d(wavelengths, radius), 

tc.sampleAt(p1, wavelengths), 

rtol=1E-13 

) 

 

# Test at some other random points in radius and wavelength. 

wl2 = np.linspace(self.minWavelength, self.maxWavelength, 151) 

for point in self.points: 

radius = (point.getX()**2 + point.getY()**2)**0.5 

self.assertFloatsAlmostEqual( 

curve2d(wl2, radius), 

tc.sampleAt(point, wl2), 

rtol=1E-2, atol=1E-2 

) 

 

# Test persistence for radial TransmissionCurves 

self.checkPersistence(tc) 

 

def testTransform(self): 

"""Test that we can transform a spatially-varying TransmissionCurve.""" 

tc, wavelengths, radii, curve2d = self.makeRadial() 

 

# If we transform by a pure rotation, what we get back should be equivalent. 

affine1 = lsst.geom.AffineTransform( 

lsst.geom.LinearTransform.makeRotation( 

2.0*np.pi*self.random.rand()*lsst.geom.radians 

) 

) 

transform1 = lsst.afw.geom.makeTransform(affine1) 

wl2 = np.linspace(self.minWavelength, self.maxWavelength, 151) 

tc1 = tc.transformedBy(transform1) 

self.assertTransmissionCurvesEqual(tc, tc1, rtol=1E-13) 

 

# Test transforming by a random affine transform. 

affine2 = lsst.geom.AffineTransform( 

lsst.geom.LinearTransform.makeScaling(1.0 + self.random.rand()), 

lsst.geom.Extent2D(self.random.randn(), self.random.randn()) 

) 

transform2 = lsst.afw.geom.makeTransform(affine2) 

tc2 = tc.transformedBy(transform2) 

for point in self.points: 

self.assertFloatsAlmostEqual( 

tc.sampleAt(point, wl2), 

tc2.sampleAt(affine2(point), wl2), 

rtol=1E-13 

) 

 

# Test further transforming the rotated transmission curve 

tc3 = tc1.transformedBy(transform2) 

self.assertTransmissionCurvesEqual(tc2, tc3) 

 

# Test persistence for transformed TransmissionCurves 

self.checkPersistence(tc3) 

 

def testExposure(self): 

"""Test that we can attach a TransmissionCurve to an Exposure and round-trip it through I/O.""" 

wavelengths = np.linspace(6200, 6400, 100) 

curve = makeTestCurve(self.random, 6200, 6400, 0.0, 0.0) 

tc1 = self.makeAndCheckSpatiallyConstant(curve, wavelengths, 0.0, 0.0) 

exposure1 = lsst.afw.image.ExposureF(4, 5) 

exposure1.getInfo().setTransmissionCurve(tc1) 

self.assertTrue(exposure1.getInfo().hasTransmissionCurve()) 

with lsst.utils.tests.getTempFilePath(".fits") as filename: 

exposure1.writeFits(filename) 

exposure2 = lsst.afw.image.ExposureF(filename) 

self.assertTrue(exposure2.getInfo().hasTransmissionCurve()) 

tc2 = exposure2.getInfo().getTransmissionCurve() 

self.assertTransmissionCurvesEqual(tc1, tc2) 

 

def testExposureRecord(self): 

"""Test that we can attach a TransmissionCurve to an ExposureRecord and round-trip it through I/O.""" 

wavelengths = np.linspace(6200, 6400, 100) 

curve = makeTestCurve(self.random, 6200, 6400, 0.0, 0.0) 

tc1 = self.makeAndCheckSpatiallyConstant(curve, wavelengths, 0.0, 0.0) 

cat1 = lsst.afw.table.ExposureCatalog(lsst.afw.table.ExposureTable.makeMinimalSchema()) 

cat1.addNew().setTransmissionCurve(tc1) 

self.assertTrue(cat1[0].getTransmissionCurve() is not None) 

with lsst.utils.tests.getTempFilePath(".fits") as filename: 

cat1.writeFits(filename) 

cat2 = lsst.afw.table.ExposureCatalog.readFits(filename) 

self.assertTrue(cat2[0].getTransmissionCurve() is not None) 

tc2 = cat2[0].getTransmissionCurve() 

self.assertTransmissionCurvesEqual(tc1, tc2) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()