def build_edges_dict(graph: pgv.AGraph) -> dict: edges = {} for src_node, dest_node in graph.edges(): if src_node not in edges: edges[src_node] = [] edges[src_node].append(dest_node) return edges
def dot_to_plan_library(filename): G = AGraph() G.read(filename) planlib = set([]) for edge in G.edges(): a = Action(list(edge)) planlib.add(a) log.info("Loaded graph: " + str(planlib)) return planlib
def remove_bad_edges(graph: AGraph, collections: Iterable[Iterable[str]]): for from_node, to_node in graph.edges(): from_group = None to_group = None for i, group in enumerate(collections): if from_node in group: from_group = i if to_node in group: to_group = i if from_group is None or to_group is None or from_group < to_group: graph.delete_edge(from_node, to_node)
def get_graphviz(self, triple_hook=graphviz_triple_hook, node_hook=graphviz_node_hook, theme_options=VIS_THEME_OPTIONS, **hook_options): """ Create a pygraphviz graph from the tree @param triple_hook: a function that returns an attribute dict (or None) given a triple and the kargs @param node_hook: a function that returns a label given a node and the kargs @param theme_options: a dict-of-dicts containing global graph/node/edge attributes @param hook_options: additional arguments to pass to the hook functions """ def _id(node): return node.uri.split("/")[-1] g = AGraph(directed=True, strict=False) triples = list(self.get_triples()) # create nodes nodeset = set(chain.from_iterable((t.subject, t.object) for t in triples)) for n in sorted(nodeset, key=lambda n:n.id): g.add_node(_id(n), **node_hook(n, **hook_options)) connected = set() # create edges for triple in sorted(triples, key=lambda t:t.predicate): kargs = triple_hook(triple, **hook_options) if kargs: if kargs.get('reverse'): g.add_edge(_id(triple.object), _id(triple.subject), **kargs) else: g.add_edge(_id(triple.subject), _id(triple.object), **kargs) connected |= {_id(triple.subject), _id(triple.object)} connected = chain.from_iterable(g.edges()) for isolate in set(g.nodes()) - set(connected): g.remove_node(isolate) # some theme options for obj, attrs in theme_options.iteritems(): for k, v in attrs.iteritems(): getattr(g, "%s_attr" % obj)[k] = v return g
def create_graph(self): """Creates a pygraphviz graph of the architecture using the current configuration.""" architecture = self.architecture blocks = self.blocks ## Create raw graph ## if not pygraphviz_available: raise ImportError( 'pygraphviz package is required by create_graph method.') from pygraphviz import AGraph graph = AGraph('\n'.join(['digraph {'] + architecture.graph + ['}'])) ## Add architecture description ## self._set_architecture_description(graph, architecture) ## Set attributes of blocks ## self._set_block_attrs(graph, architecture.inputs, block_class='Input') self._set_block_attrs(graph, architecture.outputs, block_class='Output') self._set_block_attrs(graph, architecture.blocks) ## Add input/output descriptions ## for node in itertools.chain(architecture.inputs, architecture.outputs): self._set_node_description(graph, node) ## Add tensor shapes to edges ## for node_from, node_to in graph.edges(): self._set_edge_label(graph, blocks, node_from, node_to) ## Set block properties ## for block in architecture.blocks: self._set_block_label(graph, block) ## Create subgraphs ## self._add_subgraphs(graph, architecture.blocks, dict(blocks), depth=2) return graph
def igraph_from_dot(file): """numbered_graph(file) -> graph Input: file: the name of the dot-file Output: graph: igraph.Graph object. Verticies has the attribute "name". """ print("Step 1/3: Reading from dot.") a=AGraph(file) vertices = a.nodes() edges = a.edges() numbered_edges = [] print("Step 2/3: To numbered graph. Be patient...") for x, y in edges: xx = vertices.index(x) yy = vertices.index(y) numbered_edges.append((xx,yy)) print("Step 3/3: To igraph.") g=igraph.Graph(len(vertices), numbered_edges) g.vs["name"]=vertices return g
def get_graphviz_layout(input_subgraph, ref_chain=[], aim_chain=[], freq_min=1, draw_all=False): print('SD') ps = AGraph(directed=True) ps.graph_attr['rankdir'] = 'LR' ps.graph_attr['mode'] = 'hier' nodes = delete_nodes(ref_chain, input_subgraph[1], freq_min) print('Number of nodes: ', end='') if draw_all == False: print(len(nodes)) elif draw_all == True: print(len(input_subgraph[1])) cluster_main = ps.add_subgraph() cluster_main.graph_attr['rank'] = '0' for i in range(len(ref_chain)): shape = 'circle' if ref_chain[i] in aim_chain: cluster_main.add_node(ref_chain[i], shape=shape, color='red') else: cluster_main.add_node(ref_chain[i], shape=shape, color='pink') for i in input_subgraph[0]: if i in aim_chain or i in ref_chain: continue if i in nodes: ps.add_node(str(i)) continue else: if draw_all == True: ps.add_node(str(i), color='grey') continue for i in input_subgraph[1]: color = 'black' try: if ref_chain.index(i[1]) - ref_chain.index(i[0]) == 1: color = 'red' ps.add_edge(str(i[0]), str(i[1]), color=color, penwidth=str(math.sqrt(i[2]))) continue except ValueError: pass if i[2] < freq_min: if draw_all == True: ps.add_edge(str(i[0]), str(i[1]), color='grey', penwidth=str(math.sqrt(i[2])), constraint='false') continue elif i[0] in nodes and i[1] in nodes: ps.add_edge(str(i[0]), str(i[1]), color=color, penwidth=str(math.sqrt(i[2]))) elif draw_all == True: ps.add_edge(str(i[0]), str(i[1]), color='grey', penwidth=str(math.sqrt(i[2])), constraint='false') ps.layout(prog='dot') positions = {n: n.attr['pos'] for n in ps.nodes()} edge_points = {edge: edge.attr['pos'] for edge in ps.edges()} return positions, edge_points
class UiGraphio(QMainWindow): """ Main window for application """ def __init__(self): """ Constructor """ QMainWindow.__init__(self) self.ui = graphio.Ui_MainWindow() self.ui.setupUi(self) self.scene_graph = QGraphicsScene() self.ui.graphicsView.setScene(self.scene_graph) self.ui.graphicsView.update() self.graph = AGraph(strict=True, directed=True) self.graph.layout(prog='dot') ##################################################### # View update ##################################################### def update_adj_matrix(self): count = self.graph.number_of_nodes() self.ui.tw_adjmatrix.setRowCount(count) self.ui.tw_adjmatrix.setColumnCount(count) self.ui.tw_adjmatrix.setHorizontalHeaderLabels(self.graph.nodes()) self.ui.tw_adjmatrix.setVerticalHeaderLabels(self.graph.nodes()) for (i, u) in enumerate(self.graph.nodes()): for (j, v) in enumerate(self.graph.nodes_iter()): if self.graph.has_edge(u, v): self.ui.tw_adjmatrix.setItem( i, j, QTableWidgetItem( self.graph.get_edge(u, v).attr['label'])) else: self.ui.tw_adjmatrix.setItem(i, j, QTableWidgetItem(str(0))) def update_matrix_incidence(self): nodes = self.graph.nodes() edges = self.graph.edges() self.ui.tw_incmatrix.setRowCount(len(nodes)) self.ui.tw_incmatrix.setColumnCount(len(edges)) self.ui.tw_incmatrix.setHorizontalHeaderLabels( [str(node) for node in self.graph.edges()]) self.ui.tw_incmatrix.setVerticalHeaderLabels(self.graph.nodes()) for (i, u) in enumerate(nodes): for (j, edg) in enumerate(edges): value = 0 if (u == edg[0]): value = 1 elif (u == edg[1]): value = -1 self.ui.tw_incmatrix.setItem(i, j, QTableWidgetItem(str(value))) def update_list_edges(self): edges = self.graph.edges() self.ui.tw_edges.setRowCount(len(edges)) for (i, edge) in enumerate(edges): self.ui.tw_edges.setItem(i, 0, QTableWidgetItem(edge[0])) self.ui.tw_edges.setItem(i, 1, QTableWidgetItem(edge[1])) def update_adj_list(self): nodes = self.graph.nodes() self.ui.tw_adjlist.setRowCount(self.graph.number_of_nodes()) self.ui.tw_adjlist.setVerticalHeaderLabels(nodes) for (i, node) in enumerate(nodes): value = '' for adj in self.graph.out_edges(node): value += adj[1] + ', ' self.ui.tw_adjlist.setItem(i, 0, QTableWidgetItem(value[:-2])) ##################################################### # Reset editors ##################################################### def add_cb_item(self, label): n = self.ui.cb_nodes.count() i = 0 while i < n: itext = self.ui.cb_nodes.itemText(i) if label == itext: return elif label < itext: break i += 1 # insert item to lists self.ui.cb_nodes.insertItem(i, label) self.ui.cb_starting_node.insertItem(i, label) self.ui.cb_ending_node.insertItem(i, label) @pyqtSlot(bool, name='on_bt_add_node_clicked') @pyqtSlot(bool, name='on_bt_del_node_clicked') @pyqtSlot(bool, name='on_bt_add_edge_clicked') @pyqtSlot(bool, name='on_bt_del_edge_clicked') def reset_editors(self): self.ui.cb_nodes.clearEditText() self.ui.cb_starting_node.clearEditText() self.ui.cb_ending_node.clearEditText() self.ui.sb_weight_edge.setMinimum() def redraw(self): self.graph.draw('graph', 'png', 'dot') self.scene_graph.clear() self.scene_graph.addItem(QGraphicsPixmapItem(QPixmap('graph'))) self.ui.graphicsView.update() ##################################################### # Buttons actions ##################################################### @pyqtSlot() def on_bt_add_node_clicked(self): """ Slot when click on Add node button """ label = self.ui.cb_nodes.currentText() if not self.graph.has_node(label): self.add_cb_item(label) self.graph.add_node(label) self.redraw() @pyqtSlot() def on_bt_del_node_clicked(self): """ Slot when click on Delete node button """ index = self.ui.cb_nodes.currentIndex() label = self.ui.cb_nodes.currentText() if index > -1 and self.graph.has_node(label): self.graph.remove_node(label) self.redraw() self.ui.cb_nodes.removeItem(index) self.ui.cb_starting_node.removeItem(index) self.ui.cb_ending_node.removeItem(index) @pyqtSlot() def on_bt_add_edge_clicked(self): """ Slot when click on Add branch button """ start = self.ui.cb_starting_node.currentText() end = self.ui.cb_ending_node.currentText() weight = self.ui.sb_weight_edge.value() if start and end: self.add_cb_item(start) self.add_cb_item(end) self.graph.add_edge(start, end, label=weight) self.redraw() @pyqtSlot() def on_bt_del_edge_clicked(self): """ Slot when click on Delete branch button """ start = self.ui.cb_starting_node.currentText() end = self.ui.cb_ending_node.currentText() weight = self.ui.sb_weight_edge.value() if start and end and self.graph.has_edge(start, end): self.graph.remove_edge(start, end) self.redraw() @pyqtSlot(int) @pyqtSlot(bool, name='on_bt_add_node_clicked') @pyqtSlot(bool, name='on_bt_del_node_clicked') @pyqtSlot(bool, name='on_bt_add_edge_clicked') @pyqtSlot(bool, name='on_bt_del_edge_clicked') def on_toolbox_view_currentChanged(self, index): index = self.ui.toolbox_view.currentIndex() if index == 0: self.update_adj_matrix() elif index == 1: self.update_matrix_incidence() elif index == 2: self.update_list_edges() elif index == 3: self.update_adj_list()