This module implements community detection.
It uses the louvain method described in Fast unfolding of communities in large networks, Vincent D Blondel, Jean-Loup Guillaume, Renaud Lambiotte, Renaud Lefebvre, Journal of Statistical Mechanics: Theory and Experiment 2008(10), P10008 (12pp)
It depends on Networkx to handle graph operations : http://networkx.lanl.gov/
The program can be found in a repository where you can also report bugs :
https://bitbucket.org/taynaud/python-louvainYou should consider using the cpp version at http://findcommunities.googlepages.com/ !
./community.py file.bin > tree
where file.bin is a binary graph as generated by the convert utility of the cpp version.
You can after that use the generated file with the hierarchy utility of the cpp version. Note that the program does not make much verifications about the arguments, and is expecting a friendly use.
import community
import networkx as nx
import matplotlib.pyplot as plt
#better with karate_graph() as defined in networkx example.
#erdos renyi don't have true community structure
G = nx.erdos_renyi_graph(30, 0.05)
#first compute the best partition
partition = community.best_partition(G)
#drawing
size = float(len(set(partition.values())))
pos = nx.spring_layout(G)
count = 0.
for com in set(partition.values()) :
count = count + 1.
list_nodes = [nodes for nodes in partition.keys()
if partition[nodes] == com]
nx.draw_networkx_nodes(G, pos, list_nodes, node_size = 20,
node_color = str(count / size))
nx.draw_networkx_edges(G,pos, alpha=0.5)
plt.show()