def make_game(pl: Players, gs: Gamestate): """ makes the entire recombining game tree, which is a list of trees one for each day. """ game = treelib.Tree() game.create_node(f'Root {str(gs)}', 'root0', data=(None, gs, False)) games = [game] g = games[-1] while unexpanded_nodes(g) > 0: while unexpanded_day_nodes(g) > 0: expand_day_nodes(g) while unexpanded_night_nodes(g) > 0: expand_night_nodes(g) gss = set([x.data[1] for x in g.leaves() if not x.data[2]]) if len(gss) > 0: root_gs = g[g.root].data[1] new_gs = Gamestate(root_gs.day + 1, 0, None, pl) new_game = treelib.Tree() new_game.create_node(f'Day {new_gs.day}', 'root' + str(new_gs.day - 1), data=(None, new_gs, True, 1.0)) for idx, ugss in enumerate(gss): new_game.create_node(f'{str(ugss)}', "root" + str(new_gs.day - 1) + "_n" + str(idx), parent=new_game.root, data=(None, ugss, False, 0.0)) games.append(new_game) g = new_game return games
def test_tree(): t = treelib.Tree() b = DecisionTree() const_tree = treelib.Tree() t.create_node('Is spcl_char >= 8?', 'Is spcl_char >= 8?') my_tree = b.build_tree(sample, const_tree, '') assert t.show() == const_tree.show()
def __partition_symmetry__(subtree): """ Estimates tree asymmetry based on all possible subtrees. helper function of tree_asymmetry Input treelib object Output asymmetry, degree of tree """ #split subtree into its 2 subtrees current_root = subtree.root #degree of subtree degree = len(subtree.leaves()) #get children (new roots) children = subtree.children(current_root) child_1 = None child_2 = None if len(children) > 0: child_1 = children[0].tag if len(children) > 1: child_2 = children[1].tag tree_1 = None tree_2 = None #build new subtrees if child_1 is not None: tree_1 = bt.Tree(subtree.subtree(child_1), deep=True) if child_2 is not None: tree_2 = bt.Tree(subtree.subtree(child_2), deep=True) degree_1 = 0 degree_2 = 0 #get degree if tree_1 is not None: degree_1 = len(tree_1.leaves()) if tree_2 is not None: degree_2 = len(tree_2.leaves()) if (degree_1 >= degree_2) and (degree_1 != 0): asymmetry = (degree_1 - degree_2) / degree_1 elif (degree_2 >= degree_1) and (degree_2 != 0): asymmetry = (degree_2 - degree_1) / degree_2 #shouldnt be assigned else: asymmetry = 0 return degree * asymmetry, degree
def absorb(t1: tl.Tree, t2: tl.Tree) -> tl.Tree: # work with copies. t1 = tl.Tree(tree=t1, deep=True) t2 = tl.Tree(tree=t2, deep=True) # reset all the identifiers: t1 = reset_ids(t1) t2 = reset_ids(t2) t1.paste(t1.root, t2) return t1
def collapse(t1: tl.Tree, t2: tl.Tree) -> tl.Tree: # work with copies. t1 = tl.Tree(tree=t1, deep=True) t2 = tl.Tree(tree=t2, deep=True) # reset all the identifiers: t1 = reset_ids(t1) t2 = reset_ids(t2) # paste all the children of t2 into the root of t1 for child in t2.children(t2.root): t1.paste(t1.root, t2.subtree(child.identifier)) return t1
def print_project_task_collection(project, show_inactive=False, n_shown=10000): task_collection = project.tasks active_statuses = [todo_str, ip_str, hold_str] if show_inactive: active_statuses += [abandoned_str, done_str] id_str = get_project_header_str(project) tree = treelib.Tree() tree.create_node(id_str, "header") nid = 0 for sp in active_statuses: color = STATUS_COLORMAP[sp] sp_str = "In progress" if sp == ip_str else sp.capitalize() tree.create_node(ts.f(color, sp_str), sp, parent="header") statused_tasks = task_collection[sp] if n_shown: if not statused_tasks: nid += 1 tree.create_node("No tasks.", nid, parent=sp) continue for i, t in enumerate(statused_tasks): nid += 1 task_txt = get_task_string(t, colorize_status=True) tree.create_node(ts.f(color, task_txt), nid, parent=sp) if i >= n_shown: break tree.show(key=lambda node: node.identifier)
def assemble_tree(self, *, basepath, tree_dirpaths, data_by_dirpath, ilk_by_dirpath, device_aliases, device_configurations): logger.info("Start assemble_tree") start_time = datetime.now() tree = treelib.Tree() rootpath, tag = basepath.rsplit('/', 1) for dirpath in tree_dirpaths: data = data_by_dirpath[dirpath] assert dirpath == data['dirpath'] parent_dirpath, dirname = dirpath.rsplit('/', 1) # ilk = ilk_by_dirpath[dirpath] nodepath = data.get('nodepath') alias = device_aliases.find_highest_priority(nodepath) device_config = device_configurations.find_configuration(data) node_data = get_node_data(data, device_config, alias) if parent_dirpath == basepath: pass if parent_dirpath == rootpath: parent = None else: parent = parent_dirpath tree.create_node(tag=dirname, identifier=dirpath, parent=parent, data=node_data) logger.info( f"End assemble_tree - duration: {(datetime.now() - start_time).total_seconds()}" ) return tree
def assemble_bare_tree(self): logger.info("Start assemble_bare_tree") start_time = datetime.now() tree = treelib.Tree() rootpath, tag = self.basepath.rsplit('/', 1) for dirpath in self.get_tree_dirpaths(): parent_dirpath, dirname = dirpath.rsplit('/', 1) if parent_dirpath == self.basepath: pass if parent_dirpath == rootpath: parent = None else: parent = parent_dirpath data = { 'dirpath': dirpath, 'dirname': dirname, } tree.create_node(tag=dirname, identifier=dirpath, parent=parent, data=data) logger.info( f"End assemble_bare_tree - duration: {(datetime.now() - start_time).total_seconds()}" ) return tree.to_dict(with_data=True)
def dict2treelib(name, nested_dict): """Convert a nested dictonary to a treelib Tree object. This function is recursive. The treelib representation of the trees is used for pretty-printing (in ASCII) of the tree, you only need to do str() of the returned result from this function. See `https://treelib.readthedocs.io/` Args: name: name of root node nested_dict: nested dictonary of the children at the root. Return: treelib.Tree """ import treelib tree = treelib.Tree() tree.create_node(name, name) for child in nested_dict.keys(): tree.paste(name, dict2treelib(child, nested_dict[child])) return tree
def __init__(self, repo_owner, repo_name): self.tree = treelib.Tree() self.tree.create_node("/", "/", data="directory") # retrieve data url = "https://api.github.com/repos/" + repo_owner + "/" + repo_name + "/git/trees/master?recursive=1" response = urllib.request.urlopen(url) data = json.load(response) # parse data and add paths to the tree paths = [] for node in data.get("tree"): paths.append([node.get("path"), node.get("type")]) for path, obj_type in paths: split = path.split("/") for item in split: full_name = "/" + "/".join(split[:(split.index(item) + 1)]) parent_name = "/" + "/".join(split[:(split.index(item))]) try: if obj_type == "tree": self.tree.create_node(item, full_name, parent_name, data="directory") else: self.tree.create_node(item, full_name, parent_name, data="file") except treelib.exceptions.DuplicatedNodeIdError: pass
def executor(ctx): s = ctx.obj["EXECUTOR"] pmap = ctx.obj["PMAP"] tree = treelib.Tree() tree.create_node(ts.f("u", "Executor Schedule"), "root") i = 0 for day, project_ids in s.executor_week.items(): if project_ids == executor_all_projects_key: valid_pids = list(pmap.keys()) else: valid_pids = project_ids is_today = day == datetime.datetime.today().strftime("%A") color = "g" if is_today else "w" tree.create_node(ts.f(color, day), day, data=i, parent="root") i += 1 if not valid_pids: tree.create_node(ts.f("r", "No projects for this day"), data=i, parent=day) else: for j, pid in enumerate(valid_pids): project_txt = f"{pmap[pid].name}" color = "g" if is_today else "x" tree.create_node(ts.f(color, project_txt), data=j, parent=day) tree.show(key=lambda node: node.data)
def MontreCarloTreeSearchCTPDeepSelection(game, graph, state, belief_State, iterationMax=10000): iteration = 0 tree = treelib.Tree() root = state[-1] tree.create_node(tag=None, identifier=root, parent=None, data=nodeData(0, 0)) moves = {} while (iteration <= iterationMax): weather = getWeather(graph, root, game.goal, belief_State) (score, SelectedNode, selected) = SelectionCTPblindDeepSingle( tree, state, game, iteration, weather, tree.get_node(root).data.scoreAverage, belief_State) if not selected in moves: moves[selected] = 1 else: moves[selected] = moves[selected] + 1 BackPropagationCTP(tree, SelectedNode, score) iteration = iteration + 1 newVal = list(zip(moves.keys(), moves.values())) return list(map(lambda x: (tuple(x[0].split(" ")), x[1]), newVal))
def tree2file(tree: Tree): if exists(tree_file): remove(tree_file) def gen_index(): i = 1 while True: yield i i += 1 index = gen_index() t = tl.Tree() t.create_node(tree.root_node.type, 0) nodes = list(map(lambda n: (n, 0), tree.root_node.children)) while nodes: child_nodes = [] for node, parent_index in nodes: i = next(index) t.create_node(node.type, i, parent=parent_index) for child in node.children: child_nodes.append((child, i)) nodes = child_nodes t.save2file(tree_file) return tree_file
def _treeForClasses(self, aClass, dbClasses, perspective, currentTree=None, parentIdentifier="root"): # create a full tree with the exception that only dbClasses are allowed to be in it included_classes = set() if currentTree is None: # climb up to the top parent_id = aClass.parent_id while parent_id is not None: aClass = session.query(Class).filter( Class.id == parent_id).first() parent_id = aClass.parent_id # now i'm on top currentTree = treelib.Tree() currentTree.create_node("Root", "root") if aClass in dbClasses: self._createTreeNode( currentTree, aClass, perspective, parentIdentifier) # don't care about originals here parentIdentifier = aClass.name included_classes.add(aClass) children = session.query(Class).filter( Class.parent_id == aClass.id).all() for child in children: _, included = self._treeForClasses( child, dbClasses, perspective, currentTree, parentIdentifier=parentIdentifier) included_classes.update(included) return currentTree, included_classes
def tree_from_dict(nested_dict: dict) -> treelib.Tree: """Convert a dictionary to a treelib Tree. The treelib representation of the trees is used for pretty-printing (in ASCII) of the tree, you only need to do str() of the returned result from this function. See `https://treelib.readthedocs.io/` Args: nested_dict: nested dictonary representing a tree. """ if not nested_dict.keys(): # Return an empty string, because str(treelib.Tree()) will error return "" if not len(nested_dict.keys()) == 1: raise ValueError( "The dict given to tree_from_dict() must have " "exactly one top level key, representing a single tree." ) root_name = list(nested_dict.keys())[0] tree = treelib.Tree() _add_to_tree_from_dict(nested_dict[root_name], root_name, tree) return tree
def readGSP(filepath): global last_id xmltree = ET.parse(filepath) p = xmltree.getroot() #TODO header? tree = treelib.Tree() g = p.find('goals') if len(g.getchildren()) == 0: return tree id = last_id last_id += 1 gr = g.getchildren()[0] #root tn = tree.create_node(gr.attrib['name'], id) tn.fraction = float(gr.attrib['importance']) tn.value = float(gr.attrib['progress']) if gr.attrib.has_key('deadline'): tn.deadline = gr.attrib['deadline'] # not used in this app else: tn.deadline = None add_subtree(tree, tn, gr, id) return tree
def _downTreeForClass(self, dbClass, perspective, split_class_names=[]): # create a the full inheritance tree for dbClass included_classes = set() currentTree = treelib.Tree() currentTree.create_node("Root", "root") self._createTreeNode(currentTree, dbClass, perspective, "root", original=True) included_classes.add(dbClass) # only the first class adds itself dbchildren = session.query(Class).filter( Class.parent_id == dbClass.id).all() if dbchildren: for dbchild in dbchildren: # when there are no more children, it's over# included_classes.add(dbchild) _, node_classes = self._fullTreeForClass( dbchild, perspective, currentTree, parentIdentifier=dbClass.name, split_class_names=split_class_names) included_classes.update(node_classes) return currentTree, included_classes
def build_tree(neuron): """ Input: pymaid CatmaidNeuron object Output: treelib Tree object containing all nodes """ tree = treelib.Tree() try: isinstance(neuron, pymaid.CatmaidNeuron) except: raise Exception('neuron needs to be CatmaidNeuron object') nodeList = neuron.nodes.node_id.tolist() nodeInfo = neuron.nodes[['node_id', 'parent_id']].to_numpy() while len(nodeList) > 0: for node in nodeInfo: if tree.contains(node[0]): continue if node[1] == -1: tree.create_node("root", node[0]) nodeList.remove(node[0]) if tree.contains(node[1]): tree.create_node("neuron", node[0], parent=node[1]) nodeList.remove(node[0]) else: continue return (tree)
def tree_json_to_tree_lib(tree_json): def add_nodes(subtree, parent_tweet_id): # create the (subtree) root node tweet_id = subtree["tweet"] tweet_data = { "user_id": tweets_metadata[tweet_id]["user_id"], "time": tweets_metadata[tweet_id]["time"] } tree.create_node(tag=tweet_id, identifier=tweet_id, parent=parent_tweet_id, data=tweet_data) # loop through the children for reply_subtree in subtree["replies"]: reply_tweet = reply_subtree["tweet"] add_nodes(reply_subtree, parent_tweet_id=tweet_id) tree = treelib.Tree() tree_dict = tree_json["reply_tree"] tweets_metadata = tree_json["tweets"] add_nodes(tree_dict, parent_tweet_id=None) return tree
def fit(self, data_list): root = self.create_block(data_list) self.tree = treelib.Tree() self.tree.create_node('root', '0', data=root) self.create_leaves() print(self.tree) leaves_list = self.tree.leaves() # 得到所有划分好的块 self.block_list = [leaf.data for leaf in leaves_list] # 聚类 self.cluster([]) if len(self.cluster_dict) > 0: # 通过聚类解开映射,得到类型相似的样例列表 muti_block_list = self.im_muti_block() # muti_block_list = self.ir_muti_block() muti_data_index_list = [] for muti_block in muti_block_list: muti_data_index_list.append( muti_block.get_data_index(self.dict)) # 每一个聚类中的点对应的样例序号 muti_data_list = [] # 返回的样例 for muti_data_index in muti_data_index_list: muti_data_list.append([self.data[i] for i in muti_data_index]) return muti_data_list else: return self.block_list
def __init__(self, chemin): '''pour creer un objet document il suffit de donner le chemin où path vers le fichier XML au format tei L'objet est complet dès sa création. les objet internes sont recréer dès l'intanciation''' self.id = random.random() self.chemin = str(pl.Path(chemin)) self.tree = ET.parse(chemin) self.racine_doc = self.tree.getroot() self.dico = self.racine_doc.tag[:-3] # en supposant que le fichier a bien été formater sous la format # xml/tei self.document = self.racine_doc[1][0] self.metadata = self.racine_doc[0][0] # informations relatives à l'oeuvre self.metadata [0] # informations relatives à la publication self.metadata[1] # informations relatives à la source self.treelib = tl.Tree() self.Titre = None for children in self.metadata.iter(): if children.tag[-5:] == "title": self.Titre = children.text elif children.tag[-6:] == "author": self.Auteur = children.text self.sections = [] self.definir_les_sections() self.Texte = [] self.treelib.create_node("Document ", self.id, data=self) self.creation_arbre()
def __init__(self, prefix, session): self.session = session if prefix[0] != "/": prefix = "/" + prefix self.prefix = prefix self.tree = treelib.Tree()
def draw_tree(dt_node: Node): tl = treelib.Tree() node_queue = [dt_node] parent_stack = [(None, 1)] node_id = 0 while (node_queue): tree = node_queue.pop(0) if (not tl.contains('nid' + str(tree.feature))): parent, parent_stack = stack_pop(parent_stack) tl.create_node(tree.feature_name, 'nid' + str(tree.feature), parent=parent) parent_stack.append(['nid' + str(tree.feature), 2]) tmp_tree = tree.tree for k in tmp_tree.keys(): next_tree = tmp_tree[k] if (next_tree.label != None): parent, parent_stack = stack_pop(parent_stack) tl.create_node('(' + k + ')' + str(next_tree.label), 'lid' + str(node_id), parent=parent) node_id += 1 else: parent, parent_stack = stack_pop(parent_stack) tl.create_node('(' + k + ')' + str(next_tree.feature_name), 'nid' + str(next_tree.feature), parent=parent) parent_stack.append(['nid' + str(next_tree.feature), 2]) node_queue.append(next_tree) tl.show()
def MontreCarloTreeSearch(game, state, iterationMax=10000): bestMove = "" iteration = 0 tree = treelib.Tree() root = game.startstate tree.create_node(tag=None, identifier=root, parent=None, data=nodeData(0, 0)) while (iteration <= iterationMax): SelectedNode = Selection(tree, root, iteration) ExpandedNode = Expansion(tree, SelectedNode, game, state + SelectedNode) result = Simulation(game, state + ExpandedNode.identifier) BackPropagation(tree, ExpandedNode, result) iteration = iteration + 1 print(list(map(lambda x: x.identifier, tree.children("")))) print(list(map(lambda x: x.data.tries, tree.children("")))) print(list(map(lambda x: x.data.win, tree.children("")))) bestMove1 = numpy.argmax( list(map(lambda x: x.data.tries, tree.children("")))) bestMove = tree.children("")[bestMove1].identifier return bestMove
def __init__(self): self._nodes = {} self._gameTree = treelib.Tree() self._gameTree.node_class = GameState gameRoot = self._gameTree.create_node('Root', 'Root') notStart = self._gameTree.create_node('NotStart', 'NotStart', parent=gameRoot) loginIn = self._gameTree.create_node('LoginIn', 'LoginIn', parent=notStart) battleNet = self._gameTree.create_node('BattleNet', 'BattleNet', parent=loginIn) hearthstone = self._gameTree.create_node('Hearthstone', 'Hearthstone', parent=battleNet) self._gameTree.create_node('Welcome', 'Welcome', parent=hearthstone) battleMode = self._gameTree.create_node('BattleMode', 'BattleMode', parent=hearthstone) adventureMode = self._gameTree.create_node('AdventureMode', 'AdventureMode', parent=hearthstone) self._currentState = None self._gameGraph = networkx.DiGraph()
def dict2treelib(name: str, nested_dict: dict) -> treelib.Tree: """Convert a nested dictonary to a treelib Tree object. This function is recursive. The treelib representation of the trees is used for pretty-printing (in ASCII) of the tree, you only need to do str() of the returned result from this function. See `https://treelib.readthedocs.io/` Args: name: name of root node nested_dict: nested dictonary of the children at the root. """ warnings.warn( "dict2treelib() is deprecated and will be removed, use tree_from_dict()", FutureWarning, ) tree = treelib.Tree() tree.create_node(name, name) for child in nested_dict.keys(): tree.paste(name, dict2treelib(child, nested_dict[child])) return tree
def get_dir_map(top, gitignore=None): """Map the main project directory.""" # TODO-ROB: Change ignore to a .gitignore filename default_ignore = git_ignore(os.getcwd()) if gitignore is not None: gitignore += default_ignore else: gitignore = default_ignore # Treelib will help to map everything and create a json at the same time tree = treelib.Tree() tree.create_node('.', top) # Walk through the directory of choice (top) # Topdown is true so that directories can be modified in place for root, dirs, files in os.walk(top, topdown=True): # Only remove directories from the top if root == top: print(root) try: # Remove directories from os.walk() dirs[:] = set(dirs) - set(gitignore) print(dirs) except ValueError: pass for d in dirs: rd = str(Path(root) / Path(d)) tree.create_node(d, identifier=rd, parent=root) for f in files: tree.create_node(f, parent=root) return tree
def query(client, args): """Utility to fetch a vineyard object based on object_id.""" try: value = client.get_object(as_object_id(args.object_id)) print(f"The vineyard object you requested:\n{value}") except BaseException as exc: if args.exists: print( f"The object with object_id({args.object_id}) doesn't exists") raise Exception( ('The following error was encountered while fetching ' + f'the vineyard object({args.object_id}):')) from exc if args.exists: print(f'The object with object_id({args.object_id}) exists') if args.stdout: sys.stdout.write(str(value) + '\n') if args.output_file is not None: with open(args.output_file, 'w', encoding='UTF-8') as output_file: output_file.write(str(value)) if args.meta is not None: if args.meta == 'simple': print(f'Meta data of the object:\n{value.meta}') elif args.meta == 'json': json_meta = json.dumps(value.meta, indent=4) print(f'Meta data of the object in JSON format:\n{json_meta}') if args.metric is not None: print(f'{args.metric}: {getattr(value, args.metric)}') if args.tree: meta = client.get_meta(as_object_id(args.object_id)) tree = treelib.Tree() get_tree(meta, tree) tree.show(line_type="ascii-exr") if args.memory_status: meta = client.get_meta(as_object_id(args.object_id)) if args.detail: tree = treelib.Tree() memory_dict = {} get_tree(meta, tree, True, memory_dict) tree.show(line_type="ascii-exr") print( f'The object taking the maximum memory is:\n{max(memory_dict, key=lambda x: memory_dict[x])}' ) print( f'The total memory used: {pretty_format_memory(get_memory_used(meta))}' )
def __init__(self, nltk_tree): ''' create a tree for mvrnn network using a parsed nltk tree ''' self.tree = treelib.Tree() self.nNodes = 0 self.totalScore = None root = MVRNNNode(identifier=self.nNodes) self.tree.add_node(root) self._create_tree(root, nltk_tree)
def __init__(self, mode='classification'): ''' Parameters ---------- mode : 'classification' or 'regression' ''' self.__mode = mode self.__tree = treelib.Tree()