Exemplo n.º 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)

    # Need to check point indices rather than simplex indices
    neighbors_truth = [[(1, 5, 0), (5, 1, 6)],
                       [(11, 17, 16), (17, 11, 12)],
                       [(23, 19, 24), (19, 23, 18), (23, 17, 18), (17, 23, 22)],
                       [(7, 13, 12), (13, 7, 8), (9, 13, 8), (13, 9, 14),
                        (13, 19, 18), (19, 13, 14), (17, 13, 18), (13, 17, 12)],
                       np.zeros((0, 3), dtype=np.int32)]

    centers_truth = [[(2.0, 2.0), (2.0, 2.0)],
                     [(6.0, 10.0), (6.0, 10.0)],
                     [(14.0, 14.0), (14.0, 14.0), (10.0, 14.0), (10.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), (10.0, 10.0), (10.0, 10.0)],
                     np.zeros((0, 2), dtype=np.int32)]

    for i, true_neighbor in enumerate(neighbors_truth):
        assert set(true_neighbor) == {tuple(v) for v in tri.simplices[neighbors[i]]}
        assert set(centers_truth[i]) == {tuple(c) for c in tri_info[neighbors[i]]}
Exemplo n.º 2
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'])
Exemplo n.º 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 = natural_neighbor_point(xp, yp, z, [sim_gridx[0], sim_gridy[0]], tri,
                                 members[0], tri_info)

    truth = 1.009

    assert_almost_equal(truth, val, 3)
Exemplo n.º 4
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 = natural_neighbor_point(xp, yp, z, [sim_gridx[0], sim_gridy[0]],
                                 tri, members[0], tri_info)

    truth = 1.009

    assert_almost_equal(truth, val, 3)
Exemplo n.º 5
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'])
Exemplo n.º 6
0
test_points = np.array([[2, 2], [5, 10], [12, 13.4], [12, 8], [20, 20]])

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

###########################################
# Since finding natural neighbors already calculates circumcenters, 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, circumcenters = find_natural_neighbors(tri, test_points)
print(neighbors)

###########################################
# We can plot all of the triangles as well as the circles representing the circumcircles
#
fig, ax = plt.subplots(figsize=(15, 10))
for i, inds in enumerate(tri.simplices):
    pts = tri.points[inds]
    x, y = np.vstack((pts, pts[0])).T
    ax.plot(x, y)
    ax.annotate(i, xy=(np.mean(x), np.mean(y)))

# Using circumcenters and calculated circumradii, plot the circumcircles
for idx, cc in enumerate(circumcenters):
    ax.plot(cc[0], cc[1], 'k.', markersize=5)
Exemplo n.º 7
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 = geometry.find_natural_neighbors(
    tri, list(zip(sim_gridx, sim_gridy)))

val = natural_neighbor_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 = natural_neighbor_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.interpolate.geometry.find_natural_neighbors`, we can visually
# examine the results to see if they are correct.
def draw_circle(ax, x, y, r, m, label):
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
ax.ishold = lambda: True  # Work-around for Matplotlib 3.0.0 incompatibility
delaunay_plot_2d(tri, ax=ax)

for i, zval in enumerate(zp):
    ax.annotate(f'{zval} F', 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, circumcenters = geometry.find_natural_neighbors(
    tri, list(zip(sim_gridx, sim_gridy)))

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

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


###########################################
# Using the circumcenter and circumcircle radius information from
# :func:`metpy.interpolate.geometry.find_natural_neighbors`, we can visually
# examine the results to see if they are correct.
def draw_circle(ax, x, y, r, m, label):
Exemplo n.º 9
0
test_points = np.array([[2, 2], [5, 10], [12, 13.4], [12, 8], [20, 20]])

for i, (x, y) in enumerate(test_points):
    ax.plot(x, y, 'k.', markersize=6)
    ax.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, ax = plt.subplots(figsize=(15, 10))
for i, inds in enumerate(tri.simplices):
    pts = tri.points[inds]
    x, y = np.vstack((pts, pts[0])).T
    ax.plot(x, y)
    ax.annotate(i, xy=(np.mean(x), np.mean(y)))
Exemplo n.º 10
0
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
ax.ishold = lambda: True  # Work-around for Matplotlib 3.0.0 incompatibility
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 = geometry.find_natural_neighbors(tri, list(zip(sim_gridx, sim_gridy)))

val = natural_neighbor_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 = natural_neighbor_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.interpolate.geometry.find_natural_neighbors`, we can visually
# examine the results to see if they are correct.
def draw_circle(ax, x, y, r, m, label):
test_points = np.array([[2, 2], [5, 10], [12, 13.4], [12, 8], [20, 20]])

for i, (x, y) in enumerate(test_points):
    ax.plot(x, y, 'k.', markersize=6)
    ax.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, ax = plt.subplots(figsize=(15, 10))
for i, inds in enumerate(tri.simplices):
    pts = tri.points[inds]
    x, y = np.vstack((pts, pts[0])).T
    ax.plot(x, y)
    ax.annotate(i, xy=(np.mean(x), np.mean(y)))