# #downloads Summary Data Tables
#
# I copied the data from the table, pasted into gedit and saved it. The file
# has commas per normal rules and variable number of spaces.
import csv
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
FILENAME_CHDBC = "cases_hospitalizations_deaths_by_county.tsv"
FILENAME_WSCPOP = "WashingtonStateCounty_population_2021.csv"
VOTES_2020 = "2020_vote.tsv"
county_dict = dict()
# The fields xfer_in and xfer_out are from the Seattle Times, Sep. 30, 2021 at 6:00 am
# https://www.seattletimes.com/seattle-news/slammed-by-covid-19-statewide-system-helps-transfer-rural-washington-patients-to-available-hospital-beds/
cases_df = pd.read_csv(FILENAME_CHDBC, sep="\t")
print(cases_df)
pop_df = pd.read_csv(FILENAME_WSCPOP, sep=",")
pop_df.rename(columns={'CTYNAME':'County'}, inplace = True)
print(pop_df)
# Merge the two data frames
df = pd.merge(pop_df, cases_df, on = "County", how = "inner")
print(df)
# From https://results.vote.wa.gov/results/20201103/president-vice-president_bycounty.html
# then fixed by hand. In retrospect, it was probably faster to clean up the
# files by hand than to write software to do it for me.
election_df = pd.read_csv(VOTES_2020, sep="\t")
print(election_df)
"""
with open(VOTES_2020, "r") as f:
c = csv.DictReader(f, delimiter="\t", dialect="unix")
for row in c:
for k in ['Biden', 'Trump']:
row[k] = int(row[k])
row["Victory"] = ( row["Trump"] - row["Biden"] ) / \
(row["Trump"] + row["Biden"])
print(row)
county_dict[row['County']]["Trump"] = row["Trump"]
county_dict[row['County']]["Biden"] = row["Biden"]
county_dict[row['County']]["Victory"] = row["Victory"]
for k in county_dict:
print( k, county_dict[k] )
print("County\tcases/100\thospitalizations/100\tDeaths/100\tVictory")
for k in county_dict:
if "Unassigned" == k:
continue
try:
p = county_dict[k]["pop2021"]
except KeyError as ke:
print(f"KeyError was raised when getting p. k is {k}")
raise
print(f'{k}\t{county_dict[k]["Cases"]/p:8.5f}',
f'\t{county_dict[k]["Hospitalizations"]/p:8.5f}',
f'\t{county_dict[k]["Deaths"]/p:9.6f}',
f'\t{county_dict[k]["Victory"]:9.3f}')
"""
df = pd.merge(df, election_df, on = "County", how = "inner")
print(df.loc[:, ["County", "pop2021", "Cases", "Hospitalizations", "Deaths"]])
df["Victory"] = ( df["Trump"] - df["Biden"])/(df["Biden"] + df["Trump"])
df["Cases/100"] = df["Cases"]/df["pop2021"]
df["Hospitalizations/100"] = df["Hospitalizations"]/df["pop2021"]
df["Deaths/100"] = df["Deaths"]/df["pop2021"]
print(df.loc[:, ["County", "Victory", "Cases/100", "Hospitalizations/100", "Deaths/100"]])
# Eventually, move this to the top of the program, with the other imports
from mpl_toolkits.axes_grid1 import host_subplot
from mpl_toolkits import axisartist
cases_color = 'g'
cases_marker = "+"
hospitalizations_color = 'm'
hospitalizations_marker = '^'
deaths_color = 'r'
deaths_marker = "x"
# This method came from https://stackoverflow.com/questions/15910019/annotate-data-points-while-plotting-from-pandas-dataframe
def annotate_df(ax, row):
ax.annotate(row.name, row.values,
xytext=(10,-5),
textcoords='offset points',
size=18,
color='black')
plt.figure(figsize=(24,16))
# This comes from https://matplotlib.org/stable/gallery/axisartist/demo_parasite_axes2.html#sphx-glr-gallery-axisartist-demo-parasite-axes2-py
ax1 = host_subplot(111, axes_class=axisartist.Axes)
plt.subplots_adjust(right=0.75)
ax2 = ax1.twinx()
ax2.axis["right"].toggle(all=True)
ax3 = ax1.twinx()
ax3.axis["right"] = ax2.new_fixed_axis(loc="right", offset=(60, 0))
ax3.axis["right"].toggle(all=True)
df.plot(x="Victory", y="Cases/100", kind="scatter", ax=ax1, marker=cases_marker, color=cases_color)
ax1.set_ylabel("Cases/100 people")
df.plot(x="Victory", y="Hospitalizations/100", kind="scatter", ax=ax2, marker=hospitalizations_marker, color=hospitalizations_color)
ax2.set_ylabel("Hospitalizations/100 people")
df.plot(x="Victory", y="Deaths/100", kind="scatter", ax=ax3, marker=deaths_marker, color=deaths_color)
ax3.set_ylabel("Deaths/100 people")
for i, point in df.iterrows():
ax1.text(point['Victory'], point['Cases/100'], point['County'], size=12)
ax2.text(point['Victory'], point['Hospitalizations/100'], point['County'], size=12)
ax3.text(point['Victory'], point['Deaths/100'], point['County'], size=12)
ax1.set_xlabel("Trump's margin of victory")
ax1.axis["left"].label.set_color(cases_color)
ax2.axis["right"].label.set_color(hospitalizations_color)
ax3.axis["right"].label.set_color(deaths_color)
x_lin = list() # The starting and ending points of the linear fit
y_cases = list()
y_deaths = list()
y_hospitalizations = list()
x_lin.append( min(df.Victory) )
x_lin.append( max(df.Victory) )
m_cases, b_cases = np.polyfit(df.Victory, df["Cases/100"], 1)
m_deaths, b_deaths = np.polyfit(df.Victory, df["Deaths/100"], 1)
m_hospitalizations, b_hospitalizations = np.polyfit(df.Victory, df['Hospitalizations/100'], 1)
y_cases.append ( m_cases * x_lin[0] + b_cases )
y_cases.append ( m_cases * x_lin[1] + b_cases )
y_hospitalizations.append ( m_hospitalizations * x_lin[0] + b_hospitalizations )
y_hospitalizations.append ( m_hospitalizations * x_lin[1] + b_hospitalizations )
y_deaths.append ( m_deaths * x_lin[0] + b_deaths )
y_deaths.append ( m_deaths * x_lin[1] + b_deaths )
ax1.plot(x_lin, y_cases, color=cases_color)
ax2.plot(x_lin, y_hospitalizations, color=hospitalizations_color)
ax2.plot(x_lin, y_deaths, color=deaths_color)
plt.savefig("COVID-19_rates_as_a_function_of_Trumps_victory_in_2020.png")
plt.savefig("COVID-19_rates_as_a_function_of_Trumps_victory_in_2020.svg")
plt.show()
plt.figure(figsize=(24,16))
ax2 = host_subplot(111, axes_class=axisartist.Axes)
df.plot(x="Victory", y="Hospitalizations/100", kind="scatter", ax=ax2, marker=hospitalizations_marker, color=hospitalizations_color)
ax2.set_ylabel("Hospitalizations/100 people", color=hospitalizations_color)
for i, point in df.iterrows():
ax2.text(point['Victory'], point['Hospitalizations/100'], point['County'], size=12)
ax2.set_xlabel("Trump's margin of victory")
ax2.plot(x_lin, y_hospitalizations, color=hospitalizations_color)
plt.savefig("COVID-19_Hospitalization_rates_as_a_function_of_Trumps_victory_in_2020.png")
plt.savefig("COVID-19_Hospitalization_rates_as_a_function_of_Trumps_victory_in_2020.svg")
plt.show()
plt.figure(figsize=(24,16))
ax1 = host_subplot(111, axes_class=axisartist.Axes)
df.plot(x="Victory", y="Cases/100", kind="scatter", ax=ax1, marker=cases_marker, color=cases_color)
ax1.set_ylabel("Cases/100 people", color=cases_color)
for i, point in df.iterrows():
ax1.text(point['Victory'], point['Cases/100'], point['County'], size=12)
ax1.set_xlabel("Trump's margin of victory")
ax1.plot(x_lin, y_cases, color=cases_color)
plt.savefig("COVID-19_Cases_rates_as_a_function_of_Trumps_victory_in_2020.png")
plt.savefig("COVID-19_Cases_rates_as_a_function_of_Trumps_victory_in_2020.svg")
plt.show()
plt.figure(figsize=(24,16))
ax3 = host_subplot(111, axes_class=axisartist.Axes)
df.plot(x="Victory", y="Deaths/100", kind="scatter", ax=ax3, marker=deaths_marker, color=deaths_color)
ax3.set_ylabel("Deaths/100 people", color=deaths_color)
for i, point in df.iterrows():
ax3.text(point['Victory'], point['Deaths/100'], point['County'], size=12)
ax3.set_xlabel("Trump's margin of victory")
ax3.plot(x_lin, y_deaths, color=deaths_color)
plt.savefig("COVID-19_death_rates_as_a_function_of_Trumps_victory_in_2020.png")
plt.savefig("COVID-19_death_rates_as_a_function_of_Trumps_victory_in_2020.svg")
plt.show()
plt.figure(figsize=(24,16))
ax3 = host_subplot(111, axes_class=axisartist.Axes)
# xfer_out xfer_in
df.plot(x="Victory", y="xfer_out", kind="scatter", ax=ax3, marker=deaths_marker, color=deaths_color)
ax3.set_ylabel("Patients leaving the county", color=deaths_color)
for i, point in df.iterrows():
ax3.text(point['Victory'], point['xfer_out'], point['County'], size=12)
ax3.set_xlabel("Trump's margin of victory")
plt.savefig("COVID-19_patients_leaving_as_a_function_of_Trumps_victory_in_2020.png")
plt.savefig("COVID-19_patients_leaving_as_a_function_of_Trumps_victory_in_2020.svg")
plt.show()
plt.figure(figsize=(24,16))
ax3 = host_subplot(111, axes_class=axisartist.Axes)
# xfer_out xfer_in
df.plot(x="Victory", y="xfer_in", kind="scatter", ax=ax3, marker=deaths_marker, color="g")
ax3.set_ylabel("Patients leaving the county", color=deaths_color)
for i, point in df.iterrows():
ax3.text(point['Victory'], point['xfer_in'], point['County'], size=12)
ax3.set_xlabel("Trump's margin of victory")
plt.savefig("COVID-19_patients_arriving_as_a_function_of_Trumps_victory_in_2020.png")
plt.savefig("COVID-19_patients_arriving_as_a_function_of_Trumps_victory_in_2020.svg")
plt.show()