Example #1
0
def visualize_vae(vae, skew_config, report,
                  resolution=20,
                  title="VAE Heatmap"):
    xlim, ylim = vae.get_plot_ranges()
    show_prob_heatmap(vae, xlim=xlim, ylim=ylim, resolution=resolution)
    fig = plt.gcf()
    prob_heatmap_img = vu.save_image(fig)
    report.add_image(prob_heatmap_img, "Prob " + title)

    show_weight_heatmap(
        vae, skew_config, xlim=xlim, ylim=ylim, resolution=resolution,
    )
    fig = plt.gcf()
    heatmap_img = vu.save_image(fig)
    report.add_image(heatmap_img, "Weight " + title)
    return prob_heatmap_img
Example #2
0
def create_figure(
        report: HTMLReport,
        target_poses,
        *list_of_vector_fields
):
    num_vfs = len(list_of_vector_fields)
    width = 7
    height = 7 * num_vfs
    for i, target_pos in enumerate(target_poses):
        fig, axes = plt.subplots(
            num_vfs, figsize=(width, height)
        )
        for j, vf in enumerate([vfs[i] for vfs in list_of_vector_fields]):
            # `heatmaps` is now a list of heatmaps, such that
            # heatmaps[k] = list_of_list_of_heatmaps[k][i]
            min_pos = max(target_pos - WaterMaze.TARGET_RADIUS,
                          -WaterMaze.BOUNDARY_DIST)
            max_pos = min(target_pos + WaterMaze.TARGET_RADIUS,
                          WaterMaze.BOUNDARY_DIST)

            """
            Plot Estimated & Optimal QF
            """
            ax = axes[j]
            vu.plot_vector_field(fig, ax, vf)
            ax.vlines([min_pos, max_pos], *ax.get_ylim())
            ax.set_xlabel("Position")
            ax.set_ylabel("Velocity")
            ax.set_title("{0}. t = {1}. Target X Pos = {2}".format(
                vf.info['title'],
                vf.info['time'],
                vf.info['target_pos'],
            ))
        img = vu.save_image(fig)
        report.add_image(img, "Target Position = {}".format(target_pos))
Example #3
0
def visualize_histogram(histogram, skew_config, report, title=""):
    prob = histogram.pvals
    weights = prob_to_weight(prob, skew_config)
    xrange, yrange = histogram.xy_range
    extent = [xrange[0], xrange[1], yrange[0], yrange[1]]
    for name, values in [
        ('Weight Heatmap', weights),
        ('Prob Heatmap', prob),
    ]:
        plt.figure()
        fig = plt.gcf()
        ax = plt.gca()
        values = values.copy()
        values[values == 0] = np.nan
        heatmap_img = ax.imshow(
            np.swapaxes(values, 0, 1),  # imshow uses first axis as y-axis
            extent=extent,
            cmap=plt.get_cmap('plasma'),
            interpolation='nearest',
            aspect='auto',
            origin='bottom',  # <-- Important! By default top left is (0, 0)
            # norm=LogNorm(),
        )
        divider = make_axes_locatable(ax)
        legend_axis = divider.append_axes('right', size='5%', pad=0.05)
        fig.colorbar(heatmap_img, cax=legend_axis, orientation='vertical')
        heatmap_img = vu.save_image(fig)
        if histogram.num_bins < 5:
            pvals_str = np.array2string(histogram.pvals, precision=3)
            report.add_text(pvals_str)
        report.add_image(heatmap_img, "{} {}".format(title, name))
    return heatmap_img
Example #4
0
def visualize_histogram_samples(
    histogram,
    report,
    dynamics,
    n_vis=1000,
    title="Histogram Samples",
    samples=None,
):
    plt.figure()
    if samples is None:
        generated_samples = histogram.sample(n_vis)
        samples = dynamics(generated_samples)
    plt.plot(
        samples[:, 0],
        samples[:, 1],
        '.',
    )
    xlim, ylim = histogram.get_plot_ranges()
    plt.xlim(*xlim)
    plt.ylim(*ylim)
    plt.title(title)

    fig = plt.gcf()
    sample_img = vu.save_image(fig)
    report.add_image(sample_img, title)
    return sample_img
Example #5
0
def visualize_vae_samples(
        epoch, training_data, vae,
        report, dynamics,
        n_vis=1000,
        xlim=(-1.5, 1.5),
        ylim=(-1.5, 1.5)
):
    plt.figure()
    plt.suptitle("Epoch {}".format(epoch))
    n_samples = len(training_data)
    skip_factor = max(n_samples // n_vis, 1)
    training_data = training_data[::skip_factor]
    reconstructed_samples = vae.reconstruct(training_data)
    generated_samples = vae.sample(n_vis)
    projected_generated_samples = dynamics(generated_samples)
    plt.subplot(2, 2, 1)
    plt.plot(generated_samples[:, 0], generated_samples[:, 1], '.')
    if xlim is not None:
        plt.xlim(*xlim)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.title("Generated Samples")
    plt.subplot(2, 2, 2)
    plt.plot(projected_generated_samples[:, 0],
             projected_generated_samples[:, 1], '.')
    if xlim is not None:
        plt.xlim(*xlim)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.title("Projected Generated Samples")
    plt.subplot(2, 2, 3)
    plt.plot(training_data[:, 0], training_data[:, 1], '.')
    if xlim is not None:
        plt.xlim(*xlim)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.title("Training Data")
    plt.subplot(2, 2, 4)
    plt.plot(reconstructed_samples[:, 0], reconstructed_samples[:, 1], '.')
    if xlim is not None:
        plt.xlim(*xlim)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.title("Reconstruction")

    fig = plt.gcf()
    sample_img = vu.save_image(fig)
    report.add_image(sample_img, "Epoch {} Samples".format(epoch))

    return sample_img
Example #6
0
def visualize_samples(epoch,
                      vis_samples_np,
                      histogram,
                      report,
                      dynamics,
                      n_vis=1000,
                      title=None,
                      xlim=(-1.5, 1.5),
                      ylim=(-1.5, 1.5)):
    if title is None:
        title = "Epoch {}".format(epoch)
    plt.figure()
    plt.suptitle(title)
    generated_samples = histogram.sample(n_vis)
    projected_generated_samples = dynamics(generated_samples)
    plt.subplot(3, 1, 1)
    plt.plot(generated_samples[:, 0], generated_samples[:, 1], '.')
    if xlim is not None:
        plt.xlim(*xlim)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.title("Generated Samples")
    plt.subplot(3, 1, 2)
    plt.plot(projected_generated_samples[:, 0],
             projected_generated_samples[:, 1], '.')
    if xlim is not None:
        plt.xlim(*xlim)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.title("Projected Generated Samples")
    plt.subplot(3, 1, 3)
    plt.plot(vis_samples_np[:, 0], vis_samples_np[:, 1], '.')
    if xlim is not None:
        plt.xlim(*xlim)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.title("Original Samples")

    fig = plt.gcf()
    sample_img = vu.save_image(fig)
    report.add_image(sample_img, "Epoch {} Samples".format(epoch))

    return sample_img
Example #7
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("folder_path", type=str)
    # parser.add_argument("--num_iters", type=int)
    args = parser.parse_args()
    base = Path(os.getcwd())
    base = base / args.folder_path

    path_and_iter = get_path_and_iters(base)

    resolution = 20
    x_bounds = (-1, 1)
    y_bounds = (-1, 1)

    report = HTMLReport(
        str(base / 'report.html'), images_per_row=1
    )

    # for path, itr in takespread(path_and_iter, args):
    for path, itr in [
        path_and_iter[-1]
    ]:
        report.add_text("Path: %s" % path)
        print("Loading: %s" % path)
        data = joblib.load(str(path))
        qf = data['qf']
        env = data['env']
        qf.train(False)

        start_state = env.reset()
        report.add_text("Start State = {}".format(start_state))
        report.add_text(
            "Start XY = {}".format(
                position_from_angles(np.expand_dims(start_state, 0))
            )
        )
        goal_states = [start_state]
        goal_states += [
            env.sample_goal_for_rollout()
            for _ in range(5)
        ]
        for goal_state in goal_states:
            qf_eval = create_qf_eval_fnct(qf, start_state, goal_state)
            qf_heatmap = vu.make_heat_map(
                qf_eval,
                x_bounds=x_bounds,
                y_bounds=y_bounds,
                resolution=resolution,
            )

            fig = create_figure(
                ['Estimated'],
                [qf_heatmap],
            )
            img = vu.save_image(fig)
            report.add_image(img, "Goal State = {}\nGoal XY = {}".format(
                goal_state,
                position_from_angles(np.expand_dims(goal_state, 0))
            ))

    abs_path = osp.abspath(report.path)
    print("Report saved to: {}".format(abs_path))
    report.save()
    open_report = query_yes_no("Open report?", default="yes")
    if open_report:
        cmd = "xdg-open {}".format(abs_path)
        print(cmd)
        subprocess.call(cmd, shell=True)
Example #8
0
def generate_report(fanova_info: FanovaInfo, base_dir, param_name_to_log=None):
    if param_name_to_log is None:
        param_name_to_log = {}
    f, config_space, X, Y, categorical_remapping, variants_list = fanova_info
    report = HTMLReport(
        osp.join(base_dir, 'report.html'),
        images_per_row=3,
    )

    vis = visualizer.Visualizer(f, config_space)
    cs_params = config_space.get_hyperparameters()
    importances = [get_param_importance(f, param) for param in cs_params]
    are_logs = [
        is_data_log_uniformly_distributed(X[:, i])
        for i in range(len(cs_params))
    ]
    data = sorted(
        zip(cs_params, importances, are_logs),
        key=lambda x: -x[1],
    )
    """
    List out how the categorical hyperparameters were mapped.
    """
    for name, remapping in categorical_remapping.items():
        report.add_text("Remapping for {}:".format(name))
        for key, value in remapping.inverse_dict.items():
            report.add_text("\t{} = {}\n".format(key, value))
    """
    Plot individual marginals.
    """
    print("Creating marginal plots")
    for param, importance, is_log in data:
        param_name = param.name
        if param_name in param_name_to_log:
            is_log = param_name_to_log[param_name]
        if isinstance(param, CategoricalHyperparameter):
            vis.plot_categorical_marginal(param_name, show=False)
        else:
            vis.plot_marginal(param_name, show=False, log_scale=is_log)
        img = vu.save_image()
        report.add_image(
            img,
            "Marginal for {}.\nImportance = {}".format(param_name, importance),
        )
    """
    Plot pairwise marginals.
    """
    print("Creating pairwise-marginal plots")
    num_params = len(cs_params)
    num_pairs = num_params * (num_params + 1) // 2
    pair_and_importance = (f.get_most_important_pairwise_marginals(num_pairs))
    for combi, importance in pair_and_importance.items():
        param_names = []
        for p in combi:
            param_names.append(cs_params[p].name)
        info_text = "Pairwise Marginal for {}.\nImportance = {}".format(
            param_names,
            importance,
        ),
        if any(is_categorical(f, name) for name in param_names):
            report.add_text(info_text)
            continue
        plot_pairwise_marginal(vis, combi, show=False)
        img = vu.save_image()
        report.add_image(
            img,
            txt=info_text,
        )
    """
    List the top 10 parameters.
    """
    N = min(10, len(Y))
    Y[np.isnan(Y)] = np.nanmin(Y) - 1
    best_idxs = Y.argsort()[-N:][::-1]
    all_param_names = [p.name for p in config_space.get_hyperparameters()]
    for rank, i in enumerate(best_idxs):
        variant = variants_list[i]
        report.add_text("Rank {} params, with score = {}:".format(
            rank + 1, Y[i]))
        for name, value in zip(all_param_names, X[i, :]):
            report.add_text("\t{} = {}\n".format(name, value))
        report.add_text("\texp_name = {}\n".format(variant['exp_name']))
        report.add_text("\tunique_id = {}\n".format(variant['unique_id']))

    print("Guesses for is_log")
    print("{")
    for param, _, is_log in data:
        name = param.name
        print("    '{}': {},".format(name, is_log))
    print("}")
    """
    Ask user if they want to see the report
    """
    abs_path = osp.abspath(report.path)
    print("Report saved to: {}".format(abs_path))
    report.save()
    open_report = query_yes_no("Open report?", default="yes")
    if open_report:
        cmd = "xdg-open {}".format(abs_path)
        print(cmd)
        subprocess.call(cmd, shell=True)