Ejemplo n.º 1
0
def plot_post_calibration(targets: dict, mcmc_dir: str, plot_dir: str,
                          priors: list):
    logger.info(f"Plotting {mcmc_dir} into {plot_dir}")
    plotter = FilePlotter(plot_dir, targets)
    mcmc_tables = db.load.load_mcmc_tables(mcmc_dir)
    mcmc_params = db.load.load_mcmc_params_tables(mcmc_dir)

    derived_output_tables = db.load.load_derived_output_tables(mcmc_dir)
    param_options = mcmc_params[0]["name"].unique().tolist()

    logger.info("Plotting calibration fits")
    subplotter = _get_sub_plotter(plot_dir, "calibration-fit")
    for target in targets.values():
        output_name = target["output_key"]
        # need to bypass the differential output targets because these outputs are not computed yet.
        if output_name.startswith("rel_diff") or output_name.startswith(
                "abs_diff"):
            continue
        logger.info("Plotting calibration fit for output %s", output_name)
        outputs = plots.sample_outputs_for_calibration_fit(
            output_name, mcmc_tables, derived_output_tables, 0)
        plots.plot_calibration_fit(subplotter,
                                   output_name,
                                   outputs,
                                   targets,
                                   is_logscale=True)
        plots.plot_calibration_fit(subplotter,
                                   output_name,
                                   outputs,
                                   targets,
                                   is_logscale=False)

    logger.info("Plotting posterior distributions")
    num_bins = 16
    subplotter = _get_sub_plotter(plot_dir, "posteriors")
    for chosen_param in param_options:
        plots.plot_posterior(subplotter, mcmc_params, mcmc_tables, 0,
                             chosen_param, num_bins, priors[0])

    logger.info("Plotting loglikelihood vs params")
    subplotter = _get_sub_plotter(plot_dir, "params-vs-loglikelihood")
    for chosen_param in param_options:
        plots.plot_single_param_loglike(subplotter, mcmc_tables, mcmc_params,
                                        0, chosen_param)

    logger.info("Plotting parameter traces")
    subplotter = _get_sub_plotter(plot_dir, "params-traces")
    for chosen_param in param_options:
        plots.plot_mcmc_parameter_trace(subplotter, mcmc_params, 0,
                                        chosen_param)

    logger.info("Plotting acceptance ratios")
    plots.plot_acceptance_ratio(plotter, mcmc_tables, 0)

    logger.info("Plotting loglikelihood traces")
    num_iters = len(mcmc_tables[0])
    plots.plot_burn_in(plotter, num_iters, PLOT_BURN_IN)
    plots.plot_loglikelihood_trace(plotter, mcmc_tables, PLOT_BURN_IN)

    logger.info("MCMC plots complete")
Ejemplo n.º 2
0
def plot_timeseries_with_uncertainty_for_powerbi(
    region_name: str, powerbi_db_path: str, output_dir: str
):
    """
    works on powerbi version
    Assumes a COVID model.
    TODO: Unify PowerBI and local version
    """
    os.makedirs(output_dir, exist_ok=True)
    plot_config = load_plot_config(region_name)
    db = Database(powerbi_db_path)
    uncertainty_df = db.query("uncertainty")
    outputs = uncertainty_df["type"].unique().tolist()
    quantile_vals = uncertainty_df["quantile"].unique().tolist()
    for output_name in outputs:
        this_output_dir = os.path.join(output_dir, output_name)
        os.makedirs(this_output_dir, exist_ok=True)
        plotter = FilePlotter(this_output_dir, plot_config["translations"])
        mask = uncertainty_df["type"] == output_name
        output_df = uncertainty_df[mask]
        scenarios = output_df.Scenario.unique().tolist()
        for scenario in scenarios:
            mask = output_df["Scenario"] == scenario
            scenario_df = output_df[mask]
            quantiles = {}
            for q in quantile_vals:
                mask = scenario_df["quantile"] == q
                quantiles[q] = scenario_df[mask]["value"].tolist()

            times = scenario_df.time.unique()
            logger.info("Plotting uncertainty for output %s, scenario %s", output_name, scenario)
            plots.plot_timeseries_with_uncertainty_for_powerbi(
                plotter, output_name, scenario, quantiles, times, plot_config
            )
Ejemplo n.º 3
0
def run_dashboard():
    app_name, app_dirpath = "covid_19", os.path.join(
        os.getcwd(), "data\outputs\calibrate\covid_19")
    if not app_name:
        st.write("No calibrations have been run yet")
        return

    region_name = "victoria"
    region_dirpath = os.path.join(app_dirpath, region_name)
    if not region_name:
        st.write("No region folder found")
        return

    calib_name, calib_dirpath = selectors.calibration_run(
        region_dirpath, region_name)
    if not calib_name:
        st.write("No model run folder found")
        return

    # Load MCMC tables
    mcmc_tables = db.load.load_mcmc_tables(calib_dirpath)
    mcmc_params = db.load.load_mcmc_params_tables(calib_dirpath)
    targets = load_targets(app_name, region_name)

    plotter = StreamlitPlotter(targets)
    plot_type = st.sidebar.selectbox("Select plot type",
                                     list(PLOT_FUNCS.keys()))
    plot_func = PLOT_FUNCS[plot_type]

    plot_func(
        plotter,
        calib_dirpath,
        mcmc_tables,
        mcmc_params,
        targets,
        app_name,
        region_name,
    )

    path_name = os.path.join(calib_dirpath, "saved_plots")
    if not os.path.exists(path_name):
        os.makedirs(path_name)
    with st.spinner("Saving files..."):
        file_plotter = FilePlotter(path_name, targets)
        plot_func(file_plotter, calib_dirpath, mcmc_tables, mcmc_params,
                  targets, app_name, region_name)
Ejemplo n.º 4
0
def plot_timeseries_with_uncertainty(
    path_to_percentile_outputs: str, output_names: List[str], scenario_list=[0], burn_in=0,
):
    """
    works on local version
    TODO: Deprecate
    """
    percentile_db_path = os.path.join(
        path_to_percentile_outputs, "mcmc_percentiles_burned_" + str(burn_in) + ".db"
    )
    if not os.path.exists(percentile_db_path):
        export_mcmc_quantiles(path_to_percentile_outputs, output_names, burn_in=burn_in)

    plotter = FilePlotter(path_to_percentile_outputs, {})
    for output_name in output_names:
        plots.plot_timeseries_with_uncertainty(
            plotter, path_to_percentile_outputs, output_name, scenario_list, burn_in=burn_in,
        )
Ejemplo n.º 5
0
def plot_uncertainty(targets: dict, powerbi_db_path: str, output_dir: str):
    """
    works on powerbi version
    Assumes a COVID model.
    """
    os.makedirs(output_dir, exist_ok=True)
    db = Database(powerbi_db_path)
    uncertainty_df = db.query("uncertainty")
    outputs = uncertainty_df["type"].unique().tolist()
    for output_name in outputs:
        this_output_dir = os.path.join(output_dir, output_name)
        os.makedirs(this_output_dir, exist_ok=True)
        plotter = FilePlotter(this_output_dir, targets)
        scenario_idxs = uncertainty_df["scenario"].unique().tolist()
        for scenario_idx in scenario_idxs:
            logger.info("Plotting uncertainty for output %s, scenario %s",
                        output_name, scenario_idx)
            if scenario_idx == 0:
                # Just plot the baseline scenario for the full time period.
                scenario_idxs = [0]
                x_low = 0
            else:
                # Plot the baseline compared ot the scenario, but only for the time period
                # where the scenario is active.
                scenario_idxs = [0, scenario_idx]
                mask = uncertainty_df["scenario"] == scenario_idx
                x_low = uncertainty_df[mask]["time"].min()

            plots.plot_timeseries_with_uncertainty(
                plotter,
                uncertainty_df,
                output_name,
                scenario_idxs,
                targets,
                x_low=x_low,
            )
Ejemplo n.º 6
0
def _get_sub_plotter(plot_dir: str, subplot_dirname: str):
    subplot_dir = os.path.join(plot_dir, subplot_dirname)
    os.makedirs(subplot_dir, exist_ok=True)
    return FilePlotter(subplot_dir, {})