def alp_katz(g, subgraphs, func=max): import operator landmarks = [] for sub in subgraphs.values(): if g.is_directed(): g_s = list(nx.strongly_connected_component_subgraphs(g.subgraph(sub)))[0] else: g_s = list(nx.connected_component_subgraphs(g.subgraph(sub), False))[0] n_len = nx.number_of_nodes(g_s) if n_len < 10: landmarks.append(g_s.nodes()[0]) else: try: bc = nx.katz_centrality(g_s, max_iter=100000, normalized=True) except: try: print "Running Katz centrality again for tolerance = ", str(.001) bc = nx.katz_centrality(g_s, max_iter=100000, normalized=True, tol=.001) except: print "Katz Centrality returned nothing." return [] if func == mode: landmarks.append(mode(bc)) else: landmarks.append(func(bc.iteritems(), key=operator.itemgetter(1))[0]) return landmarks
def katz(graph_data): graph = graph_data['net'] w_label = graph_data['w_label'] # sem peso e sem direção graph_simply = dir2undir(graph, w_label).simplify( multiple=True, loops=False, combine_edges=graph_data['simply_method2']) # sem peso e sem direção undir_unweig = sorted(networkx.katz_centrality(graph_simply, weight=None)) undir_unweig = list(undir_unweig.values()) # com peso weig = sorted(networkx.katz_centrality(graph_simply, weight=w_label)) weig = list(weig.values()) # com direção in_katz = sorted(networkx.katz_centrality(graph.to_networkx(), weight=None)) in_katz = list(in_katz.values()) # com peso e com direção in_weig_katz = sorted( networkx.katz_centrality(graph.to_networkx(), weight=w_label)) in_weig_katz = list(in_weig_katz.values()) return undir_unweig, weig, in_katz, in_weig_katz
def compute_katz(self, graph_id): """ Computes the Katz coeffcient for every node in the indicated graph. Ads the computed coefficient as property to nodes. """ if graph_id == 1: rank = nx.katz_centrality(self.graph_icnet) nx.set_node_attributes(self.graph_icnet, 'katz', rank) if graph_id == 2: rank = nx.katz_centrality(self.graph_aicnet) nx.set_node_attributes(self.graph_aicnet, 'katz', rank)
def test_K5(self): """Katz centrality: K5""" G = nx.complete_graph(5) alpha = 0.1 b = nx.katz_centrality(G, alpha) v = math.sqrt(1 / 5.0) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert b[n] == pytest.approx(b_answer[n], abs=1e-7) nstart = {n: 1 for n in G} b = nx.katz_centrality(G, alpha, nstart=nstart) for n in sorted(G): assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_K5(self): """Katz centrality: K5""" G = nx.complete_graph(5) alpha = 0.1 b = nx.katz_centrality(G, alpha) v = math.sqrt(1 / 5.0) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert_almost_equal(b[n], b_answer[n]) nstart = dict([(n, 1) for n in G]) b = nx.katz_centrality(G, alpha, nstart=nstart) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
def test_K5(self): """Katz centrality: K5""" G = nx.complete_graph(5) alpha = 0.1 b = nx.katz_centrality(G, alpha) v = math.sqrt(1 / 5.0) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert almost_equal(b[n], b_answer[n]) nstart = {n: 1 for n in G} b = nx.katz_centrality(G, alpha, nstart=nstart) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def centrality_algorithms(graph): # Centrality functions return a dictionary of values # Calculate the maximum and print node name with value # Value stays the same for closness centrality, but the node itself changes centrality_dict = nx.degree_centrality(graph) print('Degree Centrality: ', max(centrality_dict, key=centrality_dict.get), max(centrality_dict.values())) centrality_dict = nx.in_degree_centrality(graph) print('In Degree Centrality: ', max(centrality_dict, key=centrality_dict.get), max(centrality_dict.values())) centrality_dict = nx.out_degree_centrality(graph) print('Out Degree Centrality: ', max(centrality_dict, key=centrality_dict.get), max(centrality_dict.values())) centrality_dict = nx.eigenvector_centrality_numpy(graph) print('Eigenvector Centrality: ', max(centrality_dict, key=centrality_dict.get), max(centrality_dict.values())) centrality_dict = nx.katz_centrality(graph) print('Katz Centrality: ', max(centrality_dict, key=centrality_dict.get), max(centrality_dict.values())) centrality_dict = nx.closeness_centrality(graph) print('Closeness Centrality: ', max(centrality_dict, key=centrality_dict.get), max(centrality_dict.values())) centrality_dict = nx.betweenness_centrality(graph) print('Betweenness Centrality: ', max(centrality_dict, key=centrality_dict.get), max(centrality_dict.values()))
def centrality_calculation_by_networkx(G): ''' 使用 networkx 计算 Centrality ''' d_c = nx.degree_centrality(G) k_z = nx.katz_centrality( G=G, alpha=0.3, beta=0.3, max_iter=1000, tol=1.0e-6, nstart=None, normalized=True) # 归一化,每个元素除以集合中最大元素 max_item = max([d_c[item] for item in d_c]) degree_centrality = [round(d_c[item] / max_item, 4) for item in d_c] max_item = max([k_z[item] for item in k_z]) katz_centrality = [round(k_z[item] / max_item, 4) for item in k_z] nx_list = [{'Degree': degree_centrality}, {'Katz': katz_centrality}] return nx_list
def test_katz_centrality_nx(graph_file): gc.collect() NM = utils.read_csv_for_nx(graph_file) Gnx = nx.from_pandas_edgelist(NM, create_using=nx.DiGraph(), source="0", target="1") G = cugraph.utilities.convert_from_nx(Gnx) largest_out_degree = G.degrees().nlargest(n=1, columns="out_degree") largest_out_degree = largest_out_degree["out_degree"].iloc[0] katz_alpha = 1 / (largest_out_degree + 1) nk = nx.katz_centrality(Gnx, alpha=katz_alpha) ck = cugraph.katz_centrality(Gnx, alpha=None, max_iter=1000) # Calculating mismatch nk = sorted(nk.items(), key=lambda x: x[0]) ck = sorted(ck.items(), key=lambda x: x[0]) err = 0 assert len(ck) == len(nk) for i in range(len(ck)): if (abs(ck[i][1] - nk[i][1]) > 0.1 and ck[i][0] == nk[i][0]): err = err + 1 print("Mismatches:", err) assert err < (0.1 * len(ck))
def centrality_histogram(g, c): # Creates the centrality histogram specified if c == 'degree': degree_sequence = sorted( [val for key, val in nx.degree_centrality(g).items()]) elif c == 'in_degree': degree_sequence = sorted( [val for key, val in nx.in_degree_centrality(g).items()]) elif c == 'out_degree': degree_sequence = sorted( [val for key, val in nx.out_degree_centrality(g).items()]) elif c == 'closeness': degree_sequence = sorted( [val for key, val in nx.closeness_centrality(g).items()]) elif c == 'betweenness': degree_sequence = sorted( [val for key, val in nx.betweenness_centrality(g).items()]) elif c == 'eigenvector': degree_sequence = sorted( [val for key, val in nx.eigenvector_centrality(g).items()]) elif c == 'katz': degree_sequence = sorted( [val for key, val in nx.katz_centrality(g).items()]) degree_count = col.Counter(degree_sequence) deg, cnt = zip(*degree_count.items()) plt.bar(deg, cnt, width=0.01, color='b') plt.title('Degree Histogram') plt.ylabel('Count') plt.xlabel('Degree') plt.show()
def relevant_stats(G): cloC = nx.closeness_centrality(G, distance = 'distance') betC = nx.betweenness_centrality(G, weight = 'distance') katC = nx.katz_centrality(G) eigC = nx.eigenvector_centrality(G) return
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 nextBestNode(self, state): #Finds the next best spot for a station graph = state.get_graph() nodeScores = [] centrality = nx.katz_centrality(graph) for i in centrality: nodeScores.append(centrality[i]) bestNode = 0 bestScore = nodeScores[0] for i in range(len(nodeScores)): if (nodeScores[i] > bestScore and (i not in self.stations)): bestNode = i bestScore = nodeScores[i] #print("bestNode=",(bestScore,bestNode)) #Factor in distance from each station for i in range(len(nodeScores)): if (i not in self.stations): distanceScore = 1.0 / self.closestStation(graph, i)[1] #print("distanceScore=",distanceScore) nodeScores[i] -= distanceScore * self.distanceWeight #Get the node with the best score bestNode = 0 bestScore = nodeScores[0] for i in range(len(nodeScores)): if (nodeScores[i] > bestScore and (i not in self.stations)): bestNode = i bestScore = nodeScores[i] #print("bestNode=",(bestScore,bestNode)) return bestNode
def centralities(self): ''' Get info on centralities of data Params: None Returns: dictionary of centrality metrics with keys(centralities supported): degree - degree centrality betweeness - betweeness centrality eigenvector - eigenvector centrality hub - hub scores - not implemented authority - authority scores - not implemented katz - katz centrality with params X Y pagerank - pagerank centrality with params X Y ''' output = {} output['degree'] = nx.degree_centrality(self.G) output['betweeness'] = nx.betweenness_centrality(self.G) try: output['eigenvector'] = nx.eigenvector_centrality(self.G) output['katz'] = nx.katz_centrality(self.G) except: output['eigenvector'] = 'empty or exception' output['katz'] = 'empty or exception' # output['hub'] = 'Not implemented' # output['authority'] = 'Not implemented' # output['pagerank'] = 'Not implemented' return output
def centrality(self, c_type='degree'): """计算整个网络的所有节点的中心度,返回一个dict""" if c_type not in self._centrality.keys(): # 检测输入类型不正确的异常 raise ValueError( "输入类型不正确!type请输入degree/indegree/outdegree/eigenvector/katz/pagerank/betweenness/closeness!") if not self._centrality[c_type]: if c_type == 'degree': # 因为networkx的度数中心度计算不带权重,故自制了一个 if self.weight_type: weight = [edge[2][self.weight_type] for edge in self.network.edges(data=True)] s = 1.0 / sum(weight) else: s = 1.0 / (len(self.network) - 1.0) self._centrality[c_type] = {n: d * s for n, d in self.network.degree(weight=self.weight_type)} elif c_type == 'indegree': s = 1.0 / (len(self.network) - 1.0) self._centrality[c_type] = {n: d * s for n, d in self.network.in_degree(weight=self.weight_type)} elif c_type == 'outdegree': s = 1.0 / (len(self.network) - 1.0) self._centrality[c_type] = {n: d * s for n, d in self.network.out_degree(weight=self.weight_type)} elif c_type == 'eigenvector': self._centrality[c_type] = nx.eigenvector_centrality(self.network, max_iter=1000, weight=self.weight_type) elif c_type == 'katz': self._centrality[c_type] = nx.katz_centrality(self.network, max_iter=1000, weight=self.weight_type) elif c_type == 'pagerank': pr = nx.pagerank(self.network, weight=self.weight_type) self._centrality[c_type] = {node: pr[node] for node in pr} elif c_type == 'betweenness': self._centrality[c_type] = nx.betweenness_centrality(self.network, weight=self.weight_type) elif c_type == 'closeness': self._centrality[c_type] = nx.closeness_centrality(self.network) return self._centrality[c_type]
def get_hubs(c, method): try: if method == 'eigenvector': centralities = nx.eigenvector_centrality(c, weight='weight', max_iter=1000) elif method == 'pagerank': centralities = nx.pagerank(c, max_iter=1000) elif method == 'hits_hub': centralities = nx.hits(c, max_iter=1000)[1] elif method == 'hits_authority': centralities = nx.hits(c, max_iter=1000)[0] elif method == 'betweenness': centralities = nx.betweenness_centrality(c, weight='weight') elif method == 'katz': centralities = nx.katz_centrality(c, weight='weight', max_iter=10000) else: raise NameError except nx.PowerIterationFailedConvergence: return None max_hub = max(centralities.items(), key=itemgetter(1))[1] hubs = [node for node in centralities if abs(centralities[node]-max_hub)<=0.001] if len(hubs) == len(c.nodes()): return 'all nodes' else: return (', '.join(hubs))
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 print_graph_stats(self, G): print('average clustering: ', nx.average_clustering(G)) degrees = [val for (node, val) in G.degree()] print('average degree: ', sum(degrees) / float(len(G))) print('density: ', nx.density(G)) print('assortativity coefficient (Pearson\'s rho): ', nx.degree_pearson_correlation_coefficient(G)) print('(should be negative according to Gonzalez et al., 2007)') import operator x = nx.betweenness_centrality(G) sorted_x = sorted(x.items(), key=operator.itemgetter(1)) print('betweenness centrality', sorted_x) x = nx.degree_centrality(G) sorted_x = sorted(x.items(), key=operator.itemgetter(1)) print('degree centrality:', sorted_x) x = nx.eigenvector_centrality(G) sorted_x = sorted(x.items(), key=operator.itemgetter(1)) print('eigenvector centrality:', sorted_x) x = nx.katz_centrality(G) sorted_x = sorted(x.items(), key=operator.itemgetter(1)) print('Katz centrality:', sorted_x) x = nx.closeness_centrality(G) sorted_x = sorted(x.items(), key=operator.itemgetter(1)) print('closeness centrality:', sorted_x) print('\n')
def create_metric_column(G, metric, year=None): """ Returns an array of the pageranks for vertices in a network G Parameters ------------ G: igraph network object where each node has a time attribute metric: string of the vertex metrics we want to compute (pagerank, indegree, etc) year: is year of the network if we are passing a time truncated network - katz d_ means for directed graph u_ means for undirected graph Output ------- metric: an array of size G.vs that contains the metric for G's vertices or does not return value on invalid metric parameter """ try: # calculates metric which matched parameter if metric == 'katz': metric_column = nx.katz_centrality(Gnx) except Exception: print 'problem with %s' % metric metric_column = metric_column = [np.nan] * len(G.vs) return metric_column
def CentralityPoint2D(graph, numberOfPoints, typePlot): """Plot 3D centrality based on type.""" points = dict() c_eigenvector = nx.katz_centrality(graph) c_eigenvector = heapq.nlargest(numberOfPoints, list(c_eigenvector.values())) max_eigenvector = max(c_eigenvector) points['Eigenvalues'] = c_eigenvector c_betweenness = nx.betweenness_centrality(graph) c_betweenness = heapq.nlargest(numberOfPoints, list(c_betweenness.values())) max_betweenness = max(c_betweenness) points['Betweenness'] = c_betweenness c_closeness = nx.closeness_centrality(graph) c_closeness = heapq.nlargest(numberOfPoints, list(c_closeness.values())) max_closeness = max(c_closeness) points['Closeness'] = c_closeness c_harmonic = nx.harmonic_centrality(graph) c_harmonic = heapq.nlargest(numberOfPoints, list(c_harmonic.values())) max_harmonic = max(c_harmonic) points['Harmonic'] = c_harmonic glCoe = GlobalClusteringCoefficient(graph) points['Mix'] = (max_eigenvector, max_harmonic, max_betweenness) points['Mix2'] = (max_eigenvector, glCoe, max_closeness) points['Mix3'] = (max_eigenvector, glCoe, max_harmonic) points['Mix4'] = (max_eigenvector, glCoe, SquareClusteringCoefficient(graph)) return points[typePlot]
def test_dask_katz_centrality(client_connection): gc.collect() input_data_path = r"../datasets/karate.csv" chunksize = dcg.get_chunksize(input_data_path) ddf = dask_cudf.read_csv( input_data_path, chunksize=chunksize, delimiter=" ", names=["src", "dst", "value"], dtype=["int32", "int32", "float32"], ) df = cudf.read_csv( input_data_path, delimiter=" ", names=["src", "dst", "value"], dtype=["int32", "int32", "float32"], ) g = cugraph.DiGraph() g.from_cudf_edgelist(df, "src", "dst") dg = cugraph.DiGraph() dg.from_dask_cudf_edgelist(ddf, "src", "dst") largest_out_degree = g.degrees().nlargest(n=1, columns="out_degree") largest_out_degree = largest_out_degree["out_degree"].iloc[0] katz_alpha = 1 / (largest_out_degree + 1) mg_res = dcg.katz_centrality(dg, alpha=katz_alpha, tol=1e-6) mg_res = mg_res.compute() import networkx as nx from cugraph.tests import utils NM = utils.read_csv_for_nx(input_data_path) Gnx = nx.from_pandas_edgelist( NM, create_using=nx.DiGraph(), source="0", target="1" ) nk = nx.katz_centrality(Gnx, alpha=katz_alpha) import pandas as pd pdf = pd.DataFrame(nk.items(), columns=['vertex', 'katz_centrality']) exp_res = cudf.DataFrame(pdf) err = 0 tol = 1.0e-05 compare_res = exp_res.merge( mg_res, on="vertex", suffixes=["_local", "_dask"] ) for i in range(len(compare_res)): diff = abs( compare_res["katz_centrality_local"].iloc[i] - compare_res["katz_centrality_dask"].iloc[i] ) if diff > tol * 1.1: err = err + 1 assert err == 0
def test_P3(self): """Katz centrality: P3""" alpha = 0.1 G = nx.path_graph(3) b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} b = nx.katz_centrality(G, alpha) for n in sorted(G): assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
def run(self,steps): for _ in xrange(steps): self.update() self.prs =nx.pagerank(self.G) self.close = nx.closeness_centrality(self.G) self.bet = nx.betweenness_centrality(self.G) self.katz = nx.katz_centrality(self.G)
def katz_centrality(G, dim): katz_cent_matrix = np.zeros((dim, 1)) katz_cent_dict = nx.katz_centrality(G) z = 0 for i in hosts_list: katz_cent_matrix[z] = katz_cent_dict.get(i) z += 1 return katz_cent_matrix
def readGraph(self): self.readingUsersFile() self.graph = networkx.read_graphml(self.graphFile) (self.followees, self.followers) = self.getFolloweesAndFollowers(self.graph) self.pageRanks = networkx.pagerank(self.graph) self.katz = networkx.katz_centrality(self.graph) self.paths = networkx.shortest_path(self.graph) return self.graph
def test_beta_as_dict(self): alpha = 0.1 beta = {0: 1.0, 1: 1.0, 2: 1.0} b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} G = nx.path_graph(3) b = nx.katz_centrality(G, alpha, beta) for n in sorted(G): assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
def competitive_part(links_dataset, number): # Edge Filtering # k = number of edges we remove per iteration k = 1000 G = nx.from_pandas_edgelist(links_dataset.init_graph, 'target', 'source', edge_attr=True, create_using=nx.Graph()) removed_edges = list() for i in range(number // k): print("iter = ", i) # bridges = list(nx.bridges(G)) ranking = nx.katz_centrality(G, max_iter=100, weight='weight') mean_katz_centrality = np.mean(list(ranking.values())) weight_dict = dict() # reweight edges for edge in G.edges(): print(edge) WG = G.copy() WG.remove_edge(edge[0], edge[1]) _katz = nx.katz_centrality(G, max_iter=20, weight='weight', tol=10e-4) _mean_katz_centrality = np.mean(list(ranking.values())) weight_dict[edge] = mean_katz_centrality - _mean_katz_centrality # sort descending weight_dict = sorted(weight_dict.items(), key=lambda x: x[1], reverse=True) i = 0 # remove bridges with maximal weighted span for edge in dict(weight_dict).keys(): if i == k: break removed_edges.append(edge) G.remove_edge(edge[0], edge[1]) i += 1 return removed_edges
def katz(self): """Compute the Katz centrality for the nodes of the graph G. Returns ------- nodes : dictionary Dictionary of nodes with Katz centrality as the value. """ return nx.katz_centrality(self._graph, weight=self._weight_field)
def test_maxiter(self): alpha = 0.1 G = nx.path_graph(3) max_iter = 0 try: b = nx.katz_centrality(G, alpha, max_iter=max_iter) except nx.NetworkXError as e: assert str(max_iter) in e.args[0], "max_iter value not in error msg" raise # So that the decorater sees the exception.
def kaltzCentrality(graph, numberOfPoints): """Compute the largest kalz centralities coefficients of a graph. You can specify the number of points to output. """ c_eigenvector = nx.katz_centrality(graph) c_eigenvector = heapq.nlargest(numberOfPoints, list(c_eigenvector.values())) return c_eigenvector
def test_beta_as_dict(self): alpha = 0.1 beta = {0: 1.0, 1: 1.0, 2: 1.0} b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} G = networkx.path_graph(3) b = networkx.katz_centrality(G, alpha, beta) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=4)
def test_P3(self): """Katz centrality: P3""" alpha = 0.1 G = nx.path_graph(3) b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} b = nx.katz_centrality(G, alpha) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=4)
def test_P3(self): """Katz centrality: P3""" alpha = 0.1 G = networkx.path_graph(3) b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} b = networkx.katz_centrality(G, alpha) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=4)
def calculate_scores(self): """Computes the Kats centrality indexes for a given graph and with the tunable alpha parameter. Returns: :returns: kc. Returns the Kats centrality indexes. :rtype: set """ kc = nx.katz_centrality(self.graph, self.alpha, beta=0).items() return kc
def get_katz_centrality(Graph): dictionary = nx.katz_centrality(Graph, max_iter=2000) D = [] for name in dictionary: D.append((name, dictionary[name])) D.sort(key=lambda x: x[1]) D.reverse() return D
def comparisons(G): evec = nx.eigenvector_centrality(G) print "EVEC:",evec pagerank = nx.pagerank(G) print "PAGERANK: ", pagerank katz = nx.katz_centrality(G) print "KATZ: ", katz
def test_beta_as_dict(self): alpha = 0.1 beta = {0: 1.0, 1: 1.0, 2: 1.0} b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} G = nx.path_graph(3) b = nx.katz_centrality(G, alpha, beta) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=4)
def test_maxiter(self): alpha = 0.1 G = nx.path_graph(3) max_iter = 0 try: b = nx.katz_centrality(G, alpha, max_iter=max_iter) except nx.NetworkXError as e: assert str( max_iter) in e.args[0], "max_iter value not in error msg" raise # So that the decorater sees the exception.
def katz(g): eigv = eig(nx.adjacency_matrix(g, weight='diff').todense()) max_eigv = max(eigv[0]) max_eigv_reciprocal = 1./max_eigv alpha = max_eigv_reciprocal alpha = 0.9 * alpha beta = 1 - alpha katz_centrality = nx.katz_centrality(g, alpha=alpha, beta=beta, weight='diff') return katz_centrality
def katz_cent(G): A = nx.to_numpy_matrix(G) e = np.linalg.eigvals(A) maxe = np.max(np.real(e)) alpha = 1 / (2 * maxe) return nx.katz_centrality(G, alpha=alpha)
def compute_mean_katz_cent(self): """Computes the mean Katz centrality of a network of neurons. The centrality of a node measures how many of the shortest paths between all other nodes pairs in the network pass through it. A node with high centrality is thus crucial to efficient communication. (Bullmore et. al. 2009) https://en.wikipedia.org/wiki/Katz_centrality """ graph_centrality = nx.katz_centrality(self.network, weight="weight") return np.mean(list(graph_centrality.values()))
def node_katz_centrality(nx_graph, dict=False): """ Calculate the katz centrality for the nodes on the graph. """ katz_cent = nx.katz_centrality(nx_graph, max_iter=2000, tol=1e-03) if not dict: katz_cent = [(node, katz_cent[node]) for node in sorted(katz_cent, key=lambda v: katz_cent[v])] return katz_cent
def test_K5_unweighted(self): """Katz centrality: K5""" G = nx.complete_graph(5) alpha = 0.1 b = nx.katz_centrality(G, alpha, weight=None) v = math.sqrt(1 / 5.0) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert_almost_equal(b[n], b_answer[n]) nstart = dict([(n, 1) for n in G]) b = nx.eigenvector_centrality_numpy(G) for n in sorted(G): assert_almost_equal(b[n], b_answer[n], places=3)
def centralityMeasures(G): # Betweenness # betw = nx.betweenness_centrality(G, normalized=True, weight='weight') # print sorted([(k,v) for k,v in betw.iteritems()], key= lambda x:(-x[1],x[0])) # clsn = nx.closeness_centrality(G, normalized=True) # print sorted([(k,v) for k,v in clsn.iteritems()], key= lambda x:(-x[1],x[0])) # evec = nx.eigenvector_centrality(G, weight='weight') # print sorted([(k,v) for k,v in evec.iteritems()], key= lambda x:(-x[1],x[0])) katz = nx.katz_centrality(G, normalized=True, weight='weight', alpha=0.005) print sorted([(k,v) for k,v in katz.iteritems()], key= lambda x:(-x[1],x[0]))
def test_multiple_alpha(self): alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] for alpha in alpha_list: b_answer = {0.1: {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162}, 0.2: {0: 0.5454545454545454, 1: 0.6363636363636365, 2: 0.5454545454545454}, 0.3: {0: 0.5333964609104419, 1: 0.6564879518897746, 2: 0.5333964609104419}, 0.4: {0: 0.5232045649263551, 1: 0.6726915834767423, 2: 0.5232045649263551}, 0.5: {0: 0.5144957746691622, 1: 0.6859943117075809, 2: 0.5144957746691622}, 0.6: {0: 0.5069794004195823, 1: 0.6970966755769258, 2: 0.5069794004195823}} G = nx.path_graph(3) b = nx.katz_centrality(G, alpha) for n in sorted(G): assert_almost_equal(b[n], b_answer[alpha][n], places=4)
def calculate_center(tcgaSubgraph): """ DESCRIPTION: Calculate centrality measures INPUT: Graph object OUTPUT: Dictionary of dictionaries, each being a different centrality measure """ # calculate maximum eigenvalue of graph denseMat = nx.adjacency_matrix(tcgaSubgraph).todense() # make adj mat eigs = numpy.linalg.eig(denseMat)[0] # calculate eigenvalues maxEig = max(eigs) alpha = 1 / maxEig.real # calculate centrality measures centers = {} centers["eigen"] = nx.eigenvector_centrality(tcgaSubgraph) centers["degree"] = nx.degree_centrality(tcgaSubgraph) centers["katz"] = nx.katz_centrality(tcgaSubgraph, alpha=alpha - 0.01, beta=1.0) centers["pagerank"] = nx.pagerank(tcgaSubgraph) return centers
def centrality_calculation_by_networkx(G): ''' 使用 networkx 计算 Centrality ''' d_c = nx.degree_centrality(G) k_z = nx.katz_centrality( G=G, alpha=0.3, beta=0.3, max_iter=1000, tol=1.0e-6, nstart=None, normalized=True) # 归一化,每个元素除以集合中最大元素 d_c = [round(1.0 * item / max(d_c), 4) for item in d_c] k_z = [round(1.0 * item / max(k_z), 4) for item in k_z] nx_list = [{'Degree': d_c}, {'Katz': k_z}] return nx_list
def test(G): d_c = nx.degree_centrality(G) e_v = nx.eigenvector_centrality(G=G, max_iter=1000, tol=1.0e-6) k_z = nx.katz_centrality( G=G, alpha=0.3, beta=0.3, max_iter=1000, tol=1.0e-6, nstart=None, normalized=True) p_k = nx.pagerank(G=G, alpha=0.3, personalization=None, max_iter=100, tol=1.0e-6, nstart=None, weight='weight', dangling=None) b_c = nx.betweenness_centrality(G=G, k=None, normalized=True, weight=None, endpoints=False, seed=None) c_c = nx.closeness_centrality(G=G, u=None, distance=None, normalized=True) d_c = [round(1.0 * item / max(d_c), 4) for item in d_c] k_z = [round(1.0 * item / max(k_z), 4) for item in k_z] e_v = [round(1.0 * item / max(e_v), 4) for item in e_v] b_c = [round(1.0 * item / max(b_c), 4) for item in b_c] c_c = [round(1.0 * item / max(c_c), 4) for item in c_c] p_k = [round(1.0 * item / max(p_k), 4) for item in p_k] return [{'Eigenvector': e_v}, {'Betweenness': b_c}, {'Closeness': c_c}, {'PageRank': p_k}, {'Degree': d_c}, {'Katz': k_z}]
dep_count = tz.pipe(edges, c.pluck(1), tz.frequencies) dep_count['skimage'] import networkx as nx deps = nx.DiGraph() 'scikit-image' in packages 'scikit-learn' in packages for u, v in edges: u = u.replace('scikit-', 'sk') v = v.replace('scikit-', 'sk') deps.add_edge(u, v) deps.number_of_edges() deps.number_of_nodes() deps.node['skimage'] deps.in_edges('skimage') nodes = nx.katz_centrality(deps) central = sorted(deps.nodes(), key=nodes.__getitem__, reverse=True) central[:10] central[:20] central[:40] central[40:80] central.index('skimage') central.index('scipy') import pickle stdlib = pickle.load(open('/Users/jni/projects/depsy/data/python_standard_libs.pickle', 'rb')) central_nonstd = list(tz.filter(lambda x: x not in stdlib, central)) len(central_nonstd) central_nonstd.index('scipy') len(central) central[:5] nx.is_connected(deps.to_undirected())
def test_maxiter(self): alpha = 0.1 G = networkx.path_graph(3) b = networkx.katz_centrality(G, alpha, max_iter=0)
def test_empty(self): e = networkx.katz_centrality(networkx.Graph(), 0.1)
def make_net(centrality_name, in_path, out_path): #sample code #import _2_time_based_data_network_feature #make_net_in_path = "../3.time_based_data/1.cite_relation_devide/" #make_net_out_path = "../3.time_based_data/2.centrality_data/" #_2_time_based_data.make_net( "in_degree", make_net_in_path, make_net_out_path) #네트워크를 만들고 Centurality를 계산하고 저장할 것이다. import networkx as nx global Dump Dump = {} make_net_initialize(in_path) start_time = time.time() temp_start_time = time.time() print "============= make_net start:" + centrality_name + " ==============" print "============= from 1951 to 2015 ==============" for year in range(1951, 2016): print year f_in = open(in_path + str(year) + "_cite.csv","r") lines = f_in.readlines() f_in.close() edge_list = [] for line in lines: data = line.split(",") data_tuple = (data[0].strip(), data[1].strip()) edge_list.append(data_tuple) Net = nx.DiGraph(edge_list) Cen_in = {} if (centrality_name == "in_degree"): Cen_in = nx.in_degree_centrality(Net) elif (centrality_name == "degree"): Cen_in = nx.degree_centrality(Net) elif (centrality_name == "eigenvector"): Cen_in = nx.eigenvector_centrality_numpy(Net) elif (centrality_name == "katz"): Cen_in = nx.katz_centrality(Net) elif (centrality_name == "pagerank"): Cen_in = nx.pagerank(Net) elif (centrality_name == "communicability"): Net = nx.Graph(edge_list) Cen_in = nx.communicability_centrality(Net) elif (centrality_name == "load"): Cen_in = nx.load_centrality(Net) for j in Cen_in: key = j val = Cen_in[j] Dump[key][year] = val #저장하는 코드 f_out = open(out_path + centrality_name +"_centrality.csv", "w") for key in Dump: line = str(key) for year in range(1951, 2016): data = Dump[key].get(year, 0) line = line + ","+ str(data) line = line + "\n" f_out.write(line) f_out.close() print "============= make_net end ==============" print(centrality_name + "takes %s seconds" % (time.time() - temp_start_time)) temp_start_time = time.time()
def add_katz_node(graf): print "Adding katz to nodes" k_dict = nx.katz_centrality(graf) nx.set_node_attributes(graf, 'kat', k_dict)
for r in results: edge_list.append((r[0],r[1])) cur.close() connection.close() print edge_list #set up a blank graph G=nx.Graph() #add edges from edge list to graph G G.add_edges_from(edge_list) #calculate degree and katz centrality in graph G deg1 = nx.degree_centrality(G) katz1 = nx.katz_centrality(G) #print the nodes and edges from graph G print 'Graph G has the following nodes: ',(G.nodes()) print 'Graph G has the following edges: ',(G.edges()) print 'Graph has degree centrality: ',deg1 print 'Graph has katz centrality: ', katz1 # #edge_list_2 = [] #sql = "select call_from, call_to from random_calls where c_year = '2012' and c_week_no = '2'" #cur.execute(sql) #results = cur.fetchall() #for r in results: # edge_list_2.append((r[0],r[1])) # #print edge_list_2 #
def test_katz_centrality_unweighted(self): G = self.H alpha = self.H.alpha p = nx.katz_centrality(G, alpha) for (a, b) in zip(list(p.values()), self.G.evc): assert_almost_equal(a, b)
def test_bad_beta_numbe(self): G = nx.Graph([(0,1)]) e = nx.katz_centrality(G, 0.1,beta='foo')
def test_bad_beta(self): G = nx.Graph([(0,1)]) beta = {0:77} e = nx.katz_centrality(G, 0.1,beta=beta)
def test_empty(self): e = nx.katz_centrality(nx.Graph(), 0.1) assert_equal(e, {})
def test_multigraph(self): e = nx.katz_centrality(nx.MultiGraph(), 0.1)
def classesGraph(self,c,files_dict,scope): edges = [] interfaces = 'select path, superClass from classes' files_Names = [x.split(".")[-1] for x in files_dict] pathNames = {} for row in c.execute(interfaces): nameClass = (row[0]).split(".")[-1] pathNames[nameClass] = row[0] nameSuper = (row[1]).split(".")[-1] if (nameClass in files_Names): sup = 'root' if (nameSuper in files_Names): sup = nameSuper edges.append((sup, nameClass)) g = networkx.DiGraph() g.add_node('root') g.add_edges_from(edges) paths = networkx.single_source_dijkstra_path(g, 'root') methods={} for x in files_Names: methods[x]=[] methods['root']=[] sigsEdges=[] interfaces='select classPath,signature,return, name from methods where scope='+scope for row in c.execute(interfaces): nameClass = (row[0]).split(".")[-2] if nameClass in files_Names: methods[nameClass].append(row[3]) retAdd=[] if row[2] in files_Names: retAdd=[(nameClass,row[2])] sigsEdges.extend([ (nameClass,x) for x in self.signatureTolst(row[1]) if x in files_Names]+retAdd) fields='select classPath, name ,type from fields where scope='+scope fields_d={} for x in files_Names: fields_d[x]=[] fields_d['root']=[] for row in c.execute(fields): nameClass = (row[0]).split(".")[-2] if nameClass in files_Names: fields_d[nameClass].append(row[1]) type_f=[] if row[2] in files_Names: type_f=[(nameClass,row[2])] sigsEdges.extend(type_f) g2=networkx.DiGraph() g2.add_node('root') g2.add_edges_from(sigsEdges) counts= Counter(sigsEdges) g3=networkx.DiGraph() g3.add_node('root') for e,w in counts.items(): u,v=e g3.add_edge(u,v,weight=w) self.addFromDict(files_dict,g2.out_degree(),pathNames) self.addFromDict(files_dict,networkx.katz_centrality(g2),pathNames) self.addFromDict(files_dict,networkx.core_number(g2),pathNames) self.addFromDict(files_dict,networkx.closeness_centrality(g2),pathNames) self.addFromDict(files_dict,networkx.degree_centrality(g2),pathNames) self.addFromDict(files_dict,networkx.out_degree_centrality(g2),pathNames) self.addFromDict(files_dict,g3.out_degree(),pathNames) self.addFromDict(files_dict,networkx.core_number(g3),pathNames) self.addFromDict(files_dict,networkx.closeness_centrality(g3),pathNames) self.addFromDict(files_dict,networkx.degree_centrality(g3),pathNames) self.addFromDict(files_dict,networkx.out_degree_centrality(g3),pathNames) #gi = igraph.Graph(edges = g2.edges(),directed=True) #gi.modularity() #self.addFromDict(files_dict,gi.community_optimal_modularity(),pathNames) networkx.write_graphml(g,"C:\GitHub\weka\\graph.graphml")