In [23]:
import os
import community
import networkx as nx
import pickle
from sklearn.metrics import normalized_mutual_info_score as NMI
In [18]:
path = "../../Initial_Test/Generated Graphs/"
directories = os.listdir(path)
# for x in directories :
# exe = "mkdir "+x
# os.system(exe)
In [22]:
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 [5]:
urange = [0.05*i for i in range(20)]
In [25]:
ugraph = []
for x in urange:
values = []
truth_comm = "../../Initial_Test/Generated Graphs/"
directory = "mu_"+str(x)+"_N_1000/"
for y in range(1,51):
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)/50.0)
In [27]:
with open("louvain_scores.pickle",'wb') as handle:
pickle.dump(ugraph,handle)
In [ ]: