I would like to create a simple function that given a population of neurons, it creates the neuron objects to records its spikes. However, when I put the code in a functions it does not record the spikes but if I run the same code in the main script it actually records the spikes. What is the best way to build such a function? How can let Neuron know of these objects defined inside a function?
Thank you very much in advance.
Code: Select all
# Create a list to hold the Poisson neurons
poisson_neurons = []
# Define the parameters
num_neurons = 50 # Number of Poisson neurons
rate_poisson = 10 # Firing rate (in Hz)
simulation_duration = 1000 # Simulation duration (in ms)
def create_spike_recorder(neurons):
num_neurons = len(neurons)
global spike_times, spike_detector
spike_times = [h.Vector() for i in range(num_neurons)]
spike_detector = [h.NetCon(neurons[i], None) for i in range(num_neurons)]
for i in range(num_neurons): spike_detector[i].record(spike_times[i])
return spike_detector, spike_times
poisson_neurons = []
for i in range(num_neurons):
syn = h.NetStim()
syn.interval = 1000.0 / rate_poisson # Inter-spike interval in ms
syn.noise = 1
poisson_neurons.append(syn)
spike_detector_poisson, spike_times_poisson = create_spike_recorder(poisson_neurons)
# Create a spike recorder
spike_times = [h.Vector() for i in range(num_neurons)]
spike_detector = [h.NetCon(poisson_neurons[i], None) for i in range(num_neurons)]
for i in range(num_neurons): spike_detector[i].record(spike_times[i])
# Create a run control for the simulation
h.stdinit()
h.tstop = simulation_duration
h.run()