Debian Codegraph @

dev/benchmarks/compare_to_brian1.py

raw
1 import time
2 from processify import processify
3
4 def halfway_timer():
5 global halftime
6 halftime = time.time()
7
8 @processify
9 def brian1_CUBA(N, duration=1,
10 do_refractory=False, exact_method=False, do_monitor=False, do_synapses=False,
11 **ignored_opts):
12 global halftime
13 import time
14 results = dict()
15 start_time = time.time()
16 from brian import *
17 finished_importing_brian_time = time.time()
18 results['import_brian'] = finished_importing_brian_time-start_time
19
20 duration *= second
21 Ne = int(0.8*N)
22 Ni = N-Ne
23 taum = 20 * ms
24 taue = 5 * ms
25 taui = 10 * ms
26 Vt = -50 * mV
27 Vr = -60 * mV
28 El = -49 * mV
29
30 eqs = '''
31 dv/dt = (ge+gi-(v-El))/taum : volt
32 dge/dt = -ge/taue : volt
33 dgi/dt = -gi/taui : volt
34 '''
35
36 kwds = {}
37 if do_refractory:
38 kwds['refractory'] = 5*ms
39 if not exact_method:
40 kwds['method'] = 'Euler'
41 P = NeuronGroup(N, model=eqs, threshold=Vt, reset=Vr, **kwds)
42 P.v = Vr
43 P.ge = 0 * mV
44 P.gi = 0 * mV
45
46 Pe = P.subgroup(Ne)
47 Pi = P.subgroup(Ni)
48 we = (60 * 0.27 / 10) * mV # excitatory synaptic weight (voltage)
49 wi = (-20 * 4.5 / 10) * mV # inhibitory synaptic weight
50 p = min(80./N, 1)
51 if do_synapses:
52 Ce = Connection(Pe, P, 'ge', weight=we, sparseness=p)
53 Ci = Connection(Pi, P, 'gi', weight=wi, sparseness=p)
54 P.v = Vr + rand(len(P)) * (Vt - Vr)
55
56 if do_monitor:
57 M = SpikeMonitor(P)
58
59 netop_halfway_timer = network_operation(clock=EventClock(dt=duration*0.51))(halfway_timer)
60
61 objects_created_time = time.time()
62 results['object_creation'] = objects_created_time-finished_importing_brian_time
63
64 run(1 * msecond)
65
66 initial_run_time = time.time()
67 results['initial_run'] = initial_run_time-objects_created_time
68
69
70 run(duration)
71
72 main_run_time = time.time()
73 results['main_run'] = main_run_time-initial_run_time
74 results['second_half_main_run'] = main_run_time-halftime
75 results['total'] = main_run_time-start_time
76
77 return results
78
79 @processify
80 def brian2_CUBA(N, duration=1,
81 do_refractory=False, exact_method=False, do_monitor=False, do_synapses=False,
82 codegen_target='numpy',
83 **ignored_opts):
84 global halftime
85 import time
86 results = dict()
87 start_time = time.time()
88 from brian2 import *
89 prefs.codegen.target = codegen_target
90 finished_importing_brian_time = time.time()
91 results['import_brian'] = finished_importing_brian_time-start_time
92
93 duration *= second
94 Ne = int(0.8*N)
95 Ni = N-Ne
96 taum = 20 * ms
97 taue = 5 * ms
98 taui = 10 * ms
99 Vt = -50 * mV
100 Vr = -60 * mV
101 El = -49 * mV
102
103 eqs = '''
104 dv/dt = (ge+gi-(v-El))/taum : volt (unless refractory)
105 dge/dt = -ge/taue : volt
106 dgi/dt = -gi/taui : volt
107 '''
108 if not do_refractory:
109 eqs = eqs.replace('(unless refractory)', '')
110
111 kwds = {}
112 if do_refractory:
113 kwds['refractory'] = 5*ms
114 if not exact_method:
115 kwds['method'] = 'euler'
116 P = NeuronGroup(N, model=eqs, threshold='v>Vt', reset='v=Vr', **kwds)
117 P.v = Vr
118 P.ge = 0 * mV
119 P.gi = 0 * mV
120
121 we = (60 * 0.27 / 10) * mV # excitatory synaptic weight (voltage)
122 wi = (-20 * 4.5 / 10) * mV # inhibitory synaptic weight
123 p = min(80./N, 1.0)
124 if do_synapses:
125 Ce = Synapses(P, P, on_pre='ge += we')
126 Ci = Synapses(P, P, on_pre='gi += wi')
127 Ce.connect('i<Ne', p=p)
128 Ci.connect('i>=Ne', p=p)
129 P.v = Vr + rand(len(P)) * (Vt - Vr)
130
131 if do_monitor:
132 M = SpikeMonitor(P)
133
134 netop_halfway_timer = NetworkOperation(halfway_timer, dt=duration*0.51)
135
136 objects_created_time = time.time()
137 results['object_creation'] = objects_created_time-finished_importing_brian_time
138
139 run(1 * msecond)
140
141 initial_run_time = time.time()
142 results['initial_run'] = initial_run_time-objects_created_time
143
144 run(duration)
145
146 main_run_time = time.time()
147 results['main_run'] = main_run_time-initial_run_time
148 results['second_half_main_run'] = main_run_time-halftime
149 results['total'] = main_run_time-start_time
150
151 return results
152
153 def brian2_CUBA_cython(*args, **opts):
154 opts['codegen_target'] = 'cython'
155 return brian2_CUBA(*args, **opts)
156
157 if __name__=='__main__':
158 import functools
159 from pylab import *
160 numfigs = 0
161
162 funcs = [
163 brian1_CUBA,
164 brian2_CUBA,
165 brian2_CUBA_cython,
166 ]
167 options = dict(duration=1,
168 do_monitor=False,
169 do_refractory=False,
170 do_synapses=False,
171 exact_method=False,
172 )
173 N = [1, 10, 100, 1000,
174 10000,
175 #100000,
176 ]
177
178 for name in options.keys()+['']:
179 #for name in ['']:
180 if name:
181 if not isinstance(options[name], bool):
182 continue
183 options[name] = True
184 figure(figsize=(16, 8))
185 for func in funcs:
186 pfunc = functools.partial(func, **options)
187 all_results = map(pfunc, N)
188 times = collections.defaultdict(list)
189 for res in all_results:
190 for k, v in res.items():
191 times[k].append(v)
192 for i, k in enumerate(sorted(times.keys())):
193 subplot(2, 3, i+1)
194 if i>numfigs:
195 numfigs = i
196 title(k)
197 v = times[k]
198 loglog(N[1:], v[1:], label=func.__name__)
199 for i in range(numfigs+1):
200 subplot(2, 3, i+1)
201 legend(loc='best')
202 suptitle(', '.join('%s=%s' % (k, options[k]) for k in sorted(options.keys())))
203 tight_layout()
204 subplots_adjust(top=0.9)
205 if name:
206 savefig('compare_to_brian1.%s.png'%name)
207 options[name] = False
208 else:
209 savefig('compare_to_brian1.png')
210 show()