-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid19.py
executable file
·155 lines (123 loc) · 5.97 KB
/
covid19.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
#!/usr/bin/env python3
""" covid19.py - small python script to generate graphs about the spread
of the coronavirus (Covis-19). """
# =========================================================================== #
# File : covid19.py #
# Purpose: Small python script to graph the Covid-19 outbreak #
# #
# Author : Harald van der Laan #
# Date : 2020-04-06 #
# Version: v1.0.3 #
# =========================================================================== #
# Changelog: #
# - v1.0.0: Initial commit (Harald van der Laan) #
# - v1.0.1: Update with more graphs (Harald van der Laan) #
# - v1.0.2: Error handling with counrty's that do not #
# exist or are miss spelled. (Harald van der Laan) #
# - v1.0.3: Update with no output for export only (Harald van der Laan) #
# =========================================================================== #
# Copyright © 2020 Harald van der Laan #
# #
# Permission is hereby granted, free of charge, to any person obtaining #
# a copy of this software and associated documentation files (the "Software") #
# to deal in the Software without restriction, including without limitation #
# the rights to use, copy, modify, merge, publish, distribute, sublicense, #
# and/or sell copies of the Software, and to permit persons to whom the #
# Software is furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included #
# in all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, #
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES #
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. #
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM #
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, #
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE #
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #
# =========================================================================== #
import sys
import json
import argparse
import requests
import matplotlib.pyplot
def get_arguments():
""" Function for getting commandline arguments """
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--country', default="Netherlands",
help="Country to generate graphs of")
parser.add_argument('-e', '--export',
help="Export graph to filename.png")
parser.add_argument('-n', '--nogui', action='store_true',
help="Do not display a graph in gui")
return parser.parse_args()
def get_plot_values(url, country):
""" Function for getting remote dataset of the coronavirus
This data set is updated every 8 hours, there is a
possibility that you are seeing old data """
response = requests.get(url)
if response.ok:
jsondata = json.loads(response.text)
else:
sys.stderr.write('Could not connect to API.\n')
sys.exit(1)
try:
result = jsondata[country]
except KeyError:
sys.stderr.write(f'Could not find country {country}.\n')
sys.exit(1)
return result
def main(args):
""" Main python function """
url = 'https://pomber.github.io/covid19/timeseries.json'
country = args.country
xvalues = []
infected = []
deaths = []
recovered = []
# ipd = infected per day, dpd = deaths per day
# rpd = recovered per day
ipd = []
dpd = []
rpd = []
counter = 0
data = get_plot_values(url, country)
for entry in data:
# x axis of the graph
xvalues.append(counter)
# y axis of the graphs
infected.append(entry['confirmed'])
deaths.append(entry['deaths'])
recovered.append(entry['recovered'])
if counter == 0:
ipd.append(data[counter]['confirmed'] - 0)
dpd.append(data[counter]['deaths'] - 0)
rpd.append(data[counter]['recovered'] - 0)
else:
ipd.append(data[counter]['confirmed'] - data[counter - 1]['confirmed'])
dpd.append(data[counter]['deaths'] - data[counter - 1]['deaths'])
rpd.append(data[counter]['recovered'] - data[counter - 1]['recovered'])
counter += 1
# Creating plots for the graphs.
fig, ((gr1, gr2), (gr3, gr4), (gr5, gr6)) = matplotlib.pyplot.subplots(3, 2, figsize=(14, 7))
fig.suptitle(f'Covid-19 in {args.country}')
gr1.plot(xvalues, infected, '-ob')
gr1.set_title('Totaal aantal besmettingen')
gr2.plot(xvalues, ipd, '-ob')
gr2.set_title('Besmettingen per dag')
gr3.plot(xvalues, deaths, '-or')
gr3.set_title('Totaal aantal doden')
gr4.plot(xvalues, dpd, '-or')
gr4.set_title('Doden per dag')
gr5.plot(xvalues, recovered, '-og')
gr5.set_title('Totaal aantal genezingen')
gr6.plot(xvalues, rpd, '-og')
gr6.set_title('Genezingen per dag')
if args.export:
# argument -e is provided save graph to file
matplotlib.pyplot.savefig(args.export)
if not args.nogui:
matplotlib.pyplot.show()
if __name__ == "__main__":
ARGS = get_arguments()
main(ARGS)