loop structure

Anything that doesn't fit elsewhere.
Post Reply
firoozeh
Posts: 7
Joined: Mon Mar 09, 2020 8:40 am

loop structure

Post by firoozeh » Sat May 23, 2020 2:57 am

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

ramcdougal
Posts: 205
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: loop structure

Post by ramcdougal » Sat May 23, 2020 12:22 pm

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.

firoozeh
Posts: 7
Joined: Mon Mar 09, 2020 8:40 am

Re: loop structure

Post by firoozeh » Tue May 26, 2020 2:52 pm

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()
}

Post Reply