Esempio n. 1
0
 def __init__(self, neighbors=1, *opt):
     self.opt = opt
     self.neighbors = neighbors
     self.LEFT = 0
     self.RIGHT = 1
      
     Tree.__init__(self)
Esempio n. 2
0
    def __init__(
            self,
            state,
            dynamics_layer_sizes,
            reward_layer_sizes,
            value_layer_sizes,
            obs_dim,
            act_dim,
            memory_size,
            batch_size,
            target_num_leaves,
            cross_val_ratio=0.1,
            rand_state_0=(0, 0, 0.5, 0.05, 0.01),
    ):

        self.dynamics = relu_MLP(dynamics_layer_sizes)
        self.reward = relu_MLP(reward_layer_sizes)
        self.value = relu_MLP(value_layer_sizes)

        self.memory_train = ReplayBuffer(obs_dim, act_dim, memory_size,
                                         batch_size)
        self.memory_test = ReplayBuffer(obs_dim, act_dim, memory_size,
                                        batch_size)

        self.target_num_leaves = target_num_leaves
        new_state_node = nodeData(
            action_inbound=None,
            state=state,
            noise_state=rand_state_0,
            value_est=self.value.predict(state),
            step_reward=0,
        )
        self.T = Tree(new_state_node)
Esempio n. 3
0
 def to_tree(i, j):
     if j - i == 1:
         return Tree(sent[i], None, sent[i])
     else:
         k = backpt[i][j]
         return Tree(label_table[i][j],
                     [to_tree(i, k), to_tree(k, j)], None)
Esempio n. 4
0
    def forward(self, sent_all):
        """
        Parse all the sentences (algorithm is only run on sentences with length >= 3)
        """
        sent_len3 = []
        idx = []
        trees, probs = [None] * len(sent_all), [None] * len(sent_all)
        for i, sent in enumerate(sent_all):
            sent_stripped = [s for s in sent if s not in PUNCT]
            if len(sent_stripped) == 1:
                trees[i] = Tree(sent_stripped[0], None, sent_stripped[0])
            elif len(sent_stripped) == 2:
                trees[i] = Tree('S', [
                    Tree(sent_stripped[0], None, sent_stripped[0]),
                    Tree(sent_stripped[1], None, sent_stripped[1])
                ], None)
            else:
                sent_len3.append(sent)
                idx.append(i)

        if len(sent_len3) > 0:
            tree_all, prob_all = self.cky_len3(sent_len3)
            for tree, prob, ind in zip(tree_all, prob_all, idx):
                trees[ind] = tree
                probs[ind] = prob

        return trees, probs
Esempio n. 5
0
def animal():
    root = Tree("bird")

    while True:
        if not yes("Are you thinking of an animal? "):
            break

        tree = root
        while tree.left is not None:
            prompt = tree.cargo + "? "
            if yes(prompt):
                tree = tree.right
            else:
                tree = tree.left

        guess = tree.cargo
        prompt = "Is it a " + guess + "?"
        if yes(prompt):
            print("I rule")
            continue

        prompt = "What is the animal's name? "
        animal = input(prompt)
        prompt = "What question would distinguish a {0} from a {1}? "
        question = input(prompt.format(animal, guess))

        tree.cargo = question
        prompt = "If the animal were {0} the answer would be? "
        if yes(prompt.format(animal)):
            tree.left = Tree(guess)
            tree.right = Tree(animal)
        else:
            tree.left = Tree(animal)
            tree.right = Tree(guess)
Esempio n. 6
0
    def set_trees(self, cell, biome):
        tree_freq = ease(myrange_f(biome.tree_range))

        if cell.color == 14:
            cell.color = 3
            height = myrange(self.config.trees_height_range)
            girth = min(self.cell_size, self.cell_size) * 2 / 5
            p = Vec2(self.cell_size / 2, self.cell_size / 2)
            leaves = [[0, 0], [0, 0], [0, 0]]
            cell.trees.append(Tree(p, height, girth, leaves))
        else:
            # Trees
            for x in range(0, self.cell_size - self.config.trees_gap,
                           self.config.trees_gap):
                for y in range(0, self.cell_size - self.config.trees_gap,
                               self.config.trees_gap):
                    if rnd(1) < tree_freq:
                        height = myrange(self.config.trees_height_range)
                        girth = myrange(self.config.trees_girth_range)
                        p = Vec2(x + rnd(self.config.trees_gap),
                                 y + rnd(self.config.trees_gap))
                        leaves = [[0, 0], [0, 0], [0, 0]]
                        tree = Tree(p, height, girth, leaves)
                        cell.trees.append(tree)
                        tree.p = Vec2(
                            mid(tree.girth, tree.pos.x,
                                self.cell_size - tree.girth),
                            mid(tree.girth, tree.pos.y,
                                self.cell_size - tree.girth))
def main(ptb_file, results_dir, annotations_dir):
    correct_patterns = set()
    for root, dirs, files in os.walk(results_dir):
        for f in files:
            if 'correct_' not in f:
                continue
            for line in open(root + '/' + f):
                correct_patterns.add(line.strip()[1:-1])
    pattern_heads = dict()
    for root, dirs, files in os.walk(annotations_dir):
        for f in files:
            for line in open(root + '/' + f):
                if line.startswith('i'): continue  # skip the header
                index, pattern, head = line.strip().split('\t')
                pattern_heads[pattern[1:-1]] = int(head) - 1  # 0-index the head
    trees = []
    text = ''
    skipped = 0
    for line in open(ptb_file):
        if text and line[0] != ' ':
            try:
                trees.append(Tree.read(text))
            except AttributeError:
                #print text
                skipped += 1
            text = ''
        text += line
    if text:
        trees.append(Tree.read(text))
    good_trees = []
    for tree in trees:
        if tree_is_good(tree.root.children[0], pattern_heads):
        #if tree_is_good(tree.root.children[0], correct_patterns):
            good_trees.append(tree)
    print 'Number of trees:', len(trees)
    print 'Number of good trees:', len(good_trees)
    print 'Skipped:', skipped
    cats = error_category_counts.keys()
    cats.sort(key=lambda x: error_category_counts[x])
    for cat in cats:
        print '%s: %d' % (cat, error_category_counts[cat])
    print
    errors = error_counts.keys()
    errors.sort(key=lambda x: error_counts[x], reverse=True)
    for error in errors[:30]:
        print '%s: %d' % (error, error_counts[error])
    out = open('good_trees.mrg', 'w')
    for tree in good_trees:
        out.write(tree.pretty())
        out.write('\n\n')
    out.close()
    shuffle(good_trees)
    num_examples = 100
    out = open('marked_example_trees.mrg', 'w')
    for tree in good_trees[:num_examples]:
        mark_heads(tree.root.children[0], pattern_heads)
        out.write(tree.pretty())
        out.write('\n\n')
    out.close()
def add_random(current_tree, depth=0):
    if depth <= 0:
        return
    new_left = Tree(random.randint(0,100))
    current_tree.left = new_left
    add_random(new_left, depth-1)

    new_right = Tree(random.randint(0,100))
    current_tree.right = new_right
    add_random(new_right, depth-1)
Esempio n. 9
0
def make_class_tree(X, y):
    tree = Tree()
    tree.root = TreeNode()
    C = np.unique(y)
    nodes = {c: TreeNode() for c in C}
    for i, c in enumerate(y):
        node = nodes[c]
        leaf = TreeLeaf(i)
        node.add_child(leaf)
    for node in nodes.values():
        tree.root.add_child(node)
    return tree
Esempio n. 10
0
def make_class_tree(X, y):
    tree = Tree()
    tree.root = TreeNode()
    C = np.unique(y)
    nodes = {c: TreeNode() for c in C}
    for i, c in enumerate(y):
        node = nodes[c]
        leaf = TreeLeaf(i)
        node.add_child(leaf)
    for node in nodes.values():
        tree.root.add_child(node)
    return tree
Esempio n. 11
0
 def testMegabatch(self):
     print 'Loading test megabatch data'
     img_dat = dict()
     tree_dat = dict()
     tree_rem = dict()
     cur_queue = self.test_megabatch_queue.pop(0)
     for i in cur_queue:
         tree_rem[i] = self.data_dict[i]['n_desc']
         vgg16 = self.img_feats['vgg16'][self.data_dict[i]['vgg16']]
         vgg19 = self.img_feats['vgg19'][self.data_dict[i]['vgg19']]
         imidx = self.data_dict[i]['img_feat_idx']
         if self.data_type == 'both':
             img_dat[i] = np.hstack((vgg16[imidx, :], vgg19[imidx, :]))
         elif self.data_type == 'vgg16':
             img_dat[i] = vgg16[imidx, :]
         elif self.data_type == 'vgg19':
             img_dat[i] = vgg19[imidx, :]
         tree_dat[i] = []
         for curtreeIDX in self.data_dict[i]['desc_idx']:
             tree_dat[i].append(Tree(self.trees[curtreeIDX][1]))
         # shuffle the trees
         np.random.shuffle(tree_dat[i])
     print 'Constructing schedule for this testing megabatch'
     while len(tree_rem) >= self.minibatch_size:
         sample = np.random.choice(tree_rem.keys(),
                                   self.minibatch_size,
                                   replace=False)
         for k in sample:
             tree_rem.pop(k, None)
         sample = [[img_dat[x], tree_dat[x].pop(0)] for x in sample]
         self.test_minibatch_queue.append(sample)
     print 'Beginning testing megabatch'
Esempio n. 12
0
    def _generate_all(self, states, depth):
        """
        Generates all trees up to a certain depth that are assigned a
        given state by this BUTA.

        :type states: tuple
        :param states: A tuple of states

        :type depth: int
        :param depth: The maximum height of a generated tree

        :rtype: generator
        :return: This generator produces a series of tuples of trees.
            The ith element of each tuple is a tree whose root node is
            assigned the state states[i] by this BUTA. This generator
            produces all possible tuples of this form
        """
        for q in states:
            check_is_nonterminal(q)
        if depth <= 0:
            return

        if len(states) == 0:
            yield ()
        elif len(states) == 1:
            for label, child_states in self._inverse_transition(states[0]):
                if len(child_states) == 0:
                    yield (label, )
                else:
                    for t in self._generate_all(child_states, depth - 1):
                        yield (Tree(label, t), )
        else:
            for t in self._generate_all(states[0:1], depth):
                for s in self._generate_all(states[1:], depth):
                    yield t + s
def main():
    annotations = Annotation.objects.select_related().filter(head_correct=True)
    for annotation in annotations:
        expansion = annotation.expansion
        root = None
        for i, line in enumerate(expansion.supa_example.split('\n')):
            if 'ROOT' in line:
                if root != None:
                    print 'Two roots found; skipping'
                    continue
                root = i
        if root == None:
            # Sometimes the SUPA is empty; testing on 3/20/2013 showed that was
            # the only time this happened
            continue
        tree = Tree.read(expansion.penn_example)
        head_index = None
        for i, child in enumerate(tree.root.children):
            if root in child.terminal_indices():
                head_index = i+1
                break
        if head_index:
            annotation.head_index = head_index
            annotation.save()
        else:
            print 'Head index not found...'
    transaction.commit()
Esempio n. 14
0
def check_trees_right(woods, screen):

    #spawning trees in right side

    if len(woods) < 10:
        new_tree = Tree(screen, "right")
        if pygame.time.get_ticks() % 10 == 0:
            woods.add(new_tree)
Esempio n. 15
0
def create_tree_4():
    t = Tree(13)
    import random
    arr = list(range(1, 26))
    random.shuffle(arr)
    for x in arr:
        insert(t, x)
    return t
Esempio n. 16
0
def iterative_minimax_strategy(game: Any) -> Any:
    """
    Minimax strategy done with a tree file structure and stacks
    """
    old_game = copy.deepcopy(game)
    current_state = game.current_state
    root = Tree(current_state)
    stack = Stack()
    stack.add(root)
    while not stack.is_empty():
        top = stack.remove()
        game.current_state = top.value
        if game.is_over(top.value):
            game.current_state = top.value
            if game.is_winner(top.value.get_current_player_name()):
                top.score = 1
                game.current_state = old_game.current_state
            elif game.is_winner('p1') or game.is_winner('p2'):
                top.score = -1
                game.current_state = old_game.current_state
            else:
                top.score = 0
                game.current_state = old_game.current_state

        elif top.children == []:
            stack.add(top)
            for move in top.value.get_possible_moves():
                new_state = top.value.make_move(game.str_to_move(str(move)))
                trees = Tree(new_state)
                trees.move = move
                top.children.append(trees)
                stack.add(trees)
        else:
            children_score = []
            for child in top.children:
                children_score.append(child.score * -1)
            top.score = max(children_score)
    for child in root.children:
        if child.score * -1 == root.score:
            return child.move
    return old_game.current_state.get_possible_moves()[0]
Esempio n. 17
0
def get_names_from_file(filename,tree_type=None):
    """
    Returns a string containing names from the file, or a file-like object to POST
    to gnrd
    """
    names = None
    # needs to be multipart/form-data
    if tree_type in [TYPE_NEWICK, TYPE_NEXML]:
        type = 'newick' if tree_type == TYPE_NEWICK else 'nexml'
        n = Tree(filename, type)
        labels = n.get_otu_labels()
        labels = labels + n.get_node_labels()
        # uniquify
        labels = list(set(labels))
        if None in labels:
            del labels[labels.index(None)]
        print "Extracted %d names from %s Tree" % (len(labels), type)
        names ='\n'.join(labels)
    else:
        names = open(filename,'rb') # open in binary in case of PDF or Office document
    return names
def simplify_trees(suite_file, outfile):
    new_trees = []
    tree = ''
    for line in open(suite_file):
        if line == '\n':
            new_trees.append(convert_tree(Tree.read(tree)))
            tree = ''
        tree += line
    out = open(outfile, 'w')
    for tree in new_trees:
        out.write(tree.pretty())
        out.write('\n\n')
def animal():
	# Start with a singleton
	root = Tree("bird")

	# Loop until the user quits
	while True:
		print()
		if not yes("Are you thinking of an animal? "): break

		# Walk the tree
		tree = root
		while tree.left is not None:
			prompt = tree.cargo + "? "
			if yes(prompt):
				tree = tree.right
			else:
				tree = tree.left

		# Make a guess
		guess = tree.cargo
		prompt = "Is it a " + guess + "? "
		if yes(prompt):
			print("I rule!")
			continue

		# Get new information
		prompt = "What is the animal\'s name? "
		animal = input(prompt)
		prompt = "What question would distinguish a {0} from a {1}? "
		question = input(prompt.format(animal, guess))

		# Add new information to the tree
		tree.cargo = question
		prompt = "If the animal were {0} the answer would be? "
		if yes(prompt.format(animal)):
			tree.left = Tree(guess)
			tree.right = Tree(animal)
		else:
			tree.left = Tree(animal)
			tree.right = Tree(guess)
Esempio n. 20
0
def replace_names_nexml(filename,mapping):
    """
    Dangerously replaces the labels in a nexml file
    """
    n = Tree(filename,'nexml')
    n.replace_otu_labels(mapping)
    n.replace_node_labels(mapping)
    pieces = filename.split('.')[:1]
    prefix = pieces[0] if len(pieces) >= 1 else fname
    report_filename = prefix + '_clean.xml'
    n.write_nexml_tree(report_filename)
Esempio n. 21
0
def insert(tree, x, is_nil=is_nil):
    assert is_bst(tree)

    if is_nil(tree):
        return Tree(x)

    if x < tree.data:
        if is_nil(tree.left):
            tree.left = Tree(x)
            tree.left.parent = tree
            return tree.left
        else:
            return insert(tree.left, x)
    if tree.data < x:
        if is_nil(tree.right):
            tree.right = Tree(x)
            tree.right.parent = tree
            return tree.right
        else:
            return insert(tree.right, x)

    assert is_bst(tree)
    return tree
Esempio n. 22
0
 def megabatchAdvance(self):
     if self.testing:
         self.testMegabatch()
         return
     print 'Loading megabatch data'
     self.cur_megabatch += 1
     img_dat = dict()
     tree_dat = dict()
     tree_rem = dict()
     cur_queue = self.megabatch_queue.pop(0)
     for i in cur_queue:
         tree_rem[i] = self.data_dict[i]['n_desc']
         vgg16 = self.img_feats['vgg16'][self.data_dict[i]['vgg16']]
         vgg19 = self.img_feats['vgg19'][self.data_dict[i]['vgg19']]
         imidx = self.data_dict[i]['img_feat_idx']
         if self.data_type == 'both':
             img_dat[i] = np.hstack((vgg16[imidx, :], vgg19[imidx, :]))
         elif self.data_type == 'vgg16':
             img_dat[i] = vgg16[imidx, :]
         elif self.data_type == 'vgg19':
             img_dat[i] = vgg19[imidx, :]
         tree_dat[i] = []
         for curtreeIDX in self.data_dict[i]['desc_idx']:
             tree_dat[i].append([
                 self.trees[curtreeIDX][0],
                 Tree(self.trees[curtreeIDX][1])
             ])
         # shuffle the trees
         np.random.shuffle(tree_dat[i])
     print 'Constructing schedule for this megabatch'
     while len(tree_rem) >= self.minibatch_size:
         sample = np.random.choice(tree_rem.keys(),
                                   self.minibatch_size,
                                   replace=False)
         for k in sample:
             tree_rem[k] -= 1
             if tree_rem[k] == 0:
                 tree_rem.pop(k, None)
         sample = [[img_dat[x], tree_dat[x].pop(0)] for x in sample]
         sids = [x[1][0] for x in sample]
         sample = [[x[0], x[1][1]] for x in sample]
         self.minibatch_queue.append(sample)
         self.minibatch_idents.append(sids)
     if self.batchPerEpoch == None:
         self.batchPerEpoch = len(
             self.minibatch_queue) * (len(self.megabatch_queue) + 1)
     print 'Beginning megabatch %i (epoch %i)' % (self.cur_megabatch,
                                                  self.cur_epoch)
def main(category, suites_dir):
    tree_file = None
    for root, dirs, files in os.walk('.'):
        for f in files:
            if 'sierra_postop' in f:
                tree_file = f
    if not tree_file:
        print 'Could not find sierra_postop file!  Exiting...'
        exit(-1)
    trees = []
    patterns = []
    text = ''
    for line in open(tree_file):
        if line == 'null\n':
            print 'Null found!'
            trees.append(None)
            continue
        if line == '\n':
            trees.append(Tree.read(text))
            text = ''
            continue
        text += line
    new_trees = []
    for i, tree in enumerate(trees):
        # Clear off some of the extra processing that the SUPA pipeline adds
        if tree is None:
            new_trees.append('')
            continue
        root = tree.root.children[1]
        clear_extra_labels(root)
        new_trees.append(root)
    outfile = '../' + suites_dir + '/' + category + '/' + category
    outfile += '_PTBtrees_intermediate.mrg'
    out = open(outfile, 'w')
    for tree in new_trees:
        if tree:
            out.write(tree.pretty())
            out.write('\n\n')
        else:
            out.write('\n')
    out.close()
Esempio n. 24
0
 def constructBatch(self, batch):
     minibatch = []
     if type(batch) == str:
         batch = eval(batch)
     for i in batch:
         imx = i.split('#')[0]
         dcx = int(i.split('#')[1])
         vgg16 = self.img_feats['vgg16'][self.data_dict[imx]['vgg16']]
         vgg19 = self.img_feats['vgg19'][self.data_dict[imx]['vgg19']]
         imidx = self.data_dict[imx]['img_feat_idx']
         if self.data_type == 'both':
             img_dat = np.hstack((vgg16[imidx, :], vgg19[imidx, :]))
         elif self.data_type == 'vgg16':
             img_dat = vgg16[imidx, :]
         elif self.data_type == 'vgg19':
             img_dat = vgg19[imidx, :]
         # get tree data
         desc_idx = self.data_dict[imx]['desc_idx'][dcx]
         tree_dat = Tree(self.trees[desc_idx][1])
         minibatch.append([img_dat, tree_dat])
     return minibatch
Esempio n. 25
0
    def parse(self, tree):
        """
        Given a tree, this function computes the states assigned to each
        node of the tree (i.e., the "parse" of the tree). If this BUTA
        is nondeterministic, a tree may have more than one parse.

        :type tree: Tree
        :param tree: A tree

        :rtype: generator
        :return: The possible parses of tree according to this BUTA.
            Each parse is represented as a tree in which each node is
            labelled with its state according to this BUTA
        """
        if type(tree) is not Tree:
            for q in self._transition(tree):
                yield q
        else:
            symbol = get_root_label(tree)
            parsed_children = [set(self.parse(t)) for t in tree]
            for pc in product(*parsed_children):
                child_states = tuple(get_root_label(t) for t in pc)
                for q in self._transition(symbol, *child_states):
                    yield Tree(q, pc)
Esempio n. 26
0
from trees import Tree, Node


def isBST(root, left=None, right=None):
    if root is None:
        return True

    if left is not None and left.data >= root.data:
        return False

    if right is not None and right.data < root.data:
        return False

    return (isBST(root.left, left, root) and isBST(root.right, root, right))


t = Tree(5)
t.insertLeft(2)
t.insertRight(4)
root = t.getRoot()
t.inOrder(root)
print(isBST(root))
Esempio n. 27
0
print ids
if os.path.exists('./tol.nwk'):
    with open('tol.nwk') as fp:
        tol = json.load(fp)['newick']
else:
    tol = api.treemachine.induced_subtree(ott_ids=ids)['newick']
tree = Phylo.read(StringIO(tol), 'newick')

root_node = tree.root

def get_idx(name):
    id = int(name.split('_')[-1][3:])
    return ids.index(id)

def build_tree(node):
    if node.is_terminal():
        return TreeLeaf(get_idx(node.name))
    else:
        children = map(build_tree, node.clades)
        node = TreeNode()
        for child in children:
            node.add_child(child)
        return node

final_tree = Tree(root=build_tree(root_node))
with open('zoo.tree', 'wb') as fp:
    pickle.dump(final_tree, fp)
with open('zoo.constraints', 'wb') as fp:
    pickle.dump(list(final_tree.generate_constraints()), fp)
Esempio n. 28
0
class Agent:
    def __init__(
            self,
            state,
            dynamics_layer_sizes,
            reward_layer_sizes,
            value_layer_sizes,
            obs_dim,
            act_dim,
            memory_size,
            batch_size,
            target_num_leaves,
            cross_val_ratio=0.1,
            rand_state_0=(0, 0, 0.5, 0.05, 0.01),
    ):

        self.dynamics = relu_MLP(dynamics_layer_sizes)
        self.reward = relu_MLP(reward_layer_sizes)
        self.value = relu_MLP(value_layer_sizes)

        self.memory_train = ReplayBuffer(obs_dim, act_dim, memory_size,
                                         batch_size)
        self.memory_test = ReplayBuffer(obs_dim, act_dim, memory_size,
                                        batch_size)

        self.target_num_leaves = target_num_leaves
        new_state_node = nodeData(
            action_inbound=None,
            state=state,
            noise_state=rand_state_0,
            value_est=self.value.predict(state),
            step_reward=0,
        )
        self.T = Tree(new_state_node)

    def act(self, state):
        while self.T.num_leaves() < self.target_num_leaves:
            state_node = self.T.softmax_sample_leaves()
            state = state_node.state
            noise_state = state_node.noise_state
            noise_state, action = dampedSpringNoiseStep(state.noise_state)
            state_est = state + self.dynamics.predict(np.vstack(state, action))
            reward_est = self.reward.predict(np.vstack(state_est, action))
            value_est = self.value.predict(state_est)

            new_state_node = nodeData(
                action_inbound=action,
                state=state_est,
                noise_state=noise_state,
                value_est=value_est,
                step_reward=reward_est,
            )

            self.T.add_state(
                parent=state_node,
                child=new_state_node,
            )
        target_node = self.T.softmax_sample_leaf_states()
        next_node = self.T.step_toward_and_reroot(target_node)
        action = next_node.action

    def update(self, last_state, action, reward, state):
        pass
Esempio n. 29
0
                current = stack.pop()
                print(current.data)

                current = current.right

            else:
                done = True


def inOrderRec2(root):
    stack = deque()
    tmp = root
    stack.append(tmp)
    while stack.__len__() > 0:
        tmp = stack[-1]
        if tmp.left != None:
            stack.append(tmp.left)
            tmp = tmp.left

        data = stack.pop()
        print(data.data)
        if tmp.right != None:
            stack.ap


t = Tree(5)
t.insertLeft(2)
t.insertRight(7)
root = t.getRoot()
inOrder(root)
inOrderRec(root)
Esempio n. 30
0
def create_tree_3():
    t = Tree(13)
    for x in range(1, 10):
        insert(t, x)
    return t
Esempio n. 31
0
from trees import Tree

tree = Tree()
tree.insert(9)
tree.insert(4)
tree.insert(20)
tree.insert(1)
tree.insert(6)
tree.insert(15)
tree.insert(170)
tree.insert(1)

print()
print(tree.lookup(5))
print(tree.lookup(1))
print(tree.lookup(17))

print('\nInorder ... (Shows real sequence on number line.')
tree.traverse('inorder')
print('\nPreorder')
tree.traverse('preorder')
print('\nPostorder')
tree.traverse('postorder')
print('\nLevelorder')
tree.traverse('levelorder')  # breadth first

print('\n')

removed = tree.remove(4)
print(f'removed : {removed}' if removed is not None else 'Key Not Found!')
print('\nLevelorder')
Esempio n. 32
0
        return -1
    height_diff = abs(rightHeight - leftHeight)
    if height_diff > 1:
        return -1
    else:
        return max(leftHeight, rightHeight) + 1


def isBalancedAlt(tree_node):
    if heightAlt(tree_node) == -1:
        return False
    return True


if t1:
    x = Tree()
    x.add(5)
    x.add(3)
    x.add(2)
    x.add(4)
    print isBalancedAlt(x.root)
    y = Tree()
    y.add(4)
    y.add(3)
    y.add(5)
    y.add(6)
    print isBalancedAlt(y.root)


def searchDFS(graph, start):
    visited = []
Esempio n. 33
0
    with open('tol.nwk') as fp:
        tol = json.load(fp)['newick']
else:
    tol = api.treemachine.induced_subtree(ott_ids=ids)['newick']
tree = Phylo.read(StringIO(tol), 'newick')

root_node = tree.root


def get_idx(name):
    id = int(name.split('_')[-1][3:])
    return ids.index(id)


def build_tree(node):
    if node.is_terminal():
        return TreeLeaf(get_idx(node.name))
    else:
        children = map(build_tree, node.clades)
        node = TreeNode()
        for child in children:
            node.add_child(child)
        return node


final_tree = Tree(root=build_tree(root_node))
with open('zoo.tree', 'wb') as fp:
    pickle.dump(final_tree, fp)
with open('zoo.constraints', 'wb') as fp:
    pickle.dump(list(final_tree.generate_constraints()), fp)
def main(annotation_file, category):
    outfile = 'results/results.tsv'
    tree_file = None
    for root, dirs, files in os.walk('.'):
        for f in files:
            if 'sierra_postop' in f:
                tree_file = f
    if not tree_file:
        print 'Could not find sierra_postop file!  Exiting...'
        exit(-1)
    annotations = {}
    annotation_patterns = {}
    for line in open(annotation_file):
        if line.startswith('index'): continue
        index, pattern, head_index = line.strip().split('\t')
        annotations[int(index)] = int(head_index) - 1
        annotation_patterns[pattern] = int(head_index) - 1
    trees = []
    patterns = []
    text = ''
    for line in open(tree_file):
        if line == 'null\n':
            trees.append(None)
            continue
        if line == '\n':
            trees.append(Tree.read(text))
            text = ''
            continue
        text += line
    count_file = '../test_suites_v2/%s/%s_tagAsParent_rules_grouped.txt' % (
            category, category)
    counts = []
    i = 0
    for line in open(count_file):
        count, pattern, _ = line.split('\t')
        counts.append(int(count))
        # TODO: this could be better - like check the annotation file to be
        # sure that the patterns match
        patterns.append(pattern)
        i += 1
    if len(counts) != len(trees):
        print 'Error! Incorrect alignment between trees and counts:'
        print len(counts), len(trees)
        exit(-1)
    # 'count' is token count, 'num' is type count
    total_count = 0
    count_annotated = 0
    count_correct = 0
    num_patterns = 0
    num_annotated = 0
    num_correct = 0
    errors = []
    correct = []
    for i, tree in enumerate(trees):
        pattern = patterns[i]
        num_patterns += 1
        total_count += counts[i]
        index = i + 1
        if index not in annotations: continue
        num_annotated += 1
        count_annotated += counts[i]

        # Clear off some of the extra processing that the SUPA pipeline adds
        if tree is None:
            continue
        root = tree.root.children[1]
        head = root.label.split('__', 1)[1]
        head_index = -1

        # The labels are on trees that haven't had WH-movement undone - we have
        # to correct the annotations for that.  This isn't perfect, but it will
        # do for now.
        annotated_children = len(pattern.split()) - 1
        actual_children = len(root.children)
        is_conjpp = 'CONJPP' in [x.label.split('__')[0] for x in root.children]
        if (not is_conjpp
                and actual_children == annotated_children - 1
                and annotations[index] != 0):
            annotations[index] = annotations[index] - 1

        # Now to actually check to see what was labeled as the head
        for j, child in enumerate(root.children):
            child_head = child.label.split('__', 1)[1]
            if child_head == head:
                head_index = j + 1
                if j == annotations[index]:
                    correct.append(patterns[i])
                    num_correct += 1
                    count_correct += counts[i]
                    break
        else:
            errors.append((patterns[i], head_index, annotations[index]+1))
    percent_tested = num_annotated / num_patterns
    percent_correct = num_correct / num_annotated
    count_percent_annotated = count_annotated / total_count
    count_percent_correct = count_correct / count_annotated
    out = open(outfile, 'a')
    out.write('%s\t%d\t%d\t%.3f\t%d\t%.3f\t%d\t%d\t%.3f\t%d\t%.3f\n' % (
            category, num_patterns, num_annotated, percent_tested, num_correct,
            percent_correct, total_count, count_annotated,
            count_percent_annotated, count_correct, count_percent_correct))
    error_file = open('results/errors_%s.tsv' % category, 'w')
    error_file.write('pattern\tpredicted\tactual\n')
    for error in errors:
        error_file.write('%s\t%d\t%d\n' % error);
    correct_file = open('results/correct_%s.tsv' % category, 'w')
    for pattern in correct:
        correct_file.write('%s\n' % pattern);
Esempio n. 35
0
    rightHeight = heightAlt(tree_root.r)
    if rightHeight == -1:
        return -1
    height_diff = abs(rightHeight - leftHeight)
    if height_diff > 1:
        return -1
    else:
        return max(leftHeight, rightHeight) + 1

def isBalancedAlt(tree_node):
    if heightAlt(tree_node) == -1:
        return False
    return True

if t1:
    x = Tree()
    x.add(5)
    x.add(3)
    x.add(2)
    x.add(4)
    print isBalancedAlt(x.root)
    y = Tree()
    y.add(4)
    y.add(3)
    y.add(5)
    y.add(6)
    print isBalancedAlt(y.root)

def searchDFS(graph, start):
    visited = []
    stack = Stack()
Esempio n. 36
0
from trees.util import plot_tree
from trees import Tree, TreeNode, TreeLeaf
import matplotlib.pyplot as plt

if __name__ == "__main__":
    leaf1 = TreeLeaf(1)
    leaf2 = TreeLeaf(2)
    leaf3 = TreeLeaf(3)
    node1 = TreeNode()
    node1.add_child(leaf1)
    node1.add_child(leaf2)
    node2 = TreeNode()
    node2.add_child(node1)
    node2.add_child(leaf3)
    tree = Tree(node2)

    plot_tree(tree)
    plt.show()

    p = leaf1.detach()
    plot_tree(tree)
    plt.show()

    leaf3.attach(p)
    plot_tree(tree)
    plt.show()