1
2
3
4
5
6
7
8
9
10
11
12
13 __docformat__ = "restructuredtext en"
14 from numpy.random import uniform, multinomial, exponential,random
15 from numpy import arange, array, empty,zeros,log, isnan, nanmax
16 import time
17 import copy
18 from multiprocessing import Pool
19 try:
20 from BIP.Viz.realtime import RTplot
21 ser = RTplot()
22 except:
23 ser = None
24 try:
25 import psyco
26 psyco.full()
27 except:
28 pass
33
35 - def __init__(self,vnames,rates,inits, tmat,propensity):
36 '''
37 * vnames: list of strings
38 * rates: list of fixed rate parameters
39 * inits: list of initial values of variables
40 * propensity: list of lambda functions of the form:
41 lambda r,ini: some function of rates ans inits.
42 '''
43 self.vn = vnames
44 self.rates = rates
45 self.inits = inits
46 self.tm = tmat
47 self.pv = propensity
48 self.pvl = len(self.pv)
49 self.pv0 = zeros(self.pvl,dtype=float)
50 self.nvars = len(self.inits)
51 self.time = None
52 self.tmax = None
53 self.series = None
54 self.steps = 0
55 self.viz = False
56
57
59 return self.time,self.series,self.steps
60
61 - def run(self, method='SSA', tmax=10, reps=1, viz=False, serial=False):
62 '''
63 Runs the model.
64
65 :Parameters:
66 - `method`: String specifying the solving algorithm. Currently only 'SSA'
67 - `tmax`: duration of the simulation.
68 - `reps`: Number of replicates.
69 - `viz`: Boolean. Whether to show graph of each replicate during simulation
70 - `serial`: Boolean. False to run replicate in parallel when more than one core is a vailable. True to run them serially (easier to debug).
71
72 :Return:
73 a numpy array of shape (reps,tmax,nvars)
74 '''
75 if ser:
76 self.viz = viz
77 self.tmax = tmax
78
79 self.res = zeros((tmax,self.nvars),dtype=float)
80 tvec = arange(tmax, dtype=int)
81
82 if method =='SSA':
83 if not serial:
84 pool = Pool()
85 self.res = array(pool.map(dispatch,[self]*reps, chunksize=10))
86 pool.close()
87 pool.join()
88 else:
89 self.res = array(map(dispatch,[self]*reps))
90
91 elif method == 'SSAct':
92 pass
93
94 self.time = tvec
95 self.series = self.res
96
97
99 '''
100 Gillespie Direct algorithm
101 '''
102 tmax = self.tmax
103 ini = copy.deepcopy(self.inits)
104 r = self.rates
105 pvi = self.pv
106 tm = self.tm
107 pv = self.pv0
108 tc = 0
109 self.steps = 0
110 self.res[0,:]= ini
111 for tim in xrange(1,tmax):
112 while tc < tim:
113 i=0
114 a0=0
115 for p in pvi:
116 pv[i] = p(r,ini)
117 a0+=pv[i]
118 i+=1
119 tau = (-1/a0)*log(random())
120 if pv.any():
121 try:
122 event = multinomial(1,pv/a0)
123 except ValueError:
124
125 print "pv: ",pv
126 print "Rates: ", r
127 print "State: ", ini
128 print "Time Step: ",tim
129 raise ValueError()
130 ini += tm[:,event.nonzero()[0][0]]
131
132 tc += tau
133 self.steps +=1
134 if a0 == 0: break
135 self.res[tim,:] = ini
136 if a0 == 0: break
137 if self.viz:
138 self.ser.clearFig()
139 self.ser.plotlines(self.res.T,names=self.vn)
140 return self.res
141
142
144 -def p2(r,ini): return r[0]*ini[1]
145
147 vnames = ['S','I','R']
148 ini= [500,1,0]
149 rates = [.001,.1]
150 tm = array([[-1,0],[1,-1],[0,1]])
151
152 M = Model(vnames = vnames,rates = rates,inits=ini, tmat=tm,propensity=[p1,p2])
153 t0=time.time()
154 M.run(tmax=80,reps=1000,viz=False,serial=False)
155 print 'total time: ',time.time()-t0
156
157
158
159
160
161
162
163
164 if __name__=="__main__":
165
166
167 main()
168