def setUpClass(cls): cls.lim1 = 8 cls.lim2 = 3 cls.lim3 = 4 cls.link_num = 4 cls.points1 = [(float(i) / cls.lim1, ) for i in range(cls.lim1)] cls.links1 = { p: [ cls.points1[l] for l in [random.randint(0, cls.lim1 - 1) for x in range(cls.link_num)] ] for p in cls.points1 } cls.points2 = [(float(i) / cls.lim2, float(j) / cls.lim2) for i in range(cls.lim2) for j in range(cls.lim2)] cls.links2 = { p: [ cls.points2[l] for l in [ random.randint(0, cls.lim2**2 - 1) for x in range(cls.link_num) ] ] for p in cls.points2 } cls.points3 = [(float(i) / cls.lim3, float(j) / cls.lim3, float(k) / cls.lim3) for i in range(cls.lim3) for j in range(cls.lim3) for k in range(cls.lim3)] cls.links3 = { p: [ cls.points3[l] for l in [ random.randint(0, cls.lim3**3 - 1) for x in range(cls.link_num) ] ] for p in cls.points3 } cls.grid1 = Grid(cls.points1, cls.links1) cls.grid2 = Grid(cls.points2, cls.links2) cls.grid3 = Grid(cls.points3, cls.links3) cls.cluster1 = Cluster(cls.grid1) cls.cluster2 = Cluster(cls.grid2) cls.cluster3 = Cluster(cls.grid3) cls.cub1 = Cuboid(numpy.array([0]), numpy.array([1])) cls.cub2 = Cuboid(numpy.array([0, 0]), numpy.array([1, 1])) cls.cub3 = Cuboid(numpy.array([0, 0, 0]), numpy.array([1, 1, 1])) cls.rc1 = RegularCuboid(cls.cluster1) cls.rc2 = RegularCuboid(cls.cluster2) cls.rc3 = RegularCuboid(cls.cluster3) cls.ct1 = build_cluster_tree(cls.rc1) cls.ct2 = build_cluster_tree(cls.rc2) cls.ct3 = build_cluster_tree(cls.rc3) cls.bct1 = build_block_cluster_tree(cls.ct1, right_cluster_tree=cls.ct1, admissible_function=admissible) cls.bct2 = build_block_cluster_tree(cls.ct2, right_cluster_tree=cls.ct2, admissible_function=admissible) cls.bct3 = build_block_cluster_tree(cls.ct3, right_cluster_tree=cls.ct3, admissible_function=admissible)
def test_distance(self): dist_cub1 = Cuboid(numpy.array([2]), numpy.array([3])) self.assertEqual(self.cub1.distance(dist_cub1), 1) dist_cub2 = Cuboid(numpy.array([2, 2]), numpy.array([3, 3])) self.assertEqual(self.cub1.distance(dist_cub2), numpy.sqrt(2)) dist_cub3 = Cuboid(numpy.array([2, 2, 2]), numpy.array([3, 3, 3])) self.assertEqual(self.cub1.distance(dist_cub3), numpy.sqrt(3))
def minimal_cuboid(cluster): """Build minimal cuboid Build minimal cuboid around cluster that is parallel to the axis in Cartesian coordinates :param cluster: cluster to build cuboid around :type cluster: Cluster :return: minimal cuboid :rtype: Cuboid """ points = cluster.grid.points low_corner = numpy.array(points[0], float, ndmin=1) high_corner = numpy.array(points[0], float, ndmin=1) for p in points: p = numpy.array(p, float, ndmin=1) lowers = (p < low_corner).nonzero() for l in lowers: low_corner[l] = p[l] highers = (p > high_corner).nonzero() for h in highers: high_corner[h] = p[h] return Cuboid(low_corner, high_corner)
def test_split(self): self.assertRaises(NotImplementedError, self.dummy.split) left_cluster = Cluster(self.grid1, self.cluster1.indices[:self.lim1 / 2]) right_cluster = Cluster(self.grid1, self.cluster1.indices[self.lim1 / 2:]) left_cub = Cuboid( numpy.array([0]), numpy.array([float(self.lim1 - 1) / (2 * self.lim1)])) right_cub = Cuboid( numpy.array([float(self.lim1 - 1) / (2 * self.lim1)]), numpy.array([float(self.lim1 - 1) / self.lim1])) left_rc = RegularCuboid(left_cluster, left_cub) right_rc = RegularCuboid(right_cluster, right_cub) left_split, right_split = self.rc1.split() self.assertEqual(left_rc, left_split) self.assertEqual(right_rc, right_split) left_split, right_split = self.mc1.split() left_mc = MinimalCuboid(left_cluster) right_mc = MinimalCuboid(right_cluster) self.assertEqual(left_split, left_mc) self.assertEqual(right_split, right_mc) left_split, right_split = self.ba1.split() left_ba = Balanced(left_cluster) right_ba = Balanced(right_cluster) self.assertEqual(left_ba, left_split) self.assertEqual(right_ba, right_split) left_cluster = Cluster(self.grid2, self.cluster2.indices[:self.lim2**2 / 2]) right_cluster = Cluster(self.grid2, self.cluster2.indices[self.lim2**2 / 2:]) left_cub = Cuboid( numpy.array([0, 0]), numpy.array([ float(self.lim2 - 1) / (2 * self.lim2), float(self.lim2 - 1) / self.lim2 ])) right_cub = Cuboid( numpy.array([float(self.lim2 - 1) / (2 * self.lim2), 0]), numpy.array([ float(self.lim2 - 1) / self.lim2, float(self.lim2 - 1) / self.lim2 ])) left_rc = RegularCuboid(left_cluster, left_cub) right_rc = RegularCuboid(right_cluster, right_cub) left_split, right_split = self.rc2.split() self.assertEqual(left_rc, left_split) self.assertEqual(right_rc, right_split) left_split, right_split = self.mc2.split() left_mc = MinimalCuboid(left_cluster) right_mc = MinimalCuboid(right_cluster) self.assertEqual(left_mc, left_split) self.assertEqual(right_mc, right_split) left_split, right_split = self.ba2.split() left_ba = Balanced(left_cluster) right_ba = Balanced(right_cluster) self.assertEqual(left_ba, left_split) self.assertEqual(right_ba, right_split) left_cluster = Cluster(self.grid3, self.cluster2.indices[:self.lim3**3 / 2]) right_cluster = Cluster(self.grid3, self.cluster2.indices[self.lim3**3 / 2:]) left_cub = Cuboid( numpy.array([0, 0, 0]), numpy.array([ float(self.lim3 - 1) / (2 * self.lim3), float(self.lim3 - 1) / self.lim3, float(self.lim3 - 1) / self.lim3 ])) right_cub = Cuboid( numpy.array([float(self.lim3 - 1) / (2 * self.lim3), 0, 0]), numpy.array([ float(self.lim3 - 1) / self.lim3, float(self.lim3 - 1) / self.lim3, float(self.lim3 - 1) / self.lim3 ])) left_rc = RegularCuboid(left_cluster, left_cub) right_rc = RegularCuboid(right_cluster, right_cub) left_split, right_split = self.rc3.split() self.assertEqual(left_rc, left_split) self.assertEqual(right_rc, right_split) left_split, right_split = self.mc3.split() left_mc = MinimalCuboid(left_cluster) right_mc = MinimalCuboid(right_cluster) self.assertEqual(left_mc, left_split) self.assertEqual(right_mc, right_split) left_split, right_split = self.ba3.split() left_ba = Balanced(left_cluster) right_ba = Balanced(right_cluster) self.assertEqual(left_ba, left_split) self.assertEqual(right_ba, right_split) grid = Grid([0, 1, 5], [[1], [5], [0]]) clust = Cluster(grid) regcub = RegularCuboid(clust) split1, split2 = regcub.split() split3 = split1.split() split4 = split2.split() self.assertEqual(len(split3), 1) self.assertEqual(len(split4), 1) test_ba = Balanced(split4[0].cluster) self.assertEqual(test_ba.split(), test_ba)
def setUpClass(cls): cls.cub1 = Cuboid(numpy.array([0]), numpy.array([1])) cls.cub2 = Cuboid(numpy.array([0, 0]), numpy.array([1, 1])) cls.cub3 = Cuboid(numpy.array([0, 0, 0]), numpy.array([1, 1, 1]))
def test_split(self): left_cub = Cuboid(numpy.array([0]), numpy.array([0.5])) right_cub = Cuboid(numpy.array([0.5]), numpy.array([1])) left_split, right_split = self.cub1.split() self.assertEqual(left_cub, left_split) self.assertEqual(right_cub, right_split) left_cub0 = Cuboid(numpy.array([0, 0]), numpy.array([0.5, 1])) right_cub0 = Cuboid(numpy.array([0.5, 0]), numpy.array([1, 1])) left_cub1 = Cuboid(numpy.array([0, 0]), numpy.array([1, 0.5])) right_cub1 = Cuboid(numpy.array([0, 0.5]), numpy.array([1, 1])) left_split0, right_split0 = self.cub2.split() left_split1, right_split1 = self.cub2.split(1) self.assertEqual(left_cub0, left_split0) self.assertEqual(right_cub0, right_split0) self.assertEqual(left_cub1, left_split1) self.assertEqual(right_cub1, right_split1) left_cub0 = Cuboid(numpy.array([0, 0, 0]), numpy.array([0.5, 1, 1])) right_cub0 = Cuboid(numpy.array([0.5, 0, 0]), numpy.array([1, 1, 1])) left_cub1 = Cuboid(numpy.array([0, 0, 0]), numpy.array([1, 0.5, 1])) right_cub1 = Cuboid(numpy.array([0, 0.5, 0]), numpy.array([1, 1, 1])) left_cub2 = Cuboid(numpy.array([0, 0, 0]), numpy.array([1, 1, 0.5])) right_cub2 = Cuboid(numpy.array([0, 0, 0.5]), numpy.array([1, 1, 1])) left_split0, right_split0 = self.cub3.split() left_split1, right_split1 = self.cub3.split(1) left_split2, right_split2 = self.cub3.split(2) self.assertEqual(left_cub0, left_split0) self.assertEqual(right_cub0, right_split0) self.assertEqual(left_cub1, left_split1) self.assertEqual(right_cub1, right_split1) self.assertEqual(left_cub2, left_split2) self.assertEqual(right_cub2, right_split2)
def setUpClass(cls): cls.lim1 = 16 cls.lim2 = 4 cls.lim3 = 4 cls.link_num = 4 cls.points1 = [(float(i) / cls.lim1, ) for i in range(cls.lim1)] cls.links1 = { p: [ cls.points1[l] for l in [random.randint(0, cls.lim1 - 1) for x in range(cls.link_num)] ] for p in cls.points1 } cls.points2 = [(float(i) / cls.lim2, float(j) / cls.lim2) for i in range(cls.lim2) for j in range(cls.lim2)] cls.links2 = { p: [ cls.points2[l] for l in [ random.randint(0, cls.lim2**2 - 1) for x in range(cls.link_num) ] ] for p in cls.points2 } cls.points3 = [(float(i) / cls.lim3, float(j) / cls.lim3, float(k) / cls.lim3) for i in range(cls.lim3) for j in range(cls.lim3) for k in range(cls.lim3)] cls.links3 = { p: [ cls.points3[l] for l in [ random.randint(0, cls.lim3**3 - 1) for x in range(cls.link_num) ] ] for p in cls.points3 } cls.grid1 = Grid(cls.points1, cls.links1) cls.grid2 = Grid(cls.points2, cls.links2) cls.grid3 = Grid(cls.points3, cls.links3) cls.cluster1 = Cluster(cls.grid1) cls.cluster2 = Cluster(cls.grid2) cls.cluster3 = Cluster(cls.grid3) cls.cub1 = Cuboid(numpy.array([0]), numpy.array([1])) cls.cub2 = Cuboid(numpy.array([0, 0]), numpy.array([1, 1])) cls.cub3 = Cuboid(numpy.array([0, 0, 0]), numpy.array([1, 1, 1])) cls.rc1 = RegularCuboid(cls.cluster1) cls.rc2 = RegularCuboid(cls.cluster2) cls.rc3 = RegularCuboid(cls.cluster3) cls.ct1 = build_cluster_tree(cls.rc1, 4) cls.ct2 = build_cluster_tree(cls.rc2, 4) cls.ct3 = build_cluster_tree(cls.rc3, 4) cls.bct1 = build_block_cluster_tree(cls.ct1, cls.ct1, admissible_function=admissible) cls.bct2 = build_block_cluster_tree(cls.ct2, cls.ct2, admissible_function=admissible) cls.bct3 = build_block_cluster_tree(cls.ct3, cls.ct3, admissible_function=admissible) def build_full(x): return numpy.matrix(numpy.ones(x.shape())) def build_rmat(x): return RMat(numpy.matrix(numpy.ones((x.shape()[0], 1))), numpy.matrix(numpy.ones((x.shape()[1], 1))), max_rank=1) cls.hmat1 = build_hmatrix(cls.bct1, build_rmat, build_full) cls.hmat2 = build_hmatrix(cls.bct2, build_rmat, build_full) cls.hmat3 = build_hmatrix(cls.bct3, build_rmat, build_full)