-
Notifications
You must be signed in to change notification settings - Fork 0
/
NYTCovid.py
265 lines (235 loc) · 9.55 KB
/
NYTCovid.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# NYTCovid class definition
# Dr. Tirthajyoti Sarkar, Fremont, CA
# March-April 2020
import numpy as np
import pandas as pd
import io
import requests
import matplotlib.pyplot as plt
import seaborn as sns
import time
import datetime
class NYTCovid:
def __init__(self):
from datetime import date
self.statedf = None
self.countydf = None
self._countryupdated = False
self._countyupdated = False
self._processed = False
self._today = date.today()
def today(self):
print("Today is:", self._today)
def updateState(self,
url="https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv"):
url = url
s = requests.get(url).content
self.statedf = pd.read_csv(io.StringIO(s.decode('utf-8')))
self.statedf['date'] = pd.to_datetime(self.statedf['date'], format='%Y-%m-%d')
self._countryupdated = True
def updateCounty(self,
url="https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv"):
url = url
s = requests.get(url).content
self.countydf = pd.read_csv(io.StringIO(s.decode('utf-8')))
self.countydf['date'] = pd.to_datetime(self.countydf['date'], format='%Y-%m-%d')
self._countyupdated = True
def dateUpdate(self):
if self._countryupdated:
print("Date of the latest data:", self.statedf.iloc[-1]['date'].date())
else:
print("Data has not been updated even once!")
def peek(self):
if self._countryupdated:
print("First 5 rows of the state data")
print("=" * 50)
print(self.statedf.head())
if self._countyupdated:
print()
print("First 5 rows of the county data")
print("=" * 50)
print(self.countydf.head())
def process(self):
pd.set_option('mode.chained_assignment', None)
self.countrydict = {}
self.countydict = {}
print("Processing...")
t1 = time.time()
if self._countryupdated:
self.statelist = list(self.statedf['state'].unique())
for s in self.statelist:
state_df = self.statedf[self.statedf['state'] == s]
state_df['newcases'] = state_df['cases'].diff()
state_df['deaths'] = state_df['deaths'].diff()
self.countrydict[s] = state_df
if self._countyupdated:
self.countylist = list(self.countydf['county'].unique())
for c in self.countylist:
county_df = self.countydf[self.countydf['county'] == c]
county_df['newcases'] = county_df['cases'].diff()
county_df['newdeaths'] = county_df['deaths'].diff()
self.countydict[c] = county_df
self._processed = True
t2 = time.time()
delt = round(t2 - t1, 3)
print("Finished. Took {} seconds".format(delt))
def plot_state(self,
state='New York',
last_30_days=False):
"""
Plots statewise data
"""
if self._processed == False:
print("Data not processed yet. Cannot plot statewise.")
return None
s = str(state)
assert s in self.statelist, "Input does not appear in the list of states. Possibly wrong name/spelling"
df = self.countrydict[s]
dates = df['date']
cases = df['cases']
deaths = df['deaths']
newcases = df['newcases']
#newdeaths = df['newdeaths']
if last_30_days:
dates = df['date'][-31:-1]
cases = df['cases'][-31:-1]
deaths = df['deaths'][-31:-1]
newcases = df['newcases'][-31:-1]
newdeaths = df['newdeaths'][-31:-1]
plt.figure(figsize=(14, 4))
if last_30_days:
plt.title("Cumulative cases in {}, for last 30 days".format(s), fontsize=18)
else:
plt.title("Cumulative cases in {}".format(s), fontsize=18)
#plt.bar(x=dates.values, height=cases, color='blue', edgecolor='k')
plt.bar(dates.values, cases, color='blue', edgecolor='k')
plt.xticks(rotation=45, fontsize=14)
plt.show()
print()
plt.figure(figsize=(14, 4))
if last_30_days:
plt.title("Cumulative deaths in {}, for last 30 days".format(s), fontsize=18)
else:
plt.title("Cumulative deaths in {}".format(s), fontsize=18)
plt.bar(dates, height=deaths, color='red', edgecolor='k')
plt.xticks(rotation=45, fontsize=14)
plt.show()
print()
plt.figure(figsize=(14, 4))
if last_30_days:
plt.title("New cases in {}, for last 30 days".format(s), fontsize=18)
else:
plt.title("New cases in {}".format(s), fontsize=18)
plt.bar(dates, height=newcases, color='yellow', edgecolor='k')
plt.xticks(rotation=45, fontsize=14)
plt.show()
print()
plt.figure(figsize=(14, 4))
if last_30_days:
plt.title("New deaths in {}, for last 30 days".format(s), fontsize=18)
else:
plt.title("New deaths in {}".format(s), fontsize=18)
plt.bar(x=dates, height=newdeaths, color='orange', edgecolor='k')
plt.xticks(rotation=45, fontsize=14)
plt.show()
def plot_multi_state(self,
states=['California', 'Michigan', 'New York'],
last_30_days=False):
"""
Plots multiple states data in a single plot for comparison
"""
states = states
plt.figure(figsize=(14, 4))
if last_30_days:
plt.title("Cumulative cases, for last 30 days", fontsize=18)
colors = []
for s in states:
color = tuple(np.round(np.random.random(3), 2))
colors.append(color)
plt.plot(self.countrydict[s]['date'][-31:-1],
self.countrydict[s]['cases'][-31:-1],
color=color,
linewidth=2)
plt.xticks(rotation=45, fontsize=14)
plt.legend(states, fontsize=14)
plt.show()
else:
plt.title("Cumulative cases", fontsize=18)
colors = []
for s in states:
color = tuple(np.round(np.random.random(3), 2))
colors.append(color)
plt.plot(self.countrydict[s]['date'],
self.countrydict[s]['cases'],
color=color,
linewidth=2)
plt.xticks(rotation=45, fontsize=14)
plt.legend(states, fontsize=14)
plt.show()
def rankState(self,
N=5,
daterank=None):
"""
Ranks the states in a bar chart
Arguments:
N: Top N states to be ranked
date: Date at which the ranking is done.
Must be a string in the form '2020-3-27'
"""
from datetime import date
cases = {}
deaths = {}
newcases = {}
newdeaths = {}
if daterank == None:
d = self.statedf.iloc[-1]['date'].date()
else:
d = datetime.datetime.strptime(daterank, '%Y-%m-%d').date()
for s in self.countrydict:
df = self.countrydict[s]
for i in range(len(df)):
if df['date'].iloc[i].date() == d:
cases[s] = df.iloc[i]['cases']
deaths[s] = df.iloc[i]['deaths']
newcases[s] = df.iloc[i]['newcases']
newdeaths[s] = df.iloc[i]['newdeaths']
sorted_cases = sorted(((value, key) for (key, value) in cases.items()), reverse=True)
sorted_cases = sorted_cases[:N]
sorted_deaths = sorted(((value, key) for (key, value) in deaths.items()), reverse=True)
sorted_deaths = sorted_deaths[:N]
sorted_newcases = sorted(((value, key) for (key, value) in newcases.items()), reverse=True)
sorted_newcases = sorted_newcases[:N]
sorted_newdeaths = sorted(((value, key) for (key, value) in newdeaths.items()), reverse=True)
sorted_newdeaths = sorted_newdeaths[:N]
_, axs = plt.subplots(2, 2, figsize=(15, 9))
axs = axs.ravel()
axs[0].bar([val[1] for val in sorted_cases],
height=[val[0] for val in sorted_cases],
color='blue', edgecolor='k')
axs[0].set_title("Cumulative cases on {}".format(str(d)),
fontsize=15)
axs[1].bar([val[1] for val in sorted_deaths],
height=[val[0] for val in sorted_deaths],
color='red', edgecolor='k')
axs[1].set_title("Cumulative deaths on {}".format(str(d)),
fontsize=15)
axs[2].bar([val[1] for val in sorted_newcases],
height=[val[0] for val in sorted_newcases],
color='yellow', edgecolor='k')
axs[2].set_title("New cases on {}".format(str(d)),
fontsize=15)
axs[3].bar([val[1] for val in sorted_newdeaths],
height=[val[0] for val in sorted_newdeaths],
color='orange', edgecolor='k')
axs[3].set_title("New deaths on {}".format(str(d)),
fontsize=15)
plt.show()
if __name__ == '__main__':
covid = NYTCovid()
covid.dateUpdate()
covid.updateState()
covid.updateCounty()
covid.process()
covid.rankState()
covid.plot_multi_state()
covid.plot_state()