-
Notifications
You must be signed in to change notification settings - Fork 63
Added scipy RBF interpolation #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| import multiprocessing as mp | ||
| import numpy as np | ||
| import scipy.optimize as op | ||
| from scipy.interpolate import Rbf as rbf | ||
| import datetime | ||
|
|
||
|
|
||
|
|
@@ -116,7 +117,7 @@ def cubetobox(x): | |
| str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + ' ...') | ||
|
|
||
| # sampling next batch of points | ||
| fit = rbf(points) | ||
| fit = rbf(*np.transpose(points), function='cubic') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New rbf() function requires another input format. The input list is transposed and then unpacked with *. Old: [ [ x1, x2, ... , xd, val ] , [ ... ] , ... ] Original function included cubic basis function, so now it's called manually in arguments. |
||
| points = np.append(points, np.zeros((batch, d+1)), axis=0) | ||
|
|
||
| for j in range(batch): | ||
|
|
@@ -168,53 +169,3 @@ def rseq(n, d): | |
| points = np.array([(0.5 + alpha*(i+1)) % 1 for i in range(n)]) | ||
|
|
||
| return points | ||
|
|
||
|
|
||
| def rbf(points): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Old rbf() function is completely removed. |
||
| """ | ||
| Build RBF-fit for given points (see Holmstrom, 2008 for details). | ||
|
|
||
| Parameters | ||
| ---------- | ||
| points : ndarray | ||
| Array of multi-d points with corresponding values [[x1, x2, .., xd, val], ...]. | ||
|
|
||
| Returns | ||
| ------- | ||
| fit : callable | ||
| Function that returns the value of the RBF-fit at a given point. | ||
| """ | ||
| n = len(points) | ||
| d = len(points[0])-1 | ||
|
|
||
| def phi(r): | ||
| return r*r*r | ||
|
|
||
| Phi = [[phi(np.linalg.norm(np.subtract(points[i, 0:-1], points[j, 0:-1]))) for j in range(n)] for i in range(n)] | ||
|
|
||
| P = np.ones((n, d+1)) | ||
| P[:, 0:-1] = points[:, 0:-1] | ||
|
|
||
| F = points[:, -1] | ||
|
|
||
| M = np.zeros((n+d+1, n+d+1)) | ||
| M[0:n, 0:n] = Phi | ||
| M[0:n, n:n+d+1] = P | ||
| M[n:n+d+1, 0:n] = np.transpose(P) | ||
|
|
||
| v = np.zeros(n+d+1) | ||
| v[0:n] = F | ||
|
|
||
| try: | ||
| sol = np.linalg.solve(M, v) | ||
| except: | ||
| # might help with singular matrices | ||
| print('Singular matrix occurred during RBF-fit construction. RBF-fit might be inaccurate!') | ||
| sol = np.linalg.lstsq(M, v)[0] | ||
|
|
||
| lam, b, a = sol[0:n], sol[n:n+d], sol[n+d] | ||
|
|
||
| def fit(x): | ||
| return sum(lam[i]*phi(np.linalg.norm(np.subtract(x, points[i, 0:-1]))) for i in range(n)) + np.dot(b, x) + a | ||
|
|
||
| return fit | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import Rbf() function from scipy as rbf().