-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcentrality.py
75 lines (53 loc) · 2.14 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
71
72
73
74
75
import numpy as np
import networkx as nx
import torch
def eigen_centrality(data):
"""Calculates the eigenvector centrality for the given graph
Parameters:
data (np.array): Adjacency matrix representation of the graph
Returns:
np.array: Array of eigenvector centralities
"""
g = nx.from_numpy_matrix(data)
# # compute egeinvector centrality and transform the output to vector
ec = nx.eigenvector_centrality_numpy(g, weight="weight")
eigenvector_centralities = np.array([ec[g] for g in ec])
return eigenvector_centralities
def pagerank_centrality(data):
"""Calculates the pagerank centrality for the given graph
Parameters:
data (np.array): Adjacency matrix representation of the graph
Returns:
np.array: Array of pagerank centralities
"""
g = nx.from_numpy_matrix(data)
# # compute egeinvector centrality and transform the output to vector
cc = nx.pagerank(g, weight="weight")
pagerank_centralities = np.array([cc[g] for g in cc])
return pagerank_centralities
def betweenness_centrality(data):
"""Calculates the betweenness centrality for the given graph
Parameters:
data (np.array): Adjacency matrix representation of the graph
Returns:
np.array: Array of betweenness centralities
"""
g = nx.from_numpy_matrix(data)
# # compute egeinvector centrality and transform the output to vector
bc = nx.betweenness_centrality(g, weight="weight")
betweenness_centralities = np.array([bc[g] for g in bc])
return betweenness_centralities
def topological_measures(data):
"""Returns the topological measures for the given graph
Parameters:
data (np.array): Adjacency matrix representation of the graph
Returns:
np.array: Array with first element pagerank centrality,
second element betweenness centrality
third element eigenvector centrality
"""
topology = []
topology.append(torch.tensor(pagerank_centrality(data))) # 0
topology.append(torch.tensor(betweenness_centrality(data))) # 1
topology.append(torch.tensor(eigen_centrality(data))) # 2
return topology