Beispiel #1
0
 def test_build(self):
     m = Manifold(self.data, 'euclidean').build(criterion.MaxDepth(1))
     self.assertEqual(2, len(m.layers))
     m.build(criterion.MaxDepth(2))
     self.assertEqual(3, len(m.layers))
     self.assertEqual(len(self.data), m.graphs[0].population)
     return
Beispiel #2
0
class TestCriterion(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.data = datasets.bullseye(n=500)[0]
        return

    def setUp(self) -> None:
        self.manifold = Manifold(self.data, 'euclidean')
        return

    def test_min_radius(self):
        min_radius = 0.1
        self.manifold.build(criterion.MinRadius(min_radius), )
        self.assertTrue(
            all((cluster.radius > min_radius for layer in self.manifold.layers
                 for cluster in layer if cluster.children)))
        self.assertTrue(
            all((cluster.radius <= min_radius for layer in self.manifold.layers
                 for cluster in layer if not cluster.children)))
        return

    def test_combinations(self):
        min_points, max_depth = 10, 8
        self.manifold.build(criterion.MinPoints(min_points),
                            criterion.MaxDepth(max_depth))
        [
            self.assertLessEqual(len(c.children), 1)
            for g in self.manifold.layers for c in g
            if len(c.argpoints) <= min_points or c.depth >= max_depth
        ]
        # self.plot()
        return

    def test_medoid_near_centroid(self):
        self.manifold.build(criterion.MedoidNearCentroid(),
                            criterion.MaxDepth(8))
        # self.plot()
        return

    def test_uniform_distribution(self):
        self.manifold.build(criterion.UniformDistribution(),
                            criterion.MaxDepth(8))
        # self.plot()
        return

    def test_lfd_range(self):
        self.manifold.build(criterion.MaxDepth(12), criterion.LFDRange(60, 50))

        for leaf in self.manifold.layers[-1].clusters:
            ancestry = self.manifold.ancestry(leaf)
            included = sum(
                (1 if ancestor in self.manifold.graph.clusters else 0
                 for ancestor in ancestry))
            self.assertEqual(
                1, included,
                f"expected exactly one ancestor to be in graph. Found {included}"
            )
        return

    def test_minimize_subsumed(self):
        fraction: float = 0.2

        self.manifold.build(
            criterion.MaxDepth(12),
            criterion.LFDRange(80, 20),
            criterion.MinimizeSubsumed(fraction),
        )

        for leaf in self.manifold.layers[-1].clusters:
            ancestry = self.manifold.ancestry(leaf)
            included = sum(
                (1 if ancestor in self.manifold.graph.clusters else 0
                 for ancestor in ancestry))
            self.assertEqual(
                1, included,
                f"expected exactly one ancestor to be in graph. Found {included}"
            )
        return