Example #1
0
    def test_load(self):
        """
        Check viability and correctness of an LST saved then loaded from file.
        """
        tree = dcl.construct_tree(self.dataset, self.k, prune_threshold=self.gamma)

        with tempfile.NamedTemporaryFile() as f:
            tree.save(f.name)
            tree2 = dcl.load_tree(f.name)

        self._check_tree_viability(tree2)
        self._check_tree_correctness(tree)
Example #2
0
    def test_load(self):
        """
        Check viability and correctness of an LST saved then loaded from file.
        """
        tree = dcl.construct_tree(self.dataset,
                                  self.k,
                                  prune_threshold=self.gamma)

        with tempfile.NamedTemporaryFile() as f:
            tree.save(f.name)
            tree2 = dcl.load_tree(f.name)

        self._check_tree_viability(tree2)
        self._check_tree_correctness(tree)
Example #3
0
k = int(0.05 * n)
knn_graph, radii = dcl.utils.knn_graph(X, k, method='kd-tree')
density = dcl.utils.knn_density(radii, n, p, k)

## Build the level set tree.
gamma = int(0.1 * n)
tree = dcl.construct_tree_from_graph(knn_graph,
                                     density,
                                     prune_threshold=gamma,
                                     verbose=True)
print tree

## Save the tree to disk and load it back (just for demo purposes).
tree.save('crater_tree.lst')

tree2 = dcl.load_tree('crater_tree.lst')
print tree2

## Get leaf clusters and leaf node indices.
leaves = tree.get_leaf_nodes()
labels = tree.get_clusters(method='leaf')

## Plot the tree, coloring the nodes used to cluster.
fig = tree.plot(form='mass', color_nodes=leaves, colormap=plt.cm.Spectral)[0]
fig.show()

## Plot the clusters in feature space.
X_clusters = X[labels[:, 0], :]

fig, ax = plt.subplots()
ax.set_title("High-density cluster labels")
Example #4
0
    def test_old_trees(self):
        """
        Make sure models from previous versions of DeBaCl still load in the
        current version.
        """
        old_tree_dir = os.path.join(os.path.dirname(__file__), 'saved_trees')
        print("old tree dir: {}".format(old_tree_dir))

        for f in os.listdir(old_tree_dir):

            ## Load the tree.
            tree = dcl.load_tree(os.path.join(old_tree_dir, f))

            ## Is it a level set tree?
            self.assertIsInstance(tree, dcl.LevelSetTree)

            ## Make sure all methods run ok.
            # printing
            print(tree)
            for c in ('start_level', 'end_level', 'start_mass', 'end_mass',
                      'size', 'parent', 'children'):
                self.assertTrue(c in tree.__str__())

            # pruning
            old_gamma = tree.prune_threshold
            new_gamma = 2 * tree.prune_threshold
            pruned_tree = tree.prune(new_gamma)
            self.assertEqual(tree.prune_threshold, old_gamma)
            self.assertEqual(pruned_tree.prune_threshold, new_gamma)

            # plotting
            # plot = tree.plot()  # Travis CI doesn't like this.

            # retrieve clusters.
            labels = tree.get_clusters()
            n = len(tree.density)

            self.assertTrue(labels.dtype is np.dtype('int64'))
            self.assertLessEqual(len(labels), n)
            self.assertEqual(len(labels[:, 0]), len(np.unique(labels[:, 0])))
            self.assertGreaterEqual(np.min(labels), 0)

            max_row_index = np.max(labels[:, 0])
            self.assertLessEqual(max_row_index, n)

            # retrieve leaf indices
            leaves = tree.get_leaf_nodes()
            self.assertIsInstance(leaves, list)
            self.assertGreater(len(leaves), 0)

            # full partition
            partition = tree.branch_partition()

            self.assertTrue(partition.dtype is np.dtype('int64'))
            self.assertEqual(len(partition), n)
            self.assertEqual(len(partition[:, 0]), len(np.unique(partition[:, 0])))
            self.assertEqual(np.min(partition[:, 0]), 0)
            self.assertItemsEqual(np.unique(partition[:, 1]),
                                  tree.nodes.keys())

            # save and load
            with tempfile.NamedTemporaryFile() as t:
                tree.save(t.name)
                tree2 = dcl.load_tree(t.name)

                self.assertItemsEqual([x.start_level for x in tree.nodes.values()],
                                      [y.start_level for y in tree2.nodes.values()])
Example #5
0
    def test_old_trees(self):
        """
        Make sure models from previous versions of DeBaCl still load in the
        current version.
        """
        old_tree_dir = os.path.join(os.path.dirname(__file__), 'saved_trees')
        print("old tree dir: {}".format(old_tree_dir))

        for f in os.listdir(old_tree_dir):

            ## Load the tree.
            tree = dcl.load_tree(os.path.join(old_tree_dir, f))

            ## Is it a level set tree?
            self.assertIsInstance(tree, dcl.LevelSetTree)

            ## Make sure all methods run ok.
            # printing
            print(tree)
            for c in ('start_level', 'end_level', 'start_mass', 'end_mass',
                      'size', 'parent', 'children'):
                self.assertTrue(c in tree.__str__())

            # pruning
            old_gamma = tree.prune_threshold
            new_gamma = 2 * tree.prune_threshold
            pruned_tree = tree.prune(new_gamma)
            self.assertEqual(tree.prune_threshold, old_gamma)
            self.assertEqual(pruned_tree.prune_threshold, new_gamma)

            # plotting
            # plot = tree.plot()  # Travis CI doesn't like this.

            # retrieve clusters.
            labels = tree.get_clusters()
            n = len(tree.density)

            self.assertTrue(labels.dtype is np.dtype('int64'))
            self.assertLessEqual(len(labels), n)
            self.assertEqual(len(labels[:, 0]), len(np.unique(labels[:, 0])))
            self.assertGreaterEqual(np.min(labels), 0)

            max_row_index = np.max(labels[:, 0])
            self.assertLessEqual(max_row_index, n)

            # retrieve leaf indices
            leaves = tree.get_leaf_nodes()
            self.assertIsInstance(leaves, list)
            self.assertGreater(len(leaves), 0)

            # full partition
            partition = tree.branch_partition()

            self.assertTrue(partition.dtype is np.dtype('int64'))
            self.assertEqual(len(partition), n)
            self.assertEqual(len(partition[:, 0]),
                             len(np.unique(partition[:, 0])))
            self.assertEqual(np.min(partition[:, 0]), 0)
            self.assertItemsEqual(np.unique(partition[:, 1]),
                                  tree.nodes.keys())

            # save and load
            with tempfile.NamedTemporaryFile() as t:
                tree.save(t.name)
                tree2 = dcl.load_tree(t.name)

                self.assertItemsEqual(
                    [x.start_level for x in tree.nodes.values()],
                    [y.start_level for y in tree2.nodes.values()])
Example #6
0
k = int(0.05 * n)
knn_graph, radii = dcl.utils.knn_graph(X, k, method='kd-tree')
density = dcl.utils.knn_density(radii, n, p, k)


## Build the level set tree.
gamma = int(0.1 * n)
tree = dcl.construct_tree_from_graph(knn_graph, density, prune_threshold=gamma,
                                     verbose=True)
print tree


## Save the tree to disk and load it back (just for demo purposes).
tree.save('crater_tree.lst')

tree2 = dcl.load_tree('crater_tree.lst')
print tree2


## Get leaf clusters and leaf node indices.
leaves = tree.get_leaf_nodes()
labels = tree.get_clusters(method='leaf')


## Plot the tree, coloring the nodes used to cluster.
fig = tree.plot(form='mass', color_nodes=leaves, colormap=plt.cm.Spectral)[0]
fig.show()


## Plot the clusters in feature space.
X_clusters = X[labels[:, 0], :]