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 = "../../Initial_Test/Generated Graphs/"
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
mu_0.0_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.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(20)]

In [5]:
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]
        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)/50.0)

In [6]:
with open("spectral_scores.pickle",'wb') as handle:
    pickle.dump(ugraph,handle)

In [7]:
print ugraph


[0.90359613624765667, 0.94500730699677959, 0.94230126451049356, 0.9325365754334104, 0.92324746508805022, 0.91163172255707592, 0.9103538515204751, 0.9047188218528458, 0.90879324850806953, 0.90089129444165805, 0.90062620508560454, 0.89560007460376057, 0.88258179376605839, 0.84632722399203886, 0.73358644008333418, 0.45503504722621718, 0.32196254545622466, 0.26566108065891086, 0.23516851393701951, 0.21539509120693676]

In [ ]: