Simple function to add synapses

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

Moderator: hines

Post Reply
ahwillia
Posts: 17
Joined: Wed Apr 23, 2014 2:17 pm

Simple function to add synapses

Post by ahwillia »

This is a very basic question. I would like to implement a python function that adds an Exp2Syn at a specified location, which triggers a series of post-synaptic events at specified times. I want to do something like:

Code: Select all

from neuron import h

def add_synapse(section,loc,weight,times):
    " " "
    section = h.Section() object
    loc = float between 0 and 1
    weight = synaptic weight
    times = python list of pre-synaptic spike times
    " " "
    syn = h.Exp2Syn(section(loc))
    # specify parameters of synapse here
    nc = h.NetCon(syn)
    for tt in times:
        nc.event(tt)
    return syn, nc
This doesn't work because NetCon requires that I specify a source and target. But in this example, I really don't care about my "source". Should I use a NetStim object as the source?

What is the easiest way to do this?
regger
Posts: 21
Joined: Sun Apr 22, 2012 9:49 pm

Re: Simple function to add synapses

Post by regger »

If you want to deliver events to a NetCon at specified times, the correct way to do this is using a VecStim as the source.
You can find a good explanation by ted about how to use it here.

(N.B.: A NetStim delivers a spike train at regular intervals or stochastically with a mean rate, but is not really meant to be used when you have pre-determined spike times as in your case).

hope this helps,
best
Robert
ahwillia
Posts: 17
Joined: Wed Apr 23, 2014 2:17 pm

Re: Simple function to add synapses

Post by ahwillia »

Thanks for the reply. I think I'm moving in the right direction. However, the code below segfaults and I'm not sure why (it may be completely unrelated to the synapses, I will investigate further).

Code: Select all

from neuron import h
import numpy as np
import pylab as plt

# Load morphology and other stuff
h.load_file('stdrun.hoc')
h.xopen('ri06.hoc')
h.xopen('fixnseg.hoc')
h.xopen('5a_nogui.hoc')
h.tstop = 700.0

# Function to get a random segment
def randseg():
	tmp = np.random.uniform(0,1)
	comp = h.apic[int(np.random.uniform(0,41))] # random section along apical dendrite
	return comp(tmp)

# Add 50 random synapses, each of which fires at 250 and 350 ms
syn, nc, vs = [], [], []
for i in range(50): 
	# New synapse
	syn.append(h.Exp2Syn(randseg()))
	syn[-1].e = 0
	syn[-1].tau1 = 0.5
	syn[-1].tau2 = 20

	# New VecStim object
	vs.append(h.VecStim())
	vec = h.Vector([350,450])
	vs[-1].play(vec)
	
	# New NetCon Object
	nc.append(h.NetCon(vs[-1],syn[i]))
	nc[-1].weight[0] = 0.01

h.run()
ahwillia
Posts: 17
Joined: Wed Apr 23, 2014 2:17 pm

Re: Simple function to add synapses

Post by ahwillia »

So I updated the code to keep the h.Vector() objects in memory. Another post on the forum seemed to suggest that this was important.

However, I'm still getting sporadic segfaults. The code DOES work maybe 50% of the time. When I comment out the section for adding synapses, I get no segfaults.

Code: Select all

from neuron import h
import numpy as np
import pylab as plt

# Load morphology and other stuff
h.load_file('stdrun.hoc')
h.xopen('ri06.hoc')
h.xopen('fixnseg.hoc')
h.xopen('5a_nogui.hoc')
h.tstop = 100.0

# Function to get a random segment
def randseg():
	tmp = np.random.uniform(0,1)
	comp = h.apic[int(np.random.uniform(0,41))]
	return comp(tmp)

# Add 50 random synapses
syn, nc, vs, vec = [], [], [], []
for i in range(50): 
	# New synapse
	syn.append(h.Exp2Syn(randseg()))
	syn[-1].e = 0
	syn[-1].tau1 = 0.5
	syn[-1].tau2 = 20

	# New VecStim object
	vs.append(h.VecStim())
	vec.append(h.Vector([np.random.uniform(20,80)]))
	vs[-1].play(vec[-1])
	
	# New NetCon Object
	nc.append(h.NetCon(vs[-1],syn[-1]))
	nc[-1].weight[0] = 0.001

	# Print stim time
	print vec[-1][0]

t, v = h.Vector(), h.Vector()
v.record(h.soma[0](0.5)._ref_v)
t.record(h._ref_t)
h.run()

plt.figure
plt.plot(t,v)
plt.show()
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Simple function to add synapses

Post by ted »

A journey of 1e3 miles begins with 1e0 step. Suggestions: Get your code to work reliably with 1 synapse. Then make it work reliably with 2, then 3. Step through program execution, or simply insert comments that report the progression of execution. Verify that each model contains exactly what you expect it to.
ahwillia
Posts: 17
Joined: Wed Apr 23, 2014 2:17 pm

Re: Simple function to add synapses

Post by ahwillia »

I finally figured it out. There is nothing wrong with the code, but NEURON does not clean up after itself, causing the code to crash the second time its run. Before I was doing this:

Code: Select all

$ ipython

In [1]: %run model.py # this works fine the first time
In [2]: %run model.py # the second run usually segfaults
But when I run the model directly from the terminal it works fine every time:

Code: Select all

$ ipython model.py
$ ipython model.py 
...
This threw me for a loop. Is there a way this can be fixed?

EDIT: Also found and solved another problem. The vector of spike times sent to vecstim.play() must be in order or the simulation crashes.
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Simple function to add synapses

Post by hines »

Without knowing precisely what went wrong, I can say that it is generally necessary to clean up after discarding an old model before creating a new one. The
destruction order is generally: gids, NetCons, and cells. Get rid of gids using
http://neuron.yale.edu/neuron/static/ne ... .gid_clear
If you continue to have trouble with this, send me a zip file with all the needed *.py, *.hoc, *.mod, etc files along with instructions as to how to reproduce the problem to
michael dot hines at yale dot edu
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Simple function to add synapses

Post by ted »

ahwillia wrote:

Code: Select all

$ ipython
In [1]: %run model.py # this works fine the first time
In [2]: %run model.py # the second run usually segfaults
Is there a reason why you must execute all of the code in model.py on each run? Unless you're doing something that changes the basic structure of the model, or there is some reason why you absolutely rebuild the model from scratch, why not simply execute a new simulation? Easily done if you put

Code: Select all

h.run()

plt.figure
plt.plot(t,v)
plt.show()
in a procedure, and call that procedure as many times as you like. Or isn't that possible from inside iPython?
ahwillia
Posts: 17
Joined: Wed Apr 23, 2014 2:17 pm

Re: Simple function to add synapses

Post by ahwillia »

Yes, I don't need to run the code multiple times within the same iPython session. I just happened to be doing that when I was testing things out... So I have a satisfactory solution to the problem.
Post Reply