Exemplo n.º 1
0
 def test_neighbors(self):
     for ha in [
             hexgrid.HexArray(shape=(3, 3)),
             hexgrid.HexArray(np.empty((3, 3), dtype=np.float64))
     ]:
         for n in range(9):
             if n == 4:
                 self.assertSequenceEqual(list(ha.neighbors(n)),
                                          [0, 1, 5, 7, 6, 3])
             else:
                 self.assertEqual(len(ha.neighbors(n)), 0)
Exemplo n.º 2
0
 def test_pos(self):
     a = _get_test_array()
     ha = hexgrid.HexArray(a)
     self.assertSequenceEqual(ha.pos(0), (0.25, 0.0))
     self.assertSequenceEqual(ha.pos(1), (1.25, 0.0))
     self.assertSequenceEqual(ha.pos(3), (-0.25, 1.0))
     self.assertSequenceEqual(ha.pos(4), (0.75, 1.0))
Exemplo n.º 3
0
    def test_critical_points(self):
        a = _get_test_array()
        ha = hexgrid.HexArray(a)
        c = ha.classify_critical_points()

        self.assertSequenceEqual(a.shape, c.shape)
        # c should actually come out the same as a
        np.testing.assert_array_equal(a.astype('i4'), c)
Exemplo n.º 4
0
 def test_convex(self):
     ha = hexgrid.HexArray(shape=(10, 10))
     pos = np.array([ha.pos(n) for n in range(ha.N)])
     x, y = pos.T
     r = np.sqrt((x - x.mean())**2 + (y - y.mean())**2)
     mask = r <= 3
     hr = hexgrid.HexArrayRegion(ha)
     for n in np.nonzero(mask.ravel())[0]:
         hr.add_point(n)
     self.assertTrue(hr.is_convex())
     mask += (abs(x) <= 1)
     for n in np.nonzero(mask.ravel())[0]:
         hr.add_point(n)
     self.assertFalse(hr.is_convex())
Exemplo n.º 5
0
    def test_interior_exterior_boundary(self):
        a = np.array([[0, 0, 0, 0, 0], [0, 0, 2, 2, 0], [0, 2, 1, 2, 0],
                      [0, 0, 2, 2, 0], [0, 0, 0, 0, 0]])
        ha = hexgrid.HexArray(shape=a.shape)
        hr = hexgrid.HexArrayRegion(ha)

        a = a.ravel()
        cpt = np.nonzero(a == 1)[0][0]
        hr.add_point(cpt)
        bd = set(np.nonzero(a == 2)[0])
        eb = hr.exterior_boundary()
        self.assertSetEqual(eb, bd)
        for pt in bd:
            hr.add_point(pt)
        ib = hr.interior_boundary()
        print(hr.members)
        self.assertSetEqual(ib, bd)
Exemplo n.º 6
0
 def test_find_convex_regions(self):
     # set up kelvin's cat's eyes flow on hexgrid
     nx = 100
     ha = hexgrid.HexArray(shape=(nx, nx))
     pos = np.array([ha.pos(n) for n in range(ha.N)])
     x, y = pos.T
     x = (x - 5) * 2 * np.pi / 80
     y = (y - nx / 2) * 2 * np.pi / 100
     # Kelvin's cats eyes flow
     a = 0.8
     psi = np.log(np.cosh(y) + a * np.cos(x)) - np.log(1 + a)
     cr = hexgrid.find_convex_regions(-psi.reshape(nx, nx))
     self.assertEqual(len(cr), 1)
     hr = cr[0]
     self.assertTrue(np.all(psi[list(hr.members)] < 0))
     # how do we know that the region was correctly identified?
     psimask = (psi <= 0) & (x > 0.05) & (x <= (2 * np.pi + 0.05))
     psiset = set(np.nonzero(psimask)[0])
     # now remove a point we know is problematic
     psiset.remove(5085)
     self.assertSetEqual(psiset, hr.members)
     # test minsize kwarg
     cr = hexgrid.find_convex_regions(-psi.reshape(nx, nx), minsize=1e6)
     self.assertEqual(len(cr), 0)
Exemplo n.º 7
0
 def setUp(self):
     self.ha = hexgrid.HexArray(_get_test_array())
Exemplo n.º 8
0
 def test_maxima(self):
     a = _get_test_array()
     ha = hexgrid.HexArray(a)
     maxima = ha.maxima()
     self.assertEqual(len(maxima), 1)
     self.assertEqual(maxima[0], 4)
Exemplo n.º 9
0
from matplotlib import pyplot as plt
from floater import hexgrid
import numpy as np
from scipy.spatial import qhull

ha = hexgrid.HexArray(shape=(10, 10))
pos = np.array([ha.pos(n) for n in range(ha.N)])
x, y = pos.T
nmid = ha.N / 2 + ha.Nx / 2
r = -np.sqrt((x - x[nmid])**2 + (y - y[nmid])**2)
mask = r <= 3
plt.scatter(*pos.T, c=mask, cmap='Greys')
hr = hexgrid.HexArrayRegion(ha)

bpos = np.array([ha.pos(n) for n in hr.interior_boundary()])
ebpos = np.array([ha.pos(n) for n in hr.exterior_boundary()])
plt.scatter(*bpos.T, c='m')
plt.scatter(*ebpos.T, c='c')

hull = qhull.ConvexHull(bpos)
hv = np.hstack([hull.vertices, hull.vertices[0]])
plt.plot(*hull.points[hv].T, color='k')


def pnpoly(vertx, verty, testx, testy):
    nvert = len(vertx)
    i = 0
    j = nvert - 1
    c = False
    while (i < nvert):
        #for (i = 0, j = nvert-1; i < nvert; j = i++) {