def cma_iter_plot_scatter3d_candidates(all_variables, parameters=None):
    candidates = parameters['candidates']
    fitness = parameters['fitness']

    # Take candidates and fitness and append them
    candidates.extend(all_variables['parameters_candidates_array'])
    fitness.extend(all_variables['fitness_results'].tolist())

    candidates_arr = np.array(candidates)
    fitness_arr = np.array(fitness)
    parameter_names_sorted = all_variables['parameter_names_sorted']

    # Save data
    if parameters['dataio'] is not None:
        parameters['dataio'].save_variables_default(locals(), ['candidates', 'fitness', 'parameter_names_sorted'])
        parameters['dataio'].make_link_output_to_dropbox(dropbox_current_experiment_folder='cmaes_fit_experiment')

    # Do plots
    # Param 1 vs 2 vs 3
    parameters['ax'] = utils.scatter3d(candidates_arr[:, 0], candidates_arr[:, 1], candidates_arr[:, 2], c=np.log(fitness_arr), xlabel=parameter_names_sorted[0], ylabel=parameter_names_sorted[1], zlabel=parameter_names_sorted[2], ax_handle=parameters['ax'])
    if parameters['dataio'] is not None:
      parameters['dataio'].save_current_figure('cmaes_optim_landscape_123_scatter3d_{label}_{unique_id}.pdf')

    # Param 1 vs 2 vs 4
    parameters['ax'] = utils.scatter3d(candidates_arr[:, 0], candidates_arr[:, 1], candidates_arr[:, 3], c=np.log(fitness_arr), xlabel=parameter_names_sorted[0], ylabel=parameter_names_sorted[1], zlabel=parameter_names_sorted[3], ax_handle=parameters['ax'])
    if parameters['dataio'] is not None:
      parameters['dataio'].save_current_figure('cmaes_optim_landscape_124_scatter3d_{label}_{unique_id}.pdf')

    # Param 2 vs 3 vs 4
    parameters['ax'] = utils.scatter3d(candidates_arr[:, 1], candidates_arr[:, 2], candidates_arr[:, 3], c=np.log(fitness_arr), xlabel=parameter_names_sorted[1], ylabel=parameter_names_sorted[2], zlabel=parameter_names_sorted[3], ax_handle=parameters['ax'])
    if parameters['dataio'] is not None:
      parameters['dataio'].save_current_figure('cmaes_optim_landscape_234_scatter3d_{label}_{unique_id}.pdf')
        def plot_scatter(all_vars, result_dist_to_use_name, title='', log_color=True, downsampling=1, label_file=''):

            fig = plt.figure()
            ax = Axes3D(fig)

            result_dist_to_use = all_vars[result_dist_to_use_name]
            if not log_color:
                result_dist_to_use = np.exp(result_dist_to_use)

            utils.scatter3d(result_parameters_flat[:, 0], result_parameters_flat[:, 1], result_parameters_flat[:, 2], s=size_normal_points, c=np.log(result_dist_to_use), xlabel=parameter_names_sorted[0], ylabel=parameter_names_sorted[1], zlabel=parameter_names_sorted[2], title=title, ax_handle=ax)
            best_points_result_dist_to_use = np.argsort(result_dist_to_use)[:nb_best_points]
            utils.scatter3d(result_parameters_flat[best_points_result_dist_to_use, 0], result_parameters_flat[best_points_result_dist_to_use, 1], result_parameters_flat[best_points_result_dist_to_use, 2], c='r', s=size_best_points, ax_handle=ax)
            print "Best points, %s:" % title
            print '\n'.join(['sigma output %.2f, ratio %.2f, sigmax %.2f:  %f' % (result_parameters_flat[i, 0], result_parameters_flat[i, 1], result_parameters_flat[i, 2], result_dist_to_use[i]) for i in best_points_result_dist_to_use])

            if savefigs:
                dataio.save_current_figure('scatter3d_%s%s_{label}_{unique_id}.pdf' % (result_dist_to_use_name, label_file))

                if savemovies:
                    try:
                        utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s%s_{label}_{unique_id}.mp4' % (result_dist_to_use_name, label_file)), bitrate=8000, min_duration=8)
                        utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s%s_{label}_{unique_id}.gif' % (result_dist_to_use_name, label_file)), nb_frames=30, min_duration=8)
                    except Exception:
                        # Most likely wrong aggregator...
                        print "failed when creating movies for ", result_dist_to_use_name

                ax.view_init(azim=90, elev=10)
                dataio.save_current_figure('scatter3d_view2_%s%s_{label}_{unique_id}.pdf' % (result_dist_to_use_name, label_file))

            return ax
예제 #3
0
def _show3d(self,
            bmin=None,
            bmax=None,
            margin=0.2,
            N_grid=20,
            levels=None,
            scatter_rule=None):

    if (bmin is None) or (bmax is None):
        bmin, bmax = self.gp.get_boundary(margin=margin)

    fig = plt.figure()
    ax = Axes3D(fig)
    fax = (fig, ax)
    raise NotImplementedError("under construction: delete rule")

    def rule(Y):
        idxes_1 = []
        idxes_2 = []
        for idx in range(len(Y)):
            y = Y[idx]
            if y > 0.0:
                idxes_1.append(idx)
            else:
                idxes_2.append(idx)
        return [(idxes_1, "blue"), (idxes_2, "red")]

    utils.scatter3d(rule, self.gp.X, self.gp.Y, fax)
예제 #4
0
def _show3d(self,
            bmin=None,
            bmax=None,
            margin=0.2,
            N_grid=20,
            levels=None,
            scatter_rule=None):

    if (bmin is None) or (bmax is None):
        bmin, bmax = self.get_boundary(margin=margin)

    fig = plt.figure()
    ax = Axes3D(fig)
    fax = (fig, ax)

    if scatter_rule is None:
        ## Y -> [(idxes_1, c1), ..., (idxes_n, cn)]
        def f(Y):
            idxes = range(len(Y))
            color = "red"
            return [(idxes, color)]

        scatter_rule = f

    utils.scatter3d(scatter_rule, self.X, self.Y, fax)
    def plot_scatter(all_vars, result_dist_to_use_name, title='', log_color=True, downsampling=1, label_file='', mask_outliers=True):

        result_dist_to_use = all_vars[result_dist_to_use_name]
        result_parameters_flat = all_vars['result_parameters_flat']

        # Filter if downsampling
        filter_downsampling = np.arange(0, result_dist_to_use.size, downsampling)
        result_dist_to_use = result_dist_to_use[filter_downsampling]
        result_parameters_flat = result_parameters_flat[filter_downsampling]

        if mask_outliers:
            result_dist_to_use = mask_outliers_array(result_dist_to_use)

        best_points_result_dist_to_use = np.argsort(result_dist_to_use)[:nb_best_points]

        # Construct all permutations of 3 parameters, for 3D scatters
        params_permutations = set([tuple(np.sort(np.random.choice(result_parameters_flat.shape[-1], 3, replace=False)).tolist()) for i in xrange(1000)])

        for param_permut in params_permutations:
            fig = plt.figure()
            ax = Axes3D(fig)

            # One plot per parameter permutation
            if log_color:
                color_points = np.log(result_dist_to_use)
            else:
                color_points = result_dist_to_use

            utils.scatter3d(result_parameters_flat[:, param_permut[0]], result_parameters_flat[:, param_permut[1]], result_parameters_flat[:, param_permut[2]], s=size_normal_points, c=color_points, xlabel=parameter_names_sorted[param_permut[0]], ylabel=parameter_names_sorted[param_permut[1]], zlabel=parameter_names_sorted[param_permut[2]], title=title, ax_handle=ax)

            utils.scatter3d(result_parameters_flat[best_points_result_dist_to_use, param_permut[0]], result_parameters_flat[best_points_result_dist_to_use, param_permut[1]], result_parameters_flat[best_points_result_dist_to_use, param_permut[2]], c='r', s=size_best_points, ax_handle=ax)

            if savefigs:
                dataio.save_current_figure('scatter3d_%s_%s%s_{label}_{unique_id}.pdf' % (result_dist_to_use_name, '_'.join([parameter_names_sorted[i] for i in param_permut]), label_file))

            if savemovies:
                try:
                    utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s_%s%s_{label}_{unique_id}.mp4' % (result_dist_to_use_name, '_'.join([parameter_names_sorted[i] for i in param_permut]), label_file)), bitrate=8000, min_duration=8)
                    utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s_%s%s_{label}_{unique_id}.gif' % (result_dist_to_use_name, '_'.join([parameter_names_sorted[i] for i in param_permut]), label_file)), nb_frames=30, min_duration=8)
                except Exception:
                    # Most likely wrong aggregator...
                    print "failed when creating movies for ", result_dist_to_use_name


            if False and savefigs:
                ax.view_init(azim=90, elev=10)
                dataio.save_current_figure('scatter3d_view2_%s_%s%s_{label}_{unique_id}.pdf' % (result_dist_to_use_name, '_'.join([parameter_names_sorted[i] for i in param_permut]), label_file))

            # plt.close('all')

        print "Parameters: %s" % ', '.join(parameter_names_sorted)
        print "Best points, %s:" % title
        print '\n'.join([str_best_params(best_i, result_dist_to_use) for best_i in best_points_result_dist_to_use])
def cma_iter_plot_scatter3d_candidates(all_variables, parameters=None):
    candidates = parameters['candidates']
    fitness = parameters['fitness']

    # Take candidates and fitness and append them
    candidates.extend(all_variables['parameters_candidates_array'])
    fitness.extend(all_variables['fitness_results'].tolist())

    candidates_arr = np.array(candidates)
    fitness_arr = np.ma.masked_greater(np.array(fitness), 1e8)
    parameter_names_sorted = all_variables['parameter_names_sorted']

    # Save data
    if parameters['dataio'] is not None:
        parameters['dataio'].save_variables_default(locals(), ['candidates', 'fitness', 'parameter_names_sorted'])
        parameters['dataio'].make_link_output_to_dropbox(dropbox_current_experiment_folder='output_noise')

    # Best median parameters for now
    best_param_str = ' '.join(["%s: %s" % (param_val[0], np.round(param_val[1], 4)) for param_val in zip(parameter_names_sorted, np.median(candidates_arr[-100:], axis=0))])
    print "Best parameters: %s" % best_param_str

    # Do plots
    ax = utils.scatter3d(candidates_arr[:, 0], candidates_arr[:, 1], candidates_arr[:, 2], c=np.log(fitness_arr), xlabel=all_variables['parameter_names_sorted'][0], ylabel=all_variables['parameter_names_sorted'][1], zlabel=all_variables['parameter_names_sorted'][2], title=best_param_str)
    if parameters['dataio'] is not None:
        parameters['dataio'].save_current_figure('cmaes_optim_landscape_scatter3d_012_{label}_{unique_id}.pdf')
    plt.close(ax.get_figure())
    ax = utils.scatter3d(candidates_arr[:, 1], candidates_arr[:, 2], candidates_arr[:, 3], c=np.log(fitness_arr), xlabel=all_variables['parameter_names_sorted'][1], ylabel=all_variables['parameter_names_sorted'][2], zlabel=all_variables['parameter_names_sorted'][3], title=best_param_str)
    if parameters['dataio'] is not None:
        parameters['dataio'].save_current_figure('cmaes_optim_landscape_scatter3d_123_{label}_{unique_id}.pdf')
    plt.close(ax.get_figure())
    ax = utils.scatter3d(candidates_arr[:, 0], candidates_arr[:, 1], candidates_arr[:, 3], c=np.log(fitness_arr), xlabel=all_variables['parameter_names_sorted'][0], ylabel=all_variables['parameter_names_sorted'][1], zlabel=all_variables['parameter_names_sorted'][3], title=best_param_str)
    if parameters['dataio'] is not None:
        parameters['dataio'].save_current_figure('cmaes_optim_landscape_scatter3d_013_{label}_{unique_id}.pdf')
    plt.close(ax.get_figure())

    f, axes = plt.subplots(2, 1)
    axes[0].plot(candidates_arr)
    axes[0].set_xlabel('Time')
    axes[0].set_ylabel('Variables')
    axes[0].set_title('Best params: %s' % best_param_str)
    axes[0].legend(all_variables['parameter_names_sorted'])
    axes[1].plot(fitness_arr)
    axes[1].set_xlabel('Time')
    axes[1].set_ylabel('Fitness')
    axes[1].set_title('Current fitness: %s' % np.round(np.median(fitness_arr[-100:]), 4))
    f.canvas.draw()

    if parameters['dataio'] is not None:
        parameters['dataio'].save_current_figure('cmaes_optim_time_{label}_{unique_id}.pdf')

    plt.close(f)
def cma_iter_plot_scatter3d_candidates(all_variables, parameters=None):
    candidates = parameters["candidates"]
    fitness = parameters["fitness"]

    # Take candidates and fitness and append them
    candidates.extend(all_variables["parameters_candidates_array"])
    fitness.extend(all_variables["fitness_results"].tolist())

    candidates_arr = np.array(candidates)
    fitness_arr = np.array(fitness)
    parameter_names_sorted = all_variables["parameter_names_sorted"]

    # Save data
    if parameters["dataio"] is not None:
        parameters["dataio"].save_variables_default(locals(), ["candidates", "fitness", "parameter_names_sorted"])
        parameters["dataio"].make_link_output_to_dropbox(dropbox_current_experiment_folder="output_noise")

    # Do plots
    parameters["ax"] = utils.scatter3d(
        candidates_arr[:, 0],
        candidates_arr[:, 1],
        candidates_arr[:, 2],
        c=np.log(fitness_arr),
        xlabel=all_variables["parameter_names_sorted"][0],
        ylabel=all_variables["parameter_names_sorted"][1],
        zlabel=all_variables["parameter_names_sorted"][2],
        ax_handle=parameters["ax"],
    )
    if parameters["dataio"] is not None:
        parameters["dataio"].save_current_figure("cmaes_optim_landscape_scatter3d_{label}_{unique_id}.pdf")
def cma_iter_plot_scatter3d_candidates(all_variables, parameters=None):
    candidates = parameters['candidates']
    fitness = parameters['fitness']

    # Take candidates and fitness and append them
    candidates.extend(all_variables['parameters_candidates_array'])
    fitness.extend(all_variables['fitness_results'].tolist())

    candidates_arr = np.array(candidates)
    fitness_arr = np.array(fitness)
    parameter_names_sorted = all_variables['parameter_names_sorted']

    # Save data
    if parameters['dataio'] is not None:
        parameters['dataio'].save_variables_default(locals(), ['candidates', 'fitness', 'parameter_names_sorted'])
        parameters['dataio'].make_link_output_to_dropbox(dropbox_current_experiment_folder='output_noise')

    # Do plots
    parameters['ax'] = utils.scatter3d(candidates_arr[:, 0], candidates_arr[:, 1], candidates_arr[:, 2], c=np.log(fitness_arr), xlabel=all_variables['parameter_names_sorted'][0], ylabel=all_variables['parameter_names_sorted'][1], zlabel=all_variables['parameter_names_sorted'][2], ax_handle=parameters['ax'])
    if parameters['dataio'] is not None:
        parameters['dataio'].save_current_figure('cmaes_optim_landscape_scatter3d_{label}_{unique_id}.pdf')

    f, ax = plt.subplots()
    ax.plot(candidates_arr)
    ax.set_xlabel('Time')
    ax.set_ylabel('Variables')
    ax.set_title('Parameter optimisation')
    ax.legend(all_variables['parameter_names_sorted'])
    f.canvas.draw()

    if parameters['dataio'] is not None:
        parameters['dataio'].save_current_figure('cmaes_optim_time_{label}_{unique_id}.pdf')

    plt.close(f)
def cma_iter_plot_contourf_candidates(all_variables, parameters=None):
    candidates = parameters['candidates']
    fitness = parameters['fitness']

    # Take candidates and fitness and append them
    candidates.extend(all_variables['parameters_candidates_array'])
    fitness.extend(all_variables['fitness_results'].tolist())

    candidates_arr = np.array(candidates)
    fitness_arr = np.array(fitness)

    # Do a plot
    utils.scatter3d(candidates_arr[:, 0], candidates_arr[:, 1], candidates_arr[:, 2], c=np.log(fitness_arr), xlabel=all_variables['parameter_names_sorted'][0], ylabel=all_variables['parameter_names_sorted'][1], zlabel=all_variables['parameter_names_sorted'][2])

    if parameters['dataio'] is not None:
        parameters['dataio'].save_variables_default(locals(), ['candidates', 'fitness'])
        parameters['dataio'].save_current_figure('cmaes_optim_landscape_scatter3d_{label}_{unique_id}.pdf')
def cma_iter_plot_scatter3d_candidates(all_variables, parameters=None):
    candidates = parameters["candidates"]
    fitness = parameters["fitness"]

    # Take candidates and fitness and append them
    candidates.extend(all_variables["parameters_candidates_array"])
    fitness.extend(all_variables["fitness_results"].tolist())

    candidates_arr = np.array(candidates)
    fitness_arr = np.ma.masked_greater(np.array(fitness), 1e8)
    parameter_names_sorted = all_variables["parameter_names_sorted"]

    # Save data
    if parameters["dataio"] is not None:
        parameters["dataio"].save_variables_default(locals(), ["candidates", "fitness", "parameter_names_sorted"])
        parameters["dataio"].make_link_output_to_dropbox(
            dropbox_current_experiment_folder="sigmaoutput_normalisedsigmax_cmaes"
        )

    # Best median parameters for now
    best_param_str = " ".join(
        [
            "%s: %s" % (param_val[0], np.round(param_val[1], 4))
            for param_val in zip(parameter_names_sorted, np.median(candidates_arr[-100:], axis=0))
        ]
    ) + " > %f" % np.median(fitness_arr[-100:])
    print "Best parameters: %s" % best_param_str

    # Do plots
    ax = utils.scatter3d(
        candidates_arr[:, 0],
        candidates_arr[:, 1],
        candidates_arr[:, 2],
        c=np.log(fitness_arr),
        xlabel=all_variables["parameter_names_sorted"][0],
        ylabel=all_variables["parameter_names_sorted"][1],
        zlabel=all_variables["parameter_names_sorted"][2],
        title=best_param_str,
    )
    if parameters["dataio"] is not None:
        parameters["dataio"].save_current_figure("cmaes_optim_landscape_scatter3d_012_{label}_{unique_id}.pdf")
    plt.close(ax.get_figure())
    ax = utils.scatter3d(
        candidates_arr[:, 1],
        candidates_arr[:, 2],
        candidates_arr[:, 3],
        c=np.log(fitness_arr),
        xlabel=all_variables["parameter_names_sorted"][1],
        ylabel=all_variables["parameter_names_sorted"][2],
        zlabel=all_variables["parameter_names_sorted"][3],
        title=best_param_str,
    )
    if parameters["dataio"] is not None:
        parameters["dataio"].save_current_figure("cmaes_optim_landscape_scatter3d_123_{label}_{unique_id}.pdf")
    plt.close(ax.get_figure())
    ax = utils.scatter3d(
        candidates_arr[:, 0],
        candidates_arr[:, 1],
        candidates_arr[:, 3],
        c=np.log(fitness_arr),
        xlabel=all_variables["parameter_names_sorted"][0],
        ylabel=all_variables["parameter_names_sorted"][1],
        zlabel=all_variables["parameter_names_sorted"][3],
        title=best_param_str,
    )
    if parameters["dataio"] is not None:
        parameters["dataio"].save_current_figure("cmaes_optim_landscape_scatter3d_013_{label}_{unique_id}.pdf")
    plt.close(ax.get_figure())

    f, axes = plt.subplots(2, 1)
    axes[0].plot(candidates_arr)
    axes[0].set_xlabel("Time")
    axes[0].set_ylabel("Variables")
    axes[0].set_title("Best params: %s" % best_param_str)
    axes[0].legend(all_variables["parameter_names_sorted"])
    axes[1].plot(fitness_arr)
    axes[1].set_xlabel("Time")
    axes[1].set_ylabel("Fitness")
    axes[1].set_title("Current fitness: %s" % np.round(np.median(fitness_arr[-100:]), 4))
    f.canvas.draw()

    if parameters["dataio"] is not None:
        parameters["dataio"].save_current_figure("cmaes_optim_time_{label}_{unique_id}.pdf")

    plt.close(f)

    # same plot but normalized
    f, axes = plt.subplots(2, 1)
    axes[0].plot(candidates_arr / np.max(candidates_arr, axis=0))
    axes[0].set_xlabel("Time")
    axes[0].set_ylabel("Variables")
    axes[0].set_title("N Best params: %s" % best_param_str)
    axes[0].legend(all_variables["parameter_names_sorted"])
    axes[1].plot(fitness_arr)
    axes[1].set_xlabel("Time")
    axes[1].set_ylabel("Fitness")
    axes[1].set_title("N Current fitness: %s" % np.round(utils.nanmedian(fitness_arr[-100:]), 4))
    f.canvas.draw()

    if parameters["dataio"] is not None:
        parameters["dataio"].save_current_figure("cmaes_optim_time_normalized_{label}_{unique_id}.pdf")

    plt.close(f)
예제 #11
0
    c = [0.0]
    for i, j in indx:
        c.append(1.0)

    write_spda_file(sdp_filename, m, nblocks, size, c, indx)
    os.system("/usr/local/bin/csdp {0} {1}".format(sdp_filename, sol_filename))
    y, Z, X = read_sol_file(sol_filename, size=49)
    u, s, v = la.svd(X)
    Y = u * np.sqrt(s)

    pylab.clf()
    pylab.plot(s[:10])
    save_many('exp_comb/mvu_scree')

    pylab.clf()
    scatter3d(Y[:, 0], Y[:, 1], Y[:, 2], c=colors)
    save_many('exp_comb/mvu')

    #
    # Isomap
    #

    X = expand(N)
    Y, s = mds(X, 3)

    pylab.clf()
    pylab.plot(s[:10])
    save_many('exp_comb/isomab_scree')

    pylab.clf()
    scatter3d(Y[:, 0], Y[:, 1], Y[:, 2], c=colors)
예제 #12
0
파일: comb.py 프로젝트: stober/lspi
        c.append(1.0)

    write_spda_file(sdp_filename, m, nblocks, size, c, indx)
    os.system("/usr/local/bin/csdp {0} {1}".format(sdp_filename,
                                                       sol_filename))
    y, Z, X = read_sol_file(sol_filename, size=49)
    u, s, v = la.svd(X)
    Y = u * np.sqrt(s)

    pylab.clf()
    pylab.plot(s[:10])
    save_many('exp_comb/mvu_scree')


    pylab.clf()
    scatter3d(Y[:, 0], Y[:, 1], Y[:, 2], c=colors)
    save_many('exp_comb/mvu')

    #
    # Isomap
    #

    X = expand(N)
    Y, s = mds(X, 3)

    pylab.clf()
    pylab.plot(s[:10])
    save_many('exp_comb/isomab_scree')

    pylab.clf()
    scatter3d(Y[:, 0], Y[:, 1], Y[:, 2], c=colors)