Page 1 of 1

loop structure

Posted: Sat May 23, 2020 2:57 am
by firoozeh
Hello all,
I give my neuron a noise input to soma and an extracellular constant current and repeat this process for 100 trials. I'm trying to do this with "loop" structure and save Vm and Ve and current data for each trial. Would you please help me how can I do this?
Thanks

Re: loop structure

Posted: Sat May 23, 2020 12:22 pm
by ramcdougal
Python, HOC, and most programming languages use a for loop to run the same code a fixed number of times. e.g. in Python, we'd write

Code: Select all

for trial in range(100):
    # do stuff here
and in HOC, we might write:

Code: Select all

for (trial = 0; trial < 100; trial += 1) {
    // do stuff here
}
NEURON provides the h.Random (or just Random in HOC) class for generating pseudo-random numbers.

In Python, it's probably easiest to just let pandas write a CSV file with to_csv. In HOC, you'd have to do it yourself using the File class.

Putting it all together (assuming you're working in Python), you have something like

Code: Select all

from neuron import h
from neuron.units import mV, ms
import pandas as pd
# TODO: any other imports you need

# allow reproducible pseudo-randomness
random_stream_id3 = 1
rng = h.Random()

h.load_file('stdrun.hoc')

# TODO: setup model here

# setup recording
t = h.Vector().record(h._ref_t)
vm = # TODO: CONTINUE HERE
ve = # TODO: CONTINUE HERE

for trial in range(100):
    # select random stream
    rng.Random(trial, 0, random_stream_id3)
    # TODO: set your random parameters here
    # see https://www.neuron.yale.edu/neuron/static/py_doc/programming/math/random.html
    # e.g.
    init_v = rng.normal(-65 * mV, 3 * mV)  # TODO: you'll want to change this line to your needs

    h.finitialize(init_v)    # or whatever
    h.continuerun(100 * ms)  # or whatever

    # save the data
    pd.DataFrame({'t': t,
                  'vm': vm,
                  've': ve
    }).to_csv(f'trial{trial}.csv')
where obviously you need to fill in all the TODO stuff.

Re: loop structure

Posted: Tue May 26, 2020 2:52 pm
by firoozeh
Dear ramcdougal,
Thank you so much for your reply.
I used the following code. I can save all the data for every run, but the problem is all the data save in a single file. I want to save data of each trial in a separate file. Would you please help me to do this?
Thanks

Code: Select all

objref ivec, tvec, vvec
ivec = new Vector()
soma ivec.record(&in.i)

tvec= new Vector()
tvec.record(&t) // record the time 

vvec = new Vector()
soma vvec.record(&v(0.5)) // record v at soma

objref fil
fil=new File()
fil.wopen("fil")
proc datafile() { local i
  for i=0, tvec.size-1 {
      fil.printf("%g\t %g\t %g\n",tvec.x[i], ivec.x[i], vvec.x[i])
  }
}
for (trial = 0; trial < 10; trial += 1) {
datafile()
run()
}

Re: loop structure

Posted: Mon Jun 01, 2020 1:35 pm
by ramcdougal
You are creating a single file because your file creation is happening once, outside of the for loop.

If you look at the Python example, you'll see I'm creating a new file every time with a different filename based on the trial number (trial0.csv, trial1.csv, trial2.csv, ...) You can generate filenames dynamically in HOC using sprint (think sprintf in C).

If you use Python instead of HOC, you'll have an easier time finding coding examples.