Example #1
0
def make_tree(X, C, method='single'):
    if method == 'single':
        tree = to_tree(single(C))
    elif method == 'ward':
        tree = to_tree(ward(X))
    elif method == 'average':
        tree = to_tree(average(C))
    return Tree(root=construct_node(tree))
def preorder_indent(tree: Tree, position: Tree.Position, depth: int):
    """
    Print preorder representation of subtree of T rooted at p at depth depth
    :param tree: tree
    :param position: root
    :param depth: depth
    """
    print(2 * depth * " ", str(position.element()))
    for c in tree.children(position):
        preorder_indent(tree, c, depth + 1)
def parenthesis(tree: Tree, position: Tree.Position):
    """
    Print parenthesized representation of tree rooted at the position
    :param tree: tree
    :param position: position
    :return:
    """
    print(position.element(), end="")
    if not tree.is_leaf(position):
        first_time = Tree

        for c in tree.children(position):
            if first_time:
                print("(", end="")
            else:
                print(",", end=" ")
            first_time = False
            parenthesis(tree, c)
        print(")", end="")
def preorder_label(tree: Tree, position: Tree.Position, depth: int, path: [int]):
    """
    Print preorder representation of subtree of T rooted at p at depth depth
    :param tree: tree
    :param position: root
    :param depth: depth
    :param path: a list of ints to represent the path of the position
    """
    label = ".".join([str(j+1) for j in path])
    print(2 * depth * " ", label, str(position.element()))

    path.append(0)
    for c in tree.children(position):
        preorder_label(tree, c, depth + 1, path)
        path[-1] += 1
    path.pop()
    def test__tree__given_root__is_not_empty(self):
        root = Node(0)
        tree = Tree(root)

        assert not tree.is_empty()
    def test__tree__given_no_root__is_empty(self):
        tree = Tree(root=None)

        assert tree.is_empty()
Example #7
0
X += np.random.normal(scale=0.01, size=X.shape)

pca = PCA(2)
pca.fit(X)

# X = pca.transform(X)
N, D = X.shape

C = pdist(X)
tree = to_tree(single(C))

def construct_node(snode):
    if snode.left is None and snode.right is None:
        return TreeLeaf(snode.get_id())
    node = TreeNode()
    node.add_child(construct_node(snode.left))
    node.add_child(construct_node(snode.right))
    return node

root = construct_node(tree)
linkage_tree = Tree(root=root)
plot_tree(linkage_tree, 'linkage_induced')


if args.tree:
    with open(args.tree, 'r') as fp:
        ddt_tree = Tree.from_newick(fp.read())
    plot_tree(ddt_tree, 'ddt_induced')


Example #8
0
    def __init__(self):
        self.x_dict = LabelDictionary(
            ["write", "that", "code", "ROOT", "don't"])
        self.train_trees = TreeList()
        tree_ex1 = Tree()  # container for node_list and edge_list
        idx = self.x_dict.get_label_id("write")
        n0 = Node(len(tree_ex1), idx)  # len is 0
        tree_ex1.add_node(n0)
        idx = self.x_dict.get_label_id("that")
        n1 = Node(len(tree_ex1), idx)
        tree_ex1.add_node(n1)
        idx = self.x_dict.get_label_id("code")
        n2 = Node(len(tree_ex1), idx)
        tree_ex1.add_node(n2)
        idx = self.x_dict.get_label_id("ROOT")
        n3 = Node(len(tree_ex1), idx)
        tree_ex1.add_node(n3)

        tree_ex1.add_edge(Edge(n0, n2))
        tree_ex1.add_edge(Edge(n2, n1))
        tree_ex1.add_edge(Edge(n3, n0))

        self.train_trees.add_tree(tree_ex1)

        tree_ex2 = Tree()
        idx = self.x_dict.get_label_id("don't")
        n0 = Node(len(tree_ex1), idx)  # len is 0
        tree_ex2.add_node(n0)
        idx = self.x_dict.get_label_id("write")
        n1 = Node(len(tree_ex1), idx)
        tree_ex2.add_node(n1)
        idx = self.x_dict.get_label_id("code")
        n2 = Node(len(tree_ex1), idx)
        tree_ex2.add_node(n2)
        idx = self.x_dict.get_label_id("ROOT")
        n3 = Node(len(tree_ex1), idx)
        tree_ex2.add_node(n3)

        tree_ex2.add_edge(Edge(n0, n1))
        tree_ex2.add_edge(Edge(n1, n2))
        tree_ex2.add_edge(Edge(n3, n0))

        self.train_trees.add_tree(tree_ex2)
Example #9
0
X += np.random.normal(scale=0.01, size=X.shape)

pca = PCA(2)
pca.fit(X)

# X = pca.transform(X)
N, D = X.shape

C = pdist(X)
tree = to_tree(single(C))


def construct_node(snode):
    if snode.left is None and snode.right is None:
        return TreeLeaf(snode.get_id())
    node = TreeNode()
    node.add_child(construct_node(snode.left))
    node.add_child(construct_node(snode.right))
    return node


root = construct_node(tree)
linkage_tree = Tree(root=root)
plot_tree(linkage_tree, 'linkage_induced')

if args.tree:
    with open(args.tree, 'r') as fp:
        ddt_tree = Tree.from_newick(fp.read())
    plot_tree(ddt_tree, 'ddt_induced')
Example #10
0
 def __init__(self, file="", json=""):
     """
         Constructor.
         It simply calls GeneralTree constructor.
     """
     Tree.__init__(self, file, json)