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

paramC = 'auto' #3
lamb = 'auto' #10
omega = 'auto' #0.35

neuroni = ['2','3','5','6','7','8','10','11','13','14']#,
pesi = ['005','015','025','035','045','055','065','075','085','095','105','115','125']
pesiTraceFileNames = ['0.05','0.15','0.25','0.35','0.45','0.55','0.65','0.75','0.85','0.95','1.05','1.15','1.25']
dims=[]
times=[]
timecalctotfolder=0
for i in range(13):
    for j in range(10):
        os.chdir('tSpike_sperimentali_fino_a_1.25')
        os.chdir('tmp')
        nomefileExp = 'tempi_l23-06-13.res.6-tt6clu'+ neuroni[j] +'_'+ pesiTraceFileNames[i] +'+1_prova20122022.txt'
        treno1 = np.loadtxt(nomefileExp, usecols=(0),dtype=np.float64, unpack=True)
        os.chdir('..')
        os.chdir('..')
        os.chdir('tSpikes_AGLIF_040')
        os.chdir('tSpikes')
        nomefileSim = 'Neuron_'+ neuroni[j] +'_'+  pesi[i]   +'_t_spk_simulated.txt'
        treno2 = np.loadtxt(nomefileSim, usecols=(0),dtype=np.float64, unpack=True)
        os.chdir('..')
        os.chdir('..')
        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])
        if dim1==1:
            treno1=[treno1.tolist()]
        else:
            treno1=list(treno1)
        if dim2==1:
            treno2=[treno2.tolist()]
        else:
            treno2=list(treno2)
        if dim1==0 or dim2==0:
            start = time.time()
            myAccuracy=0
            end = time.time()
        else:
            start = time.time()
            myAccuracy,myPrecision,myF1score,myRecall = STSimM_cy(600000,treno1,treno2,paramC,lamb,omega,1)
            end = time.time()
        timecalc=end-start
        timecalctotfolder=timecalctotfolder+timecalc
        with open('All_accuracy_STSimM_AGLIF_mean_auto_peso1.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' % neuroni[j])
            f.write('%s\n' % pesiTraceFileNames[i])
        dims.append(dim)
        times.append(timecalc)
print(timecalctotfolder)
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_AGLIF_auto_peso1.txt', 'a') as f:
        f.write('%f\t' % item)
        f.write('%f\t' % meantime)
        f.write('%f\n' % stdtime)