Delete a section does not work

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

Delete a section does not work

Post by mattions »

in python seems to not be possible to delete a section

Code: Select all

from neuron import h
a = h.Section()
b = h.Section()
b.connect(a)
c = h.Section()
c.connect(a)
h.topology()
h.delete_section(c)
I've tried also:

Code: Select all

h.delete_section(c.name())
and

Code: Select all

c.push()
h.delete_section()
Any idea?
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Delete a section does not work

Post by hines »

You must have seen the message:
Cannot delete an unnamed section

Python Section objects are deleted when their reference count goes to 0. Use

Code: Select all

c = None
Note that if it was possible to delete a Python Section with delete_section(), that
h.delete_section(c) would not delete c but the currently accessed section (probably a)
and you would have to use h.delete_section(sec=c) or make c the currently accessed
section some other way.

If sections are created using the "create" statement, then they are deletable. eg.

Code: Select all

from neuron import h
h('create a, b, c')
h.topology()
c = h.c
print c, c.name()
h.delete_section(sec=c)
h.topology()
PhilippRautenberg
Posts: 15
Joined: Wed Dec 06, 2006 10:53 am

Re: Delete a section does not work

Post by PhilippRautenberg »

I have a similar problem:
My simulation loads the morphology of a single cell. As the morphology is described within a hoc-file, I run

Code: Select all

neuron.h.load_file('file.hoc')
Here, a dendrite is composed of about thousand sektions (neuron.h.dend[0]... neuron.h.dend[1000] ). To facilitate the control I also collect sektions in regions (e.g. reg_dend).

Now I want to change the morphology. For this I need to delete the old one (otherwise my simulation returns wrong results). I tryed to delete the entire neuron module. But as soon as I import it again, the morphology is also there again. The only way I see is to run a seperate python programm for each cell. This solution would be very inconvenient. What can I do?
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Delete a section does not work

Post by hines »

You should be able to delete all the hoc declared sections with:

Code: Select all

h('forall delete_section)')
#and check that they are gone with
h.topology()
Be sure not to use any python references to these deleted sections.
PhilippRautenberg
Posts: 15
Joined: Wed Dec 06, 2006 10:53 am

Re: Delete a section does not work

Post by PhilippRautenberg »

Perfect, so I can now delete sections. The references (section lists - within the example below: 'region') are the problems now, indeed. But I can't delete them.

Code: Select all

for sec in h.reg_soma:
  sec.insert(mechanism)
causes the error:

Code: Select all

NEURON: Accessing a deleted section
 near line 0
 forall delete_section()
                        ^
Segmentation fault
I really don't understand the internal handling of NEURON, as the error just occurs in the following scenario:
  • load morphology A with section lists (reg_soma, reg_dend)
  • insert mechanisms into sections of 'reg_soma'
  • run simulation
  • load morphology B with section lists (reg_soma, reg_dend)
  • insert mechanisms into sections of 'reg_soma'
  • run simulation
  • load morphology A (again) with section lists (reg_soma, reg_dend)
  • insert mechanisms into sections of 'reg_soma'
  • -> ERROR as described above
The same type of phenomenon I had also before: It seems that I can load a new morphology on top of another one.Everything is fine even without deleting anything. But as soon as I change back to an previously used one, either the simutation returns the results of the latest newly added morphology, or - if I delete the sections - it crashes with the error above. Any idea?
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Delete a section does not work

Post by hines »

Try destroying the SectionList objects to make sure all the references to deleted sections are not re-used.
PhilippRautenberg
Posts: 15
Joined: Wed Dec 06, 2006 10:53 am

Re: Delete a section does not work

Post by PhilippRautenberg »

The problem seems to be that I can't reload a morphology that was deleted once.
hoc-file little_model.hoc :

Code: Select all

proc celldef() {
  topol()
  subsets()
  geom()
  biophys()
  geom_nseg()
}
    
create soma[3]
create dend_contra[3]
create dend_ipsi[3]
proc topol_0() {
  connect soma[1](0),soma[0](1)
  connect soma[2](0),soma[0](1)
  connect dend_contra[0](0),soma[1](1)
  connect dend_contra[1](0),dend_contra[0](1)
  connect dend_contra[2](0),dend_contra[1](1)
  connect dend_ipsi[0](0),soma[2](1)
  connect dend_ipsi[1](0),dend_ipsi[0](1)
  connect dend_ipsi[2](0),dend_ipsi[1](1)
}
proc topol() {
  topol_0()
  basic_shape()
}
    
proc shape3d_0() {
 soma[0] {pt3dclear()
    pt3dadd(265.213004248, 82.7914962769, 1.9536999464, 2.0)
    pt3dadd(265.213104248, 82.7914962769, 1.9536999464, 2.0)
 }
 soma[1] {pt3dclear()
    pt3dadd(265.213104248, 82.7914962769, 1.9536999464, 2.0)
    pt3dadd(265.213500977, 83.2928009033, 1.9536999464, 2.0)
 }
 soma[2] {pt3dclear()
    pt3dadd(265.213104248, 82.7914962769, 1.9536999464, 2.0)
    pt3dadd(265.212585449, 82.2902984619, 1.9536999464, 2.0)
 }
 dend_contra[0] {pt3dclear()
    pt3dadd(265.213500977, 83.2928009033, 1.9536999464, 2.0)
    pt3dadd(265.213897705, 83.7939987183, 1.9536999464, 2.0)
 }
 dend_contra[1] {pt3dclear()
    pt3dadd(265.213897705, 83.7939987183, 1.9536999464, 2.0)
    pt3dadd(265.214294434, 84.2951965332, 1.9536999464, 2.0)
 }
 dend_contra[2] {pt3dclear()
    pt3dadd(265.214294434, 84.2951965332, 1.9536999464, 2.0)
    pt3dadd(265.214691162, 84.7964019775, 1.9536999464, 2.0)
 }
 dend_ipsi[0] {pt3dclear()
    pt3dadd(265.212585449, 82.2902984619, 1.9536999464, 2.0)
    pt3dadd(265.212188721, 81.789100647, 1.9536999464, 2.0)
 }
 dend_ipsi[1] {pt3dclear()
    pt3dadd(265.212188721, 81.789100647, 1.9536999464, 2.0)
    pt3dadd(265.211914062, 81.287902832, 1.9536999464, 2.0)
 }
 dend_ipsi[2] {pt3dclear()
    pt3dadd(265.211914062, 81.287902832, 1.9536999464, 2.0)
    pt3dadd(265.211395264, 80.7865982056, 1.9536999464, 2.0)
 }
}
proc basic_shape() {
  shape3d_0()
}

objref reg_all
objref reg_soma
objref reg_dend_contra
objref reg_dend_ipsi

proc subsets() { local ii
  reg_all = new SectionList()
  reg_soma = new SectionList()
  for ii = 0, 2  soma[ii] {
    reg_soma.append()
    reg_all.append()
  }
  reg_dend_contra = new SectionList()
  for ii = 0, 2  dend_contra[ii] {
    reg_dend_contra.append()
    reg_all.append()
  }
  reg_dend_ipsi = new SectionList()
  for ii = 0, 2  dend_ipsi[ii] {
    reg_dend_ipsi.append()
    reg_all.append()
  }
}
proc geom() {
}
proc geom_nseg() {
}
proc biophys() {
}
access soma
celldef()
And here the code (here I also try to delete the section list as I thought in the beginning that there error lies here...):

Code: Select all

import neuron
import nrn

STDGUI = 'stdgui.hoc'
LIBNRNMECH = 'NEURON/i686/.libs/libnrnmech.so'
hoc_file = 'little_model.hoc'

neuron.h.load_file(STDGUI)
neuron.h.nrn_load_dll(LIBNRNMECH)

neuron.h.load_file(hoc_file)
neuron.h.topology()
# Output:
# < tree >
neuron.h.reg_dend_contra.printnames()
# Output:
# dend_contra[0]
# dend_contra[1]
# dend_contra[2]
neuron.h('forall delete_section()')
neuron.h.topology()
# Output:
# < nothing >
neuron.h.reg_dend_contra.printnames()
# Output:
# < nothing >

# Now I try to delete the section list:
neuron.h('delete reg_dend_contra')
# Output:
# NEURON: syntax error                       
# near line 0                               
# delete reg_dend_contra
#                      ^

neuron.h.reg_dend_contra.remove()
# Output:
# NEURON: Accessing a deleted section
# near line 0
# ^ 
#        SectionList[2].remove()
# oc_restore_code tobj_count=1 should be 0
# ...

del(neuron.h.reg_dend_contra)
# Output:
# TypeError: function takes at least one argument

# ... doesn't work

# Now I load the morphology again:
neuron.h.load_file(hoc_file)
neuron.h.topology()
# Output:
# < nothing >
neuron.h.reg_dend_contra.printnames()
# Output:
# < nothing >

# the morphology seems not to be loaded again (and therefore the section list
# seems to point to the old deleted sections)
I can do load a new morphology - but never load a deleted again.
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Delete a section does not work

Post by hines »

Two problems.
Hoc objects do not get deleted using the 'delete' keyword. Redeclare or assign to None. eg.

Code: Select all

neuron.h('objref reg_dend_contra')
load_file(fname) only loads once. To force load use:

Code: Select all

neuron.h.load_file(1, hoc_file)
PhilippRautenberg
Posts: 15
Joined: Wed Dec 06, 2006 10:53 am

Re: Delete a section does not work

Post by PhilippRautenberg »

Tank you a lot! That solves the problem!
PhilippRautenberg
Posts: 15
Joined: Wed Dec 06, 2006 10:53 am

How to delete an object like IClamp

Post by PhilippRautenberg »

Hello together,

I got a new "delete" problem. When I delete all sections as described before, there remain still attached point process like a IClamp electrode. To problems are caused by this: If I open the gui with

Code: Select all

from neuron import gui
and go to model view to plot the cell, neuron crashes with

Code: Select all

NEURON: point process not located in a section
...
.

Also: The used memory increases with each change of morphology. This might be caused by the remaining point process and recording vectors.

How can I delete point processes and recording vectors? (It seems, that overwriting the reference does not work for the python-way)

Cheers,
Philipp
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Delete a section does not work

Post by hines »

Objects are destroyed only when there are no references to them.
Overwriting a reference does work in the python way in the sense that the
variable no longer is a reference to the object it used to point to.
Are you keeping a reference to the point process in some other list?
You can get a list of the things that reference a hoc object by using
http://www.neuron.yale.edu/neuron/stati ... references
anna

Delete a section does not work

Post by anna »

Hello,
I also have a problem when deleting sections (in python).
Indeed, for an isolated section, sec = None works fine.
Unfortunately, I'm unable to loop this. The sections aren't really deleted. Here's a code example:

Code: Select all

from neuron import h

soma = h.Section()
axon = h.Section()
dendrite = h.Section()

def check():
	print '-------'
	for sec in h.allsec():
		print sec	
	print '-------'
		
check()

soma = None

check()

for section in h.allsec():
	section = None
	print section
check()
Which gives as output:
-------
<nrn.Section object at 0x52520>
<nrn.Section object at 0x52560>
<nrn.Section object at 0x52540>
-------
-------
<nrn.Section object at 0x52560>
<nrn.Section object at 0x52540>
-------
None
None
-------
<nrn.Section object at 0x52560>
<nrn.Section object at 0x52540>
-------

I suspect there is an issue with h.allsec(), but I've no clear idea...
Could you please help me?
Thanks,
Anna
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Delete a section does not work

Post by hines »

The problme is that the
section = None
statement does not reduce the reference count to 0 because those sections continue
to be referenced by axon and dendrite. The only way to get rid of these sections
created by Python is to
axon = None
dendrite = None
ngreiner
Posts: 11
Joined: Tue Feb 28, 2017 8:12 am

Re: Delete a section does not work

Post by ngreiner »

Hello,

I have also fell into a trap where HOC sections were not deleted as I wanted to.
I had two python objects which referenced each other (thus forming a reference cycle), and one of which held a list of references to HOC sections.
When dereferencing the python objects (either using 'del obj' or 'obj = new_obj' for both objects), their reference count never got to 0, and thus their deletion was delayed to the next collection performed by the garbage collector of python. The deletion of the HOC sections referenced by one of my two python objects was thus also delayed to the next collection performed by the garbage collector of python.
While running simulations into a loop where I employed the idioms 'obj1 = new_obj' and 'obj2 = new_obj' at the beginning of the loop to create new HOC sections for the new simulation, the HOC sections from the previous simulations were actually not deleted, which drastically slowed down the consecutive simulations.
I fixed this bug by using weakrefs between my two objects rather than strong-refs.

Just wanted to share in case somebody else encounters that issue.

Greetings,
Nathan
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Delete a section does not work

Post by ted »

Thanks, Nathan. Readers who are interested in examples and discussions about weak references might, in addition to reading Python's own documentation, do a web search for
python weak reference
which will turn up several relevant hits in Stack Overflow https://stackoverflow.com/
Post Reply