In [1]:
import os
import community
import networkx as nx
import pickle
from sklearn.metrics import normalized_mutual_info_score as NMI
In [2]:
path = "../../LFR_low_degree/"
directories = os.listdir(path)
for x in directories :
exe = "mkdir "+x
os.system(exe)
In [3]:
for x in directories :
a = os.listdir(path+x)
print x
for m in a :
if "network" in m:
readfile = os.path.abspath(path+x+"\\"+m)
writefile = os.path.abspath(x+"\\"+m)
writefile = writefile.replace("network","community")
G = nx.read_edgelist(readfile)
communities = community.best_partition(G)
with open(writefile,'wb') as handle :
for nodes in range(1,1001) :
temp = str(nodes) + "\t" + str(communities[str(nodes)])
handle.write(temp+"\n")
In [7]:
urange = [0.05*i for i in range(19)]
In [8]:
ugraph = []
for x in urange:
values = []
truth_comm = "../../LFR_low_degree/"
directory = "mu_"+str(x)+"_N_1000/"
for y in range(1,11):
true_path = truth_comm + directory + "community_run_"+str(y)+".dat"
with open(true_path) as handle :
temp = handle.readlines()
tc = [a.split()[1] for a in temp]
louvain_path = directory + "community_run_"+str(y)+".dat"
with open(louvain_path) as handle :
temp = handle.readlines()
lc = [a.split()[1] for a in temp]
val = NMI(tc,lc)
values.append(val)
ugraph.append(sum(values)/10.0)
In [9]:
with open("louvain_scores_lfr.pickle",'wb') as handle:
pickle.dump(ugraph,handle)