def __init__(self, metabolicModel, lists, logicals): self.G = PD.Dot() self.G.set_center('true') self.graphModel = DotModel(metabolicModel, self, logicals) self.graphModel.insertLists(lists) path = IMGFILE self.G.write(path, format=FORMAT) self.setContentType("text/html") self.setText(SHOWTEXT)
def dotgraph(s, direction="BT", **kwargs): """ Creates and returns a pydot graph structure that represents an SX. direction one of "BT", "LR", "TB", "RL" """ try: def getHashSX(e): if e.is_scalar(True): try: return e.element_hash() except: return SX__hash__backup(e) else: return 0 SX__hash__backup = SX.__hash__ SX.__hash__ = getHashSX # Get the dependencies and inverse dependencies in a dict dep, invdep = dependencyGraph(s, {}, {}) allnodes = set(dep.keys()).union(set(invdep.keys())) #print "a", set(dep.keys()), [i.__hash__() for i in dep.keys()] #print "b", set(invdep.keys()), [i.__hash__() for i in invdep.keys()] #print "allnodes", allnodes, [i.__hash__() for i in allnodes] #return None artists = {} graph = pydot.Dot('G', graph_type='digraph', rankdir=direction) for node in allnodes: artists[node] = createArtist(node, dep=dep, invdep=invdep, graph=graph, artists=artists, **kwargs) for artist in artists.values(): if artist is None: continue artist.draw() open('source.dot', 'w').write(graph.to_string()) finally: SX.__hash__ = SX__hash__backup return graph
def visit_FunctionNode(self, node): "Create a graphviz graph" self.graph = pydot.Dot(self.name, graph_type='digraph') pydot_function = self.create_node(node) pydot_body = self.visit(node.body) # Create artificial arguments for brevity pydot_args = pydot.Node("Arguments (omitted)") self.add_node(pydot_args) self.add_edge(pydot_function, pydot_body) self.add_edge(pydot_function, pydot_args) return self.graph
def write_hypergraph(hgr, colored=False): """ Return a string specifying the given hypergraph in DOT Language. @type hgr: hypergraph @param hgr: Hypergraph. @type colored: boolean @param colored: Whether hyperedges should be colored. @rtype: string @return: String specifying the hypergraph in DOT Language. """ dotG = pydot.Dot() if not 'name' in dir(hgr): dotG.set_name('hypergraph') else: dotG.set_name(hgr.name) colortable = {} colorcount = 0 # Add all of the nodes first for node in hgr.nodes(): newNode = pydot.Node(str(node), hyper_node_type='hypernode') dotG.add_node(newNode) for hyperedge in hgr.hyperedges(): if (colored): colortable[hyperedge] = colors[colorcount % len(colors)] colorcount += 1 newNode = pydot.Node(str(hyperedge), hyper_node_type = 'hyperedge', \ color = str(colortable[hyperedge]), \ shape = 'point') else: newNode = pydot.Node(str(hyperedge), hyper_node_type='hyperedge') dotG.add_node(newNode) for link in hgr.links(hyperedge): newEdge = pydot.Edge(str(hyperedge), str(link)) dotG.add_edge(newEdge) return dotG.to_string()
def write(G, weighted=False): """ Return a string specifying the given graph in Dot language. @type G: graph @param G: Graph. @type weighted: boolean @param weighted: Whether edges should be labelled with their weight. @rtype: string @return: String specifying the graph in Dot Language. """ dotG = pydot.Dot() if not 'name' in dir(G): dotG.set_name('graphname') else: dotG.set_name(G.name) if (isinstance(G, graph)): dotG.set_type('graph') directed = False elif (isinstance(G, digraph)): dotG.set_type('digraph') directed = True elif (isinstance(G, hypergraph)): return write_hypergraph(G) else: raise InvalidGraphType("Expected graph or digraph, got %s" % repr(G)) for node in G.nodes(): attr_list = {} for attr in G.node_attributes(node): attr_list[str(attr[0])] = str(attr[1]) newNode = pydot.Node(str(node), **attr_list) dotG.add_node(newNode) # Pydot doesn't work properly with the get_edge, so we use # our own set to keep track of what's been added or not. seen_edges = set([]) for edge_from, edge_to in G.edges(): if (str(edge_from) + "-" + str(edge_to)) in seen_edges: continue if (not directed) and (str(edge_to) + "-" + str(edge_from)) in seen_edges: continue attr_list = {} for attr in G.edge_attributes((edge_from, edge_to)): attr_list[str(attr[0])] = str(attr[1]) if str(G.edge_label((edge_from, edge_to))): attr_list['label'] = str(G.edge_label((edge_from, edge_to))) elif weighted: attr_list['label'] = str(G.edge_weight((edge_from, edge_to))) if weighted: attr_list['weight'] = str(G.edge_weight((edge_from, edge_to))) newEdge = pydot.Edge(str(edge_from), str(edge_to), **attr_list) dotG.add_edge(newEdge) seen_edges.add(str(edge_from) + "-" + str(edge_to)) return dotG.to_string()