def most_central(self,F=1,cent_type='betweenness'): if cent_type == 'betweenness': ranking = nx.betweenness_centrality(self.G).items() elif cent_type == 'closeness': ranking = nx.closeness_centrality(self.G).items() elif cent_type == 'eigenvector': ranking = nx.eigenvector_centrality(self.G).items() elif cent_type == 'harmonic': ranking = nx.harmonic_centrality(self.G).items() elif cent_type == 'katz': ranking = nx.katz_centrality(self.G).items() elif cent_type == 'load': ranking = nx.load_centrality(self.G).items() elif cent_type == 'degree': ranking = nx.degree_centrality(self.G).items() ranks = [r for n,r in ranking] cent_dict = dict([(self.lab[n],r) for n,r in ranking]) m_centrality = sum(ranks) if len(ranks) > 0: m_centrality = m_centrality/len(ranks) #Create a graph with the nodes above the cutoff centrality- remove the low centrality nodes thresh = F*m_centrality lab = {} for k in self.lab: lab[k] = self.lab[k] g = Graph(self.adj.copy(),self.char_list) for n,r in ranking: if r < thresh: g.G.remove_node(n) del g.lab[n] return (cent_dict,thresh,g)
def find_max_degree(vertices, edges, n=2, centrality_type="degree"): max_val = 0 max_vertice_list = [] max_edges_list = [] vertice_lists, edges_lists = comb_small_networks(vertices, edges, n) for index, vertice_list in enumerate(vertice_lists): g = draw_graph(vertice_list, edges_lists[index]) if centrality_type == "degree": centrality = nx.degree_centrality(g) elif centrality_type == "eigenvector": centrality = nx.eigenvector_centrality(g) elif centrality_type == "betweenness": centrality = nx.betweenness_centrality(g) elif centrality_type == "closeness": centrality = nx.harmonic_centrality(g) if max(centrality.values()) > max_val: max_val = max(centrality.values()) max_vertice_list = vertice_list max_edges_list = edges_lists[index] return max_val, max_vertice_list
def calculate_centrality_measures(G, create_using, directed): measures = [] centrality_dict = {} #check for directed or undirected if (directed): centrality_dict['in_degree'] = nx.in_degree_centrality(G) centrality_dict['out_degree'] = nx.out_degree_centrality(G) else: centrality_dict['degree'] = nx.degree_centrality(G) #print "Completed degree" #calculate harmonic if graph is disconnected if is_connected(G, directed): centrality_dict['closeness'] = nx.closeness_centrality(G) else: centrality_dict['harmonic'] = nx.harmonic_centrality(G) #print "Completed closeness_centrality" centrality_dict['betweenness'] = nx.betweenness_centrality(G) #print "Completed betweenness" centrality_dict['eigen'] = nx.eigenvector_centrality(G) centrality_dict['pagerank'] = nx.pagerank(G) G_prime = G if directed: G_prime = nx.read_edgelist(sys.argv[1], nodetype=int) centrality_dict['clustering'] = nx.clustering(G_prime) print_tsv(centrality_dict)
yscale10 = plt.yscale('log') plt.subplot(1, 2, 2) g10a = plt.hist(centrality_total_LCC, rwidth=0.8, bins=unique_degrees_count, color='goldenrod') lx10a = plt.xlabel("Load centrality LCC") yscale10a = plt.yscale('log') plt.tight_layout() plt.savefig(join(paths['dist_dir'], 'load_centrality_plots.pdf')) # Harmonic centrality -- # Reverses the sum and reciprocal operations # in the definition of closeness centrality print("\n\nHarmonic centrality") harmonic = nx.harmonic_centrality(G) harmonic_total = list(harmonic.values()) harmonic_LCC = nx.harmonic_centrality(LCC) harmonic_total_LCC = list(harmonic_LCC.values()) print("\nHarmonic centrality total") print("Range: ", [min(harmonic_total), max(harmonic_total)]) print("Mean: ", np.average(harmonic_total)) print("Std.: ", np.sqrt(np.var(harmonic_total))) print("Median: ", np.median(harmonic_total)) print("Q1: ", np.percentile(harmonic_total, 25)) print("Q3: ", np.percentile(harmonic_total, 75)) print("\nHarmonic centrality target") print("Range: ", [min(harmonic_total_LCC), max(harmonic_total_LCC)]) print("Mean: ", np.average(harmonic_total_LCC))
def getHarmonicCent(G): harmonicCent = nx.harmonic_centrality(G) for k, v in harmonicCent.items(): harmonicCent[k] = round(v / (G.number_of_nodes() - 1), 3) return harmonicCent
"closeness_centrality": nx.closeness_centrality, "closeness_centrality_undirected": lambda G: nx.closeness_centrality(G.to_undirected()), "betweenness_centrality": nx.betweenness_centrality, "betweenness_centrality_undirected": lambda G: nx.betweenness_centrality(G.to_undirected()), "load_centrality": nx.load_centrality, "load_centrality_undirected": lambda G: nx.load_centrality(G.to_undirected()), "harmonic_centrality": nx.harmonic_centrality, "harmonic_centrality_undirected": lambda G: nx.harmonic_centrality(G.to_undirected()), "local_reaching_centrality": lambda G: {node: nx.local_reaching_centrality(G, node) for node in G.nodes}, "local_reaching_centrality_undirected": lambda G: { node: nx.local_reaching_centrality(G.to_undirected(), node) for node in G.nodes }, # "participation_coefficient": get_participation_coefficient, "participation_coefficient_undirected": get_participation_coefficient_undirected, "within_community_strength_undirected": within_community_strength_undirected }
for node in degree: asso_dict[node] = [str(degree[node])] betweenness = nx.betweenness_centrality(G) for node in betweenness: asso_dict[node].append(str(betweenness[node])) closeness = nx.closeness_centrality(G) for node in closeness: asso_dict[node].append(str(closeness[node])) load = nx.load_centrality(G) for node in load: asso_dict[node].append(str(load[node])) harmonic = nx.harmonic_centrality(G) for node in harmonic: asso_dict[node].append(str(harmonic[node])) """if(edge_num_asso == 1): eigenvector = nx.eigenvector_centrality(G) for node in eigenvector: asso_dict[node].append(str(eigenvector[node])) ktaz = nx.katz_centrality(G) for node in ktaz: asso_dict[node].append(str(ktaz[node])) else: try: eigenvector = nx.eigenvector_centrality_numpy(G)
def generate_graph_features(glycan, libr=None): """compute graph features of glycan\n | Arguments: | :- | glycan (string): glycan in IUPAC-condensed format | libr (list): library of monosaccharides; if you have one use it, otherwise a comprehensive lib will be used\n | Returns: | :- | Returns a pandas dataframe with different graph features as columns and glycan as row """ if libr is None: libr = lib g = glycan_to_nxGraph(glycan, libr=libr) #nbr of different node features: nbr_node_types = len(set(nx.get_node_attributes(g, "labels"))) #adjacency matrix: A = nx.to_numpy_matrix(g) N = A.shape[0] diameter = nx.algorithms.distance_measures.diameter(g) deg = np.array([np.sum(A[i, :]) for i in range(N)]) dens = np.sum(deg) / 2 avgDeg = np.mean(deg) varDeg = np.var(deg) maxDeg = np.max(deg) nbrDeg4 = np.sum(deg > 3) branching = np.sum(deg > 2) nbrLeaves = np.sum(deg == 1) deg_to_leaves = np.array([np.sum(A[:, deg == 1]) for i in range(N)]) max_deg_leaves = np.max(deg_to_leaves) mean_deg_leaves = np.mean(deg_to_leaves) deg_assort = nx.degree_assortativity_coefficient(g) betweeness_centr = np.array( pd.DataFrame(nx.betweenness_centrality(g), index=[0]).iloc[0, :]) betweeness = np.mean(betweeness_centr) betwVar = np.var(betweeness_centr) betwMax = np.max(betweeness_centr) betwMin = np.min(betweeness_centr) eigen = np.array( pd.DataFrame(nx.katz_centrality_numpy(g), index=[0]).iloc[0, :]) eigenMax = np.max(eigen) eigenMin = np.min(eigen) eigenAvg = np.mean(eigen) eigenVar = np.var(eigen) close = np.array( pd.DataFrame(nx.closeness_centrality(g), index=[0]).iloc[0, :]) closeMax = np.max(close) closeMin = np.min(close) closeAvg = np.mean(close) closeVar = np.var(close) flow = np.array( pd.DataFrame(nx.current_flow_betweenness_centrality(g), index=[0]).iloc[0, :]) flowMax = np.max(flow) flowMin = np.min(flow) flowAvg = np.mean(flow) flowVar = np.var(flow) flow_edge = np.array( pd.DataFrame(nx.edge_current_flow_betweenness_centrality(g), index=[0]).iloc[0, :]) flow_edgeMax = np.max(flow_edge) flow_edgeMin = np.min(flow_edge) flow_edgeAvg = np.mean(flow_edge) flow_edgeVar = np.var(flow_edge) load = np.array(pd.DataFrame(nx.load_centrality(g), index=[0]).iloc[0, :]) loadMax = np.max(load) loadMin = np.min(load) loadAvg = np.mean(load) loadVar = np.var(load) harm = np.array( pd.DataFrame(nx.harmonic_centrality(g), index=[0]).iloc[0, :]) harmMax = np.max(harm) harmMin = np.min(harm) harmAvg = np.mean(harm) harmVar = np.var(harm) secorder = np.array( pd.DataFrame(nx.second_order_centrality(g), index=[0]).iloc[0, :]) secorderMax = np.max(secorder) secorderMin = np.min(secorder) secorderAvg = np.mean(secorder) secorderVar = np.var(secorder) x = np.array([len(nx.k_corona(g, k).nodes()) for k in range(N)]) size_corona = x[x > 0][-1] k_corona = np.where(x == x[x > 0][-1])[0][-1] x = np.array([len(nx.k_core(g, k).nodes()) for k in range(N)]) size_core = x[x > 0][-1] k_core = np.where(x == x[x > 0][-1])[0][-1] M = ((A + np.diag(np.ones(N))).T / (deg + 1)).T eigval, vec = eigsh(M, 2, which='LM') egap = 1 - eigval[0] distr = np.abs(vec[:, -1]) distr = distr / sum(distr) entropyStation = np.sum(distr * np.log(distr)) features = np.array([ diameter, branching, nbrLeaves, avgDeg, varDeg, maxDeg, nbrDeg4, max_deg_leaves, mean_deg_leaves, deg_assort, betweeness, betwVar, betwMax, eigenMax, eigenMin, eigenAvg, eigenVar, closeMax, closeMin, closeAvg, closeVar, flowMax, flowAvg, flowVar, flow_edgeMax, flow_edgeMin, flow_edgeAvg, flow_edgeVar, loadMax, loadAvg, loadVar, harmMax, harmMin, harmAvg, harmVar, secorderMax, secorderMin, secorderAvg, secorderVar, size_corona, size_core, nbr_node_types, egap, entropyStation, N, dens ]) col_names = [ 'diameter', 'branching', 'nbrLeaves', 'avgDeg', 'varDeg', 'maxDeg', 'nbrDeg4', 'max_deg_leaves', 'mean_deg_leaves', 'deg_assort', 'betweeness', 'betwVar', 'betwMax', 'eigenMax', 'eigenMin', 'eigenAvg', 'eigenVar', 'closeMax', 'closeMin', 'closeAvg', 'closeVar', 'flowMax', 'flowAvg', 'flowVar', 'flow_edgeMax', 'flow_edgeMin', 'flow_edgeAvg', 'flow_edgeVar', 'loadMax', 'loadAvg', 'loadVar', 'harmMax', 'harmMin', 'harmAvg', 'harmVar', 'secorderMax', 'secorderMin', 'secorderAvg', 'secorderVar', 'size_corona', 'size_core', 'nbr_node_types', 'egap', 'entropyStation', 'N', 'dens' ] feat_dic = {col_names[k]: features[k] for k in range(len(features))} return pd.DataFrame(feat_dic, index=[glycan])
import networkx as nx import os G = nx.Graph() #Create an empty graph with no nodes and no edges. file = os.path.join("write your pathname") #Load the data file with open(file) as p: #Try to open the data file next(p) #ignore the firts row of the dataset for line in p: #iterate in the dataset s=line.split() #Break the dataset into different columns G.add_edge(s[0],s[1],weight=int(s[2])) #Add edges and weights from the dataset harmonic_centrality_dic = nx.harmonic_centrality(G) #Calculate the Hramonic Centrality of the network which will return a dictionary with open("Harmonic_Centrality_Output.txt","w") as f: #Create a text file name harmonic_Centrality_Output f.write("\t\t\t\t\t\t\t\t\t************************************\t\t\tHarmonic Centrality Output\t\t\t************************************"+"\n") #Write a header title for k,v in harmonic_centrality_dic.items(): #Iterate into the dictionary f.write(str(k)+": "+str(v)+"\n") #Write Dictionary keys and values in the file
sheet1.write(1, 0, 'Average Shortest Path') sheet1.write(1, 1, nx.average_shortest_path_length(Huw)) sheet1.write(1, 2, nx.average_shortest_path_length(Hu)) sheet1.write(1, 3, nx.average_shortest_path_length(Hud)) sheet1.write(1, 4, nx.average_shortest_path_length(H1)) sheet1.write(2, 0, 'Eccentricity') x = nx.eccentricity(Huw) sheet1.write(2, 1, np.mean(list(x.values()))) x = nx.eccentricity(Hu) sheet1.write(2, 2, np.mean(list(x.values()))) x = nx.eccentricity(Hud) sheet1.write(2, 3, np.mean(list(x.values()))) x = nx.eccentricity(H1) sheet1.write(2, 4, np.mean(list(x.values()))) sheet1.write(3, 0, 'Harmonic Centrality') x = nx.harmonic_centrality(Huw) sheet1.write(3, 1, np.mean(list(x.values()))) x = nx.harmonic_centrality(Hu) sheet1.write(3, 2, np.mean(list(x.values()))) x = nx.harmonic_centrality(Hud) sheet1.write(3, 3, np.mean(list(x.values()))) x = nx.harmonic_centrality(H1) sheet1.write(3, 4, np.mean(list(x.values()))) sheet1.write(4, 0, 'Eigenvector Centrality') x = nx.eigenvector_centrality(Huw) sheet1.write(4, 1, np.mean(list(x.values()))) x = nx.eigenvector_centrality(Hu) sheet1.write(4, 2, np.mean(list(x.values()))) x = nx.eigenvector_centrality(Hud) sheet1.write(4, 3, np.mean(list(x.values()))) x = nx.eigenvector_centrality(H1)
def getHarmonicCentrality(self): return sorted(list((nx.harmonic_centrality(self.G).values())))
my_list.append(a) print('pagerank') a = nx.pagerank(nxg).values() my_list.append(a) print('closeness') a = nx.closeness_centrality(nxg).values() my_list.append(a) print('eigenvector_centrality') a = nx.eigenvector_centrality(nxg).values() my_list.append(a) print('harmonic_centrality') a = nx.harmonic_centrality(nxg).values() my_list.append(a) print('load_centrality') a = nx.load_centrality(nxg).values() my_list.append(a) for i in my_list: print(len(i)) with open('centralidades-' + sys.argv[1].split('.')[0] + '-picke.txt', 'wb') as f: pickle.dump(my_list, f) with open('sementes.txt', 'w') as f: for s in my_list: f.write(str(s))
def calc_centralities(G,org_name,string_version): print("Calculating centralities") centrality_measures = {} string_location=f.string_version_data(string_version)[0] print(string_location) # if 1==0: if os.path.isfile('centrality_data/%s/%s.cent'%(string_location,org_name)): print("Using cached centrality data") file=open('centrality_data/%s/%s.cent'%(string_location,org_name)) lines=file.readlines() centrality_list=lines.pop(0).strip().split(' ') centrality_list.pop(0) for i,centrality in enumerate(centrality_list): centrality_measures[centrality]={} for line in lines: value_list=line.split(' ') for i,centrality in enumerate(centrality_list): # print("%d. %s" % (i+1,centrality)) centrality_measures[centrality][value_list[0]]=float(value_list[i+1]) else: print("1. Degree centrality") centrality_measures['Degree_Centrality']=nx.degree_centrality(G) print("2. Closeness centrality") centrality_measures['Closeness_Centrality']=Counter(nx.algorithms.centrality.closeness_centrality(G)) print("3. Betweenness centrality") centrality_measures['Betweenness_Centrality']=Counter(nx.algorithms.centrality.betweenness_centrality(G)) print("4. Clustering coefficient") centrality_measures['Clustering_Co-efficient']=Counter(nx.clustering(G)) print("5. Eigenvector centrality") centrality_measures['Eigenvector_Centrality']= nx.eigenvector_centrality(G) print("6. Subgraph centrality") centrality_measures["Subgraph_Centrality"]=nx.subgraph_centrality(G) print("7. Information centrality") centrality_measures["Information_Centrality"]=nx.current_flow_closeness_centrality(f.trim_graph(G)) print("8. Clique Number") cliq={} for i in G.nodes(): cliq[i]=nx.node_clique_number(G,i) centrality_measures["Clique_Number"]=cliq print("9. Edge clustering coefficient") edge_clus_coeff={} for n in G.nodes: edge_clus_coeff[n]=0 for e in G.edges(n): num=len(list(nx.common_neighbors(G,e[0],e[1]))) den=(min(G.degree(e[0]),G.degree(e[1]))-1) if den==0: den=1 edge_clus_coeff[n]+=num/den centrality_measures['Edge_Clustering_Coefficient']=edge_clus_coeff print("10. Page Rank") centrality_measures['Page_Rank']=nx.pagerank(G) print("11. Random Walk Betweenness Centrality") centrality_measures["Random_Walk_Betweenness_Centrality"]=nx.current_flow_betweenness_centrality(f.trim_graph(G)) print("12. Load Centrality") centrality_measures["Load_Centrality"]=nx.load_centrality(G) print("13. Communicability Betweenness") centrality_measures["Communicability_Betweenness"]=nx.communicability_betweenness_centrality(f.trim_graph(G)) print("14. Harmonic Centrality") centrality_measures["Harmonic_Centrality"]=nx.harmonic_centrality(G) print("15. Reaching Centrality") reach_cent={} for node in G.nodes: reach_cent[node] = nx.local_reaching_centrality(G,node) centrality_measures["Reaching_Centrality"]=reach_cent print("16. Katz Centrality(not calculated)") # centrality_measures["Katz_Centrality"]=nx.katz_centrality(G) datafile=open("refex_props/%s.refex" % (org_name)) sample_line=datafile.readline() s= sample_line.strip().split(' ') for x in range(1,len(s)): centrality_measures["refex#%d" % (x)]={} for line in datafile: props=line.strip().split(" ") props=[i.strip('\t') for i in props] for x in range(1,len(s)): centrality_measures["refex#%d" % (x)][props[0]]=float(props[x]) datafile=open("refex_rider_props/%s.riderproperties" % (org_name)) sample_line=datafile.readline() s= sample_line.strip().split(' ') s.pop(1) print(len(s)) for x in range(1,len(s)): centrality_measures["refex_rider#%d" % (x)]={} for line in datafile: props=line.strip().split(" ") props.pop(1) for x in range(1,len(props)): centrality_measures["refex_rider#%d" % (x)][props[0]]=float(props[x]) with open('centrality_data/%s/%s.cent'%(string_location,org_name),'w') as file: file.write(str(org_name)+' ') centrality_list=list(centrality_measures) for x in centrality_list: file.write(str(x)+' ') for node in G.nodes: file.write('\n'+node+' ') for x in centrality_list: if node not in centrality_measures[x]: file.write('-1 ') else: file.write(str(centrality_measures[x][node])+' ') return centrality_measures
def get_centralities(graphs_in_null_model, G, adj, level): nodes = len(G.nodes()) #Get centralities in real graph init_di = get_dynamical_importances(adj) init_h = nx.harmonic_centrality(G) init_h = dict(map(lambda kv: (kv[0], kv[1] / (nodes - 1)), init_h.items())) init_b = nx.betweenness_centrality(G) init_d = {} #convert degree view into dict for x, y in G.degree(): init_d[x] = y gs = [] harmonic_centralities = {} between_centralities = {} dynamical_importance = {} #Generate null model centralities for i in range(0, graphs_in_null_model): if i % 50 == 0: print(i) #get centralities h = nx.harmonic_centrality(G) h = dict(map(lambda kv: (kv[0], kv[1] / (nodes - 1)), h.items())) b = nx.betweenness_centrality(G) d = get_dynamical_importances(nx.to_numpy_matrix(G)) #store centralities for k, v in h.items(): harmonic_centralities.setdefault(k, []).append(v) for k, v in b.items(): between_centralities.setdefault(k, []).append(v) for k, v in b.items(): dynamical_importance.setdefault(k, []).append(v) #update graph edges = list(G.edges()) edge1 = random.choice(edges) edge2 = random.choice(edges) while (not diff_edges(G, edge1, edge2)): edge2 = random.choice(edges) k = update_edges(G, edge1, edge2) gs.append(k) G = k h_ps = get_percentiles(init_h, harmonic_centralities) b_ps = get_percentiles(init_b, between_centralities) di_ps = get_percentiles(init_di, dynamical_importance) # sorted_h = sorted(init_h.items(), key=operator.itemgetter(1), reverse=True) sorted_b = sorted(init_b.items(), key=operator.itemgetter(1), reverse=True) sorted_di = sorted(init_di.items(), key=operator.itemgetter(1), reverse=True) sorted_d = sorted(init_d.items(), key=operator.itemgetter(1), reverse=True) plt.figure(1) plt.subplot(311) plot(di_ps, 'Dynamical Importance', sorted_di, level) plt.tick_params( axis='x', # changes apply to the x-axis which='both', # both major and minor ticks are affected bottom=False, # ticks along the bottom edge are off top=False, # ticks along the top edge are off labelbottom=False) # labels along the bottom edge are off plt.subplot(312) plot(h_ps, 'Harmonic Centrality', sorted_h, level) plt.tick_params( axis='x', # changes apply to the x-axis which='both', # both major and minor ticks are affected bottom=False, # ticks along the bottom edge are off top=False, # ticks along the top edge are off labelbottom=False) # labels along the bottom edge are off plt.subplot(313) plot(b_ps, 'Betweenness Centrality', sorted_b, level) plt.xlabel('Node Number') plt.show()
def new_centrality(graph): """ Compute centrality scores for a network graph. Compute a number of different centrality and misc. scores for all nodes in a network graph. Parameters ---------- graph : networkX graph object Returns ------- core_df : Pandas DataFrame object """ core_df = pd.DataFrame() core_df['artist'] = graph.nodes( ) # Add to the artist column all nodes (artists) in a graph. scores_list = [] try: deg_cent = pd.DataFrame.from_dict(nx.degree_centrality(graph), orient='index', columns=['deg_cent']) scores_list.append(deg_cent) except: pass try: load_cent = pd.DataFrame.from_dict(nx.load_centrality(graph), orient='index', columns=['load_cent']) scores_list.append(load_cent) #between_cent = nx.betweenness_centrality(graph) except: pass try: page_rank = pd.DataFrame.from_dict(nx.pagerank_numpy(graph), orient='index', columns=['page_rank']) scores_list.append(page_rank) except: pass try: ev_cent = pd.DataFrame.from_dict( nx.eigenvector_centrality_numpy(graph), orient='index', columns=['ev_cent']) scores_list.append(ev_cent) except: pass try: cl_cent = pd.DataFrame.from_dict(nx.closeness_centrality(graph), orient='index', columns=['close_cent']) scores_list.append(cl_cent) except: pass try: cfcc = pd.DataFrame.from_dict( nx.current_flow_closeness_centrality(graph), orient='index', columns=['cf_close_cent']) scores_list.append(cfcc) except: pass """ try: ic = pd.DataFrame.from_dict(nx.information_centrality(graph), orient = 'index', columns = ['info_cent']) scores_list.append(ic) except: pass #ebc = pd.DataFrame.from_dict(nx.edge_betweenness_centrality(graph), orient = 'index', columns = ['edge_bet_cent']) try: cfbc = pd.DataFrame.from_dict(nx.current_flow_betweenness_centrality(graph), orient = 'index', columns = ['edge_cflow_cent']) scores_list.append(cfbc) except: pass #ecfbc = pd.DataFrame.from_dict(nx.edge_current_flow_betweenness_centrality(graph), orient = 'index', columns = ['cf_between_cent']) try: acfbc = pd.DataFrame.from_dict(nx.approximate_current_flow_betweenness_centrality(graph), orient = 'index', columns = ['appx.cfbt_cent']) scores_list.append(acfbc) except: pass #elc = pd.DataFrame.from_dict(nx.edge_load_centrality(graph), orient = 'index', columns = ['edge_load_cent']) """ try: hc = pd.DataFrame.from_dict(nx.harmonic_centrality(graph), orient='index', columns=['harm_cent']) scores_list.append(hc) except: pass #d = pd.DataFrame.from_dict(nx.dispersion(graph), orient = 'index', columns = ['dispersion']) """ try: soc = pd.DataFrame.from_dict(nx.second_order_centrality(graph), orient = 'index', columns = ['sec_ord_cent']) scores_list.append(soc) except: pass """ df = pd.concat(scores_list, axis=1) core_df = core_df.merge(df, left_on='artist', right_index=True) core_df['mean_cent'] = core_df.apply( lambda row: np.mean(row[1:]), # Calculate the mean of the row axis=1) return core_df
def inverse_harmonic_centrality(graph): centrality = nx.harmonic_centrality(graph) return {node: 1 / (val + 0.000001) for node, val in centrality.items()}
def harmonic_centrality(graph): return nx.harmonic_centrality(graph)
def harmonic(G, nodes): d = nx.harmonic_centrality(G, nbunch=nodes) return (d)
def compute_subgraph_center(self, subgraph): if self.method == 'betweenness_centrality': d = nx.betweenness_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'betweenness_centrality_subset': d = nx.betweenness_centrality_subset(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'information_centrality': d = nx.information_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'local_reaching_centrality': d = {} for n in self.G.nodes(): d[n] = nx.local_reaching_centrality(self.G, n, weight='weight') center = max(d, key=d.get) elif self.method == 'voterank': d = nx.voterank(subgraph) center = max(d, key=d.get) elif self.method == 'percolation_centrality': d = nx.percolation_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'subgraph_centrality': d = nx.subgraph_centrality(subgraph) center = max(d, key=d.get) elif self.method == 'subgraph_centrality_exp': d = nx.subgraph_centrality_exp(subgraph) center = max(d, key=d.get) elif self.method == 'estrada_index': d = nx.estrada_index(subgraph) center = max(d, key=d.get) elif self.method == 'second_order_centrality': d = nx.second_order_centrality(subgraph) center = max(d, key=d.get) elif self.method == 'eigenvector_centrality': d = nx.eigenvector_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'load_centrality': d = nx.load_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'closeness_centrality': d = nx.closeness_centrality(subgraph) center = max(d, key=d.get) elif self.method == 'current_flow_closeness_centrality': d = nx.current_flow_closeness_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'current_flow_betweenness_centrality': d = nx.current_flow_betweenness_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'current_flow_betweenness_centrality_subset': d = nx.current_flow_betweenness_centrality_subset(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'approximate_current_flow_betweenness_centrality': d = nx.approximate_current_flow_betweenness_centrality( subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'harmonic_centrality': d = nx.harmonic_centrality(subgraph) center = max(d, key=d.get) elif self.method == 'page_rank': d = nx.pagerank(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'hits': d = nx.hits(subgraph) center = max(d, key=d.get) elif self.method == 'katz_centrality': d = nx.katz_centrality(subgraph, weight='weight') center = max(d, key=d.get) else: new_centers = nx.center(subgraph) # new_centers gives a list of centers and here we just pick one randomly --not good for stability # to do : find a better way to choose the center--make it stable index = random.randint(0, len(new_centers) - 1) center = new_centers[index] return center
def average_harmonic_centrality(self): harmonic = nx.harmonic_centrality(self.graph) harmonic = np.fromiter(harmonic.values(), dtype=float) return np.mean(harmonic)
def compute_metrics(self): ''' Compute some network metrics ''' print(' Metrics...') print(' STRENGTH') A = self.g_ig.get_adjacency() node_str = np.zeros(self.N) for i in range(self.N): node_str[i] = np.sum(A[i, :]) self.export_data(self.g_ig, 'strength', node_str, 'igraph') # Metrics from igraph print(' DEGREE') degrees = self.g_ig.degree() self.export_data(self.g_ig, 'degree', degrees, 'igraph') print(' BETWEENNESS') betweenness = self.g_ig.betweenness(vertices=None, directed=False, cutoff=None) #betweenness = g_ig.betweenness(vertices=None, directed=False, cutoff=None, weights='weight') self.export_data(self.g_ig, 'betweenness', betweenness, 'igraph') print(' AUTHORITY') authority = self.g_ig.authority_score(weights=None, scale=True, return_eigenvalue=False) self.export_data(self.g_ig, 'authority', authority, 'igraph') print(' CLUSTERING COEFF.') cluster_coeff = self.g_ig.transitivity_local_undirected(vertices=None, mode="zero", weights=None) #cluster_coeff = g_ig.transitivity_local_undirected(vertices=None, mode="zero", weights='weight') self.export_data(self.g_ig, 'clusterCoeff', cluster_coeff, 'igraph') # Metrics from Networkx print(' CLOSENESS') closeness = nx.closeness_centrality(self.g_nx, u=None, distance=None, wf_improved=True) self.export_data(self.g_ig, 'closeness', closeness, 'networkx') print(' EIGENVECTOR CENTRALITY') eig_centrality = nx.eigenvector_centrality(self.g_nx, max_iter=1000, tol=1e-06, nstart=None, weight=None) self.export_data(self.g_ig, 'eig_centrality', eig_centrality, 'networkx') print(' HARMONIC CENTRALITY') harmonic_centrality = nx.harmonic_centrality(self.g_nx, nbunch=None, distance=None) self.export_data(self.g_ig, 'harmonic_centrality', harmonic_centrality, 'networkx') print(' PAGERANK') pagerank = nx.pagerank(self.g_nx, alpha=0.85, personalization=None, max_iter=100, tol=1e-06, nstart=None, weight='weight', dangling=None) self.export_data(self.g_ig, 'pagerank', pagerank, 'networkx')
def makeJSON(self): global info info = "" info += "{\n \"info\": {\n\"nodes\": [\n" i = 0 num = len(s_partition) sorted_betweeness = [] sorted_degree = [] sorted_eigenvector = [] sorted_closeness = [] sorted_harmonic = [] sorted_communicability = [] sorted_core = [] sorted_degree1 = [] sorted_partition = s_partition unadjusted_betweeness = [] unadjusted_degree = [] unadjusted_eigenvctor = [] unadjusted_closeness = [] unadjusted_harmonic = [] unadjusted_communicability = [] G.remove_edges_from(G.selfloop_edges()) for key, value in nx.betweenness_centrality(G).items(): value1 = 1 + (value * 100) temp1 = [key, value] temp = [key, value1] sorted_betweeness.append(temp1) unadjusted_betweeness.append(temp1) sorted_partition = sorted(sorted_partition) sorted_betweeness = sorted(sorted_betweeness) unadjusted_betweeness = sorted(unadjusted_betweeness) for key, value in nx.degree_centrality(G).items(): value1 = 1 + (value * 100) temp1 = [key, value] temp = [key, value1] sorted_degree.append(temp1) unadjusted_degree.append(temp1) sorted_degree = sorted(sorted_degree) unadjusted_degree = sorted(unadjusted_degree) for key, value in nx.eigenvector_centrality(G).items(): value1 = value * 1000 temp1 = [key, value] temp = [key, value1] sorted_eigenvector.append(temp1) unadjusted_eigenvctor.append(temp1) sorted_eigenvector = sorted(sorted_eigenvector) unadjusted_eigenvector = sorted(unadjusted_eigenvctor) for key, value in nx.closeness_centrality(G).items(): value1 = (value * 10) temp1 = [key, value] temp = [key, value1] sorted_eigenvector.append(temp1) unadjusted_eigenvctor.append(temp1) sorted_eigenvector = sorted(sorted_eigenvector) for key, value in nx.harmonic_centrality(G).items(): temp1 = [key, value] sorted_harmonic.append(temp1) unadjusted_eigenvctor.append(temp1) sorted_harmonic = sorted(sorted_harmonic) unadjusted_harmonic = sorted(unadjusted_eigenvctor) for key, value in nx.communicability_centrality(G).items(): temp1 = [key, value] sorted_communicability.append(temp1) unadjusted_eigenvctor.append(temp1) sorted_communicability = sorted(sorted_communicability) unadjusted_communicability = sorted(unadjusted_eigenvctor) for key, value in nx.core_number(G).items(): #list temp = [key, value] sorted_core.append(temp) sorted_core = sorted(sorted_core) for key, value in nx.degree(G).items(): #list temp = [key, value] sorted_degree1.append(temp) sorted_degree1 = sorted(sorted_degree1) central_dict = {} unadjusted_dict = {} global importance importance = {} for key, value in sorted_betweeness: central_dict[key] = [] importance[key] = 0 central_dict[key].append(value) for key, value in unadjusted_betweeness: unadjusted_dict[key] = [] unadjusted_dict[key].append(value) for key, value in sorted_degree: central_dict[key].append(value) importance[key] += value for key, value in unadjusted_degree: unadjusted_dict[key].append(value) for key, value in sorted_eigenvector: central_dict[key].append(value) importance[key] + value for key, value in unadjusted_eigenvector: unadjusted_dict[key].append(value) for key, value in sorted_closeness: central_dict[key].append(value) importance[key] += value for key, value in unadjusted_closeness: unadjusted_dict[key].append(value) for key, value in sorted_harmonic: central_dict[key].append(value) importance[key] += value for key, value in unadjusted_harmonic: unadjusted_dict[key].append(value) for key, value in sorted_communicability: central_dict[key].append(value) importance[key] += value for key, value in unadjusted_communicability: unadjusted_dict[key].append(value) for key, value in sorted_core: importance[key] += value for key, value in sorted_degree1: importance[key] += value averages = {} acc = {} totals = {} groups = [] for key, value in sorted_partition: val1 = booleans[key] if val1 not in groups: groups.append(val1) acc[val1] = 0 totals[val1] = 0 for key, value in importance.items(): val1 = booleans[key] for item in groups: if (val1 == item): acc[item] += value totals[item] += 1 for key, value in acc.items(): averages[key] = acc[key] / totals[key] for key, value in sorted_partition: i += 1 val1 = booleans[key] info += "{\"id\": \"" + str(key) + "\", \"group\": " + str( value) + ", \"question\": \"" + str(val1) + "\", " val = unadjusted_dict[key] info += "\"Centrality\": { \"Betweeness\": " + str( val[0]) + ", \"Degree\": " + str( val[1]) + ", \"Eigenvector\": " + str( val[2]) + ", \"Closeness\": " + str( val[3]) + ", \"Harmonic\": " + str( val[4]) + ", \"Communicability\": " + str( val[5]) + " } " if num == i: info += "} \n" else: info += "}, \n" info += "],\n \"links\":[\n" num = len(network) i = 0 partition_dict = dict(sorted_partition) for key, value in sorted(network.items()): i += 1 term = str(key).split(",") group = partition_dict[term[0]] if num == i: info += "{\"source\": \"" + term[ 0] + "\", \"target\": \"" + term[ 1] + "\", \"value\": \"" + str( value) + "\", \"group\": " + str(group) + "}\n" else: info += "{\"source\": \"" + term[ 0] + "\", \"target\": \"" + term[ 1] + "\", \"value\": \"" + str( value) + "\", \"group\": " + str(group) + "},\n" info += "],\n \"Unadjusted_centrality\":[\n" i = 0 num = len(unadjusted_dict) for key, value in unadjusted_dict.items(): i += 1 val = value info += "{\"id\": \"" + str( key) + "\",\"Centrality\": { \"Betweeness\": " + str( val[0]) + ", \"Degree\": " + str( val[1]) + ", \"Eigenvector\": " + str( val[2]) + ", \"Closeness\": " + str( val[3]) + ", \"Harmonic\": " + str( val[4]) + " } " if num == i: info += "} \n" else: info += "}, \n" num = len(importance) i = 0 info += "],\n \"importance\":[\n" for key, value in importance.items(): i += 1 val1 = booleans[key] info += "{\"id\": \"" + str(key) + "\", \"importance\": " + str( value) + ", \"question\": \"" + str(val1) + "\"" if num == i: info += "} \n" else: info += "}, \n" num = len(acc) i = 0 info += "],\n \"group_stats\":[\n" for key, value in acc.items(): i += 1 val2 = averages[key] val3 = totals[key] info += "{\"group\": \"" + str( key) + "\", \"total_importance\": " + str( value) + ", \"average_importance\": " + str( val2) + ", \"total_mentions\": " + str(val3) if num == i: info += "} \n" else: info += "}, \n" num = len(pageRank) i = 0 info += "],\n \"pagerank\":[\n" for key, value in pageRank.items(): i += 1 info += "{\"id\": \"" + str(key) + "\", \"pagerank\": " + str( value) if num == i: info += "} \n" else: info += "}, \n" num = len(booleans) i = 0 question = headers[2] info += "],\n \"question\":[\n" for key, value in booleans.items(): i += 1 info += "{\"id\": \"" + str(key) + "\", \"question\": \"" + str( value) + "\"" if num == i: info += "} \n" else: info += "}, \n" info += "]\n},"
def mergegraph(graphs, pos_old, labels_old, edge_prob=0.3, edge_num=0.4): nodes = [] edges = [] pos = {} node_cnt = 0 val = 0.9 shift_value = [[-val, val], [val, val], [-val, -val], [val, -val]] comm_lables = [] for i, g in enumerate(graphs): tmp_nodes = list(g.nodes()) tmp_edges = list(g.edges()) comm_lables += [i] * len(tmp_nodes) node_map = {k: node_cnt + i for k, i in enumerate(tmp_nodes)} node_cnt += len(tmp_nodes) new_nodes = [node_map[n] for n in tmp_nodes] new_edges = [(node_map[u], node_map[v]) for u, v in tmp_edges] for k, v in pos_old[i].items(): pos_old[i][k][0] += shift_value[i][0] pos_old[i][k][1] += shift_value[i][1] new_pos = {node_map[n]: v for n, v in pos_old[i].items()} nodes += new_nodes edges += new_edges pos.update(new_pos) G = nx.DiGraph() G.add_edges_from(edges) random.shuffle(nodes) l = int(edge_num * len(nodes)) u = nodes[0:l] random.shuffle(nodes) v = nodes[0:l] for s, t in zip(u, v): if random.random() < edge_prob: G.add_edge(s, t) G.add_edge(t, s) nodes_deg = [G.degree[i] for i in G.nodes()] centrality = nx.closeness_centrality(G) labels_central = get_labels(centrality) print('centrality done!') inf_cent = nx.information_centrality(G.to_undirected()) labels_inf_central = get_labels(inf_cent) print('info centrality done!') betweenness = nx.betweenness_centrality(G.to_undirected()) labels_betweenness = get_labels(betweenness) print('betweenness done!') loads = nx.load_centrality(G.to_undirected()) labels_load = get_labels(loads) print('load centrality done!') cmm_bet = nx.communicability_betweenness_centrality(G.to_undirected()) labels_cmm_bet = get_labels(cmm_bet) print('commu betweenness done!') sce = nx.subgraph_centrality_exp(G.to_undirected()) labels_sce = get_labels(sce) print('subgraph centrality done!') harm = nx.harmonic_centrality(G.to_undirected()) labels_harm = get_labels(harm) print('harmonic done!') lrc = { v: nx.local_reaching_centrality(G.to_undirected(), v) for v in G.nodes() } labels_lrc = get_labels(lrc) print('lrc done!') unq_lbl = np.unique(nodes_deg) lbl_map = {unq_lbl[i]: i for i in range(len(unq_lbl))} labels = [lbl_map[k] for k in nodes_deg] return G, pos, labels, comm_lables, labels_central, labels_inf_central, labels_betweenness, labels_load, labels_cmm_bet, labels_sce, labels_harm, labels_lrc
close_cent_1 = nx.closeness_centrality(graph1) bet_cent_1 = nx.betweenness_centrality(graph1) sorted_deg_cent_1 = deg_cent(graph1) sorted_harm_cent_1 = harm_cent(graph1) sorted_close_cent_1 = sorted(close_cent_1.items(), key=op.itemgetter(1), reverse=True) sorted_bet_cent_1 = sorted(bet_cent_1.items(), key=op.itemgetter(1), reverse=True) # Centralities for the weighted network close_cent_2 = nx.closeness_centrality(graph2, distance='cost') bet_cent_2 = nx.betweenness_centrality(graph2, weight='cost') sorted_deg_cent_2 = deg_cent(graph2) #sorted_harm_cent_2 = harm_cent(graph2) harm_cent_2 = nx.harmonic_centrality(graph2, distance='cost') sorted_harm_cent_2 = sorted(harm_cent_2.items(), key=op.itemgetter(1)) sorted_close_cent_2 = sorted(close_cent_2.items(), key=op.itemgetter(1), reverse=True) sorted_bet_cent_2 = sorted(bet_cent_2.items(), key=op.itemgetter(1), reverse=True)
def test_local_node_centrality(primal_graph): """ Also tested indirectly via test_networks.test_compute_centrality Test centrality methods where possible against NetworkX - i.e. harmonic closeness and betweenness Note that NetworkX improved closeness is not the same as derivation used in this package NetworkX doesn't have a maximum distance cutoff, so run on the whole graph (low beta / high distance) """ # generate node and edge maps node_uids, node_data, edge_data, node_edge_map = graphs.graph_maps_from_nX( primal_graph) G_round_trip = graphs.nX_from_graph_maps(node_uids, node_data, edge_data, node_edge_map) # needs a large enough beta so that distance thresholds aren't encountered betas = np.array([0.02, 0.01, 0.005, 0.0008, 0.0]) distances = networks.distance_from_beta(betas) # set the keys - add shuffling to be sure various orders work measure_keys = [ 'node_density', 'node_farness', 'node_cycles', 'node_harmonic', 'node_beta', 'node_betweenness', 'node_betweenness_beta' ] np.random.shuffle(measure_keys) # in place measure_keys = tuple(measure_keys) # generate the measures measures_data = centrality.local_node_centrality(node_data, edge_data, node_edge_map, distances, betas, measure_keys) node_density = measures_data[measure_keys.index('node_density')] node_farness = measures_data[measure_keys.index('node_farness')] node_cycles = measures_data[measure_keys.index('node_cycles')] node_harmonic = measures_data[measure_keys.index('node_harmonic')] node_beta = measures_data[measure_keys.index('node_beta')] node_betweenness = measures_data[measure_keys.index('node_betweenness')] node_betweenness_beta = measures_data[measure_keys.index( 'node_betweenness_beta')] # improved closeness is derived after the fact improved_closness = node_density / node_farness / node_density # test node density # node density count doesn't include self-node # connected component == 49 == len(G) - 1 # isolated looping component == 3 # isolated edge == 1 # isolated node == 0 for n in node_density[4]: # infinite distance - exceeds cutoff clashes assert n in [49, 3, 1, 0] # test harmonic closeness vs NetworkX nx_harm_cl = nx.harmonic_centrality(G_round_trip, distance='length') nx_harm_cl = np.array([v for v in nx_harm_cl.values()]) assert np.allclose(nx_harm_cl, node_harmonic[4], atol=0.001, rtol=0) # test betweenness vs NetworkX # set endpoint counting to false and do not normalise # nx node centrality NOT implemented for MultiGraph G_non_multi = nx.Graph() # don't change to MultiGraph!!! G_non_multi.add_nodes_from(G_round_trip.nodes()) for s, e, k, d in G_round_trip.edges(keys=True, data=True): assert k == 0 G_non_multi.add_edge(s, e, **d) nx_betw = nx.betweenness_centrality(G_non_multi, weight='length', endpoints=False, normalized=False) nx_betw = np.array([v for v in nx_betw.values()]) # nx betweenness gives 0.5 instead of 1 for all disconnected looping component nodes # nx presumably takes equidistant routes into account, in which case only the fraction is aggregated assert np.allclose(nx_betw[:52], node_betweenness[4][:52], atol=0.001, rtol=0) # do the comparisons array-wise so that betweenness can be aggregated d_n = len(distances) betw = np.full((d_n, primal_graph.number_of_nodes()), 0.0) betw_wt = np.full((d_n, primal_graph.number_of_nodes()), 0.0) dens = np.full((d_n, primal_graph.number_of_nodes()), 0.0) far_short_dist = np.full((d_n, primal_graph.number_of_nodes()), 0.0) far_simpl_dist = np.full((d_n, primal_graph.number_of_nodes()), 0.0) harmonic_cl = np.full((d_n, primal_graph.number_of_nodes()), 0.0) grav = np.full((d_n, primal_graph.number_of_nodes()), 0.0) cyc = np.full((d_n, primal_graph.number_of_nodes()), 0.0) for src_idx in range(len(primal_graph)): # get shortest path maps tree_map, tree_edges = centrality.shortest_path_tree(edge_data, node_edge_map, src_idx, max(distances), angular=False) tree_nodes = np.where(tree_map[:, 0])[0] tree_preds = tree_map[:, 1] tree_short_dist = tree_map[:, 2] tree_simpl_dist = tree_map[:, 3] tree_cycles = tree_map[:, 4] for to_idx in tree_nodes: # skip self nodes if to_idx == src_idx: continue # get shortest / simplest distances to_short_dist = tree_short_dist[to_idx] to_simpl_dist = tree_simpl_dist[to_idx] cycles = tree_cycles[to_idx] # continue if exceeds max if np.isinf(to_short_dist): continue for d_idx in range(len(distances)): dist_cutoff = distances[d_idx] beta = betas[d_idx] if to_short_dist <= dist_cutoff: # don't exceed threshold # if to_dist <= dist_cutoff: # aggregate values dens[d_idx][src_idx] += 1 far_short_dist[d_idx][src_idx] += to_short_dist far_simpl_dist[d_idx][src_idx] += to_simpl_dist harmonic_cl[d_idx][src_idx] += 1 / to_short_dist grav[d_idx][src_idx] += np.exp(-beta * to_short_dist) # cycles cyc[d_idx][src_idx] += cycles # only process betweenness in one direction if to_idx < src_idx: continue # betweenness - only counting truly between vertices, not starting and ending verts inter_idx = tree_preds[to_idx] # isolated nodes will have no predecessors if np.isnan(inter_idx): continue inter_idx = np.int(inter_idx) while True: # break out of while loop if the intermediary has reached the source node if inter_idx == src_idx: break betw[d_idx][inter_idx] += 1 betw_wt[d_idx][inter_idx] += np.exp(-beta * to_short_dist) # follow inter_idx = np.int(tree_preds[inter_idx]) improved_cl = dens / far_short_dist / dens assert np.allclose(node_density, dens, atol=0.001, rtol=0) assert np.allclose(node_farness, far_short_dist, atol=0.01, rtol=0) # relax precision assert np.allclose(node_cycles, cyc, atol=0.001, rtol=0) assert np.allclose(node_harmonic, harmonic_cl, atol=0.001, rtol=0) assert np.allclose(node_beta, grav, atol=0.001, rtol=0) assert np.allclose(improved_closness, improved_cl, equal_nan=True, atol=0.001, rtol=0) assert np.allclose(node_betweenness, betw, atol=0.001, rtol=0) assert np.allclose(node_betweenness_beta, betw_wt, atol=0.001, rtol=0) # catch typos with pytest.raises(ValueError): centrality.local_node_centrality(node_data, edge_data, node_edge_map, distances, betas, ('typo_key', ))
import networkx as nx for i in [0]: #xrange( 10426, 0, -1 ): print "Loading graph" adjlist = str(i) + "-subgraph.adjlist" G = nx.read_adjlist("../subgraphs/" + adjlist) print "Computing harmonic centrality" hc = nx.harmonic_centrality(G) print "Writing to file" # write dictionary to file import json with open('../centrality/' + adjlist + '-hc.json', 'w') as fp: json.dump(hc, fp, sort_keys=True, indent=4)
nx.draw_networkx_edges(g, pos, edgelist=weighted_edges, width=width) #Plot the graph plt.axis('off') plt.title('Top Character Social Network in Game of Thrones Universe') # plt.savefig("GoT_social_nx.png") plt.show() g_distance_dict = {(e1, e2): 1 / (weight + 0.1) for e1, e2, weight in g.edges(data='weight')} nx.set_edge_attributes(g, g_distance_dict, 'distance') # declare node info dataframe node_info_cols = [ 'degree', 'close_central', 'eigen_central', 'harmonic_central', 'eccentricity' ] node_info_df = pd.DataFrame(columns=node_info_cols, index=top_characters) # save node information to dataframe for name in top_characters: node_info_df['degree'][name] = g.degree(weight='weight')[name] node_info_df['close_central'][name] = nx.closeness_centrality( g, distance='distance')[name] node_info_df['eigen_central'][name] = nx.eigenvector_centrality( g, weight='weight')[name] node_info_df['harmonic_central'][name] = nx.harmonic_centrality(g)[name] node_info_df['eccentricity'][name] = nx.eccentricity(g)[name] node_info_df.to_csv('node_info_all_alt.csv')
def graph_format(self, startday, endday, usergroups, readpath, writefile): index = 0 for h in usergroups: self.handledicts.append(dict()) for d in h: self.handledicts[index][d[0]] = 1 index += 1 for x in range(startday, endday): print(x) f = open(readpath + "/" + str(x) + ".json", "r") data = json.load(f) for d in data: self._add_node(d) for key in self.nodesdict: self.tweet_graph["nodes"].append(self.nodesdict[key].json_print()) for key in self.linksdict: self.tweet_graph["links"].append(self.linksdict[key]) hasharr = [] for key in self.hashtag_count: hasharr.append({"h": key, "score": self.hashtag_count[key]}) hasharr = sorted(hasharr, key=lambda hashobj: hashobj["score"], reverse=True) print(hasharr) graph_metrics = networkx.node_link_graph(self.tweet_graph) degree_metrics = networkx.degree_centrality(graph_metrics) harmonic_metrics = networkx.harmonic_centrality(graph_metrics, distance="value") try: node_metrics = networkx.betweenness_centrality(graph_metrics, weight="value") except: node_metrics = None for key in degree_metrics: for n in self.tweet_graph["nodes"]: if n["id"] == key: n["graph_score"] = { "handle": n["name"], "betweenness": node_metrics[key] if node_metrics != None else "N/A", "degree": degree_metrics[key], "harmonic": harmonic_metrics[key] } wf = open(writefile, "w") self.tweet_graph["terms"] = hasharr json.dump(self.tweet_graph, wf, indent=4) return 1
import matplotlib.pyplot as plt pos = nx.spring_layout(g) node_color = [20000.0 * g.degree(v) for v in g] node_size = [v * 100 for v in degree_dict.values()] plt.figure(figsize=(20, 20)) nx.draw_networkx(g, pos=pos, with_labels=True, node_color=node_color, node_size=node_size) plt.axis('off') # In[38]: harmonic_dict = nx.harmonic_centrality(g) nx.set_node_attributes(g, harmonic_dict, 'harmonic') pos = nx.spring_layout(g) node_color = [2000 * harmonic_dict[v] for v in g] node_size = [v * 200 for v in harmonic_dict.values()] plt.figure(figsize=(10, 20)) nx.draw_networkx(g, pos=pos, with_labels=True, node_color=node_color, node_size=node_size) plt.axis('off') # In[39]: import operator
def closeness(g, weights=None, nodes=None, mode="out", harmonic=True, default=np.NaN): r''' Returns the closeness centrality of some `nodes`. Closeness centrality of a node `u` is defined, for the harmonic version, as the sum of the reciprocal of the shortest path distance :math:`d_{uv}` from `u` to the N - 1 other nodes in the graph (if `mode` is "out", reciprocally :math:`d_{vu}`, the distance to `u` from another node v, if `mode` is "in"): .. math:: C(u) = \frac{1}{N - 1} \sum_{v \neq u} \frac{1}{d_{uv}}, or, using the arithmetic definition, as the reciprocal of the average shortest path distance to/from `u` over to all other nodes: .. math:: C(u) = \frac{n - 1}{\sum_{v \neq u} d_{uv}}, where `d_{uv}` is the shortest-path distance from `u` to `v`, and `n` is the number of nodes in the component. By definition, the distance is infinite when nodes are not connected by a path in the harmonic case (such that :math:`\frac{1}{d(v, u)} = 0`), while the distance itself is taken as zero for unconnected nodes in the first equation. Parameters ---------- g : :class:`~nngt.Graph` Graph to analyze. weights : bool or str, optional (default: binary edges) Whether edge weights should be considered; if ``None`` or ``False`` then use binary edges; if ``True``, uses the 'weight' edge attribute, otherwise uses any valid edge attribute required. nodes : list, optional (default: all nodes) The list of nodes for which the clutering will be returned mode : str, optional (default: "out") For directed graphs, whether the distances are computed from ("out") or to ("in") each of the nodes. harmonic : bool, optional (default: True) Whether the arithmetic or the harmonic (recommended) version of the closeness should be used. Returns ------- c : :class:`numpy.ndarray` The list of closeness centralities, on per node. References ---------- .. [nx-harmonic] :nxdoc:`algorithms.centrality.harmonic_centrality` .. [nx-closeness] :nxdoc:`algorithms.centrality.closeness_centrality` ''' w = _get_nx_weights(g, weights) graph = g.graph if graph.is_directed() and mode == "out": graph = g.graph.reverse(copy=False) c = None if harmonic: c = nx.harmonic_centrality(graph, distance=w) else: c = nx.closeness_centrality(graph, distance=w, wf_improved=False) c = np.array([v for _, v in c.items()]) # normalize if harmonic: c *= 1 / (len(graph) - 1) elif default != 0: c[c == 0.] = default if nodes is None: return c return c[nodes]
def centrality(G): # _hist(nx.degree_centrality(G), "Degree Centrality") # _hist(nx.closeness_centrality(G), "Closeness Centrality") _hist(nx.harmonic_centrality(G), "Harmonic Centrality")
def normalized_harmonic_rank(self): c = [v for v in nx.harmonic_centrality(self.graph).values()] self.norm_cent, self.cent_rank = self._normalize_array_by_rank(c)
# Save the values np.savez('A1/output/facebook/degreeCentrality', in_DegCor, normIn_DegCor) print "Calculating Closeness Centrality" # Closeness Centrality closenessCentrality = nx.closeness_centrality(g) np.savez('A1/output/facebook/closenessCentrality', closenessCentrality) print "Calculating Betweenness Centrality" # Betweenness Centrality betweennessCentrality = nx.betweenness_centrality(g) np.savez('A1/output/facebook/betweennessCentrality', betweennessCentrality) print "Calculating Harmonic Centrality" # Harmonic Centrality harmonicCentrality = nx.harmonic_centrality(g) np.savez('A1/output/facebook/harmonicCentrality', harmonicCentrality) """ def getDiameterAndRadius(inEccentricity): diameter = max(inEccentricity.values()) radius = min(inEccentricity.values()) return diameter,radius # Eccentricity and radius inEccentricity = {} outEccentricity = {} for node in nodes: inEccentricity[node] = -1 outEccentricity[node] = -1