-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuertey_covid19_final.py
240 lines (214 loc) · 6.93 KB
/
nuertey_covid19_final.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
#***********************************************************************
# @file
#
# Python script for querying COVID-19 statistics via John Hopkins API
# and visualizing them.
#
# @note For the future, these visualizations could be enhanced with live
# news articles on the 'COVID-19' topic via newsapi much like follows:
#
#https://newsapi.org/v2/top-headlines?country=us&apiKey=f048819049c24d6d86bd424daa2349f1
#
#http://newsapi.org/v2/everything?q=ghana&from=2020-01-01&sortBy=publishedAt&apiKey=f048819049c24d6d86bd424daa2349f1
#
# NEWS_API_KEY = config("NEWS_API_KEY")
# NEWS_API_URL = f"https://newsapi.org/v2/top-headlines?country=us&apiKey={NEWS_API_KEY}"
#
# Notice the use of the python 3.6 introduced f-strings in the statement
# above. F-strings can be further explained by this code snippet:
#
# >>> name = "Adjoa"
# >>> age = 15
# >>> f"Hello, {name}. You are {age}."
# 'Hello, Adjoa. You are 15.'
#
# @warning None
#
# Created: April 29, 2020
# Author: Nuertey Odzeyem
#**********************************************************************/
import os
import sys
import argparse
import pandas as pd
import plotly.express as px
from covid import Covid
pd.set_option('display.max_rows', 100)
def init_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
usage="%(prog)s [-h] | [-v]",
description="Visualize COVID-19 statistics data on the World map.",
allow_abbrev=False,
add_help=True
)
parser.add_argument(
"-v", "--version", action="version",
version=f"{parser.prog} version 0.0.3"
)
return parser
parser = init_argparse()
args = parser.parse_args()
source = "john_hopkins" # Currently, the only source supported by this script.
cov_19 = Covid(source)
country_list = cov_19.list_countries()
data = pd.DataFrame(country_list)
print(data)
print()
def retrieve_covid_statistics(country_id_input, country_name_input):
#print(country_id_input)
#print(country_name_input)
#print()
try:
country_status = cov_19.get_status_by_country_name(country_name_input)
except Exception as e:
print("Caught an unexpected exception:")
print(e)
print()
country_status = {'id': country_id_input, 'country': country_name_input, 'confirmed': 0, 'active': 0, 'deaths': 0, 'recovered': 0, 'latitude': 0, 'longitude': 0, 'last_update': 1591652013000}
#print(country_status)
#print()
# Returning a dictionary is faster:
return {'country_id': country_status['id'], 'country': country_status['country'], 'confirmed': country_status['confirmed'], 'active': country_status['active'], 'deaths': country_status['deaths'], 'recovered': country_status['recovered'], 'latitude': country_status['latitude'], 'longitude': country_status['longitude'], 'last_update': country_status['last_update']}
# Employ a list for now in the list comprehension for faster processing:
combined_output = [retrieve_covid_statistics(identifier, country) for identifier, country in zip(data['id'], data['name'])]
#print(combined_output)
#print()
cols = ['country_id', 'country', 'confirmed', 'active', 'deaths', 'recovered',
'latitude', 'longitude', 'last_update']
# Now create the dataframe from the complete list as this approach is much much faster:
combined_output = pd.DataFrame(combined_output, columns=cols, index=data.index)
# Append a new column by employing the exponent operation to scale the
# data in order to render smaller values visible on the scatter mapbox.
combined_output['scaled'] = combined_output['confirmed'] ** 0.77
print(combined_output)
print()
#print('Data type of each column of combined_output Dataframe:')
#print(combined_output.dtypes)
#print()
world_data = combined_output.infer_objects()
#print('Data type of each column of combined_output Dataframe after conversion:')
#print(world_data.dtypes)
#print()
color_scale = [
"#fadc8f",
"#f9d67a",
"#f8d066",
"#f8c952",
"#f7c33d",
"#f6bd29",
"#f5b614",
"#F4B000",
"#eaa900",
"#e0a200",
"#dc9e00",
]
color_scale_2 = [
"#fce9b8",
"#fbe6ad",
"#fbe3a3",
"#fbdf99",
"#fadc8f",
"#fad985",
"#f9d67a",
"#f9d370",
"#f8d066",
"#f8cc5c",
"#f8c952",
"#f7c647",
"#f7c33d",
"#f6c033",
"#f6bd29",
"#f5b91f",
"#f5b614",
"#f4b30a",
"#F4B000",
"#efac00",
"#eaa900",
"#e5a500",
"#e0a200",
"#dc9e00"
]
# The fuchsia color code is #FF00FF. Here it is graded in order of increasing strength:
magenta_gradient = [
"#ff00ff",
"#e800e7",
"#cc00cc",
"#b000af",
"#8b008b",
]
# The Hot Gradient Color Scheme palette has 6 colors which are Red (#FE0000),
# Red-Orange (X11) (#FE3F02), University Of Tennessee Orange (#F97C00),
# Orange Peel (#FB9E00), Cyber Yellow (#FBD400) and Lemon Glacier (#FDFD04).
hot_gradient = [
"#fdfd04",
"#fbd400",
"#fb9e00",
"#f97c00",
"#fe3f02",
"#fe0000",
]
# ==========
# Example 1:
#
# Here is a simple map rendered with OpenStreetMaps tiles, without needing
# a Mapbox Access Token:
# ==========
# color_discrete_sequence=["fuchsia"],
figure = px.scatter_mapbox(
world_data,
lat="latitude",
lon="longitude",
color="confirmed",
size="scaled",
size_max=50,
hover_name="country",
hover_data=["confirmed", "deaths"],
color_continuous_scale=magenta_gradient,
zoom=3,
height=700
)
figure.update_layout(mapbox_style="open-street-map")
figure.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
# Replace the displayed latitude and longitude with custom-labelled data:
#print(figure.data[0].hovertemplate)
figure.data[0].update(
hovertemplate="<b>%{hovertext}</b><br><br>Confirmed=%{marker.color}<br>Deaths=%{customdata[1]}"
)
figure.show()
# ==========
# Example 2:
#
# Here is a map rendered with the "dark" style from the Mapbox service,
# which requires an Access Token. Obtain your free access token from www.mapbox.com
# and save it in the current directory as the filename below, '.mapbox_token':
# ==========
token = open(".mapbox_token").read()
# color_continuous_scale=px.colors.cyclical.IceFire,
figure = px.scatter_mapbox(
world_data,
lat="latitude",
lon="longitude",
color="confirmed",
size="scaled",
size_max=50,
hover_name="country",
hover_data=["confirmed", "active", "deaths"],
color_continuous_scale=hot_gradient,
zoom=3,
height=700
)
# mapbox_style="satellite-streets",
figure.layout.update(
margin={"r": 0, "t": 0, "l": 0, "b": 0},
# This takes away the colorbar on the right hand side of the plot
#coloraxis_showscale=False,
mapbox_accesstoken=token,
mapbox_style="dark",
# Center the map on Ghana, literally center of the World:
mapbox=dict(center=dict(lat=float(7.9465), lon=float(1.0232)), zoom=3,),
)
#print(figure.data[0].hovertemplate)
figure.data[0].update(
hovertemplate="<b>%{hovertext}</b><br><br>Confirmed=%{marker.color}<br>Active=%{customdata[1]}<br>Deaths=%{customdata[2]}"
)
figure.show()