Python Section syntax error

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

Moderator: hines

Post Reply
dragoneurotic
Posts: 5
Joined: Tue Nov 18, 2014 7:38 pm

Python Section syntax error

Post by dragoneurotic »

Hi All,

I am working with NEURON via python and I import a morphology file via an .asc file. All good so far.

My neurons has a soma, an axon and dend and apic sections amounting to 150 sections all together.

I use python to create some new sections that I call spines and connect them to dend and apical in a manner as follows. All is also good there. Part of this code is appended below

.....code is part of a class

def add_spines_AND_AMPA_NMDA_synapses (self, spineDensity, syn_start, syn_interval, syn_noise, syn_number ):

# # initialise and clear the spines so it is easier to run multiple times without accumulation
del spinesList[:]
del synList[:]
del netConList[:]
del synStimList[:]

# add the model parameters
model_parameters = cortical_cell.load_model_parameters()

# initialise distance to soma calculation
disFromSoma, _ = cortical_cell.distance_from_soma(secList)

# Add AMPA receptors to each spine, add NetCon Object to control the spines of a given segment
for idx, sec in enumerate(secList):

num_spines = int(spineDensity*sec.L)

# check the distance to soma. If >30 mic insert spines in section otherwise dont!
if disFromSoma[idx+1]>model_parameters['noSpinesDist']: # exclude the first idx in disToSoma because it is the soma itself

for i in range(num_spines): # Create spines in section
factor = 1/num_spines # for positioning the spines in a valid numerical segment of the section

spine = h.Section(name=f'{sec}_spine[{i}]')
spine.L = model_parameters['spine_neck_L']
spine.diam = model_parameters['spine_neck_diam']
spine.connect(sec, (i + 1) * factor) # Attach spines at different points on dendrite

# initialize a new separate synStim for each spine of the neuron
# (NB: all spines of the section will receive the same stimulus characteristics
# and the AMPA and NMDA receptors will be simultaneously activated)

synStim = h.NetStim( sec( (i + 1) * factor) )
synStim.start = syn_start # ms delay to start synaptic input
synStim.interval = syn_interval #ms inmter activation interval of synaptic input
synStim.noise = syn_noise # regularity(randomness of synaptic input)
synStim.number = syn_number # number of activations of synaptic input
synStimList.append(synStim)

hocL.append(h.SectionRef()) # Create a reference to the current section (spine) and add to hocL

# insert AMPA receptor mechanism on the spine head at position 1
ampa_receptor = h.Exp2Syn(model_parameters['spine_head_X'], sec=spine)
synList.append( ampa_receptor )

# note that every spine has a SEPARATE nETcON OBJECT FOR AMPA and a separate for NMDA receptor activation
netConObj = h.NetCon(synStimList[-1], synList[-1])
netConList.append( netConObj )

# Set AMPA receptor properties
synList[-1].e = model_parameters['rev_syn_AMPA']
synList[-1].tau1 = model_parameters['tau_1_AMPA']
synList[-1].tau2 = model_parameters['tau_2_AMPA']
netConList[-1].weight[0] = model_parameters['AMPA_W']
netConList[-1].delay = 0

code continues……………………

The problem that I experience is the following
I can plot the voltage in time (via matplotlib in python) of any section including the spine sections that I have created in python. However, when I use the GUI NEURON to launch a variable to graph, although the section are available under Show -->Python Sections, when I attempt to plot them (e.g., plot the voltage at any given pysec chosen) I get this error (pointing to the e in spine)

NEURON: syntax error
near line 1
{hoc_pointer_(&_pysec.apic[0]_spine[0].v( 0.5 ))}
^
doNotify()
Exception in gui thread

Why is this ?

Also, in the launch variable to graph, under Sections, it peculiarly does NOT list all the sections (meaning all the apical and den sections) in the morphology of the neuron despite the fact that one can inspect all the loaded sections in the e.g. density mechanisms.


Would you please give me some feedback on this issues.

Thank you kindly in advance

Antonios
ramcdougal
Posts: 270
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Python Section syntax error

Post by ramcdougal »

The main problem here is that neither hoc nor Python allows variable names with [] inside them.

You have two sets of []. If you simply delete those two characters from your section name, everything should work.
dragoneurotic
Posts: 5
Joined: Tue Nov 18, 2014 7:38 pm

Re: Python Section syntax error

Post by dragoneurotic »

thank you for the reply, this is very insightful and resolves the issue with the syntax error

Just for the records for all other users too:

old code :
spine_neck = h.Section(name= f''{sec}_spine_neck{i}')
used to give a syntax error

1. If I use the following format (i.e. remove the {sec} in the of the spine) the issue is completely resolved.
The spine sections appear under the python sections list in NEURON's Graph GUI and can be plotted during a simulation in NEURON

spine_neck = h.Section(name= f''spine_neck{i}')

2. However, interestingly if I choose to exclude the number of the spine section (i.e. remove {i}) as below
spine_neck = h.Section(name= f''{sec}_spine_neck'')

I then do not see any python sections to plot in Graph AT ALL.

Thank you for your help
hines
Site Admin
Posts: 1710
Joined: Wed May 18, 2005 3:32 pm

Re: Python Section syntax error

Post by hines »

Code: Select all

spine_neck = h.Section(name= f''{sec}_spine_neck'')
Just curious. What is the value of the string

Code: Select all

f''{sec}_spine_neck''
By the way, I didn't try to run your code fragment in your initiating message as it seemed incomplete (what is secList) and improperly indented.
dragoneurotic
Posts: 5
Joined: Tue Nov 18, 2014 7:38 pm

Re: Python Section syntax error

Post by dragoneurotic »

Hi Ted,

the whole code has been deposited in my GitHub, you can run the model if you like locally

https://github.com/antoniosdougalis/Hum ... al_Neurons

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

Re: Python Section syntax error

Post by hines »

I see that

Code: Select all

>>> for sec in secList: print(f"{sec}_spine_neck")
... 
soma[0]_spine_neck
dend[0]_spine_neck
dend[1]_spine_neck
dend[2]_spine_neck
...
So you can see that those are all invalid variable names. You need a bit more text processing to get the array index at the end of the name or eliminate the brackets.
Post Reply