Calling python function from hoc statement

Moderator: wwlytton

Post Reply
Krishna Chaitanya
Posts: 70
Joined: Wed Jan 18, 2012 12:25 am
Location: University of Pavia

Calling python function from hoc statement

Post by Krishna Chaitanya »

Hi,

I am trying to call python function in a class using hoc statements to redefine init block. It was pretty easy using hoc programming, but to do the same, in python, I used this approach.

I have a MPI based network and in the prun block, I define

Code: Select all

 
 def prun(self):
                if self.rank==0:
                        print 'In the function SIMULATING CELLS...\n'
                self.pc.set_maxstep(10)
                #h('proc init() {nrnpython("prepruninitialize()")}') - this didn't work either
                h('''objref po
                po=new PythonObject()
                proc init(){
                        finitialize(-65)
                        po.prepruninitialize()
                        finitialize()
                        if (cvode_active()){
                                cvode.re_init()
                        }else{
                                fcurrent()
                        }
                        frecord_init()
                }
                ''')
                h.stdinit()
 def prepruninitialize(self):
                if self.rank==0:
                        print 'Setting up initial conditions...\n'
                for ii in range(len(self.grcells)):
                        self.grcells[ii].setv()    # setting up initial voltage.
Whenever I execute this program, it says that "prepruninitialize is not defined". I was able to execute this successfully when I run as individual blocks on nrniv shell but not with MPI based network code.

What am I doing wrong here? Can you kindly let me know?

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

Re: Calling python function from hoc statement

Post by ted »

I think I'd try a type 1 FInitializeHandler for this. And instead of using h(''' hoc strings ''') I'd call everything from Python using h, e.g.
h.finitialize()
prepruninitialize()
. . . test h.cvode_active() and take appropriate steps from Python . . .

Then your custom prun would omit the h(''' . . . ''')

Check out the example https://www.neuron.yale.edu/neuron/stat ... izeHandler
Post Reply