def main(): """Main.""" t = Tree() for i in [4, 1, 6, 2, 5, 10, 3, 2]: print("Insert %s" % (i)) t.insert(i) print() print("Traversal") t.traverse() print()
def test_traverse_uses_depth_first_strategy(): t = Tree("a") child = t.add_child("b") child.add_child("d") child.add_child("e") t.add_child("c") visited = [] def visit_all(node): visited.append(node.value) return False t.traverse(visit_all) assert_that(visited, is_(["a", "b", "d", "e", "c"]))
def gamee_solver(firstnode): found = False allsteps = [] movesTree = Tree() movesTree.add_firstnode(firstnode) start_time = time.time() if getInversionCount(firstnode) % 2 != getInversionCount(goal) % 2: return allsteps, (time.time() - start_time) for node in movesTree.traverse(firstnode): if found == True: break if check_goal(node) == True: allsteps = findSteps(node, movesTree) found = True break else: for child in move(node): if check_goal(child) == True: allsteps = findSteps(node, movesTree) found = True break elif child not in movesTree.nodes(): movesTree.add_node(child, node) return allsteps, (time.time() - start_time)
def formCellTypeTree(n, cells, cellTypeParents, cellTypeNumCells, cellTypeConstProps, transcriptionFactors, cellTypeMeans): cellTypeTree = Tree() parentCellType = -1 projInit = np.ones(shape=n) constInit = np.zeros(shape=n) cellTypeMember = [] cellTypeChildMember = list(range(cells)) cellTypeMean = 0 numCellTypes = len(cellTypeParents) projMatrix = np.zeros(shape = (numCellTypes, n)) constMatrix = np.zeros(shape = (numCellTypes, n)) headNode = cellTypeTree.add_head(parentCellType, cellTypeMember, constInit, projInit, cellTypeMean) headNode.setChildCellTypeMembers(cellTypeChildMember) numCellTypes = len(cellTypeParents) cellTypeMembers = [[] for i in range(numCellTypes)] cellNumPool = range(cells) for cellType in range(numCellTypes): numCells = cellTypeNumCells[cellType] cellTypeIndices = random.sample(cellNumPool, numCells) cellTypeMembers[cellType] = cellTypeIndices cellNumPool = list(set(cellNumPool) - set(cellTypeIndices)) addTreeChildren(n, cellTypeTree, cellTypeParents, cellTypeMembers, cellTypeConstProps, projInit, constInit, projMatrix, constMatrix, cellTypeMeans, parentCellType, transcriptionFactors) #maxCellType = int(max(cellTypeParents)) #Highest cell type number corresponds to the highest cell type for node in cellTypeTree.traverse(-1): for field in node: print(field) return cellTypeTree, projMatrix, constMatrix
def test_traverse_returns_none_when_operation_always_returns_false(): t = Tree("a") t.add_child("b") node = t.traverse(lambda n: False) assert_that(node, is_(None))
def main(): tree = Tree() #set root node root = "Forest" mode = input( 'ENTER 0 FOR DFS, ENTER 1 FOR BFS\nNOTE: Strings must be between quotes\n' ) #print(mode) #print(mode == 1) #add elements to the graph #USAGE: tree.add_node(leaf,parent) tree.add_node(root) tree.add_node("Steve", root) tree.add_node("Kao", root) tree.add_node("Diplo", root) tree.add_node("Lol", "Steve") tree.add_node("Amy", "Steve") tree.add_node("Julio", "Amy") tree.add_node("Mary", "Amy") tree.add_node("Mark", "Julio") tree.add_node("Jane", "Mark") tree.add_node("Tahir", "Kao") tree.add_node("What", "Tahir") tree.display(root) if (mode == 0): print("DEPTH-FIRST ITERATION:\n") for node in tree.traverse(root): tree.specialdisplay(root, node) print("\n") time.sleep(1) if (mode == 1): print("BREADTH-FIRST ITERATION:\n") for node in tree.traverse(root, mode=BREADTH): tree.specialdisplay(root, node) print("\n") time.sleep(1) restart = input('RESTART? ("y" or "n")\n') if (restart == 'y' or restart == 'Y'): main()
def test_traverse_returns_self_when_operation_returns_true_on_root_node(): t = Tree("a") node = t.traverse(lambda n: True) assert_that(node, is_(t))
def importData(fname, displayTree=False, colSep=',', headerLine=False, verbose=False): """ Import tree data from a CSV (text) file or list. The data should be in the following format: node_ID_number,node_parent_ID_number,data_item1,data_item2,...,data_itemN\n The separator can, optionally, be a character other than "," The root node must have a parent id of 0 and normally should also have an index of 1 From MATLAB one can produce tree structures and dump data in the correct format using https://github.com/raacampbell13/matlab-tree and the tree.dumptree method Inputs: fname - if a string, importData assumes it is a file name and tries to load the tree from file. if it is a list, importData assumes that each line is a CSV data line and tries to convert to a tree. displayTree - if True the tree is printed to standard output after creation colSep - the data separator, a comma by default. headerLine - if True, the first line is stripped off and considered to be the column headings. headerLine can also be a CSV string or a list that defines the column headings. Must have the same number of columns as the rest of the file. verbose - prints diagnositic info to screen if true """ if verbose: print "tree.importData importing file %s" % fname #Error check if isinstance(fname,str): if os.path.exists(fname)==False: print "Can not find file " + fname return #Read in data fid = open(fname,'r') contents = fid.read().split('\n') fid.close() elif isinstance(fname,list): contents=fname #assume that fname is data rather than a file name #Get header data if present if headerLine==True: header = contents.pop(0) header = header.rstrip('\n').split(colSep) elif isinstance(headerLine,str): header = headerLine.rstrip('\n').split(colSep) elif isinstance(headerLine,list): header = headerLine else: header = False data = [] for line in contents: if len(line)==0: continue dataLine = line.split(colSep) if len(header) !=len(dataLine): print "\nTree file appears corrupt! header length is %d but data line length is %d.\ntree.importData is aborting.\n" % (len(header),len(dataLine)) return False theseData = map(int,dataLine[0:2]) #add index and parent to the first two columns #Add data to the third column. Either as a list or as a dictionary (if header names were provided) if header != False: #add as dictionary dataCol = dict() for ii in range(len(header)-2): ii+=2 dataCol[header[ii]]=dataTypeFromString.convertString(dataLine[ii]) else: dataCol = dataLine[2:] #add as list of strings theseData.append(dataCol) data.append(theseData) if verbose: print "tree.importData read %d rows of data from %s" % (len(data),fname) #Build tree tree = Tree() tree.add_node(0) for thisNode in data: tree.add_node(thisNode[0],thisNode[1]) tree[thisNode[0]].data = thisNode[2] #Optionally dump the tree to screen (unlikely to be useful for large trees) if displayTree: tree.display(0) for nodeID in tree.traverse(0): print "%s - %s" % (nodeID, tree[nodeID].data) return tree
def isChild(key1, key2): if key1 in move(key2): return True else: return False ######################################################## if getInversionCount(firstnode) % 2 != getInversionCount(goal) % 2: print("There is no solution for the input puzzle") sys.exit() ### builds a complete tree from goal as root. for node in movesTree.traverse(goal): for child in move(node): if child not in movesTree.nodes(): movesTree.add_node(child, node) print( "Problem 1. The expression for total number of possible stating states is : 9!\n" ) maxSteps = movesTree.maxSteps(goal) print("The hardest puzzle for solving the goal 12345678_ needs = ", maxSteps) pzlSteps = findSteps(firstnode) print("\nProblem 2. The maxSteps between input puzzle ", puzzle, " and the goal is: ", len(pzlSteps))
treeOfLife["Amphibians"].data = 'they croak' treeOfLife["Reptiles"].data = 'they stick to walls' treeOfLife["Mammals"].data = 'they have udders' print("List of nodes:") print(list(treeOfLife.nodes.keys())) print("") print("Children of node 'Vertebrates'") print(treeOfLife.nodes['Vertebrates'].children) print("") print(treeOfLife.display('Life')) print("\n***** Depth-first *****") for nodeID in treeOfLife.traverse("Life"): print(nodeID) print("\n***** Width-first *****") for nodeID in treeOfLife.traverse("Life", mode=_WIDTH): print(nodeID) print("\n***** Width-first of all data in vertebrates *****") for nodeID in treeOfLife.traverse("Vertebrates", mode=_WIDTH): print("%s - %s" % (nodeID, treeOfLife[nodeID].data)) print("\nLeaves:") print(treeOfLife.findLeaves('Life')) print("\nBranches:") print(treeOfLife.findBranches('Life'))
def importData(fname, displayTree=False, colSep=',', headerLine=False, verbose=False): """ Import tree data from a CSV (text) file or list. The data should be in the following format: node_ID_number,node_parent_ID_number,data_item1,data_item2,...,data_itemN\n The separator can, optionally, be a character other than "," The root node must have a parent id of 0 and normally should also have an index of 1 From MATLAB one can produce tree structures and dump data in the correct format using https://github.com/raacampbell13/matlab-tree and the tree.dumptree method Inputs: fname - if a string, importData assumes it is a file name and tries to load the tree from file. if it is a list, importData assumes that each line is a CSV data line and tries to convert to a tree. displayTree - if True the tree is printed to standard output after creation colSep - the data separator, a comma by default. headerLine - if True, the first line is stripped off and considered to be the column headings. headerLine can also be a CSV string or a list that defines the column headings. Must have the same number of columns as the rest of the file. verbose - prints diagnositic info to screen if true """ if verbose: print("tree.importData importing file %s" % fname) #Error check if isinstance(fname, str): if os.path.exists(fname) == False: print("Can not find file " + fname) return #Read in data fid = open(fname, 'r') contents = fid.read().split('\n') fid.close() elif isinstance(fname, list): contents = fname #assume that fname is data rather than a file name #Get header data if present if headerLine == True: header = contents.pop(0) header = header.rstrip('\n').split(colSep) elif isinstance(headerLine, str): header = headerLine.rstrip('\n').split(colSep) elif isinstance(headerLine, list): header = headerLine else: header = False data = [] for line in contents: if len(line) == 0: continue dataLine = line.split(colSep) if len(header) != len(dataLine): print( "\nTree file appears corrupt! header length is %d but data line length is %d.\ntree.importData is aborting.\n" % (len(header), len(dataLine))) return False theseData = list( map(int, dataLine[0:2])) #add index and parent to the first two columns #Add data to the third column. Either as a list or as a dictionary (if header names were provided) if header != False: #add as dictionary dataCol = dict() for ii in range(len(header) - 2): ii += 2 dataCol[header[ii]] = dataTypeFromString.convertString( dataLine[ii]) else: dataCol = dataLine[2:] #add as list of strings theseData.append(dataCol) data.append(theseData) if verbose: print("tree.importData read %d rows of data from %s" % (len(data), fname)) #Build tree tree = Tree() tree.add_node(0) for thisNode in data: tree.add_node(thisNode[0], thisNode[1]) tree[thisNode[0]].data = thisNode[2] #Optionally dump the tree to screen (unlikely to be useful for large trees) if displayTree: tree.display(0) for nodeID in tree.traverse(0): print("%s - %s" % (nodeID, tree[nodeID].data)) return tree
from tree import Tree import subprocess import sys if __name__ == "__main__": tree = Tree(5) tree.add_value(3) tree.add_value(9) tree.add_value(10) tree.add_value(6) tree.add_value(7) tree.traverse() tree.rotate_to_root(7) tree.traverse() tree.rotate_to_root(7) tree.traverse() tree.add_value(2) tree.add_value(4) tree.add_value(8) tree.traverse() tree.rotate_to_root(4) tree.delete(4) tree.traverse()
class DTClassifier(BaseEstimator, ClassifierMixin): def __init__(self, counts=None, features=None): """ Initialize class with chosen hyperparameters. Args: counts = how many types for each attribute Example: DT = DTClassifier() """ self.features = features self.counts = counts self.tree = Tree() self.nan_replace = np.nan def _most_common_class(self, label_results): if len(label_results[1]) == 0: return None highest_index = np.argmax(label_results[1]) return label_results[0][highest_index] def _stopper(self, label_size, indexes, data_size): if label_size <= 1: return True if len(indexes) <= 1: return True if data_size <= 1: return True return False def _fit(self, X, y, indexes): out_of, data_results = self._count_unique(X) label_size, label_results = self._count_unique(y) if self._stopper(len(label_results[0][0]), indexes, out_of): index = -1 if len(indexes) < 1 else indexes.pop() return Tree().makeAddBranch( None, None, self._most_common_class(label_results[0]), index, [], friendly_split=self.features[index] if (self.features is not None and len(self.features) > 0 and len(self.features) > abs(index)) else None) entropy = self._entropy(label_size, label_results) best_branch = (0, None, 0, []) # gain, tree, index, partitions for i in indexes: # partition based on index partitions = self._partition(X, y, i, data_results=data_results) partitions_entropy = self._partition_entropy(partitions, out_of) gain = entropy - partitions_entropy #find partition with best gain if gain >= best_branch[0]: friendly_split = None if self.features is not None: friendly_split = self.features[i] child_tree = Tree().makeAddBranch( None, None, self._most_common_class(label_results[0]), i, data_results[i][0], friendly_split=friendly_split) best_branch = (gain, child_tree, i, partitions) # don't look at the index we just did anymore to_send_copy = indexes.copy() to_send_copy.remove(best_branch[2]) for part in best_branch[3]: #per attribute grandchild_tree = self._fit(part[0], part[1], to_send_copy) parent_partition = part[0][0][best_branch[2]] best_branch[1].addChildTree(grandchild_tree, parent_partition) return best_branch[1] def fit(self, X, y): """ Fit the data; Make the Desicion tree Args: X (array-like): A 2D numpy array with the training data, excluding targets y (array-like): A 2D numpy array with the training targets Returns: self: this allows this to be chained, e.g. model.fit(X,y).predict(X_test) """ indexes = {x for x in range(np.shape(X)[1])} self.tree = self._fit(X, y, indexes) # use LabelBinarizer? return self def predict(self, X): """ Predict all classes for a dataset X Args: X (array-like): A 2D numpy array with the training data, excluding targets Returns: array, shape (n_samples,) Predicted target values per element in X. """ results = [] for x in X: results.append(self.tree.traverse(x)) return np.reshape(results, (len(results), -1)) def score(self, X, y): """ Return accuracy of model on a given dataset. Must implement own score function. Args: X (array-like): A 2D numpy array with data, excluding targets y (array-like): A 2D numpy array of the targets """ predicted = self.predict(X) count = 0 for predict, expected in zip(predicted, y): if predict == expected: count = count + 1 return count / np.shape(y)[0] def _count_unique(self, data): data_results = {} if np.shape(data)[0] == 0: data_results[0] = ([], []) return 0, data_results for index in range(np.shape(data)[1]): #find all unique non-nan values column = data[:, index] # https://stackoverflow.com/a/37148508 use nan != nan values, counts = np.unique([x for x in column if x == x], return_counts=True) nan_sum = np.size([x for x in column if x != x]) if nan_sum > 0: values = np.append(values, self.nan_replace) counts = np.append(counts, nan_sum) data_results[index] = (values, counts) return np.shape(data)[0], data_results def _partition(self, X, y, attribute_index, data_results=None): if data_results is None: data_results = self._count_unique(X)[1] partitions = [] for label in data_results[attribute_index][0]: x_part = [] y_part = [] for data_point_index in range(np.shape(X)[0]): particular_point_label = X[data_point_index][attribute_index] if label == particular_point_label: x_part.append(X[data_point_index]) y_part.append(y[data_point_index]) elif (label == self.nan_replace or label != label) and np.isnan(particular_point_label): x_part.append(X[data_point_index]) y_part.append(y[data_point_index]) partitions.append((np.array(x_part), np.array(y_part))) return partitions def _partition_entropy(self, partitions, out_of_whole): sum = 0 for part in partitions: part_label_size, part_label_result = self._count_unique(part[1]) fraction = part_label_size / out_of_whole sum = sum + self._entropy(part_label_size, part_label_result) * fraction return sum def _entropy(self, out_of, values_and_counts): sum = 0 for column in values_and_counts: for count in values_and_counts[column][1]: fraction = count / out_of sum = sum - fraction * np.log2(fraction) return sum def graph(self, class_translator=lambda x: x): if self.tree is None: return "No graph" return self.tree.graph(class_translator=class_translator) def __repr__(self): return self.graph()
# Copyright (C) by Brett Kromkamp 2011-2014 ([email protected]) # You Programming (http://www.youprogramming.com) # May 03, 2014 from tree import Tree (_ROOT, _DEPTH, _BREADTH) = range(3) tree = Tree() tree.add_node("Harry") # root node tree.add_node("Jane", "Harry") tree.add_node("Bill", "Harry") tree.add_node("Joe", "Jane") tree.add_node("Diane", "Jane") tree.add_node("George", "Diane") tree.add_node("Mary", "Diane") tree.add_node("Jill", "George") tree.add_node("Carol", "Jill") tree.add_node("Grace", "Bill") tree.add_node("Mark", "Jane") tree.display("Harry") print("***** DEPTH-FIRST ITERATION *****") for node in tree.traverse("Harry"): print(node) print("***** BREADTH-FIRST ITERATION *****") for node in tree.traverse("Harry", mode=_BREADTH): print(node)
print "List of nodes:" print treeOfLife.nodes.keys() print "" print "Children of node 'Vertebrates'" print treeOfLife.nodes['Vertebrates'].children print "" print treeOfLife.display('Life') print("\n***** Depth-first *****") for nodeID in treeOfLife.traverse("Life"): print(nodeID) print("\n***** Width-first *****") for nodeID in treeOfLife.traverse("Life", mode=_WIDTH): print(nodeID) print("\n***** Width-first of all data in vertebrates *****") for nodeID in treeOfLife.traverse("Vertebrates", mode=_WIDTH): print "%s - %s" % (nodeID, treeOfLife[nodeID].data) print "\nLeaves:" print treeOfLife.findLeaves('Life') print "\nBranches:" print treeOfLife.findBranches('Life')
counter = 0 for j in range(branch_boundrary, len(info)): temp = branch_leaf[i] branch_leaf.append(j) if counter == 0: tree.add_node(info[j], info[temp]) #print "j, temp: ", (j, temp) else: tree.add_node(info[j], info[j - 1]) #print "j, j-1: ", (j, j-1) counter += 1 tree.display(info[0]) #print("***** DEPTH-FIRST ITERATION *****"), '\n' for node in tree.traverse(info[0]): # calculate path amount dfs.append(node) if node == info[len(info) - 1]: partition = open("partition.c", "w") execution_path_r1 += 1 for i in range(1, len(dfs)): if ("if" not in dfs[i]) and ("elif" not in dfs[i]) and ( "else" not in dfs[i]) and ("}" not in dfs[i]): partition.write(dfs[i]) partition.close() os.system("mv partition.c exe_r1_path" + str(execution_path_r1) + ".c") #os.system('clang -Os -S -emit-llvm exe_r1_path'+str(execution_path_r1)+'.c -o exe_r1_path'+str(execution_path_r1)+'.ll') for i in range(len(dfs) - 1, -1, -1): if ("if" not in dfs[i]) and ("elif" not in dfs[i]) and ("else" not in dfs[i]):
tree[parent] except KeyError: tree.add_node(parent) try: tree[child] except KeyError: tree.add_node(child) tree[parent].add_child(child) tree[child].add_parent(parent) query = ("SELECT id, name FROM imm_web") cursor.execute(query) id_name_dict = {} for (id, name) in cursor: id_name_dict[id] = name tree.display(1) return_info = dict() print("***** DEPTH-FIRST ITERATION *****") for node in tree.traverse(1): print(str(node) + ", " + id_name_dict[node] + ", " + str(tree[node].parent)) return_info[node] = [id_name_dict[node], tree[node].parent] print("***** BREADTH-FIRST ITERATION *****") for node in tree.traverse(1, mode=_BREADTH): print(str(node) + ", " + id_name_dict[node]) cursor.close() cnx.close()
if branch_boundrary > 0: for i in range(0, 1): counter = 0 for j in range(branch_boundrary, len(info)): temp = branch_leaf[i] if counter == 0: tree.add_node(info[j], info[temp]) else: tree.add_node(info[j], info[j - 1]) counter += 1 tree.display(info[0]) #print("***** DEPTH-FIRST ITERATION *****"), '\n' #print ("region2:\n") for node in tree.traverse(info[0]): if "region" not in node: dfs.append(node) for i in range(0, len(dfs)): region_path.write(dfs[i]) region_path.close() """ #print (dfs) for i in range(0, len(p_num)-1): #if (p_num[i+1] < p_num[i]) and ("}" in dfs[i+1]): if (p_num[i+1] < p_num[i]) and ("{" not in dfs[i+1]): p_num.insert(i+1, 99) #print ("parents: ", p_num) for i in range(0, len(p_num)-1): path.append(dfs[i]) # if ("}" in dfs[i]) and ("}" not in dfs[i+1]) and (("else" in dfs[i+1]) or ("if" in dfs[i+1]) or ("while" in dfs[i+1])):