best way to Vector.record()?

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
davidlmorton
Posts: 10
Joined: Fri Apr 11, 2008 11:07 pm

best way to Vector.record()?

Post by davidlmorton » Mon Apr 21, 2008 9:34 pm

I'm looking to run rather simple simulations in NEURON + Python. I have the python code that generates a geometric description of the cell and I just need to be able to translate that into a NEURON model cell (easy enough in hoc). I can get the model translated (I think) but recording a variable for post-processing is not working for me. For example:

Code: Select all

[8:25pm]@Sakurai:~/dendritetics/code>nrniv -python
NEURON -- VERSION 6.2.1002 (2037) 2008-03-26
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2007
See http://www.neuron.yale.edu/credits.html

>>> from neuron import *
>>> from nrn import *
>>> soma = Section()
>>> v = Vector()
>>> soma
<nrn.Section object at 0x258c040>
>>> v
0       0       0       0       0
0       0       0       0       0


>>> v.record( soma(0.5).v )
bad stack access: expecting (double *); really (double)
nrniv: interpreter stack type error
 near line 7
 v.record( soma(0.5).v )
^
        Vector[0].record(-65)
oc_restore_code tobj_count=1 should be 0
Traceback (most recent call last):
  File "stdin", line 1, in ?
RuntimeError: hoc error
>>> 
I'd like to know what you think is the best way to gain access to a recorded variable in python. Since the rest of my project is in python, I'd hate to have to export geometries to files and import them in a .hoc file (just scripting neuron). I hope I've made sense. If recording a simulation variable into a python list or numpy array is going to be hard, am I better off just scripting neuron?

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

Post by ted » Tue Apr 22, 2008 9:44 am

If you know the hoc names of the sections you have created, then from Python you can
execute hoc statements that create a Vector and make it record the variable of interest.
Example:

Code: Select all

hoc.execute('objref vv')
hoc.execute('vv = new Vector()')
hoc.execute('vv.record(&secname.v(0.5))')
where you replace secname with the name of one of the sections you created.
vv will be available to Python as h.vv

davidlmorton
Posts: 10
Joined: Fri Apr 11, 2008 11:07 pm

Post by davidlmorton » Tue Apr 22, 2008 11:36 pm

How can I find out the hoc name of a section that I create with the above code?

hines
Site Admin
Posts: 1580
Joined: Wed May 18, 2005 3:32 pm

Post by hines » Wed Apr 23, 2008 10:24 am

To use v.record you need to do two things. 1. set the currently accessed section to
some section in the cell where the variable to be recorded makes sense.
(that information is required for the local variabl e time step method)
2) pass a pointer to the hoc range variable
In your case (I didn't execute it so there may be typos, but the idea is sound)

soma.push()
v.record(h.ref(soma(.5).v))
h.pop_section()

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

Post by ted » Wed Apr 23, 2008 4:21 pm

Code: Select all

>>> soma = Section()
>>> soma.push()
<nrn.Section object at 0xb7f1e040>
>>> v = Vector()
>>> v.record(h.ref(soma(0.5).v))
1.0
>>> h.pop_section()
1.0
>>> 
So sections can be created, and Vector record() can be asserted, without one ever having
to descend to the level of using hoc.execute() to execute a hoc statement. Nice!

davidlmorton
Posts: 10
Joined: Fri Apr 11, 2008 11:07 pm

Post by davidlmorton » Wed Apr 23, 2008 7:21 pm

The code doesn't have any syntax errors, but the behavior is not correct. Here is my code:

Code: Select all

class cell:
    def __init__(self):
        import neuron as ne
        import nrn as n
        self.ne = ne
        self.n = n


    def make_cell(self):
        soma = self.soma = self.n.Section()
        axon = self.axon = self.n.Section()
        dend = self.dend = self.n.Section()

        soma.L = 30
        soma.nseg = 5

        axon.L = 1000
        axon.nseg = 30

        dend.L = 100
        dend.nseg = 10

        axon.connect(soma,0.5,1)
        dend.connect(soma)

        soma.insert('pas')
        axon.insert('hh')
        dend.insert('pas')

        self.vect = self.ne.Vector()
        soma.push()
        self.vect.record(self.ne.h.ref(soma(0.5).v))
        self.ne.h.pop_section()

    def run(self,tstop,v_init=-40):
        print "Before sim " + str(self.soma(0.5).v)
        self.ne.h.finitialize(v_init)
        self.ne.h.fcurrent()
        while self.ne.h.t < tstop:
            self.ne.h.fadvance()
        print "After sim " + str(self.soma(0.5).v)


c = cell()
c.make_cell()
c.run(1)

And here is the result of running it:

Code: Select all

NEURON -- VERSION 6.2.1002 (2037) 2008-03-26
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2007
See http://www.neuron.yale.edu/credits.html

Added /Users/davidmorton/dendritetics/code to python's path
>>> from mycell import *
Before sim -65.0
After sim -75.4368941679
>>> c.vect
-65     -65     -65     -65     -65
-65     -65     -65     -65     -65
-65     -65     -65     -65     -65
-65     -65     -65     -65     -65
-65     -65     -65     -65     -65
-65     -65     -65     -65     -65
-65     -65     -65     -65     -65
-65     -65     -65     -65     -65
-65     -65

>>> c.ne.h.dt
0.025000000000000001
>>> c.soma(0.5).v
-75.43689416792887

As you can see, the vector is just recording the initialization value instead of the value throughout the simulation. Perhaps something is wrong with my run() procedure?

Post Reply