def test_wrong_derivative(tol=1e-9):
    field = np.random.random([nx, ny])
    interp = Bicubic(field)
    with pytest.raises(ValueError):
        interp(1, 1, derivative=3)
    with pytest.raises(ValueError):
        interp(1, 1, derivative=-1)
def test_grid_derivatives(tol=1e-9):
    field = np.random.random([nx, ny])
    derx = np.random.random([nx, ny])
    dery = np.random.random([nx, ny])
    interp = Bicubic(field, derx, dery)
    for i in range(nx):
        for j in range(ny):
            assert abs(interp(i, j) - field[i, j]) < tol

    x, y = np.mgrid[:nx, :ny]

    interp_field, interp_derx, interp_dery = interp(x, y, derivative=1)
    assert np.allclose(interp_field, field)
    assert np.allclose(interp_derx, derx)
    assert np.allclose(interp_dery, dery)
def test_grid_values(tol=1e-9):
    field = np.random.random([nx, ny])
    interp = Bicubic(field)
    for i in range(nx):
        for j in range(ny):
            assert abs(interp(i, j) - field[i, j]) < tol

    x, y = np.mgrid[:nx, :ny]

    for der in [0, 1, 2]:
        if der == 0:
            interp_field = interp(x, y, derivative=der)
        elif der == 1:
            interp_field, _, _ = interp(x, y, derivative=der)
        else:
            interp_field, _, _, _, _, _ = interp(x, y, derivative=der)
        assert np.allclose(interp_field, field)
Exemple #4
0
# %% check bicubic interpolation against fourier interpolation

# %%
fig, ax = plt.subplots()
x, y = topography.positions()
ax.plot(x[:, 0], topography.heights()[:, 0], ".k", label="original")

skips = [4, 8, 16, 32, 64]
rms_err = []
max_err = []
for skip in skips:
    grid_slice = (slice(None, None, skip), slice(None, None, skip))

    interp = Bicubic(topography.heights()[grid_slice],
                     dx[grid_slice] * topography.pixel_size[0] * skip,
                     dy[grid_slice] * topography.pixel_size[1] * skip
                     )

    interp_field, interp_derx, interp_dery = interp(
        x / (topography.pixel_size[0] * skip),
        y / (topography.pixel_size[1] * skip), derivative=1)
    l, = ax.plot(x[grid_slice][:, 0], topography.heights()[grid_slice][:, 0],
                 "+")
    ax.plot(x[:, 0], interp_field[:, 0], color=l.get_color(),
            label=r"bicubic, $l_{{cor}} / \Delta_x={}$"
            .format(hc / (skip * topography.pixel_size[0])))

    rms_err.append(
        np.sqrt(np.mean((interp_field - topography.heights()) ** 2)))
    max_err.append(np.max(abs(interp_field - topography.heights())))
    ax.legend()
def test_bicubic_between(plot=False):
    # grid points based on which the interpolation is made
    nx = 32
    ny = 17
    sx = nx
    sy = ny

    x = np.arange(nx).reshape(-1, 1)
    y = np.arange(ny).reshape(1, -1)

    def fun(x, y):
        return np.sin(2 * np.pi * x / sx) * np.cos(2 * np.pi * y / sy)

    def dfun_dx(x, y):
        return 2 * np.pi / sx * np.cos(2 * np.pi * x / sx) * np.cos(
            2 * np.pi * y / sy)

    def dfun_dy(x, y):
        return -2 * np.pi / sy * np.sin(2 * np.pi * x / sx) * np.sin(
            2 * np.pi * y / sy)

    interp = Bicubic(fun(x, y), dfun_dx(x, y), dfun_dy(x, y))
    interp_field, interp_derx, interp_dery = interp(x * np.ones_like(y),
                                                    y * np.ones_like(x),
                                                    derivative=1)

    if plot:
        import matplotlib.pyplot as plt
        fig, (ax, axdx, axdy) = plt.subplots(3, 1)
        ax.plot(x, fun(x, y)[:, 0], "k+")
        ax.plot(x, interp_field[:, 0], "ko", mfc="none")
        axdx.plot(x, dfun_dx(x, y)[:, 0], "r+")
        axdx.plot(x, interp_derx[:, 0], "ro", mfc="none")
        axdy.plot(y.flat, dfun_dy(x, y)[0, :], "g+")
        axdy.plot(y.flat, interp_dery[0, :], "go", mfc="none")

    # interpolate between the points used for definition
    fac = 8
    x_fine = np.arange(fac * nx).reshape(-1, 1) / fac
    y_fine = np.arange(fac * ny).reshape(1, -1) / fac

    interp_field0 = interp(x_fine * np.ones_like(y_fine),
                           y_fine * np.ones_like(x_fine),
                           derivative=0)
    interp_field1, interp_derx1, interp_dery1 = interp(
        x_fine * np.ones_like(y_fine),
        y_fine * np.ones_like(x_fine),
        derivative=1)
    interp_field2, interp_derx2, interp_dery2, interp_derxx2, interp_deryy2, \
        interp_derxy2 = interp(
            x_fine * np.ones_like(y_fine), y_fine * np.ones_like(x_fine),
            derivative=2)
    if plot:
        ax.plot(x_fine, interp_field1[:, 0], 'k-')
        axdx.plot(x_fine.flat, interp_derx1[:, 0], 'r-')
        axdy.plot(y_fine.flat, interp_dery1[0, :], 'g-')
        fig.show()

    np.testing.assert_allclose(interp_field0, fun(x_fine, y_fine), atol=1e-2)
    np.testing.assert_allclose(interp_field1, fun(x_fine, y_fine), atol=1e-2)
    np.testing.assert_allclose(interp_derx1,
                               dfun_dx(x_fine, y_fine),
                               atol=1e-2)
    np.testing.assert_allclose(interp_dery1,
                               dfun_dy(x_fine, y_fine),
                               atol=1e-2)
    np.testing.assert_allclose(interp_field2, fun(x_fine, y_fine), atol=1e-2)
def test_wrong_grid(tol=1e-9):
    field = np.random.random([nx, ny])
    derx = np.random.random([nx, ny - 1])
    dery = np.random.random([nx, ny])
    with pytest.raises(ValueError):
        Bicubic(field, derx, dery)