-
Notifications
You must be signed in to change notification settings - Fork 240
/
rbf_interpolator.py
432 lines (391 loc) · 18.2 KB
/
rbf_interpolator.py
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
"""Define the RBFInterpolator class."""
import numpy as np
from openmdao.surrogate_models.nn_interpolators.nn_base import NNBase
from six.moves import range
from scipy.sparse import csc_matrix
from scipy.sparse.linalg import spsolve
class RBFInterpolator(NNBase):
"""
Compactly Supported Radial Basis Function.
Attributes
----------
rbf_family : int
Specifies the order of the radial basis function to be used.
<-2> uses an 11th order, <-1> uses a 9th order, and any value from <0> to <4> uses an order
equal to <floor((dimensions-1)/2) + (3*comp) +1>.
N : int
The number of neighbors used for interpolation.
weights : ndarray
Weights for each interpolation point.
"""
def _find_R(self, npp, T, neighbor_idx):
"""
Evaluate RBF polynomial.
Parameters
----------
npp : int
Number of prediction points
T : int
Radial distance
neighbor_idx : ndarray
Int array of neighbor indices.
Returns
-------
ndarray
Evaluation of RBF polynomial.
"""
R = np.zeros((npp, self._ntpts), dtype="float")
# Choose type of CRBF R matrix
if self.rbf_family == -1:
# Comp #1 - a
Cf = np.power((1. - T), 5)
cb_poly = [5., 72., 48., 40., 8.]
# Cb = (8. + (40. * T) + (48. * T * T) +
# (72. * T * T * T) + (5. * T * T * T * T))
elif self.rbf_family == -2:
# Comp #2
Cf = np.power((1. - T), 6)
cb_poly = [5., 30., 72., 82., 36., 6.]
# Cb = (6. + (36. * T) + (82. * T * T) + (72. * T * T * T) +
# (30. * T * T * T * T) + (5. * T * T * T * T * T))
elif self.rbf_family == -3:
# Comp #3
# Cf = np.ones_like(T)
# Cb = np.sqrt((T * T) + 1.)
# Re-arranged to fit polyval scheme below.
Cf = np.sqrt(np.square(T) + 1.)
cb_poly = [1.]
else:
# The above options did not specify a dimensional requirement
# in the paper found but the rest are said to only be guaranteed
# as positive definite iff the dimensional requirements are.
# Because of this, the user can select 0 through 4 to adjust to a
# level of order trying to be attained.
dims = self._indep_dims + 1
if dims <= 2:
if self.rbf_family == 0:
# This starts the dk comps, here d=1, k=0
Cf = 1. - T
cb_poly = [1.]
# Cb = T ** 0.
elif self.rbf_family == 1:
Cf = np.power(1. - T, 3.) / 12.
cb_poly = [3., 1.]
# Cb = 1. + (3. * T)
elif self.rbf_family == 2:
Cf = np.power(1. - T, 5.) / 840.
cb_poly = [24., 15., 3.]
# Cb = 3. + (15. * T) + (24. * T * T)
elif self.rbf_family == 3:
Cf = np.power(1. - T, 7.) / 151200.
cb_poly = [315., 285., 105., 15.]
# Cb = 15. + (105. * T) + (285. * T * T) + (315. * T * T * T)
elif self.rbf_family == 4:
Cf = np.power(1. - T, 9.) / 51891840.
cb_poly = [5760., 6795., 3555., 945., 105.]
# Cb = (105. + (945. * T) + (3555. * T * T) + (6795. * T * T * T) +
# (5760. * T * T * T * T))
elif dims <= 4:
if self.rbf_family == 0:
Cf = (1. - T)
# Cb = (1. - T)
cb_poly = [-1., 1.]
elif self.rbf_family == 1:
Cf = np.power(1. - T, 4) / 20.
# Cb = 1. + (4. * T)
cb_poly = [4., 1.]
elif self.rbf_family == 2:
Cf = np.power(1. - T, 6.) / 1680.
# Cb = 3. + (18. * T) + (35. * T * T)
cb_poly = [35., 18., 3.]
elif self.rbf_family == 3:
Cf = np.power(1. - T, 8.) / 332640.
cb_poly = [480., 375., 120., 15.]
# Cb = 15. + (120. * T) + (375. * T * T) + (480. * T * T * T)
elif self.rbf_family == 4:
Cf = np.power(1. - T, 10.) / 121080960.
cb_poly = [9009., 9450., 4410., 1050., 105.]
# Cb = (105. + (1050. * T) + (4410. * T * T) + (9450. * T * T * T) +
# (9009. * T * T * T * T))
elif dims <= 6:
if self.rbf_family == 0:
Cf = np.power(1. - T, 2.)
cb_poly = [-1., 1.]
# Cb = (1. - T)
elif self.rbf_family == 1:
Cf = np.power(1. - T, 5.) / 30.
cb_poly = [5., 1.]
# Cb = 1. + (5. * T)
elif self.rbf_family == 2:
Cf = np.power(1. - T, 7.) / 3024.
cb_poly = [48., 21., 3.]
# Cb = 3. + (21. * T) + (48. * T * T)
elif self.rbf_family == 3:
Cf = np.power(1. - T, 9.) / 665280.
cb_poly = [693., 477., 135., 15.]
# Cb = 15. + (135. * T) + (477. * T * T) + (693. * T * T * T)
elif self.rbf_family == 4:
Cf = np.power(1. - T, 11.) / 259459200.
cb_poly = [13440., 12705., 5355., 1155., 105.]
# Cb = (105. + (1155. * T) + (5355. * T * T) + (12705. * T * T * T) +
# (13440. * T * T * T * T))
else:
# Although not listed, this is ideally for 8 dim or less
if self.rbf_family == 0:
Cf = np.power(1. - T, 2.)
cb_poly = [1., -2., 1.]
# Cb = (1. - T) * (1. - T)
elif self.rbf_family == 1:
Cf = np.power(1. - T, 6.) / 42.
cb_poly = [6., 1.]
# Cb = 1. + (6. * T)
elif self.rbf_family == 2:
Cf = np.power(1. - T, 8.) / 5040.
cb_poly = [63., 24., 3.]
# Cb = 3. + (24. * T) + (63. * T * T)
elif self.rbf_family == 3:
Cf = np.power(1. - T, 10.) / 1235520.
cb_poly = [960., 591., 150., 15.]
# Cb = 15. + (150. * T) + (591. * T * T) + (960. * T * T * T)
elif self.rbf_family == 4:
Cf = np.power(1. - T, 12.) / 518918400.
cb_poly = [19305., 16620., 6390., 1260., 105.]
# Cb = (105. + (1260. * T) + (6390. * T * T) + (16620. * T * T * T) +
# (19305. * T * T * T * T))
Cb = np.polyval(cb_poly, T)
for i in range(npp):
R[i, neighbor_idx[i, :-1]] = Cf[i, :] * Cb[i, :]
return R
def _find_dR(self, prediction_points, neighbor_idx, neighbor_dists):
"""
Find dR.
Parameters
----------
prediction_points : ndarray
Array of prediction locations.
neighbor_idx : ndarray
Nearest neighbor indices for prediction points.
neighbor_dists : ndarray
Distances from prediction points to neighbors.
Returns
-------
ndarray
Gradient value at prediction points.
"""
T = (neighbor_dists[:, :-1] / neighbor_dists[:, -1:])
# Solve for the gradient analytically
# The first quantity needed is dRp/dt
if self.rbf_family == -1:
frnt = np.power((1. - T), 4)
dRp_poly = [-45., -556., -120., -144., 0.]
# dRp = frnt * ((-5. * (8. + (40. * T) + (48. * T * T) +
# (72. * T * T * T) + (5. * T * T * T * T))) +
# ((1. - T) * (40. + (96. * T) +
# (216. * T * T) + (20. * T * T * T))))
elif self.rbf_family == -2:
frnt = np.power((1. - T), 5.)
dRp_poly = [-55., -275., -528., -440., -88., 0.]
# dRp = frnt * ((-6. * (6. + (36. * T) +
# (82. * T * T) + (72. * T * T * T) +
# (30. * T * T * T * T) + (5. * T * T * T * T * T))) +
# ((1. - T)) * (36. + (164. * T) +
# (216. * T * T) + (120. * T * T * T) +
# (25. * T * T * T * T)))
elif self.rbf_family == -3:
frnt = T / np.sqrt((T * T) * 1.)
dRp_poly = [1.]
else:
dims = self._indep_dims + 1
# Start dim dependent comps, review first occurrence for more info
if dims <= 2:
if self.rbf_family == 0:
# This starts the dk comps(Wendland Functs), here d=1, k=0
frnt = 1.
dRp_poly = [-1.]
# dRp = -1.
elif self.rbf_family == 1:
frnt = 1.
dRp_poly = [1., -2., 1., 0.]
# dRp = -T * (1. - T) * (1. - T)
elif self.rbf_family == 2:
frnt = np.power(1. - T, 4.) / -20.
dRp_poly = [4., 1., 0.]
# dRp = frnt * (T + (4. * T * T))
elif self.rbf_family == 3:
frnt = np.power(1. - T, 6.) / -1680.
dRp_poly = [35., 18., 3., 0.]
# dRp = frnt * ((3. * T) + (18. * T * T) + (35. * T * T * T))
elif self.rbf_family == 4:
frnt = np.power(1. - T, 8.) / -22176.
dRp_poly = [32., 25., 8., 1., 0.]
# dRp = frnt * (T + (8. * T * T) + (25. * T * T * T) + (32. * T * T * T * T))
elif dims <= 4:
if self.rbf_family == 0:
frnt = 1.
dRp_poly = [2., -2.]
# dRp = -2. * (1 - T)
elif self.rbf_family == 1:
frnt = 1.
dRp_poly = [1., -3., 3., -1., 0.]
# dRp = -T * (1. - T) * (1. - T) * (1. - T)
elif self.rbf_family == 2:
frnt = np.power(1. - T, 5.) / -30.
dRp_poly = [5., 1., 0.]
# dRp = frnt * (T + (5. * T * T))
elif self.rbf_family == 3:
frnt = np.power(1. - T, 7.) / -1008.
dRp_poly = [16., 7., 1., 0.]
# dRp = frnt * (T + (7. * T * T) + (16. * T * T * T))
elif self.rbf_family == 4:
frnt = np.power(1. - T, 9.) / -221760.
dRp_poly = [231., 159., 45., 5., 0.]
# dRp = frnt * ((5. * T) + (45. * T * T) + (159. * T * T * T)
# + (231. * T * T * T * T))
elif dims <= 6:
if self.rbf_family == 0:
frnt = 1.
dRp_poly = [-3., 6., -3.]
# dRp = -3. * (1. - T) * (1. - T)
elif self.rbf_family == 1:
frnt = 1.
dRp_poly = [-1., 4., -6., 4., -1., 0.]
# dRp = -T * ((1. - T) ** 4)
elif self.rbf_family == 2:
frnt = np.power(1. - T, 6.) / -42.
dRp_poly = [6., 1., 0.]
# dRp = frnt * (T + (6. * T * T))
elif self.rbf_family == 3:
frnt = np.power(1. - T, 8) / -1680.
dRp_poly = [21., 8., 1., 0.]
# dRp = frnt * (T + (8. * T * T) + (21. * T * T * T))
elif self.rbf_family == 4:
frnt = np.power(1. - T, 10.) / -411840.
dRp_poly = [320., 197., 50., 5., 0.]
# dRp = frnt * ((5. * T) + (50. * T * T) + (197. * T * T * T)
# + (320. * T * T * T * T))
else:
# Although not listed, this is ideally for 8 dim or less
if self.rbf_family == 0:
frnt = 1.
dRp_poly = [4., -12., 12., -4.]
# dRp = -4. * (1. - T) * (1. - T) * (1. - T)
elif self.rbf_family == 1:
frnt = 1.
dRp_poly = [1., -5., 10., -10., 5., -1., 0.]
# dRp = -T * ((1. - T) ** 5)
elif self.rbf_family == 2:
frnt = np.power(1. - T, 7.) / -56.
dRp_poly = [7., 1., 0.]
# dRp = frnt * (T + (7. * T * T))
elif self.rbf_family == 3:
frnt = np.power(1. - T, 9.) / -7920.
dRp_poly = [80., 27., 3., 0.]
# dRp = frnt * ((3. * T) + (27. * T * T) + (80. * T * T * T))
elif self.rbf_family == 4:
frnt = np.power(1. - T, 11.) / -720720.
dRp_poly = [429., 239., 55., 5., 0.]
# dRp = frnt * ((5. * T) + (55. * T * T) + (239. * T * T * T)
# + (429. * T * T * T * T))
dRp = frnt * np.polyval(dRp_poly, T)
# dt/dx becomes unstable at the training points, so perturb T slightly.
zero_idx = np.where(T == 0.0)
T[zero_idx] += 1.0e-11
# Now need dt/dx
xpi = np.subtract(prediction_points, self._tp[neighbor_idx[:, :-1], :])
xpm = prediction_points - self._tp[neighbor_idx[:, -1:], :]
dtx = (xpi - (np.square(T) * xpm)) / (np.square(neighbor_dists[:, -1:, :]) * T)
# The gradient then is the summation across neighs of w*df/dt*dt/dx
grad = np.einsum('ijk,ijk,ijl...->ilk...', dRp,
dtx, self.weights[neighbor_idx[:, :-1]])
return grad.reshape((prediction_points.shape[0], self._dep_dims, self._indep_dims))
def __init__(self, training_points, training_values, num_leaves=2, num_neighbors=5,
rbf_family=2):
"""
Initialize all attributes.
Parameters
----------
training_points : ndarray
ndarray of shape (num_points x independent dims) containing training input locations.
training_values : ndarray
ndarray of shape (num_points x dependent dims) containing training output values.
num_leaves : int
How many leaves the tree should have.
num_neighbors : int
The number of neighbors to use for interpolation.
rbf_family : int
Specifies the order of the radial basis function to be used.
<-2> uses an 11th order, <-1> uses a 9th order, and any value from <0> to <4> uses an
order equal to <floor((dimensions-1)/2) + (3*comp) +1>.
"""
super(RBFInterpolator, self).__init__(training_points, training_values, num_leaves)
if self._ntpts < num_neighbors:
raise ValueError('RBFInterpolator only given {0} training points, '
'but requested num_neighbors={1}.'.format(self._ntpts, num_neighbors))
# rbf_family is an arbitrary value that picks a function to use
self.rbf_family = rbf_family
# For weights, first find the training points radial neighbors
tdist, tloc = self._KData.query(self._tp, num_neighbors)
Tt = tdist[:, :-1] / tdist[:, -1:]
# Next determine weight matrix
Rt = self._find_R(self._ntpts, Tt, tloc)
weights = (spsolve(csc_matrix(Rt), self._tv))[..., np.newaxis]
self.N = num_neighbors
self.weights = weights
def __call__(self, prediction_points):
"""
Interpolate at the requested points.
Parameters
----------
prediction_points : ndarray
Points at which interpolation is done.
Returns
-------
predz
Need a type for predz.
"""
if len(prediction_points.shape) == 1:
# Reshape vector to n x 1 array
prediction_points.shape = (1, prediction_points.shape[0])
normalized_pts = (prediction_points - self._tpm) / self._tpr
nppts = normalized_pts.shape[0]
# Setup prediction points and find their radial neighbors
ndist, nloc = self._KData.query(normalized_pts, self.N)
# Check if complex step is being run
if np.any(np.abs(normalized_pts[0, :].imag)) > 0:
dimdiff = np.subtract(normalized_pts.reshape((nppts, 1, self._indep_dims)),
self._tp[nloc, :])
# KD Tree ignores imaginary part, muse redo ndist if complex
ndist = np.sqrt(np.sum((dimdiff * dimdiff), axis=2))
# Take farthest distance of each point
Tp = ndist[:, :-1] / ndist[:, -1:]
Rp = self._find_R(nppts, Tp, nloc)
predz = ((np.dot(Rp, self.weights[..., 0]) * self._tvr) +
self._tvm).reshape(nppts, self._dep_dims)
self._pt_cache = (normalized_pts, ndist, nloc)
return predz
def gradient(self, prediction_points):
"""
Find the gradient at each location of a set of supplied predicted points.
Parameters
----------
prediction_points : ndarray
Points at which interpolation is done.
Returns
-------
ndarray
Gradient value at prediction points.
"""
if len(prediction_points.shape) == 1:
# Reshape vector to n x 1 array
prediction_points.shape = (1, prediction_points.shape[0])
normalized_pts = (prediction_points - self._tpm) / self._tpr
# Setup prediction points and find their radial neighbors
if self._pt_cache is not None and \
np.allclose(self._pt_cache[0], normalized_pts):
pdist, ploc = self._pt_cache[1:]
else:
pdist, ploc = self._KData.query(normalized_pts, self.N)
# Find Gradient
grad = self._find_dR(normalized_pts[:, np.newaxis, :], ploc,
pdist[:, :, np.newaxis]) * (self._tvr[..., np.newaxis] / self._tpr)
return grad