Repeated Step Pulses In Python

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

Moderator: hines

Post Reply
Dmandel
Posts: 13
Joined: Wed Jul 08, 2015 2:32 pm

Repeated Step Pulses In Python

Post by Dmandel »

I'm having a lot of trouble trying to figure out if I should use a mod file or a hoc file to set up a current clamp stimulus to deliver a repeating series of depolarizing step pulses to my existing single compartment with multiple channels (a ses file).

Furthermore, I want to be able to use Python's graphing functionality... Is it possible to do all of this in Python> Namely:

1. I know how to import my ion channels into python (load_file)
2. I don't know how to add the stimuli at predetermined times to my existing ses file.

Always much appreciate your guidance.

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

Re: Repeated Step Pulses In Python

Post by ted »

Dmandel wrote:I'm having a lot of trouble trying to figure out if I should use a mod file or a hoc file to set up a current clamp stimulus to deliver a repeating series of depolarizing step pulses
For something that is likely to have been a common question, see if an answer has already been provided. Searching the Forum for "pulse" turns up, among other things, this
The Best Way to Implement a Pulse Train?
viewtopic.php?f=15&t=2337&p=9127
which in turn links to an item in the FAQ list that answers your question.
I don't know how to add the stimuli at predetermined times to my existing ses file.
Why bother? Use the ses file for whatever you need it for (presumably it recreates a SingleCompartment tool that sets up your model cell), and use Python statements for whatever additional you may need. Organize your program like so:

Code: Select all

from neuron import h, gui
h.load_file("foo.ses") # or whatever you call it
# presumably foo.ses sets up your model cell, and it's a section called soma
stim = h.IPulse1(soma(0.5)) # attach a new IPulse1 to the middle of the soma, or an IPulse2 or 3 if you prefer
 . . . statements that assign parameters to this IPulse1 object . . .
Dmandel
Posts: 13
Joined: Wed Jul 08, 2015 2:32 pm

Re: Repeated Step Pulses In Python

Post by Dmandel »

How do I add the IPulse mechanism in order to use it in Python?

I am able to use the IPulse with my model cell when loading the test1 and test2 hoc files from 'pulsedistr', and then loading my ses file.

However, I cannot successfully load a new ses file into python containing my SingleCompartment and the IPulse mechanism...
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Repeated Step Pulses In Python

Post by ted »

Sounds like you have a lot of basic questions about using NEURON. Suggest you go to the Documentation page at NEURON's web site
http://www.neuron.yale.edu/neuron/docs
and look for
Scripting NEURON with Python
Work through that and see if it takes care of your questions.
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Repeated Step Pulses In Python

Post by ted »

Harking back to my post from July 24--
ted wrote:Organize your program like so:

Code: Select all

from neuron import h, gui
h.load_file("foo.ses") # or whatever you call it
# presumably foo.ses sets up your model cell, and it's a section called soma
stim = h.IPulse1(soma(0.5)) # attach a new IPulse1 to the middle of the soma, or an IPulse2 or 3 if you prefer
 . . . statements that assign parameters to this IPulse1 object . . .
Sorry, I made a mistake here. The statement
stim = h.IPulse1(soma(0.5))
will produce an error message that says
NameError: name 'soma' is not defined

Why?

Because NEURON's GUI tools that create sections, e.g. the CellBuilder and SingleCompartment tool, define their hoc names, not their Python names. For example, a SingleCompartment tool creates a section that is called soma in hoc, but there won't be anything in Python that is called soma. Fortunately, it's very easy for Python to refer to a section that was created in hoc--just attach the prefix h.

So the Python name for the section is h.soma, and the syntax for attaching an instance of the IPulse1 class to the middle of the soma section is
stim = h.IPulse1(h.soma(0.5))
Dmandel
Posts: 13
Joined: Wed Jul 08, 2015 2:32 pm

Re: Repeated Step Pulses In Python

Post by Dmandel »

IPulse1 does not seem to be an instance of the soma section...

Do I need to write my own hoc code in order to incorporate any IPulse?

Here is my Python code.

Code: Select all


soma = h.Section()

In [9]: soma = h.Section(name='soma')

In [10]: h.psection()
soma { nseg=1  L=100  Ra=35.4
	/*location 0 attached to cell 0*/
	/* First segment only */
	insert morphology { diam=500}
	insert capacitance { cm=1}
}
Out[10]: 1.0

In [11]: stim = h.IPulse1(h.soma(0.5))

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-6e763a6da269> in <module>()
----> 1 stim = h.IPulse1(h.soma(0.5))

AttributeError: 'hoc.HocObject' object has no attribute 'IPulse1'
Dmandel
Posts: 13
Joined: Wed Jul 08, 2015 2:32 pm

Re: Repeated Step Pulses In Python

Post by Dmandel »

The real issue I'm having is accessing the IPulse mechanism, whether it is in hoc or Python.
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Repeated Step Pulses In Python

Post by ted »

Did you work through Scripting NEURON with Python?
Dmandel
Posts: 13
Joined: Wed Jul 08, 2015 2:32 pm

Re: Repeated Step Pulses In Python

Post by Dmandel »

Yes, multiple times.
Dmandel
Posts: 13
Joined: Wed Jul 08, 2015 2:32 pm

Re: Repeated Step Pulses In Python

Post by Dmandel »

Okay, after a third time through, I think I can see a way using this type of syntax.

step = 0.075
num_steps = 4
for i in numpy.linspace(step, step*num_steps, num_steps):
stim.amp = i
simulate()
# When i==step, we are at the first time through.
show_output(soma_v_vec, dend_v_vec, t_vec, i==step)
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Repeated Step Pulses In Python

Post by ted »

Dmandel wrote:IPulse1 does not seem to be an instance of the soma section...
True, but completely unrelated to the reason why this statement
stim = h.IPulse1(h.soma(0.5))
failed, and also completely unrelated to the error message
AttributeError: 'hoc.HocObject' object has no attribute 'IPulse1'

The statement
soma = h.Section()
creates a section that is known to Python as soma. Because of these facts (a section exists, and its Python name is soma), it is only necessary to do
stim = h.IPulse1(soma(0.5))

So to review, this would succeed:
soma = h.Section()
stim = h.IPulse1(soma(0.5))

Don't try to get too fancy with the "name" argument to h.Section().
soma = h.Section(name = 'soma')
doesn't mean that hoc now has a token called 'soma' that is synonymous with the Python token 'soma'. To quote a recent discussion with Michael Hines,
The 'name' arg for the section constructor is embarassingly misleading. It only refers to the label printed by GUI tools and topology and psection. That label cannot be used by the hoc interpreter as it is not added to the hoc symbol table.
"Whazzat?"

In the context of the discussion you and I are having, it's why the statement
stim = h.IPulse1(h.soma(0.5))
failed, and produced this error message
AttributeError: 'hoc.HocObject' object has no attribute 'IPulse1'

If hoc recognized the token 'soma' as the name of a section,
stim = h.IPulse1(h.soma(0.5))
would have succeeded. But the statement
soma = h.Section(name = 'soma')
doesn't make hoc add 'soma' to its symbol table, so as far as hoc is concerned there isn't anything (let alone a section) called soma, and that's why
stim = h.IPulse1(h.soma(0.5))
fails. h.IPulse1() expects its argument to refer to a nrn.Segment object (because a point process must be associated with a segment), but h.soma(0.5) doesn't return a useful result. If you execute
soma(0.5)
you'll get a result something like
<nrn.Segment object at 0xb73c9fc8>
but if you execute
h.soma(0.5)
you'll get this error message
AttributeError: 'hoc.HocObject' object has no attribute 'soma'

I'm sure there's a reason why
stim = h.IPulse1(h.soma(0.5))
generates the rather uninformative error message
AttributeError: 'hoc.HocObject' object has no attribute 'IPulse1'
but for the moment I think I need to be excused from class for a while because my brain is full.
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Repeated Step Pulses In Python

Post by ted »

Dmandel wrote:Yes, multiple times.
Good. Then I should point out that the various IPulse mechanisms are point processes, so the syntax for using them is very similar to the syntax for using the AlphaSynapse mechanism that was used in the first example of that tutorial.
Dmandel
Posts: 13
Joined: Wed Jul 08, 2015 2:32 pm

Re: Repeated Step Pulses In Python

Post by Dmandel »

Ted I can't thank you enough for the brain mess I have caused you.

Nevertheless, please see this simple python syntax and corresponding error:

>>> from neuron import h
NEURON -- VERSION 7.3 ansi (1148:433096832316) 2014-08-22
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2014
See http://www.neuron.yale.edu/neuron/credits

>>> soma = h.Section()
>>> stim = h.IPulse1(soma(0.5))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'hoc.HocObject' object has no attribute 'IPulse1'
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Repeated Step Pulses In Python

Post by ted »

Dmandel wrote:'hoc.HocObject' object has no attribute 'IPulse1'
This can happen when NEURON doesn't know what an IPulse1 is. The IPulse1 class is not one of NEURON's built-in mechanisms; it is added to NEURON when NEURON starts up and loads a library that is created by compiling mod files.

When NEURON starts up, it looks for such a library, and if it finds one, it it reports the mechanisms that it loads from that library. For example, I just now put ipulse1.mod into a directory, ran nrnivmodl in that directory (if you're using MSWin or OS X you'd run mknrndll in that directory, or drag and drop the directory onto the mknrndll icon), and then launched NEURON with the command nrniv -python. Here's the message that NEURON printed to the monitor:

Code: Select all

NEURON -- Release 7.4 (1327:d5bf062d0b56) 2015-04-25
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2015
See http://www.neuron.yale.edu/neuron/credits

loading membrane mechanisms from i686/.libs/libnrnmech.so
Additional mechanisms from files
 ipulse1.mod
Dmandel
Posts: 13
Joined: Wed Jul 08, 2015 2:32 pm

Re: Repeated Step Pulses In Python

Post by Dmandel »

Sweet!
Post Reply