// $Id: params.hoc,v 1.113 2010/12/29 03:14:38 cliffk Exp $

print "Loading params.hoc..."

//* CK's params
{declare("attendmod",1)} // Modulate weights and/or firing rates
if(b_d_p>1) {attendmod=0.5}


//* batch params
jrsvn_INTF6=1e3
jrsvd_INTF6=2e4

//* general params
{declare("mytstop",5e3)}
tstop = mytstop
v_init=1000 // so keep v's as set randomly

//* ARTC params
mg_INTF6=1.6
EAM_INTF6=65 // these are deviations above RMP
ENM_INTF6=90
EGA_INTF6=-15
seadsetting_INTF6 = 2 // fixed inter-cell weights

//* Declarations

dosetexvals=name_declared("wmatex")==0 // whether to set values in wmatex,ratex

{sprint(tstr,"d[%d][%d]",CTYPi,STYPi)}
{declare("wmatex",tstr,"ratex",tstr)} // weights,avg. rate of external inputs

{declare("fih","o[1]","nstim","o[1]")}
{vseed_stats(223481)}
{rdm.MCellRan4(seed_stats)}
{declare("vsgrpp",new Vector(CTYPi))} //% X 100 of cells of a type to stim, used in stim,sgrcells
{declare("vsgrsidx",new Vector(CTYPi))} //startind index of cell to stim when using topstim
{declare("sgrdur",mytstop)} //duration of stim
{declare("inputseed",1234)}
{declare("sgrhzE",300,"sgrhzI",125,"sgrhzNM",50,"sgron",1,"sgrdel",0)}
{declare("sgrhzdel",0.2)}//variance in sgrhz for an INTF6
{declare("EXGain",15)} // gain for external inputs
{declare("lcstim",new List())} // list of CSTIM objects
{declare("nqwmex",new NQS("ct","sy","w","rate"))} // NQS passed to CSTIM init
{sprint(tstr,"d[%d]",numcols)}
{declare("EIBalance",tstr)} // whether to balance rate of external E/I inputs
{declare("usens",1)} // whether to use NetStim in CSTIM templates -- saves memory

SCAHP=SCTH=1

//* shock-related params
{sprint(tstr,"d[%d][%d][%d]",numcols,CTYPi,STYPi)}
{declare("wshock",tstr,"durshock",tstr,"prctshock",tstr,"nshock",tstr,"isishock",tstr,"startshock",tstr)}

//* setwmatex - set weights of external inputs to INTF6s
proc setwmatex () {  local ct,sy
  if(dosetexvals) {
    for ct=0,CTYPi-1 for sy=0,STYPi-1 wmatex[ct][sy]=0
    for ct=0,CTYPi-1 {
      ratex[ct][AM2]=sgrhzE
      ratex[ct][NM2]=sgrhzNM
      ratex[ct][GA2]=ratex[ct][GA]=sgrhzI
      if(IsLTS(ct)) {
        wmatex[ct][AM2] = 0.2
        wmatex[ct][NM2] = 0.025
        wmatex[ct][GA]=wmatex[ct][GA2]=0.125
      } else {
        wmatex[ct][NM2] = 0.05   
        wmatex[ct][AM2] = 0.25 
        wmatex[ct][GA]=wmatex[ct][GA2]=0.125
      }
      for sy=0,STYPi-1 wmatex[ct][sy] *= EXGain // apply gain control
    }    
  }
  for i=0,ncellpops-1 ratex[pop2.x[i]][AM2] *= attendmod // @@@ CK: Add attentional modulation for the cell populations listed in batch.hoc
  nqwmex.clear()
  for ct=0,CTYPi-1 for sy=0,STYPi-1 if(wmatex[ct][sy]) nqwmex.append(ct,sy,wmatex[ct][sy],ratex[ct][sy])
}

//* RSparams - setup regular spiking excitatory cells
proc RSparams () { local ii,jj localobj xo,co
  for ltr(co,lcol) for case(&ii,E2,E4,E5R,E5B,E6) if(co.numc[ii]>0) {
    for jj=co.ix[ii],co.ixe[ii] { xo=co.ce.o(jj)
      
      xo.ahpwt=1
      xo.tauahp=400
      xo.RMP= -65
      xo.VTH= -40 
      xo.refrac=  50
      xo.Vblock= -25
      
      xo.tauGA  = 10
      xo.tauGA2 = 20
      xo.tauAM2 = 20
      xo.tauNM2 = 300
      
      xo.tauRR = 8 
      xo.RRWght = .75 
    }
  }
}

//* LTSparams - setup low-threshold spiking interneurons
proc LTSparams () { local ii,jj localobj xo,co
  for ltr(co,lcol) for case(&ii,I2L,I4L,I5L,I6L) if(co.numc[ii]>0) {
    for jj=co.ix[ii],co.ixe[ii] { xo=co.ce.o(jj)  
      xo.ahpwt=0.5
      xo.refrac=10 // @@@ CK: was 5, changed back to 10 on 21/03/11
      xo.tauahp=50
      xo.Vblock=-10    
      xo.RMP = -65
      xo.VTH= -47

      xo.tauGA  = 10
      xo.tauGA2 = 20
      xo.tauAM2 = 20
      xo.tauNM2 = 300
      
      xo.tauRR = 1.5
      xo.RRWght = 0.25
    }
  }
}

//* FSparams - setup fast spiking interneurons
proc FSparams () { local ii,jj localobj xo,co
  for ltr(co,lcol) for case(&ii,I2,I2C,I4,I5,I6,I6C) if(co.numc[ii]>0) {
    for jj=co.ix[ii],co.ixe[ii] { xo=co.ce.o(jj)    
      xo.ahpwt=0.5
      xo.refrac=10 // @@@ CK: was 5, changed back to 10 on 21/03/11
      xo.tauahp=50
      xo.Vblock=-10    
      xo.RMP = -63
      xo.VTH= -40

      xo.tauGA  = 10
      xo.tauGA2 = 20
      xo.tauAM2 = 20
      xo.tauNM2 = 300
      
      xo.tauRR = 1.5
      xo.RRWght = 0.25
    }
  }
}

//* setNMParams(col,celltype,mg0 factor,maxnmc) 
proc setNMParams () { local ct,mgf,maxnmc,idx localobj col,ce
  col=$o1 ce=col.ce ct=$2 mgf=$3 maxnmc=$4
  for idx=col.ix[ct],col.ixe[ct] {
    ce.o(idx).mg0 = 3.57 * mgf
    ce.o(idx).maxnmc = maxnmc
  }
}

//* setNMIParams(col[,mg0 factor,maxnmc])
proc setNMI () { local mgf,maxnmc,ct localobj col
  col=$o1
  if(numarg()>1) mgf=$2 else mgf=1 
  if(numarg()>2) maxnmc=$3 else maxnmc=1
  for ct=0,CTYPi-1 if(ice(ct)) setNMParams(col,ct,mgf,maxnmc)
}

//* schizon - turn on schizo params
proc schizon () { local c,ct
  for c=0,numcols-1 {
    setNMI(col[c],0.75,0.75)
    setNMParams(col[c],E4,1.25,1.25)
    for case(&ct,E2,E5R,E5B) setNMParams(col[c],ct,0.75,0.9)
  }
}
//* schizon - turn off schizo params
proc schizoff () { local c,ct
  for c=0,numcols-1 {
    setNMI(col[c],1,1)
    setNMParams(col[c],E4,1,1)
    for case(&ct,E2,E5R,E5B) setNMParams(col[c],ct,1,1)
  }
}

//* setcstim - set external inputs to COLUMNs using CSTIM
// REALDATA
// xopen("realinput.hoc") // Load procedures for using real data
// proc setrealstim () { local i,seed
// END-REALDATA
proc setcstim () { local i,seed
  for i=0,lcol.count-1 {
    if(dbgcols)seed=inputseed else seed=(i+1)*inputseed
	// REALDATA
    // lcstim.append(new REALSTIM(lcol.o(i),seed,sgrdur,sgrhzdel,0)) // Changed from CSTIM to REALSTIM and set EIBalance=0
	// END-REALDATA    
	lcstim.append(new CSTIM(lcol.o(i),seed,sgrdur,sgrhzdel,EIBalance[i],usens))
    lcstim.o(lcstim.count-1).setwm(nqwmex)
    lcstim.o(lcstim.count-1).setspks()
  }
}

//* clrshock -- clear all shock params and actual shocks
proc clrshock () { local i,j,k
  for i=0,numcols-1 for j=0,CTYPi-1 for k=0,STYPi-1 {
    wshock[i][j][k]=durshock[i][j][k]=prctshock[i][j][k]=nshock[i][j][k]=isishock[i][j][k]=startshock[i][j][k]=0
  }
  setshock(1)
}


//* setshock([clear vq first]) - set stims to L4 , NB: DEFAULT IS TO CLEAR vq FIRST
proc setshock () { local clr,i,j,k,l,tt,set
  if(numarg()>0) clr=$1 else clr=1
  for i=0,numcols-1 {
    set=0 // whether setting new spikes
    if(clr) {col[i].cstim.vq.clear() col[i].ce.o(0).clrvspks()} // clear shocks to this column?
    for j=0,CTYPi-1 for k=0,STYPi-1 if(wshock[i][j][k]>0 && nshock[i][j][k]>0) {
      set = 1
      tt = startshock[i][j][k] // time
      for l=0,nshock[i][j][k]-1 {
        col[i].cstim.shock(durshock[i][j][k],prctshock[i][j][k],j,tt,9342*(i+1)*(j+1)*(k+1)*(l+1),k,wshock[i][j][k]) 
        tt += isishock[i][j][k] // inc by ISI
      }
    }
    if(set) {
      col[i].cstim.pushspks()
      print col[i].cstim.vq.size(-1)
    }
  }
}

//* function calls

setwmatex()

{RSparams() LTSparams() FSparams()}

if (!jcn) rjinet() //means no jitcon

if(disinhib) inhiboff()

setcstim()