| 1 |
""" |
| 2 |
This example shows how to run several, independent simulations in standalone mode. |
| 3 |
Given that this example only involves a single neuron, an alternative – and arguably more elegant – solution |
| 4 |
would be to run the simulations in a single `NeuronGroup`, where each neuron receives input with a different rate. |
| 5 |
|
| 6 |
The example is a standalone equivalent of the one presented in :doc:`/tutorials/3-intro-to-brian-simulations`. |
| 7 |
""" |
| 8 |
import numpy as np |
| 9 |
import matplotlib.pyplot as plt |
| 10 |
import brian2 as b2 |
| 11 |
from time import time |
| 12 |
|
| 13 |
b2.set_device('cpp_standalone', build_on_run=False) |
| 14 |
|
| 15 |
if __name__ == "__main__": |
| 16 |
start_time = time() |
| 17 |
num_inputs = 100 |
| 18 |
input_rate = 10 * b2.Hz |
| 19 |
weight = 0.1 |
| 20 |
|
| 21 |
net = b2.Network() |
| 22 |
P = b2.PoissonGroup(num_inputs, rates=input_rate) |
| 23 |
eqs = """ |
| 24 |
dv/dt = -v/tau : 1 |
| 25 |
tau : second (constant) |
| 26 |
""" |
| 27 |
G = b2.NeuronGroup(1, eqs, threshold='v>1', reset='v=0', method='euler') |
| 28 |
S = b2.Synapses(P, G, on_pre='v += weight') |
| 29 |
S.connect() |
| 30 |
M = b2.SpikeMonitor(G) |
| 31 |
net.add([P, G, S, M]) |
| 32 |
|
| 33 |
net.run(1000 * b2.ms) |
| 34 |
|
| 35 |
b2.device.build(run=False) # compile the code, but don't run it yet |
| 36 |
|
| 37 |
npoints = 15 |
| 38 |
tau_range = np.linspace(1, 15, npoints) * b2.ms |
| 39 |
|
| 40 |
output_rates = np.zeros(npoints) |
| 41 |
for ii in range(npoints): |
| 42 |
tau_i = tau_range[ii] |
| 43 |
b2.device.run(run_args={G.tau: tau_i}) |
| 44 |
output_rates[ii] = M.num_spikes / b2.second |
| 45 |
|
| 46 |
print(f"Done in {time() - start_time}") |
| 47 |
|
| 48 |
plt.plot(tau_range/b2.ms, output_rates) |
| 49 |
plt.xlabel(r"$\tau$ (ms)") |
| 50 |
plt.ylabel("Firing rate (sp/s)") |
| 51 |
plt.show() |