Skip to content

Instantly share code, notes, and snippets.

@tomatosoupcan
Created March 22, 2021 20:25
Show Gist options
  • Save tomatosoupcan/57d4d5f657fe06b0ffcbb21d273c6e0e to your computer and use it in GitHub Desktop.
Save tomatosoupcan/57d4d5f657fe06b0ffcbb21d273c6e0e to your computer and use it in GitHub Desktop.
Scrapes Vaccines Data for Michigan and Plots It
from selenium import webdriver
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
np.set_printoptions(suppress=True)
def moving_avg(x, n):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[n:] - cumsum[:-n]) / float(n)
def polyfit(x, y, degree):
results = {}
coeffs = np.polyfit(x, y, degree)
p = np.poly1d(coeffs)
#calculate r-squared
yhat = p(x)
ybar = np.sum(y)/len(y)
ssreg = np.sum((yhat-ybar)**2)
sstot = np.sum((y - ybar)**2)
results['r_squared'] = ssreg / sstot
return results
def rundata(titleappend, url):
driver = webdriver.Chrome()
driver.get(url)
acd = driver.execute_script("return acd_data")
bcd = driver.execute_script("return bcd_data")
driver.close()
acd_arr = np.array([])
bcd_arr = np.array([])
for value in acd:
if value["cv"] is None:
acd_arr = np.append(acd_arr,int(0))
else:
acd_arr = np.append(acd_arr,int(value["cv"]))
for value in bcd:
if value["cv"] is None:
bcd_arr = np.append(bcd_arr,int(0))
else:
bcd_arr = np.append(bcd_arr,int(value["cv"]))
combo_vac = acd_arr+bcd_arr
vac_increase = np.array([])
prevval = combo_vac[0]
for value in combo_vac:
vac_increase = np.append(vac_increase,value-prevval)
prevval = value
vac_ma = moving_avg(vac_increase, 7)
fig, axs = plt.subplots(1,3, figsize=(19,5))
fig.suptitle(titleappend + ' Covid Data')
x = np.arange(0,combo_vac.size)
y = combo_vac
axs[0].set_title("Vaccinations Since December 12th")
axs[0].plot(x, y, color = "red", linewidth = 3)
axs[0].set(ylabel='Total Vaccinations', xlabel='Days Since the 12th')
model = np.poly1d(np.polyfit(x,y,1))
model2 = np.poly1d(np.polyfit(x,y,2))
polyline = np.linspace(0,combo_vac.size,50)
mod1 = str(round(model[1],2))+'x + '+str(round(model[0],2))
mod2 = str(round(model2[2],2))+'x^2 + '+str(round(model2[1],2))+'x + '+str(round(model2[0],2))
axs[0].plot(polyline, model(polyline), dashes=[6, 2], label='Linear\n'+'y = '+mod1+'\nr^2 = ' + str(round(polyfit(x,y,1)['r_squared'],3)), color="green")
axs[0].plot(polyline, model2(polyline), dashes=[2, 2, 10, 2], label='Quadratic\n'+'y = '+mod2+'\nr^2 = ' + str(round(polyfit(x,y,2)['r_squared'],3)), color="blue")
axs[0].legend()
x = np.arange(0,30)
y = combo_vac[-30:]
axs[1].set_title("Vaccinations Last 30 Days")
axs[1].plot(x, y, color = "red", linewidth = 3)
axs[1].set(ylabel='Total Vaccinations', xlabel='Days')
model = np.poly1d(np.polyfit(x,y,1))
model2 = np.poly1d(np.polyfit(x,y,2))
polyline = np.linspace(0,30,50)
mod1 = str(round(model[1],2))+'x + '+str(round(model[0],2))
mod2 = str(round(model2[2],2))+'x^2 + '+str(round(model2[1],2))+'x + '+str(round(model2[0],2))
axs[1].plot(polyline, model(polyline), dashes=[6, 2], label='Linear\n'+'y = '+mod1+'\nr^2 = ' + str(round(polyfit(x,y,1)['r_squared'],3)), color="green")
axs[1].plot(polyline, model2(polyline), dashes=[2, 2, 10, 2], label='Quadratic\n' +'y = '+mod2+'\nr^2 = ' + str(round(polyfit(x,y,2)['r_squared'],3)), color="blue")
axs[1].legend()
x = np.arange(0,vac_ma.size)
y = vac_ma
axs[2].set_title("Moving Average Vaccinations Per Day (7 day range)")
axs[2].plot(x, y, color = "red", linewidth = 3)
axs[2].set(ylabel='Average Vaccinations', xlabel='Time')
model = np.poly1d(np.polyfit(x,y,1))
polyline = np.linspace(0,vac_ma.size,50)
mod1 = str(round(model[1],2))+'x + '+str(round(model[0],2))
axs[2].plot(polyline, model(polyline), dashes=[6, 2], label='Linear\n'+'y = '+mod1+' \nr^2 = ' + str(round(polyfit(x,y,1)['r_squared'],3)), color="green")
axs[2].legend()
fig.canvas.set_window_title(titleappend + ' Covid Data')
plt.show()
#rundata('Oakland County',"https://data.lansingstatejournal.com/covid-19-vaccine-tracker/michigan/oakland-county-mi/26125/")
#rundata('Macomb County',"https://data.lansingstatejournal.com/covid-19-vaccine-tracker/michigan/macomb-county-mi/26099/")
rundata('Michigan',"https://data.lansingstatejournal.com/covid-19-vaccine-tracker/michigan/26/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment