| 1 |
#!/usr/bin/env python |
| 2 |
''' |
| 3 |
Spike-timing dependent plasticity |
| 4 |
Adapted from Song, Miller and Abbott (2000) and Song and Abbott (2001) |
| 5 |
|
| 6 |
This example is modified from ``synapses_STDP.py`` and writes a standalone C++ project in the directory |
| 7 |
``STDP_standalone``. |
| 8 |
''' |
| 9 |
import sys, time, os |
| 10 |
from brian2 import * |
| 11 |
|
| 12 |
standalone = int(sys.argv[-2]) |
| 13 |
n_threads = int(sys.argv[-1]) |
| 14 |
path = 'data_stdp_%d' %n_threads |
| 15 |
|
| 16 |
if standalone == 1: |
| 17 |
set_device('cpp_standalone') |
| 18 |
brian_prefs.codegen.cpp_standalone.openmp_threads = n_threads |
| 19 |
|
| 20 |
start = time.time() |
| 21 |
N = 1000 |
| 22 |
taum = 10 * ms |
| 23 |
taupre = 20 * ms |
| 24 |
taupost = taupre |
| 25 |
Ee = 0 * mV |
| 26 |
vt = -54 * mV |
| 27 |
vr = -60 * mV |
| 28 |
El = -74 * mV |
| 29 |
taue = 5 * ms |
| 30 |
F = 30 * Hz |
| 31 |
gmax = .01 |
| 32 |
dApre = .01 |
| 33 |
dApost = -dApre * taupre / taupost * 1.05 |
| 34 |
dApost *= gmax |
| 35 |
dApre *= gmax |
| 36 |
|
| 37 |
eqs_neurons = ''' |
| 38 |
dv/dt=(ge*(Ee-vr)+El-v)/taum : volt # the synaptic current is linearized |
| 39 |
dge/dt=-ge/taue : 1 |
| 40 |
''' |
| 41 |
|
| 42 |
poisson_input = PoissonGroup(N, rates=F) |
| 43 |
neurons = NeuronGroup(500, eqs_neurons, threshold='v>vt', reset='v=vr') |
| 44 |
S = Synapses(poisson_input, neurons, |
| 45 |
'''w:1 |
| 46 |
dApre/dt=-Apre/taupre : 1 (event-driven) |
| 47 |
dApost/dt=-Apost/taupost : 1 (event-driven)''', |
| 48 |
on_pre='''ge+=w |
| 49 |
Apre+=dApre |
| 50 |
w=clip(w+Apost,0,gmax)''', |
| 51 |
on_post='''Apost+=dApost |
| 52 |
w=clip(w+Apre,0,gmax)''') |
| 53 |
S.connect() |
| 54 |
S.w = 'rand()*gmax' |
| 55 |
state_mon = StateMonitor(S, 'w', record=[0]) |
| 56 |
spike_mon_1 = SpikeMonitor(poisson_input) |
| 57 |
spike_mon_2 = SpikeMonitor(neurons) |
| 58 |
start_time = time.time() |
| 59 |
|
| 60 |
net = Network(poisson_input, neurons, S, state_mon, spike_mon_1, spike_mon_2, name='stdp_net') |
| 61 |
|
| 62 |
if standalone == 1: |
| 63 |
device.insert_code('main', 'std::clock_t start = std::clock();') |
| 64 |
|
| 65 |
net.run(5 * second, report='text') |
| 66 |
|
| 67 |
if standalone == 1: |
| 68 |
device.insert_code('main', ''' |
| 69 |
std::ofstream myfile ("speed.txt"); |
| 70 |
if (myfile.is_open()) |
| 71 |
{ |
| 72 |
double value = (double) (std::clock() - start)/(%d * CLOCKS_PER_SEC); |
| 73 |
myfile << value << std::endl; |
| 74 |
myfile.close(); |
| 75 |
} |
| 76 |
''' %(max(1, n_threads))) |
| 77 |
|
| 78 |
try: |
| 79 |
os.removedirs(path) |
| 80 |
except Exception: |
| 81 |
pass |
| 82 |
|
| 83 |
if standalone == 1: |
| 84 |
device.build(project_dir=path, compile_project=True, run_project=True, debug=False) |