def parse_tree(tree_file): f = file(tree_file, 'r') # tree file tre = tree.PQRTree() # PQR-tree tre._head._value = PQRTreeValue() tre._head._value._type = 'P' first = True # True if curr is the first node for buff in f: if buff[0] != '#' and buff[0] != '>': if first: tre._head._child = tree.TreeNode(PQRTreeValue()) curr = tre._head._child # current tree first = False else: curr._brother = tree.TreeNode(PQRTreeValue()) curr = curr._brother # current tree #endif a, s, i = parse_tree_rec(buff.split(), 0, curr) # aux, support, aux var tre._head._value._support |= s #endif #endfor f.close() return tre
def ConstructTree(compatible_splits, split_lengths, taxa_index, taxa_ordered, nTrees): # Progressively build the tree - Inspired by Day's algorithm (but I've not checked how similar) # Start from the leaves # t = ConstructTree(compatible_splits) nTrees = float(nTrees) t = tree.Tree() nodes_list = [] for i, taxon in enumerate(taxa_ordered): n = tree.TreeNode() n.name = taxon x = BitVector(taxa_index, taxon).Canonical() n.dist = sum(split_lengths[x]) / float(len(split_lengths[x])) nodes_list.append(n) t.add_child(n) compatible_splits = sorted(compatible_splits) # ascending # now nodes are in preorder # one = '1' for x, nSup in compatible_splits: # have already put leaves on: if bin(x).count("1") == 1: continue # 1's correspond to the cluster for the tree rooted on the first # Nodes are in the array: [0,1,2,3,4] # if cluster (2,3) is observed then -> [0, 1, (2,3), None, 4] # if cluster (2,3,4) is then observed then go to the first non-zero index for the node? # rep = bin(x) # the tree may be non-binary. We may add multiple child nodes at once iInsert = None for i, node in enumerate(nodes_list): if (x >> i) & 1 and node != None: # then this is one of the child nodes if iInsert == None: iInsert = i node_new = tree.TreeNode() node_new.support = nSup / nTrees node_new.dist = sum(split_lengths[x]) / float( len(split_lengths[x])) node_new.add_child(node.detach()) nodes_list[i] = node_new t.add_child(node_new) else: node_new.add_child(node.detach()) nodes_list[i] = None # print(t) t.unroot() return t
def sample_from_production(self, production, length): """ return a tree that starts with this production. """ #print "sample prod", production, " length ", length rhs = production[1] l = len(rhs) result = tree.TreeNode(production[0]) if l == 0: return result if l == 1: subtree = self.sample_from_nonterminal(rhs[0], length) result.daughters.append(subtree) return result one = rhs[0] two = rhs[1] total = self.index[production][length] if total == 0.0: raise ValueError("Sampling from empty") r = random.randrange(total) for i in xrange(length + 1): value = self.index[one][i] * self.index[two][length - i] #print "split ", i, (length - i), "value", value r -= value if r < 0: result.daughters.append(self.sample_from_nonterminal(one, i)) result.daughters.append( self.sample_from_nonterminal(two, length - i)) return self.flatten(result) raise ValueError("normalisation error?")
def preorder_tree(preorder, l_preorder, r_preorder, inorder, l_inorder, r_inorder, inorder_dict): if l_inorder > r_inorder: return None root = t.TreeNode(preorder[l_preorder]) left_size = inorder_dict[preorder[l_preorder]] - l_inorder root.left_child = preorder_tree(preorder, l_preorder + 1, l_preorder + left_size, inorder, l_inorder, l_inorder + left_size - 1, inorder_dict) root.right_child = preorder_tree(preorder, l_preorder + left_size + 1, r_preorder, inorder, l_inorder + left_size + 1, r_inorder, inorder_dict) return root
def add(t, fields, level): ''' add branch b to tree t ''' kids = t.get_children_as_dict() key = translate_level_to_key(fields, level + 1) if level == LOW_LEVEL: code = fields[CLASS_FIELD] label = fields[BRANCH_NAME_FIELD] kids[key] = tree.TreeNode(code, label, int(fields[DEPOSITS_FIELD])) else: if key not in kids: t = tree.TreeNode("", key, 0) kids[key] = t t = kids[key] add(t, fields, level + 1)
def parseProjectFile(self): with open(self.project_file) as f: self.projectSettingContent = f.read() projectPat = re.compile( r'/\*\sBegin\sPBXGroup\ssection\s\*/[\s\S]*/\*\sEnd\sPBXGroup\ssection\s\*/' ) # /* Begin PBXGroup section */ # /* End PBXGroup section */ rootNamePat = re.compile(r'/\*\s.+\s\*/\s=') # 9C4A634822E4828700037752 /* Products */ = { namePat = re.compile(r'/\*\s.+\s\*/') # /* TestForProjectParser.app */ nameIdPat = re.compile(r'[0-9A-Z]+\s/\*\s.+\s\*/') # 9C4A634722E4828700037752 /* TestForProjectParser.app */, idPat = re.compile(r'[0-9A-Z]+') # 9C4A634822E4828700037752 childrenPat = re.compile(r'children\s=\s([\s\S]*)') # children = ( # 9C4A634722E4828700037752 /* TestForProjectParser.app */, # 9C4A635F22E4828800037752 /* TestForProjectParserTests.xctest */, # 9C4A636A22E4828800037752 /* TestForProjectParserUITests.xctest */, # ); rootItem = re.findall( r'[0-9A-Z]+\s=\s{[^{}]*};', projectPat.findall(self.projectSettingContent)[0])[0] contentItemArray = re.findall( r'[0-9A-Z]+\s/\*\s.+\s\*/\s=\s{[^{}]*};', projectPat.findall(self.projectSettingContent)[0]) contentItemArray.insert(0, rootItem) # 插入根节点 for contentItem in contentItemArray: node = tree.TreeNode() node.md5 = idPat.findall(contentItem)[0] if len(rootNamePat.findall(contentItem)) > 0: nameSplit = rootNamePat.findall(contentItem)[0].split(' ') node.filename = nameSplit[1] if len(node.filename.split('.')) > 1: node.isLeafNode = True for children in nameIdPat.findall( childrenPat.findall(contentItem)[0]): childrenNode = self.nodeWithNameIDString(children) node.children.append(childrenNode) ''' 插入节点 ''' self.tree.insertNode(node) pass self.tree.generate()
def load_from_file(filename): ''' create a new dposit tree from given file filename ''' t = tree.TreeNode("", "", 0) with open(filename, 'rU') as csvfile: reader = csv.reader(csvfile) for fields in reader: add(t, fields, 0) return t
def nodeWithNameIDString(self, nameIdString): idPat = re.compile(r'[0-9A-Z]+') namePat = re.compile(r'/\*\s.+\s\*/') node = tree.TreeNode() node.md5 = idPat.findall(nameIdString)[0] if len(namePat.findall(nameIdString)) > 0: nameSplit = namePat.findall(nameIdString)[0].split(' ') node.filename = nameSplit[1] if len(node.filename.split('.')) > 1: node.isLeafNode = True return node
def copy(self): p = Partition(None) # the copy p._part = tree.TreeNode(copy.copy(self._part._value)) p._support = self._support c = self._part._brother # current node to copy d = p._part # node to copy to while c != None: d._brother = tree.TreeNode(copy.copy(c._value)) d._brother._last = d c = c._brother d = d._brother #endwhile p._end = d return p
def run_command(string, root: tree.TreeNode, current: tree.TreeNode): command = string.split(' ') function_name = command[0] if function_name == 'ls': if len(command) == 1: current.list_children() else: path = command[1].split('/') tmp = root.arrive_node(path) if tmp is None: print('No such path.') else: tmp.list_children() elif function_name == 'cd': if len(command) == 1: print('Path is needed.') else: path = command[1].split('/') tmp = root.arrive_node(path) if tmp is None: print('No such path.') else: current = tmp elif function_name == 'pwd': current.print_path() elif function_name == 'mkdir': if len(command) == 1: print('Path is needed.') else: current.add_child(tree.TreeNode(command[1])) elif function_name == 'touch': if len(command) == 1: print('Path is needed.') else: current.add_child(command[1]) elif function_name == 'rm': if len(command) == 1: print('Path is needed.') else: current.del_child(command[1]) elif function_name == 'tree': root.show_tree() else: print('Command not found.') return current
def WriteSpeciesTreeIDs_TwoThree(taxa, outFN): """ Get the unrooted species tree for two or three species Args: taxa - list of species names Returns: """ t = tree.Tree() for s in taxa: t.add_child(tree.TreeNode(name=s)) t.write(outfile=outFN)
def load_from_file(filename): ''' Input: a .csv file Returns: a TreeNode object ''' cs4all_schools = pd.read_csv(CS4ALL_SCHOOLS_FILE) cs4all_school_ids = cs4all_schools['school_id'].tolist() cs4all = tree.TreeNode('1', 'cs4all Schools', 0) other = tree.TreeNode('0', 'Other Schools', 0) cs4all_child = cs4all.get_children_as_dict() other_child = other.get_children_as_dict() with open(filename) as f: reader = csv.reader(f, skipinitialspace=True) header = next(reader) for row in reader: school = dict(zip(header, row)) if school['School Code'] in [ str(int(x)) for x in cs4all_school_ids ]: name = school['School Name'] count = school['Count'] weight = cs4all.weight + int(count) cs4all.weight = weight label = "%s (%s)" % (name, int(count)) cs4all_child[name] = tree.TreeNode('1', label, int(count)) else: name = school['School Name'] count = school['Count'] weight = other.weight + int(count) other.weight = weight label = "%s (%s)" % (name, int(count)) other_child[name] = tree.TreeNode('0', label, int(count)) t = tree.TreeNode('', '', cs4all.weight + other.weight) children = t.get_children_as_dict() children['cs4all'] = cs4all children['other'] = other return t
def add_category(t, code, partial_code, weight, code_to_label): ''' Add category to the tree recursively. Inputs: t: (TreeNode) a tree code: (string) represents the rest of the time use code to add. partial_code: (string) prefix of the time use code that leads from the root to t. weight: (float) minutes spent on the activity code_to_label: (dict) maps time use codes to time use category names. ''' kids = t.get_children_as_dict() if len(code) == 2: # tier 3 code. t is a tier 2 node and we should not # have seen this code at this node before. assert code not in t._children complete_code = partial_code + code label = code_to_label.get(complete_code, "") t = tree.TreeNode(complete_code, label, weight) kids[code] = t else: # tier 1 or tier 2 sub-code sub_code = code[:2] if sub_code not in kids: # need to add the child for the sub_code # interior node, initial weight is zero complete_code = partial_code + sub_code label = code_to_label.get(complete_code, "") t = tree.TreeNode(partial_code + sub_code, label, 0) kids[sub_code] = t else: t = kids[sub_code] # recursively add the rest of the time code. add_category(t, code[2:], partial_code + sub_code, weight, code_to_label)
def left_refine(self, row): node = self._part while node != self._end and node._value <= row._set: node = node._brother #endwhile x = node._value & row._set if len(x) > 0 and x != node._value: y = node._value - x node._value = y self.insert_before(tree.TreeNode(x), node)
def right_refine(self, row): node = self._end while node != self._part and node._value <= row._set: node = node._last #endwhile x = node._value & row._set if len(x) > 0 and x != node._value: y = node._value - x node._value = y self.insert_after(tree.TreeNode(x), node)
def GraftAndUpdate(top, n, s): """ Remove n from its location in the tree and make it sister to s. Update only the sp_down data. NOT the sp_up data. Args: nTop - node being reconcilled n - node to move s - node to make n sister to. Returns: The root of the tree Implementation: n and s could be anywhere in relation to one another, need to change all the species sets that could be affected """ n, nUp, top = DetachAndCleanup(top, n) parent = s.up if parent == None: new = tree_lib.TreeNode() new.add_feature("sp_up", set()) parent = new top = new new.add_child(n) s = s.detach() new.add_child(s) s.sp_up = n.sp_down else: new = parent.add_child() new.add_child(n) s = s.detach() new.add_child(s) # sort out distances, new node will have a default distance of 1.0, correct this new.dist = 0.1 * s.dist s.dist = 0.9 * s.dist # sort out sp_down data - can do a more efficient routine later if necessary (updating only what needs updating). Otherwise: # all nodes on path from nTop to s need sp_down updating new.add_feature("sp_down", s.sp_down.union(n.sp_down)) parent.sp_down = parent.sp_down.union(n.sp_down) if not parent == top: r = parent.up while r != top: r.sp_down = set.union(*[ch.sp_down for ch in r.get_children()]) r = r.up # all nodes on path from nTop to n need sp_down updating r = nUp if r != None: while r != top: r.sp_down = set.union(*[ch.sp_down for ch in r.get_children()]) r = r.up return new.get_tree_root()
def generateAllFromProduction(self, production, length): rhs = production[1] lhs = production[0] l = len(rhs) result = [] if l == 0: result.append(tree.TreeNode(lhs)) if l == 1: subtrees = self.generateAllFrom(rhs[0], length) for subtreet in subtrees: root = tree.TreeNode(lhs) result.daughters.append(subtree) if l == 2: one = rhs[0] two = rhs[1] for i in xrange(length + 1): value = self.index[one][i] * self.index[two][length - i] for subtree1 in self.generateAllFrom(one, i): for subtree2 in self.generateAllFrom(two, length - i): root = tree.TreeNode(lhs) root.daughters.append(subtree1) root.daughters.append(subtree2) results.append(self.flatten(root)) return result
def make_PQR_tree_from_graph(support, og): # add leaves for x in support: og.insert(0, c1p.OverlapComponent(bm.Row(set([x]), 'leaf', 0, [], False))) #endfor # create PQR-tree nodes nodes = [tree.TreeNode(g._support) for g in og] # the nodes of the PQR-tree # for each overlap graph make a node in the PQR-tree for g in xrange(len(og)): head = og[g]._head # the head of the current overlap graph # if there is only one node in the tree the component is a P node otherwise: if len(og[g]._rows) > 0: # refine to determine order or R'ness is_R = False # True if node is an R-node p = part.Partition(head) # partition of the # current overlap graph for row in og[g]._rows: if not p.refine(row): is_R = True break #endif #endfor if is_R: nodes[g]._value = 'R' else: nodes[g]._value = 'Q' nodes[g]._child = p._part #endif else: # P node if len(og[g]._support) == 1: nodes[g]._value = og[g]._support.__iter__().next() else: nodes[g]._value = 'P' #endif #endif #endfor return make_PQR_tree_from_partitions(support, nodes, og)
def sample_from_nonterminal(self, nonterminal, length): """ return a tree with length n, root is nonterminal. Pick a production and then sample from that. """ #print "sampling from ", nonterminal, "length ", length if nonterminal in self.grammar.terminals: if length == 1: return tree.TreeNode(nonterminal) else: raise ValueError("terminals are of length 1") total = self.index[nonterminal][length] if total == 0.0: raise ValueError("Sampling from empty " + nonterminal + " " + str(length)) r = random.randrange(total) for prod in self.prodindex[nonterminal]: r -= self.index[prod][length] if r < 0.0: return self.sample_from_production(prod, length) #print "Residue %f" % r raise ValueError("normalisation error?" + str(r))
def build_tree(atus_filename, participant, code_to_label): ''' Construct a tree for a specific participant or a subtree that participant. Inputs: atus_filename: (string) name of the participants data file participant: (integer) line number for the participant in the file Returns: a TreeNode ''' # construct the tree data = [row for row in csv.reader(open(atus_filename), delimiter=",")] header = data[0] t = tree.TreeNode("", "", 0) if participant >= len(data): print("Invalid participant number:", participant, file=sys.stderr) sys.exit(0) add(t, header, data[participant], code_to_label) return t
return call_tuples filename = "test.out" if len(sys.argv) > 1: filename = sys.argv[1] buf_len = 3 if len(sys.argv) > 2: buf_len = int(sys.argv[2]) syscall_file = open(filename, "r") instr_tree = tree.TreeNode("") total_count = 0 correct_preds = 0 incorrect_preds = 0 predicted_instr = "" for line in syscall_file: instrs = line.split("|") total_count += len(instrs) level = instr_tree
def join(self, p): if p._support <= self._support: # refine inside c = self._part # current class # find first intersection and direction while c != None: # if len(c._value[0] & p._part._value[0]) > 0: if len(c._value & p._part._value) > 0: # backwards # if len(c._value[0] & p._end._value[0]) > 0 and len(p._end._value[0]) == len(c._value[0] & p._end._value[0]): if len(c._value & p._end._value) > 0 and len( p._end._value) == len(c._value & p._end._value): direction = False # direction to iterate in d = p._end # 'start' of the refining partition # s = d._value[0] # class at the 'start' of the # # refining partition s = d._value # class at the 'start' of the # refining partition end = c # marks the class that will be the end # of this class once refined # forwards else: direction = True d = p._part # s = d._value[0] s = d._value end = c #endif break #backwards # elif len(c._value[0] & p._end._value[0]) > 0: elif len(c._value & p._end._value) > 0: direction = False d = p._end # s = d._value[0] s = d._value end = c break #endif c = c._brother #endwhile # refine the first intersection # while len(s & c._value[0]) > 0: while len(s & c._value) > 0: # x = tree.TreeNode([s & c._value[0], d._value[1] | c._value[1]]) # class to add x = tree.TreeNode(s & c._value) # class to add self.insert_after(x, end) end = x # s = s - x._value[0] s = s - x._value # c._value[0] = c._value[0] - x._value[0] c._value = c._value - x._value if len(s) > 0: break else: if direction: d = d._brother else: d = d._last #end if d != None: # s = d._value[0] s = d._value else: break #endif #endif #endwhile # remove empty partition # if len(c._value[0]) == 0: if len(c._value) == 0: if c._brother != None: c._brother._last = c._last #endif if c._last != None: c._last._brother = c._brother else: self._part = c._brother #endif #endif c = end._brother # refine the rest of partition while c != None and d != None: # while len(s & c._value[0]) > 0: while len(s & c._value) > 0: # x = tree.TreeNode([s & c._value[0], c._value[1] | d._value[1]]) # class to add x = tree.TreeNode(s & c._value) # class to add self.insert_before(x, c) # s = s - x._value[0] s = s - x._value # c._value[0] = c._value[0] - x._value[0] c._value = c._value - x._value if len(s) > 0: break else: if direction: d = d._brother else: d = d._last #end if d != None: # s = d._value[0] s = d._value else: break #endif #endif #endwhile # if d != None and len(c._value[0]) > 0: # gap found if d != None and len(c._value) > 0: # gap found return False else: # remove empty partition # if len(c._value[0]) == 0: if len(c._value) == 0: if c._last != None: c._last._brother = c._brother #endif if c._brother != None: c._brother._last = c._last else: self._end = c._last #endif #endif #endwhile c = c._brother #endwhile if d != None: return False #endif # elif self._support <= p._support: # refine outside # if not p.join(self): # return False # #endif # # self._part = p._part # self._end = p._end # elif len(self._part._value[0] & p._support): # add to start elif len(self._part._value & p._support): # add to start # flip if needed # if p._part._value[0] <= self._part._value[0]: if p._part._value <= self._part._value: p.flip() # elif len(self._part._value[0] & p._end._value[0]) == 0: elif len(self._part._value & p._end._value) == 0: return False # gap found #endif # refine the end of the joining partition c = p._end # current node # find full classes # while c != None and c._value[0] <= self._part._value[0]: while c != None and c._value <= self._part._value: # self._part._value[0] = self._part._value[0] - c._value[0] self._part._value = self._part._value - c._value # c._value[1] = c._value[1] | self._part._value[1] c = c._last #endif # split last class # if len(c._value[0] & self._part._value[0]) > 0: if len(c._value & self._part._value) > 0: # x = c._value[0] & self._part._value[0] # class to add x = c._value & self._part._value # class to add # self._part._value[0] = self._part._value[0] - x self._part._value = self._part._value - x # c._value[0] = c._value[0] - x c._value = c._value - x # p.insert_after(tree.TreeNode([x, c._value[1] | self._part._value[1]]), c) p.insert_after(tree.TreeNode(x), c) #endif # if len(p._support & self._part._value[0]) > 0: if len(p._support & self._part._value) > 0: return False # gap found #endif # attach partition to start # if len(self._part._value[0]) == 0: if len(self._part._value) == 0: p._end._brother = self._part._brother self._part._brother._last = p._end else: p._end._brother = self._part self._part._last = p._end #endif self._part = p._part # elif len(self._end._value[0] & p._support): # add to end elif len(self._end._value & p._support): # add to end # flip if needed # if p._end._value[0] <= self._end._value[0]: if p._end._value <= self._end._value: p.flip() # elif len(self._end._value[0] & p._part._value[0]) == 0: elif len(self._end._value & p._part._value) == 0: return False # gap found #endif # refine the end of the joining partition c = p._part # current node # find full classes # while c != None and c._value[0] <= self._end._value[0]: while c != None and c._value <= self._end._value: # self._end._value[0] = self._end._value[0] - c._value[0] self._end._value = self._end._value - c._value # c._value[1] = c._value[1] | self._end._value[1] c = c._brother #endif # split last class # if len(c._value[0] & self._end._value[0]) > 0: if len(c._value & self._end._value) > 0: # x = c._value[0] & self._end._value[0] # class to add x = c._value & self._end._value # class to add # self._end._value[0] = self._end._value[0] - x self._end._value = self._end._value - x # c._value[0] = c._value[0] - x c._value = c._value - x # p.insert_before(tree.TreeNode([x, c._value[1] | self._end._value[1]]), c) p.insert_before(tree.TreeNode(x), c) #endif # if len(p._support & self._end._value[0]) > 0: if len(p._support & self._end._value) > 0: return False # gap found #endif # attach partition to end # if len(self._end._value[0]) == 0: if len(self._end._value) == 0: p._part._last = self._end._last self._end._last._brother = p._part else: p._part._last = self._end self._end._brother = p._part #endif self._end = p._end else: # gap found return False #endif self._support = self._support | p._support return True
def __init__(self, r): if r != None: # self._part = tree.TreeNode([r._set, set([r])]) # head of partition self._part = tree.TreeNode(r._set) # head of partition self._end = self._part # tail of partition self._support = r._set # support of the partition
def parse_tree_rec(tre, i, current): current._value._type = tre[i][1] child = None # current child first = None last = None i += 1 # find end of node and next node end = tre.index(current._value._type + '_', i) # end of node n = next_node(tre, i) # index of next node while end > n and n > 0: markers = tre[i:n] # children between next node and current # add markers to support if len(markers) > 0: if first == None: first = set([int(markers[0])]) #endif current._value._support |= set([int(m) for m in markers]) #endif # make child and recurse it if child == None: current._child = tree.TreeNode(PQRTreeValue()) child = current._child else: child._brother = tree.TreeNode(PQRTreeValue()) child = child._brother #endif a, s, i = parse_tree_rec(tre, n, child) # child's support, current index if current._value._first == None: first = s #endif last = s current._value._support |= s # refind end and next node end = tre.index(current._value._type + '_', i) n = next_node(tre, i) #endwhile markers = tre[i:end] # children at end of node # add markers to support if len(markers) > 0: if first == None: first = set([int(markers[0])]) #endif last = set([int(markers[-1])]) if current._value._type != 'Q': current._value._support |= set([int(m) for m in markers]) #endif #endif if current._value._type == 'Q': current._value._allowed = first | last #endif return current._value._allowed, current._value._support, end + 1
def make_PQCR_tree_from_graph(support, og): # add leaves for x in support: og.insert(0, c1p.OverlapComponent(bm.Row(set([x]), 'leaf', 0, [], False))) #endfor # create PQR-tree nodes nodes = [tree.TreeNode(g._support) for g in og] # the nodes of the PQR-tree # for each overlap graph make a node in the PQR-tree for g in xrange(len(og)): head = og[g]._head # the head of the current overlap graph # if there is only one node in the tree the component is a P node otherwise: if len(og[g]._rows) > 0: # refine to determine order or R'ness is_R = False # True if node is an R-node p = part.Partition(head) # partition of the current overlap graph # print str(p) for row in og[g]._rows: if not p.refine(row): is_R = True break #endif #endfor if is_R: # check if node is circC1P for n in xrange(g+1, len(og) - 1): if og[g]._support < og[n]._support: if n != len(og) - 1: nodes[g]._value = 'R' #endif #endif #endfor if nodes[g]._value != 'R': m = bm.BinaryMatrix() for row in og[g]._all_rows: m.add_row_info(row) #endfor if cc1p.check_circC1P(m): p_comp = part.Partition(head) non_c1p = [] for row in og[g]._rows: if p_comp.test_refine(row._set) < 0: p_comp.refine(row) else: non_c1p.append(row) #endif #endif for row in non_c1p: p_comp.left_refine(row) p_comp.right_refine(row) if len(row._set - p_comp._support) > 0: p_comp.insert_after(tree.TreeNode(row._set - p_comp._support), p_comp._end) p_comp._support = p_comp._support | row._set #endif #endfor nodes[g]._value = 'C' nodes[g]._child = p_comp._part else: nodes[g]._value = 'R' #endfor else: nodes[g]._value = 'R' #endif else: nodes[g]._value = 'Q' nodes[g]._child = p._part #endif else: # P node if len(og[g]._support) == 1: nodes[g]._value = og[g]._support.__iter__().next() else: nodes[g]._value = 'P' #endif #endif #endfor return make_PQR_tree_from_partitions(support, nodes, og)
import tree ''' This project reads a CSV file and constructs a Tree-like data structure to store relations of each "Node" created from the input data. Input: Place a file named "input.csv" in the same directory as the program. Expected format - parent_id,node_id,node_name|parent_id,node_id,node_name... ''' #Initialize tree object root = tree.TreeNode() #Read input file input_data = open('input.csv', 'r') #Split file into rows using pipe delimiter nodes_list = list(input_data)[0].split('|') #Create list with lists of values nodes_data_list = [] for node_data in nodes_list: nodes_data_list.append(node_data.split(',')) #Pass list of node values to insert function and print root = root.insert_nodes(nodes_data_list) root.print_tree()
def test(self, row, job): if row._isT: if self._tree == None: # mat = bm.BinaryMatrix() # for l in self._part_list[job._row]: # for l in self._part_list: # for r in l._list: # mat.add_row_info(r) # #endif # #endfor # if mat._height > 0: # self._tree = c1p.make_PQR_tree(mat) # add leaves # og = [Support(set([x])) for x in self._support] # nodes = [tree.TreeNode(x) for x in self._support] nodes = [] # add internal vertices for p in self._part_list: pcopy = p._part.copy() val = mc1p.PQRTreeValue() node = tree.TreeNode(val) val._support = pcopy._support # og.append(Support(pcopy._support)) if pcopy._part == pcopy._end: val._type = 'P' else: val._type = 'Q' node._child = pcopy._part #endif nodes.append(node) #endfor nodes = sort.sort(nodes, tree.comp_nodes) # delete overlap graphs with the same support i = 0 # component iterator while i < len(nodes) - 1: if nodes[i]._value._support == nodes[i + 1]._value._support: if nodes[i]._value._type == 'P': del nodes[i] elif nodes[i + 1]._value._type == 'P': del nodes[i + 1] else: i += 1 #endif else: i += 1 #endif #endwhile # add root val = mc1p.PQRTreeValue() val._type = 'P' val._support = self._support nodes.append(tree.TreeNode(val)) self._tree = tree.PQRTree() # self._tree._head = c1p.make_PQR_tree_from_partitions(self._support, nodes, og) self._tree._head = mc1p.make_PQR_tree_from_parts(nodes) # mc1p.fix_tree_node(self._tree._head) # else: # self._tree = 0 #endif self._r = job._row #endif if not job._rem and self._tree != 0: # if not any(row._set in l._list for l in self._part_list[self._r]): # if not any(row._set == r._set for r in x._list for x in self._part_list): if not self._used[job._row]: return False #endif minimal = True telo_disc = [] # check minimality for r in self._telo_rows: if r < row._set: minimal = False break #endif #endfor if minimal: i = 0 # remove non minimal rows while i < len(self._telo_rows): if row._set < self._telo_rows[i]: mc1p.undo_check_LCA_path(self._telo_rows[i], self._tree) telo_disc.append(self._telo_rows[i]) del self._telo_rows[i] else: i = i + 1 #endif #endwhile if mc1p.check_LCA_path(row._set, self._tree): self._telo_rows.append(row._set) self._telo_rows_all.append(job._row) return True else: mc1p.undo_check_LCA_path(row._set, self._tree) for r in telo_disc: mc1p.check_LCA_path(r, self._tree) self._telo_rows.append(r) #endfor #endif return False else: self._telo_rows_all.append(job._row) return True #endif # if mc1p.check_LCA_path(row._set, self._tree): # return True # #endif # # mc1p.undo_check_LCA_path(row._set, self._tree) # # return False #endif # else: elif not job._rem: # self._part_list[job._row + 1] = [c1p.PartitionTree(x._part.copy(), copy.copy(x._list)) for x in self._part_list[job._row]] # if not job._rem: # return c1p.test_row(row._set, self._part_list[job._row + 1]) # #endif ret, self._mem_list[job._row] = c1p.test_row(row, self._part_list) self._used[self._t_copy[job._row]] = ret return ret else: self._used[self._t_copy[job._row]] = False #endif return True
left_num = count_left_subtree(root.left_child) right_num = count_left_subtree(root.right_child) root.left_num = left_num return left_num + right_num + 1 # 0 # / \ # 1 2 # / / \ # 3 4 5 # / \ # 6 7 tree_node_list = [] for i in range(8): tree_node_list.append(t.TreeNode(i)) tree_node_list[0].left_child = tree_node_list[1] tree_node_list[0].right_child = tree_node_list[2] tree_node_list[1].left_child = tree_node_list[3] tree_node_list[2].left_child = tree_node_list[4] tree_node_list[2].right_child = tree_node_list[5] tree_node_list[3].left_child = tree_node_list[6] tree_node_list[3].right_child = tree_node_list[7] count_left_subtree(tree_node_list[0]) for node in tree_node_list: print(node.left_num, end=' ') print() def find_node_max_difference(root): if not root:
def refine(self, r): start_full = False # True if the first interval element is full start = -1 # first intersecting item (-1) for unset end = -1 # last intersecting item (-1) for unset node = self._part # iterated node prevNode = None # node before iterated node (for insertion before node) s = r._set # set or ones in the row # heuristic to refine the end of the partition # if len(self._end._value[0] & s) > 0 and len(self._part._value[0] & s) == 0: if len(self._end._value & s) > 0 and len(self._part._value & s) == 0: node = self._end # x = node._value[0] # class at current node x = node._value y = s & x # new refined class # find intersection of first node if x < s: # node._value[1] = node._value[1] | set([r]) start_full = True elif len(self._support & (s - x)) == 0: if len(s - x) > 0: # self.insert_before(tree.TreeNode([x - y, node._value[1]]), self._end) self.insert_before(tree.TreeNode(x - y), self._end) # self._end._value = [y, node._value[1] | set([r])] self._end._value = y #endif # add if len(s - y) > 0: # self.insert_after(tree.TreeNode([s - y, set([r])]), self._end) self.insert_after(tree.TreeNode(s - y), self._end) #endif # update support self._support = self._support | s return True else: # self._end._value = [x - y, node._value[1]] self._end._value = x - y # node = tree.TreeNode([y, node._value[1] | set([r])]) node = tree.TreeNode(y) self.insert_before(node, self._end) #endif # s = s - node._value[0] s = s - node._value node = node._last # look for end of refinement while node != self._part and len(s) > 0: # x = node._value[0] # class at current node x = node._value # class at current node y = s & x # new refined class s = s - y if len(y) < len(x): if len(y) > 0: # node._value = [y, node._value[1] | set([r])] node._value = y # self.insert_before(tree.TreeNode([x - y, node._value[1]]), node) self.insert_before(tree.TreeNode(x - y), node) #endif break #endif node = node._last #endwhile # add partition to end if possible if (len(self._support & s) == 0 and start_full) or len(s) == 0: if len(s) > 0: # self.insert_after(tree.TreeNode([s, set([r])]), self._end) self.insert_after(tree.TreeNode(s), self._end) #endif # update support self._support = self._support | s return True else: # gap found return False #endif #endif # find first intersection while node != None and len(s) > 0: # x = node._value[0] # class at current node x = node._value # class at current node y = s & x # new refined class if len(y) > 0: # s intersects x start = node s = s - y if len(y) == len(x): # x = y start_full = True # node._value[1] = node._value[1] | set([r]) else: # node._value[0] = x - y node._value = x - y # breakup partition # t = tree.TreeNode([y, node._value[1] | set([r])]) t = tree.TreeNode(y) self.insert_after(t, node) prevNode = t node = t._brother break #endif prevNode = node node = node._brother break #endif prevNode = node node = node._brother #endwhile # look for non full y's while node != None and len(s) > 0: # x = node._value[0] # class at current node x = node._value # class at current node y = s & x # new refined class if len(y) > 0: s = s - y #endif if len(y) < len(x): # y is strictly contained in x if len(y) > 0: # node._value[0] = x - y node._value = x - y # breakup partition # p = tree.TreeNode([y, node._value[1] | set([r])]) p = tree.TreeNode(y) self.insert_before(p, node) prevNode = p #endif end = node break # else: # node._value[1] = node._value[1] | set([r]) #endif prevNode = node node = node._brother #endwhile if end == -1: end = node #endif # add remainder of s onto the partition if len(s) > 0: # check if able to add to end if end != None: # check if able to add to beginning if start == self._part and (start_full or prevNode == self._part._brother): # check that part of s doesn't appear later in the partition if len(s & self._support) > 0: # gap found return False #endif if start_full: # add to the begining # self.insert_before(tree.TreeNode([s, set([r])]), self._part) self.insert_before(tree.TreeNode(s), self._part) elif prevNode == self._part._brother: # switch 0th and 1st entries self.swap(self._part, self._part._brother) # add to the begining # self.insert_before(tree.TreeNode([s, set([r])]), self._part) self.insert_before(tree.TreeNode(s), self._part) #endif else: # can't add to the begining return False # gap found #endif else: # add s to the end # self.insert_after(tree.TreeNode([s, set([r])]), self._end) self.insert_after(tree.TreeNode(s), self._end) #endif else: # switch the partition to the begining in this case to allow # overlapping partitions to add onto the begining if start == self._part and not start_full and prevNode == self._part._brother and self._part._brother._brother != None: # switch 0th and 1st entries self.swap(self._part, self._part._brother) #endif #endif # update support self._support = self._support | s return True
def make_mC1P(matrix): p = [] # list of partitions and spanning trees used tre = None i = 0 # iterator rows = [] # rows to remove to make C1P telomere = [j for j in xrange(matrix._height)] use = [True for j in xrange(matrix._height)] support = matrix.get_support() telo_rows = [] # sort the matrix by weight matrix.sort() # process each row while i < matrix._height: row = matrix.get_row_info(i) if row._isT: if tre == None: nodes = [] # add internal vertices for part in p: val = PQRTreeValue() node = tree.TreeNode(val) val._support = part._part._support if part._part._part == part._part._end: val._type = 'P' else: val._type = 'Q' node._child = part._part._part #endif nodes.append(node) #endfor nodes = sort.sort(nodes, tree.comp_nodes) # delete overlap graphs with the same support j = 0 # component iterator while j < len(nodes) - 1: if nodes[j]._value._support == nodes[j + 1]._value._support: if nodes[j]._value._type == 'P': del nodes[j] elif nodes[j + 1]._value._type == 'P': del nodes[j + 1] else: j += 1 #endif else: j += 1 #endif #endwhile # add root val = PQRTreeValue() val._type = 'P' val._support = support nodes.append(tree.TreeNode(val)) tre = tree.PQRTree() tre._head = make_PQR_tree_from_parts(nodes) #endif if use[i]: test_row = test_telomere_row # data = tre data = [telo_rows, tre] else: test_row = remove_row data = None #endif else: test_row = c1p.test_row data = p for j in xrange(i + 1, matrix._height): if matrix.get_row_info(j)._set == row._set: telomere[i] = j #endif #endfor #endif yes, temp = test_row(row, data) # true if test_row succeeds, aux use[telomere[i]] = yes if not yes: # remove row from matrix rows.append(i) #endif i += 1 #endwhile return rows