Tip of the day

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

Moderator: hines

Post Reply
apdavison
Posts: 14
Joined: Tue May 24, 2005 3:56 am
Location: CNRS, Gif sur Yvette, France
Contact:

Tip of the day

Post by apdavison »

This will become obsolete as the functionality of the python interface increases, but I have created a class HocToPy, with a static method get(), to make getting variables from hoc into python easier:

Code: Select all

import hoc

class HocToPy:
    """Static class to simplify getting variables from hoc."""
    hocvar = None
    
    def get(name,return_type):
        """Return a variable from hoc.
           name can be a hoc variable (int, float, string) or a function/method
           that returns such a variable.
        """
        fmt_dict = {'int' : '%d', 'float' : '%g', 'string' : '\\"%s\\"'}
        hoc_commands = ['strdef cmd',
                        'sprint(cmd,"HocToPy.hocvar = %s",%s)' % (fmt_dict[return_type],name),
                        'nrnpython(cmd)']
        for cmd in hoc_commands:
            hoc.execute(cmd)
        return HocToPy.hocvar
    get = staticmethod(get)
    
hoc.execute("a = 2")
hoc.execute("pi = 3.14159")
hoc.execute("strdef hello")
hoc.execute('hello = "Hello World"')

a     = HocToPy.get('a','int')
pi    = HocToPy.get('pi','float')
hello = HocToPy.get('hello','string')

print "a = ", a
print "pi = ", pi
print hello
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Post by eacheon »

where can I get the "hoc" module, excuse me?
apdavison
Posts: 14
Joined: Tue May 24, 2005 3:56 am
Location: CNRS, Gif sur Yvette, France
Contact:

hoc python module

Post by apdavison »

To get the hoc module, you have to run

Code: Select all

nrniv -python
This starts NEURON with the python interpreter (assuming NEURON has been compiled with the --with-nrnpython configure option).

You should then be able to

Code: Select all

>>> import hoc
>>> import nrn
At the moment, the hoc module only provides one function: execute()
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Post by eacheon »

Ah, seems I have to get NEURON with cvs version. however, is cvs anonymous checkout avaiable now? I got the following error:

Code: Select all

$ cvs -d :ext:anonymous@www.neuron.yale.edu:/home/cvsroot checkout iv
Host key verification failed.
cvs [checkout aborted]: end of file from server (consult above messages if any)

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

Post by ted »

The server (and host key) may have been changed since the last time that you got your
local copy of the repository. You might want to archive the copy you presently have,
then try setting up a new one.
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Post by eacheon »

ted wrote:The server (and host key) may have been changed since the last time that you got your
local copy of the repository. You might want to archive the copy you presently have,
then try setting up a new one.

I tried setting up a new one, but I got to be asked for password. I surely exported CVS_RSH, wierd. The error message is:

Code: Select all

anonymous@www.neuron.yale.edu's password:
anonymous@www.neuron.yale.edu's password:
anonymous@www.neuron.yale.edu's password:
Permission denied (publickey,gssapi-with-mic,password).
cvs [checkout aborted]: end of file from server (...)

apdavison
Posts: 14
Joined: Tue May 24, 2005 3:56 am
Location: CNRS, Gif sur Yvette, France
Contact:

nrnpython is in 5.8

Post by apdavison »

You don't need the CVS version for the example I posted above.
nrnpython was certainly in version 5.8, and I imagine it is in 5.9.
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Post by hines »

Andrew is correct, all the python code fro NEURON is in the tar.gz file.
But to answer your question about CVS. Anonymous CVS broke when the old server
broke a month ago and I have not bothered to set it up on the temporary server since
I have switched to Subversion. I just have been remiss in updating the web pages to
explain how to checkout the sources. However, see the "View the Subversion Repository" link at http://www.neuron.yale.edu/neuron/install/install.html
The TRAC replacement for ViewCVS is much, much better.
To checkout the InterViews and NEURON main trunks you can temporarily use:
svn checkout http://miris.med.yale.edu/svn/neuron/iv/trunk iv
svn checkout http://miris.med.yale.edu/svn/neuron/nrn/trunk nrn
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Post by eacheon »

If I can get the Vectors into python efficiently, I'll switch to python. Do you have a good way to manage to getting hoc's Vectors into python?
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Post by hines »

Efficient? Probably not. But you can make it efficient if the idiom I mention below is not efficient enough.

First, all variables, objects, functions, etc in HOC --- built-in or user defined---- are available with almost identical syntax in Python.
e.g. grab the following and paste into the terminal after launching "nrniv -python"

Code: Select all

 import hoc
 h = hoc.HocObject() #access to everything in hoc
 #execute any hoc statement, Following creates a factory for hoc Vectors
 h('obfunc newvec() { return new Vector($1) }')
 #python and hoc object syntax is compatible
 vec = h.newvec(10)
 #now vec is a python object that knows everything about the new
 #hoc vector of size 10
 #can call any hoc method on the Vector
 vec.indgen().add(10) # set elements to 10, 11, 12, ... 19
 #and evaluate/assign any field
 print vec.x[5] #will print the number 15
the idiom i mentioned is

Code: Select all

px = vec.x
which thereafter can be used like px[index]. So now the problem becomes whether it is efficient enough for you to copy a hoc vector to a python vector from within python or whether you want to write some c code with a peak at nrn/src/nrnpython/nrnpy_hoc.cpp to get the double* pointer from

Code: Select all

// leave pointer on stack ready for get/set final
static void eval_component(PyHocObject* po, int ix) {
        int j;
        hoc_push_object(po->ho_);
        hocobj_pushtop(po, 0, ix);
        component(po);
}
The reason I did not do this myself is because I am confused about Python double vectors, how they are represented, and how they relate to the popular numpy extension. The latter I am trying to avoid as a requirement during linking.
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Post by eacheon »

Nice job, Micheal! You did it so fast!

May I ask why you are reluctant to numpy dependency? My guess is every one is going to use numpy with NEURON so there should not be a dependency issue. At the same time I am not aware python has a way (with standard library) to represent a double vector. Numpy is a must when dealing with arrays.

I am rusty in C/C++ and do not quite understand the working of arrays in python, but people seem to be working on an nd-array interface which is destined to inclusion in Python core. In my understanding, for an object hocvector with this interface implemented we can construct a numpy array by

Code: Select all

pyarray = array(hocvector)
and gain all the strength of numpy. Is it what you are looking for?
http://numpy.scipy.org/array_interface.shtml


Again, very nice job. I am going to set out processing NEURON data directly in Python. Thanks a lot!
eacheon
Posts: 97
Joined: Wed Jan 18, 2006 2:20 pm

Post by eacheon »

eacheon wrote:Nice job, Micheal! You did it so fast!

May I ask why you are reluctant to numpy dependency? My guess is every one is going to use numpy with NEURON so there should not be a dependency issue.
I am sorry, Micheal. I realized you meant the "build dependency"...
Post Reply