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

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

# 

# 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 os 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.afw.math as afwMath 

import lsst.pex.exceptions as pex 

 

testPath = os.path.abspath(os.path.dirname(__file__)) 

 

 

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

 

def testKdTreeGetData(self): 

""" 

Test that KdTree.getData() throws exceptions when it should and returns 

the correct data when it should. 

""" 

rng = np.random.RandomState(112) 

data = rng.random_sample((10, 10)) 

kd = afwMath.KdTreeD() 

kd.Initialize(data) 

 

# test that an exception is thrown if you ask for a point with 

# a negative index 

with self.assertRaises(RuntimeError) as context: 

kd.getData(-1, 0) 

self.assertIn("Asked for point that does not exist", 

context.exception.args[0]) 

 

# test that an exception is thrown if you ask for a point beyond 

# the number of points stored in the tree 

with self.assertRaises(RuntimeError) as context: 

kd.getData(10, 0) 

self.assertIn("Asked for point that does not exist", 

context.exception.args[0]) 

 

# test that an exception is thrown if you ask for dimensions that 

# don't exist 

with self.assertRaises(RuntimeError) as context: 

kd.getData(0, -1) 

self.assertIn("points of that many dimensions", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

kd.getData(0, 10) 

self.assertIn("points of that many dimensions", 

context.exception.args[0]) 

 

# test that the correct values are returned when they should be 

for ix in range(10): 

for iy in range(10): 

self.assertAlmostEqual( 

data[ix][iy], kd.getData(ix, iy), places=10) 

 

def testKdTreeGetDataVec(self): 

""" 

Test that KdTree.getData(int) throws exceptions when it should and returns 

the correct data when it should. 

""" 

rng = np.random.RandomState(112) 

data = rng.random_sample((10, 10)) 

kd = afwMath.KdTreeD() 

kd.Initialize(data) 

 

# test that an exception is thrown if you ask for a point with 

# a negative index 

with self.assertRaises(RuntimeError) as context: 

kd.getData(-1) 

self.assertIn("point that does not exist", 

context.exception.args[0]) 

 

# test that an exception is thrown if you ask for a point beyond 

# the number of points stored in the tree 

with self.assertRaises(RuntimeError) as context: 

kd.getData(10) 

self.assertIn("point that does not exist", 

context.exception.args[0]) 

 

# test that the correct values are returned when they should be 

for ix in range(10): 

vv = kd.getData(ix) 

for iy in range(10): 

self.assertAlmostEqual(data[ix][iy], vv[iy], places=10) 

 

def testKdTreeNeighborExceptions(self): 

""" 

This test will test that KdTree throws exceptions when you ask it for 

nonsensical number of nearest neighbors. 

""" 

rng = np.random.RandomState(112) 

data = rng.random_sample((10, 10)) 

kd = afwMath.KdTreeD() 

kd.Initialize(data) 

pt = rng.random_sample(10).astype(float) 

neighdex = np.zeros((5), dtype=np.int32) 

distances = np.zeros((5), dtype=float) 

 

# ask for a negative number of neighbors 

with self.assertRaises(RuntimeError) as context: 

kd.findNeighbors(neighdex, distances, pt, -2) 

self.assertIn("zero or a negative number of neighbors", 

context.exception.args[0]) 

 

# ask for zero neighbors 

with self.assertRaises(RuntimeError) as context: 

kd.findNeighbors(neighdex, distances, pt, 0) 

self.assertIn("zero or a negative number of neighbors", 

context.exception.args[0]) 

 

# ask for more neighbors than you have data 

with self.assertRaises(RuntimeError) as context: 

kd.findNeighbors(neighdex, distances, pt, 11) 

self.assertIn("more neighbors than kd tree contains", 

context.exception.args[0]) 

 

# try sending neighdex of wrong size 

neighdex_bad = np.zeros((1), dtype=np.int32) 

with self.assertRaises(RuntimeError) as context: 

kd.findNeighbors(neighdex_bad, distances, pt, 5) 

self.assertIn("Size of neighdex", 

context.exception.args[0]) 

 

# try sending distances array of wrong size 

distances_bad = np.zeros((1), dtype=float) 

with self.assertRaises(RuntimeError) as context: 

kd.findNeighbors(neighdex, distances_bad, pt, 5) 

self.assertIn("Size of dd", 

context.exception.args[0]) 

 

# run something that works 

kd.findNeighbors(neighdex, distances, pt, 5) 

 

def testKdTree(self): 

""" 

This test will test the construction of KdTree in the pathological case 

where many of the input data points are identical. 

""" 

pp = 100 

dd = 5 

data = np.zeros((pp, dd), dtype=float) 

tol = 1.0e-10 

 

with open(os.path.join(testPath, "data", "kd_test_data.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

for j in range(dd): 

data[i][j] = float(s[j]) 

 

kd = afwMath.KdTreeD() 

 

kd.Initialize(data) 

 

kds = afwMath.KdTreeD() 

 

kds.Initialize(data) 

 

kds.removePoint(2) 

 

worstErr = -1.0 

for i in range(100): 

if i > 2: 

dd = 0.0 

for j in range(5): 

dd = dd+(kd.getData(i, j)-kds.getData(i-1, j)) * \ 

(kd.getData(i, j)-kds.getData(i-1, j)) 

if dd > worstErr: 

worstErr = dd 

self.assertLess(worstErr, tol) 

 

kd.removePoint(2) 

 

kds.removePoint(10) 

 

for i in range(99): 

if i > 10: 

dd = 0.0 

for j in range(5): 

dd = dd+(kd.getData(i, j)-kds.getData(i-1, j)) * \ 

(kd.getData(i, j)-kds.getData(i-1, j)) 

206 ↛ 207line 206 didn't jump to line 207, because the condition on line 206 was never true if dd > worstErr: 

worstErr = dd 

self.assertLess(worstErr, tol) 

 

kd.removePoint(10) 

 

kds.removePoint(21) 

 

for i in range(98): 

if i > 21: 

dd = 0.0 

for j in range(5): 

dd = dd+(kd.getData(i, j)-kds.getData(i-1, j)) * \ 

(kd.getData(i, j)-kds.getData(i-1, j)) 

220 ↛ 221line 220 didn't jump to line 221, because the condition on line 220 was never true if dd > worstErr: 

worstErr = dd 

self.assertLess(worstErr, tol) 

print("\nworst distance error in kdTest ", worstErr, "\n") 

 

def testKdTreeNeighbors(self): 

""" 

Test that KdTree.findNeighbors() does find the nearest neighbors 

""" 

rng = np.random.RandomState(112) 

data = rng.random_sample((10, 10)) 

kd = afwMath.KdTreeD() 

kd.Initialize(data) 

pt = rng.random_sample(10).astype(float) 

neighdex = np.zeros((5), dtype=np.int32) 

distances = np.zeros((5), dtype=float) 

kd.findNeighbors(neighdex, distances, pt, 5) 

 

# check that the distances to the nearest neighbors are 

# correctly reported 

for ix in range(len(neighdex)): 

dd_true = np.sqrt(np.power(pt - data[neighdex[ix]], 2).sum()) 

self.assertAlmostEqual(dd_true, distances[ix], places=10) 

 

# check that the distances are returned in ascending order 

for ix in range(len(distances)-1): 

self.assertGreaterEqual(distances[ix+1], distances[ix]) 

 

# check that the actual nearest neighbors were found 

dd_true = np.sqrt(np.power(pt-data, 2).sum(axis=1)) 

sorted_dexes = np.argsort(dd_true) 

for ix in range(len(neighdex)): 

self.assertEqual(neighdex[ix], sorted_dexes[ix]) 

 

def testKdTreeAddPoint(self): 

""" 

Test the behavior of KdTree.addPoint 

""" 

data = np.array([[1.0, 0.0, 2.0], [1.1, 2.5, 0.0], [4.5, 6.1, 0.0]]) 

kd = afwMath.KdTreeD() 

kd.Initialize(data) 

 

# test that, if you try to add an improperly-sized point, an exception 

# is thrown 

with self.assertRaises(RuntimeError) as context: 

kd.addPoint(np.array([1.1]*2)) 

self.assertIn("incorrect dimensionality", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

kd.addPoint(np.array([1.1]*4)) 

self.assertIn("incorrect dimensionality", 

context.exception.args[0]) 

 

# test that adding a correctly sized-point works 

# (i.e. that the new point is added to the tree's data) 

kd.addPoint(np.array([1.1]*3)) 

 

self.assertEqual(kd.getNPoints(), 4) 

for ix in range(3): 

for iy in range(3): 

self.assertAlmostEqual( 

data[ix][iy], kd.getData(ix, iy), places=10) 

 

for ix in range(3): 

self.assertAlmostEqual(kd.getData(3, ix), 1.1, places=10) 

 

# check that the new point is accounted for in findNeighbors() 

neighdex = np.ones(2, dtype=np.int32) 

distances = np.ones(2, dtype=float) 

vv = np.array([1.2]*3) 

kd.findNeighbors(neighdex, distances, vv, 2) 

self.assertEqual(neighdex[0], 3) 

self.assertEqual(neighdex[1], 0) 

 

def testKdTreeRemovePoint(self): 

""" 

Test the behavior of KdTree.removePoint() 

""" 

data = np.array([[1.5, 1.5, 1.5], [2.0, 2.0, 2.0], 

[4.0, 4.0, 4.0], [3.0, 3.0, 3.0]]) 

kd = afwMath.KdTreeD() 

kd.Initialize(data) 

self.assertEqual(kd.getNPoints(), 4) 

 

# test that an exception is raised if you try to remove a non-existent 

# point 

with self.assertRaises(RuntimeError) as context: 

kd.removePoint(-1) 

self.assertIn("point that doesn't exist", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

kd.removePoint(4) 

self.assertIn("point that doesn't exist", 

context.exception.args[0]) 

 

# test that things work correctly when you do remove a point 

kd.removePoint(1) 

self.assertEqual(kd.getNPoints(), 3) 

for ix in range(3): 

self.assertAlmostEqual(kd.getData(0, ix), 1.5, places=10) 

self.assertAlmostEqual(kd.getData(1, ix), 4.0, places=10) 

self.assertAlmostEqual(kd.getData(2, ix), 3.0, places=10) 

 

neighdex = np.zeros(2, dtype=np.int32) 

distances = np.zeros(2, dtype=float) 

kd.findNeighbors(neighdex, distances, np.array([2.0, 2.0, 2.0]), 2) 

self.assertEqual(neighdex[0], 0) 

self.assertEqual(neighdex[1], 2) 

 

# test that an exception is raised when you try to remove the last 

# point 

kd.removePoint(0) 

kd.removePoint(0) 

with self.assertRaises(RuntimeError) as context: 

kd.removePoint(0) 

self.assertIn("There is only one point", 

context.exception.args[0]) 

 

def testKdTreeGetTreeNode(self): 

""" 

Test that KdTree.GetTreeNode raises exceptions if you give it bad inputs 

""" 

data = np.array([[1.5, 1.5, 1.5], [2.0, 2.0, 2.0], 

[4.0, 4.0, 4.0], [3.0, 3.0, 3.0]]) 

kd = afwMath.KdTreeD() 

kd.Initialize(data) 

 

vv = np.ones(4, dtype=np.int32) 

vv_small = np.ones(1, dtype=np.int32) 

vv_large = np.ones(5, dtype=np.int32) 

 

with self.assertRaises(RuntimeError) as context: 

kd.getTreeNode(vv_small, 0) 

self.assertIn("Need to pass a 4-element", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

kd.getTreeNode(vv_large, 0) 

self.assertIn("Need to pass a 4-element", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

kd.getTreeNode(vv, -1) 

self.assertIn("point that does not exist", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

kd.getTreeNode(vv, 4) 

self.assertIn("point that does not exist", 

context.exception.args[0]) 

 

# make sure that a call with good inputs passes 

kd.getTreeNode(vv, 0) 

 

 

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

 

def testConstructorExceptions(self): 

""" 

Test that exceptions are raised when constructor is given 

improper inputs 

""" 

rng = np.random.RandomState(22) 

 

# when you pass a different number of data points and function 

# values in 

data = rng.random_sample((10, 4)) 

fn_values = rng.random_sample(11) 

with self.assertRaises(RuntimeError) as context: 

afwMath.GaussianProcessD( 

data, fn_values, afwMath.SquaredExpCovariogramD()) 

self.assertIn("did not pass in the same number", 

context.exception.args[0]) 

 

max_val = np.ones(4) 

min_val = np.ones(4) 

with self.assertRaises(RuntimeError) as context: 

afwMath.GaussianProcessD(data, min_val, max_val, fn_values, 

afwMath.SquaredExpCovariogramD()) 

self.assertIn("did not pass in the same number", 

context.exception.args[0]) 

 

many_fn_values = rng.random_sample((11, 3)) 

with self.assertRaises(RuntimeError) as context: 

afwMath.GaussianProcessD( 

data, many_fn_values, afwMath.SquaredExpCovariogramD()) 

self.assertIn("did not pass in the same number", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

afwMath.GaussianProcessD(data, min_val, max_val, many_fn_values, 

afwMath.SquaredExpCovariogramD()) 

self.assertIn("did not pass in the same number", 

context.exception.args[0]) 

 

fn_values = rng.random_sample(data.shape[0]) 

many_fn_values = rng.random_sample((10, 3)) 

 

# when you pass in improperly sized min and max val arrays 

bad_max_val = np.ones(3) 

bad_min_val = np.zeros(3) 

with self.assertRaises(RuntimeError) as context: 

afwMath.GaussianProcessD(data, bad_min_val, max_val, 

fn_values, afwMath.SquaredExpCovariogramD()) 

self.assertIn("min/max values have different dimensionality", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

afwMath.GaussianProcessD(data, min_val, bad_max_val, 

fn_values, afwMath.SquaredExpCovariogramD()) 

self.assertIn("min/max values have different dimensionality", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

afwMath.GaussianProcessD(data, bad_min_val, max_val, 

many_fn_values, afwMath.SquaredExpCovariogramD()) 

self.assertIn("min/max values have different dimensionality", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

afwMath.GaussianProcessD(data, min_val, bad_max_val, 

many_fn_values, afwMath.SquaredExpCovariogramD()) 

self.assertIn("min/max values have different dimensionality", 

context.exception.args[0]) 

 

# check that the constructor runs when it should 

afwMath.GaussianProcessD( 

data, fn_values, afwMath.SquaredExpCovariogramD()) 

 

afwMath.GaussianProcessD(data, min_val, max_val, fn_values, 

afwMath.SquaredExpCovariogramD()) 

 

afwMath.GaussianProcessD(data, many_fn_values, 

afwMath.SquaredExpCovariogramD()) 

 

afwMath.GaussianProcessD(data, min_val, max_val, many_fn_values, 

afwMath.SquaredExpCovariogramD()) 

 

def testGetDataExceptions(self): 

""" 

Test that getData() returns exceptions on bad input 

""" 

rng = np.random.RandomState(111) 

data = rng.random_sample((14, 3)) 

fn = rng.random_sample(14) 

gp = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

indices = np.array([0, 5, 7], dtype=np.int32) 

indices_bad = np.array([0, 5, 16], dtype=np.int32) 

fn_out_good = np.zeros(3) 

fn_out_bad = np.zeros(4) 

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

pts_out_bad_ct = np.zeros((5, 3)) 

pts_out_bad_dim = np.zeros((3, 7)) 

 

# check that an exception is raised if we pass in an invalid 

# index 

with self.assertRaises(RuntimeError) as context: 

gp.getData(pts_out_good, fn_out_good, indices_bad) 

self.assertIn("point that does not exist", 

context.exception.args[0]) 

 

# check that an exception is raised if pts_out asks for 

# the wrong number of points 

with self.assertRaises(RuntimeError) as context: 

gp.getData(pts_out_bad_ct, fn_out_good, indices) 

self.assertIn("enough room in your pts array", 

context.exception.args[0]) 

 

# check that an exception is raised if pts_out expects 

# points of the wrong dimensionality 

with self.assertRaises(RuntimeError) as context: 

gp.getData(pts_out_bad_dim, fn_out_good, indices) 

self.assertIn("points of the wrong dimensionality", 

context.exception.args[0]) 

 

# check that an exception is raised if fn_out expects 

# the wrong number of points 

with self.assertRaises(RuntimeError) as context: 

gp.getData(pts_out_good, fn_out_bad, indices) 

self.assertIn("room in your function value array", 

context.exception.args[0]) 

 

# check that getData runs safely when given good input 

gp.getData(pts_out_good, fn_out_good, indices) 

 

fn_many = rng.random_sample((14, 5)) 

gp_many = afwMath.GaussianProcessD( 

data, fn_many, afwMath.SquaredExpCovariogramD()) 

 

# check that a GaussianProcess with many functions throws an 

# exception when you try to run getData designed for just one 

# function 

with self.assertRaises(RuntimeError) as context: 

gp_many.getData(pts_out_good, fn_out_good, indices) 

self.assertIn("enough room for all of the functions", 

context.exception.args[0]) 

 

# now test on a GaussianProcess with many functions 

 

fn_out_good = np.zeros((3, 5)) 

fn_out_bad_ct = np.zeros((4, 5)) 

fn_out_bad_fn = np.zeros((3, 6)) 

 

# check that an exception is raised when pts_out expects 

# the wrong number of points 

with self.assertRaises(RuntimeError) as context: 

gp_many.getData(pts_out_bad_ct, fn_out_good, indices) 

self.assertIn("room in your pts array", 

context.exception.args[0]) 

 

# check that an exception is raised when pts_out expects 

# pts of the wrong dimensionality 

with self.assertRaises(RuntimeError) as context: 

gp_many.getData(pts_out_bad_dim, fn_out_good, indices) 

self.assertIn("points of the wrong dimensionality", 

context.exception.args[0]) 

 

# check that an exception is raised when fn_out expects the 

# wrong number of pts 

with self.assertRaises(RuntimeError) as context: 

gp_many.getData(pts_out_good, fn_out_bad_ct, indices) 

self.assertIn("room in your function value array", 

context.exception.args[0]) 

 

# check that an exception is raised when fn_out expects the 

# wrong number of functions 

with self.assertRaises(RuntimeError) as context: 

gp_many.getData(pts_out_good, fn_out_bad_fn, indices) 

self.assertIn("enough room for all of the functions", 

context.exception.args[0]) 

 

# check that an exception is raised when one of the indices is 

# invalid 

with self.assertRaises(RuntimeError) as context: 

gp_many.getData(pts_out_good, fn_out_good, indices_bad) 

self.assertIn("point that does not exist", 

context.exception.args[0]) 

 

# check that getData runs safely when given good input 

gp_many.getData(pts_out_good, fn_out_good, indices) 

 

def testGetData(self): 

""" 

Test that getData actually returns what you expect 

""" 

rng = np.random.RandomState(131) 

data = rng.random_sample((14, 3)) 

fn = rng.random_sample(14) 

gp = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

pts_out = np.zeros((4, 3)) 

fn_out = np.zeros(4) 

indices = np.array([4, 1, 12, 8], dtype=np.int32) 

gp.getData(pts_out, fn_out, indices) 

 

for ii in range(len(indices)): 

self.assertAlmostEqual(fn_out[ii], fn[indices[ii]], 10) 

for jj in range(3): 

self.assertAlmostEqual(pts_out[ii][jj], data[indices[ii]][jj]) 

 

# now test with a max and min array 

max_arr = np.array([0.3]*3) 

min_arr = np.array([0.12]*3) 

gp = afwMath.GaussianProcessD(data, min_arr, max_arr, 

fn, afwMath.SquaredExpCovariogramD()) 

 

gp.getData(pts_out, fn_out, indices) 

 

for ii in range(len(indices)): 

self.assertAlmostEqual(fn_out[ii], fn[indices[ii]], 10) 

for jj in range(3): 

self.assertAlmostEqual(pts_out[ii][jj], data[indices[ii]][jj]) 

 

# now test on a GaussianProcess with many functions 

fn_many = rng.random_sample((14, 6)) 

fn_out = np.zeros((4, 6)) 

gp = afwMath.GaussianProcessD( 

data, fn_many, afwMath.SquaredExpCovariogramD()) 

 

gp.getData(pts_out, fn_out, indices) 

 

for ii in range(len(indices)): 

for jj in range(6): 

self.assertAlmostEqual( 

fn_out[ii][jj], fn_many[indices[ii]][jj], 10) 

for jj in range(3): 

self.assertAlmostEqual(pts_out[ii][jj], data[indices[ii]][jj]) 

 

# with min and max array 

gp = afwMath.GaussianProcessD(data, min_arr, max_arr, 

fn_many, afwMath.SquaredExpCovariogramD()) 

 

gp.getData(pts_out, fn_out, indices) 

 

for ii in range(len(indices)): 

for jj in range(6): 

self.assertAlmostEqual( 

fn_out[ii][jj], fn_many[indices[ii]][jj], 10) 

for jj in range(3): 

self.assertAlmostEqual(pts_out[ii][jj], data[indices[ii]][jj]) 

 

def testInterpolateExceptions(self): 

""" 

Test that interpolate() raises exceptions when given improper 

arguments 

""" 

rng = np.random.RandomState(88) 

data = rng.random_sample((13, 5)) 

fn = rng.random_sample(13) 

many_fn = rng.random_sample((13, 3)) 

gg = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

gg_many = afwMath.GaussianProcessD( 

data, many_fn, afwMath.SquaredExpCovariogramD()) 

 

var = np.zeros(1) 

 

many_var = np.zeros(3) 

many_mu = np.zeros(3) 

 

# test that an exception is raised if you try to interpolate at 

# a point with an incorrect number of dimensions 

bad_pt = rng.random_sample(3) 

with self.assertRaises(RuntimeError) as context: 

gg.interpolate(var, bad_pt, 5) 

self.assertIn("point with different dimensionality", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.interpolate(many_mu, many_var, bad_pt, 5) 

self.assertIn("point with different dimensionality", 

context.exception.args[0]) 

 

good_pt = rng.random_sample(5) 

 

# test that an exception is raised if you try to ask for an improper 

# number of nearest neighbors when interpolating 

with self.assertRaises(RuntimeError) as context: 

gg.interpolate(var, good_pt, 0) 

self.assertIn("zero or negative number of neighbors", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg.interpolate(var, good_pt, -1) 

self.assertIn("zero or negative number of neighbors", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg.interpolate(var, good_pt, 14) 

self.assertIn("more neighbors than you have", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.interpolate(many_mu, many_var, good_pt, 0) 

self.assertIn("zero or negative number of neighbors", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.interpolate(many_mu, many_var, good_pt, -1) 

self.assertIn("zero or negative number of neighbors", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.interpolate(many_mu, many_var, good_pt, 14) 

self.assertIn("more neighbors than you have", 

context.exception.args[0]) 

 

# make sure that a Gaussian Process interpolating many functions 

# does not let you call the interpolate() method that returns 

# a scalar 

with self.assertRaises(RuntimeError) as context: 

gg_many.interpolate(many_var, good_pt, 4) 

self.assertIn("You need to call the version", 

context.exception.args[0]) 

 

# test that the many-function interpolate throws an exception 

# on improperly-sized mu and variance arrays 

bad_var = np.zeros(6) 

bad_mu = np.zeros(6) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.interpolate(bad_mu, many_var, good_pt, 5) 

self.assertIn("mu and/or var arrays are improperly sized", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.interpolate(many_mu, bad_var, good_pt, 5) 

self.assertIn("mu and/or var arrays are improperly sized", 

context.exception.args[0]) 

 

# if you try to pass a variance array with len != 1 to 

# the single value interpolate() 

with self.assertRaises(RuntimeError) as context: 

gg.interpolate(many_var, good_pt, 5) 

self.assertIn("variance array is the incorrect size", 

context.exception.args[0]) 

 

gg.interpolate(var, good_pt, 5) 

gg_many.interpolate(many_mu, many_var, good_pt, 5) 

 

def testSelfInterpolateExceptions(self): 

""" 

Test that selfInterpolate raises exceptions on bad arguments. 

""" 

rng = np.random.RandomState(632) 

data = rng.random_sample((15, 4)) 

fn = rng.random_sample(15) 

gg = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

many_fn = rng.random_sample((15, 3)) 

gg_many = afwMath.GaussianProcessD( 

data, many_fn, afwMath.SquaredExpCovariogramD()) 

 

var_good = np.zeros(3) 

mu_good = np.zeros(3) 

 

var_bad = np.zeros(5) 

mu_bad = np.zeros(5) 

 

# test that an exception is raised when you try to use scalar 

# selfInterpolation() on a many-function GaussianProcess 

with self.assertRaises(RuntimeError) as context: 

gg_many.selfInterpolate(var_good, 11, 6) 

self.assertIn("that accepts mu and variance array", 

context.exception.args[0]) 

 

# test that an exception is raised when you pass a var_array that is 

# too large into a scalar GaussianProcess 

with self.assertRaises(RuntimeError) as context: 

gg.selfInterpolate(var_good, 11, 6) 

self.assertIn("variance array is the incorrect size", 

context.exception.args[0]) 

 

# test that an exception is thrown when you pass in improperly-sized 

# mu and/or var arrays 

with self.assertRaises(RuntimeError) as context: 

gg_many.selfInterpolate(mu_good, var_bad, 11, 6) 

self.assertIn("mu and/or var arrays are improperly sized", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.selfInterpolate(mu_bad, var_good, 11, 6) 

self.assertIn("mu and/or var arrays are improperly sized", 

context.exception.args[0]) 

 

# make surethat selfInterpolate runs when it should 

gg_many.selfInterpolate(mu_good, var_good, 11, 6) 

var_one = np.zeros(1) 

gg.selfInterpolate(var_one, 11, 6) 

 

def testBatchInterpolateExceptions(self): 

""" 

Test that batchInterpolate() throws exceptions on bad input 

""" 

rng = np.random.RandomState(88) 

rng = np.random.RandomState(632) 

data = rng.random_sample((15, 4)) 

fn = rng.random_sample(15) 

gg = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

 

pts_good = rng.random_sample((11, 4)) 

pts_bad = rng.random_sample((11, 3)) 

mu_good = np.zeros(11) 

mu_bad = np.zeros(9) 

var_good = np.zeros(11) 

var_bad = np.zeros(9) 

 

# test for exception on points of incorrect size 

with self.assertRaises(RuntimeError) as context: 

gg.batchInterpolate(mu_good, var_good, pts_bad) 

self.assertIn("wrong dimensionality", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg.batchInterpolate(mu_good, pts_bad) 

self.assertIn("wrong dimensionality", 

context.exception.args[0]) 

 

# test for exception on output arrays of incorrect size 

with self.assertRaises(RuntimeError) as context: 

gg.batchInterpolate(mu_bad, var_good, pts_good) 

self.assertIn("do not have room for all of the points", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg.batchInterpolate(mu_bad, pts_good) 

self.assertIn("does not have enough room for all of the points", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg.batchInterpolate(mu_good, var_bad, pts_good) 

self.assertIn("do not have room", 

context.exception.args[0]) 

 

# test that it runs properly with good inputs 

gg.batchInterpolate(mu_good, var_good, pts_good) 

gg.batchInterpolate(mu_good, pts_good) 

 

fn_many = rng.random_sample((15, 6)) 

gg_many = afwMath.GaussianProcessD( 

data, fn_many, afwMath.SquaredExpCovariogramD()) 

 

# test that a GaussianProcess on many functions raises an exception 

# when you call batchInterpolate with output arrays that only have 

# room for one function 

with self.assertRaises(RuntimeError) as context: 

gg_many.batchInterpolate(mu_good, var_good, pts_good) 

self.assertIn("do not have room for all of the functions", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.batchInterpolate(mu_good, pts_good) 

self.assertIn("does not have enough room for all of the functions", 

context.exception.args[0]) 

 

mu_good = np.zeros((11, 6)) 

mu_bad_fn = np.zeros((11, 5)) 

mu_bad_pts = np.zeros((10, 6)) 

var_good = np.zeros((11, 6)) 

var_bad_fn = np.zeros((11, 5)) 

var_bad_pts = np.zeros((10, 6)) 

 

# test that a Gaussian Process on many functions raises an exception 

# when you try to interpolate on points of the wrong dimensionality 

with self.assertRaises(RuntimeError) as context: 

gg_many.batchInterpolate(mu_good, var_good, pts_bad) 

self.assertIn("wrong dimensionality", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.batchInterpolate(mu_good, pts_bad) 

self.assertIn("do not have the correct dimensionality", 

context.exception.args[0]) 

 

# test that a Gaussian Process on many functions rases an exception 

# when the output arrays are of the wrong size 

with self.assertRaises(RuntimeError) as context: 

gg_many.batchInterpolate(mu_bad_fn, var_good, pts_good) 

self.assertIn("do not have room for all of the functions", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.batchInterpolate(mu_bad_pts, var_good, pts_good) 

self.assertIn("do not have room for all of the points", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.batchInterpolate(mu_bad_pts, pts_good) 

self.assertIn("does not have enough room for all of the points", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.batchInterpolate(mu_bad_fn, pts_good) 

self.assertIn("does not have enough room for all of the functions", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.batchInterpolate(mu_good, var_bad_fn, pts_good) 

self.assertIn("do not have room for all of the functions", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg_many.batchInterpolate(mu_good, var_bad_pts, pts_good) 

self.assertIn("do not have room for all of the points", 

context.exception.args[0]) 

 

# check that a Gaussian Process on many functions runs properly 

# when given good inputs 

gg_many.batchInterpolate(mu_good, var_good, pts_good) 

gg_many.batchInterpolate(mu_good, pts_good) 

 

def testTooManyNeighbors(self): 

""" 

Test that GaussianProcess checks if too many neighbours are requested 

""" 

nData = 100 # number of data points 

dimen = 10 # dimension of each point 

data = np.zeros((nData, dimen)) 

fn = np.zeros(nData) 

gg = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

test = np.zeros(dimen) 

sigma = np.empty(1) 

mu_arr = np.empty(1) 

 

with self.assertRaises(pex.Exception) as context: 

gg.interpolate(sigma, test, 2*nData) 

self.assertIn("more neighbors than", 

context.exception.args[0]) 

 

with self.assertRaises(pex.Exception) as context: 

gg.interpolate(sigma, test, -5) 

self.assertIn("zero or negative number of neighbors", 

context.exception.args[0]) 

 

with self.assertRaises(pex.Exception) as context: 

gg.selfInterpolate(sigma, 0, 2*nData) 

self.assertIn("more neighbors than", 

context.exception.args[0]) 

 

with self.assertRaises(pex.Exception) as context: 

gg.selfInterpolate(sigma, 0, -5) 

self.assertIn("zero or negative number of neighbors", 

context.exception.args[0]) 

 

with self.assertRaises(pex.Exception) as context: 

gg.selfInterpolate(sigma, -1, nData-1) 

self.assertIn("point that does not exist", 

context.exception.args[0]) 

 

with self.assertRaises(pex.Exception) as context: 

gg.selfInterpolate(sigma, nData, nData-1) 

self.assertIn("point that does not exist", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg.interpolate(mu_arr, sigma, 2*nData) 

self.assertIn("more neighbors than", 

context.exception.args[0]) 

 

with self.assertRaises(pex.Exception) as context: 

gg.interpolate(mu_arr, sigma, 2*nData) 

self.assertIn("more neighbors than", 

context.exception.args[0]) 

 

with self.assertRaises(pex.Exception) as context: 

gg.interpolate(mu_arr, sigma, -5) 

self.assertIn("zero or negative number of neighbors", 

context.exception.args[0]) 

 

def testInterpolate(self): 

""" 

This will test GaussianProcess.interpolate using both the squared 

exponential covariogram and the neural network covariogram on data 

that was generated with known answers. 

 

The test will check that the code returns the correct values of both 

mu (interpolated function value) and sig2 (the variance) 

 

This test uses the GaussianProcess constructor that does not normalize 

coordinate values with minima and maxima. 

""" 

 

pp = 2000 # number of data points 

dd = 10 # number of dimensions 

kk = 15 # number of nearest neighbors being used 

tol = 1.0e-3 # the largest relative error that will be tolerated 

 

data = np.zeros((pp, dd), dtype=float) # input data points 

fn = np.zeros((pp), dtype=float) # input function values 

test = np.zeros((dd), dtype=float) # query points 

sigma = np.zeros((1), dtype=float) # variance 

 

xx = afwMath.SquaredExpCovariogramD() 

xx.setEllSquared(100.0) 

 

# read in the input data 

with open(os.path.join(testPath, "data", "gp_exp_covar_data.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

fn[i] = float(s[10]) 

for j in range(10): 

data[i][j] = float(s[j]) 

 

# first try the squared exponential covariogram (the default) 

try: 

gg = afwMath.GaussianProcessD(data, fn, xx) 

except pex.Exception as e: 

print(e.what()) 

 

gg.setLambda(0.001) 

 

# now, read in the test points and their corresponding known solutions 

with open(os.path.join(testPath, "data", "gp_exp_covar_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

worstMuErr = -1.0 # keep track of the worst fractional error in mu 

worstSigErr = -1.0 # keep track of the worst fractional error in the variance 

 

for z in range(len(ff)): 

s = ff[z].split() # s will store the zth line of the solution file 

for i in range(dd): 

test[i] = float(s[i]) # read in the test point coordinates 

 

mushld = float(s[dd + kk]) # read in what mu should be 

# read in what the variance should be 

sigshld = float(s[dd + kk + 1]) 

 

mu = gg.interpolate(sigma, test, kk) 

 

err = (mu - mushld) 

1017 ↛ 1020line 1017 didn't jump to line 1020, because the condition on line 1017 was never false if mushld != 0.0: 

err = err/mushld 

 

if err < 0.0: 

err = -1.0 * err 

if z == 0 or err > worstMuErr: 

worstMuErr = err 

 

err = (sigma[0] - sigshld) 

1026 ↛ 1029line 1026 didn't jump to line 1029, because the condition on line 1026 was never false if sigshld != 0.0: 

err = err/sigshld 

 

if err < 0.0: 

err = -1.0 * err 

if z == 0 or err > worstSigErr: 

worstSigErr = err 

 

print("\nThe errors for squared exponent covariogram\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

# now try with the Neural Network covariogram 

 

kk = 50 

 

nn = afwMath.NeuralNetCovariogramD() 

nn.setSigma0(1.23) 

nn.setSigma1(0.452) 

 

gg.setCovariogram(nn) 

gg.setLambda(0.0045) 

 

with open(os.path.join(testPath, "data", "gp_nn_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

worstMuErr = -1.0 

worstSigErr = -1.0 

 

for z in range(len(ff)): 

s = ff[z].split() 

for i in range(dd): 

test[i] = float(s[i]) 

 

mushld = float(s[dd + kk]) 

sigshld = float(s[dd + kk + 1]) 

 

mu = gg.interpolate(sigma, test, kk) 

 

err = (mu - mushld) 

1069 ↛ 1072line 1069 didn't jump to line 1072, because the condition on line 1069 was never false if mushld != 0.0: 

err = err/mushld 

 

if err < 0.0: 

err = -1.0 * err 

if z == 0 or err > worstMuErr: 

worstMuErr = err 

 

err = (sigma[0] - sigshld) 

1078 ↛ 1081line 1078 didn't jump to line 1081, because the condition on line 1078 was never false if sigshld != 0.0: 

err = err/sigshld 

 

if err < 0.0: 

err = -1.0 * err 

if z == 0 or err > worstSigErr: 

worstSigErr = err 

 

print("\nThe errors for neural net covariogram\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

def testMinMax(self): 

""" 

This test will test GaussianProcess.interpolate using the constructor 

that normalizes data point coordinates by minima and maxima. 

 

It will only use the squared exponential covariogram (since testInterpolate() presumably 

tested the performance of the neural network covariogram; this test is only concerned 

with the alternate constructor) 

 

This test proceeds largely like testInterpolate above 

""" 

pp = 2000 

dd = 10 

kk = 50 

tol = 1.0e-4 

data = np.zeros((pp, dd), dtype=float) 

fn = np.zeros((pp), dtype=float) 

test = np.zeros((dd), dtype=float) 

sigma = np.zeros((1), dtype=float) 

 

mins = np.zeros((dd), dtype=float) 

maxs = np.zeros((dd), dtype=float) 

 

nn = afwMath.NeuralNetCovariogramD() 

nn.setSigma0(0.555) 

nn.setSigma1(0.112) 

 

with open(os.path.join(testPath, "data", "gp_exp_covar_data.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

fn[i] = float(s[10]) 

for j in range(10): 

data[i][j] = float(s[j]) 

 

for i in range(pp): 

for j in range(dd): 

if (i == 0) or (data[i][j] < mins[j]): 

mins[j] = data[i][j] 

if (i == 0) or (data[i][j] > maxs[j]): 

maxs[j] = data[i][j] 

 

mins[2] = 0.0 

maxs[2] = 10.0 

try: 

gg = afwMath.GaussianProcessD(data, mins, maxs, fn, nn) 

except pex.Exception as e: 

print(e.what()) 

 

gg.setLambda(0.0045) 

 

with open(os.path.join(testPath, "data", "gp_minmax_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

worstMuErr = -1.0 

worstSigErr = -1.0 

for z in range(len(ff)): 

s = ff[z].split() 

for i in range(dd): 

test[i] = float(s[i]) 

 

mushld = float(s[dd + kk]) 

sigshld = float(s[dd + kk + 1]) 

 

mu = gg.interpolate(sigma, test, kk) 

 

err = (mu - mushld) 

1161 ↛ 1164line 1161 didn't jump to line 1164, because the condition on line 1161 was never false if mushld != 0.0: 

err = err/mushld 

 

if err < 0.0: 

err = -1.0 * err 

if z == 0 or err > worstMuErr: 

worstMuErr = err 

 

err = (sigma[0] - sigshld) 

1170 ↛ 1173line 1170 didn't jump to line 1173, because the condition on line 1170 was never false if sigshld != 0.0: 

err = err/sigshld 

 

if err < 0.0: 

err = -1.0 * err 

if z == 0 or err > worstSigErr: 

worstSigErr = err 

 

print("\nThe errors for Gaussian process using min-max normalization\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

def testAddPointExceptions(self): 

""" 

Test that addPoint() raises exceptions when it should 

""" 

rng = np.random.RandomState(44) 

data = rng.random_sample((10, 3)) 

fn = rng.random_sample(10) 

gg = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

fn_many = rng.random_sample((10, 5)) 

gg_many = afwMath.GaussianProcessD( 

data, fn_many, afwMath.SquaredExpCovariogramD()) 

 

pt_good = rng.random_sample(3) 

pt_bad = rng.random_sample(6) 

fn_good = rng.random_sample(5) 

fn_bad = rng.random_sample(4) 

 

# test that, when you add a point of the wrong dimensionality, 

# an exception is raised 

with self.assertRaises(RuntimeError) as context: 

gg.addPoint(pt_bad, 5.0) 

self.assertIn("dimensionality", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg.addPoint(pt_bad, fn_good) 

self.assertIn("dimensionality", 

context.exception.args[0]) 

 

# test that a GaussianProcess on many functions raises an exception 

# when you try to add a point with just one function value 

with self.assertRaises(RuntimeError) as context: 

gg_many.addPoint(pt_good, 5.0) 

self.assertIn("calling the wrong addPoint", 

context.exception.args[0]) 

 

# test that a GaussianProcess on many functions raises an 

# exception when you try to add a point with the wrong number 

# of function values 

with self.assertRaises(RuntimeError) as context: 

gg_many.addPoint(pt_good, fn_bad) 

self.assertIn("number of function values", 

context.exception.args[0]) 

 

# check that, given good inputs, addPoint will run 

gg.addPoint(pt_good, 5.0) 

gg_many.addPoint(pt_good, fn_good) 

 

# since we started with 10 data points, we should now have 11 

self.assertEqual(gg.getNPoints(), 11) 

self.assertEqual(gg_many.getNPoints(), 11) 

 

def testAddPoint(self): 

""" 

Test that getData works as expected after running addPoint() 

""" 

rng = np.random.RandomState(99) 

data = rng.random_sample((15, 4)) 

fn = rng.random_sample(15) 

gp = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

self.assertEqual(gp.getNPoints(), 15) 

pt_add = rng.random_sample(4) 

fn_add = 0.98453 

gp.addPoint(pt_add, fn_add) 

self.assertEqual(gp.getNPoints(), 16) 

fn_out = np.zeros(1) 

pt_out = np.zeros((1, 4)) 

indices = np.array([15], dtype=np.int32) 

gp.getData(pt_out, fn_out, indices) 

for ii in range(4): 

self.assertAlmostEqual(pt_out[0][ii], pt_add[ii], 10) 

self.assertAlmostEqual(fn_out[0], fn_add, 10) 

 

# now try it with a GaussianProcess on many functions 

fn = rng.random_sample((15, 3)) 

gp = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

self.assertEqual(gp.getNPoints(), 15) 

pt_add = rng.random_sample(4) 

fn_add = rng.random_sample(3) 

gp.addPoint(pt_add, fn_add) 

self.assertEqual(gp.getNPoints(), 16) 

fn_out = np.zeros((1, 3)) 

gp.getData(pt_out, fn_out, indices) 

for ii in range(4): 

self.assertEqual(pt_out[0][ii], pt_add[ii], 10) 

for ii in range(3): 

self.assertEqual(fn_out[0][ii], fn_add[ii], 10) 

 

def testAdditionInterpolation(self): 

""" 

This will test the performance of interpolation after adding new points 

to GaussianProcess' data set 

""" 

pp = 1000 

dd = 10 

kk = 15 

tol = 1.0e-4 

 

data = np.zeros((pp, dd), dtype=float) 

fn = np.zeros((pp), dtype=float) 

test = np.zeros((dd), dtype=float) 

sigma = np.zeros((1), dtype=float) 

 

xx = afwMath.SquaredExpCovariogramD() 

xx.setEllSquared(5.0) 

 

with open(os.path.join(testPath, "data", "gp_additive_test_root.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

fn[i] = float(s[10]) 

for j in range(10): 

data[i][j] = float(s[j]) 

 

# establish the Gaussian Process 

try: 

gg = afwMath.GaussianProcessD(data, fn, xx) 

except pex.Exception as e: 

print(e.what()) 

 

gg.setLambda(0.002) 

 

# now add new points to it and see if GaussianProcess.interpolate performs 

# correctly 

with open(os.path.join(testPath, "data", "gp_additive_test_data.sav"), "r") as f: 

ff = f.readlines() 

 

for z in range(len(ff)): 

s = ff[z].split() 

for i in range(dd): 

test[i] = float(s[i]) 

mushld = float(s[dd]) 

try: 

gg.addPoint(test, mushld) 

except pex.Exception as e: 

print(e.what()) 

 

with open(os.path.join(testPath, "data", "gp_additive_test_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

worstMuErr = -1.0 

worstSigErr = -1.0 

 

for z in range(len(ff)): 

s = ff[z].split() 

for i in range(dd): 

test[i] = float(s[i]) 

 

mushld = float(s[dd + kk]) 

sigshld = float(s[dd + kk + 1]) 

 

mu = gg.interpolate(sigma, test, kk) 

 

err = (mu - mushld) 

1343 ↛ 1345line 1343 didn't jump to line 1345, because the condition on line 1343 was never false if mushld != 0: 

err = err/mushld 

if err < 0.0: 

err = -1.0 * err 

if z == 0 or err > worstMuErr: 

worstMuErr = err 

 

err = (sigma[0] - sigshld) 

1351 ↛ 1353line 1351 didn't jump to line 1353, because the condition on line 1351 was never false if sigshld != 0: 

err = err/sigshld 

if err < 0.0: 

err = -1.0 * err 

if z == 0 or err > worstSigErr: 

worstSigErr = err 

 

print("\nThe errors for the test of adding points to the Gaussian process\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

def testBatch(self): 

""" 

This test will test GaussianProcess.batchInterpolate both with 

and without variance calculation 

""" 

pp = 100 

dd = 10 

tol = 1.0e-3 

 

data = np.zeros((pp, dd), dtype=float) 

fn = np.zeros((pp), dtype=float) 

 

with open(os.path.join(testPath, "data", "gp_exp_covar_data.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(100): 

s = ff[i].split() 

for j in range(dd): 

data[i][j] = float(s[j]) 

fn[i] = float(s[dd]) 

 

xx = afwMath.SquaredExpCovariogramD() 

xx.setEllSquared(2.0) 

 

try: 

gg = afwMath.GaussianProcessD(data, fn, xx) 

except pex.Exception as e: 

print(e.what()) 

 

gg.setLambda(0.0032) 

 

with open(os.path.join(testPath, "data", "gp_batch_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

ntest = len(ff) 

mushld = np.zeros((ntest), dtype=float) 

varshld = np.zeros((ntest), dtype=float) 

mu = np.zeros((ntest), dtype=float) 

var = np.zeros((ntest), dtype=float) 

 

queries = np.zeros((ntest, dd), dtype=float) 

 

for i in range(ntest): 

s = ff[i].split() 

for j in range(dd): 

queries[i][j] = float(s[j]) 

mushld[i] = float(s[dd]) 

varshld[i] = float(s[dd + 1]) 

 

# test with variance calculation 

gg.batchInterpolate(mu, var, queries) 

 

worstMuErr = -1.0 

worstVarErr = -1.0 

for i in range(ntest): 

err = mu[i]-mushld[i] 

1421 ↛ 1423line 1421 didn't jump to line 1423, because the condition on line 1421 was never false if mushld[i] != 0.0: 

err = err/mushld[i] 

1423 ↛ 1424line 1423 didn't jump to line 1424, because the condition on line 1423 was never true if err < 0.0: 

err = -1.0 * err 

if err > worstMuErr: 

worstMuErr = err 

 

err = var[i]-varshld[i] 

1429 ↛ 1431line 1429 didn't jump to line 1431, because the condition on line 1429 was never false if varshld[i] != 0.0: 

err = err/varshld[i] 

if err < 0.0: 

err = -1.0 * err 

if err > worstVarErr: 

worstVarErr = err 

 

# test without variance interpolation 

# continue keeping track of worstMuErr 

gg.batchInterpolate(mu, queries) 

for i in range(ntest): 

err = mu[i]-mushld[i] 

1441 ↛ 1443line 1441 didn't jump to line 1443, because the condition on line 1441 was never false if mushld[i] != 0.0: 

err = err/mushld[i] 

1443 ↛ 1444line 1443 didn't jump to line 1444, because the condition on line 1443 was never true if err < 0.0: 

err = -1.0 * err 

1445 ↛ 1446line 1445 didn't jump to line 1446, because the condition on line 1445 was never true if err > worstMuErr: 

worstMuErr = err 

 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstVarErr, tol) 

 

print("\nThe errors for batch interpolation\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstVarErr) 

 

def testSelf(self): 

""" 

This test will test GaussianProcess.selfInterpolation 

""" 

pp = 2000 

dd = 10 

tol = 1.0e-3 

kk = 20 

 

data = np.zeros((pp, dd), dtype=float) 

fn = np.zeros((pp), dtype=float) 

 

with open(os.path.join(testPath, "data", "gp_exp_covar_data.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(pp): 

s = ff[i].split() 

for j in range(dd): 

data[i][j] = float(s[j]) 

fn[i] = float(s[dd]) 

 

xx = afwMath.SquaredExpCovariogramD() 

xx.setEllSquared(20.0) 

try: 

gg = afwMath.GaussianProcessD(data, fn, xx) 

except pex.Exception as e: 

print(e.what()) 

 

gg.setKrigingParameter(30.0) 

gg.setLambda(0.00002) 

 

with open(os.path.join(testPath, "data", "gp_self_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

variance = np.zeros((1), dtype=float) 

 

worstMuErr = -1.0 

worstSigErr = -1.0 

 

for i in range(pp): 

s = ff[i].split() 

mushld = float(s[0]) 

sig2shld = float(s[1]) 

 

try: 

mu = gg.selfInterpolate(variance, i, kk) 

except pex.Exception as e: 

print(e.what()) 

 

err = mu - mushld 

1505 ↛ 1507line 1505 didn't jump to line 1507, because the condition on line 1505 was never false if mushld != 0.0: 

err = err/mushld 

if err < 0.0: 

err = err * (-1.0) 

if i == 0 or err > worstMuErr: 

worstMuErr = err 

 

err = variance[0] - sig2shld 

1513 ↛ 1515line 1513 didn't jump to line 1515, because the condition on line 1513 was never false if sig2shld != 0.0: 

err = err/sig2shld 

if err < 0.0: 

err = err * (-1.0) 

if i == 0 or err > worstSigErr: 

worstSigErr = err 

 

print("\nThe errors for self interpolation\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

def testVector(self): 

""" 

This will test interpolate using a vector of functions 

""" 

 

tol = 1.0e-3 

data = np.zeros((2000, 10), dtype=float) 

fn = np.zeros((2000, 4), dtype=float) 

mu = np.zeros((4), dtype=float) 

sig = np.zeros((4), dtype=float) 

mushld = np.zeros((4), dtype=float) 

vv = np.zeros((10), dtype=float) 

vvf = np.zeros((4), dtype=float) 

 

kk = 30 

 

with open(os.path.join(testPath, "data", "gp_vector_data.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

for j in range(10): 

data[i][j] = float(s[j]) 

for j in range(4): 

fn[i][j] = float(s[j+10]) 

 

nn = afwMath.NeuralNetCovariogramD() 

nn.setSigma0(2.25) 

nn.setSigma1(0.76) 

try: 

gg = afwMath.GaussianProcessD(data, fn, nn) 

except pex.Exception as e: 

print(e.what()) 

 

gg.setLambda(0.0045) 

 

worstMuErr = -1.0 

worstSigErr = -1.0 

 

with open(os.path.join(testPath, "data", "gp_vector_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

for j in range(10): 

vv[j] = float(s[j]) 

for j in range(4): 

mushld[j] = float(s[j+10]) 

sigshld = float(s[14]) 

 

gg.interpolate(mu, sig, vv, kk) 

 

for j in range(4): 

muErr = (mu[j]-mushld[j])/mushld[j] 

sigErr = (sig[j]-sigshld)/sigshld 

if muErr < 0.0: 

muErr = -1.0 * muErr 

if sigErr < 0.0: 

sigErr = -1.0 * sigErr 

if (muErr > worstMuErr): 

worstMuErr = muErr 

if (sigErr > worstSigErr): 

worstSigErr = sigErr 

 

print("\nThe errors for vector interpolation\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

worstMuErr = -1.0 

worstSigErr = -1.0 

 

with open(os.path.join(testPath, "data", "gp_vector_selfinterpolate_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

try: 

gg.selfInterpolate(mu, sig, i, kk) 

except pex.Exception as e: 

print(e.what()) 

 

for j in range(4): 

mushld[j] = float(s[j]) 

sigshld = float(s[4]) 

 

for j in range(4): 

muErr = (mu[j]-mushld[j])/mushld[j] 

1615 ↛ 1616line 1615 didn't jump to line 1616, because the condition on line 1615 was never true if muErr < -1.0: 

muErr = -1.0 * muErr 

if muErr > worstMuErr: 

worstMuErr = muErr 

 

sigErr = (sig[j]-sigshld)/sigshld 

1621 ↛ 1622line 1621 didn't jump to line 1622, because the condition on line 1621 was never true if sigErr < -1.0: 

sigErr = -1.0*sigErr 

if sigErr > worstSigErr: 

worstSigErr = sigErr 

 

print("\nThe errors for vector self interpolation\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

queries = np.zeros((100, 10), dtype=float) 

batchMu = np.zeros((100, 4), dtype=float) 

batchSig = np.zeros((100, 4), dtype=float) 

batchMuShld = np.zeros((100, 4), dtype=float) 

batchSigShld = np.zeros((100, 4), dtype=float) 

batchData = np.zeros((200, 10), dtype=float) 

batchFunctions = np.zeros((200, 4), dtype=float) 

 

with open(os.path.join(testPath, "data", "gp_vectorbatch_data.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

for j in range(10): 

batchData[i][j] = float(s[j]) 

for j in range(4): 

batchFunctions[i][j] = float(s[j+10]) 

 

try: 

ggbatch = afwMath.GaussianProcessD(batchData, batchFunctions, nn) 

except pex.Exception as e: 

print(e.what()) 

 

ggbatch.setLambda(0.0045) 

 

with open(os.path.join(testPath, "data", "gp_vectorbatch_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

for j in range(10): 

queries[i][j] = float(s[j]) 

for j in range(4): 

batchMuShld[i][j] = float(s[j+10]) 

sigShld = float(s[14]) 

for j in range(4): 

batchSigShld[i][j] = sigShld 

 

ggbatch.batchInterpolate(batchMu, batchSig, queries) 

worstMuErr = -1.0 

worstSigErr = -1.0 

for i in range(100): 

for j in range(4): 

muErr = (batchMu[i][j]-batchMuShld[i][j])/batchMuShld[i][j] 

sigErr = (batchSig[i][j]-batchSigShld[i][j])/batchSigShld[i][j] 

if muErr < 0.0: 

muErr = muErr * (-1.0) 

if sigErr < 0.0: 

sigErr = sigErr * (-1.0) 

 

if muErr > worstMuErr: 

worstMuErr = muErr 

if sigErr > worstSigErr: 

worstSigErr = sigErr 

 

print("\nThe errors for vector batch interpolation with variance\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

ggbatch.batchInterpolate(batchMu, queries) 

worstMuErr = -1.0 

worstSigErr = -1.0 

for i in range(100): 

for j in range(4): 

muErr = (batchMu[i][j]-batchMuShld[i][j])/batchMuShld[i][j] 

if muErr < 0.0: 

muErr = muErr * (-1.0) 

 

if muErr > worstMuErr: 

worstMuErr = muErr 

 

print("\nThe errors for vector batch interpolation without variance\n") 

print("worst mu error ", worstMuErr) 

self.assertLess(worstMuErr, tol) 

 

with open(os.path.join(testPath, "data", "gp_vector_add_data.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

for j in range(10): 

vv[j] = float(s[j]) 

for j in range(4): 

vvf[j] = float(s[j+10]) 

try: 

gg.addPoint(vv, vvf) 

except pex.Exception as e: 

print(e.what()) 

 

with open(os.path.join(testPath, "data", "gp_vector_add_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

try: 

gg.selfInterpolate(mu, sig, i, kk) 

except pex.Exception as e: 

print(e.what()) 

 

for j in range(4): 

mushld[j] = float(s[j]) 

sigshld = float(s[4]) 

 

for j in range(4): 

muErr = (mu[j]-mushld[j])/mushld[j] 

if muErr < 0.0: 

muErr = -1.0 * muErr 

if muErr > worstMuErr: 

worstMuErr = muErr 

 

sigErr = (sig[j]-sigshld)/sigshld 

if sigErr < 0.0: 

sigErr = -1.0*sigErr 

if sigErr > worstSigErr: 

worstSigErr = sigErr 

 

print("\nThe errors for vector add interpolation\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

def testRemovePointException(self): 

""" 

Test that an exception is raised if you try to remove 

a point that does not exist from a Gaussian Process 

""" 

rng = np.random.RandomState(118) 

data = rng.random_sample((13, 6)) 

fn = rng.random_sample(13) 

gg = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

 

with self.assertRaises(RuntimeError) as context: 

gg.removePoint(-1) 

self.assertIn("doesn't exist", 

context.exception.args[0]) 

 

with self.assertRaises(RuntimeError) as context: 

gg.removePoint(13) 

self.assertIn("doesn't exist", 

context.exception.args[0]) 

 

# test that it runs fine 

gg.removePoint(11) 

self.assertEqual(gg.getNPoints(), 12) 

 

def testRemovePoint(self): 

""" 

Test that, after a point is removed, getData works as you would expect 

""" 

rng = np.random.RandomState(99) 

data = rng.random_sample((15, 4)) 

fn = rng.random_sample(15) 

gp = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

self.assertEqual(gp.getNPoints(), 15) 

gp.removePoint(6) 

self.assertEqual(gp.getNPoints(), 14) 

indices = np.array([9, 5, 4, 11], dtype=np.int32) 

fn_out = np.zeros(4) 

pts_out = np.zeros((4, 4)) 

gp.getData(pts_out, fn_out, indices) 

for ii in range(len(indices)): 

if indices[ii] >= 6: 

target = indices[ii]+1 

else: 

target = indices[ii] 

for jj in range(4): 

self.assertAlmostEqual(pts_out[ii][jj], data[target][jj], 10) 

self.assertAlmostEqual(fn_out[ii], fn[target], 10) 

 

# now test it on a GaussianProcess on many functions 

fn = rng.random_sample((15, 5)) 

gp = afwMath.GaussianProcessD( 

data, fn, afwMath.SquaredExpCovariogramD()) 

self.assertEqual(gp.getNPoints(), 15) 

gp.removePoint(6) 

self.assertEqual(gp.getNPoints(), 14) 

indices = np.array([9, 5, 4, 11], dtype=np.int32) 

fn_out = np.zeros((4, 5)) 

pts_out = np.zeros((4, 4)) 

gp.getData(pts_out, fn_out, indices) 

for ii in range(len(indices)): 

if indices[ii] >= 6: 

target = indices[ii]+1 

else: 

target = indices[ii] 

for jj in range(4): 

self.assertAlmostEqual(pts_out[ii][jj], data[target][jj], 10) 

for jj in range(5): 

self.assertAlmostEqual(fn_out[ii][jj], fn[target][jj], 10) 

 

def testSubtractionInterpolation(self): 

""" 

This will test interpolate after subtracting points 

""" 

 

tol = 1.0e-3 

data = np.zeros((2000, 10), dtype=float) 

fn = np.zeros((2000, 4), dtype=float) 

mu = np.zeros((4), dtype=float) 

sig = np.zeros((4), dtype=float) 

mushld = np.zeros((4), dtype=float) 

vv = np.zeros((10), dtype=float) 

kk = 30 

 

with open(os.path.join(testPath, "data", "gp_subtraction_data.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

for j in range(10): 

data[i][j] = float(s[j]) 

for j in range(4): 

fn[i][j] = float(s[j+10]) 

 

xx = afwMath.SquaredExpCovariogramD() 

xx.setEllSquared(2.3) 

try: 

gg = afwMath.GaussianProcessD(data, fn, xx) 

except pex.Exception as e: 

print(e.what()) 

 

gg.setLambda(0.002) 

 

j = 1 

for i in range(1000): 

try: 

gg.removePoint(j) 

except pex.Exception as e: 

print(e.what()) 

 

j = j+1 

 

worstMuErr = -1.0 

worstSigErr = -1.0 

 

with open(os.path.join(testPath, "data", "gp_subtraction_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

for j in range(10): 

vv[j] = float(s[j]) 

for j in range(4): 

mushld[j] = float(s[j+10]) 

sigshld = float(s[14]) 

 

gg.interpolate(mu, sig, vv, kk) 

 

for j in range(4): 

muErr = (mu[j]-mushld[j])/mushld[j] 

sigErr = (sig[j]-sigshld)/sigshld 

if muErr < 0.0: 

muErr = -1.0 * muErr 

if sigErr < 0.0: 

sigErr = -1.0 * sigErr 

if (muErr > worstMuErr): 

worstMuErr = muErr 

if (sigErr > worstSigErr): 

worstSigErr = sigErr 

 

print("\nThe errors for subtraction interpolation\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

worstMuErr = -1.0 

worstSigErr = -1.0 

 

with open(os.path.join(testPath, "data", "gp_subtraction_selfinterpolate_solutions.sav"), "r") as f: 

ff = f.readlines() 

 

for i in range(len(ff)): 

s = ff[i].split() 

try: 

gg.selfInterpolate(mu, sig, i, kk) 

except pex.Exception as e: 

print(e.what()) 

 

for j in range(4): 

mushld[j] = float(s[j]) 

sigshld = float(s[4]) 

 

for j in range(4): 

muErr = (mu[j]-mushld[j])/mushld[j] 

if muErr < 0.0: 

muErr = -1.0 * muErr 

if muErr > worstMuErr: 

worstMuErr = muErr 

 

sigErr = (sig[j]-sigshld)/sigshld 

if sigErr < 0.0: 

sigErr = -1.0*sigErr 

if sigErr > worstSigErr: 

worstSigErr = sigErr 

 

print("\nThe errors for subtraction self interpolation\n") 

print("worst mu error ", worstMuErr) 

print("worst sig2 error ", worstSigErr) 

self.assertLess(worstMuErr, tol) 

self.assertLess(worstSigErr, tol) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()