Beispiel #1
0
    def test_extract_trajectory(self):
        res = plot_util.extract_trajectory(experiment=branin_experiment,
                                           maxvalue=1000)
        expected_trajectory = ([1000.0] * 1) \
                              + ([24.129964413622268] * 2) \
                              + ([16.787197168652479] * 5) \
                              + ([12.779763397329571] * 3) \
                              + ([2.6962005134978178] * 6) \
                              + ([0.72639128754424731] * 4)
        self.assertListAlmostEqual(expected=expected_trajectory, actual=res)

        res = plot_util.extract_trajectory(branin_experiment,
                                           cut=22,
                                           maxvalue=1000)
        self.assertListAlmostEqual(expected_trajectory, res)

        res = plot_util.extract_trajectory(branin_experiment,
                                           cut=10,
                                           maxvalue=1000)
        self.assertListAlmostEqual(expected_trajectory[:10], res)
        self.assertAlmostEqual(12.779763397329571, res[-1])

        self.assertRaises(ValueError, plot_util.extract_trajectory,
                          branin_experiment, 0.5)

        self.assertRaises(ValueError, plot_util.extract_trajectory,
                          branin_experiment, 0)
def main(pkl_list, name_list, autofill, optimum=0, save="", title="", log=False,
         y_min=0, y_max=0, scale_std=1, cut=sys.maxint):

    trial_list = list()
    for i in range(len(pkl_list)):
        trial_list.append(list())
        for pkl in pkl_list[i]:
            fh = open(pkl, "r")
            trials = cPickle.load(fh)
            fh.close()

            trace = plot_util.extract_trajectory(trials, cut=cut)
            trial_list[-1].append(np.array(trace))

    for i in range(len(trial_list)):
        max_len = max([len(ls) for ls in trial_list[i]])
        for t in range(len(trial_list[i])):
            if len(trial_list[i][t]) < max_len and autofill:
                diff = max_len - len(trial_list[i][t])
                # noinspection PyUnusedLocal
                trial_list[i][t] = np.append(trial_list[i][t], [trial_list[i][t][-1] for x in range(diff)])
            elif len(trial_list[i][t]) < max_len and not autofill:
                raise ValueError("(%s != %s), Traces do not have the same length, please use -a" %
                                 (str(max_len), str(len(trial_list[i][t]))))

    plot_optimization_trace(trial_list, name_list, optimum, title=title, log=log,
                            save=save, y_min=y_min, y_max=y_max, scale_std=scale_std)

    if save != "":
        sys.stdout.write("Saved plot to " + save + "\n")
    else:
        sys.stdout.write("..Done\n")
def main(pkl_list,
         name_list,
         autofill,
         optimum=0,
         save="",
         title="",
         log=False,
         y_min=0,
         y_max=0,
         scale_std=1,
         cut=sys.maxint,
         xmax=100,
         every=10):

    trial_list = list()
    for i in range(len(pkl_list)):
        trial_list.append(list())
        for pkl in pkl_list[i]:
            fh = open(pkl, "r")
            trials = cPickle.load(fh)
            fh.close()

            trace = plot_util.extract_trajectory(trials, cut=cut)
            trial_list[-1].append(np.array(trace))

    for i in range(len(trial_list)):
        max_len = max([len(ls) for ls in trial_list[i]])
        for t in range(len(trial_list[i])):
            if len(trial_list[i][t]) < max_len and autofill:
                diff = max_len - len(trial_list[i][t])
                # noinspection PyUnusedLocal
                trial_list[i][t] = np.append(
                    trial_list[i][t],
                    [trial_list[i][t][-1] for x in range(diff)])
            elif len(trial_list[i][t]) < max_len and not autofill:
                raise ValueError(
                    "(%s != %s), Traces do not have the same length, please use -a"
                    % (str(max_len), str(len(trial_list[i][t]))))

    plot_optimization_trace(trial_list,
                            name_list,
                            optimum,
                            title=title,
                            log=log,
                            save=save,
                            y_min=y_min,
                            y_max=y_max,
                            scale_std=scale_std,
                            xmax=xmax,
                            every=every)

    if save != "":
        sys.stdout.write("Saved plot to " + save + "\n")
    else:
        sys.stdout.write("..Done\n")
Beispiel #4
0
    def test_extract_trajectory(self):
        res = plot_util.extract_trajectory(experiment=branin_experiment,
                                           maxvalue=1000)
        expected_trajectory = ([1000.0] * 1) \
                              + ([24.129964413622268] * 2) \
                              + ([16.787197168652479] * 5) \
                              + ([12.779763397329571] * 3) \
                              + ([2.6962005134978178] * 6) \
                              + ([0.72639128754424731] * 4)
        self.assertListAlmostEqual(expected=expected_trajectory, actual=res)

        res = plot_util.extract_trajectory(branin_experiment, cut=22, maxvalue=1000)
        self.assertListAlmostEqual(expected_trajectory, res)

        res = plot_util.extract_trajectory(branin_experiment, cut=10, maxvalue=1000)
        self.assertListAlmostEqual(expected_trajectory[:10], res)
        self.assertAlmostEqual(12.779763397329571, res[-1])

        self.assertRaises(ValueError, plot_util.extract_trajectory,
                         branin_experiment, 0.5)

        self.assertRaises(ValueError, plot_util.extract_trajectory,
                          branin_experiment, 0)
def main(pkl_list, name_list, autofill, optimum=0, save="", title="",
         log=False, y_min=0, y_max=0, scale_std=1, cut=sys.maxint, xmax=10, every=10):

    trial_list = list()
    times_list = list()

    for i in range(len(pkl_list)):
        tmp_trial_list = list()
        tmp_times_list = list()
        for pkl in pkl_list[i]:
            fh = open(pkl, "r")
            trials = cPickle.load(fh)
            fh.close()

            trace = plot_util.extract_trajectory(trials)
            times = plot_util.extract_runtime_timestamps(trials)
            tmp_times_list.append(times)
            tmp_trial_list.append(trace)
        # We feed this function with two lists of lists and get one list of lists and one list
        tmp_trial_list, tmp_times_list = fill_trajectories(tmp_trial_list, tmp_times_list)
        trial_list.append(tmp_trial_list)
        times_list.append(tmp_times_list)

    for i in range(len(trial_list)):
        max_len = max([len(ls) for ls in trial_list[i]])
        for t in range(len(trial_list[i])):
            if len(trial_list[i][t]) < max_len and autofill:
                diff = max_len - len(trial_list[i][t])
                # noinspection PyUnusedLocal
                trial_list[i][t] = np.append(trial_list[i][t], [trial_list[i][t][-1] for x in range(diff)])
            elif len(trial_list[i][t]) < max_len and not autofill:
                raise ValueError("(%s != %s), Traces do not have the same length, please use -a" %
                                 (str(max_len), str(len(trial_list[i][t]))))

    plot_optimization_trace(trial_list, name_list, times_list, optimum, title=title, log=log,
                            save=save, y_min=y_min, y_max=y_max, scale_std=scale_std, xmax=xmax, every=every)
    if save != "":
        sys.stdout.write("Saved plot to " + save + "\n")
    else:
        sys.stdout.write("..Done\n")
Beispiel #6
0
def main(pkl_list,
         name_list,
         autofill,
         optimum=0,
         save="",
         title="",
         maxvalue=sys.maxint,
         logy=False,
         logx=False,
         y_min=None,
         y_max=None,
         x_min=None,
         x_max=None,
         scale_std=1,
         properties=None,
         aggregation="mean",
         print_length_trial_list=True,
         ylabel="Minfunction value",
         xlabel="Duration [sec]"):

    trial_list = list()
    times_list = list()

    for i in range(len(pkl_list)):
        tmp_trial_list = list()
        tmp_times_list = list()
        for pkl in pkl_list[i]:
            fh = open(pkl, "r")
            trials = cPickle.load(fh)
            fh.close()
            trace = plot_util.extract_trajectory(trials, maxvalue=maxvalue)
            times = plot_util.extract_runtime_timestamps(trials=trials)
            if np.isnan(times[-1]):
                print "Last time is nan, removing trial"
                times = times[:-1]
                trace = trace[:-1]
            tmp_times_list.append(times)
            tmp_trial_list.append(trace)
        # We feed this function with two lists of lists and get
        # one list of lists and one list
        tmp_trial_list, tmp_times_list = plot_util.\
            fill_trajectories(tmp_trial_list, tmp_times_list)
        trial_list.append(tmp_trial_list)
        times_list.append(tmp_times_list)

    for i in range(len(trial_list)):
        max_len = max([len(ls) for ls in trial_list[i]])
        for t in range(len(trial_list[i])):
            if len(trial_list[i][t]) < max_len and autofill:
                diff = max_len - len(trial_list[i][t])
                # noinspection PyUnusedLocal
                trial_list[i][t] = np.append(
                    trial_list[i][t],
                    [trial_list[i][t][-1] for x in range(diff)])
            elif len(trial_list[i][t]) < max_len and not autofill:
                raise ValueError("(%s != %s), Traces do not have the same "
                                 "length, please use -a" %
                                 (str(max_len), str(len(trial_list[i][t]))))

    plot_trajectory.plot_trajectories(
        trial_list=trial_list,
        name_list=name_list,
        x_ticks=times_list,
        optimum=optimum,
        logy=logy,
        logx=logx,
        aggregation=aggregation,
        scale_std=scale_std,
        y_max=y_max,
        y_min=y_min,
        x_max=x_max,
        x_min=x_min,
        properties=properties,
        print_length_trial_list=print_length_trial_list,
        title=title,
        save=save,
        ylabel=ylabel,
        xlabel=xlabel)
    return
Beispiel #7
0
def main(pkl_list, name_list, autofill, optimum=0, save="", title="",
         maxvalue=sys.maxint, logy=False, logx=False,
         y_min=None, y_max=None, x_min=None, x_max=None,
         scale_std=1, properties=None,
         aggregation="mean", print_length_trial_list=True,
         ylabel="Minfunction value", xlabel="Duration [sec]"):

    trial_list = list()
    times_list = list()

    for i in range(len(pkl_list)):
        tmp_trial_list = list()
        tmp_times_list = list()
        for pkl in pkl_list[i]:
            fh = open(pkl, "r")
            trials = cPickle.load(fh)
            fh.close()
            trace = plot_util.extract_trajectory(trials, maxvalue=maxvalue)
            times = plot_util.extract_runtime_timestamps(trials=trials)
            if np.isnan(times[-1]):
                print "Last time is nan, removing trial"
                times = times[:-1]
                trace = trace[:-1]
            tmp_times_list.append(times)
            tmp_trial_list.append(trace)
        # We feed this function with two lists of lists and get
        # one list of lists and one list
        tmp_trial_list, tmp_times_list = plot_util.\
            fill_trajectories(tmp_trial_list, tmp_times_list)
        trial_list.append(tmp_trial_list)
        times_list.append(tmp_times_list)

    for i in range(len(trial_list)):
        max_len = max([len(ls) for ls in trial_list[i]])
        for t in range(len(trial_list[i])):
            if len(trial_list[i][t]) < max_len and autofill:
                diff = max_len - len(trial_list[i][t])
                # noinspection PyUnusedLocal
                trial_list[i][t] = np.append(trial_list[i][t],
                                             [trial_list[i][t][-1]
                                              for x in range(diff)])
            elif len(trial_list[i][t]) < max_len and not autofill:
                raise ValueError("(%s != %s), Traces do not have the same "
                                 "length, please use -a" %
                                 (str(max_len), str(len(trial_list[i][t]))))

    plot_trajectory.plot_trajectories(trial_list=trial_list,
                                      name_list=name_list,
                                      x_ticks=times_list,
                                      optimum=optimum,
                                      logy=logy, logx=logx,
                                      aggregation=aggregation,
                                      scale_std=scale_std,
                                      y_max=y_max, y_min=y_min,
                                      x_max=x_max, x_min=x_min,
                                      properties=properties,
                                      print_length_trial_list=
                                      print_length_trial_list,
                                      title=title, save=save,
                                      ylabel=ylabel, xlabel=xlabel)
    return
def main(pkl_list,
         name_list,
         autofill,
         optimum=0,
         save="",
         title="",
         log=False,
         y_min=0,
         y_max=0,
         scale_std=1,
         cut=sys.maxint):

    trial_list = list()
    times_list = list()

    for i in range(len(pkl_list)):
        tmp_trial_list = list()
        tmp_times_list = list()
        for pkl in pkl_list[i]:
            fh = open(pkl, "r")
            trials = cPickle.load(fh)
            fh.close()

            trace = plot_util.extract_trajectory(trials)
            times = plot_util.extract_runtime_timestamps(trials)
            tmp_times_list.append(times)
            tmp_trial_list.append(trace)
        # We feed this function with two lists of lists and get one list of lists and one list
        tmp_trial_list, tmp_times_list = fill_trajectories(
            tmp_trial_list, tmp_times_list)
        trial_list.append(tmp_trial_list)
        times_list.append(tmp_times_list)

    for i in range(len(trial_list)):
        max_len = max([len(ls) for ls in trial_list[i]])
        for t in range(len(trial_list[i])):
            if len(trial_list[i][t]) < max_len and autofill:
                diff = max_len - len(trial_list[i][t])
                # noinspection PyUnusedLocal
                trial_list[i][t] = np.append(
                    trial_list[i][t],
                    [trial_list[i][t][-1] for x in range(diff)])
            elif len(trial_list[i][t]) < max_len and not autofill:
                raise ValueError(
                    "(%s != %s), Traces do not have the same length, please use -a"
                    % (str(max_len), str(len(trial_list[i][t]))))

    plot_optimization_trace(trial_list,
                            name_list,
                            times_list,
                            optimum,
                            title=title,
                            log=log,
                            save=save,
                            y_min=y_min,
                            y_max=y_max,
                            scale_std=scale_std)
    if save != "":
        sys.stdout.write("Saved plot to " + save + "\n")
    else:
        sys.stdout.write("..Done\n")
Beispiel #9
0
def main(pkl_list,
         name_list,
         autofill,
         optimum=0,
         save="",
         title="",
         log=False,
         maxvalue=sys.maxint,
         y_min=None,
         y_max=None,
         scale_std=1,
         aggregation="mean",
         cut=sys.maxint,
         xlabel="#Function evaluations",
         ylabel="Loss",
         properties=None,
         print_lenght_trial_list=False,
         plot_test_performance=False):

    trial_list = list()
    test_list = list()

    x_ticks = list()
    for i in range(len(pkl_list)):
        trial_list.append(list())
        test_list.append(list())

        for pkl in pkl_list[i]:
            if pkl in plot_util.cache:
                trials = plot_util.cache[pkl]
            else:
                fh = open(pkl, "r")
                trials = cPickle.load(fh)
                fh.close()
                plot_util.cache[pkl] = trials

            if plot_test_performance:
                trace, test = plot_util.extract_trajectory(trials,
                                                           cut=cut,
                                                           maxvalue=maxvalue,
                                                           test=True)
                # TODO: We can only plot one test result
                if len(test) > 1:
                    raise NotImplementedError(
                        "Cannot yet plot more than one testresult")
                else:
                    test = test[0]
                print test_list
                if len(test_list) == 0 or len(test_list[-1]) == 0:
                    test_list[-1].extend([[
                        test[0],
                    ], [
                        test[1],
                    ]])
                else:
                    test_list[-1][0].append(test[0])
                    test_list[-1][1].append(test[1])
            else:
                trace = plot_util.extract_trajectory(trials,
                                                     cut=cut,
                                                     maxvalue=maxvalue,
                                                     test=False)
            trial_list[-1].append(np.array(trace))

    if not plot_test_performance:
        test_list = None

    for i in range(len(trial_list)):
        max_len = max([len(ls) for ls in trial_list[i]])
        for t in range(len(trial_list[i])):
            if len(trial_list[i][t]) < max_len and autofill:
                diff = max_len - len(trial_list[i][t])
                # noinspection PyUnusedLocal
                trial_list[i][t] = np.append(
                    trial_list[i][t],
                    [trial_list[i][t][-1] for x in range(diff)])
            elif len(trial_list[i][t]) < max_len and not autofill:
                raise ValueError("(%s != %s), Traces do not have the same "
                                 "length, please use -a" %
                                 (str(max_len), str(len(trial_list[i][t]))))
        x_ticks.append(range(np.max([len(ls) for ls in trial_list[i]])))

    plot_trajectory.plot_trajectories(
        trial_list=trial_list,
        name_list=name_list,
        x_ticks=x_ticks,
        optimum=optimum,
        y_min=y_min,
        y_max=y_max,
        title=title,
        ylabel=ylabel,
        xlabel=xlabel,
        logy=log,
        save=save,
        aggregation=aggregation,
        properties=properties,
        scale_std=scale_std,
        test_trials=test_list,
        print_length_trial_list=print_lenght_trial_list)
    return