plt.plot(0.2093 * df[state][:, "delta", "tested"], label="national test-scaled") plt.plot(scale_factor * df[state][:, "delta", "tested"], label="state test-scaled") plt.plot(df[state][:, "delta", "confirmed"], label="confirmed") plt.legend() plt.PlotDevice().title(f"\n{state} / case scaling comparison").xlabel( "\ndate").ylabel("cases\n") plt.show() # I vs D estimators gamma = 0.2 window = 7 * days CI = 0.95 smooth = notched_smoothing(window) (dates_I, Rt_I, Rtu_I, Rtl_I, *_) = analytical_MPVS(df[state][:, "delta", "confirmed"], CI=CI, smoothing=smooth, totals=False) (dates_D, Rt_D, Rtu_D, Rtl_D, *_) = analytical_MPVS(df[state][:, "delta", "deceased"], CI=CI, smoothing=smooth, totals=False) plt.Rt(dates_I, Rt_I, Rtu_I, Rtl_I, CI)\ .title(f"{state} - $R_t(I)$ estimator") plt.figure()
gamma = 0.2 window = 10 CI = 0.95 state_cases = pd.read_csv(data/"Bihar_cases_data_Oct03.csv", parse_dates=["date_reported", "date_status_change"], dayfirst=True) state_cases["geo_reported"] = state_cases.geo_reported.str.strip() state_cases = state_cases[state_cases.date_reported <= "2020-09-30"] state_ts = state_cases["date_reported"].value_counts().sort_index() district_ts = state_cases.groupby(["geo_reported", "date_reported"])["date_reported"].count().sort_index() districts, pops, migrations = etl.district_migration_matrix(data/"Migration Matrix - District.csv") districts = sorted([etl.replacements.get(dn, dn) for dn in districts]) R_mandatory = dict() for district in districts:#district_ts.index.get_level_values(0).unique(): try: (_, Rt, *_) = analytical_MPVS(district_ts.loc[district], CI = CI, smoothing = notched_smoothing(window = 10), totals = False) Rm = np.mean(Rt) except ValueError as v: Rm = 1.5 R_mandatory[district] = Rm R_voluntary = {district: 1.2*R for (district, R) in R_mandatory.items()} si, sf = 0, 10 simulation_results = [ run_policies(state_cases, pops, districts, migrations, gamma, R_mandatory, R_voluntary, lockdown_period = lockdown_period, total = total_time, seed = seed) for seed in tqdm(range(si, sf)) ] plt.simulations(
run_date = str(pd.Timestamp.now()).split()[0] ts = get_time_series(df, "detected_state") states = ["Maharashtra", "Bihar", "Delhi", "Andhra Pradesh", "Telangana", "Tamil Nadu", "Madhya Pradesh"] for state in states: print(state) print(" + running estimation...") ( dates, Rt_pred, RR_CI_upper, RR_CI_lower, T_pred, T_CI_upper, T_CI_lower, total_cases, new_cases_ts, anomalies, anomaly_dates ) = analytical_MPVS(ts.loc[state].Hospitalized, CI = CI, smoothing = notched_smoothing(window = smoothing), totals = False) estimates = pd.DataFrame(data = { "dates": dates, "Rt_pred": Rt_pred, "RR_CI_upper": RR_CI_upper, "RR_CI_lower": RR_CI_lower, "T_pred": T_pred, "T_CI_upper": T_CI_upper, "T_CI_lower": T_CI_lower, "total_cases": total_cases[2:], "new_cases_ts": new_cases_ts, }) print(" + Rt today:", Rt_pred[-1]) # plt.Rt(dates, Rt_pred, RR_CI_lower, RR_CI_upper, CI)\ # .ylabel("Estimated $R_t$")\
import pandas as pd import shapely from adaptive.estimators import analytical_MPVS, linear_projection from adaptive.etl.commons import download_data from adaptive.models import SIR, NetworkedSIR from adaptive.policy import simulate_adaptive_control, simulate_lockdown from adaptive.smoothing import notched_smoothing from adaptive.utils import days, setup, weeks, million from tqdm import tqdm logger = getLogger("SULSEL") CI = 0.95 gamma = 0.2 window = 5 smoothing = notched_smoothing(window = window) # load case data schema = { "Date Symptom Onset" : "symptom_onset", "Date of Hospital Admissions": "admission", "Date tested" : "tested", "Date of positive test result": "confirmed", "Date Recovered" : "recovered", "Date Died" : "died", "Kebupaten/Kota" : "regency", "Kecamatan" : "district", "age " : "age" }
CI = 0.95 state_cases = pd.read_csv(data / "Bihar_cases_data_Oct03.csv", parse_dates=["date_reported"], dayfirst=True) state_ts = state_cases["date_reported"].value_counts().sort_index() district_names, population_counts, _ = etl.district_migration_matrix( data / "Migration Matrix - District.csv") populations = dict(zip(district_names, population_counts)) # first, look at state level predictions (dates, Rt_pred, Rt_CI_upper, Rt_CI_lower, T_pred, T_CI_upper, T_CI_lower, total_cases, new_cases_ts, anomalies, anomaly_dates) = analytical_MPVS( state_ts, CI=CI, smoothing=notched_smoothing(window=smoothing), totals=False) plt.Rt(dates, Rt_pred[1:], Rt_CI_upper[1:], Rt_CI_lower[1:], CI, ymin=0, ymax=4)\ .title("\nBihar: Reproductive Number Estimate")\ .annotate(f"data from {str(dates[0]).split()[0]} to {str(dates[-1]).split()[0]}")\ .xlabel("date")\ .ylabel("$R_t$", rotation=0, labelpad=20)\ .show() np.random.seed(33) Bihar = SIR("Bihar", 99_000_000, dT0=T_pred[-1], Rt0=Rt_pred[-1], lower_CI=T_CI_lower[-1],
from adaptive.etl.commons import download_data from adaptive.etl.covid19india import state_code_lookup from adaptive.models import SIR from adaptive.smoothing import notched_smoothing sns.set(style="whitegrid") # root = Path(__file__).parent # data = root/"data" data = Path("./data").resolve() CI = 0.95 window = 14 gamma = 0.2 infectious_period = 5 smooth = notched_smoothing(window) num_sims = 50000 # load admin data on population IN_age_structure = { # WPP2019_POP_F01_1_POPULATION_BY_AGE_BOTH_SEXES 0: 116_880, 5: 117_982 + 126_156 + 126_046, 18: 122_505 + 117_397, 30: 112_176 + 103_460, 40: 90_220 + 79_440, 50: 68_876 + 59_256 + 48_891, 65: 38_260 + 24_091, 75: 15_084 + 8_489 + 3_531 + 993 + 223 + 48, } # normalize age_structure_norm = sum(IN_age_structure.values())
import pandas as pd from adaptive.smoothing import notched_smoothing from adaptive.estimators import analytical_MPVS import adaptive.plots as plt CI = 0.95 gamma = 0.2 window = 3 smoothing = notched_smoothing(window=window) schema = { "Date Symptom Onset": "symptom_onset", "Date of Hospital Admissions": "admission", "Date tested": "tested", "Date of positive test result": "confirmed", "Date Recovered": "recovered", "Date Died": "died", "Kebupaten/Kota": "regency", "Kecamatan": "district", "age ": "age" } regency_names = { 'Pangkep': 'Pangkajene Dan Kepulauan', 'Pare-Pare': 'Parepare', 'Selayar': 'Kepulauan Selayar', 'Sidrap': 'Sidenreng Rappang' }
age_structure_norm = sum(IN_age_structure.values()) IN_age_ratios = np.array( [v / age_structure_norm for (k, v) in IN_age_structure.items()]) split_by_age = lambda v: (v * IN_age_ratios).astype(int) # get age-specific prevalence from KA sero KA = pd.read_stata("data/ka_cases_deaths_time_newagecat.dta") KA.agecat = KA.agecat.where( KA.agecat != 85, 75) # we don't have econ data for 85+ so combine 75+ and 85+ categories KA_agecases = KA.groupby(["agecat", "date"])["patientcode"]\ .count().sort_index().rename("cases")\ .unstack().fillna(0).stack() KA_ts = KA_agecases.sum(level=1) (dates, Rt_pred, Rt_CI_upper, Rt_CI_lower, T_pred, T_CI_upper, T_CI_lower, total_cases, new_cases_ts, anomalies, anomaly_dates) = analytical_MPVS(KA_ts, notched_smoothing(5)) COVID_age_ratios = (KA_agecases.sum(level=0) / KA_agecases.sum()).values split_by_prevalence = lambda v: (v * IN_age_ratios).astype(int) for seed in range(10): model = AgeStructured("KA", 6.11e7, 857, 915345, 1.826, diag(u) @ C, IN_age_ratios, COVID_age_ratios, seed) counter = 0 while model.dT[-1].sum() > 0: model.forward_epi_step() counter += 1 print(seed, counter, model.dT)
import adaptive.plots as plt from adaptive.smoothing import notched_smoothing import pandas as pd import seaborn as sns sns.set_style("whitegrid", {'axes.grid': False}) smoothed = notched_smoothing(window=7) mobility = pd.read_csv("data/2020_IN_Region_Mobility_Report.csv", parse_dates=["date"]) stringency = pd.read_csv("data/OxCGRT_latest.csv", parse_dates=["Date"]) def plot_mobility( series, label, stringency=None, until=None, annotation="Google Mobility Data; baseline mobility measured from Jan 3 - Feb 6" ): plt.plot(series.date, smoothed( series.retail_and_recreation_percent_change_from_baseline), label="Retail/Recreation") plt.plot(series.date, smoothed( series.grocery_and_pharmacy_percent_change_from_baseline), label="Grocery/Pharmacy") plt.plot(series.date, smoothed(series.parks_percent_change_from_baseline),
.set_axis(dkij.columns.str.lower(), 1)\ .assign( district = dkij.district.str.title(), subdistrict = dkij.subdistrict.apply(lambda name: next((k for (k, v) in replacements.items() if name in v), name)), ) district_cases = dkij.groupby(["district", "date_positiveresult"])["id"].count().sort_index() districts = sorted(dkij.district.unique()) migration = np.zeros((len(districts), len(districts))) R_mandatory = dict() R_voluntary = dict() max_len = 1 + max(map(len, districts)) with tqdm(districts) as progress: for district in districts: progress.set_description(f"{district :<{max_len}}") (dates, RR_pred, *_) = analytical_MPVS(district_cases.loc[district], CI = CI, smoothing = notched_smoothing(window = window), totals=False) Rt = pd.DataFrame(data = {"Rt": RR_pred[1:]}, index = dates) R_mandatory[district] = np.mean(Rt[(Rt.index > "April 1, 2020") & (Rt.index < "June 1, 2020")])[0] R_voluntary[district] = np.mean(Rt[(Rt.index < "April 1, 2020")])[0] pops = [ 2_430_410, 910_381, 2_164_070, 2_817_994, 1_729_444, 23_011 ] gdf = gpd.read_file("data/gadm36_IDN_shp/gadm36_IDN_2.shp")\ .query("NAME_1 == 'Jakarta Raya'")\
run_date = str(pd.Timestamp.now()).split()[0] ts_full = get_time_series(df, "detected_state") ts = ts_full.query("status_change_date <= 'October 14, 2020'") states = ["Bihar", "Maharashtra", "Punjab", "Tamil Nadu"][-1:] codes = ["BR", "MH", "PN", "TN"][-1:] pops = [99.02e6, 114.2e6, 27.98e6, 67.86e6][-1:] Rt_ranges = [(0.9, 2.4), (0.55, 2.06), (0.55, 2.22), (0.84, 1.06)][-1:] windows = [7, 14, 7, 10][-1:] for (state, code, pop, Rt_range, smoothing) in zip(states, codes, pops, Rt_ranges, windows): print(state) print(" + running estimation...") state_ts_full = pd.Series(data = notched_smoothing(window = smoothing)(ts_full.loc[state].Hospitalized), index = ts_full.loc[state].Hospitalized.index) (dates, Rt_pred, RR_CI_upper, RR_CI_lower, T_pred, T_CI_upper, T_CI_lower, total_cases, new_cases_ts, anomalies, anomaly_dates)\ = analytical_MPVS(ts.loc[state].Hospitalized, CI = CI, smoothing = lambda x:x, totals = False) Rt = pd.DataFrame({"Rt": Rt_pred}, index = dates) Rt_m = np.mean(Rt[(Rt.index >= "31 March, 2020") & (Rt.index <= "17 May, 2020")])[0] Rt_v = np.mean(Rt[(Rt.index < "31 March, 2020")])[0] print(" + Rt today:", Rt_pred[-1]) print(" + Rt_m :", Rt_m) print(" + Rt_v :", Rt_v) historical = pd.DataFrame({"smoothed": new_cases_ts}, index = dates) plt.Rt(dates, Rt_pred, RR_CI_lower, RR_CI_upper, CI)\ .ylabel("$R_t$")\ .xlabel("date")\ .title(f"\n{state}: Reproductive Number Estimate")\ .annotate(f"public data from {str(dates[0]).split()[0]} to {str(dates[-1]).split()[0]}")\