コード例 #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")
    )