-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
163 lines (142 loc) · 5.03 KB
/
main.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
import sys
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def load_data():
#Data taken from: https://github.com/CSSEGISandData/COVID-19.git
df = pd.read_csv("../COVID-19/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")
df = df.rename(columns={'Country/Region': 'country'})
df.columns = df.columns[:4].to_list() + pd.to_datetime(df.columns[4:]).to_list()
return df
def filter_by_country(country, df):
return df[df['country'] == country]
def keep_only_data_columns(df):
#column 3 is the Longitude column
return df.iloc[:,4:]
def get_only_metadata_columns(df):
return df.iloc[:, :4]
def add_avg_speed_column(df):
# TODO:filter out days before the first infection in the country
num_days = len(df.columns) - 4
last_column = df.columns[len(df.columns) - 1];
speed_column = df.apply(lambda df: df[last_column] / num_days, axis = 1)
df.insert(4, "speed", speed_column)
return df
def create_deltas_dataframe(df):
pad_column = df.apply(lambda x: 0, axis = 1)
shifted_df = df.copy()
shifted_df.insert(0, "pad", pad_column)
trimmed_df = shifted_df.iloc[:,:-1]
trimmed_df.columns = df.columns
deltas_df = df - trimmed_df
return deltas_df
def chart_country_data(df, country, title, filename=None):
row = filter_by_country(country, df)
row = keep_only_data_columns(row)
print(row)
row_data = row.T
# set ggplot style
# plt.style.use('ggplot')
fig, ax = plt.subplots(figsize=(15,7))
ax.set_title(title)
ax.set_ylabel('Count')
ax.set_xlabel('Date')
# set ticks every week
ax.xaxis.set_major_locator(mdates.WeekdayLocator())
# # # # format date
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
ax.bar(row_data.index, row_data.iloc[:, 0])
if (filename is None):
plt.show()
else:
plt.savefig(filename)
def write_html(country):
f = open('{0}.html'.format(country), 'w')
header = '''<html>
<header> <title> Charts from Pandas </title> </header>
<style>
.row {
display: flex;
flex-wrap: wrap;
padding: 0 4px;
}
/* Create four equal columns that sits next to each other */
.column {
flex: 40%;
max-width: 40%;
padding: 0 4px;
}
.column img {
margin-top: 8px;
vertical-align: middle;
width: 100%;
}
/* Responsive layout - makes a two column-layout instead of four columns */
@media screen and (max-width: 800px) {
.column {
flex: 40%;
max-width: 40%;
}
}
/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
.column {
flex: 100%;
max-width: 100%;
}
}
}
</style>
<body>
Here are some charts I made by myself.
'''
str = ''
str += '<div class="row"<div class="column"><img src="abs_{0}.png" alt="absolute"></div></div>\n'.format(
country)
str += '<div class="row"><div class="column"><img src="vel_{0}.png" alt="velocity"></div>\n'.format(country)
str += '<div class="column"><img src="acc_{0}.png" alt="acceleration"></div></div>\n'.format(country)
footer = '''
</body>
</html>
'''
f.write(header + str + footer)
f.close()
if __name__ == '__main__':
test_deltas = 0
if (test_deltas):
test_deltas = pd.DataFrame( {"a" : [4 ,5, 6],
"b" : [7, 8, 9],
"c" : [10, 11, 12]
})
print(test_deltas)
create_deltas_dataframe(test_deltas)
df = load_data()
print(keep_only_data_columns(df))
md_cols = get_only_metadata_columns(df)
print("Calculating Velocity DF")
velocity_df = create_deltas_dataframe(keep_only_data_columns(df))
print("Calculating Accelaration DF")
accelaration_df = create_deltas_dataframe(velocity_df)
velocity_df = pd.concat([md_cols, velocity_df], axis=1)
accelaration_df = pd.concat([md_cols, accelaration_df], axis=1)
country = 'Israel'
if (len(sys.argv) >= 2):
country = sys.argv[1]
chart_type = None
if (len(sys.argv) >= 3):
chart_type = sys.argv[2]
if (chart_type is None or chart_type == "abs"):
title = 'Total infected COVID-19 over time in {0}'.format(country)
# Graph the absolute numbers:
chart_country_data(df, country, title, 'abs_{0}'.format(country))
#title += '\nAverage infected per day in {0} is {1} '.format(country, speed)
if (chart_type is None or chart_type == "vel"):
title = 'Velocity: Infection rate over time of COVID-19 in {0}'.format(country)
# Graph the velocity:
chart_country_data(velocity_df, country, title, 'vel_{0}'.format(country))
if (chart_type is None or chart_type == "acc"):
title = 'Acceleration: Change in infection rate over time of COVID-19 in {0}'.format(country)
# Graph the accelaration:
chart_country_data(accelaration_df, country, title, 'acc_{0}'.format(country))
write_html(country)
print("DONE :-)")