-
Notifications
You must be signed in to change notification settings - Fork 1
/
centrality.py
executable file
·71 lines (43 loc) · 1.75 KB
/
centrality.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
import networkx as nx
# put it back into a 2D symmetric array
def topological_measures(data):
# ROI is the number of brain regions (i.e.,35 in our case)
ROI = 160
topology = []
# A = to_2d(data)
np.fill_diagonal(data, 0)
# create a graph from similarity matrix
G = nx.from_numpy_matrix(np.absolute(data))
U = G.to_undirected()
# Centrality #
# compute closeness centrality and transform the output to vector
cc = nx.closeness_centrality(U, distance="weight")
closeness_centrality = np.array([cc[g] for g in U])
# compute betweeness centrality and transform the output to vector
# bc = nx.betweenness_centrality(U, weight='weight')
# bc = (nx.betweenness_centrality(U))
betweenness_centrality = np.array([cc[g] for g in U])
# # compute egeinvector centrality and transform the output to vector
ec = nx.eigenvector_centrality_numpy(U)
eigenvector_centrality = np.array([ec[g] for g in U])
topology.append(closeness_centrality) # 0
topology.append(betweenness_centrality) # 1
topology.append(eigenvector_centrality) # 2
return topology
# put it back into a 2D symmetric array
def eigen_centrality(data):
# ROI is the number of brain regions (i.e.,35 in our case)
ROI = 160
topology_eigen = []
# A = to_2d(data)
np.fill_diagonal(data, 0)
# create a graph from similarity matrix
G = nx.from_numpy_matrix(np.absolute(data))
U = G.to_undirected()
# Centrality #
# # compute egeinvector centrality and transform the output to vector
ec = nx.eigenvector_centrality_numpy(U)
eigenvector_centrality = np.array([ec[g] for g in U])
topology_eigen.append(eigenvector_centrality) # 2
return topology_eigen