-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunity1.py
78 lines (53 loc) · 1.82 KB
/
community1.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
76
77
78
import numpy as np
import community as lvn
from networkx import convert_node_labels_to_integers
def list_subgraphs(communities):
"""
Returns a list of the indices of the nodes within a communities.
"""
graph_communities = []
for community_index in set(communities.values()):
node_list = [node for node in communities.keys()
if communities[node] == community_index]
graph_communities.append(node_list)
return graph_communities
def labeled_communities(communities, labels):
"""
Replaces the integer indices in a partition specification with the
appropriate node labels.
"""
subgraph_labels = []
for i in range(len(communities)):
subgraph_labels.append([lbl for j,lbl in enumerate(labels)
if j in communities[i]])
return subgraph_labels
def create_Ag(A, node_list):
"""
Returns (nxn) matrix containing only the edges which are relevant for the
nodes given in node_list.
"""
Ag1 = A.copy()
Ag2 = Ag1.tocsr()[node_list,:]
Ag = Ag2.tocsc()[:,node_list]
return Ag
def create_Ags(A, communities):
"""
Returns (mxnixni) Ags for a given NxN matrix and communities.
Note: only needs to be done once per community specification.
"""
Ags = []
for node_list in communities:
Ags.append(create_Ag(A, node_list))
return Ags
def louvain(G):
"""
Compute the communities of the graph G according to the Louvain algorithm.
Wrapper function for community.best_partition().
"""
return lvn.best_partition(G)
def scrub_graph(G):
"""
Converts the graph to one with integer indices, and provides the labels in a
separate array.
"""
return convert_node_labels_to_integers(G), G.nodes