def implementation_checks(window: int = 7): df = get_dataframe() ts = get_time_series(df) I = ts.Hospitalized.values for kernel in kernels.keys(): plt.figure() plt.bar(x = ts.index, height = I, label = "raw") smoother = convolution(kernel, window) plt.plot(ts.index, smoother(I)[:-window+1], label = "smooth") plt.title(f"kernel: {kernel} (window: {window})", loc = "left") plt.show()
.xlabel("date").ylabel("cases")\ .show() # now, do district-level estimation smoothing = 10 state_cases["geo_reported"] = state_cases.geo_reported.str.strip() district_time_series = state_cases.groupby( ["geo_reported", "date_reported"])["date_reported"].count().sort_index() migration = np.zeros((len(district_names), len(district_names))) estimates = [] max_len = 1 + max(map(len, district_names)) with tqdm(etl.normalize(district_names)) as districts: for district in districts: districts.set_description(f"{district :<{max_len}}") try: (dates, Rt_pred, Rt_CI_upper, Rt_CI_lower, *_) = analytical_MPVS(district_time_series.loc[district], CI=CI, smoothing=convolution(window=smoothing), totals=False) estimates.append( (district, Rt_pred[-1], Rt_CI_lower[-1], Rt_CI_upper[-1], linear_projection(dates, Rt_pred, smoothing))) except (IndexError, ValueError): estimates.append((district, np.nan, np.nan, np.nan, np.nan)) estimates = pd.DataFrame(estimates) estimates.columns = ["district", "Rt", "Rt_CI_lower", "Rt_CI_upper", "Rt_proj"] estimates.set_index("district", inplace=True) estimates.to_csv(data / "Rt_estimates_private.csv") print(estimates)
return (release, adaptive) if __name__ == "__main__": root = cwd() data = root / "data" figs = root / "figs" # model details gamma = 0.2 prevalence = 1 total_time = 90 * days release_date = pd.to_datetime("July 28, 2020") lockdown_period = (release_date - pd.to_datetime("today")).days smoothing = convolution() states = [ "Maharashtra", "Karnataka", "Andhra Pradesh", "Tamil Nadu", "Madhya Pradesh", "Punjab", "Gujarat", "Kerala" ] # use gravity matrix for states after 2001 census new_state_data_paths = { "Telangana": (data / "telangana.json", data / "telangana_pop.csv") } # define data versions for api files paths = { "v3": ["raw_data1.csv", "raw_data2.csv"], "v4": [
plt.plot(sir_model.dT) plt.show() plt.plot(R0_timeseries, "-", color="black", label="$R_0$") plt.plot(sir_model.Rt, "-", color="dodgerblue", label="$R_t$") plt.legend(framealpha=1, handlelength=1, loc="best") plt.PlotDevice().xlabel("time").ylabel("reproductive rate").adjust(left=0.10, bottom=0.15, right=0.99, top=0.99) plt.ylim(0.5, 1.5) plt.show() # 1: parametric scheme: dates, Rt, Rt_lb, Rt_ub, *_, anomalies, anomaly_dates = analytical_MPVS( pd.DataFrame(sir_model.dT), smoothing=convolution("uniform", 2), CI=0.99, totals=False) pd = plt.Rt(dates, Rt, Rt_ub, Rt_lb, ymin = 0.5, ymax = 2.5, CI = 0.99, yaxis_colors = False, format_dates = False, critical_threshold = False)\ .xlabel("time")\ .ylabel("reproductive rate")\ .adjust(left = 0.11, bottom = 0.15, right = 0.98, top = 0.98) plt.plot(sir_model.Rt, "-", color="white", linewidth=3, zorder=10) sim_rt, = plt.plot(sir_model.Rt, "-", color="dodgerblue", linewidth=2, zorder=11) anoms = plt.vlines(anomaly_dates, 0, 4, colors="red", linewidth=2, alpha=0.5) plt.legend([pd.markers["Rt"], sim_rt, anoms], ["Estimated $R_t$ (99% CI)", "simulated $R_t$", "anomalies"],
# # pass df = load_all_data(v3_paths=[data / filepath for filepath in paths['v3']], v4_paths=[data / filepath for filepath in paths['v4']]) data_recency = str(df["date_announced"].max()).split()[0] run_date = str(pd.Timestamp.now()).split()[0] ts = get_time_series(df) #, ["detected_state", "detected_district"]) one_day = pd.Timedelta(days=1) # fig 1 infections = ts[ ts.date >= "May 01, 2020"].Hospitalized #.sum(level = 2).sort_index() smoothed = convolution("uniform") scatter = plt.scatter(infections.index[:-7], infections.values[:-7], color="#CC4C75", marker="s", s=5, alpha=0.5) lineplot, = plt.plot(infections.index[:-7], smoothed(infections.values[:-7]), color="#CC4C75", linewidth=2) plt.PlotDevice()\ .l_title("daily confirmed cases in India")\ .r_title("source:\nCOVID19India")\ .axis_labels(x = "date", y = "cases", enforce_spacing = True)\ .adjust(left = 0.10, bottom = 0.15, right = 0.98, top = 0.90)
CI = 0.95 # private data state_cases = pd.read_csv(data/"Bihar_cases_data_Jul23.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, RR_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(state_ts, CI = CI, smoothing = convolution(window = smoothing)) plot_RR_est(dates, RR_pred, RR_CI_upper, RR_CI_lower, CI, ymin=0, ymax=4)\ .title("Bihar: Reproductive Number Estimate Comparisons")\ .xlabel("Date")\ .ylabel("Rt", rotation=0, labelpad=20) plt.ylim(0, 4) # public data paths = { "v3": [data_path(_) for _ in (1, 2)], "v4": [data_path(_) for _ in range(3, 13)] } for target in paths['v3'] + paths['v4']: download_data(data, target)
.axis_labels(x = "date", y = "tests (millions)")\ .adjust(bottom = 0.16) plt.gca().xaxis.set_major_formatter(formatter) plt.show() ## b plt.plot(india_data.tpr["July 01, 2020":], color="royalblue") plt.PlotDevice()\ .l_title("test positivity rate over time")\ .axis_labels(x = "date", y = "TPR (%)")\ .adjust(bottom = 0.16) plt.gca().xaxis.set_major_formatter(formatter) plt.show() # fig 5 smoothing = convolution("uniform", window=7) plt.plot(india_data.index, smoothing(india_data["cfr_maxcor"]), color="maroon", label="10-day lag CFR (upper bound)") plt.plot( india_data.index, (smoothing(india_data["cfr_maxcor"]) + smoothing(india_data["adj_cfr"])) / 2, color="indigo", linestyle="dashed", label="averaged") plt.plot(india_data.index, smoothing(india_data["adj_cfr"]), color="navy", label="adjusted CFR (lower bound)")