예제 #1
0
def get_gridspec(fig: plt.Figure, num: int, return_list=True):
    if num > 12:
        logger.warning(
            '[add_gridspec]: got request for more than 12 plots, only returning 12'
        )
        num = 12
    if num != 0:
        shape_dict = {
            1: (1, 1),
            2: (2, 1),
            3: (2, 2),
            5: (3, 2),
            7: (3, 3),
            10: (4, 3)
        }
        arr = np.array(list(shape_dict.keys()))  # array of index
        key = arr[arr <= num].max(
        )  # largest element lower than num (i.e. previous size which fits required num
        shape = shape_dict[key]
        gs = fig.add_gridspec(*shape)
        if return_list is True:
            gs = [gs[i, j] for i in range(shape[0]) for j in range(shape[1])]
    else:
        if return_list is True:
            gs = []
        else:
            gs = None
    return gs
예제 #2
0
def __abc_smc_plotting(fig: plt.Figure, y_obs: [[float]],
                       priors: ["stats.Distribution"],
                       fitting_model: Models.Model, model_hat: Models.Model,
                       accepted_params: [[float]],
                       weights: [float]) -> plt.Figure:

    n_params = (fitting_model.n_params - 2) if (
        type(fitting_model) is Models.SIRModel) else fitting_model.n_params
    n_rows = max([1, np.lcm(n_params, fitting_model.dim_obs)])

    gs = fig.add_gridspec(n_rows, 2)

    # plot fitted model
    row_step = n_rows // fitting_model.dim_obs
    for i in range(fitting_model.dim_obs):
        ax = fig.add_subplot(gs[i * row_step:(i + 1) * row_step, -1])
        y_obs_dim = [y[i] for y in y_obs]
        Plotting.plot_accepted_observations(ax,
                                            fitting_model.x_obs,
                                            y_obs_dim, [],
                                            model_hat,
                                            dim=i)

    row_step = n_rows // n_params
    if (type(fitting_model) is Models.SIRModel):
        for i in range(2, fitting_model.n_params):
            ax = fig.add_subplot(gs[(i - 2) * row_step:(i - 2 + 1) * row_step,
                                    0])
            name = "theta_{}".format(i)
            accepted_parameter_values = [theta[i] for theta in accepted_params]
            Plotting.plot_parameter_posterior(
                ax,
                name,
                accepted_parameter_values,
                predicted_val=model_hat.params[i],
                prior=priors[i],
                dim=i,
                weights=weights)
    else:
        for i in range(fitting_model.n_params):
            ax = fig.add_subplot(gs[i * row_step:(i + 1) * row_step, 0])
            name = "theta_{}".format(i)
            parameter_values = [theta[i] for theta in accepted_params]
            Plotting.plot_parameter_posterior(
                ax,
                name,
                accepted_parameter_values,
                predicted_val=model_hat.params[i],
                prior=priors[i],
                dim=i,
                weights=weights)

    return fig
예제 #3
0
def __abc_mcmc_plotting(fig: plt.Figure, y_obs: [[float]],
                        priors: ["stats.Distribution"],
                        fitting_model: Models.Model, model_hat: Models.Model,
                        accepted_summary_vals: [[float]],
                        accepted_params: [[float]]) -> plt.Figure:

    n_simple_ss = sum(
        len(s) == 1 for s in accepted_summary_vals[0]
    )  # number of summary stats which map to a single dimension
    n_cols = 3 if (n_simple_ss == 0) else 4
    n_params = (fitting_model.n_params - 2) if (
        type(fitting_model) is Models.SIRModel) else fitting_model.n_params
    n_rows = max([
        1,
        np.lcm.reduce([n_params,
                       max(1, n_simple_ss), fitting_model.dim_obs])
    ])

    gs = fig.add_gridspec(n_rows, n_cols)

    # plot fitted model
    row_step = n_rows // fitting_model.dim_obs
    for i in range(fitting_model.dim_obs):
        ax = fig.add_subplot(gs[i * row_step:(i + 1) * row_step, -1])
        y_obs_dim = [y[i] for y in y_obs]
        Plotting.plot_accepted_observations(ax,
                                            fitting_model.x_obs,
                                            y_obs_dim, [],
                                            model_hat,
                                            dim=i)

    # plot traces & posteriors
    row_step = n_rows // n_params
    if (type(fitting_model) is Models.SIRModel):
        for i in range(2, fitting_model.n_params):
            name = "Theta_{}".format(i)
            accepted_vals = [x[i] for x in accepted_params]
            # Chain trace
            ax = fig.add_subplot(gs[(i - 2) * row_step:((i - 2) + 1) *
                                    row_step, 0])
            Plotting.plot_MCMC_trace(ax,
                                     name,
                                     accepted_parameter=accepted_vals,
                                     predicted_val=model_hat.params[i])
            # Posteriors
            ax = fig.add_subplot(gs[(i - 2) * row_step:((i - 2) + 1) *
                                    row_step, 1])
            Plotting.plot_parameter_posterior(
                ax,
                name,
                accepted_parameter=accepted_vals,
                predicted_val=model_hat.params[i],
                prior=priors[i],
                dim=i)
    else:
        for i in range(fitting_model.n_params):
            name = "Theta_{}".format(i)
            accepted_vals = [x[i] for x in THETAS]
            # Chain trace
            ax = fig.add_subplot(gs[i * row_step:(i + 1) * row_step, 0])
            Plotting.plot_MCMC_trace(ax,
                                     name,
                                     accepted_parameter=accepted_vals,
                                     predicted_val=model_hat.params[i])
            # Posteriors
            ax = fig.add_subplot(gs[i * row_step:(i + 1) * row_step, 1])
            Plotting.plot_parameter_posterior(
                ax,
                name,
                accepted_parameter=accepted_vals,
                predicted_val=model_hat.params[i],
                prior=priors[i],
                dim=i)

    # plot summary vals
    row = 0
    row_step = n_rows // n_simple_ss
    for i in range(len(accepted_summary_vals[0])):
        if (len(accepted_summary_vals[0][i]) == 1):
            name = "s_{}".format(i)
            ax = fig.add_subplot(gs[row * row_step:(row + 1) * row_step, 2])
            row += 1
            accepted_vals = [s[i][0] for s in accepted_summary_vals]
            Plotting.plot_summary_stats(ax,
                                        name,
                                        accepted_s=accepted_vals,
                                        s_obs=s_obs[i],
                                        s_hat=s_hat[i],
                                        dim=i)

    return fig
예제 #4
0
def __abc_rejection_plotting(fig: plt.Figure, y_obs: [[float]], priors: [
    "stats.Distribution"
], fitting_model: Models.Model, model_hat: Models.Model,
                             accepted_summary_vals: [[float]],
                             accepted_observations: [[float]],
                             accepted_params: [[float]]) -> plt.Figure:
    # plot results
    n_simple_ss = sum(
        len(s) == 1 for s in accepted_summary_vals[0]
    )  # number of summary stats which map to a single dimension
    n_cols = 2 if (n_simple_ss == 0) else 3
    n_params = (fitting_model.n_params - 2) if (
        type(fitting_model) is Models.SIRModel) else fitting_model.n_params
    n_rows = max([
        1,
        np.lcm.reduce([n_params,
                       max(1, n_simple_ss), fitting_model.dim_obs])
    ])

    # plot accepted obervations for each dimension
    gs = fig.add_gridspec(n_rows, n_cols)

    # plot accepted observations (each dimension separate)
    row_step = n_rows // fitting_model.dim_obs
    for i in range(fitting_model.dim_obs):
        ax = fig.add_subplot(gs[i * row_step:(i + 1) * row_step, -1])
        y_obs_dim = [y[i] for y in y_obs]
        accepted_obs_dim = [[y[i] for y in obs]
                            for obs in accepted_observations]
        Plotting.plot_accepted_observations(ax,
                                            fitting_model.x_obs,
                                            y_obs_dim,
                                            accepted_obs_dim,
                                            model_hat,
                                            dim=i)

    # plot posterior for each parameter
    row_step = n_rows // n_params
    if (type(fitting_model) is Models.SIRModel):
        for i in range(2, fitting_model.n_params):
            name = "Theta_{}".format(i)
            ax = fig.add_subplot(gs[(i - 2) * row_step:(i - 2 + 1) * row_step,
                                    0])
            accepted_vals = [x[i] for x in accepted_params]
            Plotting.plot_parameter_posterior(
                ax,
                name,
                accepted_parameter=accepted_vals,
                predicted_val=model_hat.params[i],
                prior=priors[i],
                dim=i)
    else:
        for i in range(fitting_model.n_params):
            name = "Theta_{}".format(i)
            ax = fig.add_subplot(gs[i * row_step:(i + 1) * row_step, 0])
            accepted_vals = [x[i] for x in accepted_params]
            Plotting.plot_parameter_posterior(
                ax,
                name,
                accepted_parameter=accepted_vals,
                predicted_val=model_hat.params[i],
                prior=priors[i],
                dim=i)

    # plot histogram of each summary statistic value
    row = 0
    if (n_simple_ss != 0):
        row_step = n_rows // n_simple_ss
        for i in range(len(summary_stats)):
            if (len(accepted_summary_vals[0][i]) == 1):
                name = "s_{}".format(i)
                ax = fig.add_subplot(gs[row * row_step:(row + 1) * row_step,
                                        1])
                row += 1
                accepted_vals = [s[i][0] for s in accepted_summary_vals]
                Plotting.plot_summary_stats(ax,
                                            name,
                                            accepted_s=accepted_vals,
                                            s_obs=s_obs[i],
                                            s_hat=s_hat[i],
                                            dim=i)

    return fig