-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsir.py
More file actions
76 lines (64 loc) · 2.17 KB
/
Copy pathsir.py
File metadata and controls
76 lines (64 loc) · 2.17 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import scipy.integrate as spi
import numpy as np
import matplotlib.pyplot as plt
import json
import os
import pandas as pd
def run(args):
DIR = "Results\\SIR_{}".format(args["Name"])
os.makedirs(DIR,exist_ok=True)
POP = args["Population"]
R0 = args["R0"]
DR = args["Death_Rate"]
GAMMA = args["GAMMA"] = 1/args["Inc_Period"]
BETA = args["BETA"] = GAMMA*R0
TS = 1 #Time step
Ndays = args["Days"] #Number of days
Inf0 = args["Init_Inf"]/args["Population"]
Susp0 = 1-Inf0
Rec0 = 0
Dec0 = 0
INPUT = (Susp0, Inf0, Rec0,Dec0)
# INPUT = {'S':Susp0, 'I':Inf0, 'R':Rec0}
strs = ['S','I','R','D']
def diff_eqs(INP,t):
'''The main set of equations'''
Y = np.zeros((4))
V = dict(zip(strs,INP)) #[Susp,Inf,Rec]
Y[0] = - BETA * V['S'] * V['I']
Y[1] = BETA * V['S'] * V['I'] - GAMMA * V['I']
Y[2] = GAMMA * V['I'] * (1-DR)
Y[3] = GAMMA * V['I'] * DR
return Y # For odeint
t_start = 0.0; t_end = 1.0*Ndays; t_inc = 1.0*TS
t_range = np.arange(t_start, t_end+t_inc, t_inc)
RES = spi.odeint(diff_eqs,INPUT,t_range)
plt.subplot(211)
plt.plot(RES[:,0]*POP, '-g', label='Suspectible')
plt.plot(RES[:,1]*POP, '-m', label='Infectious')
plt.plot(RES[:,2]*POP, '-b', label='Recoveries')
plt.plot(RES[:,3]*POP, '-r', label='Deaths')
plt.legend(loc=0)
plt.title('SIR Model for R0='+str(R0)[:4])
# plt.xticks(np.arange(0,Ndays,TS))/
plt.xlabel('Timestep')
plt.ylabel('Number')
plt.grid()
plt.subplot(212)
plt.plot(RES[:,1]*POP, '-r', label='Infectious')
plt.xlabel('Timestep')
plt.ylabel('Infectious')
# plt.show()
df = pd.DataFrame(np.round(RES*POP),columns=["Suspectible",'Infected','Recovered',"Died"])
df.index.name = "Day"
plt.savefig(DIR+"//graph.png")
json.dump(args,open(DIR+"//params.json", 'w'),indent = 4)
df.to_csv(DIR+"//output.csv")
with open("SIR_parameters.json") as f:
args = json.load(f)
Nos = 9
R0s = np.linspace(1,4,Nos)
for x in range(Nos):
args["Name"] = R0s[x]
args["R0"] = R0s[x]
# run(args)