how to delete a Point Process object

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

Moderator: hines

Post Reply
oren
Posts: 55
Joined: Fri Mar 22, 2013 1:03 am

how to delete a Point Process object

Post by oren »

Hello,
I am trying to delete an Impedance tool in python but I can not manage to do it.
in hoc it is very easy

Code: Select all

objref zz
create soma
zz = new Impedance()
Impedance[0]
I get Impedance[0]

When I do

Code: Select all

objref zz
Impedance[0]
I get
nrniv: Object ID doesn't exist: Impedance[0]

As expected!

--------------------------------------------------------------------------

But when I try the same thing in python

Code: Select all

from neuron import h
soma = h.Section()

zz= h.Impedance()
h.Impedance[0]
I get
<hoc.HocObject at 0x1218c90>

When I try to delete the referance

Code: Select all

zz = None
h.Impedance[0]
I still get
<hoc.HocObject at 0x1218f60>

I also try the StringFunctions()

Code: Select all

str  = h.StringFunctions()
str.references(Impedance[0])
I get

Impedance[0] has 4 references
found 0 of them


So how can I delete the Impedance tool? ( The same thing happens with IClamp VClamp ect..)

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

Re: how to delete a Point Process object

Post by ted »

zz=None deletes it.

Code: Select all

>>> from neuron import h
>>> soma = h.Section()
>>> zz = h.Impedance()
>>> zz
<hoc.HocObject object at 0x9337cf0>
>>> h.allobjects("Impedance")
Impedance[0] with 1 refs
0.0
>>> zz=None
>>> h.allobjects("Impedance")
0.0
>>> h.Impedance[0]
Traceback (most recent call last):
  File "stdin", line 1, in <module>
IndexError: Impedance[0] instance does not exist
By the way, the Impedance class is not a "Point Process object". Most "point process" classes are localized signal sources, like a synaptic mechanism or a current or voltage clamp--something that generates a current that is localized to a region that is very small compared to the space constants for membrane potential or ionic diffusion. One (APcount) counts the number of times the voltage at its location crosses a threshold voltage in the positive direction. Some are spike sources (generate events that can be monitured by NetCons and delivered to targets).
oren
Posts: 55
Joined: Fri Mar 22, 2013 1:03 am

Re: how to delete a Point Process object

Post by oren »

Hello Ted,
Thank You,

When I run your code, I do manage to delete the Impedance,
But when I run the following code

Code: Select all

from neuron import h

soma = h.Section()

zz= h.Impedance()
h.Impedance[0]

zz = None
zz= None does not delete the Impedance,
Can you try it?

It seems that running the line
h.Impedance[0]
make a reference to this object, and then zz=None does not delete it, am I correct ?

Thank You,
Oren.
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: how to delete a Point Process object

Post by hines »

I'll look into this. It is a puzzle because it seems to happen only as long as there is limited communication with the
hoc interpreter from python and the commands are entered from the terminal window. I presume your experience shadows the comments below

Code: Select all

from neuron import h
soma = h.Section()
zz= h.Impedance()
h.Impedance[0]      
zz=None
print 'hello'  
h.Impedance[0] #still exists if copy pasted to the interpreter, does not exist if executed from a file
h.allobjects()
print 'goodbye'
h.Impedance[0] #raises an error
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: how to delete a Point Process object

Post by hines »

The issue arose from the fact that in interactive mode at least, the interpreter saves the last expression return value in the variable '_'. I.e. consider the following transcript

Code: Select all

>>> from neuron import h
>>> soma = h.Section()
>>> zz= h.Impedance()
>>> h.Impedance[0]
<hoc.HocObject object at 0x2c01ae0>
>>> zz=None
>>> _
<hoc.HocObject object at 0x2c01ae0>
>>> 1
1
>>> h.Impedance[0]
Traceback (most recent call last):
  File "stdin", line 1, in <module>
IndexError: Impedance[0] instance does not exist
oren
Posts: 55
Joined: Fri Mar 22, 2013 1:03 am

Re: how to delete a Point Process object

Post by oren »

Hi Michel
Thank You,
In python your code does work,

But in ipython it does not work

Code: Select all

In [2]: soma = h.Section()
In [3]: zz= h.Impedance()
In [4]: h.Impedance[0]
Out[4]: <hoc.HocObject at 0x1a0c420>
In [5]: zz=None
In [6]: _
Out[6]: <hoc.HocObject at 0x1a0c420>
In [7]: 1
Out[7]: 1
In [8]: h.Impedance[0]
Out[8]: <hoc.HocObject at 0x1a0c588>

But now I understand where to look...
Thank You,
Oren.
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: how to delete a Point Process object

Post by hines »

Yes. I believe IPython saves every return value for expressions typed interactively.
oren
Posts: 55
Joined: Fri Mar 22, 2013 1:03 am

Re: how to delete a Point Process object

Post by oren »

Code: Select all

%reset Out
seems to fix the problem.
Marin
Posts: 2
Joined: Fri Jan 25, 2019 9:21 am

Re: how to delete a Point Process object

Post by Marin »

Sorry to resurrect this old thread, but has a final solution to this problem been found? I really enjoy working with NEURON in a jupyter notebook, but I cannot seem to be able to delete a PointProcess. Here is a sample output from a notebook:

Code: Select all

from neuron import h
soma = h.Section()
soma.psection()

Code: Select all

    {'point_processes': {},
     'density_mechs': {},
     'ions': {},
     'morphology': {'L': 100.0,
      'diam': [500.0],
      'pts3d': [],
      'parent': None,
      'trueparent': None},
     'nseg': 1,
     'Ra': 35.4,
     'cm': [1.0],
     'regions': set(),
     'species': set(),
     'name': '__nrnsec_0x7fd9f8ad8e00',
     'hoc_internal_name': '__nrnsec_0x7fd9f8ad8e00',
     'cell': None}

Code: Select all

i = h.IClamp(0.5, sec=soma)
soma.psection()

Code: Select all

    {'point_processes': {'IClamp': {IClamp[0]}},
     'density_mechs': {},
     'ions': {},
     'morphology': {'L': 100.0,
      'diam': [500.0],
      'pts3d': [],
      'parent': None,
      'trueparent': None},
     'nseg': 1,
     'Ra': 35.4,
     'cm': [1.0],
     'regions': set(),
     'species': set(),
     'name': '__nrnsec_0x7fd9f8ad8e00',
     'hoc_internal_name': '__nrnsec_0x7fd9f8ad8e00',
     'cell': None}

Code: Select all

h.IClamp[0]

Code: Select all

    IClamp[0]

Code: Select all

i = None
_
None
[code]soma.psection()

Code: Select all

    {'point_processes': {'IClamp': {IClamp[0]}},
     'density_mechs': {},
     'ions': {},
     'morphology': {'L': 100.0,
      'diam': [500.0],
      'pts3d': [],
      'parent': None,
      'trueparent': None},
     'nseg': 1,
     'Ra': 35.4,
     'cm': [1.0],
     'regions': set(),
     'species': set(),
     'name': '__nrnsec_0x7fd9f8ad8e00',
     'hoc_internal_name': '__nrnsec_0x7fd9f8ad8e00',
     'cell': None}

Code: Select all

%reset -f out

Code: Select all

    Flushing output cache (5 entries)

Code: Select all

soma.psection()

Code: Select all

    {'point_processes': {'IClamp': {IClamp[0]}},
     'density_mechs': {},
     'ions': {},
     'morphology': {'L': 100.0,
      'diam': [500.0],
      'pts3d': [],
      'parent': None,
      'trueparent': None},
     'nseg': 1,
     'Ra': 35.4,
     'cm': [1.0],
     'regions': set(),
     'species': set(),
     'name': '__nrnsec_0x7fd9f8ad8e00',
     'hoc_internal_name': '__nrnsec_0x7fd9f8ad8e00',
     'cell': None}

Thanks!

EDIT: I should say I'm using NEURON -- VERSION 7.6.4 master (50728e66) 2018-12-14
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: how to delete a Point Process object

Post by hines »

Something mysterious is going on with jupyter that I can't yet quite figure out.
Can you work around the problem with
print (soma.psection())
Everytime Jupyter default pretty prints the result of soma.psection(), the hoc reference count of the POINT_PROCESS is incremented
and never gets decremented even with a reset -f Out.
(note: my "jupyter console" even alphabetically sorts the pretty print) e.g.

Code: Select all


hines@hines-T7500:~$ jupyter console
Jupyter console 5.2.0

Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.



In [1]: from neuron import h

In [2]: soma = h.Section()

In [3]: i = h.IClamp(soma(.5))

In [4]: print (soma.psection())
{'point_processes': {'IClamp': {IClamp[0]}}, 'density_mechs': {}, 'ions': {}, 'morphology': {'L': 100.0, 'diam': [500.0], 'pts3d': [], 'parent': None, 'trueparent': None}, 'nseg': 1, 'Ra': 35.4, 'cm': [1.0], 'regions': set(), 'species': set(), 'name': '__nrnsec_0x4d9af70', 'hoc_internal_name': '__nrnsec_0x4d9af70', 'cell': None}

In [5]: h.allobjects()
IClamp[0] with 1 refs
MechanismType[1] with 1 refs
CVode[0] with 1 refs
Out[5]: 0.0

In [6]: i=None

In [7]: h.allobjects()
MechanismType[1] with 1 refs
CVode[0] with 1 refs
Out[7]: 0.0

In [8]: i = h.IClamp(soma(.5))

In [9]: soma.psection()
Out[9]: 
{'Ra': 35.4,
 'cell': None,
 'cm': [1.0],
 'density_mechs': {},
 'hoc_internal_name': '__nrnsec_0x4d9af70',
 'ions': {},
 'morphology': {'L': 100.0,
  'diam': [500.0],
  'parent': None,
  'pts3d': [],
  'trueparent': None},
 'name': '__nrnsec_0x4d9af70',
 'nseg': 1,
 'point_processes': {'IClamp': {IClamp[0]}},
 'regions': set(),
 'species': set()}

In [10]: h.allobjects()
IClamp[0] with 2 refs
MechanismType[3] with 1 refs
CVode[0] with 1 refs
Out[10]: 0.0

In [11]: %reset -f Out
Flushing output cache (4 entries)

In [12]: h.allobjects()
IClamp[0] with 2 refs
MechanismType[3] with 1 refs
CVode[0] with 1 refs
Out[12]: 0.0

In [13]: i=None

In [14]: h.allobjects()
IClamp[0] with 1 refs
MechanismType[3] with 1 refs
CVode[0] with 1 refs
Out[14]: 0.0

In [15]: whos
Variable   Type         Data/Info
---------------------------------
h          HocObject    <TopLevelHocInterpreter>
i          NoneType     None
soma       Section      __nrnsec_0x4d9af70

In [16]: 

Marin
Posts: 2
Joined: Fri Jan 25, 2019 9:21 am

Re: how to delete a Point Process object

Post by Marin »

Thanks, using

Code: Select all

print(soma.psection())
seems to solve the problem. How weird.

BTW, is there a reason

Code: Select all

h.allobjects()
uses stderr instead of stdout? In my notebook, the output from that function shows up in the terminal, and not inside a cell because of it;
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: how to delete a Point Process object

Post by hines »

is there a reason
I don't know the reason yet.
In my notebook, the output from that function shows up in the terminal
Today I pushed a fix in the master branch of github.com/neuronsimulator/nrn that directs the output of
h.allobjects() to stdout of Python.
Post Reply