-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarking.py
61 lines (53 loc) · 1.77 KB
/
benchmarking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#Benchmarking scripts for CW3
import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'stix'
matplotlib.rcParams['font.family'] = 'STIXGeneral'
import matplotlib.pyplot as plt
import time
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from gillespie import *
def bench(y, params):
if isinstance(params,list):
out = [params[0]]
out.append(list(y*np.asarray(params[1:])))
else:
out = params
return out
scale = np.round(10**np.arange(0,3.3,0.2),0)
tspan = [0, 1000]
y0 = 0
repeats = 4
t_direct = []
t_first = []
for i, s in enumerate(scale):
t_direct.append([])
t_first.append([])
print(s)
for r in range(repeats):
transition_rules = [100]
params = np.random.rand(int(s))
transition_rules += [-1]*(len(params)-1)
tic = time.clock()
gillespie(lambda y: bench(y, params), transition_rules, tspan, y0,method='direct')
toc = time.clock()
t_direct[i].append(toc-tic)
tic = time.clock()
gillespie(lambda y: bench(y, params), transition_rules, tspan, y0,method='first')
toc = time.clock()
t_first[i].append(toc-tic)
avg_first = [np.mean(test) for test in t_first]
avg_direct = [np.mean(test) for test in t_direct]
var_first = [np.var(test) for test in t_first]
var_direct = [np.var(test) for test in t_first]
#%% Plotting
fig, ax = plt.subplots()
ax.errorbar(scale,avg_first, yerr=var_first, label='First Reaction Method')
ax.errorbar(scale,avg_direct, yerr=var_direct, label='Direct Method')
ax.set_xscale('log')
ax.set_yscale('log')
ax.grid()
ax.set_xlabel('Model complexity / number of reactions')
ax.set_ylabel('Execution time / s')
ax.set_title('Computational scaling of Gillespie algorithms\n' + 'Error bars represent variance')
ax.legend()