Jiranuwat commited on
Commit
2528937
·
1 Parent(s): a14f051

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -11
app.py CHANGED
@@ -6,8 +6,6 @@ import matplotlib.pyplot as plt
6
  import warnings
7
  warnings.filterwarnings("ignore")
8
 
9
- #dowload file
10
-
11
  #read files
12
  data = pd.read_csv('owid-monkeypox-data.csv')
13
  data = data[['location','iso_code','date','new_cases','total_cases','new_deaths','total_deaths']]
@@ -77,49 +75,87 @@ def plotdata(t, s, i,r,R0, e=None):
77
  plt.tight_layout()
78
 
79
  return fig
80
-
81
-
82
  #final model
83
- def SIR(country,t_infective):
 
 
84
  # parameter values
 
85
  t_infective = t_infective
 
 
 
 
 
 
86
  gamma = 1/t_infective
87
- beta = (all_location[country]['new_cases'].sum()/pop_dict[country])/len(all_location[country]['date'].unique())
88
- R0 = beta/gamma
89
 
90
  # initial number of infected and recovered individuals
91
  i_initial = all_location[country]['new_cases'].sum()/pop_dict[country]
92
  r_initial = 0.00
93
  s_initial = 1 - i_initial - r_initial
94
 
95
- t = np.linspace(0, 1000, 1000)
96
  x_initial = s_initial, i_initial, r_initial
97
  soln = odeint(deriv, x_initial, t, args=(beta, gamma))
98
  s, i, r = soln.T
99
  e = None
 
 
 
 
100
 
101
 
102
- return R0,t_infective,beta,gamma,plotdata(t, s, i,r,R0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  def main():
105
  st.title("SIR Model for Monkeypox")
106
 
107
  with st.form("questionaire"):
108
  country = st.selectbox("Country",data['location'].unique())# user's input
109
- recovery = st.slider("How long Monkeypox recover?", 21, 31, 21)# user's input
 
110
  country_code = code[country][0]
 
 
111
  # clicked==True only when the button is clicked
112
  clicked = st.form_submit_button("Show Graph")
113
  if clicked:
114
 
115
  # Show SIR
116
- SIR_param = SIR(country_code,recovery)
117
 
118
  st.pyplot(SIR_param[-1])
 
119
  st.success("SIR model parameters for "+str(country)+" is")
120
  st.success("R0 = "+str(SIR_param[0]))
121
  st.success("Beta = "+str(SIR_param[2]))
122
  st.success("Gamma = "+str(SIR_param[3]))
 
123
 
124
  # Run main()
125
  if __name__ == "__main__":
 
6
  import warnings
7
  warnings.filterwarnings("ignore")
8
 
 
 
9
  #read files
10
  data = pd.read_csv('owid-monkeypox-data.csv')
11
  data = data[['location','iso_code','date','new_cases','total_cases','new_deaths','total_deaths']]
 
75
  plt.tight_layout()
76
 
77
  return fig
78
+
 
79
  #final model
80
+ def SIR(country,R0,t_infective):
81
+ #R0 = 0.57 - 1.25
82
+
83
  # parameter values
84
+ R0 = R0
85
  t_infective = t_infective
86
+
87
+ # initial number of infected and recovered individuals
88
+ i_initial = all_location[country]['total_cases'].iloc[0]/pop_dict[country]
89
+ r_initial = 0.00
90
+ s_initial = 1 - i_initial - r_initial
91
+
92
  gamma = 1/t_infective
93
+ beta = R0*gamma
 
94
 
95
  # initial number of infected and recovered individuals
96
  i_initial = all_location[country]['new_cases'].sum()/pop_dict[country]
97
  r_initial = 0.00
98
  s_initial = 1 - i_initial - r_initial
99
 
100
+ t = np.linspace(0, 3000, 3000)
101
  x_initial = s_initial, i_initial, r_initial
102
  soln = odeint(deriv, x_initial, t, args=(beta, gamma))
103
  s, i, r = soln.T
104
  e = None
105
+
106
+ scaler = all_location[country]['total_cases'].apply(lambda x : x/pop_dict[country])
107
+ rangee = len(all_location[country]['total_cases'])
108
+ rmpe = mean_absolute_percentage_error(scaler,i[0:rangee])
109
 
110
 
111
+ return R0,t_infective,beta,gamma,rmpe,plotdata(t, s, i,r,R0)
112
+
113
+ def compare_plt(country):
114
+ fig = plt.figure(figsize=(12,6))
115
+ ax = [fig.add_subplot(121, axisbelow=True),fig.add_subplot(122)]
116
+ ax[0].set_title('Monkeypox confirmed cases')
117
+ ax[0].plot(all_location[country]['total_cases'],lw=3,label='Infective')
118
+ ax[0].set_xlabel('Days')
119
+ ax[0].set_ylabel('Number of cases')
120
+ ax[0].legend()
121
+
122
+ scaler = all_location[country]['total_cases'].apply(lambda x : x/pop_dict[country])
123
+ ax[1].set_title('Monkeypox confirmed cases compare with model')
124
+ ax[1].plot(scaler,lw=3,label='Real Infective')
125
+ ax[1].plot(i,lw=3,label='SIR model Infective')
126
+ ax[1].set_ylim(0,0.00005)
127
+ ax[1].set_xlim(0,200)
128
+ ax[1].set_xlabel('Days')
129
+ ax[1].set_ylabel('Fraction Number of cases')
130
+ ax[1].legend()
131
+ plt.tight_layout()
132
+
133
+ return fig
134
 
135
  def main():
136
  st.title("SIR Model for Monkeypox")
137
 
138
  with st.form("questionaire"):
139
  country = st.selectbox("Country",data['location'].unique())# user's input
140
+ recovery = st.slider("How long Monkeypox recover?", 21, 31, 21)
141
+ R0 = st.slider("Basic Reproduction Number (R0)", 0.57, 3.00, 0.57)# user's input
142
  country_code = code[country][0]
143
+ range = len(all_location['OWID_WRL']['total_cases'])
144
+ rmpe = mean_absolute_percentage_error(scaler,i[0:range])
145
  # clicked==True only when the button is clicked
146
  clicked = st.form_submit_button("Show Graph")
147
  if clicked:
148
 
149
  # Show SIR
150
+ SIR_param = SIR(country_code,R0,recovery)
151
 
152
  st.pyplot(SIR_param[-1])
153
+ st.pyplot(compare_plt(country_code))
154
  st.success("SIR model parameters for "+str(country)+" is")
155
  st.success("R0 = "+str(SIR_param[0]))
156
  st.success("Beta = "+str(SIR_param[2]))
157
  st.success("Gamma = "+str(SIR_param[3]))
158
+ st.success("RMPE = "+str(SIR_param[4]+"%"))
159
 
160
  # Run main()
161
  if __name__ == "__main__":