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

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

# 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/>. 

# 

 

""" 

Tests for math.ChebyshevBoundedField 

 

Run with: 

./testChebyshevBoundedField.py 

or 

python 

>>> import testSchema; testSchema.run() 

""" 

 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.pex.exceptions 

import lsst.geom 

import lsst.afw.image 

import lsst.afw.math 

 

try: 

type(display) 

except NameError: 

display = False 

 

48 ↛ exit,   48 ↛ exit,   48 ↛ exit,   48 ↛ exit,   48 ↛ exit,   48 ↛ exit6 missed branches: 1) line 54 didn't finish the lambda on line 54, 2) line 53 didn't finish the lambda on line 53, 3) line 52 didn't finish the lambda on line 52, 4) line 51 didn't finish the lambda on line 51, 5) line 50 didn't finish the lambda on line 50, 6) line 49 didn't finish the lambda on line 49CHEBYSHEV_T = [ 

lambda x: x**0, 

lambda x: x, 

lambda x: 2*x**2 - 1, 

lambda x: (4*x**2 - 3)*x, 

lambda x: (8*x**2 - 8)*x**2 + 1, 

lambda x: ((16*x**2 - 20)*x**2 + 5)*x, 

] 

 

 

def multiply(image, field): 

"""Return the product of image and field() at each point in image.""" 

box = image.getBBox() 

outImage = lsst.afw.image.ImageF(box) 

for i in range(box.getMinX(), box.getMaxX()+1): 

for j in range(box.getMinY(), box.getMaxY()+1): 

outImage[i, j] = image[i, j] * field.evaluate(i, j) 

return outImage 

 

 

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

 

def setUp(self): 

self.longMessage = True 

np.random.seed(5) 

self.bbox = lsst.geom.Box2I( 

lsst.geom.Point2I(-5, -5), lsst.geom.Point2I(5, 5)) 

self.x1d = np.linspace(self.bbox.getBeginX(), self.bbox.getEndX()) 

self.y1d = np.linspace(self.bbox.getBeginY(), self.bbox.getEndY()) 

self.x2d, self.y2d = np.meshgrid(self.x1d, self.y1d) 

self.xFlat = np.ravel(self.x2d) 

self.yFlat = np.ravel(self.y2d) 

self.cases = [] 

for orderX in range(0, 5): 

for orderY in range(0, 5): 

indexX, indexY = np.meshgrid(np.arange(orderX+1, dtype=int), 

np.arange(orderY+1, dtype=int)) 

for triangular in (True, False): 

ctrl = lsst.afw.math.ChebyshevBoundedFieldControl() 

ctrl.orderX = orderX 

ctrl.orderY = orderY 

ctrl.triangular = triangular 

coefficients = np.random.randn(orderY+1, orderX+1) 

if triangular: 

coefficients[indexX + indexY > 

max(orderX, orderY)] = 0.0 

self.cases.append((ctrl, coefficients)) 

 

array = np.arange(self.bbox.getArea(), dtype=np.float32).reshape(self.bbox.getDimensions()) 

self.image = lsst.afw.image.ImageF(array) 

 

def tearDown(self): 

del self.bbox 

 

def testFillImageInterpolation(self): 

ctrl, coefficients = self.cases[-2] 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(10, 15), 

lsst.geom.Extent2I(360, 350)) 

field = lsst.afw.math.ChebyshevBoundedField(bbox, coefficients) 

image1 = lsst.afw.image.ImageF(bbox) 

image2 = lsst.afw.image.ImageF(bbox) 

image3 = lsst.afw.image.ImageF(bbox) 

image4 = lsst.afw.image.ImageF(bbox) 

field.fillImage(image1) 

field.fillImage(image2, xStep=3) 

field.fillImage(image3, yStep=4) 

field.fillImage(image4, xStep=3, yStep=4) 

self.assertFloatsAlmostEqual(image1.array, image2.array, rtol=1E-2, atol=1E-2) 

self.assertFloatsAlmostEqual(image1.array, image3.array, rtol=1.5E-2, atol=1.5E-2) 

self.assertFloatsAlmostEqual(image1.array, image4.array, rtol=2E-2, atol=2E-2) 

 

def testEvaluate(self): 

"""Test the single-point evaluate method against explicitly-defined 1-d Chebyshevs 

(at the top of this file). 

""" 

factor = 12.345 

boxD = lsst.geom.Box2D(self.bbox) 

# sx, sy: transform from self.bbox range to [-1, -1] 

sx = 2.0 / boxD.getWidth() 

sy = 2.0 / boxD.getHeight() 

nPoints = 50 

for ctrl, coefficients in self.cases: 

field = lsst.afw.math.ChebyshevBoundedField( 

self.bbox, coefficients) 

x = np.random.rand(nPoints)*boxD.getWidth() + boxD.getMinX() 

y = np.random.rand(nPoints)*boxD.getHeight() + boxD.getMinY() 

z1 = field.evaluate(x, y) 

tx = np.array([CHEBYSHEV_T[i](sx*x) 

for i in range(coefficients.shape[1])]) 

ty = np.array([CHEBYSHEV_T[i](sy*y) 

for i in range(coefficients.shape[0])]) 

self.assertEqual(tx.shape, (coefficients.shape[1], x.size)) 

self.assertEqual(ty.shape, (coefficients.shape[0], y.size)) 

z2 = np.array([np.dot(ty[:, i], np.dot(coefficients, tx[:, i])) 

for i in range(nPoints)]) 

self.assertFloatsAlmostEqual(z1, z2, rtol=1E-12) 

 

scaled = field*factor 

self.assertFloatsAlmostEqual(scaled.evaluate(x, y), 

factor*z2, 

rtol=factor*1E-13) 

self.assertFloatsEqual( 

scaled.getCoefficients(), factor*field.getCoefficients()) 

 

def testMultiplyImage(self): 

"""Test Multiplying in place an image.""" 

_, coefficients = self.cases[-2] 

field = lsst.afw.math.ChebyshevBoundedField(self.image.getBBox(), coefficients) 

# multiplyImage() is in-place, so we have to make the expected result first. 

expect = multiply(self.image, field) 

field.multiplyImage(self.image) 

self.assertImagesAlmostEqual(self.image, expect) 

 

def testMultiplyImageRaisesUnequalBBox(self): 

"""Multiplying an image with a different bbox should raise""" 

_, coefficients = self.cases[-2] 

field = lsst.afw.math.ChebyshevBoundedField(self.image.getBBox(), coefficients) 

subBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 3), lsst.geom.Point2I(3, 4)) 

subImage = self.image.subset(subBox) 

with(self.assertRaises(RuntimeError)): 

field.multiplyImage(subImage) 

 

def testMultiplyImageOverlapSubImage(self): 

"""Multiplying a subimage with overlapOnly=true should only modify 

the subimage, when a subimage is passed in. 

""" 

_, coefficients = self.cases[-2] 

field = lsst.afw.math.ChebyshevBoundedField(self.image.getBBox(), coefficients) 

subBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 3), lsst.geom.Point2I(3, 4)) 

subImage = self.image.subset(subBox) 

expect = self.image.clone() 

expect[subBox] = multiply(subImage, field) 

field.multiplyImage(subImage, overlapOnly=True) 

self.assertImagesAlmostEqual(self.image, expect) 

 

def testMultiplyImageOverlapSmallerBoundedField(self): 

"""Multiplying a subimage with overlapOnly=true should only modify 

the subimage if the boundedField bbox is smaller than the image. 

 

This is checking for a bug where the bounded field was writing outside 

the overlap bbox. 

""" 

_, coefficients = self.cases[-2] 

subBox = lsst.geom.Box2I(lsst.geom.Point2I(0, 3), lsst.geom.Point2I(3, 4)) 

# The BF is only defined on the subBox, not the whole image bbox. 

field = lsst.afw.math.ChebyshevBoundedField(subBox, coefficients) 

subImage = self.image.subset(subBox) 

expect = self.image.clone() 

expect[subBox] = multiply(subImage, field) 

field.multiplyImage(self.image, overlapOnly=True) 

self.assertImagesAlmostEqual(self.image, expect) 

 

def _testIntegrateBox(self, bbox, coeffs, expect): 

field = lsst.afw.math.ChebyshevBoundedField(bbox, coeffs) 

self.assertFloatsAlmostEqual(field.integrate(), expect, rtol=1E-14) 

 

def testIntegrateTrivialBox(self): 

"""Test integrating over a "trivial" [-1,1] box. 

 

NOTE: a "trivial" BBox can't be constructed exactly, given that Box2I 

is inclusive, but the [0,1] box has the same area (because it is 

actually (-0.5, 1.5) when converted to a Box2D), and the translation 

doesn't affect the integral. 

""" 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

lsst.geom.Point2I(1, 1)) 

 

# 0th order polynomial 

coeffs = np.array([[5.0]]) 

self._testIntegrateBox(bbox, coeffs, 4.0*coeffs[0, 0]) 

 

# 1st order polynomial: odd orders drop out of integral 

coeffs = np.array([[5.0, 2.0], [3.0, 4.0]]) 

self._testIntegrateBox(bbox, coeffs, 4.0*coeffs[0, 0]) 

 

# 2nd order polynomial in x, 0th in y 

coeffs = np.array([[5.0, 0.0, 7.0]]) 

self._testIntegrateBox( 

bbox, coeffs, 4.0*coeffs[0, 0] - (4.0/3.0)*coeffs[0, 2]) 

 

# 2nd order polynomial in y, 0th in x 

coeffs = np.zeros((3, 3)) 

coeffs[0, 0] = 5.0 

coeffs[2, 0] = 7.0 

self._testIntegrateBox( 

bbox, coeffs, 4.0*coeffs[0, 0] - (4.0/3.0)*coeffs[2, 0]) 

 

# 2nd order polynomial in x and y, no cross-term 

coeffs = np.zeros((3, 3)) 

coeffs[0, 0] = 5.0 

coeffs[1, 0] = 7.0 

coeffs[0, 2] = 3.0 

self._testIntegrateBox(bbox, coeffs, 4.0*coeffs[0, 0] - 

(4.0/3.0)*coeffs[2, 0] - (4.0/3.0)*coeffs[0, 2]) 

 

def testIntegrateBox(self): 

"""Test integrating over an "interesting" box. 

 

The values of these integrals were checked in Mathematica. The code 

block below can be pasted into Mathematica to re-do those calculations. 

 

:: 

 

f[x_, y_, n_, m_] := \!\( 

\*UnderoverscriptBox[\(\[Sum]\), \(i = 0\), \(n\)]\( 

\*UnderoverscriptBox[\(\[Sum]\), \(j = 0\), \(m\)] 

\*SubscriptBox[\(a\), \(i, j\)]*ChebyshevT[i, x]*ChebyshevT[j, y]\)\) 

integrate2dBox[n_, m_, x0_, x1_, y0_, y1_] := \!\( 

\*SubsuperscriptBox[\(\[Integral]\), \(y0\), \(y1\)]\( 

\*SubsuperscriptBox[\(\[Integral]\), \(x0\), \(x1\)]f[ 

\*FractionBox[\(2 x - x0 - x1\), \(x1 - x0\)], 

\*FractionBox[\(2 y - y0 - y1\), \(y1 - y0\)], n, 

m] \[DifferentialD]x \[DifferentialD]y\)\) 

integrate2dBox[0, 0, -2.5, 5.5, -3.5, 7.5] 

integrate2dBox[1, 0, -2.5, 5.5, -3.5, 7.5] 

integrate2dBox[0, 1, -2.5, 5.5, -3.5, 7.5] 

integrate2dBox[1, 1, -2.5, 5.5, -3.5, 7.5] 

integrate2dBox[1, 2, -2.5, 5.5, -3.5, 7.5] 

integrate2dBox[2, 2, -2.5, 5.5, -3.5, 7.5] 

""" 

bbox = lsst.geom.Box2I( 

lsst.geom.Point2I(-2, -3), lsst.geom.Point2I(5, 7)) 

 

# 0th order polynomial 

coeffs = np.array([[5.0]]) 

self._testIntegrateBox(bbox, coeffs, 88.0*coeffs[0, 0]) 

 

# 1st order polynomial: odd orders drop out of integral 

coeffs = np.array([[5.0, 2.0], [3.0, 4.0]]) 

self._testIntegrateBox(bbox, coeffs, 88.0*coeffs[0, 0]) 

 

# 2nd order polynomial in x, 0th in y 

coeffs = np.array([[5.0, 0.0, 7.0]]) 

self._testIntegrateBox( 

bbox, coeffs, 88.0*coeffs[0, 0] - (88.0/3.0)*coeffs[0, 2]) 

 

# 2nd order polynomial in y, 0th in x 

coeffs = np.zeros((3, 3)) 

coeffs[0, 0] = 5.0 

coeffs[2, 0] = 7.0 

self._testIntegrateBox( 

bbox, coeffs, 88.0*coeffs[0, 0] - (88.0/3.0)*coeffs[2, 0]) 

 

# 2nd order polynomial in x,y 

coeffs = np.zeros((3, 3)) 

coeffs[2, 2] = 11.0 

self._testIntegrateBox(bbox, coeffs, (88.0/9.0)*coeffs[2, 2]) 

 

def testMean(self): 

""" 

The mean of the nth 1d Chebyshev (a_n*T_n(x)) on [-1,1] is 

0 for odd n 

a_n / (1-n^2) for even n 

Similarly, the mean of the (n,m)th 2d Chebyshev is the appropriate 

product of the above. 

""" 

bbox = lsst.geom.Box2I( 

lsst.geom.Point2I(-2, -3), lsst.geom.Point2I(5, 7)) 

 

coeffs = np.array([[5.0]]) 

field = lsst.afw.math.ChebyshevBoundedField(bbox, coeffs) 

self.assertEqual(field.mean(), coeffs[0, 0]) 

 

coeffs = np.array([[5.0, 0.0, 3.0]]) 

field = lsst.afw.math.ChebyshevBoundedField(bbox, coeffs) 

self.assertEqual(field.mean(), coeffs[0, 0] - coeffs[0, 2]/3.0) 

 

# 2nd order polynomial in x,y 

coeffs = np.zeros((3, 3)) 

coeffs[0, 0] = 7.0 

coeffs[1, 0] = 31.0 

coeffs[0, 2] = 13.0 

coeffs[2, 2] = 11.0 

field = lsst.afw.math.ChebyshevBoundedField(bbox, coeffs) 

self.assertFloatsAlmostEqual( 

field.mean(), coeffs[0, 0] - coeffs[0, 2]/3.0 + coeffs[2, 2]/9.0) 

 

def testImageFit(self): 

"""Test that we can fit an image produced by a ChebyshevBoundedField and 

get the same coefficients back. 

""" 

for ctrl, coefficients in self.cases: 

inField = lsst.afw.math.ChebyshevBoundedField( 

self.bbox, coefficients) 

for Image in (lsst.afw.image.ImageF, lsst.afw.image.ImageD): 

image = Image(self.bbox) 

inField.fillImage(image) 

outField = lsst.afw.math.ChebyshevBoundedField.fit(image, ctrl) 

self.assertFloatsAlmostEqual( 

outField.getCoefficients(), coefficients, rtol=1E-6, atol=1E-7) 

 

def testArrayFit(self): 

"""Test that we can fit 1-d arrays produced by a ChebyshevBoundedField and 

get the same coefficients back. 

""" 

for ctrl, coefficients in self.cases: 

inField = lsst.afw.math.ChebyshevBoundedField( 

self.bbox, coefficients) 

for Image in (lsst.afw.image.ImageF, lsst.afw.image.ImageD): 

array = inField.evaluate(self.xFlat, self.yFlat) 

outField1 = lsst.afw.math.ChebyshevBoundedField.fit(self.bbox, self.xFlat, self.yFlat, 

array, ctrl) 

self.assertFloatsAlmostEqual( 

outField1.getCoefficients(), coefficients, rtol=1E-6, atol=1E-7) 

weights = (1.0 + np.random.randn(array.size)**2) 

# Should get same results with different weights, since we still have no noise 

# and a model that can exactly reproduce the data. 

outField2 = lsst.afw.math.ChebyshevBoundedField.fit(self.bbox, self.xFlat, self.yFlat, 

array, weights, ctrl) 

self.assertFloatsAlmostEqual( 

outField2.getCoefficients(), coefficients, rtol=1E-7, atol=1E-7) 

 

def testPersistence(self): 

"""Test that we can fit 1-d arrays produced by a ChebyshevBoundedField and 

get the same coefficients back. 

""" 

boxD = lsst.geom.Box2D(self.bbox) 

nPoints = 50 

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

for ctrl, coefficients in self.cases: 

inField = lsst.afw.math.ChebyshevBoundedField( 

self.bbox, coefficients) 

inField.writeFits(filename) 

outField = lsst.afw.math.ChebyshevBoundedField.readFits(filename) 

self.assertEqual(inField.getBBox(), outField.getBBox()) 

self.assertFloatsAlmostEqual( 

inField.getCoefficients(), outField.getCoefficients()) 

x = np.random.rand(nPoints)*boxD.getWidth() + boxD.getMinX() 

y = np.random.rand(nPoints)*boxD.getHeight() + boxD.getMinY() 

z1 = inField.evaluate(x, y) 

z2 = inField.evaluate(x, y) 

self.assertFloatsAlmostEqual(z1, z2, rtol=1E-13) 

 

# test with an empty bbox 

inField = lsst.afw.math.ChebyshevBoundedField(lsst.geom.Box2I(), 

np.array([[1.0, 2.0], [3.0, 4.0]])) 

inField.writeFits(filename) 

outField = lsst.afw.math.ChebyshevBoundedField.readFits(filename) 

self.assertEqual(inField.getBBox(), outField.getBBox()) 

 

def testTruncate(self): 

"""Test that truncate() works as expected 

""" 

for ctrl, coefficients in self.cases: 

field1 = lsst.afw.math.ChebyshevBoundedField( 

self.bbox, coefficients) 

field2 = field1.truncate(ctrl) 

self.assertFloatsAlmostEqual( 

field1.getCoefficients(), field2.getCoefficients()) 

self.assertEqual(field1.getBBox(), field2.getBBox()) 

config3 = lsst.afw.math.ChebyshevBoundedField.ConfigClass() 

config3.readControl(ctrl) 

if ctrl.orderX > 0: 

config3.orderX -= 1 

if ctrl.orderY > 0: 

config3.orderY -= 1 

field3 = field1.truncate(config3.makeControl()) 

for i in range(config3.orderY + 1): 

for j in range(config3.orderX + 1): 

if config3.triangular and i + j > max(config3.orderX, config3.orderY): 

self.assertEqual(field3.getCoefficients()[i, j], 0.0) 

else: 

self.assertEqual(field3.getCoefficients()[i, j], 

field1.getCoefficients()[i, j]) 

 

def testEquality(self): 

for ctrl, coefficients in self.cases: 

field1 = lsst.afw.math.ChebyshevBoundedField(self.bbox, coefficients) 

field2 = lsst.afw.math.ChebyshevBoundedField(self.bbox, coefficients) 

self.assertEqual(field1, field2, msg=coefficients) 

 

# same coefficients, instantiated from different arrays 

field1 = lsst.afw.math.ChebyshevBoundedField(self.bbox, np.array([[1.0]])) 

field2 = lsst.afw.math.ChebyshevBoundedField(self.bbox, np.array([[1.0]])) 

self.assertEqual(field1, field2) 

field1 = lsst.afw.math.ChebyshevBoundedField(self.bbox, np.array([[1.0, 2.0], [3., 4.]])) 

field2 = lsst.afw.math.ChebyshevBoundedField(self.bbox, np.array([[1.0, 2.0], [3., 4.]])) 

self.assertEqual(field1, field2) 

 

# different coefficient(s) 

field1 = lsst.afw.math.ChebyshevBoundedField(self.bbox, np.array([[1.0]])) 

field2 = lsst.afw.math.ChebyshevBoundedField(self.bbox, np.array([[2.0]])) 

self.assertNotEqual(field1, field2) 

field1 = lsst.afw.math.ChebyshevBoundedField(self.bbox, np.array([[1.0, 0.0]])) 

field2 = lsst.afw.math.ChebyshevBoundedField(self.bbox, np.array([[1.0], [0.0]])) 

self.assertNotEqual(field1, field2) 

 

# different bbox 

bbox1 = lsst.geom.Box2I(lsst.geom.Point2I(-10, -10), lsst.geom.Point2I(5, 5)) 

bbox2 = lsst.geom.Box2I(lsst.geom.Point2I(-5, -5), lsst.geom.Point2I(5, 5)) 

field1 = lsst.afw.math.ChebyshevBoundedField(bbox1, np.array([[1.0]])) 

field2 = lsst.afw.math.ChebyshevBoundedField(bbox2, np.array([[1.0]])) 

self.assertNotEqual(field1, field2) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()