def main(country="Italy", province=None, file_prefix="ita"): """ Run scenario analysis template. Args: country (str): country name pronvince (str or None): province name or None (country level) file_prefix (str): prefix of the filenames """ # This script works with version >= 2.18.0-beta print(cs.get_version()) # Create output directory in example directory code_path = Path(__file__) input_dir = code_path.parent.with_name("input") output_dir = code_path.with_name("output").joinpath(f"{code_path.stem}_{file_prefix}") output_dir.mkdir(exist_ok=True, parents=True) filer = cs.Filer(output_dir, prefix=file_prefix, numbering="01") # Load datasets data_loader = cs.DataLoader(input_dir) jhu_data = data_loader.jhu() population_data = data_loader.population() # Extra datasets oxcgrt_data = data_loader.oxcgrt() # Start scenario analysis snl = cs.Scenario(country=country, province=province) snl.register(jhu_data, population_data, extras=[oxcgrt_data]) # Show records record_df = snl.records(**filer.png("records")) record_df.to_csv(**filer.csv("records", index=False)) # Show S-R trend snl.trend(**filer.png("trend")) print(snl.summary()) # Parameter estimation snl.estimate(cs.SIRF) snl.estimate_accuracy("10th", name="Main", **filer.png("estimate_accuracy")) # Score of parameter estimation metrics = ["MAE", "MSE", "MSLE", "RMSE", "RMSLE"] for metric in metrics: metric_name = metric.rjust(len(max(metrics, key=len))) print(f"{metric_name}: {snl.score(metric=metric)}") # Reproduction number in past phases snl.history("Rt", **filer.png("history_rt_past")) # Main scenario: parameters not changed snl.add(name="Main", days=60) snl.simulate(name="Main", **filer.png("simulate_main")) snl.history_rate(name="Main", **filer.png("history-rate_main")) # Forecast scenario: Short-term prediction with regression and OxCGRT data fit_dict = snl.fit(delay=(7, 31), name="Forecast") fit_dict.pop("coef").to_csv(**filer.csv("forecast_coef", index=True)) del fit_dict["dataset"], fit_dict["intercept"] print(fit_dict) snl.predict(name="Forecast") snl.adjust_end() snl.simulate(name="Forecast", **filer.png("simulate_forecast")) snl.history_rate(name="Main", **filer.png("history-rate_forecast")) # Parameter history for item in ["Rt", "rho", "sigma", "Confirmed", "Infected", "Recovered", "Fatal"]: snl.history(item, **filer.png(f"history_{item}")) # Summary snl.summary().to_csv(**filer.csv("summary", index=True))
def main(): warnings.simplefilter("error") # Create output directory in example directory code_path = Path(__file__) input_dir = code_path.parent.with_name("input") output_dir = code_path.with_name("output").joinpath(code_path.stem) output_dir.mkdir(exist_ok=True, parents=True) # Load datasets data_loader = cs.DataLoader(input_dir) jhu_data = data_loader.jhu() population_data = data_loader.population() # Japan dataset japan_data = data_loader.japan() jhu_data.replace(japan_data) # Set country name country = "Japan" abbr = "jpn" figpath = functools.partial(filepath, output_dir=output_dir, country=abbr, ext="jpg") # Start scenario analysis snl = cs.Scenario(jhu_data, population_data, country) # Show records record_df = snl.records(filename=figpath("records")) save_df(record_df, "records", output_dir, abbr, use_index=False) # Show S-R trend snl.trend(filename=figpath("trend")) print(snl.summary()) # Parameter estimation snl.estimate(cs.SIRF) snl.history("Rt", filename=figpath("history_rt_past")) # Add future phase to main scenario snl.add(name="Main", days=7) # Short-term prediction with linear regression and OxCGRT data oxcgrt_data = data_loader.oxcgrt() snl.fit_predict(oxcgrt_data, name="Forecast") # Simulation of the number of cases sim_df = snl.simulate(name="Main", filename=figpath("simulate")) save_df(sim_df, "simulate", output_dir, abbr, use_index=False) # Parameter history for item in [ "Rt", "rho", "sigma", "Confirmed", "Infected", "Recovered", "Fatal" ]: snl.history(item, filename=figpath(f"history_{item.lower()}")) # Change rate of parameters in main scenario snl.history_rate(name="Main", filename=figpath("history_rate")) snl.history_rate(params=["kappa", "sigma", "rho"], name="Main", filename=figpath("history_rate_without_theta")) # Save summary as a CSV file summary_df = snl.summary() save_df(summary_df, "summary", output_dir, abbr) # Score of main scenario metrics_list = ["MAE", "MSE", "MSLE", "RMSE", "RMSLE"] for metrics in metrics_list: metrics_name = metrics.rjust(len(max(metrics_list, key=len))) print(f"{metrics_name}: {snl.score(metrics=metrics)}")
def model(self): params = {} #Storing the parameter values as a dictionary ("country" : [Parameters]) for i in range(self.n): if len(self.countries) > 1: snl = cs.Scenario(self.jhu_data,self.population_data,country=self.countries[i],province=None,tau=1440) else: snl = cs.Scenario(self.jhu_data,self.population_data,countries=self.countries[0],province=self.states[i],tau=1440) past_date = (datetime.datetime.now()-datetime.timedelta(days=40)).strftime("%d%b%Y") snl.first_date = past_date #Defining the first date of data to be 40 days ago. snl.trend(show_figure=False) snl.disable(phases=["0th"]) #Ignoring the first phase as we are starting the data at an arbitrary point. snl.estimate(cs.SIRD,timeout=60) #Setting max time to be a minute so that it does not run too long pars = [snl.get("rho","last"),snl.get("sigma","last"), snl.get("kappa","last")] if len(self.countries) > 1: params[self.countries[i]] = pars else: params[self.states[i]] = pars return params
def createScenario(self, countryName, provinceName, jhu_data, population_data, oxcgrt_data, pcr_data, vaccine_data): print('Creating Scenario for ' + countryName) snl = cs.Scenario(country=countryName, province=provinceName) snl.interactive = True print('Registering data in scenario for ' + countryName) # snl.register(jhu_data, population_data, extras=[oxcgrt_data, pcr_data, vaccine_data]) snl.register(jhu_data, population_data, extras=[oxcgrt_data, vaccine_data]) # snl.register(jhu_data, population_data) return snl
def main(): print(cs.__version__) # Data loading data_loader = cs.DataLoader("input") jhu_data = data_loader.jhu(verbose=True) population_data = data_loader.population(verbose=True) # For Japan japan_data = data_loader.japan() jhu_data.replace(japan_data) print(japan_data.citation) # Records snl = cs.Scenario(jhu_data, population_data, country="Japan") snl.records(filename="records.jpg") # S-R trend analysis snl.trend(filename="trend.jpg") # Separate 0th phase snl.separate("01Apr2020") md(snl, "trend.md", name="Main") # Parameter estimation snl.estimate(cs.SIRF) est_cols = ["Start", "End", "Rt", *cs.SIRF.PARAMETERS, "RMSLE"] md(snl, "estimate.md", columns=est_cols, name="Main") # New snl.clear(name="New", template="Main") cols = ["Start", "End", "Rt", *cs.SIRF.PARAMETERS] md(snl, "new1.md", columns=cols) snl.delete(name="New") md(snl, "new2.md", columns=cols) # Main snl.add(end_date="31Dec2020", name="Main") md(snl, "main1.md", columns=cols) snl.add(days=100, name="Main") md(snl, "main2.md", columns=cols) # Medicine snl.clear(name="Medicine", template="Main") snl.add(end_date="31Dec2020", name="Medicine") sigma_last = snl.get("sigma", phase="last", name="Main") sigma_med = sigma_last * 2 print(round(sigma_last, 3), round(sigma_med, 3)) snl.add(sigma=sigma_med, days=100, name="Medicine") md(snl, "med1.md", columns=cols, name="Medicine") snl.add(days=30, name="Medicine") snl.delete(phases=["last"], name="Medicine") md(snl, "med2.md", columns=cols, name="Medicine") # History snl.history("sigma", filename="sigma.jpg") snl.history("Rt", filename="rt.jpg") snl.history("Infected", filename="infected.jpg") with open("describe.md", "w") as fh: fh.write(snl.describe().to_markdown()) with open("simulate.md", "w") as fh: fh.write(snl.track().tail().to_markdown())
def main(): print(cs.__version__) # Data loading data_loader = cs.DataLoader("input") jhu_data = data_loader.jhu(verbose=True) population_data = data_loader.population(verbose=True) # For Japan japan_data = data_loader.japan() jhu_data.replace(japan_data) print(japan_data.citation) # Records snl = cs.Scenario(jhu_data, population_data, country="Japan") snl.records(filename="records.jpg") # S-R trend analysis snl.trend(filename="trend.jpg") # Summary with open("summary.md", "w") as fh: fh.write(snl.summary().to_markdown()) # Parameter estimation snl.estimate(cs.SIRF) with open("summary_estimated.md", "w") as fh: fh.write(snl.summary().to_markdown()) # Parameters with open("summary_param.md", "w") as fh: cols = ["Start", "End", "ODE", "tau", *cs.SIRF.PARAMETERS] fh.write(snl.summary(columns=cols).to_markdown()) snl.history(target="theta", filename="theta.jpg") snl.history(target="kappa", filename="kappa.jpg") snl.history(target="rho", filename="rho.jpg") snl.history(target="sigma", filename="sigma.jpg") # Day-parameters with open("summary_param_day.md", "w") as fh: cols = ["Start", "End", "ODE", "tau", *cs.SIRF.DAY_PARAMETERS] fh.write(snl.summary(columns=cols).to_markdown()) snl.history(target="1/beta [day]", filename="beta.jpg") # Rt with open("summary_rt.md", "w") as fh: cols = ["Start", "End", "ODE", "Rt"] fh.write(snl.summary(columns=cols).to_markdown()) snl.history(target="Rt", filename="rt.jpg") # Accuracy with open("summary_accuracy.md", "w") as fh: cols = ["Start", "End", "RMSLE", "Trials", "Runtime"] fh.write(snl.summary(columns=cols).to_markdown()) snl.estimate_accuracy(phase="0th", filename="accuracy_0th.jpg") snl.estimate_accuracy(phase="6th", filename="accuracy_6th.jpg")
def main(): warnings.simplefilter("error") # Create output directory in example directory code_path = Path(__file__) input_dir = code_path.parent.with_name("input") output_dir = code_path.with_name("output").joinpath(code_path.stem) output_dir.mkdir(exist_ok=True, parents=True) # Load datasets data_loader = cs.DataLoader(input_dir) jhu_data = data_loader.jhu() population_data = data_loader.population() # Japan dataset japan_data = data_loader.japan() jhu_data.replace(japan_data) # Set country name country = "Japan" abbr = "jpn" figpath = functools.partial( filepath, output_dir=output_dir, country=abbr, ext="jpg") # Start scenario analysis snl = cs.Scenario(jhu_data, population_data, country) # Show records record_df = snl.records(filename=figpath("records")) record_df.to_csv(output_dir.joinpath("jpn_records.csv"), index=False) # Show S-R trend snl.trend(filename=figpath("trend")) print(snl.summary()) # Parameter estimation snl.estimate(cs.SIRF) snl.history("Rt", filename=figpath("history_rt_past")) # Add future phase to main scenario snl.add(name="Main", end_date="31Mar2021") # Simulation of the number of cases sim_df = snl.simulate(name="Main", filename=figpath("simulate")) sim_df.to_csv(output_dir.joinpath("jpn_simulate.csv"), index=False) # Parameter history for item in ["Rt", "rho", "sigma", "Infected"]: snl.history(item, filename=figpath(f"history_{item.lower()}")) # Change rate of parameters in main snl snl.history_rate(name="Main", filename=figpath("history_rate")) snl.history_rate( params=["kappa", "sigma", "rho"], name="Main", filename=figpath("history_rate_without_theta")) # Save summary as a CSV file summary_df = snl.summary() summary_df.to_csv(output_dir.joinpath("summary.csv"), index=True)
def compareglobal(): # Download datasets data_loader = cs.DataLoader("input") population_data = data_loader.population() jhu_data = data_loader.jhu() country = "Japan" # Temporary train_global = preparedataset() models = [cs.SIR, cs.SIRF, cs.SIRD] dataframes = [] for model in models: jhu_data = cs.JHUData.from_dataframe(train_global) s = cs.Scenario(jhu_data, population_data, country=country) s.add(days=35) s.trend(show_figure=False) TRAIN_UP_TO = pd.to_datetime('2020-10-01') summary = s.summary() summary["Start_dt"] = pd.to_datetime(summary["Start"], format="%d%b%Y") summary["End_dt"] = pd.to_datetime(summary["End"], format="%d%b%Y") query = summary[summary["End_dt"] > TRAIN_UP_TO] all_phases = query.index.tolist() s.combine(phases=all_phases) target_date = datetime.datetime.strftime(TRAIN_UP_TO - datetime.timedelta(days=1), format="%d%b%Y") s.separate(target_date) summary = s.summary() all_phases = summary.index.tolist() s.disable(phases=all_phases[:-1]) s.enable(phases=all_phases[-1:]) df = s.estimate(model=model) display(s.summary()) dataframes.append(df) return df
def preparedataset(): # Download datasets data_loader = cs.DataLoader("input") population_data = data_loader.population() jhu_data = data_loader.jhu() train = jhu_data.cleaned() countries = train["Country"].unique() total_data = [] days_moving_average = 3 for country in countries: try: s = cs.Scenario(jhu_data, population_data, country=country) diff = s.records_diff(variables=["Confirmed"], window=days_moving_average, show_figure=False) d = s.record_df # Add country name and number of new confirmed cases d["Country"] = country d["New Confirmed"] = diff.reset_index()["Confirmed"] d = d[:-3] total_data.append(d) except: print(country + " not found") train_df = pd.concat(total_data) train_global = train_df.groupby("Date").sum().reset_index() train_global.plot(x="Date", y="Infected") train_global.plot(x="Date", y="New Confirmed") # Prepare dataset train_global["Country"] = country train_global["Province"] = "-" train_global = train_global.drop(["Susceptible", "New Confirmed"], axis=1) return train_global
def main(): warnings.simplefilter("error") # Create output directory in example directory code_path = Path(__file__) input_dir = code_path.parent.with_name("input") output_dir = code_path.with_name("output").joinpath(code_path.stem) output_dir.mkdir(exist_ok=True, parents=True) # Load datasets data_loader = cs.DataLoader(input_dir) jhu_data = data_loader.jhu() population_data = data_loader.population() # Japan dataset japan_data = data_loader.japan() jhu_data.replace(japan_data) # Set country name country = "Japan" abbr = "jpn".lower() figpath = functools.partial(filepath, output_dir=output_dir, country=abbr, ext="jpg") # Start scenario analysis snl = cs.Scenario(jhu_data, population_data, country) # Retrospective analysis snl.retrospective("01Sep2020", model=cs.SIRF, control="Main", target="Retrospective") print(snl.summary()) # Parameter history for item in ["Rt", "theta", "rho", "sigma", "kappa", "Infected"]: snl.history(item, filename=figpath(f"history_{item.lower()}")) # Change rate of parameters in main snl snl.history_rate(name="Main", filename=figpath("history_rate")) # Save summary as a CSV file summary_df = snl.summary() summary_df.to_csv(output_dir.joinpath("summary.csv"), index=True)
def main(): print(cs.__version__) # Data loading data_loader = cs.DataLoader("input") jhu_data = data_loader.jhu(verbose=True) population_data = data_loader.population(verbose=True) # For Japan japan_data = data_loader.japan() jhu_data.replace(japan_data) print(japan_data.citation) # Records snl = cs.Scenario(jhu_data, population_data, country="Japan") df = snl.records(show_figure=False) with open("records.md", "w") as fh: fh.write(df.tail().to_markdown()) snl.records(filename="records.jpg") # S-R trend analysis snl.trend(filename="trend.jpg") # Summary with open("summary.md", "w") as fh: fh.write(snl.summary().to_markdown()) # Phase-dependent Rt snl.estimate(cs.SIRF) snl.history(target="Rt", filename="rt.jpg")
def main(): warnings.simplefilter("error") # Create output directory in example directory code_path = Path(__file__) input_dir = code_path.parent.with_name("input") output_dir = code_path.with_name("output").joinpath(code_path.stem) output_dir.mkdir(exist_ok=True, parents=True) # Load datasets data_loader = cs.DataLoader(input_dir) jhu_data = data_loader.jhu() population_data = data_loader.population() # Set country name country = "Italy" abbr = "ita" figpath = functools.partial(filepath, output_dir=output_dir, country=abbr, ext="jpg") # Start scenario analysis snl = cs.Scenario(jhu_data, population_data, country, tau=120) # Show records record_df = snl.records(filename=figpath("records")) record_df.to_csv(output_dir.joinpath("ita_records.csv"), index=False) # Show S-R trend) snl.trend(filename=figpath("trend")) print(snl.summary()) # Parameter estimation snl.estimate(cs.SIRF) # Show the history of optimization snl.estimate_history(phase="1st", filename=figpath("estimate_history_1st")) # Show the accuracy as a figure df = snl.summary() for phase in df.index: snl.estimate_accuracy(phase=phase, filename=figpath(f"estimate_accuracy_{phase}")) # Add future phase to main scenario snl.add(name="Main", end_date="31Mar2021") snl.add(name="Main", days=100) # Add future phase to alternative scenario sigma_4th = snl.get("sigma", phase="last") sigma_6th = sigma_4th * 2 snl.clear(name="Medicine", template="Main") snl.add(name="Medicine", end_date="31Mar2021") snl.add(name="Medicine", days=100, sigma=sigma_6th) # Simulation of the number of cases sim_df = snl.simulate(name="Main", filename=figpath("simulate")) sim_df.to_csv(output_dir.joinpath("ita_simulate.csv"), index=False) # Summary summary_df = snl.summary() summary_df.to_csv(output_dir.joinpath("ita_summary.csv"), index=True) # Description of scenarios desc_df = snl.describe() desc_df.to_csv(output_dir.joinpath("ita_describe.csv"), index=True) # Tracking for main scenario track_df = snl.track() track_df.to_csv(output_dir.joinpath("ita_track.csv"), index=True) # Compare scenarios for item in ["Rt", "rho", "sigma", "Infected"]: snl.history(item, filename=figpath(f"history_{item.lower()}")) # Change rate of parameters in main scenario snl.history_rate(name="Main", filename=figpath("history_rate"))
def main(): # Create output directory in example directory code_path = Path(__file__) input_dir = code_path.parent.with_name("input") output_dir = code_path.with_name("output").joinpath(code_path.stem) output_dir.mkdir(exist_ok=True, parents=True) # Create data loader instance data_loader = cs.DataLoader(input_dir) # Load JHU dataset and replace Japan data with government-announced data jhu_data = data_loader.jhu() japan_data = data_loader.japan() jhu_data.replace(japan_data) # Load Population dataset population_data = data_loader.population() # Start scenario analysis ita_scenario = cs.Scenario(jhu_data, population_data, "Italy") # Show records ita_record_df = ita_scenario.records( filename=output_dir.joinpath("ita_records.png")) ita_record_df.to_csv(output_dir.joinpath("ita_records.csv"), index=False) # Show S-R trend ita_scenario.trend(filename=output_dir.joinpath("ita_trend.png")) # Find change points ita_scenario.trend(n_points=4, filename=output_dir.joinpath("ita_change_points.png")) print(ita_scenario.summary()) # Hyperparameter estimation ita_scenario.estimate(cs.SIRF) # Show the history of optimization ita_scenario.estimate_history( phase="1st", filename=output_dir.joinpath("ita_estimate_history_1st.png")) # Show the accuracy as a figure ita_scenario.estimate_accuracy( phase="1st", filename=output_dir.joinpath("ita_estimate_accuracy_1st.png")) # Add future phase to main scenario ita_scenario.add_phase(name="Main", end_date="01Aug2020") ita_scenario.add_phase(name="Main", end_date="31Dec2020") ita_scenario.add_phase(name="Main", days=100) # Add future phase to alternative scenario sigma_4th = ita_scenario.get("sigma", phase="4th") sigma_6th = sigma_4th * 2 ita_scenario.add_phase(name="New medicines", end_date="31Dec2020", sigma=sigma_6th) ita_scenario.add_phase(name="New medicines", days=100) # Prediction of the number of cases sim_df = ita_scenario.simulate( name="Main", filename=output_dir.joinpath("ita_simulate.png")) sim_df.to_csv(output_dir.joinpath("ita_simulate.csv"), index=False) # Save summary as a CSV file summary_df = ita_scenario.summary() summary_df.to_csv(output_dir.joinpath("ita_summary.csv"), index=True) # Parameter history ita_scenario.param_history( targets=["Rt"], name="Main", divide_by_first=False, bix_plot=False, filename=output_dir.joinpath("ita_param_history_rt.png")) ita_scenario.param_history( targets=["rho", "sigma"], name="New medicines", divide_by_first=True, filename=output_dir.joinpath("ita_param_history_rho_sigma.png")) desc_df = ita_scenario.describe() desc_df.to_csv(output_dir.joinpath("ita_description.csv"), index=True)
#!/usr/bin/env python # -*- coding: utf-8 -*- # Demonstration # Use this codes in the top directory import covsirphy as cs # Load datasets data_loader = cs.DataLoader("input") jhu_data = data_loader.jhu(verbose=False) population_data = data_loader.population(verbose=False) # Set country name scenario = cs.Scenario(jhu_data, population_data, country="Italy") # Show records _ = scenario.records() # Scenario analysis scenario.trend() scenario.summary() # Parameter estimation with SIR-F model scenario.estimate(cs.SIRF) scenario.param_history(targets=["Rt"], divide_by_first=False).T # Simulation scenario.add_phase(end_date="01Jan2021") _ = scenario.simulate()
def main(): print(cs.__version__) # Data loading data_loader = cs.DataLoader("input") jhu_data = data_loader.jhu(verbose=True) population_data = data_loader.population(verbose=True) # For Japan japan_data = data_loader.japan() jhu_data.replace(japan_data) print(japan_data.citation) # Records snl = cs.Scenario(jhu_data, population_data, country="Japan") snl.records(filename="records.jpg") # S-R trend analysis snl.trend(filename="trend.jpg") md(snl, "trend.md", "Main") # Disable snl.clear(name="A") snl.disable(phases=["0th", "3rd"], name="A") md(snl, "A.md", "A") snl.enable(phases=["0th"], name="A") md(snl, "A2.md", "A") # Combine snl.clear(name="B") snl.combine(phases=["1st", "5th"], name="B") md(snl, "B.md", "B") # Delete 0th phase snl.clear(name="C") snl.delete(phases=["0th"], name="C") md(snl, "C.md", "C") snl.enable(phases=["0th"], name="C") md(snl, "C2.md", "C2") # Delete 3rd phase snl.clear(name="D") snl.delete(phases=["3rd"], name="D") md(snl, "D.md", "D") # Delete last phase snl.clear(name="E") snl.delete(phases=["6th", "7th"], name="E") md(snl, "E.md", "E") # Add phase with end_date snl.add(end_date="31Aug2020", name="E") md(snl, "E2.md", "E") # Add phase with days snl.add(days=10, name="E") md(snl, "E3.md", "E") # Add phase with end_date=None and days=None snl.add(name="E") md(snl, "E4.md", "E") # Separate snl.clear(name="F", template="E") snl.separate(date="01Apr2020", name="F") md(snl, "F.md", "F") # Change snl.clear(name="G", template="F") snl.combine(phases=["0th", "1st"], name="G") snl.separate(date="12Apr2020", name="G") md(snl, "G.md", "G") # Optimize change point candidates = ["01Mar2020", "12Apr2020"] opt_dict = {date: {} for date in candidates} snl_opt = cs.Scenario(jhu_data, population_data, country="Japan", tau=720) snl_opt.trend(show_figure=False) for date in candidates: snl_opt.clear(name=date) snl_opt.combine(phases=["0th", "1st"], name=date) snl_opt.separate(date=date, name=date) snl_opt.estimate(cs.SIRF, phases=["0th", "1st"], name=date) opt_dict[date]["0th"] = snl_opt.get("RMSLE", phase="0th", name=date) opt_dict[date]["1st"] = snl_opt.get("RMSLE", phase="1st", name=date) with open("opt.md", "w") as fh: df = pd.DataFrame.from_dict(opt_dict, orient="index") fh.write(df.to_markdown())
def estimate_country(self, jhu_data, population_data, oxcgrt_data, NPI): COUNTRY = self.COUNTRY NPI_df = oxcgrt_data.cleaned() NPI_df = NPI_df[NPI_df["Country"] == COUNTRY] # Get lockdown dates NPI_df = NPI_df.reset_index().drop('index', axis=1) NPI_df = NPI_df.groupby("Date").mean().reset_index() s = cs.Scenario(jhu_data, population_data, country=COUNTRY) days_delay, df_periods = s.estimate_delay(oxcgrt_data) # NPI_dates = {} # min_rate = 75 # if NPI != "Stringency_index": # min_rate = 3 # periods = [] # while periods == [] or min_rate >= 1: # lockdown_indexes = NPI_df[NPI_df[NPI] >= min_rate].index # lockdown_dates = NPI_df[NPI_df[NPI] >= min_rate]["Date"] # periods = self.get_periods(lockdown_indexes) # min_rate -= 1 # lockdown_dates_adjusted = [] # for date in lockdown_dates: # new_date = date + datetime.timedelta(days = days_delay) # lockdown_dates_adjusted.append(new_date) # NPI_dates[NPI] = lockdown_dates_adjusted # lockdown_dates_adjusted = pd.Series(lockdown_dates_adjusted) # start = periods[-1][1] # if start > 410: # start = periods[-1][0] # print(start, periods, len(NPI_df)) # if periods == [] or start < 90: # return False, False, False, False # DELAY_START = NPI_df.iloc[start + days_delay].Date # index = -1 # while DELAY_START >= pd.to_datetime("2021-03-01"): # DELAY_START = NPI_df.iloc[periods[-index][0]].Date # index -= 1 NPI_dates = {} lockdown_indexes = NPI_df[NPI_df[NPI] >= 75].index lockdown_dates = NPI_df[NPI_df[NPI] >= 75]["Date"] periods = self.get_periods(lockdown_indexes) if periods == []: lockdown_indexes = NPI_df[NPI_df[NPI] >= 65].index lockdown_dates = NPI_df[NPI_df[NPI] >= 65]["Date"] periods = self.get_periods(lockdown_indexes) if periods == []: lockdown_indexes = NPI_df[NPI_df[NPI] >= 4].index lockdown_dates = NPI_df[NPI_df[NPI] >= 4]["Date"] periods = self.get_periods(lockdown_indexes) if periods == []: lockdown_indexes = NPI_df[NPI_df[NPI] >= 3].index lockdown_dates = NPI_df[NPI_df[NPI] >= 3]["Date"] periods = self.get_periods(lockdown_indexes) lockdown_dates_adjusted = [] for date in lockdown_dates: new_date = date + datetime.timedelta(days=days_delay) lockdown_dates_adjusted.append(new_date) NPI_dates[NPI] = lockdown_dates_adjusted lockdown_dates_adjusted = [] for date in lockdown_dates: new_date = date + datetime.timedelta(days=days_delay) lockdown_dates_adjusted.append(new_date) lockdown_dates_adjusted = pd.Series(lockdown_dates_adjusted) # Save variable DELAY_START, which is equal to the start of a lockdown period # in this case, we will hardcode the script to the second lockdown date # if len(periods) > 2: print(len(periods), periods, "PERIODS") if periods == []: return False, False, False, False DELAY_START = NPI_df.iloc[periods[len(periods) - 1][0]].Date print(DELAY_START) if COUNTRY == "Italy": DELAY_START = pd.to_datetime('2020-12-21') if COUNTRY == "United Kingdom": df_params = pd.read_pickle("./data/df_United_Kingdom") elif COUNTRY == "Sweden": df_params = pd.read_pickle("../figures/pickles/df_Sweden") elif COUNTRY == "United States": df_params = pd.read_pickle("../figures/pickles/df_United_States") else: df_params = pd.read_pickle("../figures/pickles/df_9_countries") df_params = df_params[df_params["Country"] == COUNTRY] return DELAY_START, df_params, NPI_dates, days_delay
import covsirphy as cs from pprint import pprint # Download datasets data_loader = cs.DataLoader("input") jhu_data = data_loader.jhu() population_data = data_loader.population() # Check records snl = cs.Scenario(jhu_data, population_data, country="China") a = 1 for line in snl.records().Date: print(line) a += 1
#!/usr/bin/env python # coding: utf-8 import covsirphy as cs import sys data_loader = cs.DataLoader("input") jhu_data = data_loader.jhu(verbose=False) population_data = data_loader.population(verbose=False) scenario = cs.Scenario(jhu_data, population_data, country="India") _ = scenario.records() scenario.trend() scenario.estimate(cs.SIRF) scenario.add(end_date="04Jan2021") _ = scenario.simulate() _.to_csv("predictions.csv") print(_)