from ploting import *
from neuron import *
from numpy import *
from fixedpoints import *
from stimulus import *
#import matplotlib.animation as animation

#################################
#structure of the network
N=3 #number of neurons
struct=ones(N) # excitatory or inhibitory neurons 

##############################
#building intial conditions
u0=1*array([1,-1,-2])#*random.randn(N)
w0=0.1*random.randn(N,N)#ones((N,N))#random.randn(N, N)
w0=list(w0.reshape(N*N))
y0=list(u0)+w0
u0=array(u0)
y0=array(y0)
################################
##Building patterns
times=1
delay_inter=1000
delay_intra=40
n=2.0
v1=array([1,0,0])#*(1/sqrt(2))
v2=array([0,1,0])#*(1/sqrt(2))
v3=array([0,0,1])#*(1/sqrt(2))
patterns=[v1,-0.3*v1]#random.binomial(1,0.5,N)i
mystim=stimulus(patterns,delay_inter,delay_intra,times)
mystim.tstim=90
mystim.inten=1.9
mystim.delay_begin=700
dt=0.1
tmax=mystim.delay_begin+times*n*(mystim.tstim+delay_inter)-times*delay_inter+times*delay_intra+700


###########Building network#######
ubar=array([0.3,0.3,0.3])#0.1*random.randn(N)#array([2,3])
net=network(u0,struct)
net.covmean(array(ubar))
net.stim(mystim.stim)
net.homeostatic(0)
net.alpha=20
net.v12=0
dyn=net.dynamics(y0,dt,tmax)


#this code is for calculate the fixed points
#fixed_pnts_list=[]
#fixedpnts=fixedpoints(mystim.inten*mystim.patterns[0],net)
#for i in range(0,N):
#	fixedpnts.I=mystim.inten*mystim.patterns[i]
#	fixed_pnts_list.append(fixedpnts.FixPntsCov())
#print(fixed_pnts_list)
#################################
#######Ploting code#############

npts=len(dyn)
t  = linspace(0,tmax,npts) 
potential1=[t]
potential2=[t]
potential3=[t]
net.Tausyn=2
for i in range(0,8):
	mystim.inten=1.9+i*0.5
	net.stim(mystim.stim)
	net.homeostatic(0)
	dyn=net.dynamics(y0,dt,tmax)
	potential1.append([net.rate(dyn[i][0]) for i in range(0,npts)])
	potential2.append([net.rate(dyn[i][1]) for i in range(0,npts)])
	potential3.append([net.rate(dyn[i][2]) for i in range(0,npts)])
	print(i)

# to study the connectivity matrix
connect=[]
e1=[]
e2=[]
e3=[]
tot=[]
for i in range(0,npts):
	wk=[]
	for j in range(N,N*N+N):
		wk.append(dyn[i][j])
	wk=array(wk)
	wk=wk.reshape((N,N))
	connect.append(wk)
	U, s, V = linalg.svd(wk, full_matrices=True)
	e1.append(abs(dot(v1,dot(wk,v1)))/sum(s))
	e2.append(abs(dot(v2,dot(wk,v2)))/sum(s))
	e3.append(abs(dot(v3,dot(wk,v3)))/sum(s))
	tot.append(e1[i]+e2[i]+e3[i])
print(connect[0])
print(connect[npts-1])


#This code is for the fixed matrix
#
#times2=1
#delay_inter2=500.
#delay_intra2=10.0
#n2=1
#patterns2=[v1,v2]#random.binomial(1,0.5,N)i
#mystim2=stimulus(patterns2,delay_inter2,delay_intra2,times2)
#mystim2.tstim=20
#mystim2.inten=10
#tmax2=times2*n2*(mystim2.tstim+delay_inter2)-times2*delay_inter2+times2*delay_intra2+1500
#
#u02=array([-30,-30,-30])
#a=[1,1,0,1,1,0,0,0,0]
#y02=list(u02)+list(connect[npts-1].flatten())
#y02=array(y02)
#net2=network(u02,struct)
#net2.covmean(array(ubar))
#net2.stim(mystim2.stim)
#net2.w0=connect[npts-1]
#net2.homeostatic(3)# fixed weight matrix
#
############Building network#######
#
#dyn2=net2.dynamics(y02,dt,tmax2)
#
#npts2=len(dyn2)
#t2  = linspace(0,tmax2,npts2) 
#potential2=[t2]
#for j in range(0,N):
#	potential2.append([net2.rate(dyn2[i][j]) for i in range(0,npts2)])
#

modes=[t,e1,e2,e3,tot]
#print(linalg.eig(connect[0]))
#print(linalg.eig(connect[npts-1]))
#potential.append(e1)
#potential.append(e2)
#potential.append(e3)

elstim1=[(0.05*mystim.stim(x)[0]/mystim.inten)+0.4 for x in t]
elstim2=[(0.05*mystim.stim(x)[1]/mystim.inten)+0.4 for x in t]
elstim3=[(0.05*mystim.stim(x)[2]/mystim.inten)+0.4 for x in t]
#potential.append(elstim1)
#potential.append(elstim3)
#potential=potential+elstim
#estimulacion=[t,elstim]
plt=myplot()
plt.col='b'
plt.dynamics(potential1)
plt.col='r'
plt.dynamics(potential2)
plt.col='g'
plt.dynamics(potential3)
#plt.col='m'
#plt.dynamics([t,elstim1])
plt.laby='Rate'
plt.title='DSM experiment'
plt.present()
#plt.dynamics(modes)
i#plt.dynamics(potential2)
