-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
179 lines (140 loc) · 4.42 KB
/
utils.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
import pandas as pd
import plotly.graph_objects as go
import numpy as np
from datetime import datetime
from pandas import ExcelWriter
from pandas import ExcelFile
def get_xl_sheets(file, nbr_of_sheets=6):
"""Returns a list of DataFrames where each DataFrame is a sheet of the excel
file at `file`.
Parameters
----------
file : string
Filename of, or path to, excel file.
nbr_of_sheets : type
Number of sheets to extract from the excel file. Must be equal to or
lower than the actual number of sheets in the file.
Returns
-------
List
List of Dataframes.
"""
sheets = []
for ii in range(nbr_of_sheets):
sheet = pd.read_excel(file, ii)
sheets.append(sheet)
return sheets
width = 2
def get_frame(name):
url = (
'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/'
f'csse_covid_19_time_series/time_series_covid19_{name}_global.csv')
return pd.read_csv(url, index_col='Country/Region')
def process_df(df):
"""Process DataFrame read from COVID-19 database
Parameters
----------
df : pandas.DataFrame
Returns
-------
pandas.DataFrame
processed DataFrame
"""
df.pop('Province/State')
df.pop('Lat')
df.pop('Long')
data_dict = {}
for country in df.index.unique():
if len(df.loc[country].shape) > 1:
sum = df.loc[country].sum()
else:
sum = df.loc[country]
data_dict[country] = sum
return pd.DataFrame(data_dict)
def get_df(file_path):
df = pd.read_csv(file_path, index_col='Country/Region')
df = process_df(df)
return df
def datetimeify(ind):
date_list = []
for ii, stamp in enumerate(ind):
if len(stamp.split('/')[0]) == 1:
stamp = '0' + stamp
try:
date = datetime.strptime(stamp, '%m/%d/%Y')
except ValueError:
date = datetime.strptime(stamp, '%m/%d/%y')
date_list.append(date)
return date_list
def total_vs_time(df, descr):
date_list = datetimeify(df.index)
# Create figure
fig = go.Figure()
# Add traces, one for each slider step
for ii, country in enumerate(df.keys()):
if country == 'Sweden':
width = 4
else:
width = 2
fig.add_trace(
go.Scatter(
line=dict(width=width),
name=str(country),
x=date_list,
y=df[country]))
fig.update_layout(yaxis_type="log",
xaxis_title='Date',
yaxis_title='Covid-19 {}'.format(descr),
title='Covid-19 {}'.format(descr))
fig.update_layout(
autosize=False,
width=950,
height=750,)
fig.show()
def new_vs_total(df, descr, window=1):
# Create figure
fig = go.Figure()
# Add traces, one for each slider step
for ii, country in enumerate(df.keys()):
if country == 'Sweden':
width = 4
else:
width = 2
fig.add_trace(
go.Scatter(
line=dict(width=width),
name=str(country),
x=df[country].rolling(window=window).mean(),
y=df.diff()[country].rolling(window=window).mean()))
fig.update_layout(yaxis_type="log",
xaxis_type='log',
yaxis_title='New {} per day'.format(descr),
xaxis_title='Total {}'.format(descr),
title='Covid-19 {} rolling mean of {} days'.format(descr, window))
fig.update_layout(
autosize=False,
width=950,
height=750,)
fig.show()
def new_vs_time(df, descr, window=1):
date_list = datetimeify(df.index)
# Create figure
fig = go.Figure()
# Add traces, one for each slider step
for ii, country in enumerate(df.keys()):
fig.add_trace(
go.Scatter(
line=dict(width=width),
mode='lines+markers',
name=str(country),
x=date_list[39:],
y=df.iloc[39:].diff()[country].rolling(window).mean()))
fig.update_layout(
yaxis_title='New {} per day'.format(descr),
xaxis_title='Date',
title='Covid-19 new {} rolling mean of {} days'.format(descr, window))
fig.update_layout(
autosize=False,
width=950,
height=750,)
fig.show()