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

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

# 

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

# 

 

import itertools 

import math 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.math as afwMath 

import lsst.pex.exceptions as pexExceptions 

 

 

def nrange(num, start, delta): 

"""Return an array of num floats starting with start and incrementing by delta 

""" 

return np.arange(start, start + (delta * (num - 0.1)), delta) 

 

 

def sinc(x): 

"""Return the normalized sinc function: sinc(x) = sin(pi * x) / (pi * x) 

""" 

if abs(x) < 1.0e-15: 

return 1.0 

return math.sin(math.pi * x) / (math.pi * x) 

 

 

def referenceChebyshev1(x, n): 

"""Reference implementation of Chebyshev polynomials of the first kind 

 

f(x) = T_n(x) 

""" 

# from Wikipedia 

if n == 0: 

return 1.0 

if n == 1: 

return x 

return (2.0 * x * referenceChebyshev1(x, n-1)) - referenceChebyshev1(x, n-2) 

 

 

def referenceChebyshev1Polynomial1(x, params): 

"""Reference implementation of a 1-D polynomial of Chebyshev polynomials of the first kind 

 

f(x) = params[0] T_0(x) + params[1] T_1(x) + params[2] T_2(x) 

""" 

retVal = 0.0 

for ii in range(len(params)-1, -1, -1): 

retVal += params[ii] * referenceChebyshev1(x, ii) 

return retVal 

 

 

def referenceChebyshev1Polynomial2(x, y, params): 

"""Reference implementation of a 2-D polynomial of Chebyshev polynomials of the first kind 

 

f(x) = params[0] T_0(x) T_0(y) # order 0 

+ params[1] T_1(x) T_0(y) + params[2] T_0(x) T_1(y) # order 1 

+ params[3] T_2(x) T_0(y) + params[4] T_1(x) T_1(y) + params[5] T_0(x) T_2(y) # order 2 

+ ... 

 

Raise RuntimeError if the number of parameters does not match an integer order. 

""" 

retVal = 0.0 

order = 0 

y_order = 0 

for ii in range(0, len(params)): 

x_order = order - y_order 

retVal += params[ii] * \ 

referenceChebyshev1(x, x_order) * referenceChebyshev1(y, y_order) 

if x_order > 0: 

y_order += 1 

else: 

order += 1 

y_order = 0 

95 ↛ 96line 95 didn't jump to line 96, because the condition on line 95 was never true if y_order != 0: 

raise RuntimeError("invalid # of parameters=%d" % (len(params),)) 

return retVal 

 

 

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

def setUp(self): 

self.normErr = "Invalid {0} normalization: min={1}, max={2}, min/max norm=({3}, {4}) != (-1, 1)" 

# We need a slightly larger than the full floating point tolerance for 

# many of these tests. 

self.atol = 5e-14 

 

def testChebyshev1Function1D(self): 

errMsg = "{}: {} != {} for x={}, xMin={}, xMax={}, xNorm={}, params={}; {}" 

maxOrder = 6 

deltaParam = 0.3 

ranges = ((-1, 1), (-1, 0), (0, 1), (-17, -2), (-65.3, 2.132)) 

rangeIter = itertools.cycle(ranges) 

nPoints = 9 

 

for order in range(maxOrder + 1): 

xMin, xMax = next(rangeIter) 

xMean = (xMin + xMax) / 2.0 

xDelta = (xMax - xMin) / float(nPoints - 1) 

 

f = afwMath.Chebyshev1Function1D(order, xMin, xMax) 

numParams = f.getNParameters() 

params = np.arange(deltaParam, deltaParam * 

numParams + (deltaParam / 2.0), deltaParam) 

f.setParameters(params) 

g = afwMath.Chebyshev1Function1D(params, xMin, xMax) 

h = f.clone() 

 

self.assertEqual(f.getNParameters(), g.getNParameters()) 

 

self.assertEqual(f.getMinX(), xMin) 

self.assertEqual(f.getMaxX(), xMax) 

self.assertEqual(f.getOrder(), order) 

 

self.assertEqual(g.getMinX(), xMin) 

self.assertEqual(g.getMaxX(), xMax) 

self.assertEqual(g.getOrder(), order) 

 

minXNorm = None 

maxXNorm = None 

for x in np.arange(xMin, xMax + xDelta/2.0, xDelta): 

xNorm = 2.0 * (x - xMean) / float(xMax - xMin) 

if minXNorm is None or xNorm < minXNorm: 

minXNorm = xNorm 

144 ↛ 147line 144 didn't jump to line 147, because the condition on line 144 was never false if maxXNorm is None or xNorm > maxXNorm: 

maxXNorm = xNorm 

 

predVal = referenceChebyshev1Polynomial1(xNorm, params) 

msg = errMsg.format(type(f).__name__, f(x), predVal, x, xMin, xMax, xNorm, params, 

"order constructor") 

self.assertFloatsAlmostEqual( 

f(x), predVal, msg=msg, atol=self.atol) 

msg = errMsg.format(type(g).__name__, g(x), predVal, x, xMin, xMax, xNorm, params, 

"params constructor") 

self.assertFloatsAlmostEqual( 

g(x), predVal, msg=msg, atol=self.atol) 

msg = errMsg.format(type(h).__name__, h(x), 

predVal, x, xMin, xMax, xNorm, params, 

"clone") 

self.assertFloatsAlmostEqual( 

h(x), predVal, msg=msg, atol=self.atol) 

 

msg = self.normErr.format("x", xMin, xMax, minXNorm, maxXNorm) 

self.assertFloatsAlmostEqual( 

minXNorm, -1., msg=msg, atol=self.atol) 

self.assertFloatsAlmostEqual(maxXNorm, 1., msg=msg, atol=self.atol) 

 

def testChebyshev1Function2D(self): 

errMsg = ("{}: {} != {} for x={}, xMin={}, xMax={}, xNorm={}, " 

"yMin={}, yMax={}, yNorm={}, params={}; {}") 

maxOrder = 6 

deltaParam = 0.3 

ranges = ((-1, 1), (-1, 0), (0, 1), (-17, -2), (-65.3, 2.132)) 

xRangeIter = itertools.cycle(ranges) 

yRangeIter = itertools.cycle(ranges) 

next(yRangeIter) # make x and y ranges off from each other 

nPoints = 7 # number of points in x and y at which to test the functions 

 

for order in range(maxOrder + 1): 

xMin, xMax = next(xRangeIter) 

xMean = (xMin + xMax) / 2.0 

xDelta = (xMax - xMin) / float(nPoints - 1) 

 

yMin, yMax = next(yRangeIter) 

yMean = (yMin + yMax) / 2.0 

yDelta = (yMax - yMin) / float(nPoints - 1) 

 

xyRange = lsst.geom.Box2D(lsst.geom.Point2D(xMin, yMin), 

lsst.geom.Point2D(xMax, yMax)) 

 

f = afwMath.Chebyshev1Function2D(order, xyRange) 

numParams = f.getNParameters() 

params = nrange(numParams, deltaParam, deltaParam) 

f.setParameters(params) 

g = afwMath.Chebyshev1Function2D(params, xyRange) 

h = f.clone() 

 

self.assertEqual(f.getNParameters(), g.getNParameters()) 

self.assertEqual(f.getNParameters(), h.getNParameters()) 

 

self.assertEqual(f.getXYRange(), xyRange) 

self.assertEqual(f.getOrder(), order) 

 

self.assertEqual(g.getXYRange(), xyRange) 

self.assertEqual(g.getOrder(), order) 

 

# vary x in the inner loop to exercise the caching 

minYNorm = None 

maxYNorm = None 

for y in np.arange(yMin, yMax + yDelta/2.0, yDelta): 

yNorm = 2.0 * (y - yMean) / float(yMax - yMin) 

if minYNorm is None or yNorm < minYNorm: 

minYNorm = yNorm 

213 ↛ 216line 213 didn't jump to line 216, because the condition on line 213 was never false if maxYNorm is None or yNorm > maxYNorm: 

maxYNorm = yNorm 

 

minXNorm = None 

maxXNorm = None 

for x in np.arange(xMin, xMax + xDelta/2.0, xDelta): 

xNorm = 2.0 * (x - xMean) / float(xMax - xMin) 

if minXNorm is None or xNorm < minXNorm: 

minXNorm = xNorm 

222 ↛ 218line 222 didn't jump to line 218, because the condition on line 222 was never false if maxXNorm is None or xNorm > maxXNorm: 

maxXNorm = xNorm 

 

predVal = referenceChebyshev1Polynomial2( 

xNorm, yNorm, params) 

 

msg = errMsg.format(type(f).__name__, f(x, y), predVal, x, xMin, xMax, xNorm, 

yMin, yMax, yNorm, params, "order constructor") 

self.assertFloatsAlmostEqual( 

f(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

msg = errMsg.format(type(g).__name__, g(x, y), predVal, x, xMin, xMax, xNorm, 

yMin, yMax, yNorm, params, "params constructor") 

self.assertFloatsAlmostEqual( 

g(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

msg = errMsg.format(type(h).__name__, h(x, y), predVal, x, xMin, xMax, xNorm, 

yMin, yMax, yNorm, params, "order") 

self.assertFloatsAlmostEqual( 

h(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

 

msg = self.normErr.format("x", xMin, xMax, minXNorm, maxXNorm) 

self.assertFloatsAlmostEqual( 

minXNorm, -1., msg=msg, atol=self.atol) 

self.assertFloatsAlmostEqual( 

maxXNorm, 1., msg=msg, atol=self.atol) 

 

msg = self.normErr.format("y", yMin, yMax, minYNorm, maxYNorm) 

self.assertFloatsAlmostEqual( 

minYNorm, -1., msg=msg, atol=self.atol) 

self.assertFloatsAlmostEqual(maxYNorm, 1., msg=msg, atol=self.atol) 

 

# test that the number of parameters is correct for the given order 

def numParamsFromOrder(order): 

return (order + 1) * (order + 2) // 2 

MaxOrder = 13 

for order in range(MaxOrder+1): 

f = afwMath.Chebyshev1Function2D(order) 

predNParams = numParamsFromOrder(order) 

self.assertEqual(f.getNParameters(), predNParams) 

afwMath.Chebyshev1Function2D(np.zeros(predNParams, dtype=float)) 

 

# test that the wrong number of parameters raises an exception 

validNumParams = set() 

for order in range(MaxOrder+1): 

validNumParams.add(numParamsFromOrder(order)) 

for numParams in range(numParamsFromOrder(MaxOrder)): 

if numParams in validNumParams: 

continue 

with self.assertRaises(pexExceptions.InvalidParameterError): 

afwMath.Chebyshev1Function2D(np.zeros(numParams, dtype=float)) 

 

# test that changing parameters clears the cache 

# for simplicity use the xyRange that requires no normalization 

order = 3 

numParams = numParamsFromOrder(order) 

f = afwMath.Chebyshev1Function2D(order) 

xyRange = lsst.geom.Box2D( 

lsst.geom.Point2D(-1.0, -1.0), lsst.geom.Point2D(1.0, 1.0)) 

x = 0.5 

y = -0.24 

for addValue in (0.0, 0.2): 

params = nrange(numParams, deltaParam + addValue, deltaParam) 

f.setParameters(params) 

predVal = referenceChebyshev1Polynomial2(x, y, params) 

msg = "%s != %s for x=%s, y=%s, params=%s" % ( 

f(x, y), predVal, x, y, params) 

self.assertFloatsAlmostEqual( 

f(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

 

def testChebyshev1Function2DTruncate(self): 

errMsg = ("{} != {} = {} for x={}, xMin={}, xMax={}, xNorm={}," 

" yMin={}, yMax={}, yNorm={}, truncParams={}; order constructor") 

 

maxOrder = 6 

deltaParam = 0.3 

ranges = ((-1, 1), (-17, -2), (-65.3, 2.132)) 

xRangeIter = itertools.cycle(ranges) 

yRangeIter = itertools.cycle(ranges) 

next(yRangeIter) # make x and y ranges off from each other 

nPoints = 7 # number of points in x and y at which to test the functions 

 

for order in range(maxOrder + 1): 

xMin, xMax = next(xRangeIter) 

xMean = (xMin + xMax) / 2.0 

xDelta = (xMax - xMin) / float(nPoints - 1) 

 

yMin, yMax = next(yRangeIter) 

yMean = (yMin + yMax) / 2.0 

yDelta = (yMax - yMin) / float(nPoints - 1) 

 

xyRange = lsst.geom.Box2D(lsst.geom.Point2D(xMin, yMin), 

lsst.geom.Point2D(xMax, yMax)) 

 

fullNParams = afwMath.Chebyshev1Function2D.nParametersFromOrder( 

order) 

fullParams = nrange(fullNParams, deltaParam, deltaParam) 

fullPoly = afwMath.Chebyshev1Function2D(fullParams, xyRange) 

 

for tooBigTruncOrder in range(order + 1, order + 3): 

with self.assertRaises(pexExceptions.InvalidParameterError): 

fullPoly.truncate(tooBigTruncOrder) 

 

for truncOrder in range(order + 1): 

truncNParams = fullPoly.nParametersFromOrder(truncOrder) 

truncParams = fullParams[0:truncNParams] 

 

f = fullPoly.truncate(truncOrder) 

self.assertEqual(f.getNParameters(), truncNParams) 

 

g = afwMath.Chebyshev1Function2D( 

fullParams[0:truncNParams], xyRange) 

 

self.assertEqual(f.getNParameters(), g.getNParameters()) 

 

self.assertEqual(f.getOrder(), truncOrder) 

self.assertEqual(f.getXYRange(), xyRange) 

 

self.assertEqual(g.getOrder(), truncOrder) 

self.assertEqual(g.getXYRange(), xyRange) 

 

minXNorm = None 

maxXNorm = None 

for x in np.arange(xMin, xMax + xDelta/2.0, xDelta): 

xNorm = 2.0 * (x - xMean) / float(xMax - xMin) 

if minXNorm is None or xNorm < minXNorm: 

minXNorm = xNorm 

347 ↛ 350line 347 didn't jump to line 350, because the condition on line 347 was never false if maxXNorm is None or xNorm > maxXNorm: 

maxXNorm = xNorm 

 

minYNorm = None 

maxYNorm = None 

for y in np.arange(yMin, yMax + yDelta/2.0, yDelta): 

yNorm = 2.0 * (y - yMean) / float(yMax - yMin) 

if minYNorm is None or yNorm < minYNorm: 

minYNorm = yNorm 

356 ↛ 352line 356 didn't jump to line 352, because the condition on line 356 was never false if maxYNorm is None or yNorm > maxYNorm: 

maxYNorm = yNorm 

 

msg = errMsg.format(type(f).__name__, f(x, y), g(x, y), type(g).__name__, 

x, xMin, xMax, xNorm, yMin, yMax, yNorm, truncParams) 

self.assertFloatsAlmostEqual( 

f(x, y), g(x, y), msg=msg) 

 

msg = self.normErr.format( 

"y", yMin, yMax, minYNorm, maxYNorm) 

self.assertFloatsAlmostEqual( 

minYNorm, -1.0, msg=msg, atol=self.atol, rtol=None) 

self.assertFloatsAlmostEqual( 

maxYNorm, 1.0, msg=msg, atol=self.atol, rtol=None) 

 

msg = self.normErr.format("x", xMin, xMax, minXNorm, maxXNorm) 

self.assertFloatsAlmostEqual( 

minXNorm, -1.0, msg=msg, atol=self.atol, rtol=None) 

self.assertFloatsAlmostEqual( 

maxXNorm, 1.0, msg=msg, atol=self.atol, rtol=None) 

 

def testGaussianFunction1D(self): 

def basicGaussian(x, sigma): 

return (1.0 / (sigma * math.sqrt(2 * math.pi))) * math.exp(-x**2 / (2.0 * sigma**2)) 

 

f = afwMath.GaussianFunction1D(1.0) 

for xsigma in (0.1, 1.0, 3.0): 

f.setParameters((xsigma,)) 

g = f.clone() 

xdelta = xsigma / 10.0 

fSum = 0.0 

for x in np.arange(-xsigma * 20, xsigma * 20.01, xdelta): 

predVal = basicGaussian(x, xsigma) 

fSum += predVal 

msg = "%s = %s != %s for x=%s, xsigma=%s" % ( 

type(f).__name__, f(x), predVal, x, xsigma) 

self.assertFloatsAlmostEqual( 

f(x), predVal, msg=msg, atol=self.atol, rtol=None) 

msg += "; clone" 

self.assertFloatsAlmostEqual( 

g(x), predVal, msg=msg, atol=self.atol, rtol=None) 

approxArea = fSum * xdelta 

msg = "%s area = %s != 1.0 for xsigma=%s" % ( 

type(f).__name__, approxArea, xsigma) 

self.assertFloatsAlmostEqual( 

approxArea, 1.0, msg=msg, atol=self.atol, rtol=None) 

 

def testGaussianFunction2D(self): 

"""Note: Assumes GaussianFunction1D is correct (tested elsewhere).""" 

errMsg = "{} = {} != {} for pos1={}, pos2={}, x={}, y={}, sigma1={}, sigma2={}, angle={}" 

areaMsg = "%s area = %s != 1.0 for sigma1=%s, sigma2=%s" 

f = afwMath.GaussianFunction2D(1.0, 1.0, 0.0) 

f1 = afwMath.GaussianFunction1D(1.0) 

f2 = afwMath.GaussianFunction1D(1.0) 

for sigma1 in (0.1, 1.0, 3.0): 

for sigma2 in (0.1, 1.0, 3.0): 

for angle in (0.0, 0.4, 1.1): 

sinNegAngle = math.sin(-angle) 

cosNegAngle = math.cos(-angle) 

f.setParameters((sigma1, sigma2, angle)) 

g = f.clone() 

f1.setParameters((sigma1,)) 

f2.setParameters((sigma2,)) 

fSum = 0.0 

delta1 = sigma1 / 5.0 

delta2 = sigma2 / 5.0 

for pos1 in np.arange(-sigma1 * 5, sigma1 * 5.01, delta1): 

for pos2 in np.arange(-sigma2 * 5.0, sigma2 * 5.01, delta2): 

x = (cosNegAngle * pos1) + (sinNegAngle * pos2) 

y = (-sinNegAngle * pos1) + (cosNegAngle * pos2) 

predVal = f1(pos1) * f2(pos2) 

fSum += predVal 

msg = errMsg.format(type(f).__name__, f(x, y), predVal, 

pos1, pos2, x, y, sigma1, sigma2, angle) 

self.assertFloatsAlmostEqual( 

f(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

msg = errMsg.format(type(g).__name__, g(x, y), predVal, 

pos1, pos2, x, y, sigma1, sigma2, angle) + "; clone" 

self.assertFloatsAlmostEqual( 

g(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

approxArea = fSum * delta1 * delta2 

msg = areaMsg % (type(f).__name__, 

approxArea, sigma1, sigma2) 

# approxArea is very approximate, so we need a high 

# tolerance threshold. 

self.assertFloatsAlmostEqual( 

approxArea, 1.0, msg=msg, atol=1e-6, rtol=None) 

 

def testDoubleGaussianFunction2D(self): 

"""Note: Assumes GaussianFunction2D is correct (tested elsewhere).""" 

errMsg = "{} = {} != {} for x={}, y={}, sigma1={}, sigma2={}, b={}" 

areaMsg = "{} area = {} != 1.0 for sigma1={}, sigma2={}" 

f = afwMath.DoubleGaussianFunction2D(1.0, 1.0) 

f1 = afwMath.GaussianFunction2D(1.0, 1.0, 0.0) 

f2 = afwMath.GaussianFunction2D(1.0, 1.0, 0.0) 

for sigma1 in (1.0,): 

for sigma2 in (0.5, 2.0): 

for b in (0.0, 0.2, 2.0): 

f.setParameters((sigma1, sigma2, b)) 

g = f.clone() 

f1.setParameters((sigma1, sigma1, 0.0)) 

f2.setParameters((sigma2, sigma2, 0.0)) 

sigma1Sq = sigma1**2 

sigma2Sq = sigma2**2 

f1Mult = b * sigma2Sq / sigma1Sq 

allMult = sigma1Sq / (sigma1Sq + (b * sigma2Sq)) 

fSum = 0.0 

maxsigma = max(sigma1, sigma2) 

minsigma = min(sigma1, sigma2) 

delta = minsigma / 5.0 

for y in np.arange(-maxsigma * 5, maxsigma * 5.01, delta): 

for x in np.arange(-maxsigma * 5.0, maxsigma * 5.01, delta): 

predVal = ( 

f1(x, y) + (f1Mult * f2(x, y))) * allMult 

fSum += predVal 

msg = errMsg.format( 

type(f).__name__, 

f(x, y), predVal, x, y, sigma1, sigma2, b) 

self.assertFloatsAlmostEqual( 

f(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

msg = errMsg.format(type(g).__name__, g(x, y), predVal, 

x, y, sigma1, sigma2, b) + "; clone" 

self.assertFloatsAlmostEqual( 

g(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

approxArea = fSum * delta**2 

msg = areaMsg.format( 

type(f).__name__, approxArea, sigma1, sigma2) 

# approxArea is very approximate, so we need a high 

# tolerance threshold. 

self.assertFloatsAlmostEqual( 

approxArea, 1.0, msg=msg, atol=1e-6, rtol=None) 

 

def testIntegerDeltaFunction2D(self): 

def basicDelta(x, xo): 

return (x == xo) 

 

errMsg = "{} = {} != {} for x={}, y={}, xo={}, yo={}" 

for xo in np.arange(-5.0, 5.0, 1.0): 

for yo in np.arange(-5.0, 5.0, 1.0): 

f = afwMath.IntegerDeltaFunction2D(xo, yo) 

g = f.clone() 

for x in np.arange(-5.0, 5.0, 1.0): 

for y in np.arange(-5.0, 5.0, 1.0): 

predVal = basicDelta(x, xo) * basicDelta(y, yo) 

msg = errMsg.format(type(f).__name__, 

f(x, y), predVal, x, y, xo, yo) 

self.assertFloatsAlmostEqual( 

f(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

msg = errMsg.format(type(g).__name__, 

g(x, y), predVal, x, y, xo, yo) + "; clone" 

self.assertFloatsAlmostEqual( 

g(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

 

def testLanczosFunction1D(self): 

def basicLanczos1(x, n): 

return sinc(x) * sinc(x / float(n)) 

 

errMsg = "{} = {} != {} for n={}, x={}, xOffset={}, xAdj={}" 

for n in range(1, 5): 

f = afwMath.LanczosFunction1D(n) 

self.assertEqual(f.getOrder(), n) 

 

for xOffset in (-10.0, 0.0, 0.05): 

f.setParameters((xOffset,)) 

g = f.clone() 

for x in np.arange(-10.0, 10.1, 0.50): 

xAdj = x - xOffset 

predVal = basicLanczos1(xAdj, n) 

msg = errMsg.format(type(f).__name__, 

f(x), predVal, n, x, xOffset, xAdj) 

self.assertFloatsAlmostEqual( 

f(x), predVal, msg=msg, atol=self.atol, rtol=None) 

msg = errMsg.format(type(g).__name__, 

g(x), predVal, n, x, xOffset, xAdj) + "; clone" 

self.assertFloatsAlmostEqual( 

g(x), predVal, msg=msg, atol=self.atol, rtol=None) 

 

def testLanczosFunction2D(self): 

def basicLanczos1(x, n): 

return sinc(x) * sinc(x / float(n)) 

 

errMsg = "{} = {} != {} for n={}, x={}, xOffset={}, yOffset={}, xAdj={}, yAdj={}" 

for n in range(1, 5): 

f = afwMath.LanczosFunction2D(n) 

self.assertEqual(f.getOrder(), n) 

 

for xOffset in (-10.0, 0.0, 0.05): 

for yOffset in (-0.01, 0.0, 7.5): 

f.setParameters((xOffset, yOffset)) 

g = f.clone() 

for x in np.arange(-10.0, 10.1, 2.0): 

for y in np.arange(-10.0, 10.1, 2.0): 

xAdj = x - xOffset 

yAdj = y - yOffset 

predVal = basicLanczos1(xAdj, n) * \ 

basicLanczos1(yAdj, n) 

msg = errMsg.format(type(f).__name__, f(x, y), predVal, n, 

x, xOffset, yOffset, xAdj, yAdj) 

self.assertFloatsAlmostEqual( 

f(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

msg = errMsg.format(type(g).__name__, g(x, y), predVal, n, 

x, xOffset, yOffset, xAdj, yAdj) 

self.assertFloatsAlmostEqual( 

g(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

 

def testPolynomialFunction1D(self): 

def basic1DPoly(x, params): 

ii = len(params) - 1 

retVal = params[ii] 

while ii > 0: 

ii -= 1 

retVal = retVal * x + params[ii] 

return retVal 

 

maxOrder = 4 

deltaParam = 0.3 

errMsg = "{} = {} != {} for x={}, params={}; {}" 

 

# test value using order constructor 

for order in range(maxOrder): 

numParams = order + 1 

params = nrange(numParams, deltaParam, deltaParam) 

f = afwMath.PolynomialFunction1D(params) 

g = afwMath.PolynomialFunction1D(order) 

g.setParameters(params) 

h = f.clone() 

 

self.assertEqual(f.getOrder(), order) 

self.assertEqual(g.getOrder(), order) 

 

for x in np.arange(-10.0, 10.1, 1.0): 

predVal = basic1DPoly(x, params) 

msg = errMsg.format(type(f).__name__, 

f(x), predVal, x, params, 

"params constructor") 

self.assertFloatsAlmostEqual( 

f(x), predVal, msg=msg, atol=self.atol, rtol=None) 

msg = errMsg.format(type(g).__name__, 

g(x), predVal, x, params, 

"order constructor") 

self.assertFloatsAlmostEqual( 

g(x), predVal, msg=msg, atol=self.atol, rtol=None) 

msg = errMsg.format(type(h).__name__, 

h(x), predVal, x, params, "clone") 

self.assertFloatsAlmostEqual( 

h(x), predVal, msg=msg, atol=self.atol, rtol=None) 

 

def testPolynomialFunction2D(self): 

def basic2DPoly(x, y, params): 

retVal = 0 

numParams = len(params) 

order = 0 

ii = 0 

while True: 

for yOrder in range(order+1): 

xOrder = order - yOrder 

retVal += params[ii] * x**xOrder * y**yOrder 

ii += 1 

if ii >= numParams: 

615 ↛ 616line 615 didn't jump to line 616, because the condition on line 615 was never true if xOrder != 0: 

raise RuntimeError( 

"invalid # params=%d" % (numParams,)) 

return retVal 

order += 1 

 

numParamsList = (1, 3, 6, 10) 

deltaParam = 0.3 

errMsg = "{} = {} != {} for x={}, y={}, params={}; {}" 

 

# test function values 

for order, numParams in enumerate(numParamsList): 

params = nrange(numParams, deltaParam, deltaParam) 

f = afwMath.PolynomialFunction2D(params) 

g = afwMath.PolynomialFunction2D(order) 

g.setParameters(params) 

h = f.clone() 

 

self.assertEqual(f.getOrder(), order) 

self.assertEqual(g.getOrder(), order) 

 

# vary x in the inner loop to exercise the caching 

for y in np.arange(-10.0, 10.1, 2.5): 

for x in np.arange(-10.0, 10.1, 2.5): 

predVal = basic2DPoly(x, y, params) 

msg = errMsg.format(type(f).__name__, f(x, y), predVal, 

x, y, params, "params constructor") 

self.assertFloatsAlmostEqual( 

f(x, y), predVal, msg=msg, atol=2e-12, rtol=None) 

msg = errMsg.format(type(g).__name__, 

g(x, y), predVal, x, y, params, 

"order constructor") 

self.assertFloatsAlmostEqual( 

g(x, y), predVal, msg=msg, atol=2e-12, rtol=None) 

msg = errMsg.format(type(h).__name__, 

h(x, y), predVal, x, y, params, "clone") 

self.assertFloatsAlmostEqual( 

h(x, y), predVal, msg=msg, atol=2e-12, rtol=None) 

 

# test that the number of parameters is correct for the given order 

def numParamsFromOrder(order): 

return (order + 1) * (order + 2) // 2 

MaxOrder = 13 

for order in range(MaxOrder+1): 

f = afwMath.PolynomialFunction2D(order) 

predNParams = numParamsFromOrder(order) 

self.assertEqual(f.getNParameters(), predNParams) 

afwMath.PolynomialFunction2D(np.zeros(predNParams, dtype=float)) 

 

# test that the wrong number of parameters raises an exception 

validNumParams = set() 

for order in range(MaxOrder+1): 

validNumParams.add(numParamsFromOrder(order)) 

for numParams in range(numParamsFromOrder(MaxOrder)): 

if numParams in validNumParams: 

continue 

with self.assertRaises(pexExceptions.InvalidParameterError): 

afwMath.PolynomialFunction2D(np.zeros(numParams, dtype=float)) 

 

# test that changing parameters clears the cache 

order = 3 

numParams = numParamsFromOrder(order) 

f = afwMath.PolynomialFunction2D(order) 

x = 0.5 

y = -0.24 

for addValue in (0.0, 0.2): 

params = nrange(numParams, deltaParam + addValue, deltaParam) 

f.setParameters(params) 

predVal = basic2DPoly(x, y, params) 

msg = errMsg.format(type(f).__name__, f(x, y), 

predVal, x, y, params, "") 

self.assertFloatsAlmostEqual( 

f(x, y), predVal, msg=msg, atol=self.atol, rtol=None) 

 

def testDFuncDParameters(self): 

"""Test that we can differentiate the Function2 with respect to its parameters""" 

nOrder = 3 

params = [] 

for i in range((nOrder + 1)*(nOrder + 2)//2): 

# deterministic pretty-random numbers 

params.append(math.sin(1 + i)) 

 

f = afwMath.PolynomialFunction2D(params) 

 

for (x, y) in [(2, 1), (1, 2), (2, 2)]: 

dFdC = f.getDFuncDParameters(x, y) 

 

self.assertAlmostEqual( 

f(x, y), 

sum([params[i]*dFdC[i] for i in range(len(params))])) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()