-
Notifications
You must be signed in to change notification settings - Fork 1
/
COVID-19_Ar_RealEstimation.py
303 lines (278 loc) · 13.7 KB
/
COVID-19_Ar_RealEstimation.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
from matplotlib.pyplot import figure
import matplotlib.pyplot as plt
import matplotlib.ticker as tk
import numpy as nu
import pandas as pd
import matplotlib.dates as mdates
from datetime import datetime as dt
print("####################################################")
print(" Estimations of COVID-19 Outbreak")
print("----------------------------------------------------")
print("https://github.com/rvalla/COVID-19")
print("Data loaded from official national reports")
print("https://argentina.gob.ar/coronavirus/informe-diario")
print("----------------------------------------------------")
print()
print("Loading data...", end="\n")
#Selecting regions to study in detail...
#Note that the first one will be used as reference to decide periods of time in some charts...
#regions = ["CABA", "BUENOS AIRES", "CHACO", "CORDOBA", "RIO NEGRO", "MENDOZA", "NEUQUEN", "ARGENTINA", "SANTA FE", "TIERRA DEL FUEGO", "ENTRE RIOS"]
regions = ["CABA", "BUENOS AIRES", "ARGENTINA"]
#Selecting data to display
startDate = "2020-04-01" #Starting point for plotbyDate. Default: 03/03
caseCount = 200 #Starting point for plotbyOutbreak (number of confirmed cases)
dataGuide = 0 #Data type to calculate startpoints (0 for confirmed, 2 for deaths)
realMortality = 0.01 #Real mortality to estimate infected count from deaths
deathOffset = 10 #Number of days needed to reach a death since symptoms onset on average
#Deciding language for titles and tags...
lg = 0 # 0 for english, 1 for spanish
ratioticks = 0.1
estimationticks = 200000
newratioticks = 0.2
newestimationticks = 5000
#Deciding if you want to save and show charts...
saveChart = False
showChart = True
#Deciding what to plot...
realCasesEstimation = False
testedRatio = False
ratioAndEstimation = True
ratioAndEstimation7dAv = True
#Deciding between linear or logarithmic scales...
plotScale = "linear"
#Variables to store filenames and other strings...
fileNamePrefix = "Argentina_COVID19_"
fileNames = ["00_Confirmed.csv", "02_Deaths.csv", "07_NewConfirmed.csv", "11_NewDeaths.csv",
"26_NewConfirmed7dAv.csv", "28_Newdeaths7dAv.csv"]
dataTitles = ["Estimation based on deaths", "Estimación a partir de fallecimientos", "Known cases ratio",
"Tasa de detección", "Estimation based on deaths (7d)", "Estimación a partir de fallecimientos (7d)",
"Known cases ratio (7d)", "Tasa de detección (7d)"]
plotTitles = ["Real cases estimation", "Estimación de casos"]
shortLabels = ["Estimated cases", "Casos estimados", "Confirmed", "Confirmados", "Deaths", "Fallecimientos",
"Known ratio", "Tasa de detección"]
xTitles = ["Time in days", "Tiempo en días"]
yTitles = ["Estimated cases", "Casos estimados", "Confirmed", "Confirmados", "Deaths", "Fallecimientos",
"Known ratio", "Tasa de detección"]
tConector = [" since ", " desde ", " after ", " después de ", " and ", " y ", "mortality = ", "mortalidad = ",
"deaths offset = ", "corrimiento fallecimientos = "]
filePath = "Argentina_Data/processed_data/"
chartPath = "Argentina_Data/actual_charts/estimations/"
#Some styling...
defaultFont = "Oswald" #Change this if you don't like it or is not available in your system
legendFont = "Myriad Pro" #Change this to edit legends' font
colorlist = ["chocolate", "tab:blue", "firebrick", "tab:green", "rebeccapurple", "olivedrab",
"crimson", "darkcyan", "aqua", "dodgerblue", "palegreen", "seagreen", "limegreen", "indianred",
"slategrey", "royalblue", "navy", "slateblue", "goldenrod", "greenyellow", "darkturquoise",
"coral", "tab:pink"] #Default colors for data
backgroundPlot = "silver" #Default background color for charts
backgroundFigure = "white" #Default background color for figures
majorGridColor = "dimgrey" #Default colors for grids...
minorGridColor = "dimgray"
alphaMGC = 0.7
alphamGC = 0.9
imageResolution = 150
#Loading data...
databases = []
for d in range(len(fileNames)):
databases.append(pd.read_csv(filePath + fileNamePrefix + fileNames[d]))
databases[d].set_index("FECHA", inplace = True)
databases[d].index = pd.DatetimeIndex(databases[d].index)
databases[d].index.name = "FECHA"
#Constant configurations...
startDateTime = dt.strptime(startDate, "%Y-%m-%d")
regionsIndexes = [[],[]]
quarantineStart = "2020-03-21"
startDateIndex = databases[0].index.get_loc(startDate) #Saving the startDate index for annotations
quarantineIndex = databases[0].index.get_loc(quarantineStart)
dateFormat = mdates.DateFormatter('%b %d')
dateFormatString = "%B %d"
if lg ==1:
dateFormat = mdates.DateFormatter('%d/%m')
dateFormatString = "%d/%m"
#Printing selected regions on console
print("Plotting data of " + str(regions), end="\n")
#Method to draw a mark in social isolation start date
def markQuarantine(tag, yshift, ytshift, font, x, y, w, hw, hl):
if startDate < quarantineStart and plotScale == "linear":
plt.annotate(tag, fontsize=font, xy=(x, y + yshift), xycoords='data',
xytext=(x, y + ytshift), textcoords='data',
arrowprops=dict(facecolor='orangered', edgecolor="none", width=w, headwidth=hw, headlength=hl),
horizontalalignment='center', verticalalignment='top')
def savePlot(csvName, figure):
chartName = csvName.split(".")
plt.savefig(chartPath + str(lg) + "_" + chartName[0] + ".png", facecolor=figure.get_facecolor())
def gridAndTicks(yMax, ticksInterval):
plt.grid(which='both', axis='both')
plt.minorticks_on()
plt.grid(True, "major", "y", ls="-", lw=0.8, c=majorGridColor, alpha=alphaMGC)
plt.grid(True, "minor", "y", ls="--", lw=0.3, c=minorGridColor, alpha=alphamGC)
plt.grid(True, "major", "x", ls="-", lw=0.8, c=majorGridColor, alpha=alphaMGC)
plt.grid(True, "minor", "x", ls="--", lw=0.3, c=minorGridColor, alpha=alphamGC)
plt.xticks(fontsize=6)
plt.yticks(fontsize=6)
if plotScale == "linear":
plt.yticks(nu.arange(0, yMax, ticksInterval))
plt.gca().set_facecolor(backgroundPlot)
def ticksLocator(weekInterval):
plt.gca().xaxis.set_minor_locator(tk.AutoMinorLocator(7))
plt.gca().xaxis.set_major_locator(mdates.WeekdayLocator(interval=weekInterval))
plt.gca().xaxis.set_major_formatter(dateFormat)
plt.gca().xaxis.set_minor_formatter(tk.NullFormatter())
estimationsAndData = []
def buildData():
for r in range(len(regions)):
regiondata = pd.DataFrame(index=databases[0].index, columns=["confirmed", "deaths", "estimation", "ratio",
"new confirmed", "new deaths", "new estimated", "new ratio", "new confirmed (7d)", "new deaths (7d)",
"new estimated (7d)", "new ratio (7d)"])
for e in range(regiondata.shape[0]):
regiondata.loc[regiondata.index[e], "confirmed"] = databases[0].loc[databases[0].index[e], regions[r]]
regiondata.loc[regiondata.index[e], "deaths"] = databases[1].loc[databases[1].index[e], regions[r]]
regiondata.loc[regiondata.index[e], "new confirmed"] = databases[2].loc[databases[2].index[e], regions[r]]
regiondata.loc[regiondata.index[e], "new confirmed (7d)"] = databases[4].loc[databases[4].index[e], regions[r]]
regiondata.loc[regiondata.index[e], "new deaths"] = databases[3].loc[databases[3].index[e], regions[r]]
regiondata.loc[regiondata.index[e], "new deaths (7d)"] = databases[5].loc[databases[5].index[e], regions[r]]
for e in range(regiondata.shape[0] - deathOffset):
regiondata.loc[regiondata.index[e], "estimation"] = \
regiondata.loc[regiondata.index[e + deathOffset], "deaths"] / realMortality
regiondata.loc[regiondata.index[e], "new estimated"] = \
regiondata.loc[regiondata.index[e + deathOffset], "new deaths"] / realMortality
regiondata.loc[regiondata.index[e], "new estimated (7d)"] = \
regiondata.loc[regiondata.index[e + deathOffset], "new deaths (7d)"] / realMortality
regiondata["ratio"] = regiondata["confirmed"].div(regiondata["estimation"])
regiondata["new ratio"] = regiondata["new confirmed"].div(regiondata["new estimated"])
regiondata["new ratio (7d)"] = regiondata["new confirmed (7d)"].div(regiondata["new estimated (7d)"])
databases[d].index = pd.DatetimeIndex(databases[d].index)
databases[d].index.name = "FECHA"
global estimationsAndData
estimationsAndData.append(regiondata)
buildData()
def plotRatioAndEstimation(regions, xtitle, ytitleA, ytitleB, markQ, ticksIntervalA, ticksIntervalB, savechart, show):
figure = plt.figure(num=None, figsize=(4, 4), dpi=imageResolution, facecolor=backgroundFigure, edgecolor='k')
#figure.suptitle("COVID-19: " + plotTitles[0+lg] + "\n (" + tConector[6+lg] + str(realMortality) + ")", fontname=defaultFont, fontsize=12)
plt.subplot2grid((2, 1), (0, 0))
#Saving y values for markQ...
yquarantine = []
x = quarantineIndex
#Plotting selected data...
for i in range(len(regions)):
plotA = estimationsAndData[i][startDate:]["ratio"].plot(kind='line', label=regions[i], \
color=colorlist[i], alpha = 1.0, linewidth=2.0)
s = plt.ylim()
a = plt.xlim()
#Setting up titles
plotA.set_title(dataTitles[2+lg], fontname=defaultFont, fontsize=10)
plt.yscale(plotScale)
# plt.ylabel(ytitleA, fontname=legendFont, fontsize=8)
plt.ylabel("")
plt.xlabel("")
plt.legend(loc=4, shadow = True, facecolor = backgroundFigure, prop={'family' : legendFont, 'size' : 6})
#Setting up grid...
gridAndTicks(s[1]*1.1, ticksIntervalA)
ticksLocator(3)
plt.gca().xaxis.set_ticklabels([])
#Setting axis labels font and legend
plt.subplot2grid((2, 1), (1, 0))
#Saving y values for markQ...
yquarantine = []
x = quarantineIndex
#Plotting selected data...
for i in range(len(regions)):
plotB = estimationsAndData[i][startDate:]["estimation"].plot(kind='line', label=regions[i], \
color=colorlist[i], alpha = 1.0, linewidth=2.0, zorder=2)
plotB = estimationsAndData[i][startDate:]["confirmed"].plot(kind='line', label=regions[i], \
color=colorlist[i], alpha = 0.5, linewidth=2.0, zorder=1)
yquarantine.append(estimationsAndData[i].loc[quarantineStart, "estimation"])
s = plt.ylim()
plt.xlim(a[0], a[1])
if markQ == True:
y = max(yquarantine) #Drawing a mark on quarantineStartDate
markQuarantine("", s[1]/20, s[1]/5, 8, quarantineStart, y, 5, 9, 7)
#Setting up titles
plotB.set_title(dataTitles[0+lg] +"\n (" + tConector[8+lg] + str(deathOffset) + ", " +
tConector[6+lg] + str(realMortality) + ")", fontname=defaultFont, fontsize=10)
plt.yscale(plotScale)
# plt.ylabel(ytitleB, fontname=legendFont, fontsize=8)
plt.ylabel("")
plt.xlabel(xtitle, fontname=legendFont, fontsize=8)
#Setting up grid...
gridAndTicks(s[1]*1.1, ticksIntervalB)
ticksLocator(3)
ylabels = nu.arange(0, s[1]/1000*1.1, ticksIntervalB/1000).tolist()
for l in range(len(ylabels)):
ylabels[l] = "{:.0f}".format(ylabels[l])
ylabels[l] += "K"
plt.yticks(nu.arange(0, s[1] * 1.1, ticksIntervalB), ylabels)
plt.tight_layout(rect=[0, 0, 1, 1])
if savechart == True:
auxName = fileNames[0].split(".")
savePlot("E_00_KnownRatioAndEstimation.csv", figure)
if show == True:
plt.show()
def plotRatioAndEstimation7dAv(regions, xtitle, ytitleA, ytitleB, markQ, ticksIntervalA, ticksIntervalB, savechart, show):
figure = plt.figure(num=None, figsize=(4, 4), dpi=imageResolution, facecolor=backgroundFigure, edgecolor='k')
#figure.suptitle("COVID-19: " + plotTitles[0+lg] + "\n (" + tConector[6+lg] + str(realMortality) + ")", fontname=defaultFont, fontsize=12)
plt.subplot2grid((2, 1), (0, 0))
#Saving y values for markQ...
yquarantine = []
x = quarantineIndex
#Plotting selected data...
for i in range(len(regions)):
plotA = estimationsAndData[i][startDate:]["new ratio (7d)"].plot(kind='line', label=regions[i], \
color=colorlist[i], alpha = 1.0, linewidth=2.0)
s = plt.ylim()
a = plt.xlim()
#Setting up titles
plotA.set_title(dataTitles[6+lg], fontname=defaultFont, fontsize=10)
plt.yscale(plotScale)
# plt.ylabel(ytitleA, fontname=legendFont, fontsize=8)
plt.ylabel("")
plt.xlabel("")
plt.legend(loc=1, shadow = True, facecolor = backgroundFigure, prop={'family' : legendFont, 'size' : 6})
#Setting up grid...
gridAndTicks(s[1]*1.1, ticksIntervalA)
ticksLocator(3)
plt.gca().xaxis.set_ticklabels([])
#Setting axis labels font and legend
plt.subplot2grid((2, 1), (1, 0))
#Saving y values for markQ...
yquarantine = []
x = quarantineIndex
#Plotting selected data...
for i in range(len(regions)):
plotB = estimationsAndData[i][startDate:]["new estimated (7d)"].plot(kind='line', label=regions[i], \
color=colorlist[i], alpha = 1.0, linewidth=2.0, zorder=2)
plotB = estimationsAndData[i][startDate:]["new confirmed (7d)"].plot(kind='line', label=regions[i], \
color=colorlist[i], alpha = 0.5, linewidth=2.0, zorder=1)
yquarantine.append(estimationsAndData[i].loc[quarantineStart, "estimation"])
s = plt.ylim()
plt.xlim(a[0], a[1])
if markQ == True:
y = max(yquarantine) #Drawing a mark on quarantineStartDate
markQuarantine("", s[1]/20, s[1]/5, 8, quarantineStart, y, 5, 9, 7)
#Setting up titles
plotB.set_title(dataTitles[4+lg] +"\n (" + tConector[8+lg] + str(deathOffset) + ", " +
tConector[6+lg] + str(realMortality) + ")", fontname=defaultFont, fontsize=10)
plt.yscale(plotScale)
# plt.ylabel(ytitleB, fontname=legendFont, fontsize=8)
plt.ylabel("")
plt.xlabel(xtitle, fontname=legendFont, fontsize=8)
#Setting up grid...
gridAndTicks(s[1]*1.1, ticksIntervalB)
ticksLocator(3)
ylabels = nu.arange(0, s[1]/1000*1.1, ticksIntervalB/1000).tolist()
for l in range(len(ylabels)):
ylabels[l] = "{:.0f}".format(ylabels[l])
ylabels[l] += "K"
plt.yticks(nu.arange(0, s[1] * 1.1, ticksIntervalB), ylabels)
plt.tight_layout(rect=[0, 0, 1, 1])
if savechart == True:
auxName = fileNames[0].split(".")
savePlot("E_00_KnownRatioAndEstimation7dAv.csv", figure)
if show == True:
plt.show()
if ratioAndEstimation == True:
plotRatioAndEstimation(regions, xTitles[0+lg], yTitles[6+lg], yTitles[0+lg], True, ratioticks, estimationticks, saveChart, showChart)
if ratioAndEstimation7dAv == True:
plotRatioAndEstimation7dAv(regions, xTitles[0+lg], yTitles[6+lg], yTitles[0+lg], True, newratioticks, newestimationticks, saveChart, showChart)
#Saying good bye...
print("That's all. If you want more plots, edit the code and run again. ", end="\n")