def test_ne(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: A = Cell(rdggs, (O, 1, 2, 3)) AA = Cell(rdggs, (O, 1, 2, 3)) B = Cell(rdggs, (N, 2, 8, 7, 5)) self.assertFalse(A != AA) self.assertTrue(A != B)
def test_suid_from_index(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: # Test order='level'. # The suid with index 0 should be N. suid = (N,) get = Cell.suid_from_index(rdggs, 0, order='level') self.assertEqual(get, suid) # Should be correct on cell P1. suid = (P, 1) i = 25 get = Cell.suid_from_index(rdggs, i, order='level') self.assertEqual(get, suid) # Test order='post'. # The suid with index 0 should be N00...0. suid = [N] + [0 for i in range(rdggs.max_resolution)] suid = tuple(suid) get = Cell.suid_from_index(rdggs, 0, order='post') self.assertEqual(get, suid) # Should be correct on cell P1. suid = (P, 1) def num(k): return WGS84_123.num_cells(res_1=k, subcells=True) i = 2 * num(0) + 1 * num(1) + num(1) - 1 get = Cell.suid_from_index(rdggs, i, order='post') self.assertEqual(get, suid)
def test_Cell_init(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: # Should set the rdggs, suid, and resolution attributes correctly. suid = (S, 0, 1, 2, 6, 3, 1) resolution = 6 C = Cell(rdggs, suid) self.assertEqual(C.rdggs, rdggs) self.assertEqual(C.suid, suid) self.assertEqual(C.resolution, resolution) # Should not create invalid cells. suid = (P, rdggs.N_side**2) self.assertRaises(AssertionError, Cell, rdggs, suid) # Should create cell P1. expect = (P, 1) i = 25 get = Cell(rdggs, level_order_index=i).suid self.assertEqual(get, expect) expect = (P, 1) def num(k): return rdggs.num_cells(res_1=k, subcells=True) i = 2*num(0) + 1*num(1) + num(1) - 1 get = Cell(rdggs, post_order_index=i).suid self.assertEqual(get, expect)
def test_subcells(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: l = 6 C = Cell(rdggs, (S, 1, 0, 5, 7, 7, 3)) s = list(C.subcells(l + 1)) t = [Cell(rdggs, list(C.suid) + [i]) for i in range(9)] for i in range(9): self.assertTrue(s[i] == t[i])
def test_random_point(self): # Output should lie in the cell at least. for E in [WGS84_ASPHERE_RADIANS, WGS84_ELLIPSOID]: rdggs = RHEALPixDGGS(E) for plane in [True, False]: c = Cell(rdggs, [N, 8, 7]) p = c.random_point(plane=plane) self.assertTrue(c.contains(p, plane=plane)) #------------------------------------------------------------------------------
def test_gt(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: a = Cell(rdggs, (N, 7, 6, 8, 1)) b = Cell(rdggs, (O, 1, 2, 3)) c = Cell(rdggs, (O, 1, 2)) self.assertFalse(a > b) self.assertFalse(a > c) self.assertFalse(a > a) self.assertTrue(b > a) self.assertTrue(c > a)
def test_lt(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: a = Cell(rdggs, (N, 7, 6, 8, 1)) b = Cell(rdggs, (O, 1, 2, 3)) c = Cell(rdggs, (O, 1, 2)) self.assertTrue(a < b) self.assertTrue(a < c) self.assertFalse(a < a) self.assertFalse(b < a) self.assertFalse(c < a)
def test_ge(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: a = Cell(rdggs, (N, 7, 6, 8, 1)) b = Cell(rdggs, (O, 1, 2, 3)) bb = Cell(rdggs, (O, 1, 2, 3)) c = Cell(rdggs, (O, 1, 2)) self.assertTrue(b >= bb) self.assertTrue(bb >= a) self.assertFalse(a >= b) self.assertFalse(a >= c) self.assertTrue(b >= a) self.assertTrue(c >= a)
def test_rotate_entry(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: X = Cell(rdggs, (N,)) # Should return correct values. s = [] # Function values. t = [] # Correct values. for q in range(4): s.append ([X.rotate_entry(x, q) for x in range(9)]) t.append([0, 1, 2, 3, 4, 5, 6, 7, 8]) t.append([2, 5, 8, 1, 4, 7, 0, 3, 6]) t.append([8, 7, 6, 5, 4, 3, 2, 1, 0]) t.append([6, 3, 0, 7, 4, 1, 8, 5, 2]) for q in range(4): self.assertEqual(s[q], t[q])
def test_suid_rowcol(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: # Should work for resolution 0 cells. for suid in CELLS0: self.assertEqual(suid, Cell(rdggs, (suid,)).suid_rowcol()[0][0]) self.assertEqual(suid, Cell(rdggs, (suid,)).suid_rowcol()[1][0]) # Should work on an arbitrary cell. n = rdggs.N_side c = Cell(rdggs, (N, 1, 3, n**2 - 1, 2*n)) row, col = c.suid_rowcol() for i in range(1, c.resolution + 1): self.assertEqual(c.suid[i], n*row[i] + col[i])
def test_contains(self): for rdggs in [WGS84_003, WGS84_003_RADIANS]: # A cell should contain its nucleus, but not my test point p. # Assume that nucleus() and vertices() work. for suid in [[N, 3, 1], [P, 5, 7, 5, 1, 3], [S, 0]]: c = Cell(rdggs, suid) w = c.width() for plane in [True, False]: nucleus = c.nucleus(plane=plane) vertices = c.vertices(plane=plane) p = (max([v[0] for v in vertices]) + 1, vertices[3][1]) self.assertTrue(c.contains(nucleus, plane=plane)) self.assertFalse(c.contains(p, plane=plane))
def test_nucleus(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: # Nuclei of children should be in correct position # relative to parent cell in rHEALPix projection. a = Cell(rdggs, (S, 7, 4, 1, 2, 1)) # Arbitrary cell. w = a.width() (x, y) = a.ul_vertex() error = 1e-10 for (row, col) in product(list(range(3)), repeat=2): s = str(row*3 + col) # Child cell in (row, column) position relative to a: b = Cell(rdggs, list(a.suid) + [3*row + col]) get = b.nucleus() expect = (x + w/6 + (w/3)*col, y - w/6 - (w/3)*row) self.assertTrue(rel_err(get, expect) < error)
def test_index(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: # Empty cell should have index None empty = rdggs.cell() self.assertEqual(empty.index(order='level'), None) self.assertEqual(empty.index(order='post'), None) # Test order='level'. # A cell should have index one greater than its predecessor # at the same resolution. c = rdggs.cell((N, 2, 7, 4, 8)) b = c.predecessor() self.assertEqual(c.index(order='level'), b.index(order='level') + 1) # It should invert suid_from_index(). M = rdggs.num_cells(0, rdggs.max_resolution) k = 3616048 #randint(0, M - 1) get = Cell(rdggs, Cell.suid_from_index(rdggs, k, order='level')).\ index(order='level') self.assertEqual(get, k) # Test order='post'. # The cell N00...0 should have index 0. suid = [N] + [0]*rdggs.max_resolution a = Cell(rdggs, suid) self.assertEqual(a.index(order='post'), 0) # A cell should have index one greater than its last child. cc = list(c.subcells())[8] self.assertEqual(c.index(order='post'), cc.index(order='post') + 1) # It should invert suid_from_index(). k = 3616048 #randint(0, M - 1) get = Cell(rdggs, Cell.suid_from_index(rdggs, k, order='post')).\ index(order='post') self.assertEqual(get, k)
def test_subcell(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: C = Cell(rdggs, (S, 1, 2, 0)) D = Cell(rdggs, (S, 1, 2, 0, 5)) self.assertTrue(D.subcell(C)) self.assertFalse(C.subcell(D))
def test_neighbors(self): for rdggs in [WGS84_003, WGS84_003_RADIANS]: # Plane test. # Should work on resolution 1 cells with suids of the form s0 get = [] for s in CELLS0: nb = Cell(rdggs, [s] + [0]).neighbors() for d in ['up', 'left', 'down', 'right']: get.append(nb[d]) expect = [ (Q, 2), (R, 0), (N, 3), (N, 1), (N, 6), (R, 2), (O, 3), (O, 1), (N, 8), (O, 2), (P, 3), (P, 1), (N, 2), (P, 2), (Q, 3), (Q, 1), (N, 0), (Q, 2), (R, 3), (R, 1), (O, 6), (R, 8), (S, 3), (S, 1), ] expect = [Cell(rdggs, s) for s in expect] for i in range(len(expect)): self.assertEqual(get[i], expect[i]) # Ellipsoid test. # Quad. c = Cell(rdggs, (O, 0)) get = c.neighbors(plane=False) expect = dict() expect['north'] = Cell(rdggs, (N, 6)) expect['south'] = Cell(rdggs, (O, 3)) expect['west'] = Cell(rdggs, (R, 2)) expect['east'] = Cell(rdggs, (O, 1)) for k in list(get.keys()): self.assertEqual(get[k], expect[k]) # Cap. c = Cell(rdggs, (S, 4)) get = c.neighbors(plane=False) expect = dict() expect['north_0'] = Cell(rdggs, (S, 1)) expect['north_1'] = Cell(rdggs, (S, 5)) expect['north_2'] = Cell(rdggs, (S, 7)) expect['north_3'] = Cell(rdggs, (S, 3)) for k in list(get.keys()): self.assertEqual(get[k], expect[k]) # Dart. c = Cell(rdggs, (N, 6)) get = c.neighbors(plane=False) expect = dict() expect['west'] = Cell(rdggs, (N, 3)) expect['east'] = Cell(rdggs, (N, 7)) expect['south_west'] = Cell(rdggs, (R, 2)) expect['south_east'] = Cell(rdggs, (O, 0)) for k in list(get.keys()): self.assertEqual(get[k], expect[k]) # Skew quad. c = Cell(rdggs, (N, 3)) get = c.neighbors(plane=False) expect = dict() expect['north'] = Cell(rdggs, (N, 4)) expect['south'] = Cell(rdggs, (R, 1)) expect['west'] = Cell(rdggs, (N, 0)) expect['east'] = Cell(rdggs, (N, 6)) for k in list(get.keys()): self.assertEqual(get[k], expect[k])
def test_neighbor(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: # Plane test. c = Cell(rdggs, (N, 0)) get = c.neighbor('left') expect = Cell(rdggs, (O, 0)) self.assertEqual(get, expect) # Ellipsoid test. c = Cell(rdggs, (O, 0)) get = c.neighbor('east', plane=False) expect = Cell(rdggs, (O, 1)) self.assertEqual(get, expect) c = Cell(rdggs, (N, 6, 4)) get = c.neighbor('south_west', plane=False) expect = Cell(rdggs, (N, 6, 3)) self.assertEqual(get, expect) c = Cell(rdggs, (P, 2, 2)) get = c.neighbor('north', plane=False) expect = Cell(rdggs, (N, 8, 8)) self.assertEqual(get, expect)
def test_successor(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: A = Cell(rdggs, (N, 8, 1)) As0 = Cell(rdggs, (O,)) As1 = Cell(rdggs, (O, 0)) As2 = Cell(rdggs, (N, 8, 2)) As3 = Cell(rdggs, (N, 8, 2, 0)) self.assertTrue(A.successor(0) == As0) self.assertTrue(A.successor(1) == As1) self.assertTrue(A.successor(2) == As2) self.assertTrue(A.successor(3) == As3) A = Cell(rdggs, (S, 8, 8)) self.assertTrue(A.successor(1) == None)
def test_ul_vertex(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: # Should work for resolution 0 cells. for X in rdggs.grid(0): get = X.ul_vertex() expect = rdggs.ul_vertex[X.suid[0]] self.assertEqual(get, expect) for X in rdggs.grid(0): get = X.ul_vertex(plane=False) p = rdggs.ul_vertex[X.suid[0]] expect = rdggs.rhealpix(*p, inverse=True) self.assertEqual(get, expect) # Should work on children cells. a = Cell(rdggs, (S, 2, 3)) l = a.resolution x, y = a.ul_vertex() w = rdggs.cell_width(l + 1) error = 1e-10 # Error tolerance. for (i, j) in product(list(range(3)), repeat=2): b = Cell(rdggs, list(a.suid) + [i + 3*j]) xx, yy = b.ul_vertex() xp, yp = (x + i*w, y - j*w) self.assertTrue(rel_err([xx, yy], [xp, yp]) < error) a = Cell(rdggs, (S, 2, 3)) l = a.resolution x, y = a.ul_vertex() w = rdggs.cell_width(l + 1) error = rdggs.ellipsoid.R_A*1e-15 # Error tolerance. for (i, j) in product(list(range(3)), repeat=2): b = Cell(rdggs, list(a.suid) + [i + 3*j]) xx, yy = b.ul_vertex(plane=False) xp, yp = rdggs.rhealpix(x + i*w, y - j*w, inverse=True) self.assertTrue(rel_err([xx, yy], [xp, yp]) < error)
def test_predecessor(self): for rdggs in [WGS84_123, WGS84_123_RADIANS]: A = Cell(rdggs, (O, 0, 1)) Ap0 = Cell(rdggs, (N,)) Ap1 = Cell(rdggs, (N, 8)) Ap2 = Cell(rdggs, (O, 0, 0)) Ap3 = Cell(rdggs, (O, 0, 1, 8)) self.assertTrue(A.predecessor(0) == Ap0) self.assertTrue(A.predecessor(1) == Ap1) self.assertTrue(A.predecessor(2) == Ap2) self.assertTrue(A.predecessor(3) == Ap3) A = Cell(rdggs, (N, 0, 0)) self.assertTrue(A.predecessor(1) == None)