from accuracy_022P_cy import STSimM_cy
import numpy as np
import matplotlib.pyplot as plt
import pyspike as spk
import time

paramC =  3
lamb = 10
omega = 0.35

print('folder1')
dataFolder = '1-poisson_spikes'
dims=[]
times=[]
timecalctotfolder1=0
for j in range(20):
    for i in range(20):
        nomecartella = str(i+1)+'-N'
        nomefile = str(j+1)+'.txt'
        fileCorrente = dataFolder +'/'+ nomecartella +'/'+ nomefile
        with open(fileCorrente, "r") as a_file:
            data = [[float(num) for num in line.strip().split(',')] for line in a_file]
        list_of_lists=np.array(data)
        treno1=list(list_of_lists[:,0])
        treno2=list(list_of_lists[:,1])
        dim=np.mean([len(np.array(treno1)[np.array(treno1)!=0]),len(np.array(treno2)[np.array(treno2)!=0])])
        dim1=len(np.array(treno1)[np.array(treno1)!=0])
        dim2=len(np.array(treno2)[np.array(treno2)!=0])
        treno1=list(np.array(treno1)[np.array(treno1)!=0])
        treno2=list(np.array(treno2)[np.array(treno2)!=0])
        start = time.time()
        myAccuracy,myPrecision,myF1score,myRecall = STSimM_cy(300,treno1,treno2,paramC,lamb,omega,0.5)
        end = time.time()
        timecalc=end-start
        timecalctotfolder1=timecalctotfolder1+timecalc
        with open('All_accuracy_STSimM_mean_3_10_035.txt', 'a') as f:
            f.write('%f\t' % dim)
            f.write('%f\t' % timecalc)
            f.write('%f\t' % dim1)
            f.write('%f\t' % dim2)
            f.write('%f\t' % myAccuracy)
            f.write('%s\t' % nomecartella)
            f.write('%s\n' % nomefile)
        dims.append(dim)
        times.append(timecalc)
print(timecalctotfolder1)
print('folder2')
dataFolder = '2-poisson_bursts'
timecalctotfolder2=0
medieAll = []
for j in range(20):
    for i in range(20):
        nomecartella = str(i+1)+'-N'
        nomefile = str(j+1)+'.txt'
        fileCorrente = dataFolder +'/'+ nomecartella +'/'+ nomefile
        with open(fileCorrente, "r") as a_file:
            data = [[float(num) for num in line.strip().split(',')] for line in a_file]
        list_of_lists=np.array(data)
        treno1=list(list_of_lists[:,0])
        treno2=list(list_of_lists[:,1])
        dim=np.mean([len(np.array(treno1)[np.array(treno1)!=0]),len(np.array(treno2)[np.array(treno2)!=0])])
        dim1=len(np.array(treno1)[np.array(treno1)!=0])
        dim2=len(np.array(treno2)[np.array(treno2)!=0])
        treno1=list(np.array(treno1)[np.array(treno1)!=0])
        treno2=list(np.array(treno2)[np.array(treno2)!=0])
        start = time.time()
        myAccuracy,myPrecision,myF1score,myRecall = STSimM_cy(3600,treno1,treno2,paramC,lamb,omega,0.5)
        end = time.time()
        timecalc=end-start
        timecalctotfolder2=timecalctotfolder2+timecalc
        with open('All_accuracy_STSimM_mean_3_10_035.txt', 'a') as f:
            f.write('%f\t' % dim)
            f.write('%f\t' % timecalc)
            f.write('%f\t' % dim1)
            f.write('%f\t' % dim2)
            f.write('%f\t' % myAccuracy)
            f.write('%s\t' % nomecartella)
            f.write('%s\n' % nomefile)
        dims.append(dim)
        times.append(timecalc)
print(timecalctotfolder2)
print('folder4')
dataFolder = '4-sub-bursts'
timecalctotfolder4=0
medieAll = []
for j in range(21):
    for i in range(20):
        nomecartella = str(i+1)+'-N'
        nomefile = str(j+1)+'.txt'
        fileCorrente = dataFolder +'/'+ nomecartella +'/'+ nomefile
        with open(fileCorrente, "r") as a_file:
            data = [[float(num) for num in line.strip().split(',')] for line in a_file]
        list_of_lists=np.array(data)
        treno1=list(list_of_lists[:,0])
        treno2=list(list_of_lists[:,1])
        dim=np.mean([len(np.array(treno1)[np.array(treno1)!=0]),len(np.array(treno2)[np.array(treno2)!=0])])
        dim1=len(np.array(treno1)[np.array(treno1)!=0])
        dim2=len(np.array(treno2)[np.array(treno2)!=0])
        treno1=list(np.array(treno1)[np.array(treno1)!=0])
        treno2=list(np.array(treno2)[np.array(treno2)!=0])
        start = time.time()
        myAccuracy,myPrecision,myF1score,myRecall = STSimM_cy(300,treno1,treno2,paramC,lamb,omega,0.5)
        end = time.time()
        timecalc=end-start
        timecalctotfolder4=timecalctotfolder4+timecalc
        with open('All_accuracy_STSimM_mean_3_10_035.txt', 'a') as f:
            f.write('%f\t' % dim)
            f.write('%f\t' % timecalc)
            f.write('%f\t' % dim1)
            f.write('%f\t' % dim2)
            f.write('%f\t' % myAccuracy)
            f.write('%s\t' % nomecartella)
            f.write('%s\n' % nomefile)
        dims.append(dim)
        times.append(timecalc)
print(timecalctotfolder4)
uniquedims=list(set(dims))
for item in sorted(uniquedims):
    listdims=[i for i,x in enumerate(dims) if x==item]
    meantime=np.mean([times[i] for i in listdims])
    stdtime=np.std([times[i] for i in listdims])
    with open('MeanStd_Mean_accuracy_STSimM_3_10_035.txt', 'a') as f:
        f.write('%f\t' % item)
        f.write('%f\t' % meantime)
        f.write('%f\n' % stdtime)