In [1]:
import os
import networkx as nx
import pickle
from sklearn.metrics import normalized_mutual_info_score as NMI
from sklearn.cluster import SpectralClustering
import numpy as np

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:
            communities = {}
            readfile = os.path.abspath(path+x+"\\"+m)
            writefile = os.path.abspath(x+"\\"+m)
            writefile = writefile.replace("network","community")
            G = nx.read_edgelist(readfile)
            adj = nx.adjacency_matrix(G)
            commfile = m.replace("network","community")
            rf2 = os.path.abspath(path+x+"\\"+commfile)
            with open(rf2) as handle :
                temp = handle.readlines()
            number_comm = len(set([a.split()[1] for a in temp])) #Finding the true number of communities
            clus = SpectralClustering(n_clusters=number_comm,affinity="precomputed")
            out = clus.fit_predict(adj)
            graph_nodes = G.nodes()
            for d in range(len(graph_nodes)) :
                communities[int(graph_nodes[d])] = out[d]
            with open(writefile,'wb') as handle :
                for nodes in range(1,1001) :
                    temp = str(nodes) + "\t" + str(communities[nodes])
                    handle.write(temp+"\n")


mu_0.05_N_1000
C:\Users\abhna\Anaconda2\lib\site-packages\sklearn\manifold\spectral_embedding_.py:229: UserWarning: Graph is not fully connected, spectral embedding may not work as expected.
  warnings.warn("Graph is not fully connected, spectral embedding"
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 [4]:
urange = [0.05*i for i in range(19)]

In [5]:
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]
        spectral_path = directory + "community_run_"+str(y)+".dat"
        with open(spectral_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 [6]:
with open("spectral_scores_lfr.pickle",'wb') as handle:
    pickle.dump(ugraph,handle)

In [7]:
print ugraph


[0.90420020800260681, 0.99565413551230919, 0.99550509694056299, 0.99492941143701208, 0.98269747074038349, 0.9823916382571436, 0.98792254531458834, 0.98769651463946828, 0.97348295814255814, 0.97292845375979731, 0.97853021817273811, 0.92745656384336694, 0.86446139928087828, 0.72546203456976455, 0.61427446362548976, 0.54800600821061818, 0.50667845453281901, 0.48517380528498977, 0.47128966363188746]

In [ ]: