In [2]:
import os
from shutil import copyfile
import subprocess
from save_embedded_graph27 import main_binary as embed_main
from spearmint_ghsom import main as ghsom_main
import numpy as np
import pickle
from time import time

def save_obj(obj, name):
    with open(name + '.pkl', 'wb') as f:
        pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)

def load_obj(name):
    with open(name + '.pkl', 'rb') as f:
        return pickle.load(f)

#root dir
os.chdir("C:\Miniconda3\Jupyter\GHSOM_simplex_dsd")

#save directory
dir = os.path.abspath("parameter_tests")

#number of times to repeat
num_repeats = 10

#number of nodes in communitiy
s1 = 32

#number of links to same community
z1 = 16

#number of nodes in micro community
minc = s1
maxc = s1

#make save directory
if not os.path.isdir(dir):
    os.mkdir(dir)

#change to dir
os.chdir(dir)    

#network file names -- output of network generator
network = "network.dat"
first_level = "community.dat"

#community labels
labels = 'firstlevelcommunity'

#mixing parameter
z2 = 16

#node degree
k = z1 + z2
maxk = k

#mixing factors
mu = float(z2) / k 

num_communities = [3, 4, 5, 6]
parameter_settings = [0.5, 0.6, 0.7, 0.8, 0.9, 1]

overall_nmi_scores = np.zeros((len(num_communities), len(parameter_settings), num_repeats))

for i in range(len(num_communities)):
# for k1 in num_communities:
    k1 = num_communities[i]
    
    #number of nodes in the network
    N = k1 * s1
    
    #create directory
    dir_string = os.path.join(dir, str(k1))
    if not os.path.isdir(dir_string):
        os.mkdir(dir_string)
    
    #change working directory    
    os.chdir(dir_string)
    
    for j in range(len(parameter_settings)):
#     for p in parameter_settings:
        
        p = parameter_settings[j]
        
        #ghsom parameters
        params = {'w': 0.0001,
                 'eta': 0.0001,
                 'sigma': 1,
                  'e_sg': p,
                 'e_en': 0.8}
    
        #create directory
        dir_string_p = os.path.join(dir_string, str(p))
        if not os.path.isdir(dir_string_p):
            os.mkdir(dir_string_p)
    
        #change working directory    
        os.chdir(dir_string_p)
        
        if os.path.isfile('nmi_scores.csv'):
            print 'already completed {}/{}, loading scores and continuing'.format(k1, p)
            nmi_scores = np.genfromtxt('nmi_scores.csv', delimiter=',')
            print nmi_scores
            overall_nmi_scores[i,j,:] = nmi_scores
            continue
        
        #copy executable
        ex = "benchmark.exe"   
        if not os.path.isfile(ex):

            source = "C:\\Users\\davem\\Documents\\PhD\\Benchmark Graph Generators\\binary_networks\\benchmark.exe"
            copyfile(source, ex)

        #make benchmark parameter file
        filename = "benchmark_flags_{}_{}.dat".format(k1,p)
        if not os.path.isfile(filename):
            with open(filename,"w") as f:
                f.write("-N {} -k {} -maxk {} -minc {} -maxc {} -mu {}".format(N, k, maxk, minc, maxc, mu))
            print 'written flag file: {}'.format(filename)
            
        #cmd strings
        change_dir_cmd = "cd {}".format(dir_string_p)
        generate_network_cmd = "benchmark -f {}".format(filename)

        #output of cmd
        output_file = open("cmd_output.out", 'w')

        #record NMI scores
        if not os.path.isfile('nmi_scores.pkl'):
            print 'creating new nmi scores array'
            nmi_scores = np.zeros(num_repeats)
        else:
            print 'loading nmi score progress'
            nmi_scores = load_obj('nmi_scores')

        #record running times
        if not os.path.isfile('running_times.pkl'):
            print 'creating new running time array'
            running_times = np.zeros(num_repeats)
        else:
            print 'loading running time progress'
            running_times = load_obj('running_times')
            
        print

        #generate networks
        for r in range(1, num_repeats+1):

            network_rename = "{}_{}".format(r,network)
            first_level_rename = "{}_{}".format(r,first_level)
            gml_filename = 'embedded_network_{}.gml'.format(r)

            if not os.path.isfile(network_rename):

                process = subprocess.Popen(change_dir_cmd + " && " + generate_network_cmd, 
                                        stdout=output_file, 
                                        stderr=output_file, 
                                        shell=True)
                process.wait()
                
                print 'generated graph {}'.format(r)

                os.rename(network, network_rename)
                os.rename(first_level, first_level_rename)
                
                print 'renamed graph {}'.format(r)

            if not os.path.isfile(gml_filename):

                ##embed graph
                embed_main(network_rename, first_level_rename)

                print 'embedded graph {} as {} in {}'.format(r, gml_filename, os.getcwd())

            ##score for this network
            if not np.all(nmi_scores[r-1]):

                start_time = time()

                print 'starting ghsom for: {}/{}/{}'.format(k1, p, gml_filename)
                nmi_score, communities_detected = ghsom_main(params, gml_filename, labels)
                nmi_scores[r-1] = nmi_score

                running_time = time() - start_time
                print 'running time of algorithm: {}'.format(running_time)
                running_times[r-1] = running_time

                #save
                save_obj(nmi_scores, 'nmi_scores')
                save_obj(running_times, 'running_times')

                print 'saved nmi score for network {}: {}'.format(gml_filename, nmi_score)
                print

        ##output nmi scores to csv file
        print 'writing nmi scores and running times to file'
        np.savetxt('nmi_scores.csv',nmi_scores,delimiter=',')
        np.savetxt('running_times.csv',running_times,delimiter=',')
        print
    
print 'DONE'

print 'OVERALL NMI SCORES'
print overall_nmi_scores


already completed 3/0.5, loading scores and continuing
[ 0.51757353  0.56056547  0.60567658  0.45671193  0.5714789   0.58880318
  0.58911933  0.67335987  0.59373086  0.51496481]
already completed 3/0.6, loading scores and continuing
[ 0.5853813   0.9576527   0.5094697   0.51942692  0.58030471  0.63681553
  0.58788973  0.53538522  0.54901159  0.55437548]
already completed 3/0.7, loading scores and continuing
[ 0.9576527   0.34382339  0.9576527   0.55674736  0.9576527   1.          0.3547282
  0.9576527   0.9576527   0.41377428]
already completed 3/0.8, loading scores and continuing
[ 0.9576527   0.47805403  0.9576527   0.58652295  0.60326795  1.
  0.67261035  0.9576527   0.9576527   0.45520928]
already completed 3/0.9, loading scores and continuing
[ 0.9576527   0.76437209  0.9576527   1.          0.9576527   0.9576527
  0.71365874  0.9576527   0.9576527   0.6616903 ]
already completed 3/1, loading scores and continuing
[ -4.99600361e-06   1.59495567e-02  -4.99600361e-06  -4.99600361e-06
   6.50307114e-01   2.52959833e-01  -4.99600361e-06  -4.99600361e-06
   5.91601982e-01   5.80840480e-01]
already completed 4/0.5, loading scores and continuing
[ 0.97483193  0.97483193  0.97483193  0.82039987  0.70205668  0.76973241
  0.85694966  0.97483193  0.97483193  0.97483193]
already completed 4/0.6, loading scores and continuing
[ 0.97483193  0.97483193  0.97483193  0.97483193  0.97483193  0.87348134
  0.97483193  0.97483193  0.97483193  0.97483193]
already completed 4/0.7, loading scores and continuing
[ 0.97483193  1.          0.97483193  0.97483193  0.97483193  0.97483193
  0.97483193  0.97483193  0.97483193  0.97483193]
already completed 4/0.8, loading scores and continuing
[ 0.97483193  0.97483193  0.97483193  0.97483193  0.97483193  0.97483193
  0.97483193  0.97483193  0.97483193  0.97483193]
already completed 4/0.9, loading scores and continuing
[ 0.83465469  0.83696296  0.78397981  0.80800223  0.83465469  0.97483193
  0.83696296  0.83931733  0.83931733  0.83931733]
already completed 4/1, loading scores and continuing
[  4.44089210e-06   4.44089210e-06   4.87595300e-01   4.44089210e-06
   4.44089210e-06   4.44089210e-06   4.44089210e-06   7.07106781e-01
   4.44089210e-06   4.44089210e-06]
already completed 5/0.5, loading scores and continuing
[ 0.9826576   0.9826576   0.9826576   0.93321072  0.9826576   0.9826576
  0.9826576   0.9826576   0.9826576   0.9826576 ]
already completed 5/0.6, loading scores and continuing
[ 0.9826576   0.9826576   0.9826576   0.96868525  0.9826576   0.9826576
  0.9826576   0.9826576   0.9826576   0.9826576 ]
already completed 5/0.7, loading scores and continuing
[ 0.90979634  0.88923945  0.88923945  0.9826576   0.90979634  0.89073424
  0.78822102  0.89073424  0.9826576   0.9826576 ]
already completed 5/0.8, loading scores and continuing
[ 0.90979634  0.9826576   0.9826576   0.9826576   0.9826576   0.9826576
  0.78822102  0.88923945  0.9826576   0.9826576 ]
already completed 5/0.9, loading scores and continuing
[ 0.78822102  0.89226053  0.88923945  0.89226053  0.89226053  0.74299172
  0.89226053  0.88923945  0.78649331  0.89073424]
already completed 5/1, loading scores and continuing
[ -5.55111512e-06   2.43764733e-01   5.93211163e-01  -5.55111512e-06
  -5.55111512e-06   6.21160155e-01  -5.55111512e-06   6.21160155e-01
  -5.55111512e-06  -5.55111512e-06]
already completed 6/0.5, loading scores and continuing
[ 0.98701877  0.98701877  0.98701877  0.98701877  0.98701877  0.98701877
  1.          0.97303771  0.98701877  0.98701877]
already completed 6/0.6, loading scores and continuing
[ 0.98701877  0.91830704  0.98701877  0.98701877  0.98701877  0.98701877
  0.98701877  0.91830704  0.8475846   0.91830704]
already completed 6/0.7, loading scores and continuing
[ 0.84520438  0.98701877  0.84520438  0.98701877  0.91939104  0.98701877
  0.92049843  0.84520438  0.98701877  0.98701877]
already completed 6/0.8, loading scores and continuing
[ 0.98701877  0.91939104  0.98701877  0.92049843  0.92049843  0.98701877
  0.98701877  0.84520438  0.98701877  0.98701877]
already completed 6/0.9, loading scores and continuing
[ 0.71688232  0.8475846   0.86145118  0.8475846   0.84640874  0.84520438
  0.86145118  0.75131582  0.84520438  0.84640874]
already completed 6/1, loading scores and continuing
[ -3.33066907e-06  -3.33066907e-06  -3.33066907e-06   5.59557113e-01
   4.59797073e-01   5.66187064e-01   6.01152759e-01   5.80380907e-01
  -3.33066907e-06   5.80380907e-01]
DONE
OVERALL NMI SCORES
[[[  5.17573527e-01   5.60565468e-01   6.05676582e-01   4.56711926e-01
     5.71478896e-01   5.88803184e-01   5.89119328e-01   6.73359869e-01
     5.93730860e-01   5.14964809e-01]
  [  5.85381299e-01   9.57652696e-01   5.09469701e-01   5.19426917e-01
     5.80304706e-01   6.36815532e-01   5.87889735e-01   5.35385219e-01
     5.49011585e-01   5.54375482e-01]
  [  9.57652696e-01   3.43823389e-01   9.57652696e-01   5.56747365e-01
     9.57652696e-01   1.00000000e+00   3.54728200e-01   9.57652696e-01
     9.57652696e-01   4.13774276e-01]
  [  9.57652696e-01   4.78054033e-01   9.57652696e-01   5.86522953e-01
     6.03267952e-01   1.00000000e+00   6.72610348e-01   9.57652696e-01
     9.57652696e-01   4.55209278e-01]
  [  9.57652696e-01   7.64372090e-01   9.57652696e-01   1.00000000e+00
     9.57652696e-01   9.57652696e-01   7.13658738e-01   9.57652696e-01
     9.57652696e-01   6.61690296e-01]
  [ -4.99600361e-06   1.59495567e-02  -4.99600361e-06  -4.99600361e-06
     6.50307114e-01   2.52959833e-01  -4.99600361e-06  -4.99600361e-06
     5.91601982e-01   5.80840480e-01]]

 [[  9.74831927e-01   9.74831927e-01   9.74831927e-01   8.20399868e-01
     7.02056683e-01   7.69732413e-01   8.56949663e-01   9.74831927e-01
     9.74831927e-01   9.74831927e-01]
  [  9.74831927e-01   9.74831927e-01   9.74831927e-01   9.74831927e-01
     9.74831927e-01   8.73481342e-01   9.74831927e-01   9.74831927e-01
     9.74831927e-01   9.74831927e-01]
  [  9.74831927e-01   1.00000000e+00   9.74831927e-01   9.74831927e-01
     9.74831927e-01   9.74831927e-01   9.74831927e-01   9.74831927e-01
     9.74831927e-01   9.74831927e-01]
  [  9.74831927e-01   9.74831927e-01   9.74831927e-01   9.74831927e-01
     9.74831927e-01   9.74831927e-01   9.74831927e-01   9.74831927e-01
     9.74831927e-01   9.74831927e-01]
  [  8.34654689e-01   8.36962965e-01   7.83979812e-01   8.08002226e-01
     8.34654689e-01   9.74831927e-01   8.36962965e-01   8.39317334e-01
     8.39317334e-01   8.39317334e-01]
  [  4.44089210e-06   4.44089210e-06   4.87595300e-01   4.44089210e-06
     4.44089210e-06   4.44089210e-06   4.44089210e-06   7.07106781e-01
     4.44089210e-06   4.44089210e-06]]

 [[  9.82657596e-01   9.82657596e-01   9.82657596e-01   9.33210724e-01
     9.82657596e-01   9.82657596e-01   9.82657596e-01   9.82657596e-01
     9.82657596e-01   9.82657596e-01]
  [  9.82657596e-01   9.82657596e-01   9.82657596e-01   9.68685251e-01
     9.82657596e-01   9.82657596e-01   9.82657596e-01   9.82657596e-01
     9.82657596e-01   9.82657596e-01]
  [  9.09796338e-01   8.89239452e-01   8.89239452e-01   9.82657596e-01
     9.09796338e-01   8.90734245e-01   7.88221019e-01   8.90734245e-01
     9.82657596e-01   9.82657596e-01]
  [  9.09796338e-01   9.82657596e-01   9.82657596e-01   9.82657596e-01
     9.82657596e-01   9.82657596e-01   7.88221019e-01   8.89239452e-01
     9.82657596e-01   9.82657596e-01]
  [  7.88221019e-01   8.92260534e-01   8.89239452e-01   8.92260534e-01
     8.92260534e-01   7.42991724e-01   8.92260534e-01   8.89239452e-01
     7.86493309e-01   8.90734245e-01]
  [ -5.55111512e-06   2.43764733e-01   5.93211163e-01  -5.55111512e-06
    -5.55111512e-06   6.21160155e-01  -5.55111512e-06   6.21160155e-01
    -5.55111512e-06  -5.55111512e-06]]

 [[  9.87018767e-01   9.87018767e-01   9.87018767e-01   9.87018767e-01
     9.87018767e-01   9.87018767e-01   1.00000000e+00   9.73037711e-01
     9.87018767e-01   9.87018767e-01]
  [  9.87018767e-01   9.18307045e-01   9.87018767e-01   9.87018767e-01
     9.87018767e-01   9.87018767e-01   9.87018767e-01   9.18307045e-01
     8.47584596e-01   9.18307045e-01]
  [  8.45204381e-01   9.87018767e-01   8.45204381e-01   9.87018767e-01
     9.19391040e-01   9.87018767e-01   9.20498433e-01   8.45204381e-01
     9.87018767e-01   9.87018767e-01]
  [  9.87018767e-01   9.19391040e-01   9.87018767e-01   9.20498433e-01
     9.20498433e-01   9.87018767e-01   9.87018767e-01   8.45204381e-01
     9.87018767e-01   9.87018767e-01]
  [  7.16882320e-01   8.47584596e-01   8.61451176e-01   8.47584596e-01
     8.46408739e-01   8.45204381e-01   8.61451176e-01   7.51315824e-01
     8.45204381e-01   8.46408739e-01]
  [ -3.33066907e-06  -3.33066907e-06  -3.33066907e-06   5.59557113e-01
     4.59797073e-01   5.66187064e-01   6.01152759e-01   5.80380907e-01
    -3.33066907e-06   5.80380907e-01]]]

In [4]:
for i in range(len(num_communities)):
    for j in range(len(parameter_settings)):
        
        scores = overall_nmi_scores[i,j]
    
#     print scores
#     idx = np.argsort(scores)[::-1]
    
#     print parameter_settings[idx[0]]
        print np.mean(scores)
    
        print np.std(scores) / num_repeats
        
        print
        
    print


0.567198444804
0.00564470094075

0.601571287057
0.0123910716422

0.745733671068
0.0273775409565

0.762627534848
0.0211774502293

0.88856373003
0.011770498227

0.209163398679
0.0271480699886


0.899813019133
0.00989642701903

0.964696868751
0.00304051754915

0.977348734524
0.000755042182543

0.974831927249
1.75541673429e-17

0.842800127327
0.00472655798979

0.11947376088
0.024392814143


0.977712908606
0.00148340616069

0.981260361343
0.000419170339582

0.911573387588
0.00567865879863

0.946585997979
0.00623134762133

0.855596133891
0.00555606921939

0.207926289984
0.0273983062453


0.986918784315
0.00060322663884

0.952461832859
0.00465445738577

0.93105964482
0.00616588932741

0.952770488674
0.0046696138505

0.826949592739
0.00474250860662

0.334744249986
0.0275611466942



In [ ]: