Repository navigation
Expand file tree
/
Copy pathFPPM.py
More file actions
286 lines (258 loc) · 7.69 KB
/
Copy pathFPPM.py
File metadata and controls
286 lines (258 loc) · 7.69 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import numpy as np
import networkx as nx
from copy import deepcopy
from collections import defaultdict, deque
from scipy.cluster.hierarchy import average, to_tree
def diag_matrix(X):
'''
Get the diagonal matrix of X.
Parameters
----------
X : 2d numpy.ndarray
A matrix.
Returns
-------
return : 2d numpy.ndarray
The diagonal matrix of the input matrix.
'''
return np.diag(np.diag(X))
def normalize(X):
'''
Translate X to Y where Y[i, j] = X[i, j] / sum(X[i, :]).
The sum of any row of X can't be 0.
Parameters
----------
X : 2d numpy.ndarray
The input matrix.
Returns
-------
return : 2d numpy.ndarray
The normalised matrix that the sum of any row is 1.
'''
row_sum = X.sum(axis=1).reshape(-1, 1)
return X / row_sum
def transition_matrix(G):
'''
TODO
Parameters
----------
G : networkx.Graph
A graph.
Returns
-------
T : 2d numpy.ndarray
The transition matrix.
'''
T = []
for n in G:
row = []
nbrs_n = set(G.neighbors(n))
for m in G:
if m in nbrs_n:
nbrs_m = set(G.neighbors(m))
common_nbr = len(nbrs_n.intersection(nbrs_m)) + 1
row.append(common_nbr)
else:
row.append(0)
T.append(row)
return normalize(np.array(T))
def first_passage_prob(T, n):
'''
Calculate and yield the i-step first passage probabilities.
i is smaller than or equal to n.
Parameters
----------
T : 2d numpy.ndarray
The transition matrix.
n : int
The number of steps
'''
F = T.copy()
yield F
for _ in range(1, n):
F = T.dot(F - diag_matrix(F))
yield F
def hierarchical_clustering(similarity):
'''
Yield partitions by hierarchical clustering with average similarity.
Parameters
----------
similarity : 2d numpy.ndarray
The similarity matrix.
'''
# m, _ = similarity.shape
# communities = [{i} for i in range(m)]
# similarity = similarity - diag_matrix(similarity)
# yield communities
# while len(communities) > 2:
# max_idx = np.argmax(similarity)
# max_idx = np.unravel_index(max_idx, similarity.shape)
# i, j = min(max_idx), max(max_idx)
# similarity = np.delete(similarity, j, axis=1)
# similarity = np.delete(similarity, i, axis=1)
# ri = similarity[i, :]
# rj = similarity[j, :]
# similarity = np.delete(similarity, j, axis=0)
# similarity = np.delete(similarity, i, axis=0)
# cj = communities.pop(j)
# ci = communities.pop(i)
# communities.append(ci.union(cj))
# li, lj = len(ci), len(cj)
# rn = (li * ri + lj * rj) / (li + lj)
# similarity = np.vstack((similarity, rn))
# rn = np.concatenate((rn, [0])).reshape(-1, 1)
# similarity = np.hstack((similarity, rn))
# yield communities
m, _ = similarity.shape
similarity = np.array([similarity[i, j] for i in range(m) for j in range(i+1, m)])
distance = 1 - similarity
dendrogram = average(distance)
_, nodelist = to_tree(dendrogram, rd=True)
id2vertices = {i:{i} for i in range(m)}
yield id2vertices.values()
for _ in range(m, 2 * m - 1):
node = nodelist[_]
left = node.left.id
right = node.right.id
new_community = id2vertices[left].union(id2vertices[right])
id2vertices[node.id] = new_community
del id2vertices[left]
del id2vertices[right]
yield id2vertices.values()
def select_by_Q(dendrogram, G):
'''
Select the partition with the maximal modularity from the dendrogram.
Parameters
----------
dendrogram : generator
A generator of partitions from fine to coarse.
G : networkx.Graph
A graph.
Returns
-------
return : list
A community partition.
'''
best = None
maxQ = -1
for partition in dendrogram:
Q = nx.community.modularity(G, partition)
if Q > maxQ:
maxQ = Q
best = [_ for _ in partition]
return best
def remove_small_communities(partition, G, S, theta):
'''
Merge small communities into big communities.
Parameters
----------
partition : set
A set of communities.
G : networkx.Graph
A graph.
S : 2d numpy.ndarray
The similarity matrix.
theta : int
The minimal size of communities.
Returns
-------
return : list
The ith element is the label of vertex i.
'''
labels = {}
for idx, community in enumerate(partition):
for vertex in community:
labels[vertex] = idx
small_communities = deque()
large_labels = set()
for community in partition:
if len(community) >= theta:
some_vertex = next(iter(community))
large_labels.add(labels[some_vertex])
else:
small_communities.append(community)
while small_communities:
community = small_communities.popleft()
relevance = defaultdict(lambda: 0)
for v in community:
for _ in G.neighbors(v):
if _ not in community and labels[_] in large_labels:
relevance[labels[_]] += S[v, _]
if relevance:
mergeto = max(relevance, key=relevance.get)
for v in community:
labels[v] = mergeto
else:
small_communities.append(community)
return [labels[_] for _ in range(len(G))]
def FPPM(G, theta=3):
'''
FPPM to detect communities.
Parameters
----------
G : networkx.Graph
A graph
theta : int, optional
The minimal size of communities.
Returns
-------
return : list
The ith element is the label of vertex i.
'''
d = nx.diameter(G)
if d <= 3:
ds = {2}
else:
ds = set(range(2, d))
T = transition_matrix(G)
S = np.zeros(T.shape)
for idx, F in enumerate(first_passage_prob(T, max(ds)+1)):
S += np.corrcoef(F)
S /= len(ds)
dendrogram = hierarchical_clustering(S)
partition_Q = select_by_Q(dendrogram, G)
partition = remove_small_communities(partition_Q, G, S, theta)
return partition
def FPPM_repeat(G, theta=3, repeat=100):
'''
FPPM to detect communities with repeat. Because the calculation of
vertex similarities, hierarchical clustering and selecting th partition
with the maximal modularity from the dendrogram are fixed and the final
partition can be influenced by the ordering of small communities in the
processing of removing small communities. Only removig small communities
are repeated.
Parameters
----------
G : networkx.Graph
A graph
theta : int, optional
The minimal size of communities, default to be 3.
repeat : int, option
The repeat times, default to be 100.
Returns
-------
return : list
The ith element is the label of vertex i.
'''
d = nx.diameter(G)
if d <= 3:
ds = {2}
else:
ds = set(range(2, d))
T = transition_matrix(G)
S = np.zeros(T.shape)
for idx, F in enumerate(first_passage_prob(T, max(ds)+1)):
S += np.corrcoef(F)
S /= len(ds)
dendrogram = hierarchical_clustering(S)
partition_Q = select_by_Q(dendrogram, G)
partitions = []
for _ in range(repeat):
partitions.append(remove_small_communities(partition_Q, G, S, theta))
return partitions
if __name__ == '__main__':
import matplotlib.pyplot as plt
G = nx.karate_club_graph()
partition = FPPM(G)
nx.draw(G, node_color=partition, with_labels=True)
plt.show()