def report_trajectories(res, name_obs=False): r = Report() f = r.figure() import networkx as nx G0 = create_graph_trajectories0(res['trajectories'], label_state=False, name_obs=name_obs) f.data('tree0', nx.to_pydot(G0).create_png(), mime=MIME_PNG) G0 = create_graph_trajectories0(res['trajectories'], label_state=True, name_obs=name_obs) f.data('tree1', nx.to_pydot(G0).create_png(), mime=MIME_PNG) # # G1 = create_graph_trajectories(res['decisions']) # d1 = nx.to_pydot(G1) # d is a pydot graph object, dot options can be easily set # # r.data('tree1', d.create_pdf(), mime=MIME_PDF) # f.data('tree1', d1.create_png(), mime=MIME_PNG) # # # # if 'decisions_dis' in res: # G2 = create_graph_trajectories(res['decisions_dis']) # d2 = nx.to_pydot(G2) # d is a pydot graph object, dot options can be easily set # f.data('tree2', d2.create_png(), mime=MIME_PNG) return r
def file_to_dot(infile): interactions = defaultdict(lambda: 0) es = list(edges(infile)) for a, b, users in es: interactions[a, b] = users keys, ratio = compute_ratios(interactions, lambda k: interactions[k, k] > 5) G = nx.Graph() for a, b, users in es: if a > b and a in keys and b in keys and users > 0 and ratio[a, b] > 0: rat = (ratio[a, b] + ratio[b, a]) / 2 G.add_edge(a, b, { 'ratio': rat, 'users': users, 'connection': rat**-1 }) btwn = nx.edge_betweenness_centrality(G, weight='connection') GG = nx.Graph() GG.add_edges_from([(a, b, { 'weight': val }) for (a, b), val in btwn.items()]) # GG.add_edges_from([(a, b, merge({'betweenness':btwn[a,b]}, G[a][b])) # for a,b in G.edges_iter()]) for node in GG.nodes_iter(): GG.node[node]['height'] = GG.node[node]['width'] = size(G, node) GG.node[node]['color'] = color(G, node) Gtree = nx.minimum_spanning_tree(GG) Gtree_dot = nx.to_pydot(Gtree) return Gtree_dot.to_string()
def parse_tree(self, string, explicit=False, display=False): ''' Returns graph representation of the equation tree of string, as a networkx graph. If explicit is True, then '-x' -> '0-x' ''' stack, indices = self.parse(string) tree = EquationTreeParser._create_tree(stack, indices) if explicit: idx = -1 for node, data in tree.nodes(data=True): if data['label'] == '-' and len(tree[node]) == 1: nbr = tree[node].keys()[0] tree.remove_edge(node, nbr) tree.add_node(idx, label='0') tree.add_edge(node, idx, label='left') tree.add_edge(node, nbr, label='right') idx -= 1 if display: _, image_path = tempfile.mkstemp() pydot_graph = nx.to_pydot(tree) pydot_graph.write_png(image_path) cv2.imshow('equation tree', cv2.imread(image_path)) cv2.waitKey() cv2.destroyAllWindows() return tree
def build_graph(self, G): G.add_edge("A", "B") G.add_edge("A", "C") G.add_edge("B", "C") G.add_edge("A", "D") G.add_node("E") return G, nx.to_pydot(G)
def visualize_mcts_tree(mcts, depth, filename): """ Creates a small subgraph for visualization with a number of levels equal to 2 + depth labelled with the MCTS values from mcts and saves it as filename.png """ # Find root of the MCTS tree mcts_root = nx.topological_sort(mcts.digraph)[0] # root = GameState() subgraph = nx.DiGraph() # Don't include the empty board (the root) in the graphs # for first_move in mcts.digraph.successors(root): print(mcts_root) print(type(mcts_root)) for first_move in mcts.digraph.successors(mcts_root): add_edges(mcts.digraph, subgraph, first_move, depth) dot_graph = nx.to_pydot(subgraph) for node in dot_graph.get_nodes(): attr = node.get_attributes() try: node.set_label('{}{}/{}\n{:.2f}'.format(attr['state'], int(attr['w']), int(attr['n']), float(attr['uct']))) except KeyError: pass dot_graph.set_graph_defaults(fontname='Courier') dot_graph.set_rankdir('LR') dot_graph.write_png('{}.png'.format(filename))
def dump(self, output=False): """Prints out network and returns networkx graph Prints the devices, links, and flows associated with the network, and returns a pydot object with the network graph. Parameters ---------- output : boolean, optional Specifies whether to print the network information (the default is False). Returns ------- pydot pydot object containing the network graph """ # Print network information if output is True if output: print "Devices:\n" for device_id in self.devices: print self.devices[device_id] print "Links:\n" for link_id in self.links: print self.links[link_id] print "Flows:\n" for flow_id in self.flows: print self.flows[flow_id] # Convert the graph to a pydot object and return return nx.to_pydot(self.g)
def dot_graph(filename='conversions'): # Edges from Convert dg = nx.DiGraph() for a, b in convert.graph.edges(): cost = convert.graph.edge[a][b]['cost'] dg.add_edge(cls_name(a), cls_name(b), cost=cost, penwidth=max(log(1. / (cost + 0.06)), 1)) # Edges from Append for a, b in append.funcs: if b is not object and a != b: dg.add_edge(cls_name(b), cls_name(a), color='blue') # Color edges for n in convert.graph.nodes() + list(pluck(0, append.funcs)): if issubclass(n, tuple(ooc_types)): dg.node[cls_name(n)]['color'] = 'red' # Convert to pydot p = nx.to_pydot(dg) p.set_overlap(False) p.set_splines(True) with open(filename + '.dot', 'w') as f: f.write(p.to_string()) os.system('neato -Tpdf %s.dot -o %s.pdf' % (filename, filename)) print("Writing graph to %s.pdf" % filename) os.system('neato -Tpng %s.dot -o %s.png' % (filename, filename)) print("Writing graph to %s.png" % filename)
def build_graph(self, G): G.add_edge('A','B') G.add_edge('A','C') G.add_edge('B','C') G.add_edge('A','D') G.add_node('E') return G, nx.to_pydot(G)
def parse_tree(self, string, explicit=False, display=False): ''' Returns graph representation of the equation tree of string, as a networkx graph. If explicit is True, then '-x' -> '0-x' ''' stack, indices = self.parse(string) tree = ExpressionParser._create_tree(stack, indices) if explicit: idx = -1 for node, data in tree.nodes(data=True): if data['label'] == '-' and len(tree[node]) == 1: nbr = tree[node].keys()[0] tree.remove_edge(node, nbr) tree.add_node(idx, label='0') tree.add_edge(node, idx, label='left') tree.add_edge(node, nbr, label='right') idx -= 1 if display: """ For some reason, displaying the tree doesn't work now. """ _, image_path = tempfile.mkstemp() pydot_graph = nx.to_pydot(tree) pydot_graph.write_png(image_path) cv2.imshow('equation tree', cv2.imread(image_path)) cv2.waitKey() cv2.destroyAllWindows() return tree
def write_graph2dot(graph, other_graphs, c_fname, img_fname, for_latex, multi_page, layout): if pydot is None: print('Pydot not found. Exporting using pycflow2dot.write_dot_file().') dot_str = dump_dot_wo_pydot(graph, other_graphs, c_fname, for_latex=for_latex, multi_page=multi_page) dot_path = write_dot_file(dot_str, img_fname) else: # dump using networkx and pydot if hasattr(nx,"nx_pydot"): pydot_graph = nx.nx_pydot.to_pydot(graph) else: pydot_graph = nx.to_pydot(graph) pydot_graph.set_splines('true') if layout == 'twopi': pydot_graph.set_ranksep(5) pydot_graph.set_root('main') else: pydot_graph.set_overlap(False) pydot_graph.set_rankdir('LR') dot_path = img_fname +'.dot' pydot_graph.write(dot_path, format='dot') return dot_path
def pydot_checks(self, G): G.add_edge('A', 'B') G.add_edge('A', 'C') G.add_edge('B', 'C') G.add_edge('A', 'D') G.add_node('E') P = nx.to_pydot(G) G2 = G.__class__(nx.from_pydot(P)) assert_graphs_equal(G, G2) fname = tempfile.mktemp() assert_true(P.write_raw(fname)) Pin = pydotplus.graph_from_dot_file(fname) n1 = sorted([p.get_name() for p in P.get_node_list()]) n2 = sorted([p.get_name() for p in Pin.get_node_list()]) assert_true(n1 == n2) e1 = [(e.get_source(), e.get_destination()) for e in P.get_edge_list()] e2 = [(e.get_source(), e.get_destination()) for e in Pin.get_edge_list()] assert_true(sorted(e1) == sorted(e2)) Hin = nx.drawing.nx_pydot.read_dot(fname) Hin = G.__class__(Hin) assert_graphs_equal(G, Hin)
def write_graph2dot(graph, other_graphs, c_fname, img_fname, for_latex, multi_page, layout): if pydot is None: print('Pydot not found. Exporting using pycflow2dot.write_dot_file().') dot_str = dump_dot_wo_pydot(graph, other_graphs, c_fname, for_latex=for_latex, multi_page=multi_page) dot_path = write_dot_file(dot_str, img_fname) else: # dump using networkx and pydot pydot_graph = nx.to_pydot(graph) pydot_graph.set_splines('true') if layout == 'twopi': pydot_graph.set_ranksep(5) pydot_graph.set_root('main') else: pydot_graph.set_overlap(False) pydot_graph.set_rankdir('LR') dot_path = img_fname + '.dot' pydot_graph.write(dot_path, format='dot') return dot_path
def dot_graph(filename='conversions'): # Edges from Convert dg = nx.DiGraph() for a, b in convert.graph.edges(): cost = convert.graph.edge[a][b]['cost'] dg.add_edge(cls_name(a), cls_name(b), cost=cost, penwidth=max(log(1./(cost + 0.06)), 1)) # Edges from Append for a, b in append.funcs: if b is not object and a != b: dg.add_edge(cls_name(b), cls_name(a), color='blue') # Color edges for n in convert.graph.nodes() + list(pluck(0, append.funcs)): if issubclass(n, tuple(ooc_types)): dg.node[cls_name(n)]['color'] = 'red' # Convert to pydot p = nx.to_pydot(dg) p.set_overlap(False) p.set_splines(True) with open(filename + '.dot', 'w') as f: f.write(p.to_string()) os.system('neato -Tpdf %s.dot -o %s.pdf' % (filename, filename)) print("Writing graph to %s.pdf" % filename) os.system('neato -Tpng %s.dot -o %s.png' % (filename, filename)) print("Writing graph to %s.png" % filename)
def build_graph(self, G): G.add_edge('A', 'B') G.add_edge('A', 'C') G.add_edge('B', 'C') G.add_edge('A', 'D') G.add_node('E') return G, nx.to_pydot(G)
def pydot_checks(self, G): G.add_edge('A','B') G.add_edge('A','C') G.add_edge('B','C') G.add_edge('A','D') G.add_node('E') P = nx.to_pydot(G) G2 = G.__class__(nx.from_pydot(P)) assert_graphs_equal(G, G2) fname = tempfile.mktemp() assert_true( P.write_raw(fname) ) Pin = pydotplus.graph_from_dot_file(fname) n1 = sorted([p.get_name() for p in P.get_node_list()]) n2 = sorted([p.get_name() for p in Pin.get_node_list()]) assert_true( n1 == n2 ) e1=[(e.get_source(),e.get_destination()) for e in P.get_edge_list()] e2=[(e.get_source(),e.get_destination()) for e in Pin.get_edge_list()] assert_true( sorted(e1)==sorted(e2) ) Hin = nx.drawing.nx_pydot.read_dot(fname) Hin = G.__class__(Hin) assert_graphs_equal(G, Hin)
def _graph2pydot(graph, wrap=10, tikz=False, rankdir='TB'): """Convert (possibly labeled) state graph to dot str. @type graph: L{LabeledDiGraph} @rtype: str """ pydot = import_pydot() if pydot is None: return None dummy_nx_graph = nx.MultiDiGraph() _states2dot_str(graph, dummy_nx_graph, wrap=wrap, tikz=tikz, rankdir=rankdir) _transitions2dot_str(graph.transitions, dummy_nx_graph, tikz=tikz) pydot_graph = nx.to_pydot(dummy_nx_graph) _place_initial_states(graph, pydot_graph, tikz) pydot_graph.set_overlap('false') #pydot_graph.set_size('"0.25,1"') #pydot_graph.set_ratio('"compress"') pydot_graph.set_nodesep(0.5) pydot_graph.set_ranksep(0.1) return pydot_graph
def drawGraph(self, key=None, filename="graph", format="pdf"): graph = self.graphs.get(key) if( not graph ): return if( graph.number_of_nodes() > 1 ): ag = nx.to_pydot(graph) ag.write("%s.%s"%(filename,format),format=format)
def neato_from_networkx( g, min_node_size = 0.5, max_node_size = 2.0, min_edge_width = 1.0, max_edge_width = 5.0, legend_attribute=None, label_nodes_directly = False ) : d = nx.to_pydot( g ) d.set_overlap(False) # normalize node size nodewidths = array( [float(n.get_width()) for n in d.get_nodes()] ) # done this way in case input has all the same size to avoid divide by zero node_range = (nodewidths.max() - nodewidths.min())/(max_node_size - min_node_size) for n in d.get_nodes() : n.set_width( min_node_size + (float(n.get_width()) - nodewidths.min()) / node_range ) n.set_fixedsize( "true" ) n.set_shape('circle') # normalize edge width edge_widths = array( [float(e.get_penwidth()) for e in d.get_edges()] ) edge_width_range = (edge_widths.max() - edge_widths.min())/(max_edge_width - min_edge_width) for e in d.get_edges() : e.set_penwidth( min_edge_width + (float(e.get_penwidth()) - edge_widths.min() )/edge_width_range ) # if the legend attribute is set, create a legend node if label_nodes_directly : if legend_attribute == None : legend_attribute = 'labelval' for n in d.get_nodes() : n.set_label( n.get_attributes()[legend_attribute] ) else : legend = pydot.Node( "legend" ) nodelist = [n.get_label()+": "+n.get_attributes()[legend_attribute] for n in d.get_nodes()] nodelist.sort( lambda a,b : cmp( int( a.split(':')[0] ), int (b.split(':')[0] ) )) legend.set_label( "\l".join([x for x in nodelist])+"\l" ) legend.set_shape("box") d.add_node(legend) return d
def get_dot(G_orig): """Change labels and colors to be presented with graphviz""" G = G_orig.copy() cluster_networks.relabel_graph(G) tr = {} for node in G.nodes_iter(): tr[node] = '"{}"'.format(node) nx.relabel_nodes(G, tr, copy=False) for node in G.nodes_iter(): label = str(node) if len(label) > MAX_LABEL: label = u'{}..."'.format(label[:MAX_LABEL]) G.node[node]['label'] = label for node in cluster_networks.get_leaf_nodes(G): G.node[node]['color'] = "blue" for node in cluster_networks.hybrid_nodes(G): G.node[node]['color'] = "#7BFF74" # light green G.node[node]['style'] = 'filled' for node in cluster_networks.get_root_nodes(G): G.node[node]['color'] = "orange" for node in cluster_networks.problematic_treechild_nodes(G): G.node[node]['color'] = "#FF77EB" # light pink G.node[node]['style'] = 'filled' for u, v in cluster_networks.removable_edges(G): G.edge[u][v]['color'] = "red" for root in cluster_networks.get_root_nodes(G): G.node[root]['label'] = '"R"' dot = nx.to_pydot(G).to_string() return dot.strip()
def graph(self, out_file=None): if out_file is None: doc_dir = self.config["dir"].get("doc") out_file = os.path.join(doc_dir, "pipeline_viz.png") if file_exists(out_file): return out_file pd = nx.to_pydot(self.dag) pd.write_png(out_file, prog="dot")
def to_pydot(self, detailed=False): """Create GraphViz dot string from given AST. @type ast: L{ASTNode} @rtype: str """ g = ast_to_labeled_graph(self, detailed) return nx.to_pydot(g)
def plotGraph(self): cntxt = self.context["disease"] g = cntxt.getDocumentGraph() ag = nx.to_pydot(g, strict=True) gfile = os.path.join( self.save_dir, "report_%s_unc%s_%s_critical.pdf" % (self.proc_category, self.allow_uncertainty, self.currentCase)) ag.write(gfile, format="pdf")
def plotGraph(self): cntxt = self.context["disease"] g = cntxt.getDocumentGraph() ag = nx.to_pydot(g, strict=True) gfile = os.path.join(self.save_dir, "report_%s_unc%s_%s_critical.pdf"%(self.proc_category, self.allow_uncertainty, self.currentCase)) ag.write(gfile,format="pdf")
def print_ascii_graph(model_): """ pip install img2txt.py python -c """ from PIL import Image # NOQA from six.moves import StringIO # import networkx as nx import copy model = copy.deepcopy(model_) assert model is not model_ # model.graph.setdefault('graph', {})['size'] = '".4,.4"' model.graph.setdefault('graph', {})['size'] = '".3,.3"' model.graph.setdefault('graph', {})['height'] = '".3,.3"' pydot_graph = nx.to_pydot(model) png_str = pydot_graph.create_png(prog='dot') sio = StringIO() sio.write(png_str) sio.seek(0) pil_img = Image.open(sio) # NOQA logger.info('pil_img.size = %r' % (pil_img.size, )) # def print_ascii_image(pil_img): # img2txt = ut.import_module_from_fpath('/home/joncrall/venv/bin/img2txt.py') # import sys # pixel = pil_img.load() # width, height = pil_img.size # bgcolor = None # #fill_string = # img2txt.getANSIbgstring_for_ANSIcolor(img2txt.getANSIcolor_for_rgb(bgcolor)) # fill_string = "\x1b[49m" # fill_string += "\x1b[K" # does not move the cursor # sys.stdout.write(fill_string) # img_ansii_str = img2txt.generate_ANSI_from_pixels(pixel, width, height, bgcolor) # sys.stdout.write(img_ansii_str) def print_ascii_image(pil_img): # https://gist.github.com/cdiener/10491632 SC = 1.0 GCF = 1.0 WCF = 1.0 img = pil_img S = (int(round(img.size[0] * SC * WCF * 3)), int(round(img.size[1] * SC))) img = np.sum(np.asarray(img.resize(S)), axis=2) logger.info('img.shape = %r' % (img.shape, )) img -= img.min() chars = np.asarray(list(' .,:;irsXA253hMHGS#9B&@')) img = (1.0 - img / img.max())**GCF * (chars.size - 1) logger.info('\n'.join((''.join(r) for r in chars[img.astype(int)]))) print_ascii_image(pil_img) pil_img.close() pass
def write_networkx_to_dot(dg, filename='mydask'): import os p = nx.to_pydot(dg) p.set_rankdir('BT') with open(filename + '.dot', 'w') as f: f.write(p.to_string()) os.system('dot -Tpdf %s.dot -o %s.pdf' % (filename, filename)) os.system('dot -Tpng %s.dot -o %s.png' % (filename, filename)) print("Writing graph to %s.pdf" % filename)
def write_dot(G, path): # requires graphviz, pygraphviz, pydot g = nx.to_pydot(G) for n in g.get_nodes(): n.set_label(G.node_labels[int(n.get_name())]) for e in g.get_edges(): key = (int(e.get_destination()), int(e.get_source())) s = '\\n'.join(list(str(x) for x in G.edge_labels[key])) e.set_label(s) g.write_dot(path)
def compare_graph_network(self, goldnet, filename, topn=None): import networkx as nx #import Image as im import matplotlib as plt import pylab as P import pydot G = nx.DiGraph() net = self if topn != None: net = self.copy() net.set_top_edges_percent(topn) # Add all of the nodes for gene in net.network.keys(): G.add_node(gene) Gp = nx.to_pydot(G) if goldnet == []: for gene1 in net.network.keys(): for gene2 in net.network.keys(): if net.network[gene1][gene2] != 0: Gp.add_edge(pydot.Edge(gene1, gene2, style="bold")) else: for gene1 in net.network.keys(): for gene2 in net.network.keys(): # Figure out of node is correct, apply color/style based on that # If true positive, bold line if net.network[gene1][gene2] != 0 and goldnet.network[gene1][gene2] != 0: # thick line Gp.add_edge(pydot.Edge(gene1, gene2, style="bold")) # If false positive, dashed line elif net.network[gene1][gene2] != 0 and goldnet.network[gene1][gene2] == 0: Gp.add_edge(pydot.Edge(gene1, gene2, style="dashed")) # If false negative, dotted line elif net.network[gene1][gene2] == 0 and goldnet.network[gene1][gene2] != 0: Gp.add_edge(pydot.Edge(gene1, gene2, style="dotted")) # now output your graph to a file and display it outstem = filename dot = pydot.Dot(Gp) Gp.set_size('18!') Gp.set_rankdir('RL') Gp.set_page('20') Gp.set_ranksep(0.5) Gp.set_nodesep(0.25) #Gp.write_pdf(outstem + '_dot.pdf', prog='dot') # writes Gp to png file #use #Gp.write_pdf(outstem + '_neato.pdf', prog='neato') # writes Gp to png file #use #Gp.write_pdf(outstem + '_fdp.pdf', prog='fdp') # writes Gp to png file #use #Gp.write_pdf(outstem + '_twopi.pdf', prog='twopi') # writes Gp to png file #use Gp.write_pdf(outstem + '_circo.pdf', prog='circo') # writes Gp to png file #use
def show_pydot(g): """ Display a networkx graph using pydot. """ fd = tempfile.NamedTemporaryFile() fd.close() p = nx.to_pydot(g) p.write_jpg(fd.name) imdisp(fd.name) os.remove(fd.name)
def main(): global graph with open('tftpsrv.text.fixed.txt') as f: lines = f.readlines() graph_function(lines, 'strcpy') #graph_function(lines, 'FillScreen') #graph_function(lines, 'sprintf') pydot_graph = nx.to_pydot(graph) pydot_graph.write_png('unsafe.png')
def layer_dag_to_svg(): model = get_model() graph = nx.DiGraph() for layer in model.layers: graph.add_node(layer["name"], layer_attributes=layer) for layer in model.layers: for inputLayer in layer.get("inputs", []): graph.add_edge(model.layers[inputLayer]["name"], layer["name"], label=model.layers[inputLayer]["outputs"]) pydot_graph = nx.to_pydot(graph) pydot_graph.set_rankdir("LR") svg = pydot_graph.create_svg(prog="dot") return Response(svg, mimetype="image/svg+xml")
def nx2dot(self, G): H = G.copy() for node1_id, node2_id in H.edges(): removed_list = [] for key, value in H.get_edge_data(node1_id, node2_id).items(): if isinstance(value, dict): removed_list.append(key) for key in removed_list: del H.edge[node1_id][node2_id][key] D = nx.to_pydot(H) return D
def print_ascii_graph(model_): """ pip install img2txt.py python -c """ from PIL import Image from six.moves import StringIO #import networkx as netx import copy model = copy.deepcopy(model_) assert model is not model_ # model.graph.setdefault('graph', {})['size'] = '".4,.4"' model.graph.setdefault('graph', {})['size'] = '".3,.3"' model.graph.setdefault('graph', {})['height'] = '".3,.3"' pydot_graph = netx.to_pydot(model) png_str = pydot_graph.create_png(prog='dot') sio = StringIO() sio.write(png_str) sio.seek(0) pil_img = Image.open(sio) print('pil_img.size = %r' % (pil_img.size,)) #def print_ascii_image(pil_img): # img2txt = ut.import_module_from_fpath('/home/joncrall/venv/bin/img2txt.py') # import sys # pixel = pil_img.load() # width, height = pil_img.size # bgcolor = None # #fill_string = # img2txt.getANSIbgstring_for_ANSIcolor(img2txt.getANSIcolor_for_rgb(bgcolor)) # fill_string = "\x1b[49m" # fill_string += "\x1b[K" # does not move the cursor # sys.stdout.write(fill_string) # img_ansii_str = img2txt.generate_ANSI_from_pixels(pixel, width, height, bgcolor) # sys.stdout.write(img_ansii_str) def print_ascii_image(pil_img): #https://gist.github.com/cdiener/10491632 SC = 1.0 GCF = 1.0 WCF = 1.0 img = pil_img S = (int(round(img.size[0] * SC * WCF * 3)), int(round(img.size[1] * SC))) img = np.sum( np.asarray( img.resize(S) ), axis=2) print('img.shape = %r' % (img.shape,)) img -= img.min() chars = np.asarray(list(' .,:;irsXA253hMHGS#9B&@')) img = (1.0 - img / img.max()) ** GCF * (chars.size - 1) print( "\n".join( ("".join(r) for r in chars[img.astype(int)]) ) ) print_ascii_image(pil_img) pil_img.close() pass
def write_networkx_to_dot(dg, filename='mydask'): import os try: p = nx.to_pydot(dg) except AttributeError: raise ImportError("Can not find pydot module. Please install.\n" " pip install pydot") p.set_rankdir('BT') with open(filename + '.dot', 'w') as f: f.write(p.to_string()) os.system('dot -Tpdf %s.dot -o %s.pdf' % (filename, filename)) os.system('dot -Tpng %s.dot -o %s.png' % (filename, filename)) print("Writing graph to %s.pdf" % filename)
def write_graph2dot(graph, c_fname, img_fname, graph_opts): if pydot is None: # dump using simple logic dot_str = dump_dot_wo_pydot(graph, c_fname, graph_opts) dot_path = write_dot_file_wo_pydot(dot_str, img_fname) else: # dump using networkx and pydot if hasattr(nx, "nx_pydot"): pydot_graph = nx.drawing.nx_pydot.to_pydot(graph) else: pydot_graph = nx.to_pydot(graph) dot_path = write_dot_file_with_pydot(pydot_graph) return dot_path
def layer_dag_to_svg(): model = get_model() graph = nx.DiGraph() for layer in model.layers: graph.add_node(layer['name'], layer_attributes=layer) for layer in model.layers: for inputLayer in layer.get("inputs", []): graph.add_edge(model.layers[inputLayer]['name'], layer['name'], label=model.layers[inputLayer]['outputs']) pydot_graph = nx.to_pydot(graph) pydot_graph.set_rankdir("LR") svg = pydot_graph.create_svg(prog="dot") return Response(svg, mimetype="image/svg+xml")
def show_pydot(g): """ Display a networkx graph using pydot. Parameters ---------- g : networkx.Graph Graph to visualize. """ fd = tempfile.NamedTemporaryFile() fd.close() p = nx.to_pydot(g) p.write_jpg(fd.name) imdisp(fd.name) os.remove(fd.name)
def main(*args): if len(args) != 3: print 'Usage: {} <input file> <output file>'.format(args[0]) return 2 try: NFA = nx.read_dot(args[1]) except IOError: print '{}: file does not exist or is not readable. Terminating.'.format( args[1]) return 3 remove_quotes(NFA) Sigma, q0, F = NFA_parameters(NFA) # for node in NFA.nodes(data=True): # print('{}'.format(node)) # for edge in NFA.edges(data=True): # print edge t = input_transition(NFA, Sigma) # print '*** input_transition() result ***' # for node in sorted(t): # print('{} {}'.format(node, t[node])) q0_closure = lambda_closure(NFA, q0) DFA = powerset_construction(t, q0_closure, Sigma) DFA = rebuild_pretty(DFA, q0_closure, F) # print '=== powerset_construction() results ===' # for node in DFA.nodes(data=True): # print('{}'.format(node)) # for edge in DFA.edges(data=True): # print edge DFAdot = nx.to_pydot(DFA) DFAdot.set_rankdir(RANKDIR) DFAdot.set_charset(CHARSET) try: DFAdot.write(args[2]) except IOError: print '{}: IO error writing file. Terminating.'.format(args[2]) return 3 return 0
def _graph2pydot(graph, wrap=10, latex=False): """Convert (possibly labeled) state graph to dot str. @type graph: L{LabeledDiGraph} @rtype: str """ if _pydot_missing(): return None dummy_nx_graph = nx.MultiDiGraph() _states2dot_str(graph, dummy_nx_graph, wrap=wrap, latex=latex) _transitions2dot_str(graph.transitions, dummy_nx_graph, latex) pydot_graph = nx.to_pydot(dummy_nx_graph) pydot_graph.set_overlap("false") return pydot_graph
def layoutgraph(g): """Given a Graph object, """ #g = G.make_networkx() #G.colors_to_networkx(g) #networkx.write_gml(g.copy(), dirname+'dolphins_gamma%s.gml'%gamma) pd = networkx.to_pydot(g) #pd.to_string() prog = 'neato' #prog = 'fdp' p = subprocess.Popen('%s -Goverlap=scalexy -Gsplines=true -Gstart=5'%prog, stdin=subprocess.PIPE,stdout=subprocess.PIPE, shell=True) out = p.communicate(pd.to_string()) #from fitz import interactnow gnew = networkx.from_pydot(pydot.graph_from_dot_data(out[0])) return gnew
def main(): bibfile = sys.argv[1] with open(bibfile) as bibtex_file: bibtex = bibtexparser.load(bibtex_file) stats = quick_stats(bibtex) for k, v in stats.iteritems(): print "{0:>5} --> {1:}".format(v, k) citations = {} tags = {} for key, entry in bibtex.entries_dict.iteritems(): cites = entry.get('cites') if cites: citations[key] = unpack(cites) t = entry.get('tags') if t: tags[key] = t G = nx.DiGraph() for key, entry in citations.iteritems(): for cit in entry: G.add_edge(key, cit) pydot_G = nx.to_pydot(G) for node in pydot_G.get_nodes(): t = node.get_name() if t and tags.get(t): node.set('nodetype', tags.get(t)) pydot_G.set('rankdir', 'LR') pydot_G.set('style', 'dashed') pydot_G.write('bib.dot') call(["gvpr -c -f filter.gvpr bib.dot > bib_nice.dot"], shell=True) call([ "ccomps -x bib_nice.dot | dot | gvpack -array1 | neato -Tpng -n2 -o bib.png" ], shell=True)
def analyzeReport(self, report ): """ given an individual radiology report, creates a pyConTextGraph object that contains the context markup report: a text string containing the radiology reports """ context = self.context targets = self.targets modifiers = self.modifiers context.reset() splitter = helpers.sentenceSplitter() # alternatively you can skip the default exceptions and add your own # splitter = helpers.sentenceSpliter(useDefaults = False) #splitter.addExceptionTerms("Dr.","Mr.","Mrs.",M.D.","R.N.","L.P.N.",addCaseVariants=True) splitter.addExceptionTerms("Ms.","D.O.",addCaseVariants=True) splitter.deleteExceptionTerms("A.B.","B.S.",deleteCaseVariants=True) sentences = splitter.splitSentences(report) count = 0 for s in sentences: #print s context.setRawText(s) context.cleanText() context.markItems(modifiers, mode="modifier") context.markItems(targets, mode="target") g= context.getCurrentGraph() ic=0 context.pruneMarks() context.dropMarks('Exclusion') context.applyModifiers() #context.pruneModifierRelationships() #context.dropInactiveModifiers() print context self.outString += context.getXML()+u"\n" context.commit() count += 1 print context.getSectionText() raw_input('continue') context.computeDocumentGraph() ag = nx.to_pydot(context.getDocumentGraph(), strict=True) ag.write("case%03d.pdf"%self.currentCase,format="pdf")
def layoutgraph(g): """Given a Graph object, """ #g = G.make_networkx() #G.colors_to_networkx(g) #networkx.write_gml(g.copy(), dirname+'dolphins_gamma%s.gml'%gamma) pd = networkx.to_pydot(g) #pd.to_string() prog = 'neato' #prog = 'fdp' p = subprocess.Popen('%s -Goverlap=scalexy -Gsplines=true -Gstart=5' % prog, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) out = p.communicate(pd.to_string()) #from fitz import interactnow gnew = networkx.from_pydot(pydot.graph_from_dot_data(out[0])) return gnew
def produce_graph(nxgraph, sizedict, filename): """ Writes a networkx graph to the specified file as a .jpg, with node sizes being equal to the values in the accompanying dictionary. NB: Graphviz must be installed locally for this to work. Args: nxgraph: A graph in the standard networkX format. sizedict: A dictionary of values where keys are identical to the node names in nxgraph. Values will determine the relative size of the appropriate nodes in the output graph. filename: Name of the file you want to output to. e.g. sizedict = {'"Node1"': 45, '"Node2"': 98, '"Node3"': 67} filename = 'outfile.jpg' """ #Takes a Networkx graph, and a series of numbers with the same indices as the node #names; writes a jpg file with nodes of size determined by the series. dotgraph = nx.to_pydot(nxgraph) set_node_sizes(dotgraph, sizedict) set_node_labels(dotgraph, sizedict) dotgraph.set_nodesep(1) dotgraph.write_jpg(filename)
def report_transitions(self, r): Gd = self.create_transition_graph() G = Gd['G'] name2state = Gd['name2state'] name2obs = Gd['name2obs'] name2obsset = Gd['name2obsset'] name2cmd = Gd['name2cmd'] policy = Gd['policy'] # f = r.figure() import networkx as nx # # pos = nx.spectral_layout(G) # with f.plot('G', figsize=(5, 5)) as pylab: # @UnusedVariable # # nx_draw_with_attrs(G, pos=pos, with_labels=True) # nx.draw(G) d = nx.to_pydot(G) # d is a pydot graph object, dot options can be easily set # attributes get converted from networkx, # use set methods to control dot attributes after creation f = r.figure() f.data('pydot1', d.create_pdf(), mime=MIME_PDF) f.data('pydot2', d.create_png(), mime=MIME_PNG) f = r.figure('policy') gpolicy = get_policy_graph(policy) f.data('pydot1', gpolicy.create_pdf(), mime=MIME_PDF) f.data('pydot2', gpolicy.create_png(), mime=MIME_PNG) r.text("name2state", str(name2state)) r.text("name2obs", str(name2obs)) r.text("name2obsset", str(name2obsset)) r.text("name2cmd", str(name2cmd))
def main(): bibfile = sys.argv[1] with open(bibfile) as bibtex_file: bibtex = bibtexparser.load(bibtex_file) stats = quick_stats(bibtex) for k,v in stats.iteritems(): print "{0:>5} --> {1:}".format(v, k) citations = {} tags = {} for key, entry in bibtex.entries_dict.iteritems(): cites = entry.get('cites') if cites: citations[key] = unpack(cites) t = entry.get('tags') if t: tags[key] = t G = nx.DiGraph() for key, entry in citations.iteritems(): for cit in entry: G.add_edge(key, cit) pydot_G = nx.to_pydot(G) for node in pydot_G.get_nodes(): t = node.get_name() if t and tags.get(t): node.set('nodetype', tags.get(t)) pydot_G.set('rankdir', 'LR') pydot_G.set('style', 'dashed') pydot_G.write('bib.dot') call(["gvpr -c -f filter.gvpr bib.dot > bib_nice.dot"], shell=True) call(["ccomps -x bib_nice.dot | dot | gvpack -array1 | neato -Tpng -n2 -o bib.png"], shell=True)
def report_transitions(self, r): Gd = self.create_transition_graph() G = Gd['G'] name2state = Gd['name2state'] name2obs = Gd['name2obs'] name2obsset = Gd['name2obsset'] name2cmd = Gd['name2cmd'] policy = Gd['policy'] # f = r.figure() import networkx as nx # # pos = nx.spectral_layout(G) # with f.plot('G', figsize=(5, 5)) as pylab: # @UnusedVariable # # nx_draw_with_attrs(G, pos=pos, with_labels=True) # nx.draw(G) d = nx.to_pydot( G) # d is a pydot graph object, dot options can be easily set # attributes get converted from networkx, # use set methods to control dot attributes after creation f = r.figure() f.data('pydot1', d.create_pdf(), mime=MIME_PDF) f.data('pydot2', d.create_png(), mime=MIME_PNG) f = r.figure('policy') gpolicy = get_policy_graph(policy) f.data('pydot1', gpolicy.create_pdf(), mime=MIME_PDF) f.data('pydot2', gpolicy.create_png(), mime=MIME_PNG) r.text("name2state", str(name2state)) r.text("name2obs", str(name2obs)) r.text("name2obsset", str(name2obsset)) r.text("name2cmd", str(name2cmd))
def dump_graphviz(self): return # TODO: fix the add_node calls below dgraph = nx.to_pydot(self._G) for d in dgraph.get_node_list(): print d.get_name() # Put the roots in the same subgraph so they can be put in the same rank in # the graphviz graph. This puts them at the top of the graph roots_graph = pydot.Subgraph(subgraph_name="roots") roots = [n for n,d in self._G.in_degree().items() if d == 0] for r in roots: print repr(r) [roots_graph.add_node(dgraph.get_node(str(repr(r)))) for r in roots] roots_graph.set_rank('source') dgraph.add_subgraph(roots_graph) # Same for the bottom of the graph leaves_graph = pydot.Subgraph(subgraph_name="leaves") leaves = [n for n,d in self._G.out_degree().items() if d == 0] [leaves_graph.add_node(dgraph.get_node(l)) for l in leaves] leaves_graph.set_rank('sink') dgraph.add_subgraph(leaves_graph) dgraph.write_ps("a.ps", prog="dot")
def export_to_dot(self): """Exports the graph to a dot format (requires pydot library).""" return nx.to_pydot(self).to_string()