-
Notifications
You must be signed in to change notification settings - Fork 0
/
look_at_louvain.py
152 lines (127 loc) · 4.59 KB
/
look_at_louvain.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
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
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import zen
from zen.algorithms.community import louvain
def katz(G,tol=0.01,max_iter=1000,alpha=0.001,beta=1):
iteration = 0
centrality = np.zeros(G.num_nodes)
while iteration < max_iter:
iteration += 1 # increment iteration count
centrality_old = centrality.copy()
for node in G.nodes_():
Ax = 0
for neighbor in G.neighbors_(node):
weight = G.weight_(G.edge_idx_(neighbor,node))
Ax += np.multiply(centrality[neighbor],weight)
#Ax += centrality[neighbor] #exclude weight due to overflow in multiplication
centrality[node] = np.multiply(alpha,Ax)+beta
if np.sum(np.abs(np.subtract(centrality,centrality_old))) < tol:
return centrality
def modular_graph(Size1, Size2, edges1, edges2, common, katz_alpha=0.001):
g1 = zen.generating.barabasi_albert(Size1,edges1)
avgDeg1 = (2.0 * g1.num_edges)/g1.num_nodes
#lcc1 = np.mean(zen.algorithms.clustering.lcc_(g1))
g2 = zen.generating.barabasi_albert(Size2,edges2)
avgDeg2 = (2.0 * g2.num_edges)/g2.num_nodes
#lcc2 = np.mean(zen.algorithms.clustering.lcc_(g2))
Size = Size1 + Size2
G = zen.Graph()
for i in range(Size):
G.add_node(i)
for edge in g1.edges_iter():
u = edge[0]
v = edge[1]
G.add_edge(u,v)
for edge in g2.edges_iter():
u = edge[0]+Size1
v = edge[1]+Size1
G.add_edge(u,v)
# Select random pairs of nodes to connect the subgraphs
join_nodes = np.empty((common,2),dtype=np.int64)
nodes1 = np.random.randint(0,Size1,size=common)
nodes2 = np.random.randint(Size1,Size,size=common)
join_nodes[:,0] = nodes1
join_nodes[:,1] = nodes2
for edge in join_nodes:
if not G.has_edge(edge[0],edge[1]):
G.add_edge(edge[0],edge[1])
return G
def modularity(G,classDict,classList):
Q = zen.algorithms.modularity(G,classDict)
# Maximum Modularity
count=0.0
for e in G.edges():
n1 = G.node_idx(e[0])
n2 = G.node_idx(e[1])
if classList[n1] == classList[n2]:
count += 1
same = count / G.num_edges
rand = same - Q
qmax = 1 - rand
return Q, qmax
def main(exists=False):
if not exists:
G_synth = modular_graph(500,500,2,8,100,katz_alpha=1e-4)
zen.io.gml.write(G_synth,'adhoc.gml')
else:
G_synth = zen.io.gml.read('adhoc.gml')
print "Nodes: %d"%G_synth.num_nodes
print "Edges: %d"%G_synth.num_edges
cset = louvain(G_synth)
comm_dict = {}
comm_list = np.zeros(G_synth.num_nodes)
for i, community in enumerate(cset.communities()):
comm_dict[i] = community.nodes()
comm_list[community.nodes_()]=i
q,qmax = modularity(G_synth,comm_dict,comm_list)
print '%d communities found.'%(i+1)
print 'Q: %.3f'%q
print 'Qmax: %.3f'%qmax
print 'Normalized Q: %.3f'%(q/qmax)
evc = zen.algorithms.eigenvector_centrality_(G_synth)
evc = evc - np.min(evc)
evc = evc / np.max(evc)
kc = katz(G_synth,alpha=1e-4)
kc = kc - np.min(kc)
kc = kc / np.max(kc)
GROUP = [1,2,4,5]
fig = plt.plot(figsize=(12,8))
for i,com in enumerate(cset.communities()):
if i+1 in GROUP:
nodes = com.nodes_()
plt.scatter(evc[nodes],kc[nodes],s=7,label='%d'%(i+1))
plt.xlabel('Eigenvector centrality (normalized)',fontsize=14)
plt.ylabel('Katz centrality (normalized)',fontsize=14)
plt.legend()
plt.xlim([-0.04,1])
plt.ylim([-0.04,1])
plt.savefig('figures/louvain1.eps',bbox_inches='tight')
plt.close()
fig = plt.plot(figsize=(12,8))
for i,com in enumerate(cset.communities()):
if i+1 not in GROUP:
nodes = com.nodes_()
plt.scatter(evc[nodes],kc[nodes],s=7,label='%d'%(i+1))
plt.xlabel('Eigenvector centrality (normalized)',fontsize=14)
plt.ylabel('Katz centrality (normalized)',fontsize=14)
plt.legend()
plt.savefig('figures/louvain2.eps',bbox_inches='tight')
idx = np.where(evc>0.025)[0]
nodes = G_synth.nodes_()
c1 = nodes[idx]
c2 = nodes[~idx]
ClassDict = {}
ClassDict[0] = [G_synth.node_object(x) for x in c1]
ClassDict[1] = [G_synth.node_object(x) for x in c2]
ClassList = np.zeros(G_synth.num_nodes)
ClassList[c2] = 1
q,qmax = modularity(G_synth,ClassDict,ClassList)
print "manual community assignment:"
print q
print qmax
print q/qmax
if __name__ == '__main__':
main(exists=True)