hoc.NULLObject != Python.None

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

Moderator: hines

Post Reply
sec6
Posts: 39
Joined: Sat Mar 15, 2008 10:32 am

hoc.NULLObject != Python.None

Post by sec6 » Thu Aug 07, 2008 7:16 pm

Sometimes, in NEURON one needs a NULLObject, for example, when creating a NetCon with a source, but no target, or vice versa. (In the following toy example, I create a NetCon with no source *and* no target, which is legal, 'though useless.)

Code: Select all

oc>objref nil,nc
oc>nc = new NetCon(nil,nil)
oc>nc.event(5)
	0 
oc>
The normal way to crate a NULLObject (according to the NetCon documentation in the Programmer's Reference) is to declare an object reference, and not bind it to anything, as in the example above.

The problem is, how do I create an unbound objref in Python.
The following does not work:

Code: Select all

>>> import nrn
>>> import neuron
>>> h = neuron.h
>>> h("objref nil")
1
>>> nc = h.NetCon(h.nil,h.nil)
/Applications/NEURON-6.2/nrn/i686/bin/nrniv: if arg 1 is an object it must be a point process or NULLObject
<snip>
RuntimeError: hoc error
>>> print h.nil
None
>>> h.NetCon(None,None)
/Applications/NEURON-6.2/nrn/i686/bin/nrniv: if arg 1 is an object it must be a point process or NULLObject
<snip>
RuntimeError: hoc error
>>>
A hoc unbound objref is translated to Python None object, but apparently translation does not occur in the other direction (i.e. Python None object is not converted to hoc unbound objref)

Here's a workaround:

Code: Select all

>>> import nrn
>>> import neuron
>>> h = neuron.h
>>> h("objref nil,nc")
1
>>> h("nc = new NetCon(nil,nil)")
1
>>> nc = h.nc
>>> nc.event(5)
0.0
>>>
Since there's a workaround, this isn't a top priority, but ideally, hoc.NULLObject <=> Python.None conversion would be bidirectional. A cheaper fix would be to create a neuron.h.NULLObject object. (or singleton class) Then one would write:

Code: Select all

>>> nc = h.NetCon(h.NULLObject,h.NULLObject)
Not supremely elegant, but still an improvement.

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

Re: hoc.NULLObject != Python.None

Post by hines » Fri Aug 08, 2008 11:53 am

Automatic conversion between None and NULLobject was intended
and occurs in most contexts
but there was a code path that failed to check that case.
I committed a fix to the subversion repository
http://www.neuron.yale.edu/cgi-bin/trac ... geset/2191
so that

Code: Select all

>>> from neuron import h
>>> h.printf('%s\n', None)
NULLobject
11.0
>>> h('objref a')
1
>>> h.a == None
True
>>> print h.a
None
>>> 

Post Reply