def main(): with open("input.txt", "r+") as file: content = file.readlines() nodes = {} # create dict with all children per node for line in content: node_ids = line.replace('\n', '').split(')') node_id_parent = node_ids[0] node_id_child = node_ids[1] if not nodes.__contains__(node_id_parent): nodes[node_id_parent] = [node_id_child] else: nodes[node_id_parent].append(node_id_child) # Create first node root = Node('COM', None) # fill tree (root node only has one child) add_children(nodes, nodes['COM'][0], root) # Get relevant nodes node_you = search.find_by_attr(root, "YOU") node_san = search.find_by_attr(root, "SAN") # remove irrelevant tree above the nodes we're interested in for node in set(node_you.ancestors).intersection(set(node_san.ancestors)): node.parent = None # count amount of ancestors, root node of this subtree should be removed in each list, so subtract two. print(node_you.depth - 1 + node_san.depth - 1)
def makeTree(CUIset, ICD10_bool): #make a tree structure of all the CUI concepts and their ancestors root = Node('root', leavesCount=0, occurence=0, indexArray=None, idNode='root') for x in CUIset: conceptSet = UMLS[ x[2]] >> ICD10 #convert CUI to ICD10 to get hierarchical structure ancestors = [concept.ancestor_concepts() for concept in conceptSet ] #contains [[ICD10, ancestor1, ancestor2...]] if (len(ancestors) > 0): #sometimes concepts not recognized x.append(ancestors[0][0]) leaveExists = search.find_by_attr( root, ancestors[0][0].name) #search if leave is new for index, ancestor in reversed( list(enumerate(ancestors[0])) ): #first the highest ancestor to walk down path of this term name = ancestor.name searchNode = search.find_by_attr( root, name) # search if current node already exists if (searchNode): #node already exist searchNode.occurence += 1 if (not leaveExists ): # if there is a new leave then add leave count searchNode.leavesCount += 1 else: #new leave if (index == len(ancestors[0]) - 1): #last element in array, child of root parentNode = root vars()[name] = Node(name, parent=parentNode, leavesCount=1, occurence=1, indexArray=None, idNode=name) else: parentNode = ancestors[0][index + 1].name if ( index == 0 ): #if it is a leave add the index in de original array vars()[name] = Node(name, parent=vars()[parentNode], leavesCount=1, occurence=1, indexArray=x[0], idNode=name) else: vars()[name] = Node(name, parent=vars()[parentNode], leavesCount=1, occurence=1, indexArray=None, idNode=name) return (root, CUIset)
def loadData(self): for root, dirs, files in os.walk(self.search_dir): for file in files: place = (file.split("2018_passData_")[-1]).split("_")[0] with open(os.path.join(self.search_dir, file), "r") as f: reader = csv.DictReader(f) for row in reader: try: date = row["Date"] time_key = ["pm25"] time = ["00:00:00"] except: date = row["YY-MM-DD"] date = date[:4] + "-" + date[4:6] + "-" + date[6:8] time_key = [ " {:.1f}".format(t) for t in np.arange(0.5, 24.5, 1.0) ] time = [ "{:02d}:30:00".format(i) for i in range(24) ] date_node = search.find_by_attr(self.time_tree, name="date", value=date, maxlevel=2) if date_node is None: date_node = AnyNode(parent=self.time_tree, date=date) for t in zip(time, time_key): time_node = search.find_by_attr(date_node, name="time", value=t[0], maxlevel=2) if time_node is None: try: pm25 = float(row[t[1]]) except: pm25 = None time_node = AnyNode(parent=date_node, time=t[0]) place_node = search.find_by_attr(time_node, name="place", value=place, maxlevel=2) if place_node is None: place_node = AnyNode( parent=time_node, place=place, pm25=pm25, position=self.place_dict[place]) LOG.debug(RenderTree(self.time_tree)) with open("time_tree.txt", "w") as f: print(RenderTree(self.time_tree), file=f)
def printContent(root, tagName): print("Result from tag #"+tagName) if find_by_attr(root, tagName).is_leaf: print(tagName) else: tagChild = find_by_attr(root, tagName).children for contentNode in tagChild: #get position for formating output x = str(contentNode).rfind('/') y = str(contentNode).rfind('\'') contentData = str(contentNode) if contentNode.is_leaf: print(contentData[x+1: y]) else: printContent(contentNode, contentData[x+1: y])
def create_nodes_and_leafs(column): print('column: ', column) print() # print('row: ', row.name) # print('row: ', row) # tree_blueprint = df_tree_blueprint[column.name] # tree.children = [Node(name=) # row.where(tree_blueprint == "LEAF") # print() # last_added_node = Node( # name=row[0], parent=root, value=333) # print('last_added_node.children: ', last_added_node.children) # parent = root # def fill_branch_recursively(cell): # # print('parent: ', parent.children) # # BASE CASE: # if isinstance(cell, str): # if find_nodes(node=parent, # filter_=lambda node: node.name == cell) is None: # # LEAF # Node(name=cell, parent=parent, value=333) # else: # return fill_branch_recursively( # cell=next(tree_blueprint.values)) if column.name == columns_with_layers[0]: _ = [ Node(name=cell, parent=root, value=333) for cell in column.unique() ] else: col_index = columns_with_layers.index(column.name) - 1 # print('last_index: ', last_index) # fill_branch_recursively(cell=tree_blueprint[0]) for cell in column.unique(): # .where(tree_blueprint == "NODE"): if isinstance(cell, str): row_index = column[column == cell].index[0] parent = df.iloc[row_index, col_index] f = find_by_attr(tree, parent) print('f: ', f) print('parent: ', parent) # s = df[columns_with_layers[last_index]] # df.query( == "foo"') # print('s: ', s) # print(s.loc[df[''].isin(['one','three'])]) Node(name=cell, parent=f, value=333)
async def search(self, attr, value): """Searches the servers tag tree for the specified attribute/value pair. Typically node ID.""" logging.info("Searching %s for %s", attr, value) node = search.find_by_attr(self, name=attr, value=value) logging.info("node: %s", node) return node
def initTag(root): #init ingredients of tree from root print("===Tags Defining===") print("Please enter the Tag and its Parent\n") while True: inputTagParent = input("Enter Tag Parent : ") inputTag = input("Enter Tag : ") currentTag = find_by_attr(root, inputTagParent) if currentTag == None: tagParent = Node(inputTagParent, parent=root) tagName = Node(inputTag, parent=tagParent) else: tagName = Node(inputTag, parent=find_by_attr(root, inputTagParent)) print("Press any key except '0' to continue enter tags") if input("Your choice : ") == "0": break
def organize_tree(self, all_terms, root): """Organizes a set of Tree node objects into a tree, according to their ancestors and children Args: all_terms(dict): a dictionary with "term_name" as keys and term_dict as values root(anytree.Node) Returns root(anytree.Node): the updated root node of the tree """ # Move tree nodes in the right position according to the ontology for key, term in all_terms.items(): ancestors = term["ancestors"] if len(ancestors) == 0: continue for ancestor in ancestors: ancestor_node = search.find_by_attr(root, ancestor) if ancestor_node is None: # It's probably the term on the top continue node = search.find_by_attr(root, key) node.parent = ancestor_node return root
def search_in_tree(tree_wnid, wnid): file_name = "tree_" + tree_wnid root = cPickle.load(open(file_name, "rb")) result = search.find_by_attr(root, wnid) w = Walker() walked = [w.walk(root, result)] list_nodes = [walked[0][1].name] for i in walked[0][2]: list_nodes.append(i.name) return list_nodes
def get_sub_labels(label): root = parse_json("labels.json") try: labels = [label] node = search.find_by_attr(root, name="name", value=label) if not node: print(labels) return labels #node = search.findall(root, filter_= lambda node: label in node.name,maxcount=1) labels = recurse_children(labels,node.children) print(labels) return labels except: print("Label " + label + " not found.")
def resolve_tenant_component(self, t): if t.name == '_root': return component_parent = t.parent.components if t.parent.name != '_root' else {} for name, c in t.components.items(): if isinstance(c, NodeBase): continue fro = c.pop('from', None) t.components[name] = c = Component(self, t.name, name, c) if fro is None: if name in component_parent: c.parent = component_parent[name] else: p = find_by_attr(self.component_tree, name, name='id') c.parent = p if p else self.component_tree continue parent = find_by_attr(self.tenant_tree, fro, name='id') if parent is None: raise KeyError('Unknown tenant %s' % fro) while True: if parent.name == '_root': raise KeyError('Unknown component %s in tenant %s' % (name, fro)) if name not in parent.components: parent = parent.parent continue if not isinstance(parent.components[name], NodeBase): fro = parent.components[name].pop('from', None) parent.components[name] = Component( self, t.name, name, parent.components[name]) if fro is not None: raise ValueError( 'Not implemented - todo: lazy loading') c.parent = parent.components[name] break
def generate_var_table(self): var_obj_rows = [] declarations = findall_by_attr(self.root, "declaracao_variaveis") for list_var in declarations: _type = find_by_attr(list_var, "tipo").child().name var_list = findall_by_attr(list_var, "var") for var in var_list: if var.parent.name == "fator": break row = Row() try: one_plus_d = var.child() row.token = one_plus_d.name row.lexeme = one_plus_d.child().name row.type = _type factors = findall_by_attr((var.children)[1], "fator") row.dim = len(factors) row.tam_dim1 = factors[0].child().child().child().name row.tam_dim2 = 0 if row.dim == 2: row.tam_dim2 = factors[1].child().child().child().name row.scope = one_plus_d.scope row.line = one_plus_d.child().line except IndexError: one_d = var.child() row.token = one_d.name row.lexeme = one_d.child().name row.type = _type row.dim = 0 row.tam_dim1 = 1 row.tam_dim2 = 0 row.scope = one_d.scope row.line = one_d.child().line var_obj_rows.append(row) var_obj_rows = var_obj_rows + self.param_vars() var_obj_rows = [list((o.__dict__).values()) for o in var_obj_rows] var_table = pd.DataFrame(data=var_obj_rows, columns=[ "Token", "Lexema", "Tipo", "dim", "tam_dim1", "tam_dim2", "escopo", "linha" ]) print(var_table.to_markdown()) return var_table
def create_nodes_and_leafs(self, column: pd.Series): # Treat first layer differently if column.name == self.layer_columns[0]: col_index = self.layer_columns.index(column.name) for cell in column.unique(): row_index = column[column == cell].index[0] self.add_data_to_leaf(node=TreeNode( name=cell, parent=self.tree, ), cell=cell, row_index=row_index, col_index=col_index) else: col_index = self.layer_columns.index(column.name) - 1 for cell in column.unique(): if isinstance(cell, str): row_index = column[column == cell].index[0] self.add_data_to_leaf(node=TreeNode( name=cell, parent=find_by_attr( node=self.tree, value=df.iloc[row_index, col_index], )), cell=cell, row_index=row_index, col_index=col_index + 1)
def build_tree(items, root, ctor, get_from): while items: stack = [items.popitem()] while True: if not isinstance(stack[-1], tuple): break fro = get_from(stack[-1][1]) if fro is None: break existing = find_by_attr(root, fro, name='id') if existing is None: fro_val = items.pop(fro, None) if fro_val is None: raise KeyError('Unknown node %s' % fro) stack.append((fro, fro_val)) else: stack.append(existing) parent = root while stack: x = stack.pop() if not isinstance(x, NodeMixin): x = ctor(x[0], x[1]) x.parent = parent parent = x
async def search(self, attr, value): logging.info("Searching {0} for {1}".format(attr, value)) node = search.find_by_attr(self, name=attr, value=value) logging.info("node: {0}".format(node)) return node
def tenant(self, k): return find_by_attr(self.tenant_tree, k, name='id')
def search_node_by_name(self, name): return search.find_by_attr(self.root, value=name, name='name')
def whether_in_dic(root, word): flag = False if search.find_by_attr(root, word): flag = True return flag
def create_nodes_and_leafs(self, column: pd.Series): # First layer behaves differently if column.name == self.layer_columns[0]: # Get the column index of the passed column col_index = self.layer_columns.index(column.name) # Iter over unique cells in column for cell in column.unique(): # If the cell row_index = column[column == cell].index[0] # Add a new node to the root of the tree new_node = TreeNode( name=cell, # Parent of first layer is always the root (here: # self.tree) parent=self.tree, ) # Common arguments for all nodes including leafs # Make a key value pair and let each cell know to which # layer it relates setattr(new_node, "layer_id", int(column.name.split("_")[1])) # setattr(new_node, "description", # df["description"].iloc[row_index]) # Private arguments for leaf, no other nodes # In case this cell is a leaf, add any given attached data self.add_infos_to_leaf(node=new_node, cell=cell, row_index=row_index, col_index=col_index) else: col_index = self.layer_columns.index(column.name) - 1 for cell in column.unique(): # Exclude NaN cells? # TODO: Check for behaviour for numbers # NOTE: Why not checking the if condition above? if isinstance(cell, str): # TODO: Check if that works with just column.index[0] row_index = column[column == cell].index[0] # Add a new node to the root of the tree new_node = TreeNode( name=cell, # Find the node's parent from the root to the current parent=find_by_attr( node=self.tree, value=self.df.iloc[row_index, col_index], )) # Common arguments for all nodes including leafs # Make a key value pair and let each cell know to which # layer it relates setattr(new_node, "layer_id", int(column.name.split("_")[1])) # Private arguments for leaf, no other nodes # In case this cell is a leaf, add any given attached data self.add_infos_to_leaf(node=new_node, cell=cell, row_index=row_index, col_index=col_index + 1)
def elements_from_sequence(ele_model, seq_model, part_tree): """ generate an element list from a sequential model """ if len(part_tree.root_node.children) == 0: # initialize part tree using the seq_model part_tree.init_from_sequence(seq_model) g_tfrms = seq_model.compute_global_coords(1) buried_reflector = False eles = [] path = seq_model.path() for i, seg in enumerate(path): ifc, g, rindx, tfrm, z_dir = seg if part_tree.parent_node(ifc) is None: g_tfrm = g_tfrms[i] if g is not None: if g.medium.name().lower() == 'air': num_eles = len(eles) if num_eles == 0: process_airgap( ele_model, seq_model, part_tree, i, g, z_dir, ifc, g_tfrm, add_ele=True) else: if buried_reflector is True: num_eles = num_eles//2 eles.append((i, ifc, g, z_dir, g_tfrm)) i, ifc, g, z_dir, g_tfrm = eles[1] if num_eles == 1: i1, s1, g1, z_dir1, g_tfrm1 = eles[0] sd = max(s1.surface_od(), ifc.surface_od()) e = elements.Element(s1, ifc, g1, sd=sd, tfrm=g_tfrm1, idx=i1, idx2=i) e_node = part_tree.add_element_to_tree(e, z_dir=z_dir1) ele_model.add_element(e) if buried_reflector is True: ifc2 = eles[-1][1] ifc_node = part_tree.node(ifc2) p_node = find_by_attr(e_node, name='name', value='p1') ifc_node.parent = p_node g1_node = part_tree.node((g1, z_dir1)) g_node = part_tree.node((g, z_dir)) g_node.parent = g1_node.parent # set up for airgap i, ifc, g, z_dir, g_tfrm = eles[-1] elif num_eles > 1: if not buried_reflector: eles.append((i, ifc, g, z_dir, g_tfrm)) e = elements.CementedElement(eles[:num_eles+1]) e_node = part_tree.add_element_to_tree(e, z_dir=z_dir) ele_model.add_element(e) if buried_reflector is True: for i, j in enumerate(range(-1, -num_eles-1, -1), start=1): ifc = eles[j][1] ifc_node = part_tree.node(ifc) pid = f'p{i}' p_node = find_by_attr(e_node, name='name', value=pid) ifc_node.parent = p_node g = eles[j-1][2] z_dir = eles[j-1][3] g_node = part_tree.node((g, z_dir)) tid = f't{i}' t_node = find_by_attr(e_node, name='name', value=tid) if g_node: g_node.parent = t_node # set up for airgap i, ifc, g, z_dir, g_tfrm = eles[-1] # add an AirGap ag = elements.AirGap(g, idx=i, tfrm=g_tfrm) ag_node = part_tree.add_element_to_tree(ag, z_dir=z_dir) ag_node.leaves[0].id = (g, z_dir) ele_model.add_element(ag) eles = [] buried_reflector = False else: # a non-air medium # handle buried mirror, e.g. prism or Mangin mirror if ifc.interact_mode == 'reflect': buried_reflector = True eles.append((i, ifc, g, z_dir, g_tfrm)) else: process_airgap(ele_model, seq_model, part_tree, i, g, z_dir, ifc, g_tfrm) # rename and tag the Image space airgap node = part_tree.parent_node((seq_model.gaps[-1], seq_model.z_dir[-1])) if node.name != 'Object space': node.name = node.id.label = 'Image space' node.tag += '#image' # sort the final tree by seq_model order part_tree.sort_using_sequence(seq_model)
def visualize_tree_from_dir(dirname, mode, filename=None, node_id=None): tree, node_to_class, node_to_classes, _ = load_tree(dirname) if node_id is not None: tree = find_by_attr(tree, node_id) return visualize_tree(tree, mode, node_to_class, node_to_classes, filename)
json_structures = [json.load(open(args.path + j)) for j in json_files] root = Node("root") top_level = [j for j in json_structures if j["parentId"] is None] for structure in top_level: node = AnyNode(id=structure["id"], parent=root, name=structure["name"], data=structure["documents"][0]["content"]) json_structures.remove(structure) index = 0 while (len(json_structures) > 0): structure = json_structures[index] found = find_by_attr(root, name="id", value=structure["parentId"]) if found: content = json_structures.pop(index)["documents"][0]["content"] content = REGEX_WRAP.findall(content.strip())[0] links = REGEX_LINK.finditer(content) for link in links: content = content.replace(link.group(0), "@JournalEntry[" + link.group(1) + "]") special = REGEX_SPEC.finditer(content) for spec in special: content = content.replace(spec.group(0), "") node = AnyNode(id=structure["id"], parent=found, name=structure["name"],
def choose_table(root, word): node = search.find_by_attr(root, word) b = util.commonancestors(node)[1].name return b
recurse(obj["children"], root) return root def recurse(obj, node): for child in obj: curr_node = Node(child["name"], parent=node) if "children" in child: recurse(child["children"], curr_node) root = parse_json("labels.json") df = pd.read_csv('labels.csv', squeeze=True) #add df elements to root where appropriate list = [] for idx, label in df.items(): node = search.find_by_attr(root, name="name", value=label) if not node: list.append(label) # with open('labels.csv','w', newline='') as csvfile: # spamwriter = csv.writer(csvfile, delimiter=' ', # escapechar = ' ', quoting=csv.QUOTE_NONE) # for label in list: # # print(label) # spamwriter.writerow([label]) #Exporting to JSON exporter = JsonExporter(indent=2, sort_keys=False) #exports to tree.json file fh = open("tree.json", "w") exporter.write(root, fh)
def node(self, obj): """ Return the node paired with `obj`. """ return find_by_attr(self.root_node, name='id', value=obj)
def modify(root, word): a = search.find_by_attr(root, word) return a.parent.children[0].name