import sys, getopt, os, subprocess, shutil, time
import settings
import gdv_utils, utils

vectorFilesDir = '../vector_files/'
countingExecutable = '../count_tmp_graphlets'
ncountExecutable = '../ncount'
gdvsExecutable = '../gdvs_static'
gdvsDynamicExecutable = '../gdvs_dynamic'


def get_existing_dynamic_count_methods(output_dir, network_name, file_type = 'gcounts'):
    res = []
    for name in os.listdir(output_dir):
        if not name.startswith(network_name) or not (file_type + '_') in name:
            continue

        res.append(name.replace('.txt','').replace(network_name + '_','').replace('dgdv','dgdvs').replace('dcgdv','dcgdvs').replace('dgcounts','dgdvs').replace('dcgcounts','dcgdvs'))
    return res


def convert_dynamic_counts(output_dir, network_name, old_method, new_method, convert_gdv = True):
    m_dict_old = gdv_utils.getMethodDetailsDict(old_method)
    m_dict_new = gdv_utils.getMethodDetailsDict(new_method)

    k_old = m_dict_old['k']
    n_old = m_dict_old['n']
    k_new = m_dict_new['k']
    n_new = m_dict_new['n']


    if m_dict_new['type'] != m_dict_old['type'] or k_new > k_old or n_new > n_old or m_dict_old['network_construction'] != m_dict_new['network_construction'] or m_dict_old['delta_t'] != m_dict_new['delta_t']:
        raise Exception('Cannot convert graphlet counts from %s to %s' % (old_method,new_method))

    #convert gcounts
    graphlets_old = open(settings.VECTOR_FILES_DIR + 'graphlets_%s_%s.txt' % (k_old,n_old)).readlines()
    graphlets_new = open(settings.VECTOR_FILES_DIR + 'graphlets_%s_%s.txt' % (k_new,n_new)).readlines()

    gcounts_old = open(output_dir + network_name + '_' + old_method.replace('dgdvs','dgcounts').replace('dcgdvs','dcgcounts') + '.txt').readlines()

    outf_gcounts = open(output_dir + network_name + '_' + new_method.replace('dgdvs','dgcounts').replace('dcgdvs','dcgcounts') + '.txt','wb')
    outf_counts = open(output_dir + network_name + '_' + new_method.replace('dgdvs','dcounts').replace('dcgdvs','dccounts') + '.txt','wb')


    for g_new in graphlets_new:
        outf_gcounts.write(gcounts_old[graphlets_old.index(g_new)])
        outf_counts.write('%s\t%s' % (g_new.replace('\n',''),gcounts_old[graphlets_old.index(g_new)]))

    outf_gcounts.close()
    outf_counts.close()

    #convert gdv
    if convert_gdv:
        orbits_old = [v.split('\t')[0] for v in open(settings.VECTOR_FILES_DIR + 'orbits_%s_%s.txt' % (k_old,n_old)).readlines()]
        orbits_new = [v.split('\t')[0] for v in open(settings.VECTOR_FILES_DIR + 'orbits_%s_%s.txt' % (k_new,n_new)).readlines()]

        gdv_old = open(output_dir + network_name + '_' + old_method.replace('dgdvs','dgdv').replace('dcgdvs','dcgdv') + '.txt').readlines()
        with open(output_dir + network_name + '_' + new_method.replace('dgdvs','dgdv').replace('dcgdvs','dcgdv') + '.txt','wb') as outf:
            i=1
            for line_old in gdv_old:
                counts = line_old.split(' ')
                outf.write(counts[0] + ' ')
                del counts[0]
                for o_new in orbits_new:
                    outf.write('%s ' % counts[orbits_old.index(o_new)])
                sys.stdout.write('\r%.1f%%...' % ((100.0 * i)/len(gdv_old)))
                sys.stdout.flush()
                i+=1
                outf.write('\n')


def join_ndump2(file1,file2,outputfile):
    lines1 = open(file1).readlines()
    lines2 = open(file2).readlines()
    oF=open(outputfile,'w')

    isize1=0#size of vector in file1
    isize2=0#size of vector in file2
    node_dict=dict()
    for line in lines1:
        lineArr=line.strip('\n').split(' ')
        nodeName=lineArr[0]
        isize1=len(lineArr)-1
        ## store node's gdv
        node_dict[nodeName]=lineArr[1:]
    for line in lines2:
        lineArr=line.strip('\n').split(' ')
        nodeN=lineArr[0]
        isize2=len(lineArr)-1
        oF.write(nodeN + ' ')
        if(node_dict.has_key(nodeN)):
            oF.write(' '.join(node_dict[nodeN] + lineArr[1:]))
            ##pop the item off the dictionary
            del node_dict[nodeN]
        else:
            #nodeN is not found in file 1 (the first n orbit gdv values are 0)
            oF.write(' '.join(['0']*isize1 + lineArr[1:]))
        oF.write('\n')

    ## for those nodes that are in file 1 not in file 2
    for nodeN in node_dict.keys():
        oF.write(nodeN + ' ')
        oF.write(' '.join(node_dict[nodeN] + ['0']*isize2))
        oF.write('\n')


inputSubDir = ''
delimiter = ' '
gdv_recounting = False
gdvs_recounting = False
gdvs_counting = True
remove_gdv = False
opts, args = getopt.getopt(sys.argv[1:], "s:d:tzch", ["subdir","help","do-not-compute-gdvs","recompute-gdv","recompute-gdvs","remove-gdv"])
for o, a in opts:
    if o in ('-s', '--subdir'):
        inputSubDir = a  + '/'
    elif o == '-d':
        delimiter = a.strip()
    elif o == '--recompute-gdv':
        gdv_recounting=True
    elif o == '--remove-gdv':
        remove_gdv=True
    elif o == '--recompute-gdvs':
        gdvs_recounting=True
    elif o == '--do-not-compute-gdvs':
        gdvs_counting=False
    elif o in ('-h','--help'):
        print """Usage:
%s network_name method_name_list
-s,--subdir           Location of network directory within input/output directory
-d                    Delimiter (use 'tab' for '\\t')
--do-not-compute-gdvs Do not compute gdvs (only graphlet counting)
--remove-gdv          Remove gdv counts, but keep global graphlet counts
--recompute-gdv       Compute counts even if the file already exists
--recompute-gdvs      Compute similarities even if the file already exists
-h,--help             Print this""" % sys.argv[0]
        sys.exit(0)

networkNameTempList = args[0].split(',')

if args[1].find('key_')==0:
    methodNameList = gdv_utils.getMethodsPredefinedSets(args[1])
else:
    methodNameList = args[1].split(',')

#invert ordering so that to tsake benefit of computed counts (e.g., dgdvs_6_4_1 can be converted to dgdvs_3_3_1)
methodNameList = sorted(methodNameList, cmp = gdv_utils.compareMethodNames)[::-1]

baseInputSubDir = inputSubDir

i_networkNameTemp = 1
for networkNameTemp in networkNameTempList:

    if len(networkNameTempList)>1:
        print '%s/%s. Processing %s...' % (i_networkNameTemp,len(networkNameTempList),networkNameTemp)
    i_networkNameTemp+=1
    networkNameList = [networkNameTemp]
    if '/' in networkNameTemp:
        a_dir = settings.BASE_INPUT_DIR + baseInputSubDir + networkNameTemp
        networkNameList =  [name for name in os.listdir(a_dir) if os.path.isdir(os.path.join(a_dir, name))]
        inputSubDir = baseInputSubDir + networkNameTemp

    i_network = 1
    for networkName in networkNameList:

        if len(networkNameList)>1:
            print '%s/%s. Processing network %s' % (i_network,len(networkNameList),networkName)
        i_network += 1

        i_method = 1
        for methodName in methodNameList:

            if len(methodNameList)>1:
                print '%s/%s. Running method %s' % (i_method,len(methodNameList),methodName)
            i_method += 1
            m_dict = gdv_utils.getMethodDetailsDict(methodName)
            methodType = m_dict['type']
            if not 'd' in methodType: #either static or static-temporal

                maxGraphletSize = m_dict['n']
                networkConstructionType = m_dict['network_construction']


                if not methodType in ['s','st']:
                    raise Exception('Error: method should be either static (\'s\') or static-temporal (\'st\'): %s' % methodType)
                else:
                    methodType = 'static-temporal' if 'st' in methodType else 'static'

                inputDir = settings.BASE_INPUT_DIR + inputSubDir + networkName + '/leda_format/'

                if methodType == 'static':
                    inputDir += 'aggregated/'
                elif methodType == 'static-temporal':
                    inputDir += 'snapshots/'

                outputDir = settings.BASE_OUTPUT_DIR + inputSubDir + networkName + '/counts/' + methodType + '/'

                if not os.path.exists(outputDir):
                    os.makedirs(outputDir)

                print 'Running %s graphlet counting for %s network...' % (methodType,networkName)

                if methodType == 'static':
                    if networkConstructionType == '':
                        inputFile = inputDir  + networkName + '_aggregated' + '.gw'
                        counts_file_prefix = outputDir + networkName + '_aggregated'
                        if not os.path.exists(inputFile):
                            inputFile = inputDir  + networkName + '_aggregated_1' + '.gw'
                            counts_file_prefix += '_1'
                    else:
                        inputFile = inputDir  + networkName + '_'  + networkConstructionType + '.gw'
                        counts_file_prefix = outputDir  + networkName + '_' + networkConstructionType

                    if os.path.exists(counts_file_prefix + '.ndump2') and not gdv_recounting:
                        print 'Using existing ndump2 file: %s' % counts_file_prefix
                    else:

                        runCommand = "%s %s %s" % (
                            ncountExecutable,
                            inputFile,
                            counts_file_prefix
                        )

                        start_time = time.time()
                        p = subprocess.Popen(runCommand.split(), stdout=subprocess.PIPE)
                        print p.stdout.read()

                        #remove redundant files
                        print 'Removing redundant files...'
                        utils.clean_dir(outputDir, r'_freq')

                        end_time = time.time()
                        print 'Running time: %.3f seconds'% (end_time - start_time)

                        #save time
                        with open(outputDir  + networkName + '_' + methodName + '_time.txt','wb') as outf_time:
                            outf_time.write('%s' % (end_time - start_time))

                    #rename graphlet counts file for consistency
                    if os.path.exists(counts_file_prefix) and not os.path.exists(counts_file_prefix + '_sgcounts_5.txt') or gdv_recounting:
                        shutil.copyfile(counts_file_prefix, counts_file_prefix +'_sgcounts_5.txt')

                    #rename gdv file for consistency
                    if os.path.exists(counts_file_prefix + '.ndump2') and not os.path.exists(counts_file_prefix + '_sgdv_5.txt') or gdv_recounting:
                        shutil.copyfile(counts_file_prefix + '.ndump2', counts_file_prefix + '_sgdv_5.txt')

                    #convert graphlet counts file for smaller graphlet sizes
                    if maxGraphletSize<5 and (not os.path.exists(counts_file_prefix +'_sgcounts_%s.txt' % maxGraphletSize)
                                              or os.path.getsize(counts_file_prefix +'_sgcounts_%s.txt' % maxGraphletSize)==0
                                              or gdv_recounting):
                        print 'Converting 3-5 graphlet counts file to 3-%s graphlet counts file...' % maxGraphletSize
                        with open(counts_file_prefix +'_sgcounts_%s.txt' % maxGraphletSize,'wb') as new_counts_file:
                            i=1
                            for line in open(counts_file_prefix +'_sgcounts_5.txt').readlines():
                                if maxGraphletSize == 3 and i == 3:
                                    break
                                if maxGraphletSize == 4 and i == 9:
                                    break
                                new_counts_file.write(line)
                                i+=1

                    #convert gdv file for smaller graphlet sizes
                    if maxGraphletSize<5 and (not os.path.exists(counts_file_prefix +'_sgdv_%s.txt' % maxGraphletSize)
                                              or os.path.getsize(counts_file_prefix +'_sgdv_%s.txt' % maxGraphletSize)==0
                                              or gdv_recounting):
                        print 'Converting 3-5 graphlet counts file to 3-%s gdv file...' % maxGraphletSize
                        with open(counts_file_prefix +'_sgdv_%s.txt' % maxGraphletSize,'wb') as new_gdv_file:
                            i=1
                            for line in open(counts_file_prefix +'_sgdv_5.txt').readlines():
                                values = line.split(' ')
                                new_gdv_file.write('%s %s\n' % (values[0],' '.join(values[1:(5 if maxGraphletSize == 3 else 16 if maxGraphletSize ==4 else 0 )])))
                                i+=1

                elif methodType == 'static-temporal':

                    inputDir += networkConstructionType + '/'
                    inputFileList = sorted([f for f in os.listdir(inputDir)])

                    outputDir += networkConstructionType + '/'



                    inputTypeSubDirTrimmed = networkConstructionType.replace(networkName,'').replace('/','')
                    outputPrefix =  outputDir + networkName + ('' if (inputTypeSubDirTrimmed == '' or inputTypeSubDirTrimmed[0]=='_') else '_') + \
                                          inputTypeSubDirTrimmed

                    combinedCountsFile = outputPrefix + '.ndump2'

                    print 'Processing %s snapshots' % len(inputFileList)

                    if os.path.exists(combinedCountsFile) and not gdv_recounting:
                        print 'Using existing combined ndump2 file'
                    else:

                        if not os.path.exists(outputDir):
                            os.makedirs(outputDir)

                        start_time = time.time()

                        #count graphlets for each snapshot
                        for f in inputFileList:

                            print 'Counting graphlets for %s' % f
                            if os.path.exists(outputDir + f.replace('.gw','') + '.ndump2') and not gdv_recounting:
                                print 'Using existing ndump2 file'
                            else:
                                runCommand = "%s %s %s" % (
                                    ncountExecutable,
                                    os.path.join(inputDir, f),
                                    outputDir + f.replace('.gw','')
                                )

                                p = subprocess.Popen(runCommand.split(), stdout=subprocess.PIPE)
                                print p.stdout.read()


                        #remove redundant files
                        print 'Removing redundant files...'
                        utils.clean_dir(outputDir, r'_freq')

                        end_time = time.time()
                        print 'Running time without combining results: %.3f seconds'% (end_time - start_time)

                        #save time
                        with open(outputPrefix + '_stgdvs_%s' % maxGraphletSize + '_without_combine_time.txt','wb') as outf_time:
                            outf_time.write('%s' % (end_time - start_time))

                        print 'Combining individual ndump2 files...'
                        countFileList = [outputDir + f.replace('.gw','') + '.ndump2' for f in inputFileList]
                        join_ndump2(countFileList[0],countFileList[1],combinedCountsFile)
                        for f in countFileList[2:]:
                            join_ndump2(combinedCountsFile,f,combinedCountsFile)


                        end_time = time.time()
                        print 'Running time: %.3f seconds'% (end_time - start_time)

                        #save time
                        with open(outputPrefix + '_stgdvs_%s' % maxGraphletSize + '_time.txt','wb') as outf_time:
                            outf_time.write('%s' % (end_time - start_time))

                    #rename gdv file for consistency
                    if not os.path.exists(outputPrefix + '_stgdv_5.txt') or gdv_recounting:
                        shutil.copyfile(combinedCountsFile, outputPrefix + '_stgdv_5.txt')

                    #combine individual graphlet count files
                    if not os.path.exists(outputPrefix + '_stgcounts_5.txt') or gdv_recounting:
                        print 'Combining individual graphlet count files...'
                        utils.concatenate_files([outputDir + f.replace('.gw','') for f in inputFileList],outputPrefix + '_stgcounts_5.txt')

                    #convert graphlet counts file for smaller graphlet sizes
                    if maxGraphletSize<5 and (not os.path.exists(outputPrefix +'_stgcounts_%s.txt' % maxGraphletSize)
                                              or os.path.getsize(outputPrefix +'_stgcounts_%s.txt' % maxGraphletSize)==0
                                              or gdv_recounting):
                        print 'Converting 3-5 graphlet counts file to 3-%s graphlet counts file...' % maxGraphletSize
                        with open(outputPrefix +'_stgcounts_%s.txt' % maxGraphletSize,'wb') as new_counts_file:
                            i_global=1
                            i_snapshot=1
                            for line in open(outputPrefix +'_stgcounts_5.txt').readlines():
                                i_snapshot = i_global % 29
                                if i_snapshot > 0 and ((maxGraphletSize == 3 and i_snapshot < 3) or (maxGraphletSize == 4 and i_snapshot < 9)):
                                    new_counts_file.write(line)
                                i_global+=1

                    #convert gdv file for smaller graphlet sizes
                    if maxGraphletSize<5 and (not os.path.exists(outputPrefix +'_stgdv_%s.txt' % maxGraphletSize)
                                              or os.path.getsize(outputPrefix +'_stgdv_%s.txt' % maxGraphletSize)==0
                                              or gdv_recounting):
                        print 'Converting 3-5 graphlet counts file to 3-%s gdv file...' % maxGraphletSize
                        with open(outputPrefix +'_stgdv_%s.txt' % maxGraphletSize,'wb') as new_gdv_file:
                                for line in open(outputPrefix +'_stgdv_5.txt').readlines():
                                    values = line.split(' ')
                                    new_values = []
                                    i_global=1
                                    i_snapshot=1
                                    for v in values[1:]:
                                        i_snapshot = i_global % 73
                                        if i_snapshot > 0 and ((maxGraphletSize == 3 and i_snapshot < 5) or (maxGraphletSize == 4 and i_snapshot < 16)):
                                            new_values.append(v)
                                        i_global+=1
                                    new_gdv_file.write('%s %s\n' % (values[0],' '.join(new_values)))


                if gdvs_counting:
                    print 'Computing gdvs (3-%s node graphlets)...' % maxGraphletSize

                    if methodType == 'static' and (not os.path.isfile(counts_file_prefix +'_sgdvs_%s.txt' % maxGraphletSize) or gdvs_recounting):
                        runCommand = "%s %s %s %s %s" % (
                            gdvsExecutable,
                            counts_file_prefix + '.ndump2',
                            counts_file_prefix + '.ndump2',
                            counts_file_prefix +'_sgdvs_%s.txt' % maxGraphletSize,
                            maxGraphletSize
                        )

                        p = subprocess.Popen(runCommand.split(), stdout=subprocess.PIPE)
                        print p.stdout.read()


                    elif methodType == 'static-temporal' and (not os.path.isfile(outputPrefix + '_stgdvs_%s.txt' % maxGraphletSize) or gdvs_recounting):

                        #create weight file
                        baseWeights = [1, 0.838444533, 0.838444533, 0.838444533, 0.743940642, 0.676889065, 0.743940642, 0.743940642, 0.676889065, 0.743940642, 0.676889065, 0.676889065, 0.676889065, 0.676889065, 0.743940642, 0.676889065, 0.582385175, 0.624879821, 0.676889065, 0.624879821, 0.582385175, 0.582385175, 0.676889065, 0.676889065, 0.676889065, 0.624879821, 0.546456463, 0.676889065, 0.582385175, 0.582385175, 0.546456463, 0.676889065, 0.582385175, 0.582385175, 0.582385175, 0.624879821, 0.582385175, 0.546456463, 0.546456463, 0.624879821, 0.546456463, 0.582385175, 0.546456463, 0.582385175, 0.624879821, 0.624879821, 0.582385175, 0.515333598, 0.546456463, 0.582385175, 0.582385175, 0.515333598, 0.582385175, 0.487881285, 0.624879821, 0.582385175, 0.676889065, 0.582385175, 0.582385175, 0.546456463, 0.515333598, 0.582385175, 0.582385175, 0.515333598, 0.546456463, 0.582385175, 0.546456463, 0.546456463, 0.515333598, 0.624879821, 0.582385175, 0.582385175, 0.676889065]

                        if maxGraphletSize < 5:
                            i=4 if maxGraphletSize == 3 else 15
                            while i < len(baseWeights):
                                baseWeights[i]=0
                                i+=1

                        #concatenate several weighted
                        numSnapshots = len(inputFileList)

                        combinedWeights = []
                        i=0
                        while i<numSnapshots:
                            combinedWeights += baseWeights
                            i+=1

                        inputTypeSubDirTrimmed = networkConstructionType.replace(networkName,'').replace('/','')
                        combinedWeightsFile = open(outputPrefix  + '_combined_weights_%s.txt' % maxGraphletSize,'wb')
                        combinedWeightsFile.write("\n".join(['xx\txx\t%s' % w for w in combinedWeights]))

                        runCommand = "%s %s %s %s %s %s %s" % (
                            gdvsDynamicExecutable,
                            combinedCountsFile,
                            combinedCountsFile,
                            outputPrefix + '_stgdvs_%s.txt' % maxGraphletSize,
                            outputPrefix + '_combined_weights_%s.txt' % maxGraphletSize,
                            len(combinedWeights),
                            '0'
                        )

                        #if run otherwise, all output values are NaN
                        subprocess.Popen(runCommand, shell = True)

                if remove_gdv:
                    if methodType == 'static'  and os.path.exists(counts_file_prefix +'_sgdv_%s.txt' % maxGraphletSize):
                        print 'Removing %s' % (counts_file_prefix +'_sgdv_%s.txt' % maxGraphletSize)
                        os.remove(counts_file_prefix +'_sgdv_%s.txt' % maxGraphletSize)
                        if os.path.exists(counts_file_prefix + '.ndump2'):
                            print 'Removing %s' % (counts_file_prefix + '.ndump2')
                            os.remove(counts_file_prefix + '.ndump2')
                    elif methodType == 'static-temporal'  and os.path.exists(outputPrefix +'_stgdv_%s.txt' % maxGraphletSize):
                        print 'Removing %s' % (outputPrefix +'_stgdv_%s.txt' % maxGraphletSize)
                        os.remove(outputPrefix +'_stgdv_%s.txt' % maxGraphletSize)


            else:#DYNAMIC GRAPHLET COUNTING
                kMax = m_dict['k']
                nMax = m_dict['n']
                deltaT = m_dict['delta_t']
                onlyCausalGraphlets = 'dc' in m_dict['type']
                discardZeros = 1 if ('z' in m_dict['type']) else 0
                networkConstructionType = m_dict['network_construction']


                inputDir = settings.BASE_INPUT_DIR + inputSubDir + networkName + ('/snapshots/' if 'snapshots' in networkConstructionType else '/') + networkConstructionType + ('/' if networkConstructionType != '' else '')
                outputDir = settings.BASE_OUTPUT_DIR + inputSubDir + networkName + '/counts/'

                if networkConstructionType == '':
                    if os.path.exists(inputDir + networkName + '.txt'):
                        networkConstructionType = networkName + '.txt'
                    else:
                        networkConstructionType = 'snapshots'
                        inputDir += 'snapshots'


                inputTypeSubDirTrimmed = networkConstructionType.replace(networkName,'').replace('.txt','').replace('snapshots/','').replace('/','')

                #in case the files are taken directly from 'snapshots/' from subdir - no need to have 'snapshots' in name (since there is no temporal network in this case)
                if networkConstructionType.find('snapshots') + len('snapshots') == len(networkConstructionType):
                    inputTypeSubDirTrimmed = inputTypeSubDirTrimmed.replace('snapshots','')

                outputPrefix = outputDir + networkName + ('' if inputTypeSubDirTrimmed == '' or inputTypeSubDirTrimmed[0]=='_' else '_') + inputTypeSubDirTrimmed

                print 'Running temporal graphlet counting for %s network...' % networkName

                if not '.txt' in networkConstructionType: #then it is directory containing snapshots
                    inputFileList = sorted([os.path.join(inputDir, f) for f in os.listdir(inputDir)])
                    inputFiles = ','.join(inputFileList)
                else:
                    inputFiles = os.path.join(inputDir, networkConstructionType) #only one file for input - temporal network

                #print 'List of input files (%s files): %s' % (len(inputFileList),inputFiles)

                if not os.path.exists(outputDir):
                    os.makedirs(outputDir)

                gdv_file = outputDir + networkName + '_' + methodName.replace('gdvs','gdv').replace('z','') + '.txt' #dz and dcz affect only gdvs not graphlet counting
                gdvs_file = outputDir + networkName + '_' + methodName + '.txt'
                gcounts_file = outputDir + networkName + '_' + methodName.replace('dgdvs','dgcounts').replace('dcgdvs','dcgcounts') + '.txt'

                #check if can convert existing counts without recomputing
                converted_counts = False
                if not gdv_recounting  and (not os.path.isfile(gcounts_file)  or os.path.getsize(gcounts_file)==0):
                    for temp_m in get_existing_dynamic_count_methods(outputDir, networkName, file_type='gcounts'):
                        temp_m_dict = gdv_utils.getMethodDetailsDict(temp_m)
                        if temp_m_dict['type'] == m_dict['type'] and temp_m_dict['k'] >= kMax and temp_m_dict['n'] >= nMax and temp_m_dict['network_construction'] == m_dict['network_construction'] and temp_m_dict['delta_t'] == deltaT and temp_m != methodName:
                            print 'Converting existing counts from %s to %s' % (temp_m,methodName)
                            convert_dynamic_counts(outputDir, networkName, temp_m, methodName, convert_gdv= not remove_gdv)
                            converted_counts = True
                            break


                if not converted_counts and (not os.path.isfile(gcounts_file)  or os.path.getsize(gcounts_file)==0 or gdv_recounting):
                    print 'Counting graphlets...'

                    start_time = time.time()

                    runCommand = [
                    countingExecutable,
                    inputFiles,
                    str(kMax),
                    str(nMax),
                    str(deltaT),
                    outputPrefix,
                    '-v' + vectorFilesDir + 'graphlets_%s_%s.txt' % (kMax, nMax),
                    '-g' + vectorFilesDir + 'orbits_%s_%s.txt' % (kMax, nMax),
                    '-d%s' % delimiter,
                    '-t' if onlyCausalGraphlets else ''
                    ]

                    #print ' '.join(runCommand)

                    p = subprocess.Popen(runCommand, stdout=subprocess.PIPE)
                    print p.stdout.read()

                    end_time = time.time()
                    print 'Running time: %.3f seconds'% (end_time - start_time)

                    #save time
                    with open(outputDir + networkName + '_' + methodName + '_time.txt','wb') as outf_time:
                        outf_time.write('%s' % (end_time - start_time))


                elif not convert_dynamic_counts and os.path.exists(gdv_file):
                    print 'Using existing counts file:  %s' % gdv_file


                if gdvs_counting and (not os.path.isfile(gdvs_file) or os.path.getsize(gdv_file)==0 or gdvs_recounting):
                    print outputPrefix + '_d%s%sgdvs_%s_%s_%s.txt' % ('c' if onlyCausalGraphlets else '','z' if discardZeros == 1 else '', kMax, nMax, deltaT)
                    print 'Computing  gdvs...'

                    runCommand = "%s %s %s %s %s %s %s" % (
                    gdvsDynamicExecutable,
                    gdv_file,
                    gdv_file,
                    gdvs_file,
                    vectorFilesDir + 'orbits_%s_%s.txt' % (kMax, nMax),
                    sum(1 for line in open(vectorFilesDir + 'orbits_%s_%s.txt' % (kMax, nMax))),
                    discardZeros ## whether to discard 0s when computing dynamic gdvs
                    )
                    #print runCommand

                    p = subprocess.Popen(runCommand.split(), stdout=subprocess.PIPE)
                    print p.stdout.read()
                elif os.path.isfile(gdvs_file):
                    print 'Similarities file already exists: %s' % gdvs_file

                if remove_gdv and os.path.exists(gdv_file):
                    print 'Removing %s' % gdv_file
                    os.remove(gdv_file)

