Passing argument to function

The basics of how to develop, test, and use models.
Post Reply

Passing argument to function

Post by ps0322 »

I'm writing my first code with functions and I stuck. Please give me some advice.

1) general question. Now I'm using jEdit for my coding. Then when I want to test my code. I open the nrngui and using 'load hoc' to test my code.
However, whenever I made some change to my code, i have to quit() the nrngui, save my code(the editor do not allow me to save, I guess it is because the nrngui is using it) and open nrngui again then 'load hoc'.
I wonder is there better way to do that?(some command to 'unload' the hoc file)

Now the coding question. I want to measure the input-out relation of the cell. So I varied injected current and observe the firing rate.
Here is my code. Note that the 'singlecell.hoc' just contain the code for soma

Code: Select all

////// Current Injection and Spike recording /////////////

objref listAmp, listspk, f_means
listspk = new List()
f_means = new Vector()
listAmp = new Vector()
proc InjectRecord(){local stp,step,str,totalamp,j,i,idx
//Input $1 = start current amplitude
//	    $2 = step
//	    $3 = end current amplitude

	str = $1
	step = $2
	stp = $3 
//	str = 0.0 //$1
//	step = 0.01 //$2
//	stp = 0.1 //$3
	totalamp = (stp-str)/step+1
	for(i=str; i<=stp; i=i+step){
		  listAmp.x(j) = i
		  j = j+1

	  for idx = 0,idx <=listAmp.size-1{
		  tmpspk =recordSpk()
		  listspk.append(tmpspk) //save spike train
		  f_means.x(idx) = f_mean(n_spk(tmpspk))


objref stim
soma stim = new IClamp(0.5)

proc injectI(){  //localobj  stim 
//Input $1 is current amplitude
	//////// Iclamp ///////

	//soma stim = new IClamp(0.5)
	stim.amp = $1 //0.1 //amplitude
	stim.del = 0 //delay
	stim.dur = 500 //duration

obfunc recordSpk(){ local thre localobj vvec, tvec, spktrain
	//////// record spike /////////

	tvec = new Vector()
	tvec.record(&t) // record time
	vvec = new Vector()
	soma vvec.record(&v(0.5)) // record v at 0.5 (center) of soma
	spktrain = new Vector()
	thre = -5 // the threshold for spikebin commonly used values are in the range -20 to +10 mV
	tstop = 500
	spktrain.spikebin(vvec, thre) // Have to recall again every run() time
	return spktrain
func n_spk(){ // $o1 is spktrain
	return $o1.sum() // n spike

func f_mean() { // Input $1 = number of spike
	return $1/tstop*1000 //spike per second

When I call my function, the following error is shown.
bad stack access: expecting (double); really (Unknown)
nrniv: interpreter stack type error
near line 4
InjectRecord(00.010.1 , , )
initcode failed with 1 left
I wonder what went wrong in my code.
It seems that the NEURON did not recognize the arguments as double. How could I do it?

Also, Is that the right way to declare variables as global variable?

Code: Select all

objref listAmp, listspk, f_means
listspk = new List()
f_means = new Vector()
listAmp = new Vector()
proc InjectRecord(){local stp,step,str,totalamp,j,i,idx
Site Admin
Posts: 5819
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine

Re: Passing argument to function

Post by ted »

whenever I made some change to my code, i have to quit() the nrngui, save my code(the editor do not allow me to save
That's typical of MS Windows. Doesn't happen under OS X or Linux or Unix. If you're stuck with Windows, then you're stuck with having to exit nrngui and then restarting NEURON.
open nrngui again then 'load hoc'
Why bother with that? Just make sure the first line in your hoc file is
Then all you have to do is double click on the hoc file, and Windows will automatically start NEURON and NEURON will automatically load the hoc file. OS X and Linux users can just start NEURON by typing
nrngui filename.hoc
where "filename.hoc" means the name of the hoc file that they want NEURON to load when it starts.
Here is my code.
Have you read
Suggestions for how to develop models

Here are some suggestions and questions that may point you in the right direction.

Code: Select all

	totalamp = (stp-str)/step+1
This gambles that floating point arithmetic will produce results that have no roundoff error. There will be combinations of stp, str, and step that cause failure. You're better off deciding in advance how many steps to use and how big they should be.

numsteps = whatever // replace right hand sides with values that you want
stepsize = somevalue


objref amplitude
amplitude = new Vector(numsteps)

and you can use numsteps to control the for loop that launches the series of simulations.

The Vector class's spikebin method is great for binning spikes, but only a very crude tool for exploring a neuron's input-output relationship. Why not use the NetCon class's record() method to capture spike times to a Vector? Uses much less code, and gives you the exact time at which each spike occurs. From that you can calculate the interspike intervals, instantaneous or windowed (averaged over a window) firing frequency etc..

By the way, it is only necessary to execute NetCon.record once, during model setup (same is true for Vector.record). The Vector is automatically reset at the start of each run.

Do you expect your model cell(s) to fire tonically at a constant rate? How do you plan to deal with the fact that many kinds of cells (including, quite likely, the model cells you will be studing) show more complex firing patterns, including:
--tonic firing over only a limited range of stimulus intensities; weaker or stronger stimuli produce a limited number of spikes
--variable rate of firing, either speeding up or slowing down, during sustained current injection
--bursting or even chaotic spiking
For an F/I curve (spike frequency vs. stimulus current amplitude) to be meaningful, the frequency should be determined according to strict criteria, for example by observing interspike intervals
--over a particular time interval
--at a time when the firing rate has stabilized
--when the firing rate is maximum

Don't you want each simulation to begin with a "baseline" interval during which the cell is seen to be at a stable resting potential (or, if the cell is spontaneously active, showing a stable pattern of spiking activity) before the current is injected? Any experimentalist would expect to see that kind of experimental data, and any reviewer should require that to be part of your simulation protocol.

What about "how to pass arguments to a function?" If you run into problems with that, build a toy program that has a toy function, and run tests on that to discover the cause of the problem and how to fix it.

Re: Passing argument to function

Post by ps0322 »

Hi Ted,
This is my late Thank You message, THANK YOU so much for the advice.
My crude code was use as my toy program to learn neuron. Now, I'm familiar with Neuron a lot better than before.
Currently, I'm implementing a more serious networks which surely consider properties of neurons as you mention.
Thanks once again.
Post Reply