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-style dataset jhu_data = data_loader.jhu() # Load Population dataset population_data = data_loader.population() # Government Response Tracker (OxCGRT) oxcgrt_data = data_loader.oxcgrt(verbose=True) # Create analyser with tau value 360 [min] (can be changed) analyser = cs.PolicyMeasures( jhu_data, population_data, oxcgrt_data, tau=360) # S-R trend analysis analyser.trend() analyser.trend(min_len=2) # Parameter estimation analyser.estimate(cs.SIRF) # All results track_df = analyser.track() track_df.to_csv(output_dir.joinpath("track.csv"), index=False)
def __init__(self,countries,states=None): self.countries = countries self.states = states self.n = len(countries) if len(countries)>1 else len(states) #Number of variable countries or states self.data = cs.DataLoader("model_fitting/data") self.jhu_data = self.data.jhu() self.population_data = self.data.population()
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 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-style dataset jhu_data = data_loader.jhu() # Load Population dataset population_data = data_loader.population() # Government Response Tracker (OxCGRT) oxcgrt_data = data_loader.oxcgrt(verbose=True) # Create analyser with tau value 360 [min] (can be changed) analyser = cs.PolicyMeasures(jhu_data, population_data, oxcgrt_data, tau=360) # S-R trend analysis analyser.trend() min_len = max(analyser.phase_len().keys()) analyser.trend(min_len=min_len) pprint(analyser.phase_len(), compact=True) # Parameter estimation analyser.estimate(cs.SIRF) # All results track_df = analyser.track() track_df.to_csv(output_dir.joinpath("track.csv"), index=False) # Parameter history of Rt rt_df = analyser.history("Rt", roll_window=None, filename=output_dir.joinpath("history_rt.png")) rt_df.to_csv(output_dir.joinpath("history_rt.csv"), index=False) # Parameter history of rho rho_df = analyser.history("rho", roll_window=14, filename=output_dir.joinpath("history_rho.jpg")) rho_df.to_csv(output_dir.joinpath("history_rho.csv"), index=False) # Parameter history of sigma sigma_df = analyser.history( "sigma", roll_window=14, filename=output_dir.joinpath("history_sigma.jpg")) sigma_df.to_csv(output_dir.joinpath("history_sigma.csv"), index=False) # Parameter history of kappa kappa_df = analyser.history( "kappa", roll_window=14, filename=output_dir.joinpath("history_kappa.jpg")) kappa_df.to_csv(output_dir.joinpath("history_kappa.csv"), index=False) # Parameter history of theta theta_df = analyser.history( "theta", roll_window=14, filename=output_dir.joinpath("history_theta.jpg")) theta_df.to_csv(output_dir.joinpath("history_theta.csv"), index=False)
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) # Create data loader instance data_loader = cs.DataLoader(input_dir) # Load JHU dataset print("<The number of cases>") jhu_data = data_loader.jhu() print(jhu_data.citation) ncov_df = jhu_data.cleaned() ncov_df.to_csv(output_dir.joinpath("covid19_cleaned_jhu.csv"), index=False) # Subset for Japan japan_df, _ = jhu_data.records("Japan") japan_df.to_csv(output_dir.joinpath("jhu_cleaned_japan.csv"), index=False) # Load Population dataset print("<Population values>") population_data = data_loader.population() print(population_data.citation) population_df = population_data.cleaned() population_df.to_csv(output_dir.joinpath("population_cleaned.csv"), index=False) # Load OxCGRT dataset print("<Government response tracker>") oxcgrt_data = data_loader.oxcgrt() print(oxcgrt_data.citation) oxcgrt_df = oxcgrt_data.cleaned() oxcgrt_df.to_csv(output_dir.joinpath("oxcgrt_cleaned.csv"), index=False) # Load PCR test dataset print("<The number of PCR tests>") pcr_data = data_loader.pcr() print(pcr_data.citation) pcr_df = pcr_data.cleaned() pcr_df.to_csv(output_dir.joinpath("pcr_cleaned.csv"), index=False) pcr_data.positive_rate( country="Greece", filename=output_dir.joinpath("pcr_positive_rate_Greece.jpg")) # Load vaccine dataset print("<The number of vaccinations>") vaccine_data = data_loader.vaccine() print(vaccine_data.citation) vaccine_df = vaccine_data.cleaned() vaccine_df.to_csv(output_dir.joinpath("vaccine_cleaned.csv"), index=False) subset_df = vaccine_data.subset(country="Canada") subset_df.to_csv(output_dir.joinpath("vaccine_subset_canada.csv"), index=False) # Load population pyramid dataset print("<Population pyramid>") pyramid_data = data_loader.pyramid() print(pyramid_data.citation) subset_df = pyramid_data.subset(country="Japan") subset_df.to_csv(output_dir.joinpath("pyramid_subset_japan.csv"), index=False)
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 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() oxcgrt_data = data_loader.oxcgrt() # S-R trend analysis for all countries analyser = cs.PolicyMeasures(jhu_data, population_data, oxcgrt_data) for country in analyser.countries: snl = analyser.scenario(country) name = country.replace(" ", "_") snl.trend(filename=output_dir.joinpath(f"trend_{name}.png"))
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) # Create data loader instance data_loader = cs.DataLoader(input_dir) # Load Linelist of case reports linelist_data = data_loader.linelist() print(linelist_data.citation) linelist_data.cleaned().to_csv(output_dir.joinpath("linelist_cleaned.csv"), index=False) # Subset by area linelist_data.subset("Japan").to_csv( output_dir.joinpath("linelist_japan.csv"), index=False) # Global closed records (only recovered) linelist_data.closed(outcome="Recovered").to_csv( output_dir.joinpath("linelist_global_recovered.csv"), index=False)
def __init__(self): print('Initing PlottingCs') # Create DataLoader instance self.data_loader = cs.DataLoader("input") print('Initing dataloader (jhu)') # The number of cases (JHU style) self.jhu_data = self.data_loader.jhu() print('Initing dataloader (population)') # Population in each country self.population_data = self.data_loader.population() pprint(set(self.jhu_data.countries()) & set(self.population_data.countries()), compact=True) # Government Response Tracker (OxCGRT) self.oxcgrt_data = self.data_loader.oxcgrt() # The number of tests self.pcr_data = self.data_loader.pcr() # The number of vaccinations self.vaccine_data = self.data_loader.vaccine()
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) # Create data loader instance data_loader = cs.DataLoader(input_dir) # Load JHU dataset jhu_data = data_loader.jhu() print(jhu_data.citation) ncov_df = jhu_data.cleaned() ncov_df.to_csv(output_dir.joinpath("covid19_cleaned_jhu.csv"), index=False) # Load Japan dataset (the number of cases) japan_data = data_loader.japan() print(japan_data.citation) japan_df = japan_data.cleaned() japan_df.to_csv(output_dir.joinpath("covid19_cleaned_japan.csv"), index=False) # Replace records of Japan with Japan-specific dataset jhu_data.replace(japan_data) ncov_df = jhu_data.cleaned() ncov_df.to_csv(output_dir.joinpath("jhu_cleaned_replaced.csv"), index=False) # Load Population dataset population_data = data_loader.population() print(population_data.citation) population_df = population_data.cleaned() population_df.to_csv(output_dir.joinpath("population_cleaned.csv"), index=False) # Load OxCGRT dataset oxcgrt_data = data_loader.oxcgrt() print(oxcgrt_data.citation) oxcgrt_df = oxcgrt_data.cleaned() oxcgrt_df.to_csv(output_dir.joinpath("oxcgrt_cleaned.csv"), index=False) # Create a subset for a country with ISO3 country code oxcgrt_data.subset("JPN").to_csv( output_dir.joinpath("oxcgrt_cleaned_jpn.csv"), index=False)
def main(): print(cs.__version__) # DataLoader data_loader = cs.DataLoader("input") # JHU data jhu_data = data_loader.jhu(verbose=True) print(type(jhu_data)) print(data_loader.covid19dh_citation) print(jhu_data.raw.tail()) with open("jhu_data_cleaned.md", "w") as fh: s = jhu_data.cleaned().tail().to_markdown() fh.write(s) with open("jhu_data_subset.md", "w") as fh: subset_df = jhu_data.subset(country="JPN", province="Tokyo") s = subset_df.tail().to_markdown() fh.write(s) cs.line_plot(subset_df.set_index("Date").drop("Confirmed", axis=1), title="Japan/Tokyo: cases over time", filename="jhu_data_subset.jpg", y_integer=True) with open("jhu_data_total.md", "w") as fh: s = jhu_data.total().tail().to_markdown() fh.write(s) # Population population_data = data_loader.population(verbose=True) print(type(population_data)) with open("population_data_cleaned.md", "w") as fh: s = population_data.cleaned().tail().to_markdown() fh.write(s) print(population_data.value(country="JPN", province="Tokyo")) # OxCGRT oxcgrt_data = data_loader.oxcgrt() print(type(oxcgrt_data)) with open("oxcgrt_data_cleaned.md", "w") as fh: s = oxcgrt_data.cleaned().tail().to_markdown() fh.write(s) with open("oxcgrt_data_subset.md", "w") as fh: s = oxcgrt_data.subset(country="Japan").tail().to_markdown() fh.write(s)
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 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 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() oxcgrt_data = data_loader.oxcgrt() # Setup analyser analyser = cs.PolicyMeasures(jhu_data, population_data, oxcgrt_data) # Dictionary of the number of phases in each country with output_dir.joinpath("trend.json").open("w") as fh: analyser.trend() json.dump(analyser.phase_len(), fh, indent=4) # Show figure of S-R trend analysis for country in analyser.countries: snl = analyser.scenario(country) name = country.replace(" ", "_") snl.trend(filename=output_dir.joinpath(f"trend_{name}.png"))
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(): 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 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)
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
def do_plot(country,effective_contact_rate, recovery_rate): data_loader = cs.DataLoader("input") jhu_data = data_loader.jhu() #Make use of dataloader to get population of Countries. population_data = data_loader.population() effective_contact_rate = float(effective_contact_rate) recovery_rate = float(recovery_rate) country = str(country) # print(effective_contact_rate,recovery_rate,country) #calculate R0 print("R0 is", effective_contact_rate/recovery_rate) total_population = population_data.value(country, province = None) print("Total Population in "+ country + " is :",total_population) recovered = 0 infected = 1 susceptible = total_population - infected - recovered # number of days # days = len(jhu_data.subset("Canada", province=None)) days = range(0,365) #use of differentail equation ret = odeint(deriv, [susceptible, infected, recovered], days, args = (total_population, effective_contact_rate, recovery_rate)) S, I , R = ret.T #Build a dataframe df = pd.DataFrame({ 'susceptible': S, 'infected': I, 'recovered': R, 'day': days }) fig = df.plot(x='day', y=['infected', 'susceptible', 'recovered'], color=['#bb6424', '#aac6ca', '#cc8ac0'], kind='area', title = "SIR Model for " + country, xlabel='Days', ylabel='Population', figsize=(15,10), stacked=False) bytes_image = io.BytesIO() plt.savefig(bytes_image,format='png') bytes_image.seek(0) return bytes_image
def do_plot(country, effective_contact_rate, recovery_rate, mortality_rate): data_loader = cs.DataLoader("input") jhu_data = data_loader.jhu() #Make use of dataloader to get population of Countries. population_data = data_loader.population() #probability of direct fatality is kept very low as medicine were quite effective in later stages probability_of_direct_fatality = 0.0001 effective_contact_rate = float(effective_contact_rate) recovery_rate = float(recovery_rate) mortality_rate = float(float(mortality_rate) / 100) country = str(country) # print(effective_contact_rate,recovery_rate,country) #calculate R0 print("R0 is", effective_contact_rate / recovery_rate) total_population = population_data.value(country, province=None) print("Total Population in " + country + " is :", total_population) recovered = 0 infected = 1 susceptible = total_population - infected - recovered fatal = 40 # number of days # days = len(jhu_data.subset("Canada", province=None)) days = range(0, 365) #use of differentail equation ret = odeint(deriv, [susceptible, infected, recovered], days, args=(total_population, effective_contact_rate, recovery_rate)) S, I, R = ret.T #Build a dataframe df1 = pd.DataFrame({ 'susceptible': S, 'infected': I, 'recovered': R, 'day': days }) ###SIRF MODEL ret = odeint(derivSIRF, [susceptible, infected, recovered, fatal], days, args=(total_population, effective_contact_rate, recovery_rate, probability_of_direct_fatality, mortality_rate)) S, I, R, F = ret.T #Build a dataframe df2 = pd.DataFrame({ 'susceptible': S, 'infected': I, 'recovered': R, 'fatal': F, 'day': days }) plt.style.use('ggplot') fig, axes = plt.subplots(2) df1.plot(x='day', y=['infected', 'susceptible', 'recovered'], color=['#bb6424', '#aac6ca', '#cc8ac0'], kind='area', title="SIR Model for " + country, xlabel='Days', ylabel='Population', figsize=(15, 10), stacked=False, ax=axes[0]) df2.plot(x='day', y=['infected', 'susceptible', 'recovered', 'fatal'], color=['#bb6424', '#aac6ca', '#cc8ac0', '#F15E3F'], kind='area', title="SIR-F Model for " + country, xlabel='Days', ylabel='Population', figsize=(15, 10), stacked=False, ax=axes[1]) bytes_image = io.BytesIO() plt.savefig(bytes_image, format='png') bytes_image.seek(0) return bytes_image