import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import scipy.io as scio


dat = scio.loadmat('.//alle 3_30v.mat')

Ka1 = np.concatenate(np.transpose(dat['A']),axis=0)[0:-1:2]
Ka2 = np.concatenate(np.transpose(dat['B']),axis=0)[0:-1:2]
Ka3 = np.concatenate(np.transpose(dat['C']),axis=0)[0:-1:2]



t = np.linspace(0,dat['Length'][0][0]*dat['Tinterval'][0][0],dat['Length'][0][0])[0:-1:2]


del dat

vth = 13

tmp = np.sign(Ka1-vth)
NST = np.diff(tmp[:])
idx1 = np.where(NST[:]<-1)        
tmp1 = t[idx1]
ISI1 = np.diff(tmp1[:])

tmp = np.sign(Ka2-vth)
NST = np.diff(tmp[:])
idx2 = np.where(NST[:]<-1)        
tmp2 = t[idx2]
ISI2 = np.diff(tmp2[:])

tmp = np.sign(Ka3-vth)
NST = np.diff(tmp[:])
idx3 = np.where(NST[:]<-1)        
tmp3 = t[idx3]
ISI3 = np.diff(tmp3[:])

plt.rcParams.update({'font.family':'Calibri'})
plt.rc('xtick', labelsize=14)
plt.rc('ytick', labelsize=14)

plt.figure(figsize=(10,5))

plt.subplot(121)
plt.plot(tmp1[1::],ISI1,'darkorange',label='Oscillator 1')
plt.plot(tmp2[1::],ISI2,'royalblue',label='Oscillator 2')
plt.plot(tmp3[1::],ISI3,'crimson',label='Oscillator 3')
#plt.vlines(20,0.1,0.6, linestyle='dashed', color='black' )
#plt.vlines(21,0.1,0.6, linestyle='dashed', color='black' )
#plt.vlines(23,0.1,0.6, linestyle='dashed', color='black' )
#plt.vlines(24,0.1,0.6, linestyle='dashed', color='black' )
#plt.vlines(28,0.1,0.6, linestyle='dashed', color='black' )
#plt.vlines(31,0.1,0.6, linestyle='dashed', color='black' )
#plt.vlines(33,0.1,0.6, linestyle='dashed', color='black' )
#plt.vlines(71,0.1,0.6, linestyle='dashed', color='black' )

#plt.gca().add_patch(Rectangle((44,0.05),11,0.6,linewidth=1.5,linestyle='--',edgecolor='black',facecolor='none'))

plt.xlabel('t (s)', fontsize = 20)
plt.ylabel('ISI (s)', fontsize=20)
#plt.title('a', fontsize=14)
plt.legend(fontsize=14)
plt.tight_layout()
plt.minorticks_on()


lineoffsets1 = np.array([1,2,3])

top=0.97
bottom=0.126
left=0.049
right=0.985
hspace=0.2
wspace=0.164

plt.subplot(122)
plt.eventplot([tmp1,tmp2,tmp3], color=['darkorange','royalblue', 'crimson'], linelengths = 0.9, lineoffsets=lineoffsets1) #0.11)  
plt.xlim(44,55)   
plt.minorticks_on()
plt.xlabel('t (s)', fontsize=20)
plt.ylabel('Oscillator', fontsize = 20)
plt.subplots_adjust(left, bottom, right, top, wspace, hspace)
#plt.title('b', fontsize=14)
plt.show()


import math
from matplotlib import pyplot as plt

y = [0.17, 1.17, 2.98, 3.15, 3.5]

minimum_ele = min(y)
maximum_ele = max(y)
new_list = range(math.floor(min(y)), math.ceil(max(y))+1)

plt.yticks(new_list)
plt.ylim(0.5, 3.5)
#plt.show()
#plt.tight_layout()
#plt.savefig('.//DyN_ISI.png', dpi=1000)