Accessing extracellular mechanism variables from Python

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

Moderator: hines

Post Reply
ksarma

Accessing extracellular mechanism variables from Python

Post by ksarma »

In a simulation I am working on with Python, I need to use the extracellular mechanism. However, it appears that nrnpython currently only allows accessing variables in mechanisms which are named in the expected manner (varname_mechprefix). However, many of the variables in the extracellular mechanism do not follow this convention.

For the purposes of my simulation, I worked around this by adding some code in mech_getattro which checks to see if the current mechanism is extracellular, and if so to ignore the naming convention. This seems like a suboptimal way of dealing with this problem, though (also, it is still not possible to subscript vext to find the value of, say, vext[1], etc).

Code: Select all

static PyObject* mech_getattro(NPyMechObj* self, PyObject* name) {
        Py_INCREF(name);
        char* n = PyString_AsString(name);
printf("mech_getattro %s\n", n);
        PyObject* result = 0;
        NrnProperty np(self->prop_);
        char buf[200];
        int isptr = (strncmp(n, "_ref_", 5) == 0);
        if (strcmp(memb_func[self->prop_->type].sym->name, "extracellular") == 0) {
          sprintf(buf, "%s", isptr?n+5:n);
        }
        else {
          sprintf(buf, "%s_%s", isptr?n+5:n, memb_func[self->prop_->type].sym->name);
        }
        Symbol* sym = np.find(buf);
	if (sym) {
printf("mech_getattro sym %s\n", sym->name);
                double* px = np.prop_pval(sym, 0);
                if (isptr) {
                        result = nrn_hocobj_ptr(px);
                }else{
                        result = Py_BuildValue("d", *px);
                }
        }else{
                result = PyObject_GenericGetAttr((PyObject*)self, name);
        }
        Py_DECREF(name);
        return result;
}
willy
Posts: 7
Joined: Sat Feb 28, 2009 5:22 pm

Re: Accessing extracellular mechanism variables from Python

Post by willy »

*PUSH*

Is there a better way now or is it still impossible to use extracellular from Python?
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Accessing extracellular mechanism variables from Python

Post by hines »

The problems of setting and evaluating extracellular variables have been fixed.
http://www.neuron.yale.edu/hg/neuron/nr ... f95519f23e
Note that python requires explicit index syntax for vext, xg, xraxial,and xc
to specify which extracellular layer (0, 1) is referenced.
willy
Posts: 7
Joined: Sat Feb 28, 2009 5:22 pm

Re: Accessing extracellular mechanism variables from Python

Post by willy »

It works! Thank you very much!
smadug2
Posts: 4
Joined: Fri Mar 07, 2014 12:20 pm

Re: Accessing extracellular mechanism variables from Python

Post by smadug2 »

Sorry to bring up old issues, but I cannot seem to access the variables mentioned here (vext, xraxial, xc, xg etc.), though in Python the following code returns: "nrn.Mechanism" object has no attribute 'vext'"

Code: Select all

x=neuron.h.Section()
x.L=20
x.diam=20
x.nseg=1

x.insert('extracellular')
for seg in x: 
	seg.extracellular.xc[0]=0

#or even
for seg in x:
        seg.extracellular.xc=0
Can anyone help me with this??
smadug2
Posts: 4
Joined: Fri Mar 07, 2014 12:20 pm

Re: Accessing extracellular mechanism variables from Python

Post by smadug2 »

Sorry I should clarify, for this particular code the error is: 'nrn.Mechanism' object has no attribute 'xc', but replacing xc with vext returns the error previously mentioned
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Accessing extracellular mechanism variables from Python

Post by ted »

extracellular is not an ion channel mechanism. When you "insert extracellular" you tell NEURON that the cable equation for the affected section(s) has three "layers." The parameters that belong to the two additional layers are properties of the section and its segments, not properties of some fictive "extracellular" channel. As such, you access them exactly as you would access properties of an ordinary section like diam, L, Ra, and cm, that is, their names are seg.xc[0], seg.xraxial]1] etc..
smadug2
Posts: 4
Joined: Fri Mar 07, 2014 12:20 pm

Re: Accessing extracellular mechanism variables from Python

Post by smadug2 »

That works, thank you!
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Accessing extracellular mechanism variables from Python

Post by ted »

Glad to help. There are many things that "make sense" but aren't immediately obvious; this is one of them, and your question prompted me to clarify this particular issue.
smadug2
Posts: 4
Joined: Fri Mar 07, 2014 12:20 pm

Re: Accessing extracellular mechanism variables from Python

Post by smadug2 »

Hi Dr. Carnevale,

Thanks again for all your help. I am currently working with the extracellular mechanism to simulate extracellular stimulation, and am confused about the following: I calculated that in the extracellular_stim_and_rec example posted by you that an extracellular voltage in the range of -140 mV at the cell surface is sufficient to elicit a spike in a neuron with the HH mechanism inserted, at an electrode distance of 10 micron and with the default rho value. When I try to recreate a very simple version of this in Python, I find that I have to raise the extracellular voltage to an absolute value of 1.4e15 mV in order to elicit a small spike. Here is the code:

Code: Select all

#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import neuron
from neuron import h

#initialize
dt=0.001
soma=neuron.h.Section(name='soma')
soma.L=25
soma.diam=25
soma.nseg=1
soma.insert('pas')
soma.insert('hh')
soma.insert('extracellular')
soma.xc[0]=0
soma.xg[0]=1e9
soma.xraxial[0]=1e9

#extracellular vector
amp=-140
t_ext = neuron.h.Vector(np.arange(50/dt)*dt)
v_ext=neuron.h.Vector(np.concatenate((np.zeros(1/dt),np.ones(1/dt)*amp,np.zeros(48/dt))))
for seg in soma:
	v_ext.play(seg._ref_e_extracellular,t_ext)

#stimluate and record
rec_v=neuron.h.Vector()
rec_v.record(soma(.5)._ref_v)
rec_t=neuron.h.Vector()
rec_t.record(neuron.h._ref_t)

neuron.h.finitialize(-65) 
neuron.init()
neuron.run(50)

time=np.array(rec_t)
voltage=np.array(rec_v)
plt.plot(time,voltage)
plt.show()
Am I misunderstanding some crucial aspect of the extracellular mechanism?

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

Re: Accessing extracellular mechanism variables from Python

Post by ted »

For an extracellular stimulus to affect a cell, it is necessary that the extracellular field produces a transmembrane current. But if there is some location at which current enters the cell, there must be some other location where it exits the cell. This cannot happen if nseg is 1. To respond to an extracellular field, the model cell must have more than one segment (compartment), and the extracellular potential for at least one of those compartments must be different from the extracellular potentials of the others.
I find that I have to raise the extracellular voltage to an absolute value of 1.4e15 mV in order to elicit a small spike.
So a million megavolt "stimulus" makes a small glitch appear in a simulation. This is an example of GIGO: feed a computer a meaningless value (not to mention physically unrealistic), get a meaningless result.
aschneid42
Posts: 2
Joined: Fri Feb 09, 2018 5:11 pm

Re: Accessing extracellular mechanism variables from Python

Post by aschneid42 »

hines wrote: Tue May 12, 2009 2:38 pm The problems of setting and evaluating extracellular variables have been fixed.
http://www.neuron.yale.edu/hg/neuron/nr ... f95519f23e
Note that python requires explicit index syntax for vext, xg, xraxial,and xc
to specify which extracellular layer (0, 1) is referenced.
This link doesn't seem to work. Is there a new one?
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Accessing extracellular mechanism variables from Python

Post by ted »

aschneid42 wrote: Thu Feb 15, 2018 4:13 pm
hines wrote: Tue May 12, 2009 2:38 pm The problems of setting and evaluating extracellular variables have been fixed.
http://www.neuron.yale.edu/hg/neuron/nr ... f95519f23e
Note that python requires explicit index syntax for vext, xg, xraxial,and xc
to specify which extracellular layer (0, 1) is referenced.
This link doesn't seem to work. Is there a new one?
The link pointed to a particular revision of source code that was created almost 9 years ago, merely as a convenience for users at that time who wanted to download the code. That particular revision has been superceded by many subsequent revisions since then and should be regarded as obsolete. Whatever fix was applied to the source code and related documentation ~9 years ago has persisted in subsequent versions, assuming that it itself has not been superceded by further revision(s).
Post Reply