def update_state(): global ibin global count global step edge_filter.a = ug.ep.dist.a < ibin ug.set_edge_filter(edge_filter) gt.sfdp_layout(ug, pos=pos, K=K, init_step=step, max_iter=1) if count > 0 and count % 1000 == 0: win.graph.fit_to_window(ink=True) ibin += init_foot step += init_foot * 3 count += 1 # The following will force the re-drawing of the graph, and issue a # re-drawing of the GTK window. win.graph.regenerate_surface() win.graph.queue_draw() if offscreen: pixbuf = win.get_pixbuf() pixbuf.savev(r"%s/frame%06d.png" % (outpath, count), "png", [], []) if count > max_count: sys.exit(0) # We need to return True so that the main loop will call this function more # than once. return True
def graph_sequence(g, ibin, init_foot, fbin, init_step, max_iter, K): dist_array = np.array(g.ep.dist.a) filter_array = dist_array <= ibin gv = gt.GraphView(g, efilt=filter_array) ng = gt.Graph(gv, prune=True) pos = gt.sfdp_layout(ng, pos=ng.vp.pos, K=K) ng.vp.pos = pos graphs = [ng] cbin = ibin + init_foot # pos = gt.sfdp_layout(g, pos=pos, K=1.5) while cbin <= fbin: filter_array = dist_array <= cbin gv = gt.GraphView(g, efilt=filter_array) ng = gt.Graph(gv, prune=True) ng.copy_property(graphs[-1].vp.pos, tgt=ng.vp.pos) # print(ng.num_edges()) ng.vp.pos = gt.sfdp_layout( ng, pos=ng.vp.pos, max_iter=max_iter, init_step=init_step, K=K ) graphs.append(ng) cbin += init_foot # step += init_foot * 3 return graphs
def community(): g = gt.load_graph(filename) print 'Graph loaded, now finding community' # state = gt.BlockState(g, B=blocks) # for i in xrange(iterations): # if i < iterations / 2: # gt.mcmc_sweep(state) # else: # gt.mcmc_sweep(state, beta=float('inf')) # g.vp['blocks'] = state.get_blocks() spins = {} if 'blocks' in g.vp: spins = {'spins': g.vp['blocks']} g.vp['blocks'] = gt.community_structure(g, n_iter=iterations, n_spins=blocks, **spins) if 'pos' in g.vp: gt.sfdp_layout(g, groups=g.vp['blocks'], pos=g.vp['pos']) for i in xrange(blocks): print '%d nodes in block %d' % (len(gt.find_vertex(g, g.vp['blocks'], i)), i) g.save(filename)
def update_state(): global time mark.a = False visited.a = False g.set_vertex_filter(None) # visit the nodes in random order vs = list(g.vertices()) shuffle(vs) for v in vs: if time - 5 < order[v] < time + 5: visited[v] = True state[v] = past elif time == order[v]: visited[v] = True mark[v] = True state[v] = present else: visited[v] = False # Filter out the recovered vertices g.set_vertex_filter(visited) size = gt.prop_to_size(g.degree_property_map("total"), ma=10) for v in vs: vsize[v] = size[v] #vsize = gt.prop_to_size(deg) gt.sfdp_layout(g, pos=pos, eweight=g.ep['weight'], groups=g.vp['comm_infomap'], max_iter=0) print(g.num_vertices()) # The following will force the re-drawing of the graph, and issue a # re-drawing of the GTK window. win.graph.fit_to_window(g=g) win.graph.regenerate_surface(reset=True) win.graph.queue_draw() # if doing an offscreen animation, dump frame to disk time += 1 if time > max_time: sys.exit(0) print(time) if offscreen: pixbuf = win.get_pixbuf() strdate = (initial_date + timedelta(days=time)).strftime('%d-%m-%Y') pixbuf = put_text(pixbuf, strdate, 0, 10) pixbuf.savev(dir + '/news-date_%06d.png' % time, 'png', [], []) # We need to return True so that the main loop will call this function more # than once. print('-' * 80) return True
def sfdp_placement(g, output_folder, color_property_map=None, ask_for_acceptance=True, opacity=1): pos_sfdp = None while True: print('[tsnetwork] Performing SFDP') pos = gt.sfdp_layout(g, multilevel=True, C=1.2, p=1) pos_sfdp = g.new_vertex_property('vector<double>') for v in g.vertices(): pos_sfdp[v] = (float(pos[v][0]), float(pos[v][1])) print('[tsnetwork] Saving SFDP layout...') layout_io.save_drawing(output_folder, g, pos=pos_sfdp, description='sfdp', color_property_map=color_property_map, edge_colors="rgb", draw_vertices=False, opacity=opacity) if ask_for_acceptance: if usr_input.confirm('[tsnetwork] Is the generated sfdp layout ({0}) acceptable? [y/n]: '.format(output_folder + '/sfdp.pdf')): break else: break # Copy SFDP vertex coordinates to Y_init Y_init = np.zeros((g.num_vertices(), 2)) for v in g.vertices(): Y_init[int(v), :] = pos_sfdp[v] layout_io.save_layout(output_folder + '/sfdp.pickle', g, None, Y_init) return Y_init, pos_sfdp
def paint_graph(path, graph, communities): if path: sys.stdout.write('Drawing graph ... ') sys.stdout.flush() network = gt.Graph(graph, directed=False) folder = os.path.abspath(path) # colors = random.sample(range(100,151), len(communities)) r_cols = randomcolor.RandomColor().generate(count=len(communities) + 1) colors = [ list(int(r_col[1:][i:i + 2], 16) for i in (0, 2, 4)) for r_col in r_cols ] # color = graph.new_vertex_property('vector<float>') color = network.new_vertex_property('int') base_color = colors.pop() for v in network.vertices(): color[v] = (base_color[0] << 16) + ( base_color[1] << 8) + base_color[2] for community in communities: c = colors.pop() for v in community: color[v] = (c[0] << 16) + (c[1] << 8) + c[2] pos = gt.sfdp_layout(network) gt.graph_draw(network, pos=pos, vertex_fill_color=color, output=os.path.join(folder, 'graph-communities.svg')) sys.stdout.write('Ok!\n') sys.stdout.flush()
def all_data(): pos = gt.sfdp_layout(g, p=2.6, geometry=(700, 60)) # for k in g.vertices(): # print(k) nodes=[] convert =[] for v in g.vertices(): s_node={} s_node["name"]=v_userid[v]["userid"] s_node["cx"] = (pos[v][0]+650)/1.5 s_node["cy"] = (pos[v][1]+500)/1.5 convert.append(v_userid[v]["userid"]) # o_node={} # o_node[v_userid[v]["userid"]]=s_node nodes.append(s_node) all_data={} all_data['nodes']=nodes links=[] for e in g.edges(): s_edge={} s_edge['source'] = convert.index(str(e_source[e])) s_edge['target'] = convert.index(str(e_target[e])) # s_edge['source_x'] = (pos[e.source()][0]+400)/1.5 # s_edge['source_y'] = (pos[e.source()][1]+300)/1.5 # s_edge['target_x'] = (pos[e.target()][0]+400)/1.5 # s_edge['target_y'] = (pos[e.target()][1]+300)/1.5 links.append(s_edge) all_data['links']=links return all_data
def test_graphtool(): """Test creating a tree from a suffix tree. """ _num_bases, sequences, _ids = seqan.readFastaDNA(fasta_file('dm01r.fasta')) #for s in sequences: # s.remove('T') index = seqan.IndexStringDNASetESA(sequences) suffix = 'ACGTATGC' predicate = seqan.traverse.suffixpredicate(suffix) #predicate = seqan.traverse.depthpredicate(4) builder = seqan.io.graphtool.Builder(index, predicate=predicate) #pos = GT.radial_tree_layout(builder.graph, builder.graph.vertex(0)) pos = GT.sfdp_layout(builder.graph) GT.graph_draw( builder.graph, pos=pos, vertex_size=2, vertex_fill_color="lightgrey", vertex_text=builder.occurrences, vertex_pen_width=seqan.io.graphtool.root_vertex_property(builder), edge_text=seqan.io.graphtool.edge_labels_for_output(builder), edge_color=seqan.io.graphtool.color_edges_by_first_symbol(builder), edge_end_marker="none", edge_pen_width=2, edge_dash_style=seqan.io.graphtool.dash_non_suffix_edges(builder, suffix), #edge_pen_width=builder.edge_lengths, output="graphtool.png" ) return builder
def process_graphs(self, params, gs): """ :params params: Dict of parameter. :params gs: A list of graph-tool's Graph objects :returns: A Graph object and its layout """ parameter = {p: params[p] for p in self.parameter} ts = [] tposs = [] for g in gs: if g.num_vertices() <= 1: logging.warn("zero or one node in input_graph") ts.append(g) tpos = gt.sfdp_layout(g) tposs.append(tpos) continue state = gt.minimize_nested_blockmodel_dl(g, deg_corr=True) pos, t, tpos = gt.draw_hierarchy(state, output="output.pdf", **parameter) self.add_edge_id(t) self.propagate_label(t, g) self.copy_clabels(t, state) t.gp.label = t.new_gp("string") t.gp.label = OUTPUT_LABEL ts.append(t) tposs.append(tpos) return ts, tposs
def layout(g, rootv, sfdp=True, deg0=-45.0, degspan=90.0, radius=100): isouter = lambda x: not bool([ v for v in x.out_neighbours() if v != x ]) ## isouter = lambda x: x.out_degree()==0 outer_vertices = [ int(x) for x in g.vertices() if isouter(x) ] nouter = len(outer_vertices) angle = [float(deg0)] unit = float(degspan)/(nouter-1) iv2angle = {} outer_seen = set() iv2dist = defaultdict(lambda:0) all_seen = set() pos = g.new_vertex_property('vector<float>') pin = g.new_vertex_property('bool') pin[rootv] = 1 for v in g.vertices(): pos[v] = [0.0, 0.0] radius = float(radius) wt = g.new_edge_property('float') for e in g.edges(): strees = g.edge_strees[e] if len(strees) > 0: wt[e] = 1.0 else: wt[e] = 0.01 for iv in outer_vertices: pv, pe = gt.shortest_path(g, rootv, g.vertex(iv)) for e in reversed(pe): wt[e] += 0.5 g.wt = wt def traverse(v, dist=0, angle=angle): iv = int(v) if iv in outer_vertices: iv2dist[iv] = max(iv2dist[iv], dist) if iv not in outer_seen: ang = angle[0] iv2angle[iv] = ang angle[0] += unit rad = math.radians(ang) x = math.cos(rad) * radius y = math.sin(rad) * radius pos[v] = [x, y] pin[v] = 1 outer_seen.add(iv) all_seen.add(iv) for oe in v.out_edges(): ov = oe.target() ## if len(G.edge_strees[oe])>0 and (int(ov) not in all_seen): if int(ov) not in all_seen: traverse(ov, dist+1, angle) traverse(rootv) if sfdp: pos = gt.sfdp_layout(g, pos=pos, pin=pin, C=10, p=3,# theta=2, K=0.1, eweight=wt, mu=0.0, multilevel=False) return pos, pin
def make_article_graph(self, layout="arf"): """Make an article graph""" self.graph = Graph(directed=False) # add vertex self.graph.add_vertex(len(self.db)) # add properties cb = self.graph.new_vertex_property("int", self.db['Cited by'].values) self.graph.vertex_properties['nmb_citation'] = cb # Add links auths = list(self.author_betweeness.keys()) auth2ind = {auths[i]: i for i in range(len(auths))} auth2pub = self._get_author_publication() for _, pubs in auth2pub.items(): if len(pubs) < 2: continue combis = itertools.combinations(pubs, 2) self.graph.add_edge_list(list(combis)) # layout if layout == "arf": self.layout_pos = arf_layout(self.graph) elif layout == "sfpd": self.layout_pos = sfdp_layout(self.graph) elif layout == "fr": self.layout_pos = fruchterman_reingold_layout(self.graph) elif layout == "radial": self.layout_pos = radial_tree_layout(self.graph, auth2ind['Logan, B.E.']) else: raise ValueError()
def paint_graph(self, location, graph, communities): sys.stdout.write('Drawing graph ... ') sys.stdout.flush() network = gt.Graph(graph, directed=False) folder = os.path.abspath(self._output_dir) r_cols = randomcolor.RandomColor().generate(count=len(communities) + 1) colors = [ list(int(r_col[1:][i:i + 2], 16) for i in (0, 2, 4)) for r_col in r_cols ] color = network.new_vertex_property('int') base_color = colors.pop() for v in network.vertices(): color[v] = (base_color[0] << 16) + ( base_color[1] << 8) + base_color[2] for community in communities: c = colors.pop() for v in community: color[v] = (c[0] << 16) + (c[1] << 8) + c[2] pos = gt.sfdp_layout(network) gt.graph_draw(network, pos=pos, vertex_fill_color=color, output=os.path.join( folder, str(len(communities)) + '_' + location + '_graph-communities.svg')) sys.stdout.write('Ok!\n') sys.stdout.flush()
def test_graphtool(): """Test creating a tree from a suffix tree. """ _num_bases, sequences, _ids = seqan.readFastaDNA(fasta_file('dm01r.fasta')) #for s in sequences: # s.remove('T') index = seqan.IndexStringDNASetESA(sequences) suffix = 'ACGTATGC' predicate = seqan.traverse.suffixpredicate(suffix) #predicate = seqan.traverse.depthpredicate(4) builder = seqan.io.graphtool.Builder(index, predicate=predicate) #pos = GT.radial_tree_layout(builder.graph, builder.graph.vertex(0)) pos = GT.sfdp_layout(builder.graph) GT.graph_draw( builder.graph, pos=pos, vertex_size=2, vertex_fill_color="lightgrey", vertex_text=builder.occurrences, vertex_pen_width=seqan.io.graphtool.root_vertex_property(builder), edge_text=seqan.io.graphtool.edge_labels_for_output(builder), edge_color=seqan.io.graphtool.color_edges_by_first_symbol(builder), edge_end_marker="none", edge_pen_width=2, edge_dash_style=seqan.io.graphtool.dash_non_suffix_edges( builder, suffix), #edge_pen_width=builder.edge_lengths, output="graphtool.png") return builder
def draw_graph(only_classified_users): g = create_graph(only_classified_users, False) pos = gt.sfdp_layout( g, vweight=g.degree_property_map('total'), eweight=g.ep.weight, groups=g.vp.group, multilevel=True, gamma=1.0, mu_p=0.2, weighted_coarse=True, coarse_method="hybrid", # mivs hybrid ec ) return gt.graph_draw( g, pos=pos, vertex_fill_color=g.vp.group, vertex_size=gt.prop_to_size(g.degree_property_map('total'), ma=20, mi=3), edge_color='white', edge_pen_width=0.1, bg_color=[0.0, 0.0, 0.0, 1.0], output_size=[800, 600], # vertex_text=g.vp.username, # vertex_font_size=8, display_props=[g.vp.group_name, g.vp.username] # output=pj(RESOURCE_DIR, 'graph.pdf'), )
def layout_and_plot(net, color_pmap, outfile_pre, filename_mod = '.net', size_pmap = None, reverse_colors = False): ''' Plot the net, using a predefined layout if it's included as a vector property. :param net: The network to plot. :param color_pmap: Property map on `net` to color nodes. :size_pmap: Property map on `net` to set size of verticies. :param outfile_pre: Prefix for output filename. :param filename_mod: Extension to use on the output filename. ''' # Define a default size if size_pmap is None: size_pmap = net.new_vertex_property('float', val = 20) # If a layout isn't included, calculate it if 'layout' not in net.vp: print('Calculating graph layout') #net.vp['layout'] = gt.fruchterman_reingold_layout(net) net.vp['layout'] = gt.sfdp_layout(net, verbose = True) #net.vp['layout'] = gt.radial_tree_layout(net, 0, r=2) # Set the colormap if not reverse_colors: colormap = bwr else: colormap = bwr_r # Plot the graph gt.graph_draw(net, vertex_fill_color = color_pmap, vcmap = colormap, vertex_size = size_pmap, edge_pen_width = 1, pos = net.vp['layout'], #pin = True, fit_view = 1, output_size = (2000, 2000), output = outfile_pre + filename_mod + '.png') return net.vp['layout']
def print_groups(network, groups): group_vp = network.new_vertex_property("int") for i, group in enumerate(groups): for agent in group: group_vp[network.vertex_index[agent]] = i network.vp.group = group_vp pos = gt.sfdp_layout(network) gt.graph_draw(network, pos=pos, vertex_fill_color=group_vp) plt.show()
def drawMST(mst, outPrefix, isolate_clustering, clustering_name, overwrite): """Plot a layout of the minimum spanning tree Args: mst (graph_tool.Graph) A minimum spanning tree outPrefix (str) Output prefix for save files isolate_clustering (dict) Dictionary of ID: cluster, used for colouring vertices clustering_name (str) Name of clustering scheme to be used for colouring overwrite (bool) Overwrite existing output files """ import graph_tool.all as gt graph1_file_name = outPrefix + "/" + os.path.basename( outPrefix) + "_mst_stress_plot.png" graph2_file_name = outPrefix + "/" + os.path.basename( outPrefix) + "_mst_cluster_plot.png" if overwrite or not os.path.isfile(graph1_file_name) or not os.path.isfile( graph2_file_name): sys.stderr.write("Drawing MST\n") pos = gt.sfdp_layout(mst) if overwrite or not os.path.isfile(graph1_file_name): deg = mst.degree_property_map("total") deg.a = 4 * (np.sqrt(deg.a) * 0.5 + 0.4) ebet = gt.betweenness(mst)[1] ebet.a /= ebet.a.max() / 50. eorder = ebet.copy() eorder.a *= -1 gt.graph_draw(mst, pos=pos, vertex_size=gt.prop_to_size(deg, mi=20, ma=50), vertex_fill_color=deg, vorder=deg, edge_color=ebet, eorder=eorder, edge_pen_width=ebet, output=graph1_file_name, output_size=(3000, 3000)) if overwrite or not os.path.isfile(graph2_file_name): cluster_fill = {} for cluster in set(isolate_clustering[clustering_name].values()): cluster_fill[cluster] = list(np.random.rand(3)) + [0.9] plot_color = mst.new_vertex_property('vector<double>') mst.vertex_properties['plot_color'] = plot_color for v in mst.vertices(): plot_color[v] = cluster_fill[ isolate_clustering[clustering_name][mst.vp.id[v]]] gt.graph_draw( mst, pos=pos, vertex_fill_color=mst.vertex_properties['plot_color'], output=graph2_file_name, output_size=(3000, 3000))
def draw_graph(self): g = self.arcgraph pos = gt.sfdp_layout(self.arcgraph) gt.graph_draw(g, pos, output_size=(10000, 10000), vertex_fill_color=g.vp.color, edge_pen_width=10, output=join('/mnt', 'g', 'LisaDaten', 'Paper2', 'figures', 'arcgraph' + self.name + '.pdf'))
def draw_graph_heatmap(graph, value_map, output, directed=False, palette=sns.cubehelix_palette(10, start=.5, rot=-.75), position=None): # for normalize values = value_map.values() maxv = max(values) minv = min(values) if len(values) != len(graph): # some graph nodes missing from map. # make them 0 minv = min(minv, 0) gt_graph = gt.Graph(directed=directed) node_map = {node: gt_graph.add_vertex() for node in graph} if not directed: seen_edges = set() for node, edges in graph.iteritems(): i = node_map[node] for e in edges: j = node_map[e] if directed: gt_graph.add_edge(i, j) else: if (j, i) not in seen_edges: gt_graph.add_edge(i, j) seen_edges.add((i, j)) node_intensity = gt_graph.new_vertex_property("vector<float>") node_label = gt_graph.new_vertex_property("string") for id, value in value_map.iteritems(): node = node_map[id] node_intensity[node] = find_color(value, maxv, minv, palette) node_label[node] = id for id in graph: if id not in value_map: node = node_map[id] node_intensity[node] = find_color(0, maxv, minv, palette) node_label[node] = id if position is None: position = gt.sfdp_layout(gt_graph) gt.graph_draw(gt_graph, pos=position, vertex_text=node_label, vertex_fill_color=node_intensity, output=output) return position
def draw_graph(graph, value_map=None, output=None, show_ids=False, directed=False, position=None): gt_graph = gt.Graph(directed=directed) node_map = {node: gt_graph.add_vertex() for node in graph} if not directed: seen_edges = set() for node, edges in graph.iteritems(): i = node_map[node] for e in edges: j = node_map[e] if directed: gt_graph.add_edge(i, j) else: if (j, i) not in seen_edges: gt_graph.add_edge(i, j) seen_edges.add((i, j)) if position is None: position = gt.sfdp_layout(gt_graph) node_label = gt_graph.new_vertex_property("string") if value_map is not None: for id, value in value_map.iteritems(): node = node_map[id] node_label[node] = id for id in graph: if id not in value_map: node = node_map[id] node_label[node] = id elif show_ids: for id in graph: node = node_map[id] node_label[node] = id if show_ids: gt.graph_draw(gt_graph, pos=position, vertex_text=node_label, output=output) else: gt.graph_draw(gt_graph, pos=position, output=output) return position
def __init__(self, dn): # Base line settings: self.settings = {} self.settings['min_vert_size'] = 20 self.dn = dn # Standard matplotlib colors as vectors with opacity as last entry. self.colors = [(0.12, 0.47, 0.71, 1.), (1.0, 0.5, 0.05, 1.), (0.17, 0.62, 0.17, 1.), (0.84, 0.15, 0.16, 1.), (0.58, 0.4, 0.74, 1.), (0.55, 0.34, 0.29, 1.), (0.89, 0.47, 0.76, 1.)] # Create Graph and populate with edges and vertices: g = gt.Graph() self.g = g # Convert connections to edges and nodes: edge_list, node_list = dn.connections, dn.nodes # Add a vertex to the graph for every node and save it with the respective node object: node_list['vert'] = None for i, node_i in node_list.iterrows(): node_list['vert'][i] = g.add_vertex() self.node_list = node_list # Add an edge to the graph for every connection and save it with the respective 'source' and 'target' objects: edge_list['edge'] = None for i, edge_i in edge_list.iterrows(): source_i = node_list[node_list['node'] == edge_i['source']] target_i = node_list[node_list['node'] == edge_i['target']] edge_list['edge'][i] = g.add_edge(source_i.vert.values[0], target_i.vert.values[0]) self.edge_list = edge_list # Create property maps for the vertices: vert_prop = {} vert_prop['size'] = g.new_vertex_property('double') vert_prop['shape'] = g.new_vertex_property('string') vert_prop['text'] = g.new_vertex_property('string') vert_prop['pie_fractions'] = g.new_vertex_property('vector<double>') vert_prop['halo'] = g.new_vertex_property('bool') vert_prop['halo_size'] = g.new_vertex_property('double') vert_prop['halo_color'] = g.new_vertex_property('vector<double>') vert_prop['fill_color'] = g.new_vertex_property('vector<double>') self.vert_prop = vert_prop # Create property maps for the edges: edge_prop = {} edge_prop['pen_width'] = g.new_edge_property('double') edge_prop['color'] = g.new_edge_property('vector<double>') edge_prop['text'] = g.new_edge_property('string') self.edge_prop = edge_prop self.pos = gt.sfdp_layout(g, K=0.5)
def draw_cluster_save(graph, path=Path.cwd(), resolution=(4000, 4000)): pos = gt.sfdp_layout(graph, p=1) if path.is_dir(): path = path.joinpath(datetime.now().isoformat() + '.png') fill_color = _fill_vertex_color(graph) gt.graph_draw(graph, pos=pos, vertex_size=9, vertex_fill_color=fill_color, bg_color=(1, 1, 1, 1), output_size=resolution, output=str(path)) return
def saveSimulation(self): if not os.path.isdir(OUTPUT_DIR): os.mkdir(OUTPUT_DIR) if not os.path.isdir(SIMULATIONS_DIR): os.mkdir(SIMULATIONS_DIR) simulation_dir = SIMULATIONS_DIR + "s_" + self.simulation_id + "/" os.mkdir(simulation_dir) evolution_img_dir = simulation_dir + "evolution_imgs/" os.mkdir(evolution_img_dir) self.__exportSimulationOutputAsXML(dir=simulation_dir) # exports graph structure in xml: key0 and key1 are respectively opinion property and opinion color property self.original_graph.save(simulation_dir + "graph.xml") # 0%, 25%, 50%, 75%, 100% steps = [0] steps.append(int(self.rounds / 100 * 25)) steps.append(int(self.rounds / 100 * 50)) steps.append(int(self.rounds / 100 * 75)) steps.append(self.rounds) layout = gt.sfdp_layout(self.original_graph) gt.graph_draw(self.original_graph, pos=layout, vertex_text=self.original_graph.vertex_index, vertex_text_color=(1, 1, 1, 1), edge_color=(1, 1, 1, 0.7), output=simulation_dir + "graph.png", output_size=(1600, 1600), adjust_aspect=False, bg_color=(0.09411764705882353, 0.11372549019607843, 0.15294117647058825, 1)) # prints graph evolution in .png for step in steps: opinion = self.evolutionMap[step][0] opinion_color = self.evolutionMap[step][1] gt.graph_draw(self.original_graph, pos=layout, vertex_color=(1, 1, 1, 0), vertex_fill_color=opinion_color, vertex_text=opinion, vertex_text_color=(1, 1, 1, 1), edge_color=(1, 1, 1, 0.7), output=evolution_img_dir + "round-" + str(step) + ".png", output_size=(1600, 1600), adjust_aspect=False, bg_color=(0.09411764705882353, 0.11372549019607843, 0.15294117647058825, 1))
def draw(self, out_filename): """ Draws the POA graph to given out_file (out_filename is a filename which must contain the .png extension) """ # sfdp_layout seems to work for now. Try more later layout = sfdp_layout(self._g) graph_draw(self._g, pos=layout, vertex_text=self.v_names, vertex_font_size=12, output=out_filename)
def plot_graph_with_partition(out_neighbors, b, graph_object=None, pos=None): """Plot the graph with force directed layout and color/shape each node according to its block assignment Parameters ---------- out_neighbors : list of ndarray; list length is N, the number of nodes each element of the list is a ndarray of out neighbors, where the first column is the node indices and the second column the corresponding edge weights b : ndarray (int) array of block assignment for each node graph_object : graph tool object, optional if a graph object already exists, use it to plot the graph pos : ndarray (float) shape = (#nodes, 2), optional if node positions are given, plot the graph using them Returns ------- graph_object : graph tool object the graph tool object containing the graph and the node position info""" if len(out_neighbors) <= 5000: if graph_object is None: graph_object = gt.Graph() edge_list = [(i, j) for i in range(len(out_neighbors)) if len(out_neighbors[i]) > 0 for j in out_neighbors[i][:, 0]] graph_object.add_edge_list(edge_list) if pos is None: graph_object.vp['pos'] = gt.sfdp_layout(graph_object) else: graph_object.vp['pos'] = graph_object.new_vertex_property( "vector<float>") for v in graph_object.vertices(): graph_object.vp['pos'][v] = pos[ graph_object.vertex_index[v], :] block_membership = graph_object.new_vertex_property("int") vertex_shape = graph_object.new_vertex_property("int") block_membership.a = b[0:len(out_neighbors)] vertex_shape.a = np.mod(block_membership.a, 10) gt.graph_draw(graph_object, inline=True, output_size=(400, 400), pos=graph_object.vp['pos'], vertex_shape=vertex_shape, vertex_fill_color=block_membership, edge_pen_width=0.1, edge_marker_size=1, vertex_size=7) else: print('That\'s a big graph!') return graph_object
def sfdp_draw(graph, output="lattice_3d.pdf"): ''' Deprecated ''' output = os.path.join('saved_graph/pdf', output) sfdp_pos = gt.graph_draw(graph, pos=gt.sfdp_layout(graph, cooling_step=0.95, epsilon=1e-3, multilevel=True), output_size=(300,300), output=output) # print 'graph view saved to %s' %output return sfdp_pos
def braid_layout(self, **kwargs): """ Create a position vertex property for a braid. We use the actual bead time for the x coordinate, and a spring model for determining y. FIXME how do we minimize crossing edges? """ # FIXME what I really want here is symmetry about a horizontal line, and a spring-block layout that # enforces the symmetry. # 1. randomly assign vertices to "above" or "below" the midline. # 2. Compute how many edges cross the midline ( # 3. Compute SFDP holding everything fixed except those above, in a single cohort. # 4. Repeat for the half of the cohort below the midline # 5. Iterate this a couple times... groups = self.new_vertex_property("int") pos = self.new_vertex_property("vector<double>") pin = self.new_vertex_property("bool") xpos = 1 for c in self.cohorts(): head = gen = self.children(self.parents(c) - c) for (v, m) in zip(head, range(len(head))): pin[v] = True pos[v] = np.array([xpos, len(head) - 1 - 2 * m]) while gen.intersection(c): gen = self.children(gen).intersection(c) xpos += 1 xpos -= 1 # We already stepped over the tail in the above loop tail = self.parents(self.children(c) - c) - head for (v, m) in zip(tail, range(len(tail))): pin[v] = True pos[v] = np.array([xpos, len(tail) - 1 - 2 * m]) xpos += 1 # position remaining beads not in a cohort but not tips gen = self.children(c) - c for (v, m) in zip(gen, range(len(gen))): pos[v] = np.array([xpos, len(gen) - 1 - 2 * m]) while True: # Count number of generations to tips gen = self.children(gen) - gen if not gen: break xpos += 1 # position tips tips = frozenset(map(lambda x: self.vhashes[x.hash], self.tips)) for (v, m) in zip(tips, range(len(tips))): pos[v] = np.array([xpos, len(tips) - 1 - 2 * m]) pin[v] = True # feed it all to the spring-block algorithm. if 'C' not in kwargs: kwargs['C'] = 0.1 if 'K' not in kwargs: kwargs['K'] = 2 return gt.sfdp_layout(self, pos=pos, pin=pin, groups=groups, **kwargs)
def create(): mongo = MongoClient(mongo_uri) users = mongo.rc.twitter.graph.find(limit=n * (1+skip), timeout=False) print 'Retrieved %d users to create a graph with' % n users = skipevery(users, skip) users = printevery(users, 10, 'On user %d') g = make_graph(users, directed) if show: print 'Filtering complete. Now calculating base layout' g.vp['pos'] = gt.sfdp_layout(g) print 'Graph %s made, now saving as %s' % (g, filename) g.save(filename)
def build_graph(self): logging.info('Building graph') import graph_tool.all as GT graph = GT.Graph(directed=False) vertices = dict() names = graph.new_vertex_property('string') shapes = graph.new_vertex_property('string') colours = graph.new_vertex_property('string') def get_vertex(name, shape, colour): if name in vertices: return vertices[name] else: vertex = graph.add_vertex() names[vertex] = name shapes[vertex] = shape colours[vertex] = colour vertices[name] = vertex return vertex def get_word_vertex(name): return get_vertex(name, 'circle', 'blue') def get_topic_vertex(name): return get_vertex(name, 'square', 'red') def get_document_vertex(name): return get_vertex(name, 'triangle', 'green') for k in range(self.statistics.num_topics_used): name = 'TP: %d' % k topic_vertex = get_topic_vertex(name) for w in self.statistics.words_for_topic(k): word_vertex = get_word_vertex('Word: %d' % w) graph.add_edge(word_vertex, topic_vertex) for d in self.statistics.documents_for_topic(k): doc_vertex = get_document_vertex('Doc: %d' % d) graph.add_edge(topic_vertex, doc_vertex) pos = GT.sfdp_layout(graph) GT.graph_draw( graph, pos=pos, vertex_shape=shapes, vertex_fill_color=colours) return graph, vertices, shapes, colours
def make_author_graph(self, layout="arf"): """Make an author graph""" self.graph = Graph(directed=False) # add vertex auths = self.author_list self.graph.add_vertex(len(auths)) # add links auth2ind = {auths[i]: i for i in range(len(auths))} abet = [] authbet = copy.deepcopy(self.author_betweeness) for auth in auths: for col, weight in authbet[auth].items(): if col == auth: continue self.graph.add_edge(auth2ind[auth], auth2ind[col]) del authbet[col][auth] # ensure that edges are not doubled abet.append(weight) # add properties cb = self.graph.new_edge_property("int", abet) self.graph.edge_properties['weight'] = cb # layout if layout == "arf": self.layout_pos = arf_layout(self.graph, weight=self.graph.ep.weight, pos=self.layout_pos, max_iter=10000) elif layout == "sfpd": self.layout_pos = sfdp_layout(self.graph, eweight=self.graph.ep.weight, pos=self.layout_pos) elif layout == "fr": self.layout_pos = fruchterman_reingold_layout(self.graph, weight=self.graph.ep.weight, circular=True, pos=self.layout_pos) elif layout == "radial": nc = self.get_total_citation() main_auth_ind = np.argmax(list(nc.values())) main_auth = list(nc.keys())[main_auth_ind] self.layout_pos = radial_tree_layout(self.graph, auth2ind[main_auth]) elif layout == "planar": self.layout_pos = planar_layout(self.graph) else: raise ValueError()
def runSimulationOn(simulation_configurator: sc.SimulationConfigurator, animated: bool = False): g: gt.Graph = simulation_configurator.graph # initializes the graph with the original opinion data g = __init_properties(g) # map that stores the evolution of the graph during rounds saving its properties in a tuple evolutionMap = {0: (copy.copy(g.vertex_properties["opinion"]), copy.copy(g.vertex_properties["opinion_color"]))} rounds = 0 if animated: win = None layout = gt.sfdp_layout(g) while not __absorptionStateReached(g): # select uniformly at random a vertex from graph g v: gt.Vertex = g.vertex(random.randint(0, len(g.get_vertices()) - 1)) updated_opinion: int if random.uniform(0, 1) <= simulation_configurator.bias: # get opinion 1 with probability specified in the configurator updated_opinion = 1 else: # get updated opinion according to update rule specified in configurator updated_opinion = simulation_configurator.opinion_update_rule.run(g, v) g.vertex_properties["opinion"][v] = updated_opinion g.vertex_properties["opinion_color"][v] = __color_map(updated_opinion) rounds += 1 evolutionMap[rounds] = ( copy.copy(g.vertex_properties["opinion"]), copy.copy(g.vertex_properties["opinion_color"])) if animated: win = gt.graph_draw(g=g, pos=layout, vertex_fill_color=g.vertex_properties["opinion_color"], vertex_text=g.vertex_properties["opinion"], window=win, output_size=(900, 900), return_window=True, main=False) # return an object containing the information produced during the simulation return sr.SimulationResult(evolutionMap, simulation_configurator)
def output_json_b(u): pos = gt.sfdp_layout(u, p=2.6, K=40, C=1) nodes = [] convert = [] for v in u.vertices(): s_node = {} s_node["name"] = v_userid[v]["userid"] s_node['race'] = v_race[v]["race"] s_node['cohort'] = v_cohort[v]['cohort'] s_node['gender'] = v_gender[v]['gender'] s_node['c1net'] = v_c1net[v]['c1net'] s_node['c2net'] = v_c2net[v]['c2net'] s_node['c3net'] = v_c3net[v]['c3net'] s_node['c4net'] = v_c4net[v]['c4net'] s_node['c5net'] = v_c5net[v]['c5net'] s_node['c6net'] = v_c6net[v]['c6net'] s_node['c7net'] = v_c7net[v]['c7net'] s_node["out-degree"] = v.out_degree() s_node["in-degree"] = v.in_degree() s_node["cx"] = (pos[v][0]) s_node["cy"] = (pos[v][1]) convert.append(v_userid[v]["userid"]) # o_node={} # o_node[v_userid[v]["userid"]]=s_node nodes.append(s_node) all_data = {} all_data['nodes'] = nodes links = [] for e in u.edges(): s_edge = {} s_edge['source'] = convert.index(str(e_source[e])) s_edge['target'] = convert.index(str(e_target[e])) s_edge['type'] = e_type[e]['type'] s_edge['year'] = e_year[e]['year'] s_edge['weight'] = len(u.edge(e.target(), e.source(), all_edges=True, add_missing=False)) + \ len(u.edge(e.source(), e.target(), all_edges=True, add_missing=False)) # s_edge['source_x'] = (pos[e.source()][0]+400)/1.5 # s_edge['source_y'] = (pos[e.source()][1]+300)/1.5 # s_edge['target_x'] = (pos[e.target()][0]+400)/1.5 # s_edge['target_y'] = (pos[e.target()][1]+300)/1.5 links.append(s_edge) all_data['links'] = links return (all_data)
def draw_graph(graph): vertices_names = graph.new_vertex_property('string') for vertex in graph.vertices(): vertices_names[vertex] = \ graph.vertex_properties['actors_on_vertices'][vertex] + \ ' ' + str(graph.vertex_properties['pagerank'][vertex]) pos = sfdp_layout(graph, eweight=graph.edge_properties['weights_on_edges']) graph_draw( graph, pos=pos, vertex_fill_color=graph.vertex_properties['pagerank'], vertex_text=vertices_names, vertex_font_size=5, output_size=(1024, 1024), output='pagerank/' + graph.graph_properties['repo_on_graph'].replace('/', '%') + '.pdf')
def __sfdp_layout(self, net, **params): """ :Graph net: A Graph :string vweight: Attribute name of vertex weight :string eweight: Attribute name of edge weight :string pin: A name of vertex attribute with boolean values, which, if given, specify the vertices which will not have their positions modified. :string groups: A name of vertex attribute with group assignments. Vertices belonging to the same group will be put close together. :string pos: A name of initial vertex layout attribute. If not provided, it will be randomly chosen. """ valid_params = {} vparam = ["vweight", "pin", "groups", "pos"] for param_name in vparam: if params[param_name] in net.vp: valid_params[param_name] = net.vp[params[param_name]] eparam = ["eweight"] for param_name in eparam: if params[param_name] in net.ep: valid_params[param_name] = net.ep[params[param_name]] return gt.sfdp_layout(net, **valid_params)
def draw_graph(graph): vertices_names = graph.new_vertex_property('string') for vertex in graph.vertices(): vertices_names[vertex] = \ graph.vertex_properties['actors_on_vertices'][vertex] + \ ' ' + str(graph.vertex_properties['pagerank'][vertex]) pos = sfdp_layout(graph, eweight=graph.edge_properties['weights_on_edges']) graph_draw(graph, pos=pos, vertex_fill_color=graph.vertex_properties['pagerank'], vertex_text=vertices_names, vertex_font_size=5, output_size=(1024, 1024), output='pagerank/' + graph.graph_properties['repo_on_graph'].replace('/', '%') + '.pdf')
def build_result_graph_in_whole(self, reactionlist, filename): g = gt.Graph() reactions = {r.id: r for r in self.model.reactions} edges = [] for reaction in self.model.reactions: for c, val in reaction.reactants.iteritems(): if val < 0: edges.append((c, reaction.id)) else: edges.append((reaction.id, c)) ids = g.add_edge_list(edges, hashed=True, string_vals=True) g.vertex_properties["ids"] = ids g.vertex_properties["names"] = g.new_vertex_property("string") g.vertex_properties["color"] = g.new_vertex_property("string") g.vertex_properties["size"] = g.new_vertex_property("int") g.edge_properties["arrows"] = g.new_edge_property("string") for v in g.vertices(): v_id = g.vp.ids[v] g.vp.names[v] = v_id g.vp.color[ v] = "red" if v_id in reactionlist else "lightgrey" if v_id in reactionlist else "white" g.vp.size[v] = 10 if v_id in reactions else 3 for e in g.edges(): g.ep.arrows[e] = "none" pos = gt.sfdp_layout(g, multilevel=True) gt.graph_draw(g, pos, vertex_size=g.vp.size, vertex_fill_color=g.vp.color, edge_pen_width=0.5, edge_end_marker=g.ep.arrows, output=filename, fit_view=True, output_size=(10000, 10000))
def paint_kcore(path, graph, name): if path: sys.stdout.write('Drawing kcore graph ... ') sys.stdout.flush() network = gt.Graph(graph, directed=False) folder = os.path.abspath(path) network = gt.GraphView(network, vfilt=gt.label_largest_component(network)) kcore = gt.kcore_decomposition(network) pos = gt.sfdp_layout(network) gt.graph_draw(network, pos=pos, vertex_fill_color=kcore, vertex_text=kcore, output=os.path.join(folder, str(name) + '-graph-kcore.svg')) sys.stdout.write('Ok!\n') sys.stdout.flush()
def draw_graph_animation(graph): vertices_names = graph.new_vertex_property('string') graph.vertex_properties['vertices_names'] = vertices_names for vertex in graph.vertices(): vertices_names[vertex] = \ graph.vertex_properties['actors_on_vertices'][vertex] + \ ' ' + str(graph.vertex_properties['pagerank'][vertex]) graph.vertex_properties['pos'] = sfdp_layout( graph, eweight=graph.edge_properties['weights_on_edges']) dir_name = 'pagerank/' + \ graph.graph_properties['repo_on_graph'].replace('/', '%') + '/' os.mkdir(dir_name) def event_bulk(vertex): event = graph.vertex_properties['events_on_vertices'][vertex] return event['created_at'].strftime("%Y-%m-%d %H") batch_sizes = map(lambda x: len(x[1]), sorted(group_by( event_bulk, graph.vertices()).items(), key=lambda x: x[0])) def tail_number(n): if n == 0: return batch_sizes[0] else: return tail_number(n - 1) + batch_sizes[n] batch_numbers = map(tail_number, range(len(batch_sizes))) map(draw_graph_frame, map( lambda x: (graph, dir_name, x), batch_numbers)) images = [Image.open(dir_name + str(i) + '.png') for i in batch_numbers] writeGif(dir_name + 'animation.gif', images, duration=0.1)
def draw(self, filename, engine='graphviz'): """Draws the TS. The filename extension determines the format. [engine] Rendering engine used to draw the TS. Valid values: cairo, graphviz, astg (for draw_astg) If [filename] is None and engine is 'cairo', then the interactive window is used""" if engine == 'graphviz': vprops = {'label':self.vp_state_name} eprops = {'label':self.ep_edge_label} if 'frequency' in self.g.vertex_properties: # vp_width = self.g.new_vertex_property("float") all_traces = self.vp_state_frequency[self.get_state(self.get_initial_state())] # for v in self.g.vertices(): # vp_width[v] = 3.0*self.vp_state_frequency[v]/all_traces # vprops['width'] = vp_width vsize=(self.vp_state_frequency, 2.0/all_traces) else: vsize=0.105 if 'frequency' in self.g.edge_properties: all_traces = self.vp_state_frequency[self.get_state(self.get_initial_state())] penwidth=(self.ep_edge_frequency, 10.0/all_traces) else: penwidth=1.0 print vprops if self.g.num_vertices() < 150: layout = 'dot' overlap = False else: layout = None overlap = 'Prism' gt.graphviz_draw(self.g, ratio='auto', vprops=vprops, splines=True, eprops=eprops, sep=1.0, overlap=overlap, vsize=vsize, penwidth=penwidth, layout=layout, output=filename) elif engine=='cairo': #use cairo pos = gt.sfdp_layout(self.g) vprops = {'text':self.vp_state_name} if 'frequency' in self.g.vertex_properties: vp_width = self.g.new_vertex_property("float") all_traces = self.vp_state_frequency[self.get_state(self.get_initial_state())] #use numpy array access # vp_widht.a = int(max(100.0*self.vp_state_frequency.a/all_traces) for v in self.g.vertices(): vp_width[v] = int(100.0*self.vp_state_frequency[v]/all_traces) vprops['size'] = vp_width gt.graph_draw(self.g, pos=pos, vprops=vprops, output=filename) elif engine=='astg': if filename.endswith('.ps'): format = '-Fps' elif filename.endswith('.gif'): format = '-Fgif' elif filename.endswith('.dot'): format = '-Fdot' elif filename.endswith('.png'): format = '-Fpng' elif filename.endswith('.svg'): format = '-Fsvg' else: raise TypeError, 'Unsupported output for draw_astg' #check if file can be forwarded as input_filename if self.filename and not self.modified_since_last_write: input_filename = self.filename else: # or create tmp file with save tmpfile = tempfile.NamedTemporaryFile(mode='w', delete=False) print "Saving TS to temporary file '{0}'".format( tmpfile.name ) self.save(tmpfile) tmpfile.close() input_filename = tmpfile.name params = ['draw_astg', '-sg', '-noinfo', format, input_filename] output = subprocess.check_output( params ) with open(filename,'w+b') as f: f.write(output)
def generate_graph(): """ brew tap homebrew/science brew install graph-tool """ from graph_tool.all import price_network, sfdp_layout, graph_draw from graph_tool.all import dfs_search, DFSVisitor, seed_rng from numpy.random import seed class AnnotationVisitor(DFSVisitor): def __init__(self, pred, dist): self.pred = pred self.dist = dist self.roots = {} def tree_edge(self, e): depth = self.dist[e.source()] if depth == 1: genre = int(e.source()) if genre not in self.roots: self.roots[genre] = len(self.roots) else: genre = self.pred[e.source()] self.pred[e.target()] = genre self.dist[e.target()] = depth + 1 # For run-to-run stability, provide a constant seed: seed(SEED) seed_rng(SEED) print 'Generating graph...' g = price_network(2000) print 'Performing layout...' pos = sfdp_layout(g) print 'Adding depths...' dist = g.new_vertex_property("int") pred = g.new_vertex_property("int64_t") g.set_directed(False) visitor = AnnotationVisitor(pred, dist) dfs_search(g, g.vertex(0), visitor) print 'Iterating over verts...' flattened = [] maxp = [-9999, -9999] minp = [+9999, +9999] maxd = 0 for v in g.vertices(): root_id = pred.a[v] if root_id not in visitor.roots: continue x, y, z = pos[v].a[0], pos[v].a[1], visitor.roots[root_id] minp[0] = min(minp[0], x) minp[1] = min(minp[1], y) maxp[0] = max(maxp[0], x) maxp[1] = max(maxp[1], y) maxd = max(maxd, dist.a[v]) flattened += [x, y, z] print 'max depth is', maxd print 'nroots is', len(visitor.roots) print 'ncolors is', len(COLORS) extent = (maxp[0] - minp[0], maxp[1] - minp[1]) padding = extent[0] * PADDING_FRACTION minp[0] -= padding minp[1] -= padding maxp[0] += padding maxp[1] += padding scale = [ 1.0 / (maxp[0] - minp[0]), 1.0 / (maxp[1] - minp[1])] scale = min(scale[0], scale[1]) midp = [ 0.5 * (maxp[0] + minp[0]), 0.5 * (maxp[1] + minp[1])] flatarray = [] for v in g.vertices(): root_id = pred.a[v] if root_id not in visitor.roots: continue x, y, root = pos[v].a[0], pos[v].a[1], visitor.roots[root_id] x = (0.5 + (x - midp[0]) * scale) y = (0.5 + (y - midp[1]) * scale) prom = int(dist.a[v]) flatarray += [x, y, root, prom] return flatarray
Pin[g.vertex(1)] = True Pin[g.vertex(0)] = True assert isinstance(Pin, object) pos = gt.sfdp_layout(g, epsilon=0.2) pos[g.vertex(1)][0] = pos[g.vertex(0)][0] pos = gt.sfdp_layout(g) g.vertex_properties['pos'] = pos g.save('S2Dlattice.xml.gz') ''' g = gt.lattice([N, N]) Pin = g.new_vertex_property('bool') Pin.a = False Pin[g.vertex(1)] = True Pin[g.vertex(0)] = True assert isinstance(Pin, object) pos = gt.sfdp_layout(g, epsilon=0.2) pos[g.vertex(1)][0] = pos[g.vertex(0)][0] pos = gt.sfdp_layout(g) g.vertex_properties['pos'] = pos g.save('2Dlattice.xml.gz') ''' g = gt.lattice([N, N]) pos = gt.sfdp_layout(g, epsilon=0.000000001, C=1) #pos = gt.sfdp_layout(g) g.vertex_properties['pos'] = pos for i in range(0, N-1): for j in range(0, N-1): g.add_edge(i*N+j, i*N+j+N+1)
def display(self): pos = sfdp_layout(self.g) graph_draw(self.g, pos=pos, vertex_size = 50, vertex_text=self.g.vp.display, vertex_font_size=12, vertex_text_position=-1, edge_pen_width=3) #self.g.vertex_index)
## if taxid in r.conflicts: ## for n in r.conflicts[taxid]: ## ne = gv.add_edge(tgt, n.v) ## ecolor[ne] = 'red' ## ewidth[ne] = 1.0 ## gv.wt[ne] = 1.0 ## edges[ne] = 1 if not edges[e]: edges[e] = 1 ecolor[e] = 'red' ewidth[e] = 1.0 gv.wt[e] = 1.0 pos = gt.sfdp_layout(gv.g, pos=pos, pin=pin, ## C=10, p=3,# theta=2, ## K=0.1, eweight=gv.wt, ## mu=0.0, multilevel=False) gt.interactive_window( gv.g, pos=pos, pin=True, vertex_fill_color=vcolor, vertex_text_position=3.1415, ## vertex_text=vtext, vertex_text=taxg.vertex_name, vertex_size=vsize, edge_color=ecolor, edge_pen_width=ewidth, update_layout=False )
for e in taxg.edges(): src = e.source() tgt = e.target() if not verts[src]: verts[src] = 1 pos[src] = [0.0, 0.0] vcolor[src] = 'red' if not verts[tgt]: verts[tgt] = 1 pos[tgt] = [0.0, 0.0] vcolor[tgt] = 'red' if not edges[e]: edges[e] = 1 ecolor[e] = 'red' ewidth[e] = 1.0 gv.wt[e] = 1.0 pos = gt.sfdp_layout(gv, pos=pos, pin=pin, eweight=gv.wt, multilevel=False) gt.interactive_window( gv, pos=pos, pin=True, vertex_fill_color=vcolor, vertex_text_position=3.1415, vertex_text=taxg.vertex_name, vertex_size=vsize, edge_color=ecolor, edge_pen_width=ewidth, update_layout=False )
e = pairs_graph.add_edge(v1, v2) edge_weights[e] = cur_weight components_label = label_components(pairs_graph) largest_label = label_largest_component(pairs_graph) #print(components_label[0].a) print(largest_label.a) degr = pairs_graph.new_vertex_property("int") for v in pairs_graph.vertices(): degr[v] = v.out_degree() print("hihi") posPlot = sfdp_layout(pairs_graph, gamma = 5, max_level = 50, vweight = degr, C = 0.5, K = 5, p = 7, theta = 0.1) print("hi") graph_draw(pairs_graph, pos=posPlot, vertex_text=ver_names, edge_text=edge_weights) # position = arf_layout(pairs_graph, max_iter=3) # graph_draw(pairs_graph, pos=position, vertex_text=ver_names, edge_text=edge_weights, vertex_size=5, vertex_font_size=10, # edge_font_size=15, vertex_fill_color=part) mc, part = min_cut(pairs_graph, edge_weights) print("Cut value = " + str(mc)) group_property = pairs_graph.new_vertex_property("int") for v in pairs_graph.vertices(): if part[v] == 0:
#! /usr/bin/env python # -*- coding: utf-8 -*- """ @author: lockheed Information and Electronics Engineering Huazhong University of science and technology E-mail:[email protected] Created on: 4/29/14 3:21 PM Copyright (C) lockheedphoenix """ import graph_tool.all as gt g = gt.lattice([60, 60]) pos = gt.sfdp_layout(g) g.vertex_properties['pos']=pos K = 2 ''' for k in range(2, K): for v in g.vertices(): g.add_edge(v, g.vertex((g.vertex_index[v]+k)%(g.num_vertices()))) ''' g.save('RG.xml.gz')
def build_json(choice): if choice == "1": ## Loads a graph with the OTT taxonomy taxonomy="ott" print "Loading OTT taxonomy into graph..." g = tg.load_taxonomy_graph('taxonomy/ott2.2/ott2.2.xml.gz') print "OTT taxonomy Graph loaded successfully." print "Loading ott-treecache file..." datafile = open('trees/ott-treecache.txt', 'r') #read in the treecache file print "Loaded." elif choice == "2": taxonomy="ncbi" print "Loading NCBI taxonomy into graph..." g = tg.load_taxonomy_graph('taxonomy/ncbi/ncbi.xml.gz') print "NCBI taxonomy Graph loaded successfully." print "Loading ncbi-treecache file..." datafile = open('trees/ncbi-treecache.txt', 'r') #read in the treecache file print "Loaded." data = [] errors = [] blacklist = [] ## Loop all of the entries in the treecache.txt file and assign them to data. for row in datafile: data.append(row) #print row ## Creates a Tree Blacklist that will ignore problematic trees that cause crashes based on strange formatting issues until then can be resolved. print "Loading tree blacklist..." tree_blacklist = open('trees/tree_blacklist.txt', 'r') #read in the tree blacklist file print "Loaded." ## Loop all of the entries in the tree_blacklist.txt file and assign them to blacklist. for tree in tree_blacklist: blacklist.append(tree.strip()) rowcount = 0 for row in data: #iterate through each unique stree id in the file allowing the code below to generate the graph, write the JSON and save the file active_tree = row.split(":") #split the row from treecache into tree id and newick string tree if active_tree[0] in blacklist: ## if a tree is in the blacklist, ignore it. print ("Tree %s is being ignored as it is black listed." % active_tree[0]) else: stree = int(active_tree[0]) # convert tree id string into int r = ivy.tree.read(active_tree[1].replace("?", "")) #read the tree, also replacing an extraneous ? characters leafcount = 0 r.ladderize() ivy.tree.index(r) for n in r: if n.isleaf: leafcount = leafcount + 1 v = n.label.split('_') n.snode_id = int(v[0]) n.taxid = int(v[1]) if (len(v)>1 and v[1] and v[1] != 'None') else None else: n.snode_id = int(n.label) if leafcount <= 5000: #check to prune trees that have more than 5000 leaves. They will not display correctly in graph form. try: #used to catch all errors from incorrectly formatted trees (ie: ? characters, and other issues) r.stree = stree ### ADD CODE HERE TO SKIP TREES WITH MORE THAN 5000 leaves tg.map_stree(g, r) taxids = set() for lf in r.leaves(): taxids.update(lf.taxid_rootpath) taxg = tg.taxid_new_subgraph(g, taxids) # taxg is a new graph containing only the taxids in stree # these properties will store the vertices and edges that are traced # by r verts = taxg.new_vertex_property('bool') edges = taxg.new_edge_property('bool') # add stree's nodes and branches into taxonomy graph tg.merge_stree(taxg, r, stree, verts, edges) # verts and edges now filter the paths traced by r in taxg # next, add taxonomy edges to taxg connecting 'incertae sedis' # leaves in stree to their containing taxa for lf in r.leaves(): if lf.taxid and lf.incertae_sedis: taxv = taxg.taxid_vertex[lf.taxid] ev = taxg.edge(taxv, lf.v, True) if ev: assert len(ev)==1 e = ev[0] else: e = taxg.add_edge(taxv, lf.v) taxg.edge_in_taxonomy[e] = 1 # make a view of taxg that keeps only the vertices and edges traced by # the source tree gv = tg.graph_view(taxg, vfilt=verts, efilt=edges) gv.vertex_strees = taxg.vertex_strees gv.edge_strees = taxg.edge_strees # the following code sets up the visualization ecolor = taxg.new_edge_property('string') for e in taxg.edges(): est = taxg.edge_strees[e] eit = taxg.edge_in_taxonomy[e] if len(est) and not eit: ecolor[e] = 'blue' elif len(est) and eit: ecolor[e] = 'green' else: ecolor[e] = 'yellow' ewidth = taxg.new_edge_property('int') for e in taxg.edges(): est = taxg.edge_strees[e] if len(est): ewidth[e] = 3 else: ewidth[e] = 1 vcolor = taxg.new_vertex_property('string') for v in taxg.vertices(): if not taxg.vertex_in_taxonomy[v]: vcolor[v] = 'blue' else: vcolor[v] = 'green' vsize = taxg.new_vertex_property('int') for v in taxg.vertices(): if taxg.vertex_in_taxonomy[v] or v.out_degree()==0: vsize[v] = 8 else: vsize[v] = 2 pos, pin = tg.layout(taxg, gv, gv.root, sfdp=True, deg0=195.0, degspan=150.0, radius=400) for v in gv.vertices(): pin[v] = 1 for e in taxg.edges(): src = e.source() tgt = e.target() if not verts[src]: verts[src] = 1 pos[src] = [0.0, 0.0] vcolor[src] = 'red' if not verts[tgt]: verts[tgt] = 1 pos[tgt] = [0.0, 0.0] vcolor[tgt] = 'red' if not edges[e]: edges[e] = 1 ecolor[e] = 'red' ewidth[e] = 1.0 gv.wt[e] = 1.0 pos = gt.sfdp_layout(gv, pos=pos, pin=pin, eweight=gv.wt, multilevel=False) ### Use function in TreeGraph.py to parse Graph(gv) into JSON print "Generating JSON..." result = tg.graph_json(gv, pos=pos, ecolor=ecolor, ewidth=ewidth, vcolor=vcolor, vsize=vsize) result = result[1:] #strip the original { from the json so we can insert the time stamp date = time.strftime("%Y%m%d%I%M%S") # grab the system date for the filename and convert it to a string treeid = str(stree) # convert stree int into a string timestamp = "{\"timestamp\": \"%s\", " %date final_result = timestamp+result # add date to first line of json file for later parsing path = str(os.path.dirname(os.path.realpath(__file__))) path = path[:-8] path = "%s//%s/" % (path, taxonomy) # build the full path to write the file too filename = "%stree_%s.JSON" % (path, treeid) # build the full file_name for writing if not os.path.exists(path): ## if directory doesn't exist, create it. os.makedirs(path) f = open(filename, 'w') f.write(final_result) f.close print "Done." rowcount = rowcount + 1 except: # catch *all* exceptions e = sys.exc_info()[0] e = str(e) treeid = str(stree) print ("Error: %s</p>" % e) errorstring = "Error: " + e + " on Tree: " + treeid # rough hack to store trees with errors and the general error errors.append(errorstring) # store all of the error strings rowcount = rowcount + 1 continue ## continue converting the rest of the trees into JSON even if a specific tree has errors else: print "Tree has more than 5000 leaves. No graph will be generated." print "JSON Generation Complete." ## write the error strings to a log file for review later if errors: with open("error_log.txt", "w+") as error_log: pickle.dump(errors, error_log)
#! /usr/bin/env python # -*- coding: utf-8 -*- """ @author: lockheed Information and Electronics Engineering Huazhong University of science and technology E-mail:[email protected] Created on: 3/13/14 9:06 PM Copyright (C) lockheedphoenix """ import graph_tool.all as gt """ 20140320 nig g = gt.load_graph('BA_networks.xml.gz') pos = gt.arf_layout(g) g.vertex_properties['pos'] = pos g.save('BA_networks_2.xml.gz') """ g = gt.lattice([40, 40], True) pos = gt.sfdp_layout(g, cooling_step=0.99, epsilon=1e-3) g.vertex_properties['pos'] = pos g.save('3Dlattice.xml.gz') gt.graph_draw(g, pos)
def draw(self, filename, engine='cairo'): """Draws the TS. The filename extension determines the format. [engine] Rendering engine used to draw the TS. Valid values: cairo, graphviz, astg (for draw_astg) If [use_graphviz] is False, then Cairo is used to draw the graf. In such a case, if [filename] is None, then the interactive window is used""" if engine == 'graphviz': pass elif engine=='cairo': #use cairo pos = gt.sfdp_layout(self.g) names = self.vp_elem_name.copy() shapes = self.vp_elem_type.copy() color = self.g.new_vertex_property("vector<double>") for v in self.g.vertices(): if self.vp_elem_type[v] == 'place': if self.vp_place_initial_marking[v] > 0: names[v] = str(self.vp_place_initial_marking[v]) else: names[v] = '' if shapes[v] == 'place': shapes[v] = 'circle' else: shapes[v] = 'square' if self.vp_elem_type[v] == 'place': color[v] = [0.7,0.2,0.2,0.9] else: color[v] = [0.2,0.2,0.7,0.9] vprops = {'text':names, 'shape':shapes, 'fill_color':color} # if 'frequency' in self.g.vertex_properties: # vp_width = self.g.new_vertex_property("float") # all_traces = self.vp_state_frequency[self.get_state(self.get_initial_state())] # #use numpy array access ## vp_widht.a = int(max(100.0*self.vp_state_frequency.a/all_traces) # for v in self.g.vertices(): # vp_width[v] = int(100.0*self.vp_state_frequency[v]/all_traces) # vprops['size'] = vp_width gt.graph_draw(self.g, pos=pos, vprops=vprops, output=filename) elif engine=='astg': if filename.endswith('.ps'): format = '-Tps' elif filename.endswith('.gif'): format = '-Tgif' elif filename.endswith('.dot'): format = '-Tdot' else: raise TypeError, 'Unsupported output for draw_astg' #check if file can be forwarded as input_filename if self.filename and not self.modified_since_last_write: input_filename = self.filename else: # or create tmp file with save tmpfile = tempfile.NamedTemporaryFile(mode='w', delete=False) print "Saving TS to temporary file '{0}'".format( tmpfile.name ) self.save(tmpfile) tmpfile.close() input_filename = tmpfile.name params = [draw_astg, '-sg', '-nonames', '-noinfo', format, input_filename] output = subprocess.check_output( params ) with open(filename,'w+b') as f: f.write(output) else: raise ValueError, "Unknown graphical engine"
def get_pos(g, algorithm='sfdp'): if algorithm is 'sfdp': return gt.sfdp_layout(g) elif algorithm is 'arf': return gt.arf_layout(g, max_iter=2000)
def layout(g, h, t, pos=None): g = gt.GraphView(g, efilt=lambda e: h[e] or t[e]) return gt.sfdp_layout(g, pos=pos)