loop structure

Anything that doesn't fit elsewhere.
Post Reply
firoozeh
Posts: 6
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.

Post Reply