def get_available_merc_base_classes(self): class_source = csv.DictReader(open(BaseStats.MERC_CLASS_CSV)) for row in class_source: class_tree = treelib.Tree() main_node = treelib.Node(tag=row["Key"], identifier=row["Key"], data=[row["Master1"], row["Master2"]]) class_tree.add_node(main_node) if row["Master1"] != "None": master_1_node = treelib.Node(tag=row["Master1"], identifier=row["Master1"]) class_tree.add_node(master_1_node, main_node) if row["Master2"] != "None": master_2_node = treelib.Node(tag=row["Master2"], identifier=row["Master2"]) class_tree.add_node(master_2_node, main_node) if class_tree.size() == 1: continue else: self.merc_class_trees.append(class_tree)
def create_node(board, parent=None): sequence = [board_to_board_array(board)] if parent: sequence = parent.data['sequence'] + sequence # tag = moves_to_string(sequence_to_moves(board)) # tag = '\n'.join([fen.split()[0] # for fen in [board_array_to_board(board_array, swap_axes=False).fen() # for board_array in sequence]]) node = treelib.Node(data={'board': board, 'sequence': sequence}) return node
def add_cables(self): # add cables in between all component "edges" (sets of two linked components) cable_index = get_largest_index(self._sink_tree) + 1 edges = get_tree_edges(self._sink_tree) for edge in edges: new_node = treelib.Node("Cable " + str(cable_index), cable_index, data=Cable([0, 0, 0])) new_node.data.name = "Cable " + str(cable_index) cable_index += 1 self._sink_tree = link_into_edge(new_node, edge, self._sink_tree) self.reset_components()
def save2lines(self, point1): global traced_lines, traced_tree, cur_picked_point if traced_tree.size() == 0: tree_root = treelib.Node([point1.tolist()]) traced_tree.add_node(tree_root, parent=None) else: for node_id in traced_tree.expand_tree(traced_tree.root, mode=treelib.Tree.WIDTH): this_node = traced_tree.get_node(node_id) this_line = this_node.tag if cur_picked_point.tolist() in this_line: cur_point_index = this_line.index(cur_picked_point.tolist()) if cur_point_index == 0 and len(this_line) != 1: this_line.insert(0, point1.tolist()) this_node.tag = this_line elif cur_point_index == 0 and len(this_line) == 1: this_line.append(point1.tolist()) this_node.tag = this_line elif cur_point_index == len(this_line) - 1: this_line.append(point1.tolist()) this_node.tag = this_line else: new_node = treelib.Node([cur_picked_point.tolist(), point1.tolist()]) traced_tree.add_node(new_node, parent=this_node.identifier) break
def split_by_num_loads(self, max_num_loads): """ Splits subtrees of panels into new panels based on number of loads. :param max_num_loads: maximum number of loads per panel """ panels = list_of_type(self._sink_tree, Panel) new_panels = list() for panel in panels: load_center_count = 1 # we want to check each child of every panel and sort it into a better-fitting new panel if # it does not fit within the maximum distance of its original parent panel if len(self._sink_tree.is_branch( panel.identifier)) > max_num_loads: # split branch in half! children = [ self._sink_tree.get_node(nid) for nid in self._sink_tree.is_branch(panel.identifier) ] left_children = children[:len(children) // 2] # attach left_children to new panel location = list( map(sum, zip(left_children[0].data.location, [1, 1, 0]))) # prevent zero-length cable left_panel = Panel(location) left_panel.name = panel.data.name + "-" + str( load_center_count) left_panel.group = panel.data.group parent = self._sink_tree.parent(panel.identifier) identifier = get_largest_index(self._sink_tree) + 1 # TODO: use default identifiers to avoid confusion left_panel_node = treelib.Node(tag=left_panel.name, identifier=identifier, data=left_panel) self._sink_tree.add_node(node=left_panel_node, parent=parent) new_panels.append(left_panel_node) chosen_panel = left_panel_node for child in left_children: self._sink_tree.move_node(child.identifier, chosen_panel.identifier) load_center_count += 1 self.reset_components() # does this need to be run?
def split_by_current(self, max_current): """ Splits subtrees of panels into new panels based on specified max current. :param max_current: maximum current in Amps """ panels = list_of_type(self._sink_tree, Panel) new_panels = list() for panel in panels: load_center_count = 1 children = [ self._sink_tree.get_node(nid) for nid in self._sink_tree.is_branch(panel.identifier) ] current_load = sum( [child.data.power_in.current for child in children]) if current_load > max_current: left_children = children[:len(children) // 2] # attach left_children to new panel location = list( map(sum, zip(left_children[0].data.location, [1, 1, 0]))) # prevent zero-length cable left_panel = Panel(location) left_panel.name = panel.data.name + "-" + str( load_center_count) left_panel.group = panel.data.group parent = self._sink_tree.parent(panel.identifier) identifier = get_largest_index(self._sink_tree) + 1 # TODO: use default identifiers to avoid confusion left_panel_node = treelib.Node(tag=left_panel.name, identifier=identifier, data=left_panel) self._sink_tree.add_node(node=left_panel_node, parent=parent) new_panels.append(left_panel_node) chosen_panel = left_panel_node for child in left_children: self._sink_tree.move_node(child.identifier, chosen_panel.identifier) load_center_count += 1 self.reset_components() # does this need to be run?
def _add_children_nodes(tree, parent): """Iteratively add filterset sniffers Parameters ---------- tree : :class:`treelib.Tree` instance tree that recapitulates folder organization and sniffing content parent : :class:`treelib.Node` instance parent node (usually analysis node) to which filterset nodes will be associated Yields ------ fnode : :class:`treelib.Node` instance child node to parent, containing """ for item in parent.data._contains: path = os.path.join(parent.data.path, item) fold = SniffFolder(path) node = treelib.Node(tag=fold.label, data=fold) tree.add_node(node, parent=parent.identifier) _add_children_nodes(tree, node) return
def sniff(self, path=None): if self.path is None: if path is None: msg = ('Load an experiment folder path as argument please.') warnings.warn(msg) return else: self.path = path # analysis = os.path.join(self.path, 'analysis') # if not os.path.exists(analysis): # msg = ('No analysis folder under {}'.format(self.path)) # warnings.warn(msg) # return fold = SniffFolder(self.path) root = treelib.Node(tag=self.label, data=fold) root.level = 0 # necessary for later purposes tree = treelib.Tree() tree.add_node(root, parent=None) _add_children_nodes(tree, root) self._tree = tree tree.show() return
#coding:utf-8 import treelib as tb t = tb.Tree() print t.size() a = tb.Node([1]) b = tb.Node([2]) c = tb.Node([3]) d = tb.Node([4]) e = tb.Node([5]) t.add_node(a, parent=None) t.add_node(b, parent=a.identifier) t.add_node(c, parent=b.identifier) t.add_node(d, parent=a.identifier) t.add_node(e, parent=d.identifier) sub_t1 = t.subtree(b.identifier) print sub_t1.size() tb.Tree.WIDTH for node_id in t.expand_tree(t.root, mode=t.WIDTH): print type(t.get_node(node_id).tag) t.show() sub_t1.show() print t.is_branch(e.identifier) print t
def split_by_distance(self, max_distance): """ Splits subtrees of panels into new panels based on distance between groups. :param max_distance: maximum distance between loads and panel """ # There are two good methods here. The easiest will be to separate the ship into quadrants but will require # additional data. The harder will be to separate components into clusters based on K-Means Clustering. # The simple method is to place the panel next to the location of the first component, then enforce a strict # distance limit on its other components. If one component does not meet this criterion, we create a new panel. # This third method is not great but will do for the current scope. def count(name): test = re.findall(r'\d+', name) res = list(map(int, test)) if len(res) < 2: return -1 else: return res[1] def increment_count(name): counter = count(name) if counter < 0: return name + "-1" else: return name.replace("-" + str(counter), "-" + str(counter + 1)) panels = list_of_type(self._sink_tree, Panel) new_panels = list() for panel in panels: load_center_count = 1 # we want to check each child of every panel and sort it into a better-fitting new panel if # it does not fit within the maximum distance of its original parent panel for child in [ self._sink_tree.get_node(nid) for nid in self._sink_tree.is_branch(panel.identifier) ]: if taxicab_ship_distance(child.data.location, panel.data.location) < max_distance: # keep association with current panel pass else: # TODO: fix multiple associations with new panel reference chosen_panel = None # attach to panel of same hierarchy with better fit if new_panels: # attempt to choose existing panel for new_panel in new_panels: if taxicab_ship_distance(child.data.location, new_panel.data.location) < max_distance \ and new_panel.data.group == panel.data.group: chosen_panel = new_panel else: # create new panel and add to list of new_panels # choose new panel location = list( map(sum, zip(child.data.location, [1, 1, 0]))) # prevent zero-length cable new_panel = Panel(location) new_panel.name = increment_count(panel.data.name) new_panel.group = panel.data.group parent = self._sink_tree.parent(panel.identifier) identifier = get_largest_index(self._sink_tree) + 1 # TODO: use default identifiers to avoid confusion new_panel_node = treelib.Node(tag=new_panel.name, identifier=identifier, data=new_panel) self._sink_tree.add_node(node=new_panel_node, parent=parent) new_panels.append(new_panel_node) chosen_panel = new_panel_node # attach the misfit component to the chosen panel self._sink_tree.move_node(child.identifier, chosen_panel.identifier) load_center_count += 1 self.reset_components() # does this need to be run?