def _get_model_data(state='NC', type='SIR', sds=0): """ Get model output data :param state: name of the state to return data for :param type: 'SIR' or 'Hospital Use' or 'Hospital Census' :param sds: social distance rate within [0, 1] with 0 meaning no social distancing and 1 meaning maximal social distancing :return: """ n = states[state] census = pd.read_csv("data/census.csv") pops = census[["NAME", "POPESTIMATE2019"]] nms = pops["NAME"] t_recovery = 23 conf, dead, rec = (pd.DataFrame.from_dict(get_state_level(x)).drop( ["Lat", "Long", "Country/Region"], axis=1) for x in get_hopkins()) # the zeros mess up our slope state_conf = conf[conf['Province/State'] == n] data = state_conf.drop("Province/State", axis=1) data = data.iloc[0] data = data.loc[data != 0] state_growth = get_slope(data) td = doubling_time(state_growth) new_names = {i: v for i, v in enumerate(conf["Province/State"])} c_tot, d_tot, r_tot = (x.sum(1).rename(new_names) for x in [conf, dead, rec]) state_curve = {} N = pops[pops["NAME"] == n]["POPESTIMATE2019"].values[0] I = c_tot[n] R = r_tot[n] D = d_tot[n] model = PennDeath(N, I, R, D, 0, contact_reduction=sds, t_double=td, recover_time=t_recovery) curve, occ = model.sir(60) sir = { k: v for k, v in curve.items() if k in ["susceptible", "infected", "recovered", "dead"] } hosp_use = { k: v for k, v in curve.items() if k not in ["susceptible", "infected", "recovered", "dead"] } state_curve = { "SIR": sir, "Hospital Use": hosp_use, "Hospital Census": occ } return state_curve
def plot_penn(Pdp: PennDeath, n_days: int) -> None: curve, admissions = Pdp.sir(n_days) fig, ax = plt.subplots(1, 3, figsize=(15, 5)) for k, v in curve.items(): if k not in tx.rates.keys(): ax[0].plot(v, label=k) ax[0].legend() else: ax[1].plot(v, label=k) ax[1].legend() ax[1].set_title("Hospital Resource Usage") ax[0].set_title("SIR curve") for k, v in admissions.items(): ax[2].plot(v, label=k) ax[2].legend() ax[2].set_title("Additional Resource Usage by day") fig.suptitle(f"No social distancing, total deaths = {int(max(curve['dead']))}") plt.show()
d_tot["United States"] = d_tot.sum() r_tot["United States"] = r_tot.sum() pops[pops["NAME"] == "United States"]["POPESTIMATE2019"].values[0] out = {} sds = [0, 0.2, 0.5, 0.7] for n in names: state_curve = {} for s in sds: N = pops[pops["NAME"] == n]["POPESTIMATE2019"].values[0] I = c_tot[n] R = r_tot[n] D = r_tot[n] td = d_times[n] model = PennDeath(N, I, R, D, 0, t_double=td, recover_time=t_recovery) curve, occ = model.sir(60) sir = { k: v for k, v in curve.items() if k in ["susceptible", "infected", "recovered"] } hosp_use = { k: v for k, v in curve.items() if k not in ["susceptible", "infected", "recovered"] } state_curve[s] = { "SIR": sir, "Hospital Use": hosp_use, "Hospital Census": occ
# Third party modules import matplotlib import matplotlib.pyplot as plt # First party modules from comodels import PennDeath matplotlib.use("tkagg") plt.style.use("seaborn-paper") tx = PennDeath( 330460832, 14250, 125, 205, 57, death_rate=0.01, birth_rate=0, contact_reduction=0.0 ) def plot_penn(Pdp: PennDeath, n_days: int) -> None: curve, admissions = Pdp.sir(n_days) fig, ax = plt.subplots(1, 3, figsize=(15, 5)) for k, v in curve.items(): if k not in tx.rates.keys(): ax[0].plot(v, label=k) ax[0].legend() else: ax[1].plot(v, label=k) ax[1].legend() ax[1].set_title("Hospital Resource Usage") ax[0].set_title("SIR curve") for k, v in admissions.items(): ax[2].plot(v, label=k) ax[2].legend() ax[2].set_title("Additional Resource Usage by day")
r_tot["United States"] = r_tot.sum() pops[pops["NAME"] == "United States"]["POPESTIMATE2019"].values[0] out = {} sds = list(np.round(np.arange(0, 1, 0.1), decimals=1)) for n in names: state_curve = {} for s in sds: N = pops[pops["NAME"] == n]["POPESTIMATE2019"].values[0] I = c_tot[n] R = r_tot[n] D = r_tot[n] td = d_times[n] model = PennDeath(N, I, R, D, 0, t_double=td, recover_time=t_recovery, contact_reduction=s) curve, occ = model.sir(60) sir = { k: v for k, v in curve.items() if k in ["susceptible", "infected", "recovered"] } hosp_use = { k: v for k, v in curve.items() if k not in ["susceptible", "infected", "recovered"] } state_curve[s] = {
import comodels help(comodels) help(comodels.PennDeath) help(comodels.Penn) # import the penn model import matplotlib.pyplot as plt from comodels import PennDeath help(PennDeath) tx = PennDeath(N=28304596, I=223, R=0, D=3, D_today=2) help(PennDeath.sir) def plot_penn(Pdp: PennDeath, n_days: int) -> None: # predict the coming storm and plot it curve, admissions = Pdp.sir(n_days) fig, ax = plt.subplots(1, 3, figsize=(15, 5)) for k, v in curve.items(): if k not in Pdp.rates.keys(): ax[0].plot(v, label=k) ax[0].legend() else: ax[1].plot(v, label=k) ax[1].legend() ax[1].set_title('Hospital Resource Usage') ax[0].set_title('SIR curve') for k, v in admissions.items(): ax[2].plot(v, label=k) ax[2].legend()