def test_api_and_all_types_and_1d_with_deriv(): # 1d example, deriv test x = np.linspace(0, 10, 50) z = np.sin(x) xx = np.linspace(0, 10, 100) for name in rbf.rbf_dct.keys(): print(name) cases = [ (True, dict(rbf=name)), (True, dict(p='mean')), (False, dict(p='scipy')), # not accurate enough, only API test (True, dict(rbf=name, r=1e-11)), (True, dict(rbf=name, p=rbf.estimate_p(x[:, None]))), (True, dict(rbf=name, p=rbf.estimate_p(x[:, None]), r=1e-11)), ] for go, kwds in cases: rbfi = rbf.Rbf(x[:, None], z, **kwds) if go: assert np.allclose(rbfi(xx[:, None]), np.sin(xx), rtol=0, atol=1e-4) assert np.allclose(rbfi(xx[:, None], der=1)[:, 0], np.cos(xx), rtol=0, atol=1e-3)
def go(nn, ax): x = np.linspace(0, 10, nn) xi = np.linspace(x[0], x[-1], 300) y = np.sin(x) + np.random.rand(len(x)) ax.plot(x, y, 'o', alpha=0.3) for name in ['gauss', 'multi']: f = rbf.Rbf(x[:, None], y, rbf=name) ax.plot(xi, f(xi[:, None]), label=name)
def test_interpol_high_dim(): # 100 points in 40-dim space # # Test only API. Interpolating random points makes no sense, of course. # However, on the center points (= data points = training points), # interpolation must be "perfect". X = rand(10, 40) z = rand(10) rbfi = rbf.Rbf(X, z) assert np.abs(z - rbfi(X)).max() < 1e-13
def test_2d(): # 2d example, y = f(x1,x2) = sin(x1) + cos(x2) x1 = np.linspace(-1, 1, 20) X1, X2 = np.meshgrid(x1, x1) X1 = X1.T X2 = X2.T z = (np.sin(X1) + np.cos(X2)).flatten() X = np.array([X1.flatten(), X2.flatten()]).T print(X.shape, z.shape) rbfi = rbf.Rbf(X, z, p=1.5) # test fit at 300 random points within [-1,1]^2 Xr = -1.0 + 2 * np.random.rand(300, 2) zr = np.sin(Xr[:, 0]) + np.cos(Xr[:, 1]) err = np.abs(rbfi(Xr) - zr).max() print(err) assert err < 1e-6 # Big errors occur only at the domain boundary: -1, 1, errs at the points # should be smaller err = np.abs(z - rbfi(X)).max() print(err) assert err < 1e-6
y = XY[:, 1] return (x**2 + y**2) if __name__ == '__main__': # Some nice 2D examples fu = MexicanHat([-10, 20], [-10, 15], 20, 20, 'rand') ## fu = UpDown([-2,2], [-2,2], 20, 20, 'grid') ## fu = SinExp([-1,2.5], [-2,2], 40, 30, 'rand') ## fu = Square([-1,1], [-1,1], 20, 20, 'grid') Z = fu(fu.XY) rbfi = rbf.Rbf(fu.XY, Z, rbf='inv_multi', verbose=True) dati = SurfaceData(fu.xlim, fu.ylim, fu.nx * 2, fu.ny * 2, 'grid') ZI_func = fu(dati.XY) ZI_rbf = rbfi(dati.XY) ZG_func = ZI_func.reshape((dati.nx, dati.ny)) ZG_rbf = ZI_rbf.reshape((dati.nx, dati.ny)) zlim = [ZI_func.min(), ZI_func.max()] fig, ax = mpl.fig_ax3d(clean=True) ax.scatter(fu.XY[:, 0], fu.XY[:, 1], Z, color='b', label='f(x,y) samples') dif = np.abs(ZI_func - ZI_rbf).reshape((dati.nx, dati.ny)) if not shiny: ax.plot_wireframe(dati.XG,
rbf_kwds = dict(rbf='inv_multi', r=None) fig, axs = plt.subplots(2, 1) x = np.linspace(0, 10, 100) rnd = np.random.RandomState(seed=123) y = np.sin(x) + rnd.rand(len(x)) points = x[:,None] values = y xi = np.linspace(x[0], x[-1], len(x)*4)[:,None] fe = rbf.FitError(points, values, rbf_kwds=rbf_kwds) ax = axs[0] ax.plot(x, y, 'o', alpha=0.3) rbfi = rbf.Rbf(points, values, **rbf_kwds) p_default = rbfi.get_params()[0] ax.plot(xi, rbfi(xi), 'r', label='$p$ default') rbfi = rbf.fit_opt(points, values, method='de', what='p', opt_kwds=dict(bounds=[(0.1, 10)], maxiter=10), rbf_kwds=rbf_kwds, cv_kwds=None) p_opt = rbfi.get_params()[0] ax.plot(xi, rbfi(xi), 'g', label='$p$ opt') rbfi = rbf.Rbf(points, values, p=10, **rbf_kwds) p_cv = rbfi.get_params()[0] ax.plot(xi, rbfi(xi), 'y', label='$p$ big') rbfi = rbf.Rbf(points, values, p=rbf.estimate_p(points, 'scipy'),
def test_func_api(): X = rand(10, 3) z = rand(10) r1 = rbf.Rbf(X, z, rbf='multi') r2 = rbf.Rbf(X, z, rbf=rbf.rbf_dct['multi']) assert (r1(X) == r2(X)).all()
def test_p_api(): X = rand(10, 3) z = rand(10) for name in ['scipy', 'mean']: f = rbf.Rbf(X, z, p=name) assert f.p == rbf.estimate_p(X, method=name)