Running multiple simulations in one process

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

Moderator: hines

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

Re: Running multiple simulations in one process

Post by hines » Tue Jan 06, 2015 4:20 am

Deletion of python sections via h.delete_section(sec=pysec) can be implemented but remember that deletion also occurs when the last python reference to the section dissappears as well.
In the typical case, hoc cell templates that construct hoc sections or python cell objects that construct python sections, the section are deleted with the cell is destroyed (though in the
latter case, the sections are destroyed only if there are no references to the section outside the cell object.

With regard to h.allsec(). that really is all the hoc and python sections so I'm not sure I understand what you mean.

Code: Select all

>>> from neuron import h
>>> h('create soma1')
1
>>> s = h.Section(name='soma2')
>>> for sec in h.allsec():
...   print sec.name()
... 
soma1
soma2


aaronmil
Posts: 35
Joined: Fri Apr 25, 2014 10:54 am

Re: Running multiple simulations in one process

Post by aaronmil » Tue Jan 06, 2015 11:14 am

hines wrote:Deletion of python sections via h.delete_section(sec=pysec) can be implemented but remember that deletion also occurs when the last python reference to the section dissappears as well.
In the typical case, hoc cell templates that construct hoc sections or python cell objects that construct python sections, the section are deleted with the cell is destroyed (though in the
latter case, the sections are destroyed only if there are no references to the section outside the cell object.
I think I finally figured out why I was having such a difficult time with python sections not being destroyed. While learning how to specify models, I've been executing code in .py files, and then creating and investigating variables from an iPython console, and going back and forth between the two. Apparently iPython "keeps alive" an awful lot of references, making it near impossible to destroy hoc objects. If I limit the executed code to a single run of a .py file, then objects can be destroyed more effectively. However you can see how this is awkward for a new user to have to deal with, so a simple solution would be let the user destroy hoc objects manually, since they will likely not encounter any errors from the references that exist in iPython if they will not be calling any of those references.
hines wrote: With regard to h.allsec(). that really is all the hoc and python sections so I'm not sure I understand what you mean.
I was more requesting a feature than reporting a bug. Since I have been having issues with h.allsec() still containing references to objects that I attempted but failed to destroy from an iPython console, it would be nice if h.allsec() took an argument to limit the sections it reports, as follows:

Code: Select all

from neuron import h

class CellClass(object):
	def __init__(self):
		self.sec_list = []
	
cell1 = CellClass()

soma1 = h.Section(name='soma1', cell=cell1)
soma2 = h.Section(name='soma2')
soma3 = h('create soma3')

for s in h.allsec():
	print s.name()
<__main__.CellClass object at 0x1114cbf10>.soma1
soma2
soma3

Code: Select all

h.soma3.push()
h.delete_section()
for s in h.allsec():
	print s.name()
<__main__.CellClass object at 0x1114cbf10>.soma1
soma2

So it would be nice if now I could use:

Code: Select all

for s in h.allsec(cell=cell1):
	print s.name()
to limit the list of reported sections to those attached to cell1:
<__main__.CellClass object at 0x1114cbf10>.soma1

I think the reason why so many other forum posts are about section garbage collection, is that users are using h.allsec() in their code, but getting references to sections that they thought they destroyed. They wouldn't care if they could use h.allsec() to only report sections attached to a cell that is active in the current simulation, even if other sections still exist in the hoc environment which are not being simulated but are still technically referenced.

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

Re: Running multiple simulations in one process

Post by hines » Tue Jan 06, 2015 2:03 pm

I will consider your comments. One problem is that a basic change in this area can have bad ripple effects and generate other kinds of confusion.
An important issue is not only whether a section exists at the interpreter level, but also whether it is being simulated during a run.
Note that I believe your demo idiom where a cell class is created and then you construct a section outside of the cell using

Code: Select all

soma1 = h.Section(name='soma1', cell=cell1)
is quite outside the style I was thinking about when I made the named cell=cell1 arg. I would instead have created the sections within the cell1 instance
with

Code: Select all

self.soma1 = h.Section(name='soma1', cell=cell1)
so that when the cell is destroyed, so is the soma1. Also cell1.soma1 makes more sense to me considering that one of the purposes of a CellClass is to have
many instances and I would have named it so it would exist as cell1.soma

aaronmil
Posts: 35
Joined: Fri Apr 25, 2014 10:54 am

Re: Running multiple simulations in one process

Post by aaronmil » Tue Jan 06, 2015 3:37 pm

hines wrote:I will consider your comments.
Thank you.
hines wrote: One problem is that a basic change in this area can have bad ripple effects and generate other kinds of confusion.
An important issue is not only whether a section exists at the interpreter level, but also whether it is being simulated during a run.
Note that I believe your demo idiom where a cell class is created and then you construct a section outside of the cell using

Code: Select all

soma1 = h.Section(name='soma1', cell=cell1)
is quite outside the style I was thinking about when I made the named cell=cell1 arg. I would instead have created the sections within the cell1 instance
That is actually how I've been structuring my cell classes, that simplification was just to quickly illustrate that I meant a Python class that had nothing to do with a hoc template.

I have a related question. Does the gid used by ParallelContext require that a cell class be implemented as a hoc template? Or can that refer to a parameter of a Python class?

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

Re: Running multiple simulations in one process

Post by hines » Wed Jan 07, 2015 1:20 am

Does the gid used by ParallelContext require that a cell class be implemented as a hoc template? Or can that refer to a parameter of a Python class?
It does not require a hoc template. But whether or not the gid is a parameter of a Python class is not relevant. The gid is actually associated with a spike
detector using pc.cell(gid, h.NetCon(some_section(x)._ref_v, None, sec=self.some_section))
Any section would work, however if the section was created with a cell=self named arg, then pc.gid2cell(gid) will return the correct python cell.

Post Reply