Skip to content

Commit

Permalink
Other modularity and community detection test (#21)
Browse files Browse the repository at this point in the history
* Create synchronous_modularity.py

Label propagation algorithm test.

* Create Louvain_modularity.py

Louvain algorithm test.
  • Loading branch information
wyim-pgl authored Dec 10, 2020
1 parent ad5f275 commit 7af0d0b
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 0 deletions.
68 changes: 68 additions & 0 deletions script/Louvain_modularity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

""" Prototype of the Louvain algorithm method. Implemented based on
Fast unfolding of communities in large networks, Vincent D et al., Journal of Statistical Mechanics: Theory and Experiment(2008)
"""


import sys
import numpy as np
import networkx as nx
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import community as community_louvain
import itertools
from numpy.linalg import eigh
xrange=range

def partition(G, refinement=True):
partition = community_louvain.best_partition(G)

# draw the graph
pos = nx.spring_layout(G)
# color the nodes according to their partition
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=40, cmap=cmap, node_color=list(partition.values()))
nx.draw_networkx_edges(G, pos, alpha=0.5)
plt.savefig("network_louvain.png")
plt.gca().clear()



def main(arg):
""" Main entry
"""
fp = open(arg)
G = nx.Graph()
for row in fp:
if row[0] == '%': # Comments
continue
if len(row.split()) == 2:
a, b = row.split()
G.add_edge(a, b)
else:
a, b, w = row.split()
w = int(w)
G.add_edge(a, b, weight=w)

print (G.edges())
s = partition(G, refinement=True)
# for node, part in zip(itertools.repeat(G.nodes(), s)):
# print (node, part)
pos = nx.spring_layout(G)
nx.draw(G, pos, node_color=range(G.number_of_nodes()),
cmap=plt.cm.Blues)
plt.savefig("graph.png")
plt.gca().clear()
nx.draw(G, pos, node_color=s, cmap=plt.cm.jet)
plt.savefig("graph.partitioned.png")

if __name__ == '__main__':
if len(sys.argv) != 2:
print ("Usage:", file=sys.stderr)
print ("\t{} karate/out.ucidata-zachary".format(sys.argv[0]), file=sys.stderr)
print ("\t{} rice/out".format(sys.argv[0]), file=sys.stderr)
sys.exit(1)

main(sys.argv[1])
Binary file added script/Semi-synchronous.partitioned.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added script/graph.partitioned.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added script/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added script/network_louvain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added script/network_visualization/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added script/network_visualization/network_louvain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions script/network_visualization/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

71 changes: 71 additions & 0 deletions script/synchronous_modularity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

""" Prototype of the Label propagation algorithm for community detection based on belows
[1] Raghavan, Usha Nandini, Réka Albert, and Soundar Kumara. "Near linear time algorithm to detect community structures in large-scale networks." Physical review E 76.3 (2007): 036106.
[2] Cordasco, Gennaro, and Luisa Gargano. "Community detection via semi-synchronous label propagation algorithms." 2010 IEEE International Workshop on: Business Applications of Social Network Analysis (BASNA). IEEE, 2010.
"""


import sys
import numpy as np
import networkx as nx
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import itertools
xrange=range

def partition(G):
#c_iter = community.asyn_lpa_communities(G=G, weight=None) # Asynchronous
c_iter = nx.algorithms.community.label_propagation_communities(G=G) # Semi-synchronous
max_k_w = []
for c in c_iter:
max_k_w += [c]
community_num_group = len(max_k_w)
color_list_community = [[] for i in range(len(G.nodes()))]
for i in range(len(G.nodes())):
for j in range(community_num_group):
if i in max_k_w[j]:
color_list_community[i] = j
fig = plt.figure()
edges = G.edges()
weights = [G[u][v]['weight'] for u, v in edges]

def main(arg):
""" Main entry
"""
fp = open(arg)
G = nx.Graph()
for row in fp:
if row[0] == '%': # Comments
continue
if len(row.split()) == 2:
a, b = row.split()
G.add_edge(a, b)
else:
a, b, w = row.split()
w = int(w)
G.add_edge(a, b, weight=w)

print (G.edges())
s = partition(G)
node_size = 5
pos = nx.spring_layout(G)
s = partition(G)
nx.draw(G, pos, node_color=s, cmap=plt.cm.Blues)
#nx.draw_networkx_nodes(G, pos, node_size=node_size, cmap='jet', vmin=0, vmax=max(color_list_community))
#nx.draw_networkx_edges(G, pos, width=1)
#nx.draw_networkx_labels(G, pos, font_size=1, font_color="black")
#plt.xticks([])
#plt.yticks([])
plt.savefig("Semi-synchronous.partitioned.png")
plt.gca().clear()

if __name__ == '__main__':
if len(sys.argv) != 2:
print ("Usage:", file=sys.stderr)
print ("\t{} karate/out.ucidata-zachary".format(sys.argv[0]), file=sys.stderr)
print ("\t{} rice/out".format(sys.argv[0]), file=sys.stderr)
sys.exit(1)

main(sys.argv[1])

0 comments on commit 7af0d0b

Please sign in to comment.