def normalize_path(buf): buf['output']=[] item_number = 1 root = anytree.Node('root') level = {} level[0] = root #skip the first line iter_input = iter(buf['input'].rows) next(iter_input) # A Tree struct to handle this part for row in iter_input: none_count=0 for data in row: if data.value == None: none_count+=1 else: value = str(data.value) if value.rfind('(') > 0: value = value[:value.rfind('(')-1] node = anytree.Node(value, parent=level[none_count-1]) level[none_count]=node break for content in root.leaves: output = [] output.append(item_number) output.append(str(content)[12:-2]) buf['output'].append(output) item_number+=1
def test_render_str(): """Render string cast.""" root = anytree.Node("root") s0 = anytree.Node("sub0", parent=root) anytree.Node("sub0B", parent=s0) anytree.Node("sub0A", parent=s0) anytree.Node("sub1", parent=root) r = anytree.RenderTree(root) expected = u"\n".join([ u"Node('/root')", u"├── Node('/root/sub0')", u"│ ├── Node('/root/sub0/sub0B')", u"│ └── Node('/root/sub0/sub0A')", u"└── Node('/root/sub1')", ]) eq_str(str(r), expected) r = anytree.RenderTree( root, childiter=lambda nodes: [n for n in nodes if len(n.name) < 5]) expected = u"\n".join([ u"Node('/root')", u"├── Node('/root/sub0')", u"└── Node('/root/sub1')", ]) eq_str(str(r), expected)
def get_complete_candidates(self): """ Generates dependency-candidates based on a strategy trying to find all dependencie. The top-down approach starts with a maximal lhs at level 1. With each additional level, columns are dropped until a minimal lhs is reached. """ self.top_down_convergence = True most_recent_nodes = self.get_newest_children() for node in most_recent_nodes: if self.is_continuous: if (node.score < node.parent.score) and (len(node.name) > 1): self.top_down_convergence = False pot_lhs = node.name for col in pot_lhs: tree.Node( [c for c in pot_lhs if c != col], parent=node, score=None) elif not self.is_continuous: if (node.score > node.parent.score*0.98) \ and (len(node.name) > 1): self.top_down_convergence = False pot_lhs = node.name for col in pot_lhs: tree.Node( [c for c in pot_lhs if c != col], parent=node, score=None)
def build_knowledge_tree(self): # 创建一个空白的知识树,最初它只有一个入口 ktree_inport = anytree.Node('[# 入口 #]') # 创建一个空白的待处理卡片队列 todo_cards = [] # 获取卡片库的左边界和右边界 leftedge_cards_dict = self.get_leftedge_cards() rightedge_cards_dict = self.get_rightedge_cards() for leftedge_card_name in leftedge_cards_dict.keys(): new_node1 = anytree.Node( \ self.get_card(leftedge_card_name).get('display_name'), ktree_inport) todo_cards.append((leftedge_card_name, new_node1)) while (todo_cards): todo_card = todo_cards[0] todo_cards = todo_cards[1:] after_cardnames = self.get_after_cards(todo_card[0]) for after_cardname in after_cardnames: new_node2 = anytree.Node( self.get_card(after_cardname).get('display_name'), todo_card[1]) if after_cardname in rightedge_cards_dict.keys(): anytree.Node('[# 出口 #]', new_node2) else: pass todo_cards.append((after_cardname, new_node2)) return ktree_inport
def treeify(alist, lecture): """ Turns concepts defined in alist into a tree, returns the top node """ top = anytree.Node(lecture) nodes = {} names = [] depth = finddepth(alist) for s in alist: st = s bef, aft = before_slash(st) if bef not in names: nodes['n{0}'.format(bef)] = anytree.Node(bef, parent=top) names.append(bef) for d in range(depth - 1): bef, aft = before_slash(st) aft2 = before_slash(aft)[0] if aft2 not in names and len(aft2) != 0: nodes['n{0}'.format(aft2)] = anytree.Node( aft2, parent=nodes['n{0}'.format(bef)]) names.append(aft2) if len(aft) != 0: st = aft else: break return top
def merkle_tree(hashes): if len(hashes) == 0: return None # It is a bit of a special case, you'd expect the len == 1 case to be # handled the same as the len % 2 == 1 case. if len(hashes) == 1: return anytree.Node(name=hashes[0]) leaves = [anytree.Node(name=hash_) for hash_ in hashes] def next_layer(nodes): layer = [] if len(nodes) % 2 == 1: nodes.append(anytree.Node(name=nodes[-1].name)) while len(nodes) != 0: first = nodes.pop(0) second = nodes.pop(0) parent = anytree.Node(name=hashlib.sha256( hashlib.sha256(first.name + second.name).digest()).digest()) first.parent = parent second.parent = parent layer.append(parent) return layer while len(leaves) != 1: leaves = next_layer(leaves) return leaves[0]
def explode(in_tree): leaves = [ node for node in anytree.PreOrderIter(in_tree, filter_=lambda n: n.name != '') ] for i in range(len(leaves)): l = leaves[i] if l.depth > 4: next_l = leaves[i + 1] if i > 0: leaves[i - 1].name += l.name zero_node = anytree.Node(name=0) else: l.name = 0 if i + 1 != len(leaves) - 1: leaves[i + 2].name += next_l.name else: next_l.name = 0 parent = l.parent zero_node = anytree.Node(name=0) l.parent.parent.children = [ zero_node if c == parent else c for c in l.parent.parent.children ] return True return False
def _build_gate(self, parent, left, gate_type, right): """ Ensures the parent's left wire is the left children's output wire and the parent's right wire is the right children's output wire. :param Node parent: parent of the current node :param str left: left expression to parse :param str gate_type: type of gate (AND, XOR...) :param str right: righr expression to parse :return: the node as used in the package `anytree` that holds the :class:`Gate`. :rtype: :class:`anytree.Node` """ is_left_leaf = len(left.split()) == 1 is_right_leaf = len(right.split()) == 1 gate = Gate(gate_type, create_left=is_left_leaf, create_right=is_right_leaf) if parent: node = anytree.Node(gate, parent=parent) else: node = anytree.Node(gate) if parent: if parent.children[0] == node: parent.name.left_wire = gate.output_wire elif parent.children[1] == node: parent.name.right_wire = gate.output_wire return node
def test_akm_distance_child(self): label_1 = 'root' label_2 = 'leaf' t = tree.Node(label_1, children=[tree.Node(label_2)]) self.assertEqual(1, akm_distance(label_1, label_2, tree=t))
def org_unit_tree_creator(n, a, b): creator = OrgUnitNameCreator() root = tree.Node(OrgUnit(0, creator.generate_name())) leaf_nodes = [root] ou_id = 1 while n > 0: # select random leaf node # TODO: Tends to pick from large clusters selection = 0 if len(leaf_nodes) > 1: dist = get_truncated_normal(2, sd=b // 2, low=0, upp=len(leaf_nodes) - 1) # Selects the closest # next_parent_node = leaf_nodes.pop(random.randint(0, len(leaf_nodes) - 1)) selection = int(dist.rvs(1)[0]) next_parent_node = leaf_nodes.pop(selection) # select number of new divisions number_of_divisions = min( random.randint(a, b) if next_parent_node.depth > 0 else b, n) for i in range(number_of_divisions): leaf_nodes.append( tree.Node(OrgUnit(ou_id, creator.generate_name(next_parent_node)), parent=next_parent_node)) ou_id += 1 n -= number_of_divisions return root
def find_subtree_spanning_leaves(leaves): """ Creates the smallest subtree containing given leaf nodes. :param leaves: leaf nodes nodes of a tree. Leaves must belong to the same tree. :return: the root of the subtree found. """ new_nodes = {} for leave in leaves: for node in reversed(leave.path): id = node.id parent = node.parent if not parent is None: parent_id = parent.id new_parent = new_nodes.get(parent_id, None) if new_parent is None: new_parent = t.Node('', id=parent_id) new_nodes[parent_id] = new_parent new_node = new_nodes.get(id, None) if new_node is None: new_node = t.Node('', id=id, parent=new_parent) new_nodes[id] = new_node else: if parent is None: subtree_root = new_node else: new_node.parent = new_parent new_node.name = node.name return subtree_root
def build_tree(entries): """ Builds a tree from a list of entries. :param entries: list of entries, where each entry is a dictionary with the following fields: id, name, and parentId. :return: a tree """ root = t.Node('root', my_name='root', id=-1) nodes = {} for e in tqdm(entries, desc='Building tree'): id = e['id'] parent_id = e.get('parentId', None) if parent_id is None: parent = root else: parent = nodes.get(parent_id, None) if parent is None: parent = t.Node('', id=parent_id) nodes[parent_id] = parent assert not parent is None node = nodes.get(id, None) if node is None: node = t.Node('', parent=parent, id=id) nodes[id] = node else: if node.parent is None: node.parent = parent node.name = e['name'] return root
def includes(self, sources, *flags): root = anytree.Node('root', header=Header()) headers = {} for source in sources: sroot = anytree.Node(source, parent=root, header=Header()) stack = [] seen = set() for l in self.stderr(source, '-E', '-H', *flags).split('\n'): # invalid precompiled header file is printed with ‘...x’ # and a valid one with ‘...!’ m = re.search(r'^([.]+) (.+)', l.strip()) if not m: continue level = len(m.group(1)) path = m.group(2) while stack and stack[-1][0] >= level: stack.pop() if path not in headers: headers[path] = Header() if path not in seen: headers[path].count += 1 seen.add(path) node = anytree.Node(m.group(2), parent=stack[-1][1] if stack else sroot, header=headers[path]) stack.append((level, node)) return root, headers
def test_get(): """Get.""" top = at.Node("top", parent=None) sub0 = at.Node("sub0", parent=top) sub0sub0 = at.Node("sub0sub0", parent=sub0) sub0sub1 = at.Node("sub0sub1", parent=sub0) sub1 = at.Node("sub1", parent=top) r = at.Resolver('name') eq_(r.get(top, "sub0/sub0sub0"), sub0sub0) eq_(r.get(sub1, ".."), top) eq_(r.get(sub1, "../"), top) eq_(r.get(sub1, "../."), top) eq_(r.get(sub1, "../sub0/sub0sub1"), sub0sub1) eq_(r.get(sub1, "."), sub1) eq_(r.get(sub1, ""), sub1) with assert_raises( at.ChildResolverError, "Node('/top') has no child sub2. Children are: 'sub0', 'sub1'."): r.get(top, "sub2") eq_(r.get(sub0sub0, "/top"), top) eq_(r.get(sub0sub0, "/top/sub0"), sub0) with assert_raises(at.RootResolverError, "Cannot go above root node Node('/top')"): r.get(top, "..") with assert_raises(at.ResolverError, "root node missing. root is '/top'."): r.get(sub0sub0, "/") with assert_raises(at.ResolverError, "unknown root node '/bar'. root is '/top'."): r.get(sub0sub0, "/bar")
def gen_hierarchy_from_clusters(args, clusters): """ Organize clusters into hierarchy Args: clusters: linkage matrix (num_features-1 X 4) rows indicate successive clustering iterations columns, respectively: 1st cluster index, 2nd cluster index, distance, sample count Returns: hierarchy_root: root of resulting hierarchy over features """ # Generate hierarchy from clusters nodes = [ anytree.Node(str(idx), static_indices=str(idx)) for idx in range(args.num_features) ] for idx, cluster in enumerate(clusters): cluster_idx = idx + args.num_features left_idx, right_idx, _, _ = cluster left_idx = int(left_idx) right_idx = int(right_idx) cluster_node = anytree.Node(str(cluster_idx)) nodes[left_idx].parent = cluster_node nodes[right_idx].parent = cluster_node nodes.append(cluster_node) hierarchy_root = nodes[-1] return hierarchy_root
def pretty_print_help(self, root, visual=False, parent=None): """ Pretty prints the tree. - root is the highest-level node you wish to start printing - [visual] controls whether a png should be created, by default, this is false. - [parent] is an optional parameter specifying the parent of a given node, should only be used by this function. Returns the root with its children as an anytree node. """ if not root: return None if visual: newroot = anytree.Node(str(root) + "(" + str(root.ident) + ")", parent=parent) else: newroot = anytree.Node(str(root), parent=parent) if root.left: newroot.left = self.pretty_print_help(root.left, visual, parent=newroot) else: if not root.terminal: # Drop never sends packets newroot.left = anytree.Node(' ===> ', parent=newroot) if root.right: newroot.right = self.pretty_print_help(root.right, visual, parent=newroot) else: if (not root.terminal and root.branching): # Tamper only has one child newroot.right = anytree.Node(' ===> ', parent=newroot) return newroot
def test_same_name(): """Same Name.""" root = at.Node("root") sub0 = at.Node("sub", parent=root) sub1 = at.Node("sub", parent=root) r = at.Resolver() eq_(r.get(root, "sub"), sub0) eq_(r.glob(root, "sub"), [sub0, sub1])
def query_parse(filepath): dirpath = os.path.dirname(filepath) filepre = os.path.splitext(os.path.basename(filepath))[0] tokpath = os.path.join(dirpath, filepre + '.toks') parentpath = os.path.join(dirpath, filepre + '.parents') with open(filepath) as datafile, \ open(tokpath, 'w') as tokfile, \ open(parentpath, 'w') as parentfile: for line in tqdm(datafile): clauses = line.split(" .") vars = dict() root = None for clause in clauses: triple = [item.replace("\n", "") for item in clause.split(" ")] root_node = anytree.Node(triple[1]) left_node = anytree.Node(triple[0], root_node) right_node = anytree.Node(triple[2], root_node) leveled = [left_node, root_node, right_node] for item in triple: if item.startswith("?u_"): if item in vars: children = vars[item].parent.children if children[0] == vars[item]: vars[item].parent.children = [ root_node, children[1] ] else: vars[item].parent.children = [ children[0], root_node ] vars[item] = [ node for node in leveled if node.name == item ][0] break else: vars[item] = [ node for node in leveled if node.name == item ][0] if root is None: root = root_node pre_order = [node for node in anytree.iterators.PreOrderIter(root)] tokens = [node.name for node in pre_order] for i in range(len(pre_order)): pre_order[i].index = i + 1 idxs = [ node.parent.index if node.parent is not None else 0 for node in pre_order ] tokfile.write(" ".join(tokens) + "\n") parentfile.write(" ".join(map(str, idxs)) + "\n")
def test_by_attr(): """by attr.""" root = anytree.Node("root", lines=["root"]) s0 = anytree.Node("sub0", parent=root, lines=["su", "b0"]) anytree.Node("sub0B", parent=s0, lines=["sub", "0B"]) anytree.Node("sub0A", parent=s0) anytree.Node("sub1", parent=root, lines=["sub1"]) eq_(anytree.RenderTree(root).by_attr(), u"root\n├── sub0\n│ ├── sub0B\n│ └── sub0A\n└── sub1") eq_(anytree.RenderTree(root).by_attr("lines"), u"root\n├── su\n│ b0\n│ ├── sub\n│ │ 0B\n│ └── \n└── sub1")
def _build_tree(self, anytree_node_list, i): if (i <= self.get_max_depth()) and len(anytree_node_list): splited_node_list = [] for anytree_node in anytree_node_list: my_node = anytree_node.name if my_node.get_elements_count() > 1: left, right = my_node.generate_2nodes_from_split() l_node = anytree.Node(left, parent=anytree_node) r_node = anytree.Node(right, parent=anytree_node) splited_node_list.extend([l_node, r_node]) self._build_tree(splited_node_list.copy(), i + 1)
def test_calculate_weight(self): root = tree.Node('root') inner = tree.Node('inner', parent=root) leaf = tree.Node('leaf', parent=inner) path = [Edge(parent=root, child=leaf), Edge(parent=inner, child=leaf)] weight = sum([calculate_weight(edge, tau=2.0) for edge in path]) # Answer is: w(root, inner) + w(inner, leaf) = 1.5 # w(root, inner) = 1 / (log(|child(root)|) + 1 = 1 # w(inner, leaf) = 1/2 * w(root, inner) / (log|child(x)| + 1) = 0.5 self.assertEqual(1.5, weight)
def test_calculate_path_different_branch(self): root = tree.Node('root') left_inner = tree.Node('left_inner', parent=root) left_leaf = tree.Node('left_leaf', parent=left_inner) right_inner = tree.Node('right_inner', parent=root) right_leaf = tree.Node('right_child', parent=right_inner) path = calculate_path(left_leaf, right_leaf) self.assertEqual(4, len(path)) self.assertEqual(left_leaf, path[0].child) self.assertEqual(right_leaf, path[-1].child)
def move(self, board, return_move): moves = board.get_moves(self.color) if self.tree is None: self.tree = anytree.Node("root", children=[], board=deepcopy(board), move=None, wins=0, simulations=0, moves_left=moves, color=not self.color) else: self.tree = anytree.search.find_by_attr(self.tree, name="board", value=board, maxlevel=3) if self.tree is None: self.tree = anytree.Node("root", children=[], board=deepcopy(board), move=None, wins=0, simulations=0, moves_left=moves, color=not self.color) else: self.tree.parent = None start = time.time() i = 0 while time.time() - start < self.simulation_time: selected_node = self._selection() expansion_node = self._expansion(selected_node) result = self._simulation(deepcopy(expansion_node.board), expansion_node.color) self._back_propagation(expansion_node, result, 1) i += 1 if i % 100 == 0: print(i) for pre, _, node in anytree.RenderTree(self.tree, maxlevel=2): print("%s %s->(%s/%s --> %s%%)" % (pre, node.move, node.wins, node.simulations, node.wins / node.simulations)) best_node = None max_simulations = 0 for n in self.tree.children: if n.simulations > max_simulations: best_node = n max_simulations = n.simulations return_move.append(best_node.move) return
def test_render_repr(): """Render representation.""" root = anytree.Node("root") anytree.Node("sub", parent=root) r = anytree.RenderTree(root) if six.PY2: expected = ("RenderTree(Node('/root'), style=ContStyle(), " "childiter=<type 'list'>)") else: expected = ("RenderTree(Node('/root'), style=ContStyle(), " "childiter=<class 'list'>)") eq_(repr(r), expected)
def test_ignorecase(): """ Case insensitive resolver """ root = at.Node("root") sub0 = at.Node("sUB0", parent=root) sub1 = at.Node("sub1", parent=root) r = at.Resolver(ignorecase=True) eq_(r.get(root, "SUB0"), sub0) eq_(r.get(root, "sub0"), sub0) eq_(r.get(root, "sUB0"), sub0) eq_(r.glob(root, "SU*1"), [sub1]) eq_(r.glob(root, "/*/SU*1"), [sub1]) eq_(r.glob(sub0, "../*1"), [sub1])
def add_gate(self, gate): """ Add a gate to the gating strategy, see `gates` module. The gate ID must be unique in the gating strategy. :param gate: instance from a sub-class of the Gate class :return: None """ if not isinstance(gate, Gate): raise ValueError("gate must be a sub-class of the Gate class") parent_id = gate.parent if parent_id is None: parent_id = 'root' # TODO: check for uniqueness of gate ID + gate path combo matched_nodes = anytree.findall( self._gate_tree, filter_=lambda g_node: g_node.name == gate.id and g_node.parent. name == parent_id) if len(matched_nodes) != 0: raise KeyError("Gate ID '%s' is already defined" % gate.id) parent_id = gate.parent if parent_id is None: parent_node = self._gate_tree else: matching_nodes = anytree.search.findall_by_attr( self._gate_tree, parent_id) if len(matching_nodes) == 0: # TODO: could be in a quadrant gate raise ValueError( "Parent gate %s does not exist in the gating strategy" % parent_id) elif len(matching_nodes) > 1: raise ValueError( "Multiple gates exist matching parent ID %s, specify full gate path" % parent_id) parent_node = matching_nodes[0] node = anytree.Node(gate.id, parent=parent_node, gate=gate) # Quadrant gates need special handling to add their individual quadrants as children. # Other gates cannot have the main quadrant gate as a parent, they can only reference # the individual quadrants as parents. if isinstance(gate, fk_gates.QuadrantGate): for q_id, q in gate.quadrants.items(): anytree.Node(q_id, parent=node, gate=q)
def split(in_tree): leaves = [ node for node in anytree.PreOrderIter(in_tree, filter_=lambda n: n.name != '') ] for leaf in leaves: if leaf.name > 9: lval = math.floor(leaf.name / 2) rval = math.ceil(leaf.name / 2) leaf.name = '' anytree.Node(lval, parent=leaf) anytree.Node(rval, parent=leaf) return True return False
def _make_tree(self): self.tree_nodes = {} self.tree_root = anytree.Node("root") for variant, lineage in self.variant_to_lineage.items(): nodes_to_add = LineagePredictor._lineage_to_itself_plus_parents( lineage["name"]) for i, node_name in enumerate(nodes_to_add): if node_name in self.tree_nodes: continue parent = self.tree_root if i == 0 else self.tree_nodes[ nodes_to_add[i - 1]] new_node = anytree.Node(node_name, parent=parent) self.tree_nodes[node_name] = new_node
def searchNodes(self, eltern, kindNodes, metaNodes): self.nodeCount += 1 curNode = at.Node(self.nodeCount, parent=eltern) if kindNodes > 0: for i in range(kindNodes): self.it += 2 self.searchNodes(curNode, self.inputContents[self.it - 1], self.inputContents[self.it]) if metaNodes > 0: metaArr = [] for j in range(metaNodes): self.it += 1 metaArr.append(self.inputContents[self.it]) meta = at.Node("Metadata", parent=curNode, metadaten=metaArr) return
def add_population(self, population: Population): """ Add a new Population to this FileGroup. Parameters ---------- population: Population Returns ------- None Raises ------ DuplicatePopulationError Population already exists AssertionError Population is missing index """ if population.population_name in self.tree.keys(): err = f"Population with name '{population.population_name}' already exists" raise DuplicatePopulationError(err) assert population.index is not None, "Population index is empty" if population.n is None: population.n = len(population.index) self.populations.append(population) self.tree[population.population_name] = anytree.Node( name=population.population_name, parent=self.tree.get(population.parent))