-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCovid19Plotter.py
406 lines (369 loc) · 14.9 KB
/
Covid19Plotter.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
'''
This script allows you to INPUT countries of interest, data of interest, starting date and
an integer smoothing parameter in order to plot the time series for the given type of data
and country set using matplotlib module and parsing the information provided in the dataset
collected by "Our World in Data" organization. Credits for the dataset goes to:
Hasell, J., Mathieu, E., Beltekian, D. et al. A cross-country database of COVID-19 testing.
Sci Data 7, 345 (2020). https://doi.org/10.1038/s41597-020-00688-8
'''
import csv
import requests
import datetime
import os
import pandas as pd
import warnings
warnings.filterwarnings("ignore", "(?s).*MATPLOTLIBDATA.*", category=UserWarning)
import matplotlib.pyplot as plt
import PIL.Image as pili
import data as dt
##################################################
##### GENERIC FUNCTION rolling: dataset -> dataset
##################################################
def rolling_n_deep_avg(alist,n_deep):
'''
INPUT: a list, integer n_deep
OUTPUT: rolling "n_deep" data average list ("n_deep"-1 elements shorter).
Integer rounded result
'''
new_list = []
for elem in range(len(alist)-n_deep+1):
subsidiary_list = [alist[elem+num] for num in range(n_deep)]
new_list.append( sum(subsidiary_list)/n_deep)
return new_list
#########################
#### PARSE DATA FUNCTIONS
#########################
def transdate(string):
"""
INPUT: string of the form "YYYY-MM-DD"
OUTPUT: datetime.date object with the corresponding date
"""
day = int(string[8:])
month = int(string[5:7])
year = int(string[:4])
#print(day, month, year)
return datetime.date(year, month, day)
def parse_title(a_string):
'''
Take a string such as 'new_deaths_per_million' and return 'New deaths per million'
'''
a_list = a_string.split('_')
b_string = (' '.join(a_list)).capitalize()
return b_string
#####################################################
#####################################################
def parser_local(countries):
"""
INPUT: List of Country Names in English
OUTPUT: A dictionary of dictionaries. The key value of the outer dictionary is a tuple (date, 'country').
The inner dictionary has keys from dt.COLUMNS and their respective values.
"""
num_of_countries = len(countries)
tot_entries = ((datetime.date.today()-datetime.date(2019, 12, 31)).days+1)*num_of_countries
print('')
print('At most a total of',tot_entries,'entries expected.')
with open(dt.FILE, 'rt', newline = "", encoding='utf-8-sig') as myfile:
csvreader = csv.reader(myfile,skipinitialspace = True,
delimiter = ',', quoting = csv.QUOTE_MINIMAL)
ntable = []
num = 0
print('')
for line in csvreader:
if line[2] in countries:
ntable.append(line)
num += 1
#print(num,'\b', sep=' ', end='', flush=True)
#print('')
print(num,'entries found for selected countries ... OK')
print('')
ncolumn = dt.COLUMNS
mydic = {}
for element in ntable:
mydic[(element[2],element[3])] = {ncolumn[index]: element[index] for index in range(len(ncolumn))}
return mydic
def order_for_plot(mydic, country, init_date, type_graph, deep):
'''
INPUT: - Parsed dictionary with all the data corresponding to selected countries. Assumed the form
returned by parser_local function
- a single country (string)
- initial date to consider in format 'YYYY-MM-DD'
- type_graph data we are interested in
- integer deep level or rolling average
RETURNS: List of [date, data] elements with the moving average applied on data for single country
'''
data_table = []
date_table = []
for key, val in mydic.items():
if len(val[type_graph]) > 0 and key[0] == country and transdate(val['date']) >= transdate(init_date):
data_table.append(float(val[type_graph]))
date_table.append(transdate(val['date']))
elif len(val[type_graph]) == 0 and key[0] == country and transdate(val['date']) >= transdate(init_date):
data_table.append(0)
date_table.append(transdate(val['date']))
if len(data_table) == 0:
return []
else:
new_data = rolling_n_deep_avg(data_table,deep)
new_date = list(date_table)
for iter in range(deep-1):
new_date.pop(0)
return pd.Series(data=new_data, index=new_date)
#####################################################
#####################################################
def draw_many_lines(countries, data_type, deep=7, init_date='2019-12-31', outputfile=0):
"""
INPUT: - List of countries written as strings in English
- String with the data type
- depth of the moving average. Default being 7
- Initial date to consider in format 'YYYY-MM-DD'. Default being the whole COVID series.
- name of output file. If parameter omitted plot is rendered in the browser
OUTPUT: Produces a png plot rendered in a temporary file (unless outputfile chosen)
for the specified countries and data type with a deep-day rolling mean.
RETURN: None
"""
plt.style.use('seaborn-notebook')
if os.path.exists(dt.FILE):
statbuf = datetime.datetime.fromtimestamp((os.stat(dt.FILE)).st_mtime)
print('')
print('=====================================================================')
print('')
print("LAST DOWNLOAD OF DATA: {}".format(statbuf))
print('')
fresh_or_not = input("Want to download fresh data? Press y/n (default is 'n'): ")
if fresh_or_not == 'y':
print('')
print('Downloading fresh data from "Our World in Data" ...', end='')
my_web = requests.get(dt.WEB, allow_redirects=True)
with open('owid-covid-data.csv', 'wb') as newdatafile:
newdatafile.write(my_web.content)
elif not os.path.exists(dt.FILE):
print('')
print('Downloading fresh data from "Our World in Data" ...', end='')
my_web = requests.get(dt.WEB, allow_redirects=True)
with open('owid-covid-data.csv', 'wb') as newdatafile:
newdatafile.write(my_web.content)
print('OK')
mydic = parser_local(countries)
for pais in countries:
print('Processing data for',pais,'... ', end='')
if data_type in dt.COLUMNS:
mytable = order_for_plot(mydic, pais, init_date, data_type, deep)
else:
print('')
print('')
print('WARNING!!!!')
print('No such data type as '+data_type+'. Please check your spelling and run again.')
print('')
return
if len(mytable) > 0:
print('OK')
if deep == 1:
plot_title = parse_title(data_type)+". dataset OWID"
elif deep > 1:
plot_title = parse_title(data_type)+". "+str(deep)+" days moving average. dataset OWID"
mytable.plot(legend=True, label=pais, grid=True, logy=False, figsize=(12,6),
title=plot_title, xlabel='date')
else:
print('')
print('')
print('WARNING!!!!')
print('No such country as '+pais+'. Please check your spelling and run again.')
print('')
return
if outputfile == 0:
print('Preparing plot... ', end='')
plt.savefig('temporalMLO2511.png')
fooooo = pili.open('temporalMLO2511.png').show()
os.remove('temporalMLO2511.png')
print('OK')
else:
print('Saving plot... ', end='')
plt.savefig(outputfile)
print('OK')
print('File saved to',outputfile)
def input_output_execution():
'''
INPUT: -
RETURN: -
OUTPUT: after executing input questions to the user ir executes draw_many_lines function with the proper arguments
'''
### GENERAL INFORMATION
os.system('cls')
print('======================================================================================================')
print('')
print(' PLOT COVID-19 DATA ')
print('This program is designed to plot time series lines for different COVID-19 data of different countries.')
print('Author: Matias Leoni')
print('')
print('Credit for the dataset:')
print('Hasell, J., Mathieu, E., Beltekian, D. et al. A cross-country database of COVID-19 testing.')
print('Sci Data 7, 345 (2020). https://doi.org/10.1038/s41597-020-00688-8')
print('======================================================================================================')
### INPUT Country names in English
country_list = []
while True:
print()
print('Input country name in english (e.g. Argentina).')
print("If you would like to see a country list to check your spelling press '?'")
country = input('If no more countries are needed just press ENTER: ')
if country == '' and len(country_list) > 0:
break
elif country == '' and len(country_list) == 0:
os.system('cls')
print()
print('At least one country name is needed')
elif country == '?':
os.system('cls')
print()
if len(country_list) > 0:
print('Your set of country choices is: ',end='')
print(*country_list, sep=', ')
print('')
print(*sorted(list(dt.COUNTRIES)), sep=', ')
elif country.title() not in dt.COUNTRIES:
os.system('cls')
print()
if len(country_list) > 0:
print('Your set of country choices is: ',end='')
print(*country_list, sep=', ')
print('')
print(country, 'is non-existent. Check your spelling')
elif country.title() in dt.COUNTRIES:
country_list.append(country.title())
country_list = list(set(country_list))
os.system('cls')
print('')
print('Your set of country choices is: ',end='')
print(*country_list, sep=', ')
os.system('cls')
print('')
print('Your set of country choices is: ',end='')
print(*country_list, sep=', ')
### Present data types
print()
print('=====================================================================')
print()
print()
data_type_list = dt.data_list
data_type_obj = dt.numbered_dic(data_type_list)
data_type_dic = data_type_obj.alist
print('LIST OF DATA TYPES')
print('----------------------------------------------------------------------')
data_type_obj.printdic()
print()
print('----------------------------------------------------------------------')
print()
### INPUT Data types
more = True
while more:
print('Please choose a data type from the previous list.')
try:
data_type = data_type_dic[int(input('For example, press 2 for new_cases: '))]
more = False
except ValueError:
print('')
print('Not an integer number. Try again')
print('')
except KeyError:
print('')
print('Your number is out of range. Try again')
print('')
print()
print()
os.system('cls')
print('')
print('Your set of country choices is: ',end='')
print(*country_list, sep=', ')
print('')
print('Your choice of data type to plot: ', parse_title(data_type))
print('=====================================================================')
print('')
### INPUT integer number corresponding to the number of days for the moving average
while True:
try:
print('Input an integer number (days) for smoothing data with moving average.')
deep_avg = input('1 is the default. 7 is the recommended for daily data: ')
if deep_avg == '':
deep_avg = 1
else:
deep_avg = int(deep_avg)
break
except ValueError:
print('')
print('That was not an integer number. Please try again')
print('')
print()
print()
os.system('cls')
print('')
print('Your set of country choices is: ',end='')
print(*country_list, sep=', ')
print('')
print('Your choice of data type to plot: ', parse_title(data_type))
print('')
if deep_avg > 1:
print('You decided to plot using a', deep_avg,'days moving average ')
print('=====================================================================')
print('')
### INPUT the desired starting date
while True:
try:
print('Input starting date in format YYYY-MM-DD.')
init_date = input('For example 2020-06-30. Press ENTER for default (beginning of the pandemic):')
if init_date == '':
init_date = '2019-12-31'
else:
transdate(init_date)
break
except ValueError:
print('')
print('Non-existent date or incorrect format. Please try again')
print('')
print()
print()
os.system('cls')
print('')
print('Your set of country choices is: ',end='')
print(*country_list, sep=', ')
print('')
print('Your choice of data type to plot: ', parse_title(data_type))
print('')
if deep_avg > 1:
print('You decided to plot using a', deep_avg,'days moving average ')
print('')
print('You will plot data starting on', init_date)
print('=====================================================================')
print('')
### INPUT the name of the file to render the plot
print('Input the file name for the output (e.g: grafico.png).')
outputfile = input('Press ENTER to show the plot without saving it: ')
if outputfile == '':
outputfile = 0
os.system('cls')
print('')
print('Your set of country choices is: ',end='')
print(*country_list, sep=', ')
print('')
print('Your choice of data type to plot: ', parse_title(data_type))
print('')
if deep_avg > 1:
print('You decided to plot using a', deep_avg,'days moving average ')
print('')
print('You will plot data starting on', init_date)
print('')
if outputfile == 0:
print('The plot will be shown in a new window and will not be saved')
else:
print('The plot will be saved to the file: ', outputfile)
### EXECUTE
draw_many_lines(country_list, data_type, deep_avg, init_date, outputfile)
# while True:
# execute = input('Do you want to execute the program interactively? (y/n): ')
# if execute == 'y':
# input_output_execution()
# del(execute)
# break
# elif execute == 'n':
# del(execute)
# break
input_output_execution()