Page 1 of 1
SubClassing a section
Posted: Mon Jan 26, 2009 1:46 am
by mattions
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
Re: SubClassing a section
Posted: Mon Jan 26, 2009 7:11 am
by marekrud
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
Re: SubClassing a section
Posted: Mon Jan 26, 2009 3:12 pm
by hines
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
gives the least namespace polution if one is going
to subclass Section as well as other Hoc templates.
Re: SubClassing a section
Posted: Tue Jan 27, 2009 6:52 am
by mattions
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.
Re: SubClassing a section
Posted: Thu Jan 29, 2009 9:21 am
by hines
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