-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_data.py
More file actions
executable file
·158 lines (127 loc) · 5.89 KB
/
plot_data.py
File metadata and controls
executable file
·158 lines (127 loc) · 5.89 KB
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
import csv, os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
import pandas as pd
from sys import argv
import pickle
import warnings
warnings.filterwarnings("ignore")
plt.style.use('ggplot')
#os.nice(0)
#print('this is a test')
rel_path = "/".join(os.path.abspath(__file__).split('/')[:-1])
DATA_FILE= os.path.join(rel_path, 'data.csv')
def main():
try:
COUNTRY = argv[1].replace("_", " ")
LAST_DAYS = int(argv[2])
except:
COUNTRY = 'Switzerland'
LAST_DAYS = 0
with open(os.path.join(rel_path, 'continents.pkl'), 'rb') as inFile:
countries = pickle.load(inFile)
data = pd.read_csv(DATA_FILE, quotechar='"')
data.drop('Unnamed: 0', 1, inplace=True)
Days = [0, 14, 30, 60, 90]
for COUNTRY in countries:
for LAST_DAYS in Days:
print(f"Plotting {COUNTRY} {LAST_DAYS} Days")
country = data[data['Country'] == COUNTRY]
country.loc[:,'New'] = country.loc[:,'Confirmed'].diff()
country.loc[:,'7-Day'] = country.loc[:,'New'].rolling(5).mean()
country.loc[:,'Active 7 Day'] = country.loc[:,'New'].rolling(7).sum()
#print(country.tail(5))
def get_ticks(steps, xlim):
xmin, xmax = xlim
locs, stepsize = np.linspace(xmin, xmax, steps, retstep=True)
stepsize = int(np.floor(stepsize))
if stepsize==0:
stepsize = 1
ticks = country['Date'][::-1*stepsize][::-1]
return(locs, ticks)
quick_dict = {0: 'All', 14:'Last 14', 30:'Last 30', 60:'Last 60', 90:'Last 90'} # mostly just to convert 0 days to all days in title
country = country.iloc[-1*LAST_DAYS:] # this is stupid, but I don't want to type in the indexing every time
steps = 15
# New Cases Per Day
plt.scatter(country['Date'], country['New'], s=3)
plt.plot(country['Date'], country['New'], linewidth=0.5, label='New Cases Per Day')
plt.plot(country['Date'], country['7-Day'], linewidth=2, label='7 Day Rolling Average', c='green')
current = country['7-Day'].iloc[-1]
plt.plot(country['Date'], [current for i in country['7-Day']], linestyle='--', c='orange', label='Current Level')
locs, labels = get_ticks(steps, plt.xlim())
plt.xticks(locs, labels, rotation=70)
plt.legend()
plt.title(f'New Cases ({quick_dict[LAST_DAYS]} Days) - {COUNTRY}')
plt.tight_layout()
plt.savefig(os.path.join(rel_path, f'Images/{COUNTRY}_NewCases_{LAST_DAYS}Days'))
#plt.show()
plt.clf()
# Active Cases
plt.plot(country['Date'], country['Active 7 Day'], label='Active Cases (7 Day)')
current = country['Active 7 Day'].iloc[-1]
plt.plot(country['Date'], [current for i in country['Active 7 Day']], linestyle='--', label='Current Level')
locs, labels = get_ticks(steps, plt.xlim())
plt.xticks(locs, labels, rotation=70)
plt.title(f'Active Cases ({quick_dict[LAST_DAYS]} Days) - {COUNTRY}')
plt.legend()
plt.tight_layout()
plt.savefig(os.path.join(rel_path, f'Images/{COUNTRY}_ActiveCases_{LAST_DAYS}Days'))
#plt.show()
plt.clf()
# Total Cases
fig, ax = plt.subplots()
ax.semilogy(country['Date'], country['Confirmed'], label='Total Cases')
ax.semilogy(country['Date'], country['Recovered'], label='Recovered', c='green')
ax.grid(b=True, which='major', axis='both')
locs, labels = get_ticks(steps, plt.xlim())
ax.set_xticks(locs)
ax.set_xticklabels(labels, rotation=70)
plt.title(f'Total Cases ({quick_dict[LAST_DAYS]} Days) (Logarithmic) - {COUNTRY}')
ax.legend(loc='upper left')
'''
ax2 = ax.twinx()
ax2.semilogy(country['Date'], country['Deaths'], label='Deaths', c='black')
ax2.legend(loc='lower right')
#ax2.yaxis.set_minor_formatter(mticker.ScalarFormatter())
ax2.set_ylabel('Deaths')
'''
plt.tight_layout()
plt.savefig(os.path.join(rel_path, f'Images/{COUNTRY}_TotalCases_{LAST_DAYS}Days'))
#plt.show()
plt.clf()
# Deaths
fig, ax = plt.subplots()
ax.semilogy(country['Date'], country['Deaths'], label='Deaths', c='black')
ax.grid(b=True, which='major', axis='both')
locs, labels = get_ticks(steps, plt.xlim())
ax.set_xticks(locs)
ax.set_xticklabels(labels, rotation=70)
plt.title(f'Total Deaths ({quick_dict[LAST_DAYS]} Days) (Logarithmic) - {COUNTRY}')
#ax2.yaxis.set_minor_formatter(mticker.ScalarFormatter())
ax.legend()
plt.tight_layout()
plt.savefig(os.path.join(rel_path, f'Images/{COUNTRY}_Deaths_{LAST_DAYS}Days'))
#plt.show()
plt.clf()
plt.cla()
# The Numbers
columnLabels = ['Confirmed', 'Recovered', 'Deaths', 'New', 'Active 7 Day']
fig, ax = plt.subplots()
# hide axes
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
df = pd.DataFrame(country[columnLabels][-10:].astype(int), columns=columnLabels)
row_Labels = country['Date'][-10:].astype(str)
ax.table(cellText=df.values, colLabels=df.columns, rowLabels=row_Labels.values, loc='center')
fig.tight_layout()
plt.tight_layout()
plt.savefig(os.path.join(rel_path, f'Images/{COUNTRY}_RawTable'))
plt.clf()
plt.cla()
return
if __name__ == "__main__":
main()