Page 1 of 1

reset NEURON

Posted: Mon Aug 08, 2011 3:03 am
by btorb
Hi, Is there a way to "reset" NEURON? In Nest there is a ResetKernel() method. Since i experience some issues with the scope of NEURON in Python, it would be nice to see such a method. Did i overlook it until now? Or, is there a python way of completely reloading the NEURON module during execution?

Kind regards, Ben

Re: reset NEURON

Posted: Mon Aug 08, 2011 10:32 am
by hines
There is no single function that "resets" the interpreter. To clear a model, it generally suffices
to use, in order,
ParallelContext.gid_clear()
unref all the NetCon
unref all the cell objects.

This assumes the usual network style of model construction. For a single top level cell
it usually suffices to
forall delete_section()

Re: reset NEURON

Posted: Mon Aug 08, 2011 11:32 am
by ted
unref all the NetCon
unref all the cell objects
Other top level hoc objects (Vectors, Graphs etc.) will also persist unless you unref them.

Re: reset NEURON

Posted: Thu May 03, 2012 11:16 am
by Prokopiou
I am using a single top level cell and I'm having trouble clearing the hoc interpreter.

I tried executing h("forall delete_section()") but with no success (h.allsec() still returns the same sections).

I also tried looping through the sections in python's end (using for a in h.allsec()) and trying to set a = None (or del a) but still, nothing is deleted and h.allsec() still returns the same values.

This is an issue for me since every time I run my program in the same python interpreter, the hoc interpreter is not cleared and new sections are added to the previous ones.

Am I missing something?

Thanks

Re: reset NEURON

Posted: Thu May 03, 2012 12:32 pm
by ted
Prokopiou wrote:I am using a single top level cell and I'm having trouble clearing the hoc interpreter.

I tried executing h("forall delete_section()") but with no success (h.allsec() still returns the same sections).
Then I guess your code is an example for which the statement
For a single top level cell
it usually suffices to
forall delete_section()
is true for a reason that is not helpful to you.

The workaround that always works is to create a class based on your model cell. That will require a bit of programming effort, but it will solve the problem.

Re: reset NEURON

Posted: Fri Jun 01, 2012 11:19 pm
by ramcdougal
Prokopiou wrote:This is an issue for me since every time I run my program in the same python interpreter, the hoc interpreter is not cleared and new sections are added to the previous ones.
ted wrote:The workaround that always works is to create a class based on your model cell.
An alternative strategy for non-interactive programs is to only do simulations in a fresh copy of the main process made via os.fork(). Either write the output to a file or send it back to the original process via pipes for plotting, analysis, etc...

Re: reset NEURON

Posted: Thu Oct 03, 2013 10:02 pm
by figoyouwei
Hi Hines:

How exactly to do these three things in hoc and in PyNeuron ? thanks !

ParallelContext.gid_clear()
unref all the NetCon
unref all the cell objects.

Re: reset NEURON

Posted: Mon Nov 04, 2013 11:56 am
by ted
Sorry about the delay in answering your question.
ParallelContext.gid_clear()
Suppose this is how you created the ParallelContext instance:
objref pc
pc = new ParallelContext()
Then just execute
pc.gid_clear()

The Python usage would be
h.pc.gid_clear()
assuming that you did
from neuron import h
unref all the NetCon
Easiest way to do this is to keep track of each NetCon when you create it, by appending it to a List, and make sure that no objref points to any NetCon, so the only references to NetCon instances are in that list. Then you can unref that list, and poof! all of the NetCons vanish.

If you are using the NetCon record() method to capture spike times to one or more Vectors, you may also have to unref those Vectors in order to get rid of the NetCons; I'm not sure about this, so you'll have to verify for yourself.
unref all the cell objects.
Same strategy as getting rid of all NetCons, except the part about NetCon record obviously doesn't apply.

Re: reset NEURON

Posted: Wed Nov 06, 2013 12:20 pm
by figoyouwei
thanks for the syntax "pc.gid_clear()"

but what is exactly the syntax for "unref all the NetCon" ? When for example, all the NetCon objects are stored in a list call "nclist" ?

Re: reset NEURON

Posted: Thu Nov 07, 2013 1:20 am
by ted
Suppose each NetCon was created by executing a statement of the form
nclist.append(new NetCon(pre, post))
where nclist is an instance of the List class.

Then
objref nclist
will discard all references to NetCons, as long as there is no other objref that points to a NetCon.

Re: reset NEURON

Posted: Fri Feb 28, 2014 12:17 pm
by romain.caze
Prokopiou wrote:I am using a single top level cell and I'm having trouble clearing the hoc interpreter.

I tried executing h("forall delete_section()") but with no success (h.allsec() still returns the same sections).

I also tried looping through the sections in python's end (using for a in h.allsec()) and trying to set a = None (or del a) but still, nothing is deleted and h.allsec() still returns the same values.

This is an issue for me since every time I run my program in the same python interpreter, the hoc interpreter is not cleared and new sections are added to the previous ones.

Am I missing something?

Thanks
Hi All,

To delete section from h you can use the following trick

Code: Select all

for sec in h.allsec():
    h("%s{delete_section()}"%sec.name())
This is working in the sense that if you create a section within h no prob like:

Code: Select all

from neuron import h
h("create foo")

#Return foo
for sec in h.allsec():
    print sec.name()

#Delete foo
for sec in h.allsec():
    h("%s{delete_section()}"%sec.name())

#This time return nothing
for sec in h.allsec():
    sec.name())
But if you use nrn to create a section you are gonna run into trouble (I let you the fun of discovering that (it is Friday afternoon here I do not have time to finish the post))

Code: Select all

from neuron import nrn
foo = nrn.Section(name="foo")

#Return foo
for sec in h.allsec():
    print sec.name()

#Delete foo
for sec in h.allsec():
    h("%s{delete_section()}"%sec.name())

#This time return foo?
for sec in h.allsec():
    sec.name())