Last active
November 12, 2020 23:12
-
-
Save ortutay/159fcb05abd3199a6c1cdc82db2f944b to your computer and use it in GitHub Desktop.
covid masks in US states
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import requests | |
import requests_cache | |
import re | |
import pprint | |
import numpy as np | |
import matplotlib.pyplot as plt | |
pp = pprint.PrettyPrinter() | |
requests_cache.install_cache('c') | |
us_state_abbrev = { | |
'Alabama': 'AL', | |
'Alaska': 'AK', | |
'American Samoa': 'AS', | |
'Arizona': 'AZ', | |
'Arkansas': 'AR', | |
'California': 'CA', | |
'Colorado': 'CO', | |
'Connecticut': 'CT', | |
'Delaware': 'DE', | |
'District of Columbia': 'DC', | |
'Florida': 'FL', | |
'Georgia': 'GA', | |
'Guam': 'GU', | |
'Hawaii': 'HI', | |
'Idaho': 'ID', | |
'Illinois': 'IL', | |
'Indiana': 'IN', | |
'Iowa': 'IA', | |
'Kansas': 'KS', | |
'Kentucky': 'KY', | |
'Louisiana': 'LA', | |
'Maine': 'ME', | |
'Maryland': 'MD', | |
'Massachusetts': 'MA', | |
'Michigan': 'MI', | |
'Minnesota': 'MN', | |
'Mississippi': 'MS', | |
'Missouri': 'MO', | |
'Montana': 'MT', | |
'Nebraska': 'NE', | |
'Nevada': 'NV', | |
'New Hampshire': 'NH', | |
'New Jersey': 'NJ', | |
'New Mexico': 'NM', | |
'New York': 'NY', | |
'North Carolina': 'NC', | |
'North Dakota': 'ND', | |
'Northern Mariana Islands':'MP', | |
'Ohio': 'OH', | |
'Oklahoma': 'OK', | |
'Oregon': 'OR', | |
'Pennsylvania': 'PA', | |
'Puerto Rico': 'PR', | |
'Rhode Island': 'RI', | |
'South Carolina': 'SC', | |
'South Dakota': 'SD', | |
'Tennessee': 'TN', | |
'Texas': 'TX', | |
'Utah': 'UT', | |
'Vermont': 'VT', | |
'Virgin Islands': 'VI', | |
'Virginia': 'VA', | |
'Washington': 'WA', | |
'West Virginia': 'WV', | |
'Wisconsin': 'WI', | |
'Wyoming': 'WY' | |
} | |
def get_state_population(state_name): | |
f = open('pop.tsv') | |
for line in f: | |
n, pop = line.split('\t') | |
n = n.strip() | |
pop = pop.strip().replace(',', '') | |
print(n, pop) | |
if n == state_name: | |
return int(pop) | |
raise Exception(state_name) | |
def get_data(state, dt, days_around, field): | |
# print(state, dt, field) | |
print('.', end='') | |
url = 'https://api.covidtracking.com/v1/states/%s/daily.json' % (state.lower()) | |
data = requests.get(url).json() | |
before = [] | |
after = [] | |
for day in data: | |
day_date_str = day['date'] | |
m = re.search(r'(\d\d\d\d)(\d\d)(\d\d)', str(day_date_str)) | |
day_dt = datetime.datetime(year=int(m.group(1)), month=int(m.group(2)), day=int(m.group(3))) | |
delta = (dt - day_dt).days | |
if delta < 0 and delta >= -days_around: | |
before.append(day[field]) | |
if delta >= 0 and delta < days_around: | |
after.append(day[field]) | |
return before, after | |
def get_dates(): | |
# Source for dates.tsv: https://masks4all.co/what-states-require-masks/ | |
f = open('dates.tsv') | |
dates = {} | |
pops = {} | |
for line in f: | |
# print(line) | |
state, _, _, date_str = line.split('\t') | |
parts = [int(x) for x in date_str.split('/')] | |
dt = datetime.datetime(year=parts[2], month=parts[0], day=parts[1]) | |
dates[us_state_abbrev[state]] = dt | |
pops[us_state_abbrev[state]] = get_state_population(state) | |
return dates, pops | |
def run(): | |
dates, pops = get_dates() | |
# print(dates) | |
cases_total_before, cases_total_after = 0, 0 | |
death_total_before, death_total_after = 0, 0 | |
n = 35 | |
cases_series = np.array([0.] * n * 2) | |
death_series = np.array([0.] * n * 2) | |
for state, dt in dates.items(): | |
cases_before, cases_after = get_data(state, dt, n, 'positiveIncrease') | |
death_before, death_after = get_data(state, dt, n, 'deathIncrease') | |
cases_total_before += sum(cases_before) / pops[state] | |
cases_total_after += sum(cases_after) / pops[state] | |
death_total_before += sum(death_before) / pops[state] | |
death_total_after += sum(death_after) / pops[state] | |
state_cases_series = np.array(cases_before + cases_after) / float(pops[state]) | |
state_death_series = np.array(death_before + death_after) / float(pops[state]) | |
cases_series += state_cases_series | |
death_series += state_death_series | |
plt.plot(state_cases_series * len(dates), alpha=.1, c='g') | |
plt.plot(state_death_series * len(dates) * 15, alpha=.1, c='b') | |
print('--') | |
print('cases_total_before', cases_total_before) | |
print('cases_total_after ', cases_total_after) | |
print('death_total_before', death_total_before) | |
print('death_total_after ', death_total_after) | |
print(cases_series) | |
plt.plot(cases_series, label='cases per pop') | |
plt.plot(death_series * 30, label='deaths per pop') | |
plt.axvline(x=n, c='r', label='mask mandate') | |
plt.title('cases and deaths per population %s days \nbefore/after mask mandate in US states' % n) | |
plt.show() | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment