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")


mu_0.05_N_1000
mu_0.0_N_1000
mu_0.15_N_1000
mu_0.1_N_1000
mu_0.25_N_1000
mu_0.2_N_1000
mu_0.35_N_1000
mu_0.3_N_1000
mu_0.45_N_1000
mu_0.4_N_1000
mu_0.55_N_1000
mu_0.5_N_1000
mu_0.65_N_1000
mu_0.6_N_1000
mu_0.75_N_1000
mu_0.7_N_1000
mu_0.85_N_1000
mu_0.8_N_1000
mu_0.95_N_1000
mu_0.9_N_1000

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)