SubClassing a section

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

Moderator: hines

Post Reply
mattions
Posts: 65
Joined: Tue Jul 15, 2008 11:21 am
Location: EMBL-EBI Cambridge UK

SubClassing a section

Post by mattions » Mon Jan 26, 2009 1:46 am

I'm trying to subclass a section, but something is not working.
Am I doing correctly?

Code: Select all

from neuron import hclass, h
class MySection(hclass(h.Section)):
      def printTest(self):
          "Hello, I'm section %s" %self.name()

a = MySection()
this is the error:

Code: Select all

/usr/lib/python2.5/site-packages/neuron/__init__.pyc in __new__(cls, *args, **kwds)
    137         def __new__(cls, *args, **kwds):
    138             kwds.update({'hocbase':cls.htype})
--> 139             return hoc.HocObject.__new__(cls, *args, **kwds)
    140     setattr(hc, 'htype', c)
    141     return hc

TypeError: HOC base class not valid

marekrud

Re: SubClassing a section

Post by marekrud » Mon Jan 26, 2009 7:11 am

Hello,

I do it this way:

Code: Select all

from neuron import hclass, h
import nrn

class MySection(nrn.Section):
    def printTest(self):
        print "Hello, I'm section %s" %self.name()
        

a = MySection()

a.printTest()
but would like to know if it is the right way.

Cheers

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

Re: SubClassing a section

Post by hines » Mon Jan 26, 2009 3:12 pm

It is an oversight that I did not allow
for the possibility of subclassing Section using the idiom

Code: Select all

class MySection(hclass(h.Section)):
. The expectation of mattions is reasonable but
Section is not a HocObject so does not presently fall into the working domain.

I'm kind of surprised that the standard syntax used by marekrud works since I have not done
any specific programming to make the Section class subclassable. So that seems the best way
to do it. Let me know if there are any cases where it is not working correctly. I think that

Code: Select all

from neuron import hclass, h, nrn 
gives the least namespace polution if one is going
to subclass Section as well as other Hoc templates.

mattions
Posts: 65
Joined: Tue Jul 15, 2008 11:21 am
Location: EMBL-EBI Cambridge UK

Re: SubClassing a section

Post by mattions » Tue Jan 27, 2009 6:52 am

One thing more on this
I discovered that when you initiate the class in python you should use the init of the parent class (I've got a segmental fault)

Actually that is the right way to do it in python...
Test this code:

Code: Select all

from neuron import hclass, h
import nrn

class MySection(nrn.Section):
    def __init__(self):
        self.L = 1000
        print self.name(), self.L
       

a = MySection()
segmentation Fault

the blessed one should be:

Code: Select all

from neuron import hclass, h
import nrn

class MySection(nrn.Section):
    def __init__(self):
        nrn.Section.__init__(self) # Using the init of the parent
        self.L = 1000
        print self.name(), self.L

a = MySection()
I can't link to the specs in python, but this is what is recommended by Dive into Python:
http://diveintopython.org/object_orient ... asses.html
(Ex: 5.6)

I do not have to do the same with the NetStim example. Dunno why.

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

Re: SubClassing a section

Post by hines » Thu Jan 29, 2009 9:21 am

For uniformity, hclass has been extended to allow h.Section . However, as mattions noted above, if you override the
__init__, you must call nrn.Section.__init__(self) so the Python Section actually constructs the NEURON Section
instead of leaving its field as NULL. One cannot use h.Section.__init__ because h.Section() is actually a HocObject
method that returns a new instance of nrn.Section. So, for example

Code: Select all

from neuron import hclass, h, nrn

class A(hclass(h.Section)) :
  def __init__(self) :
    nrn.Section.__init__(self)
    print self.name()
  def printTest(self) :
    print "Hello, I'm section %s" %self.name()

a = A()
a.nseg = 4
a.insert('hh')
a.printTest()
for seg in a :
  print seg, seg.x, seg.hh.gnabar

Post Reply