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 output_json_fd_lay(u, model): if model == "FR": pos = gt.fruchterman_reingold_layout(u, n_iter=100, r=100, a=10) if model == "Ran": pos = gt.random_layout(g, dim=2) if model == "ARF": pos = gt.arf_layout(g, max_iter=0) # 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['gender'] = v_gender[v]["gender"] s_node['schregion'] = v_schregion[v]['schregion'] s_node['school'] = v_school[v]['school'] s_node['major'] = v_major[v]['major'] s_node['marriage'] = v_marriage[v]['marriage'] s_node['grade'] = v_grade[v]['grade'] s_node['liveplace'] = v_liveplace[v]['liveplace'] s_node['height'] = v_height[v]['height'] s_node['weight'] = v_weight[v]['weight'] s_node['scorelevel'] = v_scorelevel[v]['scorelevel'] s_node['birthyear'] = v_birthyear[v]['birthyear'] 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['date'] = e_date[e]['date'] s_edge['dailycount'] = e_dailycount[e]['dailycount'] 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)) links.append(s_edge) all_data['links'] = links # gt.graph_draw(u, pos=pos, output="graph-draw.png") return(all_data)
def __fruchterman_reingold_layout(self, net, **params): """ :Graph net: A Graph :string weight: Attribute name of an edge attribute with the respective weights :string pos: Attribute name of vector vertex property maps where the coordinates should be stored. If provided, this will also be used as the initial position of the vertices. """ valid_params = {} vparam = ["pos"] for param_name in vparam: if params[param_name] in net.vp: valid_params[param_name] = net.vp[params[param_name]] eparam = ["weight"] for param_name in eparam: if params[param_name] in net.ep: valid_params[param_name] = net.ep[params[param_name]] return gt.fruchterman_reingold_layout(net, **valid_params)
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 output_json_fd_lay_b(u, model): if model == "FR": pos = gt.fruchterman_reingold_layout(u, n_iter=100, r=100, a=10) if model == "Ran": pos = gt.random_layout(g, dim=2) if model == "ARF": pos = gt.arf_layout(g, max_iter=0) 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)) links.append(s_edge) all_data['links'] = links return (all_data)
def draw_graph( adata: AnnData, layout: _Layout = 'sfdp', # init_pos: Union[str, bool, None] = None, # root: Optional[int] = None, use_tree: bool = False, random_seed: Optional[int] = None, adjacency: Optional[spmatrix] = None, key_added_ext: Optional[str] = None, key: Optional[str] = 'schist', copy: bool = False, **kwds, ): """\ Extends scanpy.tools.draw_graph function using some layouts available in graph-tool library. Three layouts are available here: - SFDP spring-block layout. - ARF spring-block layout. - Fruchterman-Reingold spring-block layout. Fruchterman-Reingold is already available in scanpy, but here can be used to render the nested model tree. In order to use these plotting function, the NestedBlockState needs to be saved when building the model, so `save_state=True` needs to be set. Parameters ---------- adata Annotated data matrix. A NestedBlockState object needs to be saved layout A layout among 'sfdp', 'fr' or 'arf'. Other graph-tool layouts haven't been implemented. use_tree When this is set, the tree of the nested model is used to generate layout, otherwise the layout only accounts for the neighborhood graph. random_seed Random number to be used as seed for graph-tool adjacency Sparse adjacency matrix of the graph, defaults to `adata.uns['neighbors']['connectivities']`. key_added_ext By default, append `layout`. key The slot in `AnnData.uns` containing the state. Default is 'nsbm' copy Return a copy instead of writing to adata. **kwds Parameters of chosen igraph layout. See e.g. `fruchterman-reingold`_ [Fruchterman91]_. One of the most important ones is `maxiter`. .. _fruchterman-reingold: http://igraph.org/python/doc/igraph.Graph-class.html#layout_fruchterman_reingold Returns ------- Depending on `copy`, returns or updates `adata` with the following field. **X_draw_graph_layout** : `adata.obsm` Coordinates of graph layout. E.g. for layout='fa' (the default), the field is called 'X_draw_graph_fa' """ if random_seed: np.random.seed(random_seed) gt.seed_rng(random_seed) n_cells = adata.shape[0] start = logg.info(f'drawing single-cell graph using layout {layout!r}') if layout not in _LAYOUTS: raise ValueError(f'Provide a valid layout, one of {_LAYOUTS}.') adata = adata.copy() if copy else adata if adjacency is None and 'neighbors' not in adata.uns: raise ValueError('You need to run `pp.neighbors` first ' 'to compute a neighborhood graph.') if not key in adata.uns: raise ValueError( 'You need to run `nested_model` before trying to run this function ' ) if use_tree and 'state' not in adata.uns[key]: raise ValueError( 'When `use_tree` is set to `True`, a state should be saved' 'running `nested_model(adata, save_state=True)`.') if adjacency is None: adjacency = adata.uns['neighbors']['connectivities'] g = get_graph_tool_from_adjacency(adjacency) weights = g.ep['weight'] if use_tree: state = state_from_blocks(adata) g, _, _ = gt.get_hierarchy_tree(state, empty_branches=False) weights = None # actual drawing positions = np.zeros((n_cells, 2)) if layout == 'fr': positions = gt.fruchterman_reingold_layout(g, weight=weights) positions = np.array([x for x in positions][:n_cells]) elif layout == 'sfdp': positions = gt.sfdp_layout(g) positions = np.array([x for x in positions][:n_cells]) elif layout == 'arf': positions = gt.arf_layout(g) positions = np.array([x for x in positions][:n_cells]) adata.uns['draw_graph'] = {} adata.uns['draw_graph']['params'] = dict(layout=layout, random_seed=random_seed) key_added = f'X_draw_graph_{layout}' adata.obsm[key_added] = positions logg.info( ' finished', time=start, deep=('added\n' f' {key_added!r}, graph_drawing coordinates (adata.obsm)'), ) return adata if copy else None
pos_prop2 = g.new_vertex_property('vector<double>') for v in g.vertices(): i = g.vertex_index[v] pos_prop2[v] = pos[i]#[::-1] fastlayout_strided = time.clock() fastlayout_strided = fastlayout - start # draw the graph in graph_tool gt.graph_draw(g, pos=pos_prop2, output="output/fr-fastlayout-strided.pdf") # call and plot the reference implementation of graph-tool start = time.clock() reference = gt.fruchterman_reingold_layout(g, n_iter=max_it) reflayout = time.clock() reflayout = reflayout - start gt.graph_draw(g, pos=reference, output="output/fr-reference.pdf") print "Time for fastlayout is: ", fastlayout print "Time for reference implementaion in gt is: ", reflayout print "Speedup is x%.2f" % (reflayout / fastlayout) # Graph-Tool references # ===================== # * vertices and edgelist # https://graph-tool.skewed.de/static/doc/quickstart.html#iterating-over-all-vertices-or-edges # * calling a layout function # https://graph-tool.skewed.de/static/doc/draw.html#graph_tool.draw.fruchterman_reingold_layout # * draw graphs
vprop_shape[v_switch] = "hexagon" for host in switch['hosts']: v_host = g.add_vertex() e_link = g.add_edge(v_switch, v_host) vprop_text[v_host] = host['name'] vprop_color[v_host] = 100 vprop_size[v_host] = 40 vprop_shape[v_host] = "circle" name_to_vertex[host['name']] = v_host for link in network['switch_links']: v_node1 = name_to_vertex[link['node1']['name']] v_node2 = name_to_vertex[link['node2']['name']] g.add_edge(v_node1, v_node2) pos = fruchterman_reingold_layout(g, n_iter=1000) #manually assign positions pos[name_to_vertex['s1']] = [2, 0] pos[name_to_vertex['s2']] = [3, 0.5] pos[name_to_vertex['s3']] = [2, 1] pos[name_to_vertex['s4']] = [1, 1.5] pos[name_to_vertex['s5']] = [3, 1.5] pos[name_to_vertex['s6']] = [2, 2] pos[name_to_vertex['s7']] = [1, 2.5] pos[name_to_vertex['s8']] = [3, 2.5] pos[name_to_vertex['h1']] = [2, -0.7] pos[name_to_vertex['h2']] = [3.5, 0] pos[name_to_vertex['h3']] = [1.5, 0.5] pos[name_to_vertex['h4']] = [0.5, 1]
def calculate_cen_b(pre_u, type, pos_type): # if type == "DeC": if type == "BetC": v_be = pre_u.new_vertex_property("float") v_betweenness = gtc.betweenness(pre_u, vprop=v_be) pre_u.vertex_properties['betweenness'] = v_be if type == "CloC": v_clo = pre_u.new_vertex_property("float") v_closeness = gtc.closeness(pre_u, vprop=v_clo) pre_u.vertex_properties['closeness'] = v_clo # if type == "EigenveC": # if type == "KatzC": # if type == "EigentrustC": if pos_type == "FD": pos = gt.sfdp_layout(pre_u, p=2.6, K=40, C=1) if pos_type == "FR": pos = gt.fruchterman_reingold_layout(pre_u, n_iter=100, r=100, a=10) nodes = [] convert = [] for v in pre_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]) if 'v_be' in locals().keys(): s_node["BetC"] = v_be[v] if 'v_clo' in locals().keys(): if math.isnan(v_clo[v]): v_clo[v] = 0 s_node["CloC"] = v_clo[v] 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 pre_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(pre_u.edge(e.target(), e.source(), all_edges=True, add_missing=False)) + \ len(pre_u.edge(e.source(), e.target(), all_edges=True, add_missing=False)) links.append(s_edge) all_data['links'] = links return (all_data)
def make_fr_graph(self): if self.random_state is not None: np.random.seed(self.random_state) pos = self.g.new_edge_property("double") self.g.vertex_properties['pos'] = gt.fruchterman_reingold_layout( self.g)
def calculate_cen(pre_u, type, pos_type): # if type == "DeC": if type == "BetC": v_be = pre_u.new_vertex_property("float") v_betweenness = gtc.betweenness(pre_u, vprop=v_be) pre_u.vertex_properties['betweenness'] = v_be if type == "CloC": v_clo = pre_u.new_vertex_property("float") v_closeness = gtc.closeness(pre_u, vprop=v_clo) pre_u.vertex_properties['closeness'] = v_clo # if type == "EigenveC": # if type == "KatzC": # if type == "EigentrustC": if pos_type == "FD": pos = gt.sfdp_layout(pre_u, p=2.6, K=40, C=1) if pos_type == "FR": pos = gt.fruchterman_reingold_layout(pre_u, n_iter=100, r=100, a=10) nodes = [] convert = [] for v in pre_u.vertices(): s_node = {} s_node["name"] = v_userid[v]["userid"] s_node['gender'] = v_gender[v]["gender"] s_node['schregion'] = v_schregion[v]['schregion'] s_node['school'] = v_school[v]['school'] s_node['major'] = v_major[v]['major'] s_node['marriage'] = v_marriage[v]['marriage'] s_node['grade'] = v_grade[v]['grade'] s_node['liveplace'] = v_liveplace[v]['liveplace'] s_node['height'] = v_height[v]['height'] s_node['weight'] = v_weight[v]['weight'] s_node['scorelevel'] = v_scorelevel[v]['scorelevel'] s_node['birthyear'] = v_birthyear[v]['birthyear'] 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] if 'v_be' in locals().keys(): s_node["BetC"] = v_be[v] if 'v_clo' in locals().keys(): if math.isnan(v_clo[v]): v_clo[v] = 0 s_node["CloC"] = v_clo[v] # print(v_clo[v]) 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 pre_u.edges(): s_edge = {} s_edge['source'] = convert.index(str(e_source[e])) s_edge['target'] = convert.index(str(e_target[e])) s_edge['date'] = e_date[e]['date'] s_edge['dailycount'] = e_dailycount[e]['dailycount'] s_edge['weight'] = len(pre_u.edge(e.target(), e.source(), all_edges=True, add_missing=False)) + \ len(pre_u.edge(e.source(), e.target(), all_edges=True, add_missing=False)) links.append(s_edge) all_data['links'] = links return(all_data)
color_list = [[0.64, 0.16, 0.16, 1], [0.41, 0.55, 0.13, 1], [0.06, 0.3, 0.54, 1]] mode_list = ["BA", "SW", "ER"] color_index = 0 mode_index = 2 mode_select = 6 #demo_graph, node_number, edge_number = open_file_data_graph(file_name[mode_select]) demo_graph, node_number, edge_number = networkx_graph_generator( mode=mode_list[mode_index]) # create graph_tool graph original_graph = gt.Graph(directed=False) for edge in demo_graph.edges(): original_graph.add_edge(edge[0], edge[1]) pos1 = gt.fruchterman_reingold_layout(original_graph) #pos1 = gt.sfdp_layout(original_graph) gt.graph_draw(original_graph, pos=pos1, vprops={ "size": 5, "fill_color": color_list[color_index] }, eprops={ "color": [0.5, 0.5, 0.5, 0.5], "marker_size": 4 }, output_size=(400, 400), output="out.eps") gt.graph_draw(original_graph, pos=pos1,
color_index = 0 mode_index = 0 er_graph, node_number, edge_number = networkx_graph_generator( mode=mode_list[mode_index]) dat, dat_path = dats_generator(graph_topology=er_graph, mode="gaussian", dat_number=1, seed=True) dat = dat[0] dat_path = dat_path[0] # create graph_tool graph original_graph = gt.Graph(directed=False) for edge in er_graph.edges(): original_graph.add_edge(edge[0], edge[1]) pos1 = gt.fruchterman_reingold_layout(original_graph) #pos1 = gt.sfdp_layout(original_graph) gt.graph_draw(original_graph, pos=pos1, vprops={ "size": 5, "fill_color": color_list[color_index] }, eprops={ "color": [0.5, 0.5, 0.5, 0.5], "marker_size": 4 }, output_size=(400, 400), output="ER.eps") count = 0 for i in pos1:
edge_width[edge] = width_edge_dict[link] g.edge_properties["edge_color"] = edge_color g.edge_properties["edge_label"] = edge_label g.edge_properties["edge_width"] = edge_width print('global_clustering', gt.global_clustering(g)) print('assortativity out', gt.assortativity(g, "out")) # correlations print('assortativity in', gt.assortativity(g, "in")) print('assortativity total', gt.assortativity(g, "total")) g = gt.GraphView(g) print('start drawing') pos_fr = gt.fruchterman_reingold_layout(g, n_iter=1000) g.vertex_properties["pos_fr"] = pos_fr control = g.new_edge_property("vector<double>") for e in g.edges(): d = sqrt(sum((pos_fr[e.source()].a - pos_fr[e.target()].a)**2)) / 5 control[e] = [0.3, d, 0.7, d] g.edge_properties["control"] = control gt.graph_draw(g, pos=pos_fr, vertex_pen_width=0.6, vertex_color=[1, 1, 1, 1], vertex_fill_color=color_bytype, vertex_size=gt.prop_to_size(degrees, mi=10, ma=20), edge_color=edge_color, edge_control_points=control,