-
Notifications
You must be signed in to change notification settings - Fork 1
/
optimize_panel.py
237 lines (204 loc) · 7.31 KB
/
optimize_panel.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import datetime
import gripql
import pandas
import numpy as np
from scipy.integrate import odeint
from scipy.optimize import minimize
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
from app import app
conn = gripql.Connection("http://localhost:8201")
G = conn.graph("covid")
#get Oregon Counties
q = G.query().V().hasLabel("SummaryLocation").has(gripql.eq("province_state", "OR"))
q = q.render(["$._gid", "$.county"])
countyOptions = list( { "label" : a[1], "value" : a[0] } for a in q )
countyDropDown = dcc.Dropdown(
id='opt-county-dropdown',
options=countyOptions,
value=countyOptions[0]['value']
)
def getCountySummaryReports(fips):
q = G.query().V(fips).out("summary_reports").render(["date", "confirmed", "deaths", "recovered"])
return list(q)
def getCountyPopulation(fips):
q = G.query().V(fips).out("census").has(gripql.eq("gender", None)).render(["population"])
population = sum(list(a[0] for a in q))
return population
# Based on model found at https://github.com/omerka-weizmann/2_day_workweek/blob/master/code.ipynb
def SEIR_model(y,t,config):
"""
SEIR model
@y,t: - variables for the differential equations
@config: include - rates beta,gamma for differential equations
"""
S,E,I,R = y
Tinc,Tinf = config["Tinc"],config["Tinf"]
Rt = config["Rt"]
dydt = [-Rt/Tinf * (I*S),
Rt/Tinf * (I*S) - (1/Tinc)*E,
(1/Tinc)*E - (1/Tinf)*I,
(1/Tinf)*I]
return dydt
def calc_delta(df, R=3.0, Tinc=3, Tinf=15, startI=0.00005, beta=0.25, gamma=0.25, Toffset=0):
"""
calc_delta
@df: county summary report
@R: replication value
@Tinc: time-incubation
@Tinf: time-infection
@startI: starting infection
@Toffset: offset of observations (missing days from actual begining)
"""
tmax = df['days'].max()+1+Toffset
t = np.linspace(1,tmax,tmax)
config = {'Rt' : R, 'Tinc': Tinc, 'Tinf': Tinf, 'beta': 0.25, 'gamma': 0.25}
SEIR_y0 = [1-startI,startI/2,startI/2,0]
modelOutput = odeint(SEIR_model, SEIR_y0, t, args=(config,), atol=1e-12, rtol=1e-12)
# exposed + infected + recovered
modelSums = pandas.DataFrame(modelOutput[:,[1,2,3]]).sum(axis=1)
# compare to confirmed numbers
delta = np.sum(np.power(df['confirmed'].values - (modelSums[df['days']+Toffset] * population),2))
return delta
def optimize_R(df, config):
return minimize(lambda x: calc_delta(df, R=x[0], **config), (3), bounds=((1,7),), method="L-BFGS-B", options={"ftol":1e-12})
def summaryReportDataFrame(summary_reports):
data = {}
for row in summary_reports:
d = datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S")
data[d] = {"confirmed":int(row[1]), "deaths" : int(row[2]), "recovered":int(row[3])}
df = pandas.DataFrame(data).transpose().sort_index()
delta = pandas.Series( (df.index - df.index[0]).round("D").astype('timedelta64[D]'), index=df.index, name="days")
return df.join(delta)
optimizeGraph = dcc.Graph(id='optimize-graph')
@app.callback(Output('county-data', 'data'),
[Input('opt-county-dropdown', 'value')])
def updateCountData(value):
print("Updating counts")
summary_reports = getCountySummaryReports(value)
population = getCountyPopulation(value)
return { "summary_reports" : summary_reports, "population" : population }
@app.callback(Output('model-data', 'data'),
[Input('opt-r-value', 'value'), Input('opt-infection-start', 'value'),
Input('opt-incubation-days', 'value'), Input('opt-infectious-days', 'value'),
Input('opt-offset-days', 'value'),Input("opt-length-days", "value")])
def updateModel(Rt, startI, Tinc, Tinf, Toffset, Tmax):
print("Running Model")
t = np.linspace(1,Tmax,Tmax)
config = {'Rt' : Rt, 'Tinc': Tinc, 'Tinf': Tinf, 'beta': 0.25, 'gamma': 0.25}
SEIR_y0 = [1-startI,startI/2,startI/2,0]
modelOutput = odeint(SEIR_model, SEIR_y0, t, args=(config,), atol=1e-12, rtol=1e-12)
modelSums = pandas.DataFrame(modelOutput[:,[1,2,3]]).sum(axis=1)
return modelSums.to_list()
@app.callback(Output("optimize-graph", "figure"),
[Input('county-data', "data"), Input('model-data', "data"),
Input('opt-offset-days', "value")])
def renderGraph(countyData, modelData, tOffset):
print("Doing Model Render")
report = {}
if countyData is None or modelData is None:
return {}
for row in countyData['summary_reports']:
d = datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S")
report[d] = { "confirmed" : row[1], "deaths" : row[2], "recovered" : row[3] }
reportDF = pandas.DataFrame(report).transpose().sort_index()
reportDates = reportDF.index.to_list()
modelDF = pandas.Series(modelData) * countyData['population']
modelDates = (pandas.to_timedelta(modelDF.index - tOffset, unit="D") + reportDF.index[0]).to_list()
return {
"data" : [{
"x" : reportDates,
"y" : list( report[d]["confirmed"] for d in reportDates ),
"name" : "Total Reported"
},{
"x" : modelDates,
"y" : modelDF.to_list(),
"name" : "Projection"
}]
}
@app.callback(Output("county-population-text", "children"),
[Input('county-data', "data")])
def renderCountyPopulationText(data):
p = data.get('population', 0)
if p is None:
p = 0
return html.Label('Population: %d' % (p))
@app.callback(Output("county-infection-text", "children"),
[Input('opt-infection-start', 'value'), Input('county-data', "data")])
def renderInfectionRate(value, countyData):
p = countyData.get('population', 0)
if p is None:
p = 0
return html.Label("Starting with %f individuals in county" % (value * p))
inputs = html.Div([
html.P([
html.Label("Infection Start %: "),
dcc.Input(
id='opt-infection-start',
type="number",
min=0.0,
max=1.0,
step=0.0000001,
value=0.00002),
html.Div(id="county-infection-text")
]),
html.P([
html.Label("Incubation Days: "),
dcc.Input(
id='opt-incubation-days',
type="number",
min=1,
max=30,
step=1,
value=3)
]),
html.P([
html.Label("Infectious Days: "),
dcc.Input(
id='opt-infectious-days',
type="number",
min=1,
max=30,
step=1,
value=4)
]),
html.P([
html.Label("R: "),
dcc.Input(
id='opt-r-value',
type="number",
min=0,
max=20,
step=0.1,
value=2.5)
]),
html.P([
html.Label("Model Offset Days: "),
dcc.Input(
id='opt-offset-days',
type="number",
min=0,
max=30,
step=1,
value=0)
]),
html.P([
html.Label("Model Length: "),
dcc.Input(
id='opt-length-days',
type="number",
min=10,
max=360,
step=1,
value=30)
])
])
OptimizeParams = html.Div([
dcc.Store("model-data"),
dcc.Store("county-data"),
countyDropDown,
html.Div(id="county-population-text"),
inputs,
optimizeGraph
])