In [1]:
import os
from shutil import copyfile
import subprocess
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)
    
PARAMETER_SETTING = 0.6

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

#save directory
dir = os.path.abspath("real_world_benchmarks_{}".format(PARAMETER_SETTING))

#number of times to repeat
num_repeats = 100

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

#change to dir
os.chdir(dir)    

#ghsom parameters
params = {'w': 0.0001,
         'eta': 0.0001,
         'sigma': 1,
         'e_sg': PARAMETER_SETTING,
         'e_en': 0.8}

#network names
network_names = ['karate','dolphin','polbooks','football']

#community labels
labels = ['club','group','value','value']

overall_nmi_scores = np.zeros((len(network_names), num_repeats))
overall_communities_detected = np.zeros((len(network_names), num_repeats))

for i in range(len(network_names)):
    
    #name of current network
    network_name = network_names[i]

    #label of current network
    label = labels[i]
    
    #create directory
    dir_string = os.path.join(dir, network_name)
    if not os.path.isdir(dir_string):
        os.mkdir(dir_string)
    
    #change working directory    
    os.chdir(dir_string)
    
    if os.path.isfile('nmi_scores.csv'):
        print 'already completed {} network, loading nmi scores and continuing'.format(network_name)
        nmi_scores = np.genfromtxt('nmi_scores.csv', delimiter=',')
        overall_nmi_scores[i] = nmi_scores
        communities_detected = np.genfromtxt('communties_detected.csv', delimiter=',')
        overall_communities_detected[i] = communities_detected
        continue
    
    #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')
        
    #record communities detected
    if not os.path.isfile('communities_detected.pkl'):
        print 'creating new communites detected array'
        communities_detected = np.zeros(num_repeats)
    else:
        print 'loading communites detected progress'
        communities_detected = load_obj('communities_detected')
        
    #copy embedded gml
    gml_filename = 'embedded_{}.gml'.format(network_name)  
    if not os.path.isfile(gml_filename):
        
        source = "C:\Miniconda3\Jupyter\GHSOM_simplex_dsd\{}".format(gml_filename)
        copyfile(source, gml_filename)
    
    #generate networks
    for r in range(1,num_repeats+1):
            
        ##score for this network
        if not np.all(nmi_scores[r-1]):
            
            start_time = time()
            
            print 'starting ghsom for: {}, repeat: {}'.format(gml_filename, r)
            nmi_score, comm_det = ghsom_main(params, gml_filename, label, 10000)
            nmi_scores[r-1] = nmi_score
            communities_detected[r-1] = comm_det
            
            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')
            save_obj(communities_detected, 'communities_detected')
            
            print 'saved nmi score for network {}: {}'.format(gml_filename, nmi_score)
            print 'saved communities detected for network {}: {}'.format(gml_filename, comm_det)
            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=',')
    np.savetxt('communties_detected.csv',communities_detected,delimiter=',')
    
    overall_nmi_scores[i] = nmi_scores
    overall_communities_detected[i] = communities_detected
    
print 'DONE'

print 'OVERALL NMI SCORES'
print overall_nmi_scores
print overall_communities_detected


already completed karate network, loading nmi scores and continuing
already completed dolphin network, loading nmi scores and continuing
already completed polbooks network, loading nmi scores and continuing
already completed football network, loading nmi scores and continuing
DONE
OVERALL NMI SCORES
[[ 0.57617756  0.47834623  0.46959898  0.5294656   0.5294656   0.47834623
   0.52352659  0.5294656   0.58033157  0.48590751  0.47834623  0.60824404
   0.46959898  0.58033157  0.47834623  0.47834623  0.48590751  0.46959898
   0.46959898  0.5294656   0.47834623  0.46997158  0.47527903  0.5294656
   0.47834623  0.46959898  0.5294656   0.5294656   0.4928676   0.44218727
   0.43324696  0.47834623  0.44218727  0.5294656   0.5294656   0.46959898
   0.5294656   0.47347514  0.47834623  0.59343131  0.46959898  0.47834623
   0.57617756  0.46959898  0.4928676   0.4928676   0.58033157  0.5294656
   0.48590751  0.47834623  0.5294656   0.46959898  0.60824404  0.46959898
   0.46959898  0.46959898  0.46959898  0.5294656   0.52352659  0.58033157
   0.58033157  0.5294656   0.46959898  0.50404987  0.5294656   0.47834623
   0.43324696  0.46959898  0.57617756  0.46959898  0.46959898  0.47834623
   0.5294656   0.46997158  0.47347514  0.58842383  0.5294656   0.44218727
   0.5294656   0.47834623  0.47347514  0.44218727  0.47347514  0.46959898
   0.47347514  0.47834623  0.47834623  0.58033157  0.50404987  0.47347514
   0.47834623  0.47834623  0.52352659  0.47834623  0.46959898  0.46959898
   0.46959898  0.59343131  0.47834623  0.46959898]
 [ 0.47675483  0.48548981  0.71153202  0.49609041  0.71153202  0.4865605
   0.4865605   0.48984984  0.47675483  0.4865605   0.47675483  0.49788256
   0.47675483  0.57015121  0.48984984  0.49609041  0.60030441  0.48984984
   0.4865605   0.49609041  0.4865605   0.60030441  0.47675483  0.49585313
   0.47675483  0.51999994  0.7252163   0.47675483  0.49585313  0.49609041
   0.7252163   0.49585313  0.48984984  0.48984984  0.57015121  0.4865605
   0.4865605   0.48707936  0.69960902  0.63614901  0.49609041  0.60030441
   0.50563041  0.4865605   0.48390454  0.53492331  0.7252163   0.49450469
   0.47675483  0.50423703  0.69960902  0.5057058   0.49609041  0.60030441
   0.5057058   0.71153202  0.47675483  0.52807347  0.47675483  0.60030441
   0.50563041  0.48984984  0.4865605   0.49585313  0.49609041  0.47675483
   0.48390454  0.4865605   0.4865605   0.47675483  0.47675483  0.57015121
   0.49609041  0.48390454  0.49609041  0.48548981  0.47675483  0.4865605
   0.49609041  0.49450469  0.49609041  0.48952268  0.4865605   0.4865605
   0.48984984  0.60030441  0.60030441  0.5057058   0.47675483  0.49609041
   0.4865605   0.48984984  0.49609041  0.48984984  0.49609041  0.5057058
   0.71153202  0.50563041  0.48707936  0.57015121]
 [ 0.51076215  0.51813975  0.51681121  0.51009765  0.51076215  0.51312341
   0.56779484  0.51076215  0.51076215  0.51076215  0.56007797  0.51076215
   0.51076215  0.51076215  0.51076215  0.51312341  0.51076215  0.51076215
   0.5491174   0.56007797  0.51009765  0.50499336  0.51076215  0.51009765
   0.51076215  0.51009765  0.51076215  0.51076215  0.51009765  0.5198172
   0.54654507  0.51312341  0.51076215  0.51009765  0.51076215  0.51009765
   0.55229488  0.51009765  0.56779484  0.51076215  0.51009765  0.51076215
   0.51312341  0.52106516  0.51009765  0.51009765  0.51312341  0.51312341
   0.51009765  0.51009765  0.51009765  0.51076215  0.51167659  0.54038985
   0.51009765  0.51076215  0.51076215  0.50739484  0.51009765  0.51076215
   0.5155065   0.51009765  0.5198172   0.51076215  0.51009765  0.51813975
   0.56007797  0.51076215  0.51009765  0.56007797  0.51009765  0.51076215
   0.51009765  0.51009765  0.51076215  0.51076215  0.51009765  0.51009765
   0.51009765  0.51009765  0.55233631  0.51312341  0.51312341  0.51009765
   0.51009765  0.51009765  0.51076215  0.51076215  0.51009765  0.50739484
   0.51009765  0.51009765  0.51076215  0.51813975  0.53934797  0.51076215
   0.51076215  0.51076215  0.52106516  0.51009765]
 [ 0.75454467  0.73253338  0.71493981  0.73194635  0.76993941  0.78834969
   0.77703383  0.75013003  0.75342126  0.71078397  0.8034001   0.74841203
   0.72873555  0.74841203  0.73019415  0.73318921  0.7173374   0.75682188
   0.73253338  0.7173374   0.73006296  0.73194635  0.74858012  0.72833141
   0.73019415  0.75601539  0.74926912  0.73646996  0.76341024  0.7557236
   0.71493981  0.735165    0.74841203  0.72873555  0.71078397  0.73077727
   0.74338473  0.74515161  0.73077727  0.71493981  0.73194635  0.74152374
   0.73194635  0.7173374   0.71735985  0.76341024  0.73006296  0.73006296
   0.74152374  0.76971562  0.73077727  0.73646996  0.75013003  0.735165
   0.73253338  0.7173374   0.73143311  0.73738924  0.74946214  0.7499111
   0.73143311  0.71504432  0.74841203  0.74926912  0.7557236   0.76505476
   0.76482662  0.75399504  0.70354356  0.73019415  0.73036206  0.74551309
   0.7171143   0.73143311  0.73999386  0.74926912  0.71493981  0.73738924
   0.73849409  0.73143311  0.7233008   0.7173374   0.74926912  0.73143311
   0.75647327  0.74238203  0.73143311  0.72873555  0.73738924  0.74690946
   0.73738924  0.74925726  0.72873555  0.73006296  0.74338473  0.74349165
   0.72939262  0.73905983  0.73738924  0.73006296]]
[[ 4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
   4.  4.  4.  5.  5.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
   4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
   4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
   4.  5.  4.  3.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
   4.  4.  4.  4.  4.  4.  4.  4.  4.  4.]
 [ 4.  4.  5.  4.  5.  4.  4.  4.  4.  4.  4.  4.  4.  5.  4.  4.  5.  4.
   4.  4.  4.  5.  4.  4.  4.  4.  5.  4.  4.  4.  5.  4.  4.  4.  5.  4.
   4.  4.  5.  4.  4.  5.  4.  4.  4.  4.  5.  4.  4.  4.  5.  4.  4.  5.
   4.  5.  4.  4.  4.  5.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  5.
   4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  5.  5.  4.  4.  4.
   4.  4.  4.  4.  4.  4.  5.  4.  4.  5.]
 [ 4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
   4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
   4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
   4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
   4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.  4.
   4.  4.  4.  4.  4.  4.  4.  4.  4.  4.]
 [ 8.  7.  7.  7.  8.  7.  7.  7.  8.  7.  8.  8.  7.  8.  7.  8.  7.  8.
   7.  7.  7.  7.  8.  7.  7.  7.  8.  7.  8.  8.  7.  7.  8.  7.  7.  7.
   7.  7.  7.  7.  7.  8.  7.  7.  7.  8.  7.  7.  8.  8.  7.  7.  7.  7.
   7.  7.  7.  7.  7.  8.  7.  7.  8.  8.  8.  7.  7.  7.  7.  7.  7.  8.
   7.  7.  8.  8.  7.  7.  7.  7.  7.  7.  8.  7.  8.  7.  7.  7.  7.  8.
   7.  7.  7.  7.  7.  9.  7.  7.  7.  7.]]

In [2]:
for score in overall_nmi_scores:
    
    mean = np.mean(score)
    print mean
    se = np.std(score) / num_repeats
    print se
    print


0.500371004372
0.000426054968268

0.523271512179
0.000694054672717

0.516600171505
0.000146423780636

0.738648910478
0.000166885582662


In [ ]: