Problem using xpvalue

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

Moderator: hines

Post Reply
aluchko

Problem using xpvalue

Post by aluchko » Wed Dec 15, 2010 6:47 pm

I'm having trouble calling xpvalue from python using a reference to a value in hoc.

init.hoc:

Code: Select all

load_file("simplepanel.hoc")

objref sp
sp = new SimplePanel()
sp.showopt()
simplepanel.hoc:

Code: Select all

begintemplate SimplePanel

public showopt
objref optbox, p, this

proc init() {
     foo = 1
     bar = 2
}


strdef tstr
proc showopt() {
	optbox = new VBox()
	xpanel("Simple Panel")
	xpvalue("Foo", &foo)
	p = new PythonObject()	
	nrnpython("import content")
	p.content.showopt(this)
	xpanel()
	optbox.map()
}

endtemplate SimplePanel
content.py:

Code: Select all

import neuron

def showopt(hObj):
    h = neuron.hoc.HocObject()
    h.xbutton("Works", "nrnpython(\"print \\\"works\\\"\")")
    print hObj.bar
    print hObj._ref_bar
    h.xpvalue("Bar", hObj._ref_bar)
Instead of creating the xpvalue I see the following error

Code: Select all

nrngui init.hoc 
NEURON -- VERSION 7.2 (469+:e613810284ee+) 2010-09-15
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2008
See http://www.neuron.yale.edu/credits.html

nrniv: unable to open font "*helvetica-medium-r-normal*--14*", using "fixed"
        1 
2.0
<hoc.HocObject object at 0xe60468>
/usr/bin/nrniv: Segmentation violation
 in init.hoc near line 5
 sp.showopt()
             ^
        xpvalue("Bar", ...)
      SimplePanel[0].showopt()
Traceback (most recent call last):
  File "content.py", line 8, in showopt
    h.xpvalue("Bar", hObj._ref_bar)
RuntimeError: hoc error
/usr/bin/nrniv: PyObject method call failed: showopt
 in init.hoc near line 5
 ^
        SimplePanel[0].showopt()
initcode failed with 1 left
Am I making the call improperly or is something else happening? (if you comment out the xpvalue than the rest works fine)

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

Re: Problem using xpvalue

Post by hines » Thu Dec 16, 2010 11:09 am

That can be worked around by using the Pythonic form of xvalue
h.xvalue("Bar", (hObj, "bar"))
I am embarrassed that the extendef pythonic arguments for the various
gui element functions have not made it into the help files, I am enclosing
a test program that illustrates the usage at the end of this message.

Thanks. That bug is now fixed (mostly) by
http://www.neuron.yale.edu/hg/neuron/nr ... caac0e4d44
I say 'mostly' because a call to xpvalue from python will not be
able to automatically add the units string (if any) to the button label.
The underlying problem is that the ability to determine the
Symbol associated with a pointer at the time the pointer is used
by the internal function, hoc_get_last_pointer_symbol, is a property of some details of the hoc interpreter
which are bypassed by python. Two other hoc functions that make use
of this internal function, CVode.atolscale and units()
http://www.neuron.yale.edu/neuron/stati ... #atolscale
http://www.neuron.yale.edu/neuron/stati ... html#units
were extended long ago to allow a string to specify the name of the variable. The one other
function that uses this internal function
http://www.neuron.yale.edu/neuron/stati ... ble_domain
will need to be extended in the future.

As mentioned above, here is a python file that illustrates the python argument style for the gui elements.
cat gui1.py

Code: Select all

from neuron import h
class Demo(object):
  def __init__(self):
    self.x = 2
    self.cbstate = False
    self.sbut = True
    self.ss = 'A variable label'
    self.build()
    self.map()

  def build(self):
    self.box = h.HBox()
    self.box.intercept(1)
    self.box.ref(self)
    h.xpanel("")
    h.xbutton("Button 1", (self.bact, 1))
    h.xbutton("Button 2", (self.bact, (2,)))
    h.xbutton("Button 3", self.bact_noarg)
    h.xbutton("Button 4", (self.bact_2arg, ("hello", 4)))
    for i in range(3):
      h.xradiobutton("Radio "+str(i), (self.bact, i))
    h.xmenu("Menu")
    for i in range(3):
      h.xbutton("Item "+str(i), (self.bact, i))
    for i in range(3):
      h.xradiobutton("Radio "+str(i), (self.bact, i))
    h.xcheckbox('checkbox', (self, 'cbstate'), self.cb)
    h.xstatebutton('state', (self, 'sbut'), self.st)
    h.xmenu()
    h.xpanel()
    self.g = h.Graph()
    self.g.menu_tool('graph menu tool 1', self.gcb, (self.gsel, 1))
    h.xpanel("")
    h.xvalue("x", (self, "x"), 1, self.chgx)
    h.xcheckbox('checkbox', (self, 'cbstate'), self.cb)
    h.xstatebutton('state', (self, 'sbut'), self.st)
    h.xlabel('fixed label')
    h.xvarlabel((self, 'ss'))
    h.xslider((self, 'x'), 0, 20, self.slide)
    self.g.menu_tool('graph menu tool 2', self.gcb, (self.gsel, 2))
    h.xpanel()
    self.box.intercept(0)

  def map(self):
    self.box.map("Demo")

  def bact(self, i):
    print "bact", i

  def bact_noarg(self):
    print "bact_noarg"

  def bact_2arg(self, a1, a2):
    print "bact_2arg", a1, a2

  def chgx(self):
    print "x =", self.x
    self.ss = 'x is ' + str(self.x)

  def cb(self):
    print "cbstate=", self.cbstate

  def st(self):
    print 'sbut=', self.sbut

  def slide(self):
    print 'slide x=', self.x

  def gcb(self, mode, x, y, key):
    print mode, x, y, key

  def gsel(self, i):
    print 'menu tool', i, 'selected'

Demo()

aluchko

Re: Problem using xpvalue

Post by aluchko » Thu Dec 16, 2010 8:45 pm

Thanks, that did the trick!

Post Reply