Example #1
0
def test_find_natural_neighbors():
    r"""Test find natural neighbors function."""
    x = list(range(0, 20, 4))
    y = list(range(0, 20, 4))
    gx, gy = np.meshgrid(x, y)
    pts = np.vstack([gx.ravel(), gy.ravel()]).T
    tri = Delaunay(pts)

    test_points = np.array([[2, 2], [5, 10], [12, 13.4], [12, 8], [20, 20]])

    neighbors, tri_info = find_natural_neighbors(tri, test_points)

    neighbors_truth = [[0, 1], [24, 25], [16, 17, 30, 31],
                       [18, 19, 20, 21, 22, 23, 26, 27], []]

    for i, true_neighbor in enumerate(neighbors_truth):
        assert_array_almost_equal(true_neighbor, neighbors[i])

    cc_truth = np.array([(2.0, 2.0), (2.0, 2.0), (14.0, 2.0), (14.0, 2.0),
                         (6.0, 2.0), (6.0, 2.0), (10.0, 2.0), (10.0, 2.0),
                         (2.0, 14.0), (2.0, 14.0), (6.0, 6.0), (6.0, 6.0),
                         (2.0, 6.0), (2.0, 6.0), (2.0, 10.0), (2.0, 10.0),
                         (14.0, 14.0), (14.0, 14.0), (10.0, 6.0), (10.0, 6.0),
                         (14.0, 6.0), (14.0, 6.0), (14.0, 10.0), (14.0, 10.0),
                         (6.0, 10.0), (6.0, 10.0), (10.0, 10.0), (10.0, 10.0),
                         (6.0, 14.0), (6.0, 14.0), (10.0, 14.0), (10.0, 14.0)])

    r_truth = np.empty((32, ))
    r_truth.fill(2.8284271247461916)

    for key in tri_info:
        assert_almost_equal(cc_truth[key], tri_info[key]['cc'])
        assert_almost_equal(r_truth[key], tri_info[key]['r'])
Example #2
0
def natural_neighbor(xp, yp, variable, grid_x, grid_y):
    r"""Generate a natural neighbor interpolation of the given
    points to the given grid using the Liang and Hale (2010)
    approach.

    Liang, Luming, and Dave Hale. "A stable and fast implementation
    of natural neighbor interpolation." (2010).

    Parameters
    ----------
    xp: (N, ) ndarray
        x-coordinates of observations
    yp: (N, ) ndarray
        y-coordinates of observations
    variable: (N, ) ndarray
        observation values associated with (xp, yp) pairs.
        IE, variable[i] is a unique observation at (xp[i], yp[i])
    grid_x: (M, 2) ndarray
        Meshgrid associated with x dimension
    grid_y: (M, 2) ndarray
        Meshgrid associated with y dimension

    Returns
    -------
    img: (M, N) ndarray
        Interpolated values on a 2-dimensional grid
    """

    tri = Delaunay(list(zip(xp, yp)))

    grid_points = points.generate_grid_coords(grid_x, grid_y)

    members, triangle_info = triangles.find_natural_neighbors(tri, grid_points)

    img = np.empty(shape=(grid_points.shape[0]), dtype=variable.dtype)
    img.fill(np.nan)

    for ind, (grid, neighbors) in enumerate(members.items()):

        if len(neighbors) > 0:

            img[ind] = nn_point(xp, yp, variable, grid_points[grid], tri,
                                neighbors, triangle_info)

    img = img.reshape(grid_x.shape)
    return img
Example #3
0
def test_nn_point(test_data):
    r"""Test find natural neighbors for a point interpolation function."""
    xp, yp, z = test_data

    tri = Delaunay(list(zip(xp, yp)))

    sim_gridx = [30]
    sim_gridy = [30]

    members, tri_info = find_natural_neighbors(tri,
                                               list(zip(sim_gridx, sim_gridy)))

    val = nn_point(xp, yp, z, [sim_gridx[0], sim_gridy[0]],
                   tri, members[0], tri_info)

    truth = 1.009

    assert_almost_equal(truth, val, 3)
def test_nn_point(test_data):
    r"""Test find natural neighbors for a point interpolation function."""
    xp, yp, z = test_data

    tri = Delaunay(list(zip(xp, yp)))

    sim_gridx = [30]
    sim_gridy = [30]

    members, tri_info = find_natural_neighbors(tri,
                                               list(zip(sim_gridx, sim_gridy)))

    val = nn_point(xp, yp, z, [sim_gridx[0], sim_gridy[0]], tri, members[0],
                   tri_info)

    truth = 1.009

    assert_almost_equal(truth, val, 3)
Example #5
0
def test_find_natural_neighbors():
    r"""Tests find natural neighbors function"""

    x = list(range(0, 20, 4))
    y = list(range(0, 20, 4))
    gx, gy = np.meshgrid(x, y)
    pts = np.vstack([gx.ravel(), gy.ravel()]).T
    tri = Delaunay(pts)

    test_points = np.array([[2, 2], [5, 10], [12, 13.4], [12, 8], [20, 20]])

    neighbors, tri_info = find_natural_neighbors(tri, test_points)

    neighbors_truth = [[0, 1],
                       [24, 25],
                       [16, 17, 30, 31],
                       [18, 19, 20, 21, 22, 23, 26, 27],
                       []]

    for i in range(len(neighbors)):

        assert_array_almost_equal(neighbors_truth[i], neighbors[i])

    cc_truth = np.array([(2.0, 2.0), (2.0, 2.0), (14.0, 2.0),
                         (14.0, 2.0), (6.0, 2.0), (6.0, 2.0),
                         (10.0, 2.0), (10.0, 2.0), (2.0, 14.0),
                         (2.0, 14.0), (6.0, 6.0), (6.0, 6.0),
                         (2.0, 6.0), (2.0, 6.0), (2.0, 10.0),
                         (2.0, 10.0), (14.0, 14.0), (14.0, 14.0),
                         (10.0, 6.0), (10.0, 6.0), (14.0, 6.0),
                         (14.0, 6.0), (14.0, 10.0), (14.0, 10.0),
                         (6.0, 10.0), (6.0, 10.0), (10.0, 10.0),
                         (10.0, 10.0), (6.0, 14.0), (6.0, 14.0),
                         (10.0, 14.0), (10.0, 14.0)])

    r_truth = np.array([2.8284271247461916] * 32)

    for i in range(len(tri_info)):

        cc = tri_info[i]['cc']
        r = tri_info[i]['r']

        assert_almost_equal(cc_truth[i], cc)
        assert_almost_equal(r_truth[i], r)
Example #6
0
def test_find_natural_neighbors():
    r"""Test find natural neighbors function."""
    x = list(range(0, 20, 4))
    y = list(range(0, 20, 4))
    gx, gy = np.meshgrid(x, y)
    pts = np.vstack([gx.ravel(), gy.ravel()]).T
    tri = Delaunay(pts)

    test_points = np.array([[2, 2], [5, 10], [12, 13.4], [12, 8], [20, 20]])

    neighbors, tri_info = find_natural_neighbors(tri, test_points)

    neighbors_truth = [[0, 1],
                       [24, 25],
                       [16, 17, 30, 31],
                       [18, 19, 20, 21, 22, 23, 26, 27],
                       []]

    for i, true_neighbor in enumerate(neighbors_truth):
        assert_array_almost_equal(true_neighbor, neighbors[i])

    cc_truth = np.array([(2.0, 2.0), (2.0, 2.0), (14.0, 2.0),
                         (14.0, 2.0), (6.0, 2.0), (6.0, 2.0),
                         (10.0, 2.0), (10.0, 2.0), (2.0, 14.0),
                         (2.0, 14.0), (6.0, 6.0), (6.0, 6.0),
                         (2.0, 6.0), (2.0, 6.0), (2.0, 10.0),
                         (2.0, 10.0), (14.0, 14.0), (14.0, 14.0),
                         (10.0, 6.0), (10.0, 6.0), (14.0, 6.0),
                         (14.0, 6.0), (14.0, 10.0), (14.0, 10.0),
                         (6.0, 10.0), (6.0, 10.0), (10.0, 10.0),
                         (10.0, 10.0), (6.0, 14.0), (6.0, 14.0),
                         (10.0, 14.0), (10.0, 14.0)])

    r_truth = np.empty((32,))
    r_truth.fill(2.8284271247461916)

    for key in tri_info:
        assert_almost_equal(cc_truth[key], tri_info[key]['cc'])
        assert_almost_equal(r_truth[key], tri_info[key]['r'])
test_points = np.array([[2, 2], [5, 10], [12, 13.4], [12, 8], [20, 20]])

for i, (x, y) in enumerate(test_points):
    plt.plot(x, y, 'k.', markersize=6)
    plt.annotate('test ' + str(i), xy=(x, y))

###########################################
# Since finding natural neighbors already calculates circumcenters and circumradii, return
# that information for later use.
#
# The key of the neighbors dictionary refers to the test point index, and the list of integers
# are the triangles that are natural neighbors of that particular test point.
#
# Since point 4 is far away from the triangulation, it has no natural neighbors.
# Point 3 is at the confluence of several triangles so it has many natural neighbors.
neighbors, tri_info = find_natural_neighbors(tri, test_points)
print(neighbors)

###########################################
# We can then use the information in tri_info later.
#
# The dictionary key is the index of a particular triangle in the Delaunay triangulation data
# structure. 'cc' is that triangle's circumcenter, and 'r' is the radius of the circumcircle
# containing that triangle.
fig = plt.figure(figsize=(15, 10))
for i, inds in enumerate(tri.simplices):
    pts = tri.points[inds]
    x, y = np.vstack((pts, pts[0])).T
    plt.plot(x, y)
    plt.annotate(i, xy=(np.mean(x), np.mean(y)))
Example #8
0
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
delaunay_plot_2d(tri, ax=ax)

for i, zval in enumerate(zp):
    ax.annotate('{} F'.format(zval), xy=(pts[i, 0] + 2, pts[i, 1]))

sim_gridx = [30., 60.]
sim_gridy = [30., 60.]

ax.plot(sim_gridx, sim_gridy, '+', markersize=10)
ax.set_aspect('equal', 'datalim')
ax.set_title('Triangulation of observations and test grid cell '
             'natural neighbor interpolation values')

members, tri_info = triangles.find_natural_neighbors(tri, list(zip(sim_gridx, sim_gridy)))

val = nn_point(xp, yp, zp, (sim_gridx[0], sim_gridy[0]), tri, members[0], tri_info)
ax.annotate('grid 0: {:.3f}'.format(val), xy=(sim_gridx[0] + 2, sim_gridy[0]))

val = nn_point(xp, yp, zp, (sim_gridx[1], sim_gridy[1]), tri, members[1], tri_info)
ax.annotate('grid 1: {:.3f}'.format(val), xy=(sim_gridx[1] + 2, sim_gridy[1]))


###########################################
# Using the circumcenter and circumcircle radius information from
# :func:`metpy.gridding.triangles.find_natural_neighbors`, we can visually
# examine the results to see if they are correct.
def draw_circle(ax, x, y, r, m, label):
    th = np.linspace(0, 2 * np.pi, 100)
    nx = x + r * np.cos(th)
tri = Delaunay(pts)
delaunay_plot_2d(tri)

for i, zval in enumerate(zp):
    plt.annotate('{} F'.format(zval), xy=(pts[i, 0] + 2, pts[i, 1]))

sim_gridx = [30., 60.]
sim_gridy = [30., 60.]

plt.plot(sim_gridx, sim_gridy, '+', markersize=10)
plt.axes().set_aspect('equal', 'datalim')
plt.title('Triangulation of observations and test grid cell '
          'natural neighbor interpolation values')

members, tri_info = triangles.find_natural_neighbors(tri, list(zip(sim_gridx, sim_gridy)))

val = nn_point(xp, yp, zp, (sim_gridx[0], sim_gridy[0]), tri, members[0], tri_info)
plt.annotate('grid 0: {:.3f}'.format(val), xy=(sim_gridx[0] + 2, sim_gridy[0]))

val = nn_point(xp, yp, zp, (sim_gridx[1], sim_gridy[1]), tri, members[1], tri_info)
plt.annotate('grid 1: {:.3f}'.format(val), xy=(sim_gridx[1] + 2, sim_gridy[1]))


###########################################
# Using the circumcenter and circumcircle radius information from
# :func:`metpy.gridding.triangles.find_natural_neighbors`, we can visually
# examine the results to see if they are correct.
def draw_circle(x, y, r, m, label):
    nx = x + r * np.cos(np.deg2rad(list(range(360))))
    ny = y + r * np.sin(np.deg2rad(list(range(360))))
Example #10
0
test_points = np.array([[2, 2], [5, 10], [12, 13.4], [12, 8], [20, 20]])

for i, (x, y) in enumerate(test_points):
    plt.plot(x, y, 'k.', markersize=6)
    plt.annotate('test ' + str(i), xy=(x, y))

###########################################
# Since finding natural neighbors already calculates circumcenters and circumradii, return
# that information for later use.
#
# The key of the neighbors dictionary refers to the test point index, and the list of integers
# are the triangles that are natural neighbors of that particular test point.
#
# Since point 4 is far away from the triangulation, it has no natural neighbors.
# Point 3 is at the confluence of several triangles so it has many natural neighbors.
neighbors, tri_info = find_natural_neighbors(tri, test_points)
print(neighbors)

###########################################
# We can then use the information in tri_info later.
#
# The dictionary key is the index of a particular triangle in the Delaunay triangulation data
# structure. 'cc' is that triangle's circumcenter, and 'r' is the radius of the circumcircle
# containing that triangle.
fig = plt.figure(figsize=(15, 10))
for i, inds in enumerate(tri.simplices):
    pts = tri.points[inds]
    x, y = np.vstack((pts, pts[0])).T
    plt.plot(x, y)
    plt.annotate(i, xy=(np.mean(x), np.mean(y)))