Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
covid masks in US states
import datetime
import requests
import requests_cache
import re
import pprint
import numpy as np
import matplotlib.pyplot as plt
pp = pprint.PrettyPrinter()
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 = '' % (state.lower())
data = requests.get(url).json()
before = []
after = []
for day in data:
day_date_str = day['date']
m ='(\d\d\d\d)(\d\d)(\d\d)', str(day_date_str))
day_dt = datetime.datetime(year=int(, month=int(, day=int(
delta = (dt - day_dt).days
if delta < 0 and delta >= -days_around:
if delta >= 0 and delta < days_around:
return before, after
def get_dates():
# Source for dates.tsv:
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('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)
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment