-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUS_CORONA_Analysis.py
389 lines (329 loc) · 11.1 KB
/
US_CORONA_Analysis.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# -*- coding: utf-8 -*-
"""US_CORONA_Analysis.ipynb
"""
import numpy as np
import pandas as pd
import io
import requests
import matplotlib.pyplot as plt
"""In case of failure of URL,The dataset is taken from ::::
https://covidtracking.com/api
"""
url="http://covidtracking.com/api/states/daily.csv"
s=requests.get(url).content
df = pd.read_csv(io.StringIO(s.decode('utf-8')))
df['date'] = pd.to_datetime(df['date'], format='%Y%m%d')
df.drop(['dateChecked'],axis=1,inplace=True)
df['state']=df['state'].apply(str)
df.info()
df.head(10)
df.fillna(value=-1, inplace=True)
df.head(50)
"""Function to plot a bar chart -->>"""
def plot_var(var='positiveIncrease',
state='NY'):
"""
Plots a bar chart of the given variable over the date range
"""
assert type(var)==str, "Expected string as the variable name"
assert type(state)==str, "Expected string as the state name"
y = df[df['state']==state][var]
x = df[df['state']==state]['date']
plt.figure(figsize=(12,4))
plt.title("Plot of \"{}\" for {}".format(var,state),fontsize=18)
plt.bar(x=x,height=y,edgecolor='k',color='orange')
plt.grid(True)
plt.xticks(fontsize=14,rotation=45)
plt.yticks(fontsize=14)
plt.show()
plot_var()
plot_var('hospitalizedIncrease','GA')
plot_var('deathIncrease','MI')
plot_var('totalTestResultsIncrease','MA')
"""Function to plot scatter plot -->>"""
def plot_xy(varx='totalTestResultsIncrease',
vary='positiveIncrease',
state='NY'):
"""
Plots a bar chart of the given variable over the date range
"""
assert type(varx)==str, "Expected string as the variable x name"
assert type(vary)==str, "Expected string as the variable y name"
y = df[df['state']==state][vary]
x = df[df['state']==state][varx]
if (x.nunique()!=1) and (y.nunique()!=1):
plt.figure(figsize=(12,4))
plt.title("Plot of \"{}\" vs. \"{}\" for {}".format(varx,vary,state),fontsize=18)
plt.scatter(x=x,y=y,edgecolor='k',color='lightgreen',s=100)
plt.grid(True)
plt.xticks(fontsize=14,rotation=45)
plt.yticks(fontsize=14)
plt.show()
else:
print("Some of the data unavailable for a scatter plot. Sorry!")
plot_xy(state='NY')
plot_xy('hospitalized','death','GA')
"""Testing tracker function -->>"""
def plotTesting(lst_states=['NY','CA','MA','TX','PA','AL','CO']):
"""
Plots the cumulative testing done by the given list of states
"""
legends = []
plt.figure(figsize=(10,5))
plt.title("Total test results",fontsize=18)
for s in lst_states:
data = np.array(df[df['state']==s]['totalTestResults'])[-1::-1]
slope = int((data[-1]-data[0])/len(data))
plt.plot(data,linewidth=2)
plt.text(x=len(data)-2,y=data[-1]*1.05,s=s,fontsize=14)
legends.append(str(slope)+" tests/day in " + s)
plt.legend(legends,fontsize=14)
plt.grid(True)
plt.xlim(0,len(data)+2)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.xlabel("Days",fontsize=16)
plt.ylabel("Total test results",fontsize=16)
plt.show()
plotTesting()
"""Function to compute fatality ratio -->>"""
def fatality_ratio(state='CA'):
"""
Computes the fatality ratio for the given state
Fatality ratio is the ratio of total dead to total positive case
"""
date = df.iloc[0]['date']
try:
d = float(df[(df['state']==state) & (df['date']==date)]['death'])
p = float(df[(df['state']==state) & (df['date']==date)]['positive'])
except:
print("Could not retrieve the necessary information")
if (d!=-1.0) and (p!=-1.0) and (p!=0):
return round(d/p,3)
else:
return -1
"""Function to compute hospitalization ratio -->>"""
def hospitalization_ratio(state='NY'):
"""
Computes the hospitalization ratio for the given state
Hospitalization ratio is the ratio of total hospitalized to total positive case
"""
date = df.iloc[0]['date']
try:
h = float(df[(df['state']==state) & (df['date']==date)]['hospitalized'])
p = float(df[(df['state']==state) & (df['date']==date)]['positive'])
except:
print("Could not retrieve the necessary information")
if (h!=-1.0) and (p!=-1.0) and (p!=0):
return round(h/p,3)
else:
return -1
"""Function to compute positive case/total test ratio -->>"""
def positiveTest_ratio(state='NY'):
"""
Computes the test-positive ratio for the given state
Test-positive ratio is the ratio of total positive cases to total number of tests
"""
date = df.iloc[0]['date']
try:
p = float(df[(df['state']==state) & (df['date']==date)]['positive'])
t = float(df[(df['state']==state) & (df['date']==date)]['totalTestResults'])
except:
print("Could not retrieve the necessary information")
return -1
if (p!=-1.0) and (t!=-1.0) and (t!=0):
return round(p/t,3)
else:
return -1
"""Function to compute recovery ratio -->>"""
def recovery_ratio(state='NY'):
"""
Computes the recovery ratio for the given state
Recovery ratio is the ratio of total recovered cases to total positive cases
"""
date = df.iloc[0]['date']
try:
r = float(df[(df['state']==state) & (df['date']==date)]['recovered'])
p = float(df[(df['state']==state) & (df['date']==date)]['positive'])
except:
print("Could not retrieve the necessary information")
return -1
if (r!=-1.0) and (p!=-1.0) and (p!=0):
return round(r/p,3)
else:
return -1
"""Fatality ratio chart -->>"""
states = ['CA','NY','MI','MA','PA','IL','AL','CO','PR']
fr,x = [],[]
for s in states:
data = fatality_ratio(s)
if data!=-1:
fr.append(data)
x.append(s)
plt.figure(figsize=(8,4))
plt.title("Fatality ratio chart",fontsize=18)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.bar(x=x,height=fr,color='red',
edgecolor='k',linewidth=2)
plt.show()
"""Hospitalization ratio chart -->>"""
states = ['CA','NY','MI','MA','PA','IL']
hos,x = [],[]
for s in states:
data = hospitalization_ratio(s)
if data!=-1:
hos.append(data)
x.append(s)
plt.figure(figsize=(8,4))
plt.title("Hospitalization ratio chart",fontsize=18)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.bar(x=x,height=hos,color='brown',
edgecolor='k',linewidth=2)
plt.show()
"""Test-positive ratio chart -->>"""
states = ['CA','NY','MI','MA','PA','IL','CO','AL']
tp,x = [],[]
for s in states:
data = positiveTest_ratio(s)
if data!=-1:
tp.append(data)
x.append(s)
plt.figure(figsize=(8,4))
plt.title("Test-positive ratio chart",fontsize=18)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.bar(x=x,height=tp,color='blue',
edgecolor='k',linewidth=2)
plt.show()
"""Bubble charts -->>"""
states = list(df['state'].unique())
for s in ['AS','GU','MP','PU','VI']:
try:
states.remove(s)
except:
pass
fr,x = [],[]
for s in states:
data = fatality_ratio(s)
if data!=-1:
fr.append(data)
x.append(s)
fr = np.array(fr)
plt.figure(figsize=(15,7))
plt.tick_params(
axis='x', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom=False, # ticks along the bottom edge are off
top=False, # ticks along the top edge are off
labelbottom=False)
plt.title("Fatality ratio across the states",fontsize=18)
plt.scatter(x=x,y=fr,
s=4e5*fr**2,
color='orange',edgecolor='red',alpha=0.75,linewidth=2.5)
#plt.xticks(rotation=45,fontsize=12)
for i,s in enumerate(x):
plt.annotate(s=s,xy=(x[i],fr[i]))
plt.ylim(0,0.12)
plt.yticks(fontsize=16)
plt.grid(True,axis='y')
plt.show()
states = list(df['state'].unique())
for s in ['AS','GU','MP','PU','VI']:
try:
states.remove(s)
except:
pass
tp,x = [],[]
for s in states:
data = positiveTest_ratio(s)
if data!=-1:
tp.append(data)
x.append(s)
tp = np.array(tp)
plt.figure(figsize=(15,7))
plt.tick_params(
axis='x', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom=False, # ticks along the bottom edge are off
top=False, # ticks along the top edge are off
labelbottom=False)
plt.title("Test-positive ratio across the states",fontsize=18)
plt.scatter(x=x,y=tp,
s=2e4*tp**2,
color='blue',edgecolor='red',alpha=0.5,linewidth=2)
plt.xticks(rotation=90,fontsize=12)
for i,s in enumerate(x):
plt.annotate(s=s,xy=(x[i],tp[i]))
plt.ylim(0,0.6)
plt.yticks(fontsize=16)
plt.grid(True,axis='y')
plt.show()
"""Function for states having 14 days of decreasing case counts -->>"""
def caseCountsdecrease(days=14,state='NY'):
"""
Determines whether the given state has a decreasing case counts for given number of days
Arguments:
days: Number of days to go back
state: Name of the state (a string)
Returns:
A tuple containing the successive difference vector (of new cases) and
the number of negative quantities in that vector. When all the quantities are negative,
the state has shown consistent decrease in new cases for the given number of days.
"""
positiveIncrease = np.array(df[df['state']==state]['positiveIncrease'][:days+1])[-1::-1]
diff = np.diff(positiveIncrease)
countofNeg = np.sum(diff <= 0, axis=0)
return (countofNeg, diff)
states = ['CA','MI','GA','LA']
cd = []
x = np.arange(1,15,1)
plt.figure(figsize=(10,6))
plt.title("Last 14 days successive difference in new positive cases \n(more negative numbers is better)",
fontsize=18)
for s in states:
_,data = caseCountsdecrease(days=14,state=s)
plt.plot(x,data,linewidth=2)
plt.legend(states,fontsize=16,ncol=2)
plt.grid(True)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.xlabel("Last 14 days",fontsize=16)
plt.ylabel("Successive difference in new cases",fontsize=16)
plt.hlines(y=0,xmin=0,xmax=15,linestyles='--',lw=3)
plt.show()
states = ['CA']
cd = []
x = np.arange(1,15,1)
plt.figure(figsize=(10,6))
plt.title("Last 14 days successive difference in new positive cases \n(more negative numbers is better)",
fontsize=18)
for s in states:
_,data = caseCountsdecrease(days=14,state=s)
plt.plot(x,data,linewidth=2)
plt.legend(states,fontsize=16,ncol=2)
plt.grid(True)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.xlabel("Last 14 days",fontsize=16)
plt.ylabel("Successive difference in new cases",fontsize=16)
plt.hlines(y=0,xmin=0,xmax=15,linestyles='--',lw=3)
plt.show()
states = ['LA']
cd = []
x = np.arange(1,15,1)
plt.figure(figsize=(10,6))
plt.title("Last 14 days successive difference in new positive cases \n(more negative numbers is better)",
fontsize=18)
for s in states:
_,data = caseCountsdecrease(days=14,state=s)
plt.plot(x,data,linewidth=2)
plt.legend(states,fontsize=16,ncol=2)
plt.grid(True)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.xlabel("Last 14 days",fontsize=16)
plt.ylabel("Successive difference in new cases",fontsize=16)
plt.hlines(y=0,xmin=0,xmax=15,linestyles='--',lw=3)
plt.show()