예제 #1
0
def main():
    print(cs.__version__)
    pprint(cs.SIRF.EXAMPLE, compact=True)
    # Reproduction number
    eg_dict = cs.SIRF.EXAMPLE.copy()
    model_ins = cs.SIRF(
        population=eg_dict["population"],
        **eg_dict["param_dict"]
    )
    print(model_ins.calc_r0())
    # Set tau value and start date of records
    example_data = cs.ExampleData(tau=1440, start_date="01Jan2020")
    # Add records with SIR model
    model = cs.SIRF
    area = {"country": "Full", "province": model.NAME}
    example_data.add(model, **area)
    # Change parameter values if needed
    # example_data.add(model, param_dict={"kappa": 0.001, "kappa": 0.002, "rho": 0.4, "sigma": 0.0150}, **area)
    # Records with model variables
    df = example_data.specialized(model, **area)
    # Plotting
    cs.line_plot(
        df.set_index("Date"),
        title=f"Example data of {model.NAME} model",
        y_integer=True,
        filename="sirf.png"
    )
예제 #2
0
def main():
    warnings.simplefilter("error")
    # Create output directory in example directory
    code_path = Path(__file__)
    output_dir = code_path.with_name("output").joinpath(code_path.stem)
    output_dir.mkdir(exist_ok=True, parents=True)
    # Setting
    eg_tau = 1440
    eg_population = 1_000_000
    model = cs.SIRFV
    eg_population = model.EXAMPLE["population"]
    set_param_dict = model.EXAMPLE["param_dict"]
    # Simulation
    example_data = cs.ExampleData(tau=eg_tau)
    example_data.add(model)
    # Non-dimensional
    nondim_df = example_data.non_dim(model)
    nondim_df.to_csv(output_dir.joinpath(
        f"{model.NAME}_non_dim.csv"), index=False)
    cs.line_plot(
        nondim_df.set_index("t"),
        title=f"{model.NAME}: Example data (non-dimensional)",
        ylabel=str(),
        h=1.0,
        filename=output_dir.joinpath(f"{model.NAME}_non_dim.png")
    )
    # Dimensional
    dim_df = example_data.cleaned()
    dim_df.to_csv(output_dir.joinpath(f"{model.NAME}_dim.csv"), index=False)
    cs.line_plot(
        dim_df.set_index("Date").drop("Confirmed", axis=1),
        title=f"{model.NAME}: Example data (dimensional)",
        h=eg_population,
        y_integer=True,
        filename=output_dir.joinpath(f"{model.NAME}_dim.png")
    )
    # Hyperparameter estimation of example data
    estimator = cs.Estimator(
        example_data, model=model, population=eg_population,
        country=model.NAME, province=None, tau=eg_tau, omega=0.001
    )
    estimator.run()
    estimated_df = estimator.summary(name=model.NAME)
    estimated_df = estimated_df.append(
        pd.Series({**set_param_dict, "tau": eg_tau}, name="set")
    )
    estimated_df["tau"] = estimated_df["tau"].astype(np.int64)
    estimated_df.to_csv(
        output_dir.joinpath(f"{model.NAME}_estimate_parameter.csv"), index=True
    )
    # Show the history of optimization
    estimator.history(filename=output_dir.joinpath(
        f"{model.NAME}_estimate_history.png")
    )
    # Show the accuracy as a figure
    estimator.accuracy(filename=output_dir.joinpath(
        f"{model.NAME}_estimate_accuracy.png")
    )
예제 #3
0
def main():
    # Create output directory in example directory
    code_path = Path(__file__)
    output_dir = code_path.with_name("output").joinpath(code_path.stem)
    output_dir.mkdir(exist_ok=True, parents=True)
    # Simulation
    eg_population = 1_000_000
    eg_tau = 1440
    setted_param_dict = {
        "theta": 0.002,
        "kappa": 0.005,
        "rho": 0.2,
        "sigma": 0.075
    }
    simulator = ODESimulator(country="Example", province="Example-1")
    simulator.add(model=SIRF,
                  step_n=180,
                  population=eg_population,
                  param_dict=setted_param_dict,
                  y0_dict={
                      "x": 0.999,
                      "y": 0.001,
                      "z": 0,
                      "w": 0
                  })
    simulator.run()
    # Non-dimensional
    nondim_df = simulator.non_dim()
    nondim_df.to_csv(output_dir.joinpath("non_dim.csv"), index=False)
    # Dimensional
    dim_df = simulator.dim(tau=eg_tau, start_date="22Jan2020")
    dim_df.to_csv(output_dir.joinpath("dim.csv"), index=False)
    line_plot(dim_df.set_index("Date")[["Infected", "Recovered", "Fatal"]],
              title="Example data",
              filename=output_dir.joinpath("dim.png"))
    # Hyperparameter estimation of example data
    estimator = Estimator(clean_df=dim_df,
                          model=SIRF,
                          population=eg_population,
                          country="Example",
                          province="Example-1",
                          tau=eg_tau)
    estimator.run()
    estimated_df = estimator.summary(name="SIR-F")
    estimated_df.loc["Setted"] = pd.Series({
        **setted_param_dict, "tau": eg_tau
    })
    estimated_df["tau"] = estimated_df["tau"].astype(np.int64)
    estimated_df.to_csv(output_dir.joinpath("estimate_parameter.csv"),
                        index=True)
    # Show the history of optimization
    estimator.history(filename=output_dir.joinpath("estimate_history.png"))
    # Show the accuracy as a figure
    estimator.accuracy(filename=output_dir.joinpath("estimate_accuracy.png"))
예제 #4
0
def main():
    warnings.simplefilter("error")
    # Create output directory in example directory
    code_path = Path(__file__)
    output_dir = code_path.with_name("output").joinpath(code_path.stem)
    output_dir.mkdir(exist_ok=True, parents=True)
    # Setting
    eg_population = 1_000_000
    eg_tau = 1440
    start_date = "22Jan2020"
    model = cs.SIRF
    set_param_dict = {
        "theta": 0.002,
        "kappa": 0.005,
        "rho": 0.2,
        "sigma": 0.075
    }
    y0_dict = {
        "Susceptible": 999_000,
        "Infected": 1000,
        "Recovered": 0,
        "Fatal": 0
    }
    # Simulation
    simulator = cs.ODESimulator(country="Example", province=model.NAME)
    simulator.add(model=cs.SIRF,
                  step_n=1000,
                  population=eg_population,
                  param_dict=set_param_dict,
                  y0_dict=y0_dict)
    # Non-dimensional
    nondim_df = simulator.non_dim()
    nondim_df.to_csv(output_dir.joinpath("non_dim.csv"), index=False)
    cs.line_plot(nondim_df.set_index("t"),
                 title=f"{model.NAME}: Example data (non-dimensional)",
                 ylabel=str(),
                 h=1.0,
                 filename=output_dir.joinpath("non_dim_long.png"))
    # Dimensional
    dim_df = simulator.dim(tau=eg_tau, start_date=start_date)
    dim_df.to_csv(output_dir.joinpath("dim.csv"), index=False)
    cs.line_plot(dim_df.set_index("Date"),
                 title=f"{model.NAME}: Example data (dimensional)",
                 h=eg_population,
                 y_integer=True,
                 filename=output_dir.joinpath("dim_long.png"))
예제 #5
0
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)
예제 #6
0
def main():
    # Create output directory in example directory
    code_path = Path(__file__)
    output_dir = code_path.with_name("output").joinpath(code_path.stem)
    output_dir.mkdir(exist_ok=True, parents=True)
    # Simulation
    eg_population = 1_000_000
    eg_tau = 1440
    setted_param_dict = {
        "theta": 0.002,
        "kappa": 0.005,
        "rho": 0.2,
        "sigma": 0.075
    }
    simulator = cs.ODESimulator(country="Example", province="Example-1")
    simulator.add(model=cs.SIRF,
                  step_n=1000,
                  population=eg_population,
                  param_dict=setted_param_dict,
                  y0_dict={
                      "x": 0.999,
                      "y": 0.001,
                      "z": 0,
                      "w": 0
                  })
    simulator.run()
    # Non-dimensional
    nondim_df = simulator.non_dim()
    nondim_df.to_csv(output_dir.joinpath("non_dim.csv"), index=False)
    # Dimensional
    dim_df = simulator.dim(tau=eg_tau, start_date="22Jan2020")
    dim_df.to_csv(output_dir.joinpath("dim.csv"), index=False)
    cs.line_plot(dim_df.set_index("Date")[["Infected", "Recovered", "Fatal"]],
                 title="Example data",
                 filename=output_dir.joinpath("dim.png"),
                 ylim=(None, None))
예제 #7
0
def main():
    # Create output directory in example directory
    code_path = Path(__file__)
    output_dir = code_path.with_name("output").joinpath(code_path.stem)
    output_dir.mkdir(exist_ok=True, parents=True)
    # Setting
    eg_population = 1_000_000
    eg_tau = 1440
    start_date = "22Jan2020"
    model = cs.SIRF
    set_param_dict = {
        "theta": 0.002, "kappa": 0.005, "rho": 0.2, "sigma": 0.075
    }
    set_param_dict_2 = {
        "theta": 0.002, "kappa": 0.005, "rho": 0.1, "sigma": 0.075
    }
    y0_dict = {
        "Susceptible": 999_000, "Infected": 1000, "Recovered": 0, "Fatal": 0
    }
    # Dataset for S-R trend analysis
    simulator = cs.ODESimulator(country="Trend", province=model.NAME)
    simulator.add(
        model=model, step_n=30, population=eg_population,
        param_dict=set_param_dict, y0_dict=y0_dict
    )
    simulator.add(
        model=model, step_n=30, population=eg_population,
        param_dict=set_param_dict_2
    )
    simulator.run()
    dim_df = simulator.dim(tau=eg_tau, start_date=start_date)
    cs.line_plot(
        dim_df.set_index("Date"),
        title=f"{model.NAME}: Example data for S-R trend analysis",
        h=eg_population,
        y_integer=True,
        filename=output_dir.joinpath("trend_data.png")
    )
    restored_df = model.restore(dim_df)
    # 1st trial of S-R trend analysis
    change_finder = cs.ChangeFinder(restored_df, eg_population, "Trend")
    change_finder.run(n_points=1, n_jobs=1, seed=0)
    change_finder.show(
        filename=output_dir.joinpath("trend_1st.png")
    )
    # 2nd trial of S-R trend analysis
    change_finder = cs.ChangeFinder(restored_df, eg_population, "Trend")
    change_finder.run(n_points=1, n_jobs=1, seed=0)
    change_finder.show(
        filename=output_dir.joinpath("trend_2nd.png")
    )
    # Dataset for parameter estimation
    simulator = cs.ODESimulator(country="Param", province=model.NAME)
    simulator.add(
        model=model, step_n=150, population=eg_population,
        param_dict=set_param_dict, y0_dict=y0_dict
    )
    simulator.run()
    dim_df = simulator.dim(tau=eg_tau, start_date=start_date)
    cs.line_plot(
        dim_df.set_index("Date"),
        title=f"{model.NAME}: Example data for parameter estimation",
        h=eg_population,
        y_integer=True,
        filename=output_dir.joinpath("estimate_data.png")
    )
    # 1st trial of parameter estimation
    estimator = cs.Estimator(
        clean_df=dim_df, model=model, population=eg_population,
        country="Param", province=model.NAME, tau=eg_tau
    )
    estimator.run(n_jobs=1, seed=0)
    estimator.history(filename=output_dir.joinpath("estimate_history_1st.png"))
    # 2nd trial of parameter estimation
    estimator = cs.Estimator(
        clean_df=dim_df, model=model, population=eg_population,
        country="Param", province=model.NAME, tau=eg_tau
    )
    estimator.run(n_jobs=1, seed=0)
    estimator.history(filename=output_dir.joinpath("estimate_history_2nd.png"))
예제 #8
0
def main():
    # Create output directory in example directory
    code_path = Path(__file__)
    output_dir = code_path.with_name("output").joinpath(code_path.stem)
    output_dir.mkdir(exist_ok=True, parents=True)
    # Setting
    eg_population = 1_000_000
    eg_tau = 1440
    start_date = "22Jan2020"
    model = cs.SIR
    set_param_dict = {"rho": 0.2, "sigma": 0.075}
    y0_dict = {
        "Susceptible": 999_000,
        "Infected": 1000,
        "Fatal or Recovered": 0
    }
    # Simulation
    simulator = cs.ODESimulator(country="Example", province=model.NAME)
    simulator.add(model=model,
                  step_n=180,
                  population=eg_population,
                  param_dict=set_param_dict,
                  y0_dict=y0_dict)
    simulator.run()
    # Non-dimensional
    nondim_df = simulator.non_dim()
    nondim_df.to_csv(output_dir.joinpath(f"{model.NAME}_non_dim.csv"),
                     index=False)
    cs.line_plot(nondim_df.set_index("t"),
                 title=f"{model.NAME}: Example data (non-dimensional)",
                 ylabel=str(),
                 h=1.0,
                 filename=output_dir.joinpath(f"{model.NAME}_non_dim.png"))
    # Dimensional
    dim_df = simulator.dim(tau=eg_tau, start_date=start_date)
    dim_df.to_csv(output_dir.joinpath("dim.csv"), index=False)
    cs.line_plot(dim_df.set_index("Date"),
                 title=f"{model.NAME}: Example data (dimensional)",
                 h=eg_population,
                 y_integer=True,
                 filename=output_dir.joinpath(f"{model.NAME}_dim.png"))
    # Hyperparameter estimation of example data
    estimator = cs.Estimator(clean_df=dim_df,
                             model=model,
                             population=eg_population,
                             country="Example",
                             province=model.NAME,
                             tau=eg_tau)
    estimator.run()
    estimated_df = estimator.summary(name=model.NAME)
    estimated_df.loc["Setted"] = pd.Series({**set_param_dict, "tau": eg_tau})
    estimated_df["tau"] = estimated_df["tau"].astype(np.int64)
    estimated_df.to_csv(
        output_dir.joinpath(f"{model.NAME}_estimate_parameter.csv"),
        index=True)
    # Show the history of optimization
    estimator.history(
        filename=output_dir.joinpath(f"{model.NAME}_estimate_history.png"))
    # Show the accuracy as a figure
    estimator.accuracy(
        filename=output_dir.joinpath(f"{model.NAME}_estimate_accuracy.png"))
예제 #9
0
 def test_function(self, jhu_data, imgfile):
     df = jhu_data.subset(country="Japan").set_index(Term.DATE)
     line_plot(df=df, filename=imgfile, show_legend=True)
     line_plot(df=df, filename=imgfile, show_legend=False)