Random Spikes

Particularly useful chunks of hoc and/or NMODL code. May be pedestrian or stunningly brilliant, may make you gasp or bring tears to your eyes, but always makes you think "I wish I had written that; I'm sure going to steal it."
Post Reply
Samuel_30
Posts: 10
Joined: Wed Nov 16, 2016 5:13 pm

Random Spikes

Post by Samuel_30 »

Hello,

I am a beginner in computational neuroscience and NEURON.

I am developing a code for a masters research, with my adviser, and I want to perfect some mechanisms.
I have many difficulties (the first is the language).
I've studied the whole tutorial, but I need some more basic tips.

Code: Select all

load_file("nrngui.hoc")

begintemplate SThcell
public soma, nclist
public x, y, z
public sinapse
public conta

create soma
objectvar nclist
objectvar sinapse
objectvar conta

proc init() {
    x=$1
    y=$2
    z=$3

    create soma
    nclist=new List()


    soma {
      pt3dclear()
      pt3dadd(x,y,z,50)
      pt3dadd(x+50,y,z,50) 	
      nseg = 1
      diam = 18.8
      L = 18.8
      Ra = 123.0
      insert hh
	sinapse=new ExpSynSTDP_triplet(0)
      sinapse.e=0
      sinapse.tau=10
      sinapse.factor=1//potentiation depotentiation factor
 	sinapse.gw=1e-5 //uS max conductance
    }

 
}
endtemplate SThcell


//Usa-se os templates

objref neuron[2]
neuron[0]=new SThcell(0,0,0)
neuron[1]=new SThcell(100,0,0)

CONNECTION_PROB=1
CONNECTION_THRESHOLD=0
CONNECTION_DELAY=0
CONNECTION_WEIGHT=1e-5 // max conductance uS

access neuron[0].soma
neuron[0].soma neuron[1].nclist.append(new NetCon(&neuron[0].soma.v(0.5),neuron[1].sinapse,CONNECTION_THRESHOLD,CONNECTION_DELAY,CONNECTION_WEIGHT))


objectvar stim[8]

neuron[0].soma {
    stim[0] = new IClamp(0.5)
    stim[0].del = 100
    stim[0].dur = 1
    stim[0].amp = 0.1

    stim[1] = new IClamp(0.5)
    stim[1].del = 300
    stim[1].dur = 1
    stim[1].amp = 0.0

    stim[2] = new IClamp(0.5)
    stim[2].del = 500
    stim[2].dur = 1
    stim[2].amp = 0.0

    stim[3] = new IClamp(0.5)
    stim[3].del = 4000 //700
    stim[3].dur = 1
    stim[3].amp = 0.0

}

neuron[1].soma {
    stim[4] = new IClamp(0.5)
    stim[4].del = 110
    stim[4].dur = 1
    stim[4].amp = 0.1

    stim[5] = new IClamp(0.5)
    stim[5].del = 300
    stim[5].dur = 1
    stim[5].amp = 0.0

    stim[6] = new IClamp(0.5)
    stim[6].del = 500
    stim[6].dur = 1
    stim[6].amp = 0.0

    stim[7] = new IClamp(0.5)
    stim[7].del = 700
    stim[7].dur = 1
    stim[7].amp = 0.0

}


tstop = 1000

objref grafico[7]

grafico[0]=new Graph()
grafico[0].size(0,tstop ,-100,50)
grafico[0].beginline()
grafico[0].addvar("Pre",&neuron[0].soma.v(0.5),1,1)
grafico[0].addvar("Pos",&neuron[1].soma.v(0.5),2,1)
grafico[0].flush()

grafico[2]=new Graph()
grafico[2].size(0,tstop ,0,10)
grafico[2].beginline()
grafico[2].addvar("Gsyn",&neuron[1].sinapse.g,3,1)
grafico[2].flush()

grafico[4]=new Graph()
grafico[4].size(0,tstop ,-30,20)
grafico[4].beginline()
grafico[4].addvar("peso",&neuron[1].sinapse.peso,3,1)
grafico[4].flush()

grafico[5]=new Graph()
grafico[5].size(0,tstop ,-30,20)
grafico[5].beginline()
grafico[5].addvar("O1",&neuron[1].sinapse.O1,1,1)
grafico[5].addvar("O2",&neuron[1].sinapse.O2,2,1)
grafico[5].addvar("R1",&neuron[1].sinapse.R1,3,1)
grafico[5].addvar("R2",&neuron[1].sinapse.R2,4,1)
grafico[5].flush()


graphList[0].append(grafico[0])
graphList[0].append(grafico[1])
graphList[0].append(grafico[2])
graphList[0].append(grafico[3])
graphList[0].append(grafico[4])
graphList[0].append(grafico[5])
graphList[0].append(grafico[6])

run() 


This is an hoc code of two HH neurons, connected by t-STDP.

Need:

1. Insert new excitatory synapses that connect in my STDP synapse and generate stimuli, aiming to make the model more "realistic".

P.S .: I think I should use Net con and Net stim, but I do not know how to do it correctly.

2. This code has a mod file for the STDP synapse. I need to build others, representing the ion channels. I know I find this in the tutorial. But, my level of knowledge is so low, I need to know how to call a mod file in an hoc code.

Sorry for the inexperience. I am in the learning stage and it is being very challenging!


My final goal will be to build a neocortical network to simulate the mirror system in verbal coding. An unprecedented research in computational neuroscience.
Last edited by Samuel_30 on Tue Jan 24, 2017 10:46 pm, edited 1 time in total.
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: [Basic] Insert synapses and stimuli into two neurons con

Post by ted »

Samuel_30 wrote:I've studied the whole tutorial
There are lots of tutorials. Not all are equally useful. Which one do you mean?
Need:
1. Insert new excitatory synapses that connect in my STDP synapse and generate stimuli, aiming to make the model more "realistic".
P.S .: I think I should use Net con and Net stim, but I do not know how to do it correctly.
The Programmer's Reference is your friend--you'll find a link to it at the top of every page on NEURON's web site http://www.neuron.yale.edu/.

You'll also find a link to the very informative article
Hines, M.L. and Carnevale, N.T.
Translating network models to parallel hardware in NEURON.
J. Neurosci. Methods 169:425-455, 2008
at http://www.neuron.yale.edu/neuron/nrnpubs

"But I'm not ready to parallelize anything--I don't even have a model yet!"

Read the article anyway. Focus on the two examples of serial (non-parallelized) code, which illustrate not only how to use hoc to accomplish many tasks, but also provide a good pattern for well-organized, modular code. The source code is included in the preprint available from the above URL, and you can also download complete source code for this paper from ModelDB https://senselab.med.yale.edu/ModelDB/

If you run into keywords that you don't understand, see the Programmer's Reference. If you're not sure about hoc or its syntax, get this short set of slides
http://www.neuron.yale.edu/neuron/stati ... slides.pdf
and/or check out
https://www.neuron.yale.edu/neuron/stat ... g/hoc.html
2. This code has a mod file for the STDP synapse.
Does it? Where did it come from?
I need to build others, representing the ion channels. I know I find this in the tutorial. But, my level of knowledge is so low, I need to know how to call a mod file in an hoc code.
Sorry for the inexperience. I am in the learning stage and it is being very challenging!
Sounds like you don't have the NEURON Book. You may find it helpful to get an overview of NEURON by reading
Hines, M.L. and Carnevale, N.T.
The NEURON simulation environment.
Neural Computation 9:1179-1209, 1997
(preprint available from http://www.neuron.yale.edu/neuron/nrnpubs)

You may also find it helpful to read parts of
Hines, M.L. and Carnevale, N.T.
Expanding NEURON's repertoire of mechanisms with NMODL.
Neural Computation 12:995-1007, 2000
(enhanced preprint available from http://www.neuron.yale.edu/neuron/nrnpubs)

It may not be necessary to write any new NMODL code--if you need ion channels or other mechanisms that aren't built into NEURON, you may be able to find mod files in ModelDB that are exactly what you need, or close enough that only minor changes are required.
My final goal will be to build a neocortical network to simulate the mirror system in verbal coding. An unprecedented research in computational neuroscience.
That's a worthy and ambitious project. Has anybody made an initial stab at it?
Samuel_30
Posts: 10
Joined: Wed Nov 16, 2016 5:13 pm

Re: [Basic] Insert synapses and stimuli into two neurons con

Post by Samuel_30 »

There are lots of tutorials. Not all are equally useful. Which one do you mean?
In this one: http://www.anc.ed.ac.uk/school/neuron/
The Programmer's Reference is your friend--you'll find a link to it at the top of every page on NEURON's web site http://www.neuron.yale.edu/.
The Programmer's Reference has the item NetCon that will be useful to me. But could you show an example of how to insert connections in my model and how can I stimulate the neurons from these connections?

Does it? Where did it come from?

My advisor who created. It is an alpha synapse code.
Can you tell how many types of .mod mechanisms can be created with NEURON, and cite some code that calls these mechanisms?



Many thanks for all the references. The work plan of my research is sending by email.
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: [Basic] Insert synapses and stimuli into two neurons con

Post by ted »

Samuel_30 wrote:
There are lots of tutorials. Not all are equally useful. Which one do you mean?
In this one: http://www.anc.ed.ac.uk/school/neuron/
. . . could you show an example of how to insert connections in my model and how can I stimulate the neurons from these connections?
Gillies & Sterratt used NetCons to connect spike sources (presynaptic model cells) to target synaptic mechanisms on postsynaptic model cells. I thought their example code was fairly clear. So are the examples in Hines & Carnevale 2008. The Programmer's Reference entry about the NetCon class spells out what a NetCon does, and what kinds of things can be targets. I'm not sure what more I can add.
My advisor who created. It is an alpha synapse code.
Does the NMODL code that defines it have a NET_RECEIVE block that specifies what happens when an event arrives, and how synaptic plasticity is implemented?
Can you tell how many types of .mod mechanisms can be created with NEURON
You mean with NMODL. Effectively unlimited (I hesitate to say "aleph null")--whatever can be described by a mix of algebraic equations, ordinary differential equations, reaction schemes, and event-driven state machines.
cite some code that calls these mechanisms?
Among the NEURON models in ModelDB you'll find roughly 1000 or so. Some are used in more than one model entry.
Samuel_30
Posts: 10
Joined: Wed Nov 16, 2016 5:13 pm

Re: [Basic] Insert synapses and stimuli into two neurons con

Post by Samuel_30 »

Hi Ted,

I want to introduce spikes in my model, more or less that way. See the NetStim I've inserted. But it did not work.

Code: Select all

load_file("nrngui.hoc")

begintemplate SThcell
public soma, nclist
public x, y, z
public sinapse
public conta

create soma
objectvar nclist
objectvar sinapse
objectvar conta

proc init() {
    x=$1
    y=$2
    z=$3

    create soma
    nclist=new List()


    soma {
      pt3dclear()
      pt3dadd(x,y,z,50)
      pt3dadd(x+50,y,z,50) 	
      nseg = 1
      diam = 18.8
      L = 18.8
      Ra = 123.0
      insert hh
	sinapse=new ExpSynSTDP_triplet(0)
      sinapse.e=0
      sinapse.tau=10
      sinapse.factor=1//potentiation depotentiation factor
 	sinapse.gw=1e-5 //uS max conductance
    }

NetStim parameters


tsyn=20
intrvl=10
nmbr=900
noisevar=0.0

s = new NetStim(0.5)
s.interval=intrvl   // ms (mean) time between spikes
s.number=nmbr     // (average) number of spikes
s.start=tsyn   // ms (mean) start time of first spike
s.noise=0      // range 0 to 1. Fractional randomness.
 
}
endtemplate SThcell


//Usa-se os templates

objref neuron[2]
neuron[0]=new SThcell(0,0,0)
neuron[1]=new SThcell(100,0,0)

CONNECTION_PROB=1
CONNECTION_THRESHOLD=0
CONNECTION_DELAY=0
CONNECTION_WEIGHT=1e-5 // max conductance uS

access neuron[0].soma
neuron[0].soma neuron[1].nclist.append(new NetCon(&neuron[0].soma.v(0.5),neuron[1].sinapse,CONNECTION_THRESHOLD,CONNECTION_DELAY,CONNECTION_WEIGHT))


objectvar stim[8]

neuron[0].soma {
    stim[0] = new IClamp(0.5)
    stim[0].del = 100
    stim[0].dur = 1
    stim[0].amp = 0.1

    stim[1] = new IClamp(0.5)
    stim[1].del = 300
    stim[1].dur = 1
    stim[1].amp = 0.0

    stim[2] = new IClamp(0.5)
    stim[2].del = 500
    stim[2].dur = 1
    stim[2].amp = 0.0

    stim[3] = new IClamp(0.5)
    stim[3].del = 4000 //700
    stim[3].dur = 1
    stim[3].amp = 0.0

}

neuron[1].soma {
    stim[4] = new IClamp(0.5)
    stim[4].del = 110
    stim[4].dur = 1
    stim[4].amp = 0.1

    stim[5] = new IClamp(0.5)
    stim[5].del = 300
    stim[5].dur = 1
    stim[5].amp = 0.0

    stim[6] = new IClamp(0.5)
    stim[6].del = 500
    stim[6].dur = 1
    stim[6].amp = 0.0

    stim[7] = new IClamp(0.5)
    stim[7].del = 700
    stim[7].dur = 1
    stim[7].amp = 0.0

}


tstop = 1000

objref grafico[7]

grafico[0]=new Graph()
grafico[0].size(0,tstop ,-100,50)
grafico[0].beginline()
grafico[0].addvar("Pre",&neuron[0].soma.v(0.5),1,1)
grafico[0].addvar("Pos",&neuron[1].soma.v(0.5),2,1)
grafico[0].flush()

grafico[2]=new Graph()
grafico[2].size(0,tstop ,0,10)
grafico[2].beginline()
grafico[2].addvar("Gsyn",&neuron[1].sinapse.g,3,1)
grafico[2].flush()

grafico[4]=new Graph()
grafico[4].size(0,tstop ,-30,20)
grafico[4].beginline()
grafico[4].addvar("peso",&neuron[1].sinapse.peso,3,1)
grafico[4].flush()

grafico[5]=new Graph()
grafico[5].size(0,tstop ,-30,20)
grafico[5].beginline()
grafico[5].addvar("O1",&neuron[1].sinapse.O1,1,1)
grafico[5].addvar("O2",&neuron[1].sinapse.O2,2,1)
grafico[5].addvar("R1",&neuron[1].sinapse.R1,3,1)
grafico[5].addvar("R2",&neuron[1].sinapse.R2,4,1)
grafico[5].flush()


graphList[0].append(grafico[0])
graphList[0].append(grafico[1])
graphList[0].append(grafico[2])
graphList[0].append(grafico[3])
graphList[0].append(grafico[4])
graphList[0].append(grafico[5])
graphList[0].append(grafico[6])

run()


Samuel_30
Posts: 10
Joined: Wed Nov 16, 2016 5:13 pm

Re: [Basic] Insert synapses and stimuli into two neurons con

Post by Samuel_30 »

Here is the NMODL file:

Code: Select all

TITLE Stream-Specific Spike Timing Dependent Plasticity

COMMENT
This is a Alpha Function Synapse with synaptic weight controled by a STDP algorithm based on the Triplet Model of Pfisfel and Gerstner (2006).

Implementer: Prof. Fabio M Simoes de Souza, UFABC, Brazil, 2016
ENDCOMMENT

NEURON {
	POINT_PROCESS ExpSynSTDP_triplet
	RANGE tau, e, i, factor, gw
	RANGE R1, R2, O1, O2
	RANGE A2mais, A2menos, A3mais, A3menos
	RANGE Taumais, Taumenos, Tauy, Taux
	RANGE t_last_pre, t_last_post 
	RANGE flag_pre, flag_post 
	RANGE flag1_pre, flag1_post 
	RANGE peso
	NONSPECIFIC_CURRENT i
}

UNITS {
	(nA) = (nanoamp)
	(mV) = (millivolt)
	(uS) = (microsiemens)
}

PARAMETER {
	e = 0	(mV)
	tau= 10 (ms)
	factor = 1
	gw=0.01
	latency=0
	A2mais=4.6e-3 
	A2menos=3e-3
	A3mais=9.1e-3
	A3menos=7.5e-9
	Taumais=16.8
	Taumenos=33.7
	Tauy=47
	Taux=575

	}

ASSIGNED {
	v (mV)
	i (nA)
	tpost (ms)
	t_last_pre (ms)
	t_last_post (ms)
	flag_pre
	flag_post
	flag1_pre
	flag1_post
	peso
}

STATE {
	g (uS)
	R1
	R2
	O1
	O2
}

INITIAL {
	g=0
	peso=0
	tpost = -1e9
	net_send(0, 1)
	O1=0
	O2=0
	R1=0
	R2=0	
	t_last_pre=0
	t_last_post=0
	flag_pre=0
	flag_post=0
	flag1_pre=0
	flag1_post=0
	peso=0

}

BREAKPOINT {
	SOLVE state METHOD cnexp

	if (g<0) {g=0} :hard bound
	
if (flag1_pre==1)  {
	peso= peso -factor*O1*(A2menos+A3menos*R2) }

if (flag1_post==1)  {
	peso= peso + factor*R1*(A2mais+A3mais*O2)

}

	i = g*(v - e)
}

DERIVATIVE state {

:A2mais = 0.005  * (1-g/(2*gw)) :soft bounds
:A2menos = 0.0051 * (g/(2*gw)) :soft bounds

if (flag1_pre==0) {
	R1'= -R1/Taumais/1
	R2'= -R2/Taux/1
} 
if (flag1_pre==1)  {
	R1'= (-R1 + 1)/Taumais/1
	R2'= (-R2 + 1)/Taux/1
	flag1_pre=0 
	printf("t_last_pre=%g \n", t_last_pre)
:	peso= peso + factor*R1*(A2mais+A3mais*O2)
}

if (flag1_post==0) {
	O1'= -O1/Taumenos/1
	O2'= -O2/Tauy/1
}
if(flag1_post==1)  {
	O1'= (-O1 + 1)/Taumenos/1
	O2'= (-O2 + 1)/Tauy/1
	flag1_post=0 
	printf("t_last_post=%g \n", t_last_post)
:	peso= peso -factor*O1*(A2menos+A3menos*R2) 
	}	

:	peso= peso -factor*O1*(A2menos+A3menos*R2) + factor*R1*(A2mais+A3mais*O2)


	g'= -g/tau

:printf("fabio flag_pre=%g \n", flag_pre)



}

NET_RECEIVE(w (uS), A, tpre (ms)) {
	INITIAL { A = 0  tpre = -1e9 }


	if (flag == 0) { : presynaptic spike  (after last post so depress)
:printf("entry flag=%g t=%g w=%g A=%g tpre=%g tpost=%g\n", flag, t, w, A, tpre, tpost)

g = g + w + peso
:g = g + w 

		tpre = t
		A = A :* (1 - d*exp((tpost - t)/dtau))
		flag_pre=1 

:printf("status=%g %g\n",1,t)


	}else if (flag == 2) { : postsynaptic spike
:printf("entry flag=%g t=%g tpost=%g\n", flag, t, tpost)
		tpost = t
:		flag_pre=1 :meu
:		flag_post=1 :meu

:printf("status=%g %g\n",2,t)


		FOR_NETCONS(w1, A1, tp) { : also can hide NET_RECEIVE args
:printf("entry FOR_NETCONS w1=%g A1=%g tp=%g\n", w1, A1, tp)
			A1 = A1 : + p*exp((tp - t)/ptau)
		flag_post=1 :meu
		flag_pre=0 :meu

:printf("status=%g %g\n",3,t)

		}
:meu

if (flag_post ==1) { 
	printf("fabio flag_post=%g \n", flag_post)
	flag1_post=1 
	t_last_post=t
	}

:printf("fabio t_last_pre=%g t_last_post=%g \n", t_last_pre,t_last_post)


	} else { : flag == 1 from INITIAL block
:printf("entry flag=%g t=%g\n", flag, t)
		WATCH (v > -20) 2
	}

if (flag_pre ==1) {
	t_last_pre=t
	flag1_pre=1 
	printf("fabio flag_pre=%g \n", flag_pre)
	}
:meu

}
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: [Basic] Insert synapses and stimuli into two neurons con

Post by ted »

Too much complexity. Why does everybody think they have to start with multiple graphs, multiple cells, multiple synaptic mechanisms and connections? It's best to stick with the KISS principle, even after you're sure you know what you're doing.

Suggest you start by creating a model that has just a single instance of the cell class that Gillies & Steratt developed, attach an IClamp to its soma, and see if you can make it fire a spike. Keep run times short, maybe 50 ms or less, so that graphs will show the spike waveform and maybe even the width of the current pulse itself.

After you have that working, modify the model by turning off the IClamp and attaching a simple synaptic mechanism (ExpSyn with tau = 5 ms would be a good choice) to the cell soma and driving that synapse with events generated by a NetStim. Make the NetStim generate its first event at a reasonable time, like no later than 5 or 10 ms.

When that works, revise the model so one of the STDP synapses is attached to the soma. Set that mechanism's time constants and plasticity parameters to reasonable values, and drive it with events generated by a NetStim. Test it to make sure that it obeys the plasticity rule. You'll want to have a graph that shows the time course of synaptic conductance (unless the synaptic mechanism is actually a time-varying current source, in which case you'll need to examine the current it generates).

At that point, you'll be much better prepared to get on with your real project.
Samuel_30
Posts: 10
Joined: Wed Nov 16, 2016 5:13 pm

Re: [Basic] Insert synapses and stimuli into two neurons con

Post by Samuel_30 »

Hi Ted,

Last time I think I did not explain correctly.

What I really need is to generate stochastic stimuli in this model.

I created a netstim, but it did not work.

Here:

Code: Select all

[code]

NEURON	{ 
  POINT_PROCESS NetStims
  RANGE y
  RANGE interval, number, start
  RANGE noise
}

PARAMETER {
	interval	= 10 (ms) <1e-9,1e9>: time between spikes (msec)
	number	= 10 <0,1e9>	: number of spikes
	start		= 50 (ms)	: start of first spike
	noise		= 0 <0,1>	: amount of randomeaness (0.0 - 1.0)
}

ASSIGNED {
	y
	event (ms)
	on
	end (ms)
}

PROCEDURE seed(x) {
	set_seed(x)
}

INITIAL {
	on = 0
	y = 0
	if (noise < 0) {
		noise = 0
	}
	if (noise > 1) {
		noise = 1
	}
	if (start >= 0 && number > 0) {
	: first spike occurs at start
		event = start
		net_send(event, 3)
	}
}	

PROCEDURE init_sequence(t(ms)) {
	if (number > 0) {
		on = 1
		event = t
		end = t + 1e-6 + invl(interval)*(number-1)
	}
}

FUNCTION invl(mean (ms)) (ms) {
	if (mean <= 0.) {
		mean = .01 (ms) : I would worry if it were 0.
	}
	if (noise == 0) {
		invl = mean
	}else{
		invl = (1. - noise)*mean + noise*mean*exprand(1)
	}
}

PROCEDURE event_time() {
	if (number > 0) {
		event = event + invl(interval)
	}
	if (event > end) {
		on = 0
	}
}

NET_RECEIVE (w) {
	if (flag == 0) { : external event
		if (w > 0 && on == 0) { : turn on spike sequence
			init_sequence(t)
			net_send(0, 1)
		}else if (w < 0 && on == 1) { : turn off spiking
			on = 0
		}
	}
	if (flag == 3) { : from INITIAL
		if (on == 0) {
			init_sequence(t)
			net_send(0, 1)
		}
	}
	if (flag == 1 && on == 1) {
		y = 2
		net_event(t)
		event_time()
		if (on == 1) {
			net_send(event - t, 1)
		}
		net_send(.1, 2)
	}
	if (flag == 2) {
		y = 0
	}
}


[/code]
Samuel_30
Posts: 10
Joined: Wed Nov 16, 2016 5:13 pm

Re: [Basic] Insert synapses and stimuli into two neurons con

Post by Samuel_30 »

And I really need to develop from that template.


Best regards, Happy new year!

Samuel
Post Reply