def test_dot_for_hypergraph(self): gr = testlib.new_hypergraph() dotstr = dot.write(gr) gr1 = dot.read_hypergraph(dotstr) dotstr = dot.write(gr1) gr2 = dot.read_hypergraph(dotstr) graph_equality(gr1, gr2)
def write_graphs_to_dots(self): assert self.build_graph self._load_packages() from pygraph.readwrite import dot base = self.output_dir with open(join(base, 'digraph.dot'), 'w') as f: data = dot.write(self.digraph) f.write(data) with open(join(base, 'bfs.dot'), 'w') as f: (st, order) = breadth_first_search(self.digraph) bfs = digraph() bfs.add_spanning_tree(st) data = dot.write(bfs) f.write(data) with open(join(base, 'dfs.dot'), 'w') as f: (st, pre, post) = depth_first_search(self.digraph) dfs = digraph() dfs.add_spanning_tree(st) data = dot.write(dfs) f.write(data)
def test_dot_for_digraph(self): gr = testlib.new_digraph() dotstr = dot.write(gr) gr1 = dot.read(dotstr) dotstr = dot.write(gr1) gr2 = dot.read(dotstr) graph_equality(gr1, gr2) assert len(gr.nodes()) == len(gr1.nodes()) assert len(gr.edges()) == len(gr1.edges())
def test_output_names_in_dot(self): gr1 = testlib.new_graph() gr1.name = "Some name 1" gr2 = testlib.new_digraph() gr2.name = "Some name 2" gr3 = testlib.new_hypergraph() gr3.name = "Some name 3" assert "Some name 1" in dot.write(gr1) assert "Some name 2" in dot.write(gr2) assert "Some name 3" in dot.write(gr3)
def handle(self, **options): gr = graph() cats_by_id = dict((c.id, c) for c in Category.objects.all()) # Add nodes dups = count() for c in cats_by_id.itervalues(): try: gr.add_node(c) except AdditionError: dups.next() parent = cats_by_id.get(c.parent_id) print 'WARNING: duplicate node :: <Category %i | %s>' % (c.id, c) print '\twith parent ' + '<Category %i | %s>' % ( parent.id, parent) if parent else 'None' if dups.next() > 0: return # Add edges # gr.add_edge((CONCRETE_NODE, ROOT_NODE)) for c in cats_by_id.itervalues(): parent = cats_by_id.get(c.parent_id) if parent: gr.add_edge((c, parent)) # import ipdb; ipdb.set_trace() # The whole tree from the root st, order = breadth_first_search( gr, root=Category.objects.get(title="Abstract")) gst = digraph() gst.add_spanning_tree(st) dot = write(gst) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'pdf', os.path.join(output_dir, 'abstract.pdf')) st, order = breadth_first_search( gr, root=Category.objects.get(title="Concrete")) gst = digraph() gst.add_spanning_tree(st) dot = write(gst) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'pdf', os.path.join(output_dir, 'concrete.pdf'))
def drawGraph(self, inputs): fileName = 'planetModel.png' gr = graph() self.addGraphNode(1,gr, inputs) # Draw as PNG #with open("./planetModel.viz", 'wb') as f: #dot = write(gr,f) #f.write(dot) #gvv = gv.readstring(dot) #gv.layout(gvv,'dot') #gv.render(gvv,'png', fileName) #f.close() gst = digraph() self.addGraphNode(1,gst, inputs) with open("./planetModel.viz", 'wb') as f: #st, order = breadth_first_search(gst, root=1) #gst2 = digraph() #gst2.add_spanning_tree(gst.nodes()) #gst2.(1, 'post') dot = write(gst,f) f.write(dot) gvv = gv.readstring(dot) gv.layout(gvv,'dot') gv.render(gvv,'png', fileName) f.close() return fileName
def drawGraph(self, inputs): fileName = 'planetModel.png' gr = graph() self.addGraphNode(1, gr, inputs) # Draw as PNG #with open("./planetModel.viz", 'wb') as f: #dot = write(gr,f) #f.write(dot) #gvv = gv.readstring(dot) #gv.layout(gvv,'dot') #gv.render(gvv,'png', fileName) #f.close() gst = digraph() self.addGraphNode(1, gst, inputs) with open("./planetModel.viz", 'wb') as f: #st, order = breadth_first_search(gst, root=1) #gst2 = digraph() #gst2.add_spanning_tree(gst.nodes()) #gst2.(1, 'post') dot = write(gst, f) f.write(dot) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'png', fileName) f.close() return fileName
def graph_to_file(graph, filename='graph.png', delete_single=False): """Exports a graph to a image file. Params: graph - The graph to export. filename - The destination of the output. The filename should include an extention, the format of the file will always be PNG no matter what the extention is. delete_single - If set to true then all nodes without any neighbours will be deleted prior to exporting the graph. """ logger = logging.getLogger('.'.join((__name__, 'graph_to_file'))) logger.info("Exporting a graph to %s", filename) # Delete nodes that don't have any neighbours if delete_single: del_nodes = [node for node in graph.nodes() if not graph.neighbors(node)] logger.info("Deleting %d nodes without neighbours", len(del_nodes)) for node in del_nodes: graph.del_node(node) # Write the graph dot = graphtodot.write(graph) gvgraph = graphviz.graph_from_dot_data(dot) gvgraph.write(filename, format='png')
def plot_graph(gr): """ draws the graph to file """ p = 100.0 / (len(gr.nodes())+1) gr_ext = graph() for node in gr.nodes(): if coin(p): if not gr_ext.has_node(node): gr_ext.add_node(node,attrs=gr.node_attributes(node)) for n in [ ed[0] for ed in gr.edges() if ed[1] == node ]: if coin(0.3): if not gr_ext.has_node(n): gr_ext.add_node(n,attrs=gr.node_attributes(n)) #print "Edges:",gr_ext.edges() if not gr_ext.has_edge((node,n)): gr_ext.add_edge((node,n)) dot = write(gr_ext) gvv = gv.readstring(dot) gv.layout(gvv,'dot') if args[1]== 'karate.txt': gv.render(gvv,'png','community1.png') elif args[1] == 'email.txt': gv.render(gvv,'png','community2.png') elif args[1] == 'hep-th-citations.txt': gv.render(gvv,'png','community3.png') elif args[1] == 'amazon1.txt': gv.render(gvv,'png','community4.png') elif args[1] == 'p2p-Gnutella30.txt': gv.render(gvv,'png','community5.png')
def graph_to_file(graph, filename='graph.png', delete_single=False): """Exports a graph to a image file. Params: graph - The graph to export. filename - The destination of the output. The filename should include an extention, the format of the file will always be PNG no matter what the extention is. delete_single - If set to true then all nodes without any neighbours will be deleted prior to exporting the graph. """ logger = logging.getLogger('.'.join((__name__, 'graph_to_file'))) logger.info("Exporting a graph to %s", filename) # Delete nodes that don't have any neighbours if delete_single: del_nodes = [ node for node in graph.nodes() if not graph.neighbors(node) ] logger.info("Deleting %d nodes without neighbours", len(del_nodes)) for node in del_nodes: graph.del_node(node) # Write the graph dot = graphtodot.write(graph) gvgraph = graphviz.graph_from_dot_data(dot) gvgraph.write(filename, format='png')
def drawGraphFromSM(SM, names, outFile): fig = plt.figure(1) plot1 = plt.imshow(SM, origin='upper', cmap=cm.gray, interpolation='nearest') plt.show() gr = graph() namesNew = [] for i,f in enumerate(names): if sum(SM[i,:])>0: gr.add_nodes([f]) namesNew.append(f) Max = SM.max() Mean = mean(SM) Threshold = Mean * 1.5 for i in range(len(names)): for j in range(len(names)): if i<j: if SM[i][j] > 0: gr.add_edge((names[i], names[j])) # Draw as PNG dot = write(gr) gvv = gv.readstring(dot) gv.layout(gvv,'dot') gv.render(gvv,'png', outFile)
def run(self): #print "save graph in dir" gr = digraph() for x in self.nodes: gr.add_node(x.name) for x in self.nodes: x.addEdges(gr) dirLck.acquire() if not os.path.isdir(self.dir): if -1 == os.getcwd().find(self.dir): os.mkdir(self.dir) dirLck.release() if -1 == os.getcwd().find(self.dir): os.chdir(self.dir) dot = write(gr) f = open(self.filename, 'w') f.write(dot) f.close() proc = subprocess.Popen('dot -Tjpg ' + self.filename +' -o ' + self.filename + '.jpg', shell=True, stdout=subprocess.PIPE,) r = proc.communicate()[0] print ""
def draw(self, filename): print "initial state:", self.initial_state print "final states", self.final_state print "transition table:", self.transition_table vertexes = self.transition_table.keys() edges = self._edges() gr = digraph() #gr.add_nodes([str(vertex) for vertex in vertexes]) for vertex in vertexes: attrs = [] if ((isinstance(self.final_state, list) and vertex in self.final_state) or (isinstance(self.final_state, int) and vertex == self.final_state)): attrs.append('final') gr.add_node(str(vertex), attrs=attrs) for edge, label in edges.items(): label = ', '.join(label) gr.add_edge(edge=edge, label=label) dot = write(gr) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'png', '%s.png' % filename)
def pruner_bakeoff(nPoints, nSeeds, r0, delta, spread, lumpage, outputNameRoot): """ Generate a graph, then use all the CANDIDATE_PRUNERS to prune it, and save the results for later investigation. """ from spiralPointDistribution import spiralPointDistribution from pointsToOutwardDigraph import graphFromPoints #import matplotlib.pyplot as plot #import matplotlib.tri as tri import pygraph.readwrite.dot as dotIO points = spiralPointDistribution(nPoints, nSeeds, r0, delta, spread, lumpage) outdir = os.path.dirname(outputNameRoot) if not os.path.exists(outdir): os.makedirs(outdir) #x, y = zip(*points) #plot.figure() #plot.gca().set_aspect('equal') #plot.triplot(tri.Triangulation(x, y), 'r,:') #plot.scatter(x, y) #plot.show() #NOT YET FINISHED #save png as outputNameRoot.prunerName.png (once I get Python graphviz bindings working) for pruner in CANDIDATE_PRUNERS: graph = graphFromPoints(points, nSeeds) graph = friendly_rename(graph) graph = pruner.prune(graph) dotstring = dotIO.write(graph) dotname = "{0}.{1}.gv".format(outputNameRoot, pruner.__class__.__name__) with open(dotname, "w") as dotfile: dotfile.write(dotstring)
def GraficarGrafo(self, grafo, proy): grafof = self.GraficoProyecto(proy) dot = write(grafof) _gvv = _gv.readstring(dot) _gv.layout(_gvv,'dot') _gv.render(_gvv,'png',"sgs/public/images/grafo.png") return dict()
def search(self): st, order = breadth_first_search(self.net, "book") gst = digraph() gst.add_spanning_tree(st) dot = write(gst, True) out_file = open("file.gv", "w") out_file.write(dot) out_file.close()
def save_as_pdf(gr,filename,show_weights=False): from pygraph.readwrite.dot import write import gv dot = write(gr, weighted=show_weights) gvv = gv.readstring(dot) gv.layout(gvv,'fdp') gv.render(gvv,'pdf',filename)
def draw_graph(self): dot = write(self.graph) f = open('currencies.dot', 'a') f.write(dot) f.close() command = '/usr/local/bin/dot -Tpng currencies.dot > currencies.png' print "Generating graph with %s" % command os.system(command)
def draw_pretty(pl, output_filename): gr = pygraph_build_pipeline_graph(pl) dot = write(gr) G = pgv.AGraph(dot, name=pl.name) prettify_graph(pl, G) G.layout(prog='dot') G.draw(output_filename)
def main(args): g = generate(args.nodes, args.edges, True) while not find_cycle(g): g = generate(args.nodes, args.edges, True) with open(args.output, 'w') as f: f.write(write(g))
def handle(self, **options): gr = graph() cats_by_id = dict((c.id, c) for c in Category.objects.all()) # Add nodes dups = count() for c in cats_by_id.itervalues(): try: gr.add_node(c) except AdditionError: dups.next() parent = cats_by_id.get(c.parent_id) print 'WARNING: duplicate node :: <Category %i | %s>' % (c.id, c) print '\twith parent ' + '<Category %i | %s>' % (parent.id, parent) if parent else 'None' if dups.next() > 0: return # Add edges # gr.add_edge((CONCRETE_NODE, ROOT_NODE)) for c in cats_by_id.itervalues(): parent = cats_by_id.get(c.parent_id) if parent: gr.add_edge((c, parent)) # import ipdb; ipdb.set_trace() # The whole tree from the root st, order = breadth_first_search(gr, root=Category.objects.get(title="Abstract")) gst = digraph() gst.add_spanning_tree(st) dot = write(gst) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'pdf', os.path.join(output_dir, 'abstract.pdf')) st, order = breadth_first_search(gr, root=Category.objects.get(title="Concrete")) gst = digraph() gst.add_spanning_tree(st) dot = write(gst) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'pdf', os.path.join(output_dir, 'concrete.pdf'))
def sample_gene_interactions(c, args, idx_to_sample): #fetch variant gene dict for all samples get_variant_genes(c, args, idx_to_sample) #file handle for fetching the hprd graph file_graph = os.path.join(path_dirname, 'hprd_interaction_graph') #load the graph using cPickle and close file handle gr = graph() f = open(file_graph, 'rb') gr = cPickle.load(f) f.close() k = [] variants = [] #calculate nodes from the graph hprd_genes = gr.nodes() if args.gene == None or args.gene not in hprd_genes: sys.stderr.write("gene name not given else not represented in the p-p interaction file\n") elif args.gene in hprd_genes: x, y = \ breadth_first_search(gr,root=args.gene,filter=radius(args.radius)) gst = digraph() gst.add_spanning_tree(x) dot = write(gst) out.write(dot) st, sd = shortest_path(gst, args.gene) if args.var_mode: for sample in sam.iterkeys(): var = sam[str(sample)] #for each level return interacting genes if they are # variants in the sample. # 0th order would be returned if the user chosen # gene is a variant in the sample for x in range(0, (args.radius+1)): for each in var: for key, value in sd.iteritems(): if value == x and key == each[0]: print "\t".join([str(sample),str(args.gene), \ str(x), \ str(key), \ str(each[1]), \ str(each[2]), \ str(each[3])]) elif (not args.var_mode): for sample in sam.iterkeys(): for each in sam[str(sample)]: variants.append(each[0]) for x in range(0, (args.radius+1)): for key, value in sd.iteritems(): if value == x and key in set(variants): k.append(key) if k: print "\t".join([str(sample), str(args.gene), \ str(x)+"_order:", ",".join(k)]) else: print "\t".join([str(sample), str(args.gene), str(x)+"_order:", "none"]) #initialize keys for next iteration k = []
def _dep_graph_dump(dgr, filename): """Dump dependency graph to file, in specified format.""" # write to file dottxt = dot.write(dgr) if os.path.splitext(filename)[-1] == '.dot': # create .dot file write_file(filename, dottxt) else: _dep_graph_gv(dottxt, filename)
def output_graph(graph, root=None): """ Returns a tuplet containing: - the result of the depth_first_search() function starting at 'root' (is is a tuplet) - a dot format output of the given graph (display it using graphviz dotty command) """ dfs = depth_first_search(graph, root) dot = write(graph) return [dfs, dot]
def visualize_with_pygraph(args): data, nodes = get_input(args.file) graph = digraph() graph.add_nodes(nodes) for start, stop, distance in emit_edge(data): graph.add_edge((start, stop), distance) dot = write(graph, weighted=True) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'png', args.out)
def matriz_relaciones(self,id_item): """ Obtiene una matriz de la relaciones. """ if id_item is not None: id_item=int(id_item) antecesores=Relaciones.get_antecesores(id_item) sucesores=Relaciones.get_sucesores(id_item) item=Item.get_item_by_id(id_item) relacionados=[] usados=[] n = len(antecesores)+len(sucesores)+1 for antecesor in antecesores: if antecesor not in relacionados: if (antecesor.estado_oculto=="Activo"): relacionados.append(antecesor) usados.append(antecesor.nombre_item) if item not in relacionados: relacionados.append(item) usados.append(item.nombre_item) for sucesor in sucesores: if sucesor not in relacionados: if (sucesor.estado_oculto=="Activo"): relacionados.append(sucesor) usados.append(sucesor.nombre_item) #Graph creation gr = graph() # Add nodes and edges for item in usados: gr.add_nodes([item]) # Add relation and edges for item in relacionados: padres= Relaciones.get_mis_padres(item.id_item) if len(padres)>0: for padre in padres: gr.add_edge((item.nombre_item, padre.nombre_item)) else: #remover nodo print("hola") # Draw as PNG dot = write(gr) gvv = gv.readstring(dot) gv.layout(gvv,'dot') gv.render(gvv,'png','/home/hermann/saip2011/saip2011/public/images/arbol.png')
def write_graph(graph, filename, format='svg'): """Write a python-graph graph as an image. `format` can be any of those supported by pydot.Dot.write(). """ from pygraph.readwrite.dot import write dotdata = write(graph) from pydot import graph_from_dot_data dotgraph = graph_from_dot_data(dotdata) dotgraph.write(filename, format=format)
def graphe_mine(request): """ Dessine une image du graphe des mineurs Les élèves sont les sommets du graphe, et les arêtes sont les relations parrain, fillot, co. """ nombre_promos = int(request.GET.get("nombre_promos", 3)) generate = bool(request.GET.get("generate", False)) use_splines = bool(request.GET.get("splines", False)) chemin = os.path.join(settings.MEDIA_ROOT, "trombi") chemin_dot = os.path.join(chemin, "graphe_mine" + str(nombre_promos) + ".dot") chemin_png = os.path.join(chemin, "graphe_mine" + str(nombre_promos) + ".png") if generate: from pygraph.classes.graph import graph from pygraph.classes.digraph import digraph from pygraph.algorithms.searching import breadth_first_search from pygraph.readwrite.dot import write gr = graph() #On ajoute les aretes for eleve1 in UserProfile.objects.promos_actuelles(nombre_promos): for eleve2 in eleve1.co.all(): if eleve2.promo is not None: if eleve2.annee() <= nombre_promos: ajouter_relation(gr, eleve1, eleve2) for eleve2 in eleve1.parrains.all(): if eleve2.promo is not None: if eleve2.annee() <= nombre_promos: ajouter_relation(gr, eleve1, eleve2) #Suppression des noeuds isoles for i in gr: if not gr.neighbors(i): gr.del_node(i) dot = write(gr) # Utilisation de splines, pour éviter les overlap noeud/arêtes. Ralentit énormément les calculs. if use_splines: dot = dot[:17] + "splines=true; " + dot[17:] open(chemin_dot, 'w').write(dot) subprocess.call([ "neato", "-Tpng", chemin_dot, "-Goverlap=false", "-o", chemin_png ]) url = os.path.join(settings.MEDIA_URL, "trombi/graphe_mine" + str(nombre_promos) + ".png") return render(request, 'trombi/graphe-mine.html', {'url': url})
def write_to_dot(adj_matrix): """Returns the dot file format string from an adjacency matrix for use with graphviz.""" gr = graph() gr.add_nodes(range(0, adj_matrix.shape[0])) for i in range(0, adj_matrix.shape[0]): for j in range(0, i+1): if adj_matrix[i][j] == 1: gr.add_edge([i, j]) return write(gr)
def draw_graph(gr): """ draws the graph to file """ print "Nodes levels after 100 steps:" for node in gr.nodes(): print "Node: %i - level:%.1f" % (node, dict(gr.node_attributes(node)).get('level')) dot = write(gr) gvv = gv.readstring(dot) gv.layout(gvv,'dot') gv.render(gvv,'png','pumping.png')
def _dep_graph(fn, specs, silent=False): """ Create a dependency graph for the given easyconfigs. """ # check whether module names are unique # if so, we can omit versions in the graph names = set() for spec in specs: names.add(spec['ec']['name']) omit_versions = len(names) == len(specs) def mk_node_name(spec): if spec.get('external_module', False): node_name = "%s (EXT)" % spec['full_mod_name'] elif omit_versions: node_name = spec['name'] else: node_name = ActiveMNS().det_full_module_name(spec) return node_name # enhance list of specs all_nodes = set() for spec in specs: spec['module'] = mk_node_name(spec['ec']) all_nodes.add(spec['module']) spec['unresolved_deps'] = [ mk_node_name(s) for s in spec['unresolved_deps'] ] all_nodes.update(spec['unresolved_deps']) # build directed graph dgr = digraph() dgr.add_nodes(all_nodes) for spec in specs: for dep in spec['unresolved_deps']: dgr.add_edge((spec['module'], dep)) # write to file dottxt = dot.write(dgr) if fn.endswith(".dot"): # create .dot file write_file(fn, dottxt) else: # try and render graph in specified file format gvv = gv.readstring(dottxt) gv.layout(gvv, 'dot') gv.render(gvv, fn.split('.')[-1], fn) if not silent: print "Wrote dependency graph for %d easyconfigs to %s" % (len(specs), fn)
def export(self, graph, path, type): """ Export the graph """ dot = write(graph) gvv = gv.readstring(dot) gv.setv(gvv,'rankdir','LR') gv.layout(gvv,'dot') graphname = path + '/' + type + '.svg-' + str(time.time()) gv.render(gvv, 'svg', graphname) return graphname
def do_graph(clustering,num_elems_of_traj_2,std_deviations,filename): """ """ graph = digraph() labels = populate_nodes_with_labels(clustering, num_elems_of_traj_2,std_deviations, graph) prob_matrix = calculate_probability_matrix(clustering) add_graph_edges(graph,labels,clustering,prob_matrix) tmp_file = open("tmp_dot","w") tmp_file.write(write(graph)) tmp_file.close() common.print_and_flush("delegating to dot...") os.system("cat tmp_dot|dot -Tpng -o "+filename+";rm tmp_dot")
def dot(self, augment=True): from pygraph.readwrite.dot import write # Get the dot encoding dot = write(self.graph, weighted=True) if (not augment) or (self.domain_size == 0) or (len(self.graph.edges()) == 0): return dot # Split into lines dot_lines = dot.split("\n") new_dot = [] # First line is the graph header new_dot.append(dot_lines[0]) # Next we add some FSA stuff new_dot.append("label=%s" % self.graph.name) new_dot.append("rankdir=LR;") # Next is the nodes init_nodes = [] norm_nodes = [] goal_nodes = [] index = 1 while '->' not in dot_lines[index]: # Check if the node is an initial or accepting state node_id = dot_lines[index].split(';')[0].split( '[')[0].rstrip().strip('"') if node_id in [str(item) for item in self.goal_vals]: goal_nodes.append("node [shape = doublecircle];") goal_nodes.append(dot_lines[index]) elif node_id == str(self.init_val): init_nodes.append("node [shape = diamond];") init_nodes.append(dot_lines[index]) else: norm_nodes.append("node [shape = circle];") norm_nodes.append(dot_lines[index]) index += 1 # Append them in the right order new_dot.extend(init_nodes) new_dot.extend(norm_nodes) new_dot.extend(goal_nodes) # Add the rest for i in range(index, len(dot_lines)): new_dot.append(dot_lines[i]) return "\n".join(new_dot)
def draw_graph(gr): """ draws the graph to file """ print "Nodes levels after 100 steps:" for node in gr.nodes(): print "Node: %i - level:%.1f" % (node, dict( gr.node_attributes(node)).get('level')) dot = write(gr) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'png', 'pumping.png')
def _dep_graph(fn, specs, silent=False): """ Create a dependency graph for the given easyconfigs. """ # check whether module names are unique # if so, we can omit versions in the graph names = set() for spec in specs: names.add(spec['ec']['name']) omit_versions = len(names) == len(specs) def mk_node_name(spec): if spec.get('external_module', False): node_name = "%s (EXT)" % spec['full_mod_name'] elif omit_versions: node_name = spec['name'] else: node_name = ActiveMNS().det_full_module_name(spec) return node_name # enhance list of specs all_nodes = set() for spec in specs: spec['module'] = mk_node_name(spec['ec']) all_nodes.add(spec['module']) spec['unresolved_deps'] = [mk_node_name(s) for s in spec['unresolved_deps']] all_nodes.update(spec['unresolved_deps']) # build directed graph dgr = digraph() dgr.add_nodes(all_nodes) for spec in specs: for dep in spec['unresolved_deps']: dgr.add_edge((spec['module'], dep)) # write to file dottxt = dot.write(dgr) if fn.endswith(".dot"): # create .dot file write_file(fn, dottxt) else: # try and render graph in specified file format gvv = gv.readstring(dottxt) gv.layout(gvv, 'dot') gv.render(gvv, fn.split('.')[-1], fn) if not silent: print "Wrote dependency graph for %d easyconfigs to %s" % (len(specs), fn)
def _dot_output(self, fmt='png'): """Run dot with specified output format and return output.""" command = ['dot', '-T%s' % fmt] proc = subprocess.Popen( command, stdin=subprocess.PIPE, stdout=subprocess.PIPE ) stdout, _ = proc.communicate(dot.write(self.graph).encode()) if proc.returncode != 0: raise RuntimeError( 'Cannot create dot graphs via {}'.format(' '.join(command)) ) return stdout
def CreateClassDiagram(self): for delphi in self.documentation: for delphiClass in delphi.classes: self.gr.add_nodes([delphiClass.name]) for delphi in self.documentation: for delphiClass in delphi.classes: try: self.gr.add_edge((delphiClass.name, delphiClass.superClass)) except: print("Edge already exist") dot = write(self.gr) gvv = gv.readstring(dot) gv.layout(gvv,'dot') gv.render(gvv,'svg','Delphi-Classdiagram.svg')
def Main(): parser = argparse.ArgumentParser() parser.add_argument("input_pdf", type=str, help="pdf file that will be analysed.") parser.add_argument("-T", "--debug-with-token", action="store_true", help="print token strings.") parser.add_argument("-o", "--output-png", default="out.png", help="path to a png file to be written.", type=str) args = parser.parse_args() tk = Tokenizer(args.input_pdf) queue = deque([tk.token(), tk.token(), tk.token()]) obj_num = "" attr = "" gr = digraph() while not allNone(queue): if isObjToken(queue): attr = "" obj_num = getObjNum(queue) if not gr.has_node(obj_num): gr.add_node(obj_num) elif isIndirect(queue): if not gr.has_node(getIndirectNum(queue)): gr.add_node(getIndirectNum(queue)) if not gr.has_edge((obj_num, getIndirectNum(queue))): #print "obj: " + obj_num + " to: " + getIndirectNum(queue) gr.add_edge((obj_num, getIndirectNum(queue)), label=attr) attr = getAttrName(queue, attr) debug(queue.popleft(), args.debug_with_token) queue.append(tk.token()) dot = write(gr) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'png', args.output_png)
def dep_graph(fn, specs, silent=False): """ Create a dependency graph for the given easyconfigs. """ # check whether module names are unique # if so, we can omit versions in the graph names = set() for spec in specs: names.add(spec['module'][0]) omit_versions = len(names) == len(specs) def mk_node_name(mod): if omit_versions: return mod[0] else: return '-'.join(mod) # enhance list of specs for spec in specs: spec['module'] = mk_node_name(spec['module']) spec['unresolvedDependencies'] = [mk_node_name(s) for s in spec['unresolvedDependencies']] # [s[0] for s in spec['unresolvedDependencies']] # build directed graph dgr = digraph() dgr.add_nodes([spec['module'] for spec in specs]) for spec in specs: for dep in spec['unresolvedDependencies']: dgr.add_edge((spec['module'], dep)) # write to file dottxt = dot.write(dgr) if fn.endswith(".dot"): # create .dot file write_file(fn, dottxt) else: # try and render graph in specified file format gvv = gv.readstring(dottxt) gv.layout(gvv, 'dot') gv.render(gvv, fn.split('.')[-1], fn) if not silent: print "Wrote dependency graph for %d easyconfigs to %s" % (len(specs), fn)
def main(argv): outputfile = "deps.png" parser = argparse.ArgumentParser( description='Find coffeescript dependencies') parser.add_argument('dirs', metavar='dir', nargs='+', help='directories to process') parser.add_argument('-o', '--output', help='output file') parser.add_argument('-t', action='store_true', help='show dependencies in text mode') args = parser.parse_args() if args.output is not None: outputfile = args.output for root, dirs, files in os.walk(args.dirs[0]): for f in files: fullpath = os.path.join(root, f) if os.path.splitext(fullpath)[1] == '.coffee': if not prune(fullpath): analyze(fullpath) for item in deps: for node in deps[item]: gr.add_edge((item, "%s.coffee" % (node))) if args.t: for item in deps: print item for node in deps[item]: print "\t %s.coffee" % (node) # Draw as PNG dot = write(gr) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'png', outputfile)
def viz(self, filename): ''' Generate a image of the experiment graph ''' # Construct the image of the graph dot = write(self) experimentGraphViz = AGraph(string=dot) # Parameters of the image experimentGraphViz.graph_attr['label']='Experiment' experimentGraphViz.graph_attr['dpi'] = '100' experimentGraphViz.graph_attr['overlap'] = 'scale' experimentGraphViz.node_attr['shape']='box' experimentGraphViz.node_attr['label']= '' experimentGraphViz.node_attr['color']= 'blue' experimentGraphViz.node_attr['style']= 'filled' experimentGraphViz.edge_attr['color']='black' experimentGraphViz.layout() # Write the image in the file experimentGraphViz.draw(filename)
def DepGraph(j): linestring = open('tmp/deps'+str(j)+'.txt', 'r').read() lines = linestring.split('\n') deps = [] for _deps in lines: ar = _deps.split() if len(ar) > 0: deps.append({'from': ar[1].replace(':',''),'to': ar[4].replace(':',''),'vec': ar[6]}) # znajdz wszystkie instrukcje IND i dodaj je tu # deps.append({'from': '12','to': '12','vec': ''}) # przejrzyj tmp/petit_loop_0.t znajdz wszystkie instrukcje od for gr = digraph() for dep in deps: if not gr.has_node(dep['from']): gr.add_nodes([dep['from']]) if not gr.has_node(dep['to']): gr.add_nodes([dep['to']]) for dep in deps: if not gr.has_edge((dep['from'],dep['to'])): gr.add_edge((dep['from'],dep['to'])) # Draw as PNG dot = write(gr) gvv = gv.readstring(dot) gv.layout(gvv,'dot') gv.render(gvv,'png','tmp/scc_'+str(j)+'.png') gv.render(gvv, 'eps', 'tmp/scc_' + str(j) + '.eps') return gr
def sample_gene_interactions(c, args, idx_to_sample): out = open("file.dot", 'w') #fetch variant gene dict for all samples samples = get_variant_genes(c, args, idx_to_sample) #file handle for fetching the hprd graph config = read_gemini_config(args=args) path_dirname = config["annotation_dir"] file_graph = os.path.join(path_dirname, 'hprd_interaction_graph') #load the graph using cPickle and close file handle gr = graph() f = open(file_graph, 'rb') gr = cPickle.load(f) f.close() k = [] variants = [] #calculate nodes from the graph hprd_genes = gr.nodes() if args.gene == None or args.gene not in hprd_genes: sys.stderr.write("Gene name not found or") sys.stderr.write(" gene not in p-p interaction file\n") elif args.gene in hprd_genes: x, y = \ breadth_first_search(gr,root=args.gene,filter=radius(args.radius)) gst = digraph() gst.add_spanning_tree(x) dot = write(gst) out.write(dot) st, sd = shortest_path(gst, args.gene) if args.var_mode: for sample in samples.iterkeys(): var = samples[str(sample)] #for each level return interacting genes if they are # variants in the sample. # 0th order would be returned if the user chosen # gene is a variant in the sample for x in range(0, (args.radius + 1)): for each in var: for key, value in sd.iteritems(): if value == x and key == each[0]: print "\t".join([str(sample),str(args.gene), \ str(x), \ str(key), \ str(each[1]), \ str(each[2]), \ str(each[3]), \ str(each[4]), \ str(each[5]), \ str(each[6]), \ str(each[7]), \ str(each[8]), \ str(each[9]), \ str(each[10]), \ str(each[11])]) elif (not args.var_mode): for sample in samples.iterkeys(): for each in samples[str(sample)]: variants.append(each[0]) for x in range(0, (args.radius + 1)): for key, value in sd.iteritems(): if value == x and key in set(variants): k.append(key) if k: print "\t".join([str(sample), str(args.gene), \ str(x)+"_order:", ",".join(k)]) else: print "\t".join([str(sample), str(args.gene), \ str(x)+"_order:", "none"]) #initialize keys for next iteration k = [] #initialize variants list for next iteration variants = []
% "NAN") # get us some sort order! sorted_nodes = topological_sorting(gr) length = len(sorted_nodes) priority = 1000 for n in sorted_nodes: # now that should be the priority in the c file: print("%d %s" % (priority, n)) filename = os.path.join(os.path.dirname(__file__), "../src/iop/%s.c" % n) if not os.path.isfile(filename): filename = os.path.join(os.path.dirname(__file__), "../src/iop/%s.cc" % n) if not os.path.isfile(filename): if not n == "rawspeed": print("could not find file `%s'" % filename) continue if apply_changes: replace_all( filename, "( )*?(?P<identifier>((\w)*))( )*?->( )*?priority( )*?(=).*?(;).*\n", " \g<identifier>->priority = %d; // module order created by iop_dependencies.py, do not edit!\n" % priority) priority -= 1000.0 / (length - 1.0) # beauty-print the sorted pipe as pdf: dot = write(gr) gvv = gv.AGraph(dot) gvv.layout(prog='dot') gvv.draw(os.path.join(os.path.dirname(__file__), 'iop_deps.pdf'))
# Dormindo um pouco para evitar bloqueios time.sleep(5) # Criando grafo gr = graph() gr.add_node(RANGE) # Pegando ips ips = IPSet([RANGE]) todos = [] for ip in ips: todos.append(ip) # Fazendo shuffle random.shuffle(todos) for ip in todos: try: search(str(ip), gr) except httplib.IncompleteRead: print "[!] Erro httplib.IncompleteRead: IP (%s)" % ip st, order = breadth_first_search(gr, root=RANGE) gst = digraph() gst.add_spanning_tree(st) dot = write(gst) gvv = gv.readstring(dot) gv.layout(gvv, 'dot') gv.render(gvv, 'png', 'bing.png')
def run(*args): parser = argparse.ArgumentParser( prog="graphutils.py", description="Generates incomplete call graphs from JavaScript files.") parser.add_argument( "filepath", action="store", help="the path to JavaScript file or a Chrome extension itself") parser.add_argument( "-o", action="store", dest="outputpath", help="""output path (not the file!). the output file name will be the same as the extension or original JS file name.""") parser.add_argument( "-e", action="store_true", default=False, dest="isextension", help="""indicates that filepath is a path to a single extension (e.g. extensions/aapbdbdomjkkjkaonfhkkikfgjllcleb)""") results = parser.parse_args() filepath = results.filepath if results.outputpath: outputpath = results.outputpath if not os.path.exists(outputpath): os.mkdir(outputpath) else: outputpath = "." # current directory isextension = results.isextension if isextension: ext_path = filepath ext = os.path.basename(filepath) outputname = ext try: ext_name = fileutils.get_extension_name(os.path.join( ext_path, ext)) if ext_name is None: raise Exception() except: ext_name = "Unknown" print(ext_name) print "Combining extension .js files...", js_string = fileutils.combine_js_files(os.path.join(ext_path, ext)) print "OK" else: # Take the filename without extension outputname = os.path.splitext(os.path.basename(filepath))[0] print "Reading JS file...", js_file = open(filepath, "r") js_string = js_file.read() js_file.close() print "OK" # ------------------- SETUP PHASE ------------------- # --- Open and Create AST from Extension --- print "Creating AST...", ast = astutils.create_AST_from_string(js_string) print "OK" # --- Create Frame object and execute alpha-rename --- print "Creating frames...", fr = alpharenamer.create_frames(ast) print "OK" print "Alpha-renaming...", fr = alpharenamer.alpha_rename(fr, ast) print "OK" # --- Clean Call List --- print "Cleaning call list of frames...", alpharenamer.traverse_frames(fr, clean_call_list) print "OK" # ------------------- OUTPUT PHASE ------------------- # --- Create Call Graph in DOT Format --- print "Writing DOT file... ", # node_list : list of all the function calls in the source # edge_list : list of tuples denoting edges between calls node_list, edge_list = generate_nodes_edges(fr) call_gr = create_call_graph(node_list, edge_list) dot_file = open(os.path.join(outputpath, outputname + "_dot.txt"), "w") dot = write(call_gr) dot_file.write(dot) print "OK"