def test_asarray(self): x = np.linspace(0, 5) y = x**2 cs = CoordString(np.c_[x, y]) arr = cs.asarray() self.assertTrue(np.all(arr == np.c_[x, y])) return
def test_creation_invalid(self): x = np.linspace(0, 5) y = x**2 z = x**3 with self.assertRaises(ValueError): # this one has too few dimensions cs = CoordString(x) with self.assertRaises(ValueError): # this one has too many dimensions cs = CoordString(np.c_[x, y, z, z]) with self.assertRaises(ValueError): # this one has the wrong shape cs = CoordString(np.r_[x, y, z]) return
def test_creation2(self): x = np.linspace(0, 5) y = x**2 cs = CoordString(np.c_[x, y]) self.assertEqual(cs.rank, 2) self.assertEqual(len(cs), len(x)) return
def test_setitem(self): x = np.linspace(0, 5) y = x**2 cs = CoordString(np.c_[x, y]) cs[22] = np.array((-1, -3), dtype=np.float64) self.assertEqual(cs[22], (-1, -3)) return
def test_bbox3(self): x = np.linspace(0, 5) y = x**2 z = x**3 cs = CoordString(np.c_[x, y, z]) self.assertEqual(cs.bbox, (0, 0, 5, 25)) return
def test_quadtree_duplicates(self): # a naive quadtree with enter an infinite loop when there are # duplicates exceeding leaf node capacity. this test ensures that this # doesn't happen, and that the duplicate points can be retrieved vertices = [(3.0, 4.0) for _ in range(11)] cs = CoordString(vertices) quadtree = QuadTree(cs, leaf_capacity=10) indices_within = quadtree.search_within(2, 3, 4, 5) self.assertEqual(len(indices_within), 11) return
def test_quadtree_search(self): vertices = [(x, x**2) for x in np.linspace(-10, 10, 1000)] cs = CoordString(vertices) quadtree = QuadTree(cs) indices_within = quadtree.search_within(-5, 0, 5, 25) self.assertEqual(len(indices_within), 500) self.assertEqual(min(indices_within), 250) self.assertEqual(max(indices_within), 749) return
def test_quadtree_construction(self): vertices = [(x, x**2) for x in np.linspace(-10, 10, 1000)] cs = CoordString(vertices) quadtree = QuadTree(cs) self.assertEqual(len(quadtree), 1000) return
def test_slicing_stepped(self): x = np.linspace(0, 5) y = x**2 cs = CoordString(np.c_[x, y]) self.assertTrue(np.all(cs.slice(10, 35, 3) == np.c_[x, y][10:35:3])) return
def test_slicing(self): x = np.linspace(0, 5) y = x**2 cs = CoordString(np.c_[x, y]) self.assertTrue(np.all(cs.slice(10, 25) == np.c_[x, y][10:25])) return
def test_asarray_empty(self): cs = CoordString([]) arr = cs.asarray() self.assertTrue(np.all(arr == np.array([[]], dtype=np.float64)))
def test_nan_raises(self): coords = [(1, 2), (3, 4), (5, np.nan), (7, 8), (9, 10)] with self.assertRaises(ValueError): CoordString(coords)
def test_hash(self): A = CoordString([(i, i + 1) for i in range(0, 100, 3)]) B = CoordString([(i + 1, i) for i in range(100, 0, -3)]) C = CoordString([(i, i + 1) for i in range(0, 100, 3)]) self.assertEqual(hash(A), hash(C)) self.assertNotEqual(hash(A), hash(B))