예제 #1
0
 def test_K4(self):
     """Closeness centrality: K4"""
     G = nx.complete_graph(4)
     b = nx.current_flow_closeness_centrality(G)
     b_answer = {0: 2.0 / 3, 1: 2.0 / 3, 2: 2.0 / 3, 3: 2.0 / 3}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_P4(self):
     """Closeness centrality: P4"""
     G=networkx.path_graph(4)
     b=networkx.current_flow_closeness_centrality(G,normalized=False)
     b_answer={0: 1.0/6, 1: 1.0/4, 2: 1.0/4, 3:1.0/6}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
 def test_K4(self):
     """Closeness centrality: K4"""
     G = nx.complete_graph(4)
     b = nx.current_flow_closeness_centrality(G)
     b_answer = {0: 2.0 / 3, 1: 2.0 / 3, 2: 2.0 / 3, 3: 2.0 / 3}
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def centrality(net):
    values ={}
    close = nx.closeness_centrality(net, normalized= True)
    eigen = nx.eigenvector_centrality_numpy(net)
    page = nx.pagerank(net)
    bet = nx.betweenness_centrality(net,normalized= True)
    flow_c = nx.current_flow_closeness_centrality(net,normalized= True)
    flow_b = nx.current_flow_betweenness_centrality(net,normalized= True)
    load = nx.load_centrality(net, normalized = True)
    com_c = nx.communicability_centrality(net)
    com_b = nx.communicability_betweenness_centrality(net, normalized= True)
    degree = net.degree()
    
    file3 = open("bl.csv",'w')
    for xt in [bet,load,degree,page,flow_b,com_c,com_b,eigen,close,flow_c]:#[impo,bet,flow_b,load,com_c,com_b] :
        for yt in [bet,load,degree,page,flow_b,com_c,com_b,eigen,close,flow_c]:#[impo,bet,flow_b,load,com_c,com_b] :
            corr(xt.values(),yt.values(),file3)
        print
        file3.write("\n")
    file3.close()
    #plt.plot(x,y, 'o')
    #plt.plot(x, m*x + c, 'r', label='Fitted line')
    #plt.show()
    #for key,item in close.iteritems() :
        #values[key] = [impo.get(key),bet.get(key),flow_b.get(key), load.get(key),com_c.get(key),com_b.get(key)]
        
    return values
예제 #5
0
 def test_K4(self):
     """Closeness centrality: K4"""
     G = networkx.complete_graph(4)
     b = networkx.current_flow_closeness_centrality(G, normalized=True)
     b_answer = {0: 2.0, 1: 2.0, 2: 2.0, 3: 2.0}
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n])
def centrality(net):
    values = {}
    close = nx.closeness_centrality(net, normalized=True)
    eigen = nx.eigenvector_centrality_numpy(net)
    page = nx.pagerank(net)
    bet = nx.betweenness_centrality(net, normalized=True)
    flow_c = nx.current_flow_closeness_centrality(net, normalized=True)
    flow_b = nx.current_flow_betweenness_centrality(net, normalized=True)
    load = nx.load_centrality(net, normalized=True)
    com_c = nx.communicability_centrality(net)
    com_b = nx.communicability_betweenness_centrality(net, normalized=True)
    degree = net.degree()

    file3 = open("bl.csv", 'w')
    for xt in [
            bet, load, degree, page, flow_b, com_c, com_b, eigen, close, flow_c
    ]:  #[impo,bet,flow_b,load,com_c,com_b] :
        for yt in [
                bet, load, degree, page, flow_b, com_c, com_b, eigen, close,
                flow_c
        ]:  #[impo,bet,flow_b,load,com_c,com_b] :
            corr(xt.values(), yt.values(), file3)
        print
        file3.write("\n")
    file3.close()
    #plt.plot(x,y, 'o')
    #plt.plot(x, m*x + c, 'r', label='Fitted line')
    #plt.show()
    #for key,item in close.iteritems() :
    #values[key] = [impo.get(key),bet.get(key),flow_b.get(key), load.get(key),com_c.get(key),com_b.get(key)]

    return values
예제 #7
0
 def test_P4_normalized(self):
     """Closeness centrality: P4 normalized"""
     G = networkx.path_graph(4)
     b = networkx.current_flow_closeness_centrality(G, normalized=True)
     b_answer = {0: 1. / 2, 1: 3. / 4, 2: 3. / 4, 3: 1. / 2}
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n])
 def test_P4(self):
     """Closeness centrality: P4"""
     G = nx.path_graph(4)
     b = nx.current_flow_closeness_centrality(G)
     b_answer = {0: 1.0 / 6, 1: 1.0 / 4, 2: 1.0 / 4, 3: 1.0 / 6}
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
예제 #9
0
    def analyze(self, save=True, wordkey='word'):
        net = self.g
        print('>> analyzing graph of', net.order(), 'nodes and', net.size(),
              'edges ...')
        statd = {}
        statd['centrality_degree'] = nx.degree_centrality(net)
        statd['centrality_information'] = nx.current_flow_closeness_centrality(
            net)
        statd['centrality_closeness'] = nx.closeness_centrality(net)
        statd['centrality_betweenness'] = nx.betweenness_centrality(net)
        statd['centrality_betweenness_weighted'] = nx.betweenness_centrality(
            net, weight='weight')
        statd['centrality_eigenvector'] = nx.eigenvector_centrality(net)
        statd['centrality_degree'] = nx.degree_centrality(net)
        statd['clustering_coefficient'] = nx.clustering(net)
        statd['eccentricity'] = nx.eccentricity(net)
        print('>> done with analysis.')

        old = []
        for node in net.nodes():
            dx = {
                'model': self.model.name,
                wordkey: node,
                'neighbors':
                ', '.join(sorted(list(nx.all_neighbors(net, node))))
            }
            #for k,v in self.node2d.get(node,{}).items(): dx[k]=v
            for statname, node2stat in list(statd.items()):
                dx[statname] = node2stat[node]
            old += [dx]

        if save:
            pytxt.write2(self.fn.replace('.graphml', '.analysis2.txt'), old)
        return old
 def test_K4(self):
     """Closeness centrality: K4"""
     G=nx.complete_graph(4)
     b=nx.current_flow_closeness_centrality(G)
     b_answer={0: 2.0/3, 1: 2.0/3, 2: 2.0/3, 3: 2.0/3}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
 def test_K4(self):
     """Closeness centrality: K4"""
     G=networkx.complete_graph(4)
     b=networkx.current_flow_closeness_centrality(G,normalized=True)
     b_answer={0: 2.0, 1: 2.0, 2: 2.0, 3: 2.0}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
예제 #12
0
def compute_centrality(star_dict, edge_dict):
    
    #build up a nx graph
    galaxy = networkx.Graph()
    for v, vertex in star_dict.iteritems():
        galaxy.add_node(v)
    
    for v, neighbors in edge_dict.iteritems():
        for n in neighbors:
            galaxy.add_edge(v,n)
            
    print "betweenness"
    betweenness_map = networkx.current_flow_betweenness_centrality(galaxy)
    betweenness_map = normalize(betweenness_map)
    
    for key, value in betweenness_map.iteritems():
        star_dict[key]['betweenness'] = value
        
    print "closeness"
    closeness_map = networkx.current_flow_closeness_centrality(galaxy)
    closeness_map = normalize(closeness_map)
    
    for key, value in closeness_map.iteritems():
        star_dict[key]['closeness'] = value
        

    print "pagerank"
    pagerank_map = networkx.pagerank_scipy(galaxy)
    pagerank_map = normalize(pagerank_map)
    
    for key, value in pagerank_map.iteritems():
        star_dict[key]['pagerank'] = value
 def test_P4_normalized(self):
     """Closeness centrality: P4 normalized"""
     G=networkx.path_graph(4)
     b=networkx.current_flow_closeness_centrality(G,normalized=True)
     b_answer={0: 1./2, 1: 3./4, 2: 3./4, 3:1./2}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
예제 #14
0
 def test_P4(self):
     """Closeness centrality: P4"""
     G = nx.path_graph(4)
     b = nx.current_flow_closeness_centrality(G)
     b_answer = {0: 1.0 / 6, 1: 1.0 / 4, 2: 1.0 / 4, 3: 1.0 / 6}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
예제 #15
0
파일: code.py 프로젝트: kirk86/Task-1
def node_current_flow_closeness_centrality(X):
    """
    based on networkx function: current_flow_closeness_centrality
    """
    XX = np.zeros((X.shape[0], np.sqrt(X.shape[1])))
    for i, value in enumerate(X):
        adj_mat = value.reshape((np.sqrt(len(value)),-1))
        adj_mat = (adj_mat - np.min(adj_mat)) / (np.max(adj_mat) - np.min(adj_mat))
        adj_mat = 1 - adj_mat

#        th = np.mean(adj_mat) - 0.05
#        adj_mat = np.where(adj_mat < th, adj_mat, 0.)

        percent, th, adj_mat, triu = percentage_removed(adj_mat, 0.64) #74
        print("percent = {0}, threshold position = {1}, threshold = {2}\n".format(percent, th, triu[th]))

        g = nx.from_numpy_matrix(adj_mat)
        print "Graph Nodes = {0}, Graph Edges = {1} ".format(g.number_of_nodes(), g.number_of_edges())
        print "\nEdge kept ratio, {0}".format(float(g.number_of_edges())/((g.number_of_nodes()*(g.number_of_nodes()-1))/2))

        deg_cent = nx.current_flow_closeness_centrality(g)
        node_cent = np.zeros(g.number_of_nodes())

        for k in deg_cent:
            node_cent[k] = deg_cent[k]
        XX[i] = node_cent
        print "graph {0} => mean {1}, min {2}, max {3}".format(i, np.mean(XX[i]), np.min(XX[i]), np.max(XX[i]))
#    XX = XX*100
    ss = StandardScaler()
    XX = ss.fit_transform(XX.T).T

    return XX
 def test_star(self):
     """Closeness centrality: star """
     G=networkx.Graph()
     G.add_star(['a','b','c','d'])
     b=networkx.current_flow_closeness_centrality(G,normalized=True)
     b_answer={'a': 1.0, 'b': 0.6, 'c': 0.6, 'd':0.6}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
 def test_star(self):
     """Closeness centrality: star """
     G=nx.Graph()
     nx.add_star(G, ['a', 'b', 'c', 'd'])
     b=nx.current_flow_closeness_centrality(G)
     b_answer={'a': 1.0/3, 'b': 0.6/3, 'c': 0.6/3, 'd':0.6/3}
     for n in sorted(G):
         assert_almost_equal(b[n],b_answer[n])
 def test_star(self):
     """Closeness centrality: star """
     G = nx.Graph()
     nx.add_star(G, ["a", "b", "c", "d"])
     b = nx.current_flow_closeness_centrality(G)
     b_answer = {"a": 1.0 / 3, "b": 0.6 / 3, "c": 0.6 / 3, "d": 0.6 / 3}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
예제 #19
0
 def test_star(self):
     """Closeness centrality: star """
     G = nx.Graph()
     nx.add_star(G, ['a', 'b', 'c', 'd'])
     b = nx.current_flow_closeness_centrality(G)
     b_answer = {'a': 1.0 / 3, 'b': 0.6 / 3, 'c': 0.6 / 3, 'd': 0.6 / 3}
     for n in sorted(G):
         assert almost_equal(b[n], b_answer[n])
 def test_star(self):
     """Closeness centrality: star"""
     G = nx.Graph()
     nx.add_star(G, ["a", "b", "c", "d"])
     b = nx.current_flow_closeness_centrality(G)
     b_answer = {"a": 1.0 / 3, "b": 0.6 / 3, "c": 0.6 / 3, "d": 0.6 / 3}
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
예제 #21
0
def current_flow_closeness(G):
    """Current-flow closeness centrality"""
    G = G.to_undirected()
    G = invert_edge_weights(G)
    if nx.is_connected(G):
        return nx.current_flow_closeness_centrality(G)
    else:
        return _aggregate_for_components(G, nx.current_flow_closeness_centrality)
예제 #22
0
 def test_star(self):
     """Closeness centrality: star """
     G = networkx.Graph()
     G.add_star(['a', 'b', 'c', 'd'])
     b = networkx.current_flow_closeness_centrality(G, normalized=True)
     b_answer = {'a': 1.0, 'b': 0.6, 'c': 0.6, 'd': 0.6}
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n])
예제 #23
0
파일: graph.py 프로젝트: kvalle/TextNet
def current_flow_closeness(G):
    """Current-flow closeness centrality"""
    G = G.to_undirected()
    G = invert_edge_weights(G)
    if nx.is_connected(G):
        return nx.current_flow_closeness_centrality(G)
    else:
        return _aggregate_for_components(G,
                                         nx.current_flow_closeness_centrality)
예제 #24
0
def get_wire_centrality(network, this_TimeStamp=0, mode = 'betweenness'):
    edgeList = network.connectivity.edge_list
    conMat = np.zeros((network.numOfWires, network.numOfWires))
    conMat[edgeList[:,0], edgeList[:,1]] = network.junctionConductance[this_TimeStamp,:]
    conG = nx.from_numpy_array(conMat)
    if mode == 'betweenness':
        return np.array(list(nx.current_flow_betweenness_centrality_subset(conG, network.sources, network.drains, weight = 'weight').values()))
    elif mode == 'closeness':
        return np.array(list(nx.current_flow_closeness_centrality(conG, weight = 'weight').values()))
예제 #25
0
def network_algorithms(g, dfs):
	print("Calculating network algorithms")

	# iterate over graph components
	for i in dfs:
		metrics = []

		# find all edges of the subgraph and only keep the "offer" relationship
		id_ = i.select("id").map(lambda a: a.id)
		ids = id_.collect()

		edges_ = g.edges.rdd.filter(lambda a: a.src in ids).toDF()
		df = edges_.select("src", "dst").toPandas()
		edge_list = [tuple(x) for x in df.values]

		# generate a networkx graph
		G = nx.Graph()
		G.add_edges_from(edge_list)

		# calculate several network metrics for the graph
		metrics.append(result_to_pandas(dict(nx.degree(G)), "degree"))

		metrics.append(result_to_pandas(nx.closeness_centrality(G), "closeness_centrality"))

		metrics.append(result_to_pandas(nx.betweenness_centrality(G), "betweenness_centrality"))

		metrics.append(result_to_pandas(nx.current_flow_closeness_centrality(G), "current_flow_closeness_centrality"))

		metrics.append(result_to_pandas(nx.current_flow_betweenness_centrality(G), "current_flow_betweenness_centrality"))

		metrics.append(result_to_pandas(nx.katz_centrality_numpy(G), "katz_centrality"))

		metrics.append(result_to_pandas(nx.load_centrality(G), "load_centrality"))

		metrics.append(result_to_pandas(nx.pagerank(G), "pagerank"))

		# TypeError: Cannot use scipy.linalg.eig for sparse A with k >= N - 1. Use scipy.linalg.eig(A.toarray()) or reduce k.
		# metrics.append(result_to_pandas(nx.eigenvector_centrality_numpy(G), "eigenvector_centrality"))

		# join network metrics into one graph
		res = pd.concat(metrics, axis=1, sort=False)
		res = res.reset_index(drop=False)
		res.rename(columns={"index": "id"}, inplace=True)
		print(res)

		# convert the result into spark dataframe
		spark_df = sqlContext.createDataFrame(res)

		# create or add to big dataframe that contains all components
		try:
			out = out.unionAll(spark_df)
		except NameError:
			out = spark_df

	return out
 def f29(self):
     return "ND"
     start = 0
     try:
         c_vals = nx.current_flow_closeness_centrality(self.G).values()
         total = len(c_vals)
         res = sum(c_vals) / total
     except nx.NetworkXError:
         res = "ND"
     stop = 0
     # self.feature_time.append(stop - start)
     return res
 def f29(self):
     return "ND"
     start = 0
     try:
         c_vals = nx.current_flow_closeness_centrality(self.G).values()
         total = len(c_vals)
         res = sum(c_vals) / total
     except nx.NetworkXError:
         res = "ND"
     stop = 0
     # self.feature_time.append(stop - start)
     return res
예제 #28
0
def summary(graph):
    """
    Summarize all the important graph topological properties
    :param graph:
    :return:
    """
    temp_dict = {
        'degree_centrality': nx.degree_centrality(graph),
        'betweeness': nx.betweenness_centrality(graph),
        'resistance_centrality': nx.current_flow_closeness_centrality(graph)
    }

    return temp_dict
예제 #29
0
def check_variation():
    for countn in [100]:
        t = []
        for countg in range(2000):
            # G = nx.generators.random_graphs.powerlaw_cluster_graph(countn ,1, 0.05)
            # G = nx.barabasi_albert_graph(200, 3)
            G = nx.erdos_renyi_graph(countn, 0.3)
            t.append(nx.current_flow_closeness_centrality(G))
            # t.append(get_egr(G))
        # meanegr.append(np.mean(t))
        # egrvar.append(np.var(t))

    plt.plot(t)
    plt.hist(t, bins=15)
def calculate_centrality(G):
	# dc_dumps = json.dumps(nx.degree_centrality(G).items(),sort_keys=True,indent=4)
	# dc_loads = json.loads(dc_dumps)
	dc_sorted = sorted(nx.degree_centrality(G).items(), key=itemgetter(0), reverse=True)
	bc_sorted = sorted(nx.betweenness_centrality(G).items(), key=itemgetter(0), reverse=True)
	clc_sorted = sorted(nx.closeness_centrality(G).items(), key=itemgetter(0), reverse=True)
	coc_sorted = sorted(nx.communicability_centrality(G).items(), key=itemgetter(0), reverse=True)
	lc_sorted = sorted(nx.load_centrality(G).items(), key=itemgetter(0), reverse=True)
	cfbc_sorted = sorted(nx.current_flow_betweenness_centrality(G).items(), key=itemgetter(0), reverse=True)
	cfcc_sorted = sorted(nx.current_flow_closeness_centrality(G).items(), key=itemgetter(0), reverse=True)
	# print ec_sorted[0]
	
	developer_centrality = []

	developer_file = file("public/wordpress/developer.json")
	developers = json.load(developer_file)
	for developer in developers:
		degree = 0
		betweenness = 0
		closeness = 0
		communicability = 0
		load = 0
		current_flow_betweenness = 0
		current_flow_closeness = 0
		for i in range (0, len(dc_sorted)):
			# if ( not dc_sorted[i][0] == bc_sorted[i][0] == clc_sorted[i][0] == coc_sorted[i][0] == lc_sorted[i][0] == cfbc_sorted[i][0]):
			# 	print 'false'
			if( developer['developer'] == dc_sorted[i][0]):
				degree = dc_sorted[i][1]
				betweenness = bc_sorted[i][1]
				closeness = clc_sorted[i][1]
				communicability = coc_sorted[i][1]
				load = lc_sorted[i][1]
				current_flow_betweenness = cfbc_sorted[i][1]
				current_flow_closeness = cfcc_sorted[i][1]

		developer_centrality.append({
			'name': developer['developer'],
		 	'degree': degree,
			'betweenness': betweenness,
			'closeness': closeness,
			'communicability': communicability,
			'load': load,
			'current_flow_betweenness': current_flow_betweenness,
			'current_flow_closeness':current_flow_closeness,
		 })

	return developer_centrality
def get_graph_metric(G, metric, extra_weighting_values="None"):
    """
    Function to get many of the graph theory metrics we are interested in
    using Networkx. Current metrics that can be obtained include:
    eigenvector centrality, weighted clustering, katz centrality,
    current flow closeness centrality, pagerank, closeness vitality
    :param G: graph for a team
    :param metric: what metric to analyze
    :param extra_weighting_values: if you want to additionally weight the output
    by some additional metric enter that metric here and will weight each by player
    by that players weighted value/ sum all weighted values
    :return: all_ec, output
    all_ec: a list for all players without labels for each player
    output: dict with each player as a key
    """
    if metric == "eigenvector_centrality":
        output = nx.eigenvector_centrality(G, weight='weight', max_iter=500)
    elif metric == "weighted_clustering":
        output = nx.algorithms.cluster.clustering(G, weight='weight')
    elif metric == "katz_centrality":
        output = nx.katz_centrality(G, max_iter=1000)
    elif metric == "current_flow_closeness_centrality":
        output = nx.current_flow_closeness_centrality(G, weight='weight')
    elif metric == "pagerank":
        output = nx.pagerank_numpy(G, weight='weight')
    elif metric == "closeness_vitality":
        output = nx.closeness_vitality(G, weight='weight')
    else:
        print('Need to give valid metric')
    if extra_weighting_values != "None":
        from_node = nx.get_node_attributes(G,extra_weighting_values)
        weighted_val = [from_node[i] for i in list(from_node)]
        weighted_val = weighted_val/np.sum(weighted_val)
    all_ec = []
    for j, i in enumerate(output.keys()):
        if extra_weighting_values != "None":
            all_ec.append(output[i] * weighted_val[j])
        else:
            all_ec.append(output[i])
    return all_ec, output
예제 #32
0
def node_current_flow_closeness_centrality(X):
    """
    based on networkx function: current_flow_closeness_centrality
    """
    XX = np.zeros((X.shape[0], np.sqrt(X.shape[1])))
    for i, value in enumerate(X):
        adj_mat = value.reshape((np.sqrt(len(value)), -1))
        adj_mat = (adj_mat - np.min(adj_mat)) / (np.max(adj_mat) -
                                                 np.min(adj_mat))
        adj_mat = 1 - adj_mat

        #        th = np.mean(adj_mat) - 0.05
        #        adj_mat = np.where(adj_mat < th, adj_mat, 0.)
        print(
            "\n========== Node current flow closeness centrality ==========\n")
        percent, th, adj_mat, triu = percentage_removed(adj_mat, 0.64)  #74
        print("percent = {0}, threshold position = {1}, threshold = {2}\n".
              format(percent, th, triu[th]))

        g = nx.from_numpy_matrix(adj_mat)
        print "Graph Nodes = {0}, Graph Edges = {1} ".format(
            g.number_of_nodes(), g.number_of_edges())
        print "\nEdge kept ratio, {0}".format(
            float(g.number_of_edges()) / ((g.number_of_nodes() *
                                           (g.number_of_nodes() - 1)) / 2))

        deg_cent = nx.current_flow_closeness_centrality(g)
        node_cent = np.zeros(g.number_of_nodes())

        for k in deg_cent:
            node_cent[k] = deg_cent[k]
        XX[i] = node_cent
        print "graph {0} => mean {1}, min {2}, max {3}".format(
            i, np.mean(XX[i]), np.min(XX[i]), np.max(XX[i]))
#    XX = XX*100
    ss = StandardScaler()
    XX = ss.fit_transform(XX.T).T

    return XX
 def ave_current_flow_closeness(self): #f-23
     d = nx.current_flow_closeness_centrality(self.ng)
     return sum(d.values())/float(self.N)
my_ns = Gu.nodes
print(len(my_ns))

if nx.is_connected(Gu):
    print('connected')
else:
    print('unconnected')

#Betweenness centrality
BFCN = nx.current_flow_betweenness_centrality(Gu, weight='inv_weight')
BCN = nx.betweenness_centrality(Gu, weight='inv_weight')
print('Done cf betweenness')

#Closeness centrality
CFCN = nx.current_flow_closeness_centrality(Gu, weight='inv_weight')
CCN = nx.closeness_centrality(Gu, distance='inv_weight')
print('Done cf closeness')

#PCN=nx.percolation_centrality(Gu, weight='inv_weight')

#Eigenvalue centrality
ECN = nx.eigenvector_centrality(Gu, max_iter=200, weight='weight')
print('Done eigenvalue')

DCN = nx.degree_centrality(Gu)
print('Done degree')

LCN = nx.load_centrality(Gu, weight='inv_weight')
print('Done load')
예제 #35
0
def current_flow_closeness_centrality(num_players, num_seeds, G):
	closeness = nx.current_flow_closeness_centrality(G)
	sorted_closeness = np.array(sorted(closeness.items(),key=itemgetter(1), reverse = True))
	return sorted_closeness[:num_seeds, 0]
예제 #36
0
plt.title('Spectral Layout', fontsize=20)
nx.draw_spectral(galexy_network,
                 labels=galexy_id2word,
                 font_family=font_name,
                 **option)
plt.subplot(224)
plt.title('Spring Layout', fontsize=20)
nx.draw_spring(galexy_network,
               labels=galexy_id2word,
               font_family=font_name,
               **option)

plt.show()

#Degree (연결중심성)
nx.degree_centrality(galexy_network)

#Eigenvector (위세 중심성)
nx.eigenvector_centrality(galexy_network, weight='weight')

#Closeness (근접 중심성)
nx.closeness_centrality(galexy_network, distance='weight')

#Current Flow Closeness (매개 중심성)
nx.current_flow_closeness_centrality(galexy_network)

#Current Flow Betweenness
nx.current_flow_betweenness_centrality(galexy_network)

#Communicability Betweenness
nx.communicability_betweenness_centrality(galexy_network)
예제 #37
0
# Use graphAdjMatrix() function to read in both 0-1 and weighted adjacency matrices
# Ensure that no 0-weighted edges are missed out
adjShape = np.shape(adjMatrix)
graph = graphAdjMatrix(adjMatrix[0:adjShape[1], ],
                       adjMatrix[adjShape[1]:adjShape[0], ])

# Degree Centrality
sorted(nx.degree_centrality(graph).items(),
       reverse=True,
       key=operator.itemgetter(1))
# Betweenness Centrality
sorted(nx.betweenness_centrality(graph, weight='weight').items(),
       reverse=True,
       key=operator.itemgetter(1))
# Current Flow Closeness Centrality
sorted(nx.current_flow_closeness_centrality(graph, weight='weight').items(),
       reverse=True,
       key=operator.itemgetter(1))
# Eigenvector Centrality
sorted(nx.eigenvector_centrality_numpy(graph, weight='weight').items(),
       reverse=True,
       key=operator.itemgetter(1))

# Read the coordinates from file
coordDF = pd.read_table('HW2_tsp.txt',
                        sep=' ',
                        header=None,
                        skiprows=10,
                        index_col=0,
                        names=["x", "y"])
예제 #38
0
def centralityAnalysis(graph, valueFilter, distanceFilter, \
                        out_file, centrality, selectedAtoms):
    """
    This function calculates various network (graph) centralities of a protein.

    This function calculates some network centrality measures such as
    degree, betweenness, closeness, current flow betweenness and eigenvector.
    This function needs Python 3.6 or later to maintain dictionary order.!!!

    Parameters
    ----------
    graph: object
        It is a Networkx Graph object.
    valueFilter: float
        The ccMatrix values lower than the valueFilter will be ignored.
    distanceFilter: float
        The distance values higher than the distanceFilter will be ignored
        and they will not be considered as edges in a network. 
        This kind of value pruning may work for low conformational change MD
        simulations or ENM based calculations. However, if there are large
        scale structural changes, it will be necessary to eliminate the edges 
        based on contacts and their preservation in during the entire simulation. 
    out_file: string
        Prefix of the output file. According to the centralty measure, it will be
        extended.
    centrality: string
        It can have 'degree', 'betweenness', 'closeness',
        'current_flow_betweenness', 'current_flow_closeness', 'eigenvector'
        or 'community'.
    selectedAtoms: object
        This is a prody.parsePDB object of typically CA atoms of a protein.

    Returns
    -------
    Nothing

    """
    # dynNetwork = buildDynamicsNetwork(ccMatrix, distanceMatrix, \
    #                                 valueFilter, distanceFilter,\
    #                                 selectedAtoms)

    n = selectedAtoms.numAtoms()
    ########################## Calculate degrees of all nodes
    if centrality == 'degree':
        degreeResult = graph.degree(weight='weight')
        degreeResultList = []
        for i in range(0, len(degreeResult)):
            degreeResultList.append(degreeResult[i])
        # print(degreeResultList)
        print("@> Degree calculation finished!")

        # open a file for degree
        degreeFile = open(
            f"{out_file}_degree_value_filter{valueFilter:.2f}.dat", "w")
        for i in range(n):
            #    print(str(i)+" "+(str(graph.degree(i, weight='weight'))))
            degreeFile.write("{0:d}\t{1:.6f}\t{2:s}\n".format(
                selectedAtoms[i].getResnum(), degreeResult[i],
                selectedAtoms[i].getChid()))
        degreeFile.close()
        projectCentralitiesOntoProteinVMD(centrality,
                                          degreeResultList,
                                          out_file,
                                          selectedAtoms,
                                          scalingFactor=1)
        projectCentralitiesOntoProteinPyMol(centrality,
                                            degreeResultList,
                                            out_file,
                                            selectedAtoms,
                                            scalingFactor=1)
        plotCentralities(centrality,
                         degreeResultList,
                         out_file,
                         selectedAtoms,
                         scalingFactor=1)

    ########################## Calculate betweenness
    elif centrality == 'betweenness':
        betweennessResult = nx.betweenness_centrality(graph,
                                                      k=None,
                                                      normalized=True,
                                                      weight='weight',
                                                      endpoints=False,
                                                      seed=None)
        print("@> Betweenness calculation finished!")
        # print(betweennessResult)

        # open a file for betweenness
        betweennessFile = open(
            f"{out_file}_betweenness_value_filter{valueFilter:.2f}.dat", "w")

        for i in range(n):
            betweennessFile.write("{0:d}\t{1:.6f}\t{2:s}\n".\
                format(selectedAtoms[i].getResnum(),
                        betweennessResult[i],
                        selectedAtoms[i].getChid()))
        betweennessFile.close()
        # print(list(betweennessResult.values()))
        # print(len(list(betweennessResult.values())))
        projectCentralitiesOntoProteinVMD(centrality,
                                          list(betweennessResult.values()),
                                          out_file,
                                          selectedAtoms,
                                          scalingFactor=1000)
        projectCentralitiesOntoProteinPyMol(centrality,
                                            list(betweennessResult.values()),
                                            out_file,
                                            selectedAtoms,
                                            scalingFactor=1000)
        plotCentralities(centrality,
                         list(betweennessResult.values()),
                         out_file,
                         selectedAtoms,
                         scalingFactor=1)

    ########################## Calculate closeness
    elif centrality == 'closeness':
        closenessResult = nx.closeness_centrality(graph,
                                                  u=None,
                                                  distance='weight')
        print("@> Closeness calculation finished!")

        # open a file for closeness
        closenessFile = open(
            out_file + "_closeness_value_filter" +
            "{:.2f}".format(valueFilter) + '.dat', "w")

        for i in range(n):
            #    print(str(i)+" "+(str(graph.closeness(i, weight='weight'))))
            closenessFile.write("{0:d}\t{1:.6f}\t{2:s}\n".format(
                selectedAtoms[i].getResnum(), closenessResult[i],
                selectedAtoms[i].getChid()))
        closenessFile.close()

        projectCentralitiesOntoProteinVMD(centrality,
                                          list(closenessResult.values()),
                                          out_file,
                                          selectedAtoms,
                                          scalingFactor=1)
        projectCentralitiesOntoProteinPyMol(centrality,
                                            list(closenessResult.values()),
                                            out_file,
                                            selectedAtoms,
                                            scalingFactor=1)
        plotCentralities(centrality,
                         list(closenessResult.values()),
                         out_file,
                         selectedAtoms,
                         scalingFactor=1)

    ########################## Calculate current_flow_betweenness
    elif centrality == 'current_flow_betweenness':
        current_flow_betweennessResult = nx.current_flow_betweenness_centrality(
            graph, normalized=True, weight='weight')

        print("@> Current flow betweenness calculation finished!")

        # open a file for current_flow betweenness
        current_flow_betweennessFile = open(
            f"{out_file}_current_flow_betweenness_value_filter{valueFilter:.2f}.dat",
            "w")

        for i in range(n):
            #    print(str(i)+" "+(str(graph.betweenness(i, weight='weight'))))
            current_flow_betweennessFile.write("{0:d}\t{1:.6f}\t{2:s}\n".\
                format(selectedAtoms[i].getResnum(),
                        current_flow_betweennessResult[i],
                        selectedAtoms[i].getChid()))
        current_flow_betweennessFile.close()

        projectCentralitiesOntoProteinVMD(
            centrality,
            list(current_flow_betweennessResult.values()),
            out_file,
            selectedAtoms,
            scalingFactor=1000)
        projectCentralitiesOntoProteinPyMol(
            centrality,
            list(current_flow_betweennessResult.values()),
            out_file,
            selectedAtoms,
            scalingFactor=1000)
        plotCentralities(centrality,
                         list(current_flow_betweennessResult.values()),
                         out_file,
                         selectedAtoms,
                         scalingFactor=1)
    ########################## Calculate closeness
    elif centrality == 'current_flow_closeness':
        current_flow_closenessResult = nx.current_flow_closeness_centrality(
            graph, weight='weight')

        print("@> Current flow closeness calculation finished!")

        # open a file for current_flow closeness
        current_flow_closenessFile = open(
            f"{out_file}_current_flow_closeness_value_filter{valueFilter:.2f}.dat",
            "w")

        for i in range(n):
            #    print(str(i)+" "+(str(graph.closeness(i, weight='weight'))))
            current_flow_closenessFile.write("{0:d}\t{1:.6f}\t{2:s}\n".\
                format(selectedAtoms[i].getResnum(),
                        current_flow_closenessResult[i],
                        selectedAtoms[i].getChid()))
        current_flow_closenessFile.close()

        projectCentralitiesOntoProteinVMD(
            centrality,
            list(current_flow_closenessResult.values()),
            out_file,
            selectedAtoms,
            scalingFactor=1000)
        projectCentralitiesOntoProteinPyMol(
            centrality,
            list(current_flow_closenessResult.values()),
            out_file,
            selectedAtoms,
            scalingFactor=1000)
        plotCentralities(centrality,
                         list(current_flow_closenessResult.values()),
                         out_file,
                         selectedAtoms,
                         scalingFactor=1)
    ########################## Calculate eigenvector centrality
    elif centrality == 'eigenvector':
        eigenvectorResult = nx.eigenvector_centrality_numpy(graph,
                                                            weight='weight')
        print("@> Eigenvector calculation finished!")

        # open a file for eigenvectors
        eigenvectorFile = open(
            f"{out_file}_eigenvector_value_filter{valueFilter:.2f}.dat", "w")

        for i in range(n):
            #    print(str(i)+" "+(str(graph.closeness(i, weight='weight'))))
            eigenvectorFile.write("{0:d}\t{1:.6f}\t{2:s}\n".format(
                selectedAtoms[i].getResnum(), eigenvectorResult[i],
                selectedAtoms[i].getChid()))
        eigenvectorFile.close()

        projectCentralitiesOntoProteinVMD(centrality,
                                          list(eigenvectorResult.values()),
                                          out_file,
                                          selectedAtoms,
                                          scalingFactor=1)
        projectCentralitiesOntoProteinPyMol(centrality,
                                            list(eigenvectorResult.values()),
                                            out_file,
                                            selectedAtoms,
                                            scalingFactor=1)
        plotCentralities(centrality,
                         list(eigenvectorResult.values()),
                         out_file,
                         selectedAtoms,
                         scalingFactor=1)
    ########################## Calculate communities with Girvan-Newman
    elif centrality == 'community':
        from networkx.algorithms import community
        communities = community.girvan_newman(graph)
        sortedCommunities = tuple(sorted(c) for c in next(communities))
        print("@> There are " + str(len(sortedCommunities)) + \
                " communities in your structure.")
        projectCommunitiesOntoProteinVMD(sortedCommunities, out_file,
                                         selectedAtoms)
        projectCommunitiesOntoProteinPyMol(sortedCommunities, out_file,
                                           selectedAtoms)
        print("@> Community calculation finished!")

    else:
        print("ERROR: Unknown centrality selected! It can only be")
        print("       'degree', 'betweenness', 'closeness',")
        print("       'current_flow_betweenness', 'current_flow_closeness'")
        print("       or 'eigenvector!'")
        sys.exit(-1)
예제 #39
0
파일: nodes.py 프로젝트: Behoston/biol_sys
def add_current_flow_closeness_node(graf):
    print "Adding current flow CLOSENESS to nodes"
    cfc_dict = nx.current_flow_closeness_centrality(graf)
    nx.set_node_attributes(graf, 'cfc', cfc_dict)
def flow_center(net):
    return distriCentra(nx.current_flow_closeness_centrality(net,normalized= True).values(),
                        nx.current_flow_closeness_centrality(star(net),normalized= True).values(),
                        'information')
예제 #41
0
nx.draw_networkx(graph,pos)
labels = nx.get_edge_attributes(graph,'weight')
nx.draw_networkx_edge_labels(graph, pos,edge_labels=labels)

#%%
#Top 2 nodes by betweenness centrality measure
betweenness = nx.betweenness_centrality(graph)
sorted(betweenness, key=betweenness.get, reverse=True)[:2]

## These two nodes are the two for which the highest sum of
## fractions of shortest paths for all pairs run through these
## nodes. Implying that they are "between" the most pairs, and
## so have a high importance within the network.

#%%
flowcloseness = nx.current_flow_closeness_centrality(graph)
sorted(flowcloseness, key=flowcloseness.get, reverse=True)[:2]

## Flow closeness centrality (similar to Information Centrality)
## takes into account not just the shortest paths but all possible
## paths, and identifies the nodes through which the highest amount
## of information (or current for electrical networks) is passed.
## In this case we get the two nodes 0 and 33, with the highest
## information flow centrality. This is similar to betweenness
## but node 0 has higher centrality when you only take into account
## shortest paths, and node 33 has higher centrality when you take
## into account all paths (which may be more robust).

#%%
eigenvector = nx.eigenvector_centrality(graph)
sorted(eigenvector, key=eigenvector.get, reverse=True)[:2]
예제 #42
0
## These two nodes are the two for which the highest sum of
## fractions of shortest paths for all pairs run through these
## nodes. Implying that they are "between" the most pairs, and
## so have a high importance within the network.

## The reason we use (weight = None) in the function and calculate
## the betweenness centrality for an "unweighted graph" is that
## the edge weights represent the friendship strength between two nodes.
## The shortest path in this case will be defined as the "least friendly path"
## between two nodes. Hence we calculate the betweenness
## centrality based on unweighted graph. In this case, the shortest path will be
## the "shortest route" to reach a person via friends (regardless of the friendship
## strength)

#%%
flowcloseness = nx.current_flow_closeness_centrality(graph, weight='weight')
sorted(flowcloseness, key=flowcloseness.get, reverse=True)[:2]
# sorted(nx.current_flow_closeness_centrality(graph, weight = 'weight').items(), reverse = True, key = operator.itemgetter(1))

## Flow closeness centrality (similar to Information Centrality)
## takes into account not just the shortest paths but all possible
## paths, and identifies the nodes through which the highest amount
## of information (or current for electrical networks) is passed.
## In this case we get the two nodes 0 and 33, with the highest
## information flow centrality. This is similar to betweenness
## but node 0 has higher centrality when you only take into account
## shortest paths, and node 33 has higher centrality when you take
## into account all paths (which may be more robust).

#%%
eigenvector = nx.eigenvector_centrality(graph, weight='weight')
예제 #43
0
    def centrality(self):
        result = {}
        result['degree_centrality'] = nx.degree_centrality(self.graph)

        if self.directed == 'directed':
            result['in_degree_centrality'] = nx.in_degree_centrality(
                self.graph)
            result['out_degree_centrality'] = nx.out_degree_centrality(
                self.graph)

        result['closeness_centrality'] = nx.closeness_centrality(self.graph)
        result['betweenness_centrality'] = nx.betweenness_centrality(
            self.graph)

        # fix the tuple cant decode into json problem
        stringify_temp = {}
        temp = nx.edge_betweenness_centrality(self.graph)
        for key in temp.keys():
            stringify_temp[str(key)] = temp[key]
        result['edge_betweenness_centrality'] = stringify_temp

        if self.directed == 'undirected':
            result[
                'current_flow_closeness_centrality'] = nx.current_flow_closeness_centrality(
                    self.graph)
            result[
                'current_flow_betweenness_centrality'] = nx.current_flow_betweenness_centrality(
                    self.graph)

            stringify_temp = {}
            temp = nx.edge_current_flow_betweenness_centrality(self.graph)
            for key in temp.keys():
                stringify_temp[str(key)] = temp[key]
            result['edge_current_flow_betweenness_centrality'] = stringify_temp

            result[
                'approximate_current_flow_betweenness_centrality'] = nx.approximate_current_flow_betweenness_centrality(
                    self.graph)
            result['eigenvector_centrality'] = nx.eigenvector_centrality(
                self.graph)
            result[
                'eigenvector_centrality_numpy'] = nx.eigenvector_centrality_numpy(
                    self.graph)
            result['katz_centrality'] = nx.katz_centrality(self.graph)
            result['katz_centrality_numpy'] = nx.katz_centrality_numpy(
                self.graph)
            result['communicability'] = nx.communicability(self.graph)
            result['communicability_exp'] = nx.communicability_exp(self.graph)
            result[
                'communicability_centrality'] = nx.communicability_centrality(
                    self.graph)
            result[
                'communicability_centrality_exp'] = nx.communicability_centrality_exp(
                    self.graph)
            result[
                'communicability_betweenness_centrality'] = nx.communicability_betweenness_centrality(
                    self.graph)
            result['estrada_index'] = nx.estrada_index(self.graph)

        result['load_centrality'] = nx.load_centrality(self.graph)

        stringify_temp = {}
        temp = nx.edge_load(self.graph)
        for key in temp.keys():
            stringify_temp[str(key)] = temp[key]
        result['edge_load'] = stringify_temp
        result['dispersion'] = nx.dispersion(self.graph)

        fname_centra = self.DIR + '/centrality.json'
        with open(fname_centra, "w") as f:
            json.dump(result, f, cls=SetEncoder, indent=2)
        print(fname_centra)
예제 #44
0
import networkx as nx
import plot_multigraph
import matplotlib.pylab as plt
from matplotlib import pylab as plt

n = 80
p = 10. / n
G = nx.fast_gnp_random_graph(n, p, seed=42)

def to_list(dict_):
  return [dict_[k] for k in G.nodes()]

graph_colors = [
  ("degree", to_list(nx.degree_centrality(G))),
  ("betweenness", to_list(nx.betweenness_centrality(G))),
  ("load", to_list(nx.load_centrality(G))),
  ("eigenvector", to_list(nx.eigenvector_centrality_numpy(G))),
  ("closeness_centrality", to_list(nx.closeness_centrality(G))),
  ("current_flow_closeness", to_list(nx.current_flow_closeness_centrality(G))),
  ("current_flow_betweenness", to_list(nx.current_flow_betweenness_centrality(G))),
  ("katz", to_list(nx.katz_centrality_numpy(G))),
  ("communicability", to_list(nx.communicability_centrality(G))),
]

fig = plot_multigraph.plot_color_multigraph(G, graph_colors, 3, 3, node_size=50)
plt.savefig('graphs/centrality.png', facecolor=fig.get_facecolor())
예제 #45
0
def create(G,t_num, inc=10,initial=10):
    
    G2 = nx.Graph()
    
    es = grow2(G2,G,inc)
    
    y1 = []
    y0 = []
    
    data = []
    
    for t in range(t_num):
        print t
        G1 = G2.copy()
        
        es = grow2(G2,G,initial)
        
        if es:
            C = nx.current_flow_closeness_centrality(G1)
            pr = nx.pagerank_numpy(G1)
            M = mod(G1)
            K = katz(G1)
            cl = nx.closeness_centrality(G1)
            
            nodes = G2.nodes()
            
            link = []
            no_link = []
            
            for i in range(0,len(nodes)):
                for j in range(i+1,len(nodes)):
                    
                    #print i,j
                    
                    a = nodes[i]
                    b = nodes[j] 
                    
                    if a in G1 and b in G1 and nx.has_path(G1,a,b) and nx.shortest_path_length(G1,a,b) < 7:
                        
                        features = zeros(11)
                        
                        if nx.has_path(G1,a,b): features[0] = nx.shortest_path_length(G1,a,b)
                        features[1] = min(C[a],C[b])
                        features[2] = max(C[a],C[b])
                        features[3] = jaccard(G1,a,b)
                        features[4] = M[G1.nodes().index(a),G1.nodes().index(b)]
                        features[5] = K[G1.nodes().index(a),G1.nodes().index(b)]
                        features[6] = min(pr[a],pr[b])
                        features[7] = max(pr[a],pr[b])
                        features[8] = max(cl[a],cl[b])
                        features[9] = min(cl[a],cl[b])
                        features[10] = adamic_adar(G1,a,b)
                        
                        if b in G2.neighbors(a):
                            if b not in G1.neighbors(a):             
                                link += [[a,b,features]]
                        else:
                            no_link += [[a,b,features]]
            
            data.append((link,no_link)) 

    return data
예제 #46
0
for t in range(20):
    
    y2 = [[],[]]
    y3 = [[],[]]
    y4 = [[],[]]
    pry = [[],[]]
    
    G1 = G2.copy()
    
    es = grow2(G2,G,100)
    print "es",len(es)
    
    if es:
        print "flow"
        C = nx.current_flow_closeness_centrality(G1)
        print "pr"
        #pr = nx.pagerank_numpy(G1)
        M = katz(G1)
        print "pr done", M.shape
    
        a1 =0
        b1 =0
        
        nodes = G2.nodes()
        
        for i in range(0,len(nodes)):
            for j in range(i+1,len(nodes)):
                
                print i,j
                
예제 #47
0
metros, mg, nodedata = importnetwork(year)

#Compute statistics
stats = metros

#Degree
degree = mg.degree()
stats = dict2column(stats, degree, 'degree')

#Weighted Degree
wdegree = mg.degree(weight = 'exmptgross')
stats = dict2column(stats, wdegree, 'wdegree')

#Current Closeness Centrality 
flowcloseness = nx.current_flow_closeness_centrality(mg, weight = 'exmptgross')
stats = dict2column(stats, flowcloseness, 'flowcloseness')

#Vertex Closeness
closeness = nx.closeness_centrality(mg)
stats = dict2column(stats, closeness, 'closeness')

#Vertex Betweenness
btwnness = nx.betweenness_centrality(mg)
stats = dict2column(stats, btwnness, 'btwnness')

#Current Betweenness
flowbtwnness = nx.current_flow_betweenness_centrality(mg, weight = 'exmptgross')
stats = dict2column(stats, flowbtwnness, 'flowbtwnness')

#Eigenvector Centrality
예제 #48
0
# 数值相似性
# nx.numeric_assortativity_coefficient()
# print(deg_ass)

# 衡量点较好指标
# 节点度的中心性 节点的度中心性=度÷{图n-1最大可能的度}, n是图的节点数量。
# deg_ctl=nx.degree_centrality(G)
# print(deg_ctl)

#  节点紧密度 节点和图中其它节点之间最短路径的平均值。
# clo_ctl=nx.closeness_centrality(G)
# print(clo_ctl)

# 衡量点(流量中心)较好指标
# 当前流量中心度 把边当成电阻,节点是电阻之间的节点。(两点之间相连,有多少流量经过该节点)
clo_flow_ctl = nx.current_flow_closeness_centrality(G)
print(sum(clo_flow_ctl.values()))

# print(max(clo_flow_ctl,key=lambda x:clo_flow_ctl[x]))
# print(max(clo_flow_ctl,key=clo_flow_ctl.get))
print(sorted(clo_flow_ctl, key=lambda x: clo_flow_ctl[x]))

# 关系中心
com_centre = nx.communicability_betweenness_centrality(G)

# print(com_centre)
print(sum(com_centre.values()))
print(sorted(com_centre, key=lambda x: com_centre[x]))

# 链路分析
# 网页排名 根据节点的度计算节点的排名
    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