1
2
3
4
5
6
7
8
9
10
11
12
13 __docformat__ = "restructuredtext en"
14 """
15 Example of an SEIR model with two Infectious classes: subclinical(Is) and clinical(Ic)
16 Is
17 / \
18 S -> E R
19 \ /
20 Ic
21
22 States:
23 S: Susceptible
24 E: Exposed
25 Is: Infectious subclinical
26 Ic: Infectious clinical
27 R: Recovered
28
29 Transition rates:
30 b,ks,kc,rs,rc = (0.001, 0.1, 0.1, 0.01, .01)
31 Transitions:
32 S -> E : b*S*(Is+Ic)
33 E -> Is : ks*E
34 E -> Ic : kc*E
35 Is -> R : rs*Is
36 Ic -> R : rc*Ic
37
38 """
39 from cgillespie import Model as CModel
40 from gillespie import Model
41 import time
42
43 from numpy import array
44 vnames = ['S','E','Is','Ic','R']
45
46
47 r = (0.001, 0.1, 0.1, 0.01, .01)
48 ini = array((490,0,0,10,0))
49
51 -def f2(r,ini):return r[1]*ini[1]
52 -def f3(r,ini):return r[2]*ini[1]
53 -def f4(r,ini):return r[3]*ini[2]
54 -def f5(r,ini):return r[4]*ini[3]
55
56 propf = (f1,f2,f3,f4,f5)
57
58 tmat = array([[-1, 0, 0, 0, 0],
59 [ 1,-1,-1, 0, 0],
60 [ 0, 1, 0,-1, 0],
61 [ 0, 0, 1, 0,-1],
62 [ 0, 0, 0, 1, 1]])
63 M=Model(vnames=vnames,rates = r,inits=ini,tmat=tmat,propensity=propf)
64 CM = CModel(vnames=vnames,rates = r,inits=ini,tmat=tmat,propensity=propf)
65
66
67 t0 = time.time()
68 M.run(tmax=80,reps=100,viz=0,serial=0)
69 pt = time.time()-t0
70 print 'Python total time: ',pt, ' seconds.'
71 t,series,steps = M.getStats()
72 print steps,'steps'
73
74 t0 = time.time()
75 CM.run(tmax=80,reps=100)
76 ct = time.time()-t0
77 print 'Cython total time: ',ct, ' seconds.'
78 t2,series2,steps2 = CM.getStats()
79 print steps2,' steps'
80 print "Cython speedup: %sx"%(pt/ct)
81 from pylab import plot , show, legend, errorbar, title, figure
82
83 plot(t,series.mean(axis=0),'-o')
84 title('python curve')
85 legend(vnames,loc=0)
86 figure()
87 plot(t2,series2.mean(axis=2),'-o')
88 title('cython curve')
89 legend(vnames,loc=0)
90 show()
91