-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlotIndia.py
312 lines (262 loc) · 12.8 KB
/
PlotIndia.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from Util import *
import pickle
import Plot
from EKF import *
import json
import Model
from scipy.integrate import odeint
from functools import partial
import numpy as np
import pandas as pd
import pdb
import os
import matplotlib.pyplot as plt
from matplotlib import ticker
def odeSimulator (model, x0, T) :
dx = partial(model.dx, module=np)
result = odeint(dx, x0, T)
return result
class KalmanSimulator () :
def __init__ (self, data, model, x0) :
self.x0 = x0
self.data = data
self.model = model
self.dates = data['Date'].map(self.splitDates)
self.firstCases = Date(self.dates.iloc[0])
self.dataEndDate = Date(self.dates.iloc[-1])
self.peopleDied = self.dates[data['Total Dead'] > 0].size > 0
if self.peopleDied :
self.firstDeath = Date(self.dates[data['Total Dead'] > 0].iloc[0])
self.startDate = self.firstDeath - 17
self.deaths = self.data['Daily Dead'][data['Total Dead'] > 0].to_numpy()
else :
self.startDate = self.firstCases
self.P = (data['Total Cases'] - data['Total Recovered'] - data['Total Dead']).to_numpy()
self.h1, self.h2 = [0] * 30, [0] * 30
self.h1[9:12] = model.mortality.tolist() # Setting mortality
self.h1[21:24] = model.mortality.tolist() # Setting mortality
self.h1[24:27] = model.mortality.tolist() # Setting mortality
self.h2[-6:-3] = [1,1,1] # Setting P
self.setP0()
self.setQ()
def setP0(self) :
self.P0 = np.eye(30)
def setQ (self) :
self.Q = np.eye(30)
def splitDates (self, date) :
d, m, _ = date.split('-')
d = int(d)
return f'{d} {m}'
def H (self, date) :
if self.peopleDied :
if date < self.firstCases :
return np.array([self.h1])
elif self.firstCases <= date <= self.dataEndDate - 17 :
return np.array([self.h1, self.h2])
elif self.dataEndDate - 17 < date <= self.dataEndDate :
return np.array([self.h2])
else :
return np.array([])
else :
if date <= self.dataEndDate :
return np.array([self.h2])
else :
return np.array([])
def Z (self, date):
if self.peopleDied :
if date < self.firstCases :
m = self.deaths[date - self.startDate]
return np.array([m])
elif self.firstCases <= date <= self.dataEndDate - 17 :
m = self.deaths[date - self.startDate]
p = self.P[date - self.firstCases]
return np.array([m, p])
elif self.dataEndDate - 17 < date <= self.dataEndDate :
p = self.P[date - self.firstCases]
return np.array([p])
else :
return np.array([])
else :
if date <= self.dataEndDate :
p = self.P[date - self.firstCases]
return np.array([p])
else :
return np.array([])
def R (self, date):
if self.peopleDied :
if date < self.firstCases :
return np.array([1])
elif self.firstCases <= date <= self.dataEndDate - 17 :
return np.eye(2)
elif self.dataEndDate - 17 < date <= self.dataEndDate :
return np.array([1])
else :
return np.array([])
else :
if date <= self.dataEndDate :
return np.array([1])
else :
return np.array([])
def __call__ (self, T) :
endDate = self.startDate + T
series, variances = extendedKalmanFilter(
self.model.dx, self.x0, self.P0,
self.Q, self.H, self.R, self.Z,
self.startDate, endDate)
return series, variances
def getInfections(dataDir, plot_start_date = Date('14 Mar')):
data_end_date = None
with open('./Data/beta.json') as fd :
betas = json.load(fd)
transportMatrix = np.loadtxt('./Data/transportMatrix.csv', delimiter=',')
statePop = [getStatePop(s) for s in Model.STATES]
mortality = [0.01 * getAgeMortality(s) for s in Model.STATES]
data = [getData(s) for s in Model.STATES]
model = Model.IndiaModel(transportMatrix, betas, statePop, mortality, data)
seriesOfSeries = []
lastSeries = []
seriesOfVariances = []
lastVariance = []
x0 = [0] * 15
tEnd = Date('15 Aug')
with open(os.path.join(dataDir, 'series.pkl'), 'rb') as fd :
seriesOfSeries = pickle.load(fd)
with open(os.path.join(dataDir, 'var.pkl'), 'rb') as fd :
seriesOfVariances = pickle.load(fd)
state_id = 1
total_population = 0
for m, datum, series, variance ,state, population in zip(model.models, data, seriesOfSeries, seriesOfVariances, Model.STATES, statePop) :
ks = KalmanSimulator(datum, m, x0)
total_population += population.sum()
T = len(series)
compartments = {k: [3*i, 3*i + 1, 3*i + 2] for i, k in enumerate(['S', 'E', 'A', 'I', 'Xs', 'Xe', 'Xa', 'Xi', 'P', 'R'])}
p, p_std = gather(T, series, variance, compartments['P'])
symptomatics, symptomatics_std = gather(T, series, variance, compartments['P'] + compartments['I'] + compartments['Xi'] + compartments['A'] + compartments['Xa'])
if data_end_date is None:
data_end_date = ks.startDate + len(p)
total_p = np.zeros((data_end_date - plot_start_date))
total_symptomatics = np.zeros((data_end_date - plot_start_date))
total_p += p[len(p) - len(total_p):]
total_symptomatics += symptomatics[len(p) - len(total_symptomatics):]
else:
assert data_end_date.date == (ks.startDate + len(p)).date, "Inconsistency in the data - all simulations not ending at the same date"
assert data_end_date.date == (ks.startDate + len(symptomatics)).date, "Inconsistency in the data - all simulations not ending at the same date"
if len(p) < len(total_p):
p = np.concatenate([np.zeros(- len(p) + len(total_p)), p])
symptomatics = np.concatenate([np.zeros(- len(symptomatics) + len(total_symptomatics)), symptomatics])
total_p += p[len(p) - len(total_p):]
total_symptomatics += symptomatics[len(p) - len(total_symptomatics):]
state_id = state_id + 1
return total_p, total_symptomatics, total_population
def gather(T, series, variances, indices):
outputSeries = [sum(x[index] for index in indices) for x in series]
outputVariances = [x[indices, :][:, indices].sum() for x in variances]
outputVariances = [np.sqrt(x) for x in outputVariances]
return np.array(outputSeries), np.array(outputVariances)
def plot (
base,
intervention1,
intervention2,
beginDate,
step,
plot_of,
population = None,
state = "India"
) :
T = len(base_p)
# Define a closure function to register as a callback
def convert_fraction_to_number(axis):
y1, y2 = axis.get_ylim()
rightAxis.set_ylim(population * float(y1) / 100., population * float(y2) / 100.)
rightAxis.figure.canvas.draw()
def convert_fraction_to_number2(axis):
y1, y2 = axis.get_ylim()
rightAxis2.set_ylim(population * float(y1) / 100., population * float(y2) / 100.)
rightAxis2.figure.canvas.draw()
def displayNumbers(x, pos):
if x >= 1e7: return '%1.1fM' % (x * 1e-6)
elif x > 1e5: return '%1.2fM' % (x * 1e-6)
elif x > 1e4: return '%1.0fk' % (x * 1e-3)
elif x > 1e3: return '%1.1fk' % (x * 1e-3)
else: return str(int(x))
formatter = ticker.FuncFormatter(displayNumbers)
def displayDate(y, pos):
return (beginDate + y).date
formatter_date = ticker.FuncFormatter(displayDate)
colors = ['b', 'g', 'r']
#Plotting Actual State Predictions
fig, ax1 = plt.subplots(nrows=1, ncols=1, sharex=True, figsize=(20, 10))
plt.xticks(rotation = 'vertical')
ax1.xaxis.set_major_formatter(formatter_date)
if population is not None:
rightAxis = ax1.twinx()
rightAxis.yaxis.set_major_formatter(formatter)
ax1.callbacks.connect("ylim_changed", convert_fraction_to_number)
fig.suptitle(state + ": " + plot_of, fontsize=25)
ax1.plot(np.arange(T), base * 100. / population, color = colors[0], label = "No Intervention")
# ax1.fill_between(np.arange(T), np.maximum(p - p_std, 0) * 100. / population, (p + p_std) * 100. / population, facecolor = colors[0], alpha=0.2)
ax1.plot(np.arange(T), intervention1 * 100. / population, color = colors[1], label = "Intervention 1")
# ax1.fill_between(np.arange(T), np.maximum(symptomatics - symptomatics_std, 0) * 100. / population, (symptomatics + symptomatics_std) * 100. / population , facecolor = colors[1], alpha=0.2)
ax1.plot(np.arange(T), intervention2 * 100. / population, color = colors[2], label = "Intervention 2")
# ax1.fill_between(np.arange(T), np.maximum(symptomatics - symptomatics_std, 0) * 100. / population, (symptomatics + symptomatics_std) * 100. / population , facecolor = colors[1], alpha=0.2)
# ax1.scatter(np.arange(0), [], c= colors[2], label = "Reported Positive")
ax1.legend(fontsize = 20, loc="upper left")
ax1.set_xlabel('Time / days', fontsize=25)
rightAxis.set_ylabel('Number of people', fontsize=25)
ax1.set_ylabel('Percentage of Total Population', fontsize=25)
# ax1.set_yscale('log')
ax1.xaxis.set_major_locator(ticker.MultipleLocator(step))
ax1.tick_params(axis='both', which='major', labelsize=20)
if population is not None:
rightAxis.tick_params(axis='both', which='major', labelsize=20)
# #### INSET GRAPH
# left, bottom, width, height = [0.18, 0.37, 0.35, 0.35]
# ax2 = fig.add_axes([left, bottom, width, height])
# if population is not None:
# rightAxis2 = ax2.twinx()
# rightAxis2.yaxis.set_major_formatter(formatter)
# ax2.callbacks.connect("ylim_changed", convert_fraction_to_number2)
# rightAxis2.tick_params(axis='both', which='major', labelsize=20)
# T2 = Date('1 Jun') - beginDate
# base_deaths = base_deaths[:T2]
# intervention1_deaths = intervention1_deaths[:T2]
# intervention2_deaths = intervention2_deaths[:T2]
# ax2.plot(np.arange(T2), base_deaths * 100. / population, color = colors[0], label = "No Intervention")
# # ax2.fill_between(np.arange(T2), np.maximum(p - p_std, 0) * 100. / population, (p + p_std) * 100. / population, facecolor = colors[0], alpha=0.2)
# ax2.plot(np.arange(T2), intervention1_deaths * 100. / population, color = colors[1], label = "Intervention 1")
# # ax2.fill_between(np.arange(T2), np.maximum(symptomatics - symptomatics_std, 0) * 100. / population, (symptomatics + symptomatics_std) * 100. / population, facecolor = colors[1], alpha=0.2)
# ax2.plot(np.arange(T2), intervention2_deaths * 100. / population, color = colors[2], label = "Intervention 2")
# # ax2.fill_between(np.arange(T2), np.maximum(symptomatics - symptomatics_std, 0) * 100. / population, (symptomatics + symptomatics_std) * 100. / population, facecolor = colors[1], alpha=0.2)
# tickLabels = list(DateIter(beginDate, beginDate + T + 30))[::7]
# tickLabels = [d.date for d in tickLabels]
# tickLabels = ['', *tickLabels]
# ax2.xaxis.set_major_locator(ticker.MultipleLocator(7))
# ax2.set_xticklabels(tickLabels, rotation = 'vertical')
# ax2.tick_params(axis='both', which='major', labelsize=18)
plt.gcf().subplots_adjust(bottom=0.2)
fig.savefig('./Plots/master/' + state.upper() + '_' + plot_of.replace(" ", "_").lower())
plt.close(fig)
plt.clf()
if __name__ == "__main__":
base_p, base_symptomatics, total_population= getInfections('/Users/sahil/Desktop/sem8/covid/blossomRuns/base/', plot_start_date = Date('1 Apr'))
intervention1_p, intervention1_symptomatics, _ = getInfections('/Users/sahil/Desktop/sem8/covid/blossomRuns/intervention1/', plot_start_date = Date('1 Apr'))
intervention2_p, intervention2_symptomatics, _ = getInfections('/Users/sahil/Desktop/sem8/covid/blossomRuns/intervention2/', plot_start_date = Date("1 Apr"))
print(list(zip(base_p, base_symptomatics)))
plot(
base = base_p,
intervention1 = intervention1_p,
intervention2 = intervention2_p,
plot_of = "Tested Positive",
beginDate = Date('1 Apr'),
step = 7,
population = total_population
)
plot(
base = base_symptomatics,
intervention1 = intervention1_symptomatics,
intervention2 = intervention2_symptomatics,
plot_of = "Total Infected",
beginDate = Date('1 Apr'),
step = 7,
population = total_population
)