Problem using xpvalue

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

Moderator: hines

Post Reply

Problem using xpvalue

Post by aluchko »

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


Code: Select all


objref sp
sp = new SimplePanel()

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")

endtemplate SimplePanel

Code: Select all

import neuron

def showopt(hObj):
    h = neuron.hoc.HocObject()
    h.xbutton("Works", "nrnpython(\"print \\\"works\\\"\")")
    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

nrniv: unable to open font "*helvetica-medium-r-normal*--14*", using "fixed"
<hoc.HocObject object at 0xe60468>
/usr/bin/nrniv: Segmentation violation
 in init.hoc near line 5
        xpvalue("Bar", ...)
Traceback (most recent call last):
  File "", 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
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)
Site Admin
Posts: 1603
Joined: Wed May 18, 2005 3:32 pm

Re: Problem using xpvalue

Post by hines »

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 ... 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() ... #atolscale ... 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 ... 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.

Code: Select all

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

  def build(self): = h.HBox()
    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))
    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.g = h.Graph()
    self.g.menu_tool('graph menu tool 1', self.gcb, (self.gsel, 1))
    h.xvalue("x", (self, "x"), 1, self.chgx)
    h.xcheckbox('checkbox', (self, 'cbstate'), self.cb)
    h.xstatebutton('state', (self, 'sbut'),
    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))

  def map(self):"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 = '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'


Re: Problem using xpvalue

Post by aluchko »

Thanks, that did the trick!
Post Reply