Obtaining a segmentation fault using neuron

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

Moderator: hines

Post Reply
romain.caze
Posts: 16
Joined: Fri Oct 29, 2010 6:22 am

Obtaining a segmentation fault using neuron

Post by romain.caze » Tue Feb 11, 2014 5:17 am

Dear all,

The following python code snippet creates a segmentation fault (making the ipython kernel crashes).

Code: Select all

from neuron import nrn, h

def section_print():
    """Print the different sections in the NEURON namespace"""
    for sec in h.allsec():
        print sec.name()

class Segfault(nrn.Section):
    """
    Should wrap a NEURON section
    """
    def __init__(self, name="fault"):
        """Initialize the section
        """
        #Create a section within h
        h("create {}".format("fault"))
        #Address this section to the object
        self = getattr(h, "fault")
        #Set an attribut
        self.L = 10
seg = Segfault()
section_print()
seg.L
The section_print() is here to show why I wrote this code in the first place. I wanted to wrap the neuron section into an object without passing by h, in other words to not have to write h.soma when you refer to the neuron section soma.

The section_print shows that it works well at the beginning (there is a "fault" section created within h), but trying to get the attribut "L" makes the kernel crash. I found solutions (/dirty tricks) to avoid this problem but I do not understand why it is happening. I already came across segmentation faults, e.g. trying to change a cell model on the fly.

So I have three questions:

What is happening here?
How come neuron is capable of producing segmentation faults?
Are there simple rules of thumb to avoid segmentation fault using neuron?

Best,
Romain

regger
Posts: 21
Joined: Sun Apr 22, 2012 9:49 pm

Re: Obtaining a segmentation fault using neuron

Post by regger » Tue Feb 11, 2014 8:04 am

NEURON can crash with a segfault because it's written in C, you're just using a python wrapper. So behind the scenes, there's still a lot of pointers hanging around, and you may or may not have done something there...
Anyways, to subclass the neuron section, you should initialize the nrn.Section object explicitly.
Try something like

Code: Select all

class NewSection(nrn.Section):
    """
    Should wrap a NEURON section
    """
    def __init__(self, name="fault"):
        """Initialize the section
        """
        nrn.Section.__init__(self, name=name)
Since you now have called the nrn.Section.__init__ method, you don't need to access anything through the h object anymore, you should be able to just do something like:

Code: Select all

newSec = NewSection(name='something')
newSec.L = 123.4
h.psection(sec=newSec)
Best, Robert

romain.caze
Posts: 16
Joined: Fri Oct 29, 2010 6:22 am

Re: Obtaining a segmentation fault using neuron

Post by romain.caze » Tue Feb 11, 2014 11:30 am

Thank you Robert for you quick answer.

I am happy you propose this solution as it was the first thing I did. I was really happy until I typed (assuming that you runned the code your put in your response before)

Code: Select all

from neuron import h
h("print newSec.L")
This strangely returns an error message as if the section is inexistant.

Code: Select all

NEURON: syntax error
 near line 0
 print newSec.L
             ^
I was puzzled of this error message coming from nowhere and unhappy with it because I wanted to use h afterwards in my code. So now I have another question:
Why is it returning this error message?

Best,
Romain

romain.caze
Posts: 16
Joined: Fri Oct 29, 2010 6:22 am

Re: Obtaining a segmentation fault using neuron

Post by romain.caze » Tue Feb 11, 2014 11:35 am

I forgot something.
Even more puzzling, if after creating the section using the previous method you type

Code: Select all

h.newS
And use the tab autocompletion in ipython it turns it into

Code: Select all

h.newSec
But when you type enter it returns

Code: Select all

AttributeError: 'hoc.HocObject' object has no attribute 'newSec'
???

regger
Posts: 21
Joined: Sun Apr 22, 2012 9:49 pm

Re: Obtaining a segmentation fault using neuron

Post by regger » Wed Feb 12, 2014 7:29 am

Hi Romain,
yes I tried the code I posted and it works fine for me.
On the other hand, I can't reproduce the autocomplete thing at the iptyhon console. You would have to post the whole code you were using.
But it's probably related to the other problem. It seems like the python object newSec does not exist in the hoc object namespace... I vaguely remember something about a PythonObject in the hoc interpreter, but that's about it.
Also, just wondering - why do you want to go through all the trouble of executing hoc statements such as print from python when you have basically all methods you need available in the python world now?

romain.caze
Posts: 16
Joined: Fri Oct 29, 2010 6:22 am

Re: Obtaining a segmentation fault using neuron

Post by romain.caze » Mon Feb 17, 2014 7:26 am

Dear Robert,

When you say the "code works fine for me" you mean you obtain the same kind of error, don't you?

This is exactly the problem: "the object newSec does not exit in the hoc object namespace". Is there a purpose to that, because it looks like an problematic bug to me.

Obviously I am doing things a bit more complex than "print" (I just wanted the simplest command to illustrate the problem). I am creating sections to automatically generate neurons morphologies (using another class e.g. BipolarNeuron). In the process, I am recoding some hoc high level function (like the one in Mainen & Sejnowski 1998 to add an axon to a neuron) and as a first code draft it is more handy to work with direct hoc statements.

BTW ipython and autocompletion is pure gold to me;)

Best,
Romain

regger
Posts: 21
Joined: Sun Apr 22, 2012 9:49 pm

Re: Obtaining a segmentation fault using neuron

Post by regger » Mon Feb 17, 2014 7:42 am

romain.caze wrote:Dear Robert,

When you say the "code works fine for me" you mean you obtain the same kind of error, don't you?
No. I mean it works and I don't get an error. I do get the same error as you when I execute your statements afterwards. However, I don't get the autocomplete option for h.newSec in ipython.
romain.caze wrote:This is exactly the problem: "the object newSec does not exit in the hoc object namespace". Is there a purpose to that, because it looks like an problematic bug to me.
Sorry, I have no idea whether this behavior is intended by the developers or not...
romain.caze wrote:In the process, I am recoding some hoc high level function (like the one in Mainen & Sejnowski 1998 to add an axon to a neuron) and as a first code draft it is more handy to work with direct hoc statements.
Except for this case here it seems :)
I've gone through this process too (implementing a NEURON interface in python) and I don't think I've used a single h("...") statement... The NEURON python interface can basically do anything I've needed it to. But that's just my two cents, might be different in your case.

Best,
Robert

romain.caze
Posts: 16
Joined: Fri Oct 29, 2010 6:22 am

Re: Obtaining a segmentation fault using neuron

Post by romain.caze » Mon Feb 17, 2014 11:58 am

Dear All,

Thank you for your posts Robert. It helped to better define the questions but it did not answer the first question: "How come there is a segmentation fault using the example code?" nor the second: "How come a section object created with python is not in the h namspace?".

I reinstantiate here the first question (the question of this thread)

Code: Select all

from neuron import nrn, h
class Segfault(nrn.Section):
    """
    Should wrap a NEURON section
    """
    def __init__(self, name="fault"):
        """Initialize the section
        """
        #Create a section within h
        h("create {}".format("fault"))
        #Address this section to the object
        self = getattr(h, "fault")
        #Set an attribut
        self.L = 10
seg = Segfault()
print seg.L
Why does this code snippet trigger a segmentation fault? (knowing that NEURON is written in C, explaining the numerous ways of obtaining segmentation faults)

For the second I opened a new thread in the present forum (maybe there is another forum where NEURON developpers can help but I only know this one).

If you have an answer for the first question please post it here, if you have an answer for the second question please post it in the other thread.

Best,
Romain

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

Re: Obtaining a segmentation fault using neuron

Post by hines » Mon Feb 17, 2014 12:51 pm

I'm afraid I don't have a direct answer to your question since I don't understand the full implications of the statement

Code: Select all

self = getattr(h, "fault")
I'll have to study that a bit more. Anyway, if all you want is to avoid typeing the 'h.' when you use the section in python and you also want to have the section available in hoc , consider
pythonname = h.hoc_name_of_section

However, do you want the hoc section to be freed when python no longer references the hoc section. That would require something similar to what you are trying to do but with a destructor that
explicitly makes the hoc section the currently accessed section and then calls h.delete_section(). Or h.delete_section(sec=self) But as I say, the 'self' in this context is giving me a lot of
conceptual confusion. My confusion is partly that the nrn.Section() normally creates a wrapped anonymous section from the hoc perspective, then you are creating a named hoc section, then
a field of the object you created (Segfault instance) is assigned to be the object itself. So wouldn't the Segfault instance reference count go to 0? I could almost imagine getting a segfault in native
python under these circumstances.

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

Re: Obtaining a segmentation fault using neuron

Post by hines » Mon Feb 17, 2014 2:44 pm

I'm still unsure what it means, precisely, to reset the 'self' while constructing an object. However, the following works:

Code: Select all

class Segfault(nrn.Section):
    """
    Should wrap a NEURON section
    """
    def __init__(self, name="fault"):
        """Initialize the section
        """
        nrn.Section.__init__(self, name=name)
        self.L = 10
seg = Segfault()   
seg.L
for sec in h.allsec():
  print sec.name()
I read in my Python reference that when an instance is created, the __init__ methods of the base classes are not invoked
and so it is up to the derived class to involde the nrn.Section.__init__(self, ...) method. I don't see any way this can be avoided
as the segmentation violation is due to an internal sec=0 pointer inside the nrn.Section instance.

romain.caze
Posts: 16
Joined: Fri Oct 29, 2010 6:22 am

Re: Obtaining a segmentation fault using neuron

Post by romain.caze » Tue Feb 18, 2014 6:26 am

Hi Ted,

Thank you for your answer, you gave the cause of the segmentation fault (nrn.Section), and I agree the method you propose (that we discussed previously with robert) seems the most natural.
But:

Anyway, if all you want is to avoid typeing the 'h.' when you use the section in python and you also want to have the section available in hoc , consider
pythonname = h.hoc_name_of_section
Does not work (see the thread called "Section object created in python partially existing in hoc") about this issue.

This is what I am currently using in my code (working), the reason being that it makes the section appears in hoc and it makes it accessible through hoc; whereas this is not the case for the method using nrn.Section.

Code: Select all

from neuron import nrn, h
class Mechanism(object):
    """
    Create mechanism to insert in the membrane of a cell

    Examples
    --------
    >>> leak = Mechanism('pas', {'e': -65, 'g': 0.0002})
    >>> hh = Mechanism('hh')
    """
    def __init__(self, name, parameters={}):
        """
        Parameters
        ----------
        name: a char
            the label of the mechanism
        parameters: a dictionary
            contains the different parameter of a mechanism
        """
        self.name = name
        self.parameters = parameters

    def insert_into(self, section):
        """
        Method used to insert a mechanism into a section

        Parameters
        ----------
        section: a NEURON section
            the section where the mechanism needs to be inserted

        """
        section.insert(self.name)
        for name, value in self.parameters.items():
            for segment in section:
                mech = getattr(segment, self.name)
                setattr(mech, name, value)


class Section(object):
    """
    Create a NEURON section
    """
    def __init__(self, name, parameters={"L":10, "diam":10, "nseg":1, "Ra":100, "cm":1},
                 mechanisms=[], parent=None, p_point=0, c_point=0):
        """Initialize the section
        """
        h("create {}".format(name))
        self = getattr(h, name)
        #Old method to create the section, nice but no appearance in h...
        #To work it also need to inherit from nrn.Section
        #super(Section, self).__init__(name=name)
        for key, value in parameters.items():
            #Set the property of the section
            setattr(self, key, value)

        #Connect to parent section
        if parent:
            h("connect %s(%d), %s(%d)"%(self.name(), c_point, parent, p_point))
        #Add the mechanisms
        for mechanism in mechanisms:
            mechanism.insert_into(self)

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

Re: Obtaining a segmentation fault using neuron

Post by hines » Tue Feb 18, 2014 7:36 am

Anyway, if all you want is to avoid typeing the 'h.' when you use the section in python and you also want to have the section available in hoc , consider
pythonname = h.hoc_name_of_section

Does not work (see the thread called "Section object created in python partially existing in hoc") about this issue.
I wa a little unclear. I meant that hoc_name_of_section came from a h('create hoc_name_of_section'). I.e
h("create soma")
soma = h.soma
soma.L = 10

Now you can use soma in python whereever you could use h.soma

Post Reply