@@ -76,6 +76,14 @@ def add_spontaneous(self, source, target, rate):
76
76
"""
77
77
self .transitions .add_edge (source , target , rate = rate )
78
78
79
+ def add_birth_rate (self , rate ):
80
+ for comp in self .transitions .nodes ():
81
+ self .transitions .nodes [comp ]['birth' ]= rate
82
+
83
+ def add_death_rate (self , rate ):
84
+ for comp in self .transitions .nodes ():
85
+ self .transitions .nodes [comp ]['death' ]= rate
86
+
79
87
def add_vaccination (self , source , target , rate , start ):
80
88
"""
81
89
Add a vaccination transition between two compartments
@@ -187,7 +195,19 @@ def _new_cases(self, population, time, pos):
187
195
188
196
diff [pos [source ]] -= rate
189
197
diff [pos [target ]] += rate
190
-
198
+
199
+ # Population dynamics
200
+ for comp , data in self .transitions .nodes (data = True ):
201
+ comp_id = pos [comp ]
202
+
203
+ if "birth" in data :
204
+ births = population [comp_id ]* data ["birth" ]
205
+ diff [comp_id ] += births
206
+
207
+ if "death" in data :
208
+ deaths = population [comp_id ]* data ["death" ]
209
+ diff [comp_id ] -= deaths
210
+
191
211
return diff
192
212
193
213
def plot (self , title = None , normed = True , ** kwargs ):
@@ -277,6 +297,7 @@ def simulate(self, timesteps, t_min=1, seasonality=None, **kwargs):
277
297
N = np .sum (pop )
278
298
279
299
300
+ # Disease dynamics
280
301
for comp in comps :
281
302
trans = list (self .transitions .edges (comp , data = True ))
282
303
@@ -286,6 +307,10 @@ def simulate(self, timesteps, t_min=1, seasonality=None, **kwargs):
286
307
source = pos [comp ]
287
308
target = pos [node_j ]
288
309
310
+
311
+ if pop [source ] == 0 :
312
+ continue
313
+
289
314
rate = data ['rate' ]
290
315
291
316
if 'start' in data and data ['start' ] >= t :
@@ -317,6 +342,18 @@ def simulate(self, timesteps, t_min=1, seasonality=None, **kwargs):
317
342
for i in range (len (delta )):
318
343
new_pop [i ] += delta [i ]
319
344
345
+ # Population dynamics
346
+ for comp , data in self .transitions .nodes (data = True ):
347
+ comp_id = pos [comp ]
348
+
349
+ if "birth" in data :
350
+ births = np .random .binomial (pop [comp_id ], data ["birth" ])
351
+ new_pop [comp_id ] += births
352
+
353
+ if "death" in data :
354
+ deaths = np .random .binomial (pop [comp_id ], data ["death" ])
355
+ new_pop [comp_id ] -= deaths
356
+
320
357
values .append (new_pop )
321
358
322
359
values = np .array (values )
0 commit comments