示例#1
0
def get_performance_data(file_list, name_list, maxvalue):
    # Get data from csv
    performance = list()
    time_ = list()
    name_list_test_train = []
    for name in range(len(name_list)):
        # We have a new experiment
        trn_perf = []
        tst_perf = []
        time_for_name = []

        for fl in file_list[name]:
            _none, csv_data = read_util.read_csv(fl, has_header=True)
            csv_data = np.array(csv_data)
            # Replace too high values with args.maxint
            train_data = [
                min([maxvalue, float(i.strip())]) for i in csv_data[:, 1]
            ]
            test_data = [
                min([maxvalue, float(i.strip())]) for i in csv_data[:, 2]
            ]
            time_data = [float(i.strip()) for i in csv_data[:, 0]]

            trn_perf.append(train_data)
            tst_perf.append(test_data)
            time_for_name.append(time_data)

        merged_performances = trn_perf + tst_perf
        len_trn_perf = len(trn_perf)
        merged_time = time_for_name + time_for_name

        merged_performances, merged_time = merge_test_performance_different_times. \
            fill_trajectory(merged_performances, merged_time)
        trn_perf = merged_performances[:, :len_trn_perf]
        tst_perf = merged_performances[:, len_trn_perf:]

        # Convert to numpy arrays
        trn_perf = np.array(trn_perf)
        tst_perf = np.array(tst_perf)
        merged_time = np.array(merged_time)

        # Append performance to global plotting array
        name_list_test_train.append("%s_train" % name_list[name])
        performance.append(trn_perf.transpose())
        name_list_test_train.append("%s_test" % name_list[name])
        performance.append(tst_perf.transpose())
        # Append the time twice, once for the test, once for the training array
        time_.append(merged_time)
        time_.append(merged_time)
    performance = np.array([np.array(i) for i in performance])
    time_ = np.array(time_)
    # This plotting function requires a time array for each experiment
    # new_time_list = np.array([time_ for i in range(len(performance))])
    new_time_list = np.array(time_)
    return name_list_test_train, new_time_list, performance
示例#2
0
def main():
    prog = "python plot_scatter.py any.csv"
    description = "Reads performances from a two-column .csv and creates a" \
                  " scatterplot"

    parser = ArgumentParser(description=description, prog=prog)

    # General Options
    # parser.add_argument("-l", "--log", action="store_true", dest="log",
    #                     default=False, help="Plot on log scale")
    parser.add_argument("--max",
                        dest="max",
                        type=float,
                        default=1000,
                        help="Maximum of both axes")
    parser.add_argument("--min",
                        dest="min",
                        type=float,
                        default=None,
                        help="Minimum of both axes")
    parser.add_argument("-s",
                        "--save",
                        dest="save",
                        default="",
                        help="Where to save plot instead of showing it?")
    parser.add_argument("--title",
                        dest="title",
                        default="",
                        help="Optional supertitle for plot")
    parser.add_argument("--greyFactor",
                        dest="grey_factor",
                        type=float,
                        default=1,
                        help="If an algorithms is not greyFactor-times better"
                        " than the other, show this point less salient, > 1")
    parser.add_argument("-v",
                        "--verbose",
                        dest="verbose",
                        action="store_true",
                        default=False,
                        help="Plot some debug info")
    parser.add_argument("-f",
                        "--lineFactors",
                        dest="linefactors",
                        default=None,
                        help="Plot X speedup/slowdown, format "
                        "'X,..,X' (no spaces)")
    parser.add_argument(
        "-c",
        "--columns",
        dest="columns",
        default='1,2',
        help="Use these columns from csv; starting at 1, format"
        " 'xaxis,yaxis' (nospaces)")
    parser.add_argument("--size",
                        dest="user_fontsize",
                        default=12,
                        type=int,
                        help="Standard fontsize")
    parser.add_argument("--dpi",
                        dest="dpi",
                        default=100,
                        type=int,
                        help="DPI for saved figure")

    args, unknown = parser.parse_known_args()

    if len(unknown) != 1:
        print "Wrong number of arguments"
        parser.print_help()
        sys.exit(1)

    if args.grey_factor < 1:
        print "A grey-factor lower than one makes no sense"
        parser.print_help()
        sys.exit(1)

    # Check selected columns
    columns = [int(float(i)) for i in args.columns.split(",")]
    if len(columns) != 2:
        raise ValueError("Selected more or less than two columns: %s" %
                         str(columns))
    # As python starts with 0
    columns = [i - 1 for i in columns]

    # Load validationResults
    res_header, res_data = read_util.read_csv(unknown[0],
                                              has_header=True,
                                              data_type=np.float)
    res_data = np.array(res_data)
    print "Found %s points" % (str(res_data.shape))

    # Get data
    if max(columns) > res_data.shape[1] - 1:
        raise ValueError("You selected column %d, but there are only %d" %
                         (max(columns) + 1, res_data.shape[1]))
    if min(columns) < 0:
        raise ValueError("You selected a column number less than 1")
    data_x = res_data[:, columns[0]]
    data_y = res_data[:, columns[1]]
    label_x = res_header[columns[0]]
    label_y = res_header[columns[1]]

    linefactors = list()
    if args.linefactors is not None:
        linefactors = [float(i) for i in args.linefactors.split(",")]
        if len(linefactors) < 1:
            print "Something is wrong with linefactors: %s" % args.linefactors
            sys.exit(1)
        if min(linefactors) < 1:
            print "A line-factor lower than one makes no sense"
            sys.exit(1)
    if args.grey_factor > 1 and args.grey_factor not in linefactors:
        linefactors.append(args.grey_factor)

    fig = scatter.plot_scatter_plot(x_data=data_x,
                                    y_data=data_y,
                                    labels=[label_x, label_y],
                                    title=args.title,
                                    max_val=args.max,
                                    min_val=args.min,
                                    grey_factor=args.grey_factor,
                                    linefactors=linefactors,
                                    debug=args.verbose,
                                    user_fontsize=args.user_fontsize,
                                    dpi=args.dpi)

    if args.save != "":
        print "Save plot to %s" % args.save
        plot_util.save_plot(fig, args.save, plot_util.get_defaults()['dpi'])
    else:
        fig.show()
def main():
    prog = "python plot_test_of_best_train.py <WhatIsThis> one/or/many/" \
           "*ClassicValidationResults*.csv"
    description = "Plot a test error of best training trial"

    parser = ArgumentParser(description=description, prog=prog)

    # General Options
    parser.add_argument("--logy",
                        action="store_true",
                        dest="logy",
                        default=False,
                        help="Plot y-axis on on log scale")
    parser.add_argument("--ymax",
                        dest="ymax",
                        type=float,
                        default=None,
                        help="Maximum of the y-axis")
    parser.add_argument("--ymin",
                        dest="ymin",
                        type=float,
                        default=None,
                        help="Minimum of the y-axis")
    parser.add_argument("--xmax",
                        dest="xmax",
                        type=float,
                        default=None,
                        help="Maximum of the x-axis")
    parser.add_argument("--xmin",
                        dest="xmin",
                        type=float,
                        default=None,
                        help="Minimum of the x-axis")

    parser.add_argument("-s",
                        "--save",
                        dest="save",
                        default="",
                        help="Where to save plot instead of showing it?")
    parser.add_argument("-t",
                        "--title",
                        dest="title",
                        default="",
                        help="Optional supertitle for plot")
    parser.add_argument("--maxvalue",
                        dest="maxvalue",
                        type=float,
                        default=sys.maxint,
                        help="Replace all values higher than this?")
    parser.add_argument("--ylabel",
                        dest="ylabel",
                        default="loss",
                        help="Label on y-axis")
    parser.add_argument("-v",
                        "--verbose",
                        dest="verbose",
                        action="store_true",
                        default=False,
                        help="print number of runs on plot")

    # Properties
    # We need this to show defaults for -h
    defaults = plot_util.get_defaults()
    for key in defaults:
        parser.add_argument("--%s" % key,
                            dest=key,
                            default=None,
                            help="%s, default: %s" % (key, str(defaults[key])))
    args, unknown = parser.parse_known_args()

    if len(unknown) < 2:
        print "To less arguments given"
        parser.print_help()
        sys.exit(1)

    sys.stdout.write("Found " + str(len(unknown)) + " arguments\n")

    # Get files and names
    file_list, name_list = read_util.get_file_and_name_list(unknown,
                                                            match_file='.csv')
    for idx in range(len(name_list)):
        print "%20s contains %d file(s)" % (name_list[idx], len(
            file_list[idx]))

    if args.verbose:
        name_list = [
            name_list[i] + " (" + str(len(file_list[i])) + ")"
            for i in range(len(name_list))
        ]

    # Get data from csv
    train_performance = list()
    test_performance = list()
    time_ = list()
    for name in range(len(name_list)):
        # We have a new experiment
        train_performance.append(list())
        test_performance.append(list())
        for fl in file_list[name]:
            _none, csv_data = read_util.read_csv(fl, has_header=True)
            csv_data = np.array(csv_data)
            # Replace too high values with args.maxint
            train_performance[-1].append([
                min([args.maxvalue, float(i.strip())]) for i in csv_data[:, 1]
            ])
            test_performance[-1].append([
                min([args.maxvalue, float(i.strip())]) for i in csv_data[:, 2]
            ])
            time_.append([float(i.strip()) for i in csv_data[:, 0]])
            # Check whether we have the same times for all runs
            if len(time_) == 2:
                if time_[0] == time_[1]:
                    time_ = [
                        time_[0],
                    ]
                else:
                    raise NotImplementedError(".csv's do not use the same "
                                              "timesteps")

    train_performance = [np.array(i) for i in train_performance]
    test_performance = [np.array(i) for i in test_performance]
    time_ = np.array(time_).flatten()

    # Now get the test results for the best train performance
    # All arrays are numExp x numTrials
    test_of_best_train = list()
    min_test = list()
    max_test = list()
    for i in range(len(train_performance)):
        test_of_best_train.append(
            get_test_of_best_train(train_performance[i], test_performance[i]))
        min_test.append(np.min(test_performance[i], 0))
        max_test.append(np.max(test_performance[i], 0))

    prop = {}
    args_dict = vars(args)
    for key in defaults:
        prop[key] = args_dict[key]

    fig = plot_optimization_trace(times=time_,
                                  performance_list=test_of_best_train,
                                  min_test=min_test,
                                  max_test=max_test,
                                  name_list=name_list,
                                  title=args.title,
                                  logy=args.logy,
                                  logx=True,
                                  properties=prop,
                                  y_min=args.ymin,
                                  y_max=args.ymax,
                                  x_min=args.xmin,
                                  x_max=args.xmax,
                                  ylabel=args.ylabel)
    if args.save != "":
        print "Save plot to %s" % args.save
        plot_util.save_plot(fig, args.save, plot_util.get_defaults()['dpi'])
    else:
        fig.show()
示例#4
0
def main():
    prog = "python plot_scatter.py"
    description = "Plots performances of the best config at one time vs " \
                  "another in a scatter plot"

    parser = ArgumentParser(description=description, prog=prog)

    # General Options
    parser.add_argument("--max", dest="max", type=float,
                        default=1000, help="Maximum of both axes")
    parser.add_argument("--min", dest="min", type=float,
                        default=None, help="Minimum of both axes")
    parser.add_argument("-s", "--save", dest="save",
                        default="", help="Where to save plot instead of "
                                         "showing it?")
    parser.add_argument("--title", dest="title",
                        default="", help="Optional supertitle for plot")
    parser.add_argument("--greyFactor", dest="grey_factor", type=float,
                        default=1, help="If an algorithms is not greyFactor-"
                                        "times better than the other, show this"
                                        " point less salient, > 1")
    parser.add_argument("-v", "--verbose", dest="verbose", action="store_true",
                        default=False, help="Plot some debug info")
    parser.add_argument("-f", "--lineFactors", dest="linefactors",
                        default=None, help="Plot X speedup/slowdown,"
                                           " format 'X,..,X' (no spaces)")
    parser.add_argument("--time", dest="time", default=None,
                        help="Plot config at which time?, format 'time1,time2'")
    parser.add_argument("--obj", dest="obj", default=None, required=True,
                        help="Path to validationObjectiveMatrix-traj-* file")
    parser.add_argument("--res", dest="res", required=True,
                        help="Path to validationResults-traj-run-* file")
    parser.add_argument("--minvalue", dest="minvalue", type=float,
                        help="Replace all values smaller than this",)
    parser.add_argument("--fontsize", dest="fontsize", type=int, default=20,
                        help="Use this fontsize for plotting",)

    args, unknown = parser.parse_known_args()

    if len(unknown) != 0:
        print("Wrong number of arguments")
        parser.print_help()
        sys.exit(1)

    if args.grey_factor < 1:
        print("A grey-factor lower than one makes no sense")
        parser.print_help()
        sys.exit(1)

    # Load validationResults
    res_header, res_data = read_util.read_csv(args.res, has_header=True)
    av_times = [float(row[0]) for row in res_data]
    if args.time is None:
        # Print available times and quit
        print("Choose a time from")
        print("\n".join(["* %s" % i for i in av_times]))
        sys.exit(0)
    time_arr = args.time.split(",")
    if len(time_arr) != 2 or \
            (len(time_arr) == 2 and (time_arr[1] == "" or time_arr[0] == "")):
        print("Something wrong with %s, should be 'a,b'" % args.time)
        print("Choose a time from")
        print("\n".join(["* %s" % i for i in av_times]))
        sys.exit(0)
    time_1 = float(time_arr[0])
    time_2 = float(time_arr[1])

    # Now extract data
    config_1 = [int(float(row[len(res_header)-2].strip('"'))) for row in
                res_data if int(float(row[0])) == int(time_1)]
    config_2 = [int(float(row[len(res_header)-2].strip('"'))) for row in
                res_data if int(float(row[0])) == int(time_2)]
    if len(config_1) == 0 or len(config_2) == 0:
        print("Time int(%s) or int(%s) not found. Choose a time from:" %
              (time_1, time_2))
        print("\n".join(["* %s" % i for i in av_times]))
        sys.exit(1)
    config_1 = config_1[0]
    config_2 = config_2[0]

    obj_header, obj_data = read_util.read_csv(args.obj, has_header=True)
    head_template = '"Objective of validation config #%s"'
    idx_1 = obj_header.index(head_template % config_1)
    idx_2 = obj_header.index(head_template % config_2)

    data_one = np.array([float(row[idx_1].strip('"')) for row in obj_data])
    data_two = np.array([float(row[idx_2].strip('"')) for row in obj_data])

    print("Found %s points for config %d and %s points for config %d" %
          (str(data_one.shape), config_1, str(data_two.shape), config_2))

    linefactors = list()
    if args.linefactors is not None:
        linefactors = [float(i) for i in args.linefactors.split(",")]
        if len(linefactors) < 1:
            print("Something is wrong with linefactors: %s" % args.linefactors)
            sys.exit(1)
        if min(linefactors) < 1:
            print("A line-factor lower than one makes no sense")
            sys.exit(1)
    if args.grey_factor > 1 and args.grey_factor not in linefactors:
        linefactors.append(args.grey_factor)

    label_template = 'Objective of validation config #%s, best at %s sec'
    
    # This might produce overhead for large .csv files
    times = [int(float(row[0])) for row in res_data]
    time_1 = res_data[times.index(int(time_1))][0]
    time_2 = res_data[times.index(int(time_2))][0]

    data_one = np.array([max(args.minvalue, i) for i in data_one])
    data_two = np.array([max(args.minvalue, i) for i in data_two])

    fig = scatter.plot_scatter_plot(x_data=data_one, y_data=data_two,
                                    labels=[label_template %
                                            (config_1, str(time_1)),
                                            label_template %
                                            (config_2, str(time_2))],
                                    title=args.title,
                                    max_val=args.max, min_val=args.min,
                                    grey_factor=args.grey_factor,
                                    linefactors=linefactors,
                                    user_fontsize=args.fontsize,
                                    debug=args.verbose)
    if args.save != "":
        print("Save plot to %s" % args.save)
        plot_util.save_plot(fig, args.save, plot_util.get_defaults()['dpi'])
    else:
        fig.show()
def main():
    prog = "python get_percentage_solved.py <WhatIsThis> one/or/many/*RunResultLineMatrix-traj*.csv"
    description = "Returns a table with average percentage of solved instances"

    parser = ArgumentParser(description=description, prog=prog)

    # General Options
    parser.add_argument("-s",
                        "--save",
                        dest="save",
                        default="",
                        help="Where to save tex table instead of showing it?")
    parser.add_argument("-t",
                        "--title",
                        dest="title",
                        default="",
                        help="Optional supertitle for plot")
    parser.add_argument("-v",
                        "--verbose",
                        dest="verbose",
                        action="store_true",
                        default=False,
                        help="print number of runs on plot")
    args, unknown = parser.parse_known_args()

    # Get files and names
    file_list, name_list = read_util.get_file_and_name_list(unknown,
                                                            match_file='.csv')
    for idx in range(len(name_list)):
        print("%20s contains %d file(s)" %
              (name_list[idx], len(file_list[idx])))

    if args.verbose:
        name_list = [
            name_list[i] + " (" + str(len(file_list[i])) + ")"
            for i in range(len(name_list))
        ]

    # Get data from csv
    solutions = dict()
    allowed_solutions = ("SAT", "UNSAT", "TIMEOUT", "CRASHED", "KILLED")
    num_instances = None
    for name in range(len(name_list)):
        print("Read %s" % name_list[name])
        # We have a new experiment
        solutions[name_list[name]] = dict()
        for s in allowed_solutions:
            solutions[name_list[name]][s] = 0
        solutions[name_list[name]]["runs"] = 0

        for fl in file_list[name]:
            solutions[name_list[name]]["runs"] += 1
            _none, csv_data = read_util.read_csv(fl, has_header=True)
            if num_instances is None:
                num_instances = len(csv_data)
            else:
                assert num_instances == len(csv_data), \
                    "Found a different number of instances (%s): %f != %f" % \
                    (fl, num_instances, len(csv_data))
            # get last configuration
            for instance in range(num_instances):
                # -5 is the solution for the final incumbent
                # if there is no additional info
                sol = csv_data[instance][-5].replace('"', '').strip()
                try:
                    solutions[name_list[name]][sol] += 1
                except KeyError:
                    # -6 is the solution for the final incumbent
                    # if there is additional info
                    sol = csv_data[instance][-6].replace('"', '').strip()
                    solutions[name_list[name]][sol] += 1

    # Reorganize dictionary
    tab_dict = list()
    tab_dict.append([
        "",
    ])
    for key in sorted(solutions.keys()):
        tab_dict[0].append(key)

    # num runs
    tab_dict.append(["num_runs"])
    for key in sorted(solutions.keys()):
        tab_dict[-1].append(solutions[key]["runs"])

    # Mean of solutions
    for sol in sorted(allowed_solutions):
        ls = list([
            sol,
        ])
        for key in sorted(solutions.keys()):
            ls.append(
                round(solutions[key][sol] / float(solutions[key]["runs"]), 2))
        tab_dict.append(ls)

    # calc %solved
    tab_dict.append(["%SOLVED"])
    for key in sorted(solutions.keys()):
        per_solved = (solutions[key]["SAT"] + solutions[key]["UNSAT"]) / float(
            (np.sum([solutions[key][i] for i in allowed_solutions])))
        tab_dict[-1].append(round(per_solved, 4) * 100)

    print tabulate.tabulate(tab_dict, )
def main():
    prog = "python plot_performance <WhatIsThis> one/or/many/runs_and_res*.csv"
    description = "Plot a median trace with quantiles for multiple experiments"

    parser = ArgumentParser(description=description,
                            prog=prog,
                            formatter_class=ArgumentDefaultsHelpFormatter)

    # General Options
    parser.add_argument("-l",
                        "--log",
                        action="store_true",
                        dest="log",
                        default=False,
                        help="Plot on log scale")
    parser.add_argument("--ymax",
                        dest="ymax",
                        type=float,
                        default=None,
                        help="Maximum of the y-axis")
    parser.add_argument("--ymin",
                        dest="ymin",
                        type=float,
                        default=None,
                        help="Minimum of the y-axis")
    parser.add_argument("--xmax",
                        dest="xmax",
                        type=float,
                        default=None,
                        help="Maximum of the x-axis")
    parser.add_argument("--xmin",
                        dest="xmin",
                        type=float,
                        default=None,
                        help="Minimum of the x-axis")
    parser.add_argument("-s",
                        "--save",
                        dest="save",
                        default="",
                        help="Where to save plot instead of showing it?")
    parser.add_argument("-t",
                        "--title",
                        dest="title",
                        default="",
                        help="Optional supertitle for plot")
    parser.add_argument("--maxvalue",
                        dest="maxvalue",
                        type=float,
                        default=plottingscripts.utils.macros.MAXINT,
                        help="Replace all values higher than this?")
    parser.add_argument("-v",
                        "--verbose",
                        dest="verbose",
                        action="store_true",
                        default=False,
                        help="print number of runs on plot")
    parser.add_argument("--agglomeration",
                        dest="agglomeration",
                        type=str,
                        default="median",
                        help="Show mean or median",
                        choices=("mean", "median"))
    parser.add_argument("--ylabel", dest="ylabel", default="Performance")
    parser.add_argument("--optimum",
                        dest="optimum",
                        default=0,
                        type=float,
                        help="Plot difference to optimum")

    args, unknown = parser.parse_known_args()

    sys.stdout.write("\nFound " + str(len(unknown)) + " arguments\n")

    if len(unknown) < 2:
        print("To less arguments given")
        parser.print_help()
        sys.exit(1)

    # Get files and names
    file_list, name_list = read_util.get_file_and_name_list(unknown,
                                                            match_file=".")
    for idx in range(len(name_list)):
        print("%20s contains %d file(s)" %
              (name_list[idx], len(file_list[idx])))

    if args.verbose:
        name_list = [
            name_list[i] + " (" + str(len(file_list[i])) + ")"
            for i in range(len(name_list))
        ]

    # Get data from csv
    performance_list = list()
    time_list = list()

    show_from = -plottingscripts.utils.macros.MAXINT

    for name in range(len(name_list)):
        # We have a new experiment
        performance = list()
        time_ = list()
        for fl in file_list[name]:
            _none, csv_data = read_util.read_csv(fl, has_header=True)
            csv_data = np.array(csv_data)

            # Replace too high values with args.maxint
            data = [
                min([args.maxvalue,
                     float(i.strip()) - args.optimum]) for i in csv_data[:, 1]
            ]

            # do we have only non maxint data?
            show_from = max(data.count(args.maxvalue), show_from)

            performance.append(data)
            time_.append([float(i.strip()) for i in csv_data[:, 0]])
        if len(time_) > 1:
            performance, time_ = merge_test_performance_different_times.\
                fill_trajectory(performance_list=performance, time_list=time_)
            print(performance[0][:10])
        else:
            time_ = time_[0]
        performance = [np.array(i) for i in performance]
        time_ = np.array(time_)
        performance_list.append(performance)
        time_list.append(time_)

    if args.xmin is None and show_from != 0:
        args.xmin = show_from

    fig = plot_methods.\
        plot_optimization_trace_mult_exp(time_list=time_list,
                                         performance_list=performance_list,
                                         title=args.title,
                                         name_list=name_list,
                                         logx=args.log, logy=False,
                                         agglomeration=args.agglomeration,
                                         y_min=args.ymin,
                                         y_max=args.ymax,
                                         x_min=args.xmin,
                                         x_max=args.xmax,
                                         ylabel=args.ylabel)

    if args.save != "":
        print("Save plot to %s" % args.save)
        plot_util.save_plot(fig, args.save, plot_util.get_defaults()['dpi'])
    else:
        fig.show()
示例#7
0
def main():
    prog = "python plot_ac_overhead <WhatIsThis> one/or/many/*ValidationResults-traj-run*.csv"
    description = "Plot a median trace with quantiles for multiple experiments"

    parser = ArgumentParser(description=description, prog=prog)

    # General Options
    parser.add_argument("--logx", action="store_true", dest="logx",
                        default=False, help="Plot x-axis on log scale")
    parser.add_argument("--logy", action="store_true", dest="logy",
                        default=False, help="Plot y-axis on log scale")
    parser.add_argument("--ymax", dest="ymax", type=float,
                        default=None, help="Maximum of the y-axis")
    parser.add_argument("--ymin", dest="ymin", type=float,
                        default=None, help="Minimum of the y-axis")
    parser.add_argument("--xmax", dest="xmax", type=float,
                        default=None, help="Maximum of the x-axis")
    parser.add_argument("--xmin", dest="xmin", type=float,
                        default=None, help="Minimum of the x-axis")
    parser.add_argument("--ylabel", dest="ylabel", default=None,
                        help="Label on y-axis")
    parser.add_argument("-s", "--save", dest="save",
                        default="", help="Where to save plot instead of showing it?")
    parser.add_argument("-t", "--title", dest="title",
                        default="", help="Optional supertitle for plot")
    parser.add_argument("--maxvalue", dest="maxvalue", type=float,
                        default=sys.maxint, help="Replace all values higher than this?")
    parser.add_argument("--agglomeration", dest="agglomeration", type=str,
                        default="median", choices=("median", "mean"),
                        help="Plot mean or median")
    parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False,
                        help="print number of runs on plot")
    args, unknown = parser.parse_known_args()

    sys.stdout.write("\nFound " + str(len(unknown)) + " arguments\n")

    if len(unknown) < 2:
        print "To less arguments given"
        parser.print_help()
        sys.exit(1)

    if args.ylabel is None:
        args.ylabel = "%s AC overhead [sec]" % args.agglomeration

    # Get files and names
    file_list, name_list = read_util.get_file_and_name_list(unknown, match_file='.csv')
    for idx in range(len(name_list)):
        print "%20s contains %d file(s)" % (name_list[idx], len(file_list[idx]))

    if args.verbose:
        name_list = [name_list[i] + " (" + str(len(file_list[i])) + ")" for i in range(len(name_list))]

    # Get data from csv
    overhead = list()
    time_ = list()
    show_from = -sys.maxint

    for name in range(len(name_list)):
        # We have a new experiment
        overhead.append(list())
        for fl in file_list[name]:
            _none, csv_data = read_util.read_csv(fl, has_header=True)
            csv_data = np.array(csv_data)
            overhead[-1].append(csv_data[:, 3])
            time_.append([float(i.strip()) for i in csv_data[:, 0]])
            # Check whether we have the same times for all runs
            if len(time_) == 2:
                if time_[0] == time_[1]:
                    time_ = [time_[0], ]
                else:
                    raise NotImplementedError(".csv are not using the same times")
    overhead = [np.array(i, dtype=np.float32) for i in overhead]
    # print time_
    time_ = np.array(time_).flatten()

    # This plotting function requires a time array for each experiment
    new_time_list = [time_ for i in range(len(overhead))]
    fig = plot_methods.plot_optimization_trace_mult_exp(time_list=new_time_list,
                                                        performance_list=overhead,
                                                        title=args.title,
                                                        name_list=name_list,
                                                        logx=args.logx,
                                                        logy=args.logy,
                                                        y_min=args.ymin,
                                                        y_max=args.ymax,
                                                        x_min=args.xmin,
                                                        x_max=args.xmax,
                                                        agglomeration=args.agglomeration,
                                                        ylabel=args.ylabel)

    if args.save != "":
        print "Save plot to %s" % args.save
        plot_util.save_plot(fig, args.save, plot_util.get_defaults()['dpi'])
    else:
        fig.show()
示例#8
0
def main():
    prog = "python plot_ValidationPerformance.py <WhatIsThis> " \
           "one/or/many/*ClassicValidationResults*.csv"
    description = "Plot a median trace with quantiles for multiple experiments"

    parser = ArgumentParser(description=description,
                            prog=prog,
                            formatter_class=ArgumentDefaultsHelpFormatter)

    # General Options
    parser.add_argument("--logx",
                        action="store_true",
                        dest="logx",
                        default=False,
                        help="Plot x-axis on log scale")
    parser.add_argument("--logy",
                        action="store_true",
                        dest="logy",
                        default=False,
                        help="Plot y-axis on log scale")
    parser.add_argument("--ymax",
                        dest="ymax",
                        type=float,
                        default=None,
                        help="Maximum of the y-axis")
    parser.add_argument("--ymin",
                        dest="ymin",
                        type=float,
                        default=None,
                        help="Minimum of the y-axis")
    parser.add_argument("--xmax",
                        dest="xmax",
                        type=float,
                        default=None,
                        help="Maximum of the x-axis")
    parser.add_argument("--xmin",
                        dest="xmin",
                        type=float,
                        default=None,
                        help="Minimum of the x-axis")
    parser.add_argument("--ylabel",
                        dest="ylabel",
                        default=None,
                        help="Label on y-axis")
    parser.add_argument("--xlabel",
                        dest="xlabel",
                        default="time [sec]",
                        help="Label on x-axis")
    parser.add_argument("-s",
                        "--save",
                        dest="save",
                        default="",
                        help="Where to save plot instead of showing it?")
    parser.add_argument("-t",
                        "--title",
                        dest="title",
                        default="",
                        help="Optional supertitle for plot")
    parser.add_argument("--maxvalue",
                        dest="maxvalue",
                        type=float,
                        default=plottingscripts.utils.macros.MAXINT,
                        help="Replace all values higher than this?")
    parser.add_argument("--agglomeration",
                        dest="agglomeration",
                        type=str,
                        default="median",
                        choices=("median", "mean"),
                        help="Plot mean or median")
    parser.add_argument("-v",
                        "--verbose",
                        dest="verbose",
                        action="store_true",
                        default=False,
                        help="print number of runs on plot")
    parser.add_argument("--scaleY",
                        dest="scale_y",
                        default=1,
                        type=float,
                        help="Multiply all Y values with this factor")
    group = parser.add_mutually_exclusive_group()
    group.add_argument('--train',
                       dest="train",
                       default=False,
                       action='store_true')
    group.add_argument('--test',
                       dest="test",
                       default=True,
                       action='store_true')

    # Properties
    # We need this to show defaults for -h
    defaults = plot_util.get_defaults()
    for key in defaults:
        parser.add_argument("--%s" % key,
                            dest=key,
                            default=None,
                            help="%s, default: %s" % (key, str(defaults[key])))
    args, unknown = parser.parse_known_args()

    sys.stdout.write("\nFound " + str(len(unknown)) + " arguments\n")

    if len(unknown) < 2:
        print("To few arguments given")
        parser.print_help()
        sys.exit(1)

    if args.ylabel is None:
        if args.train:
            args.ylabel = "%s performance on train instances" % \
                          args.agglomeration
        else:
            args.ylabel = "%s performance on test instances" % \
                          args.agglomeration

    # Get files and names
    file_list, name_list = read_util.get_file_and_name_list(unknown,
                                                            match_file='.csv')
    for idx in range(len(name_list)):
        print("%20s contains %d file(s)" %
              (name_list[idx], len(file_list[idx])))

    if args.verbose:
        name_list = [
            name_list[i] + " (" + str(len(file_list[i])) + ")"
            for i in range(len(name_list))
        ]

    # Get data from csv
    performance = list()
    time_ = list()
    show_from = -plottingscripts.utils.macros.MAXINT

    for name in range(len(name_list)):
        # We have a new experiment
        performance.append(list())
        for fl in file_list[name]:
            _none, csv_data = read_util.read_csv(fl, has_header=True)
            csv_data = np.array(csv_data)
            # Replace too high values with args.maxint
            if args.train:
                data = [
                    min([args.maxvalue, float(i.strip())])
                    for i in csv_data[:, 1]
                ]
            elif args.test:
                data = [
                    min([args.maxvalue, float(i.strip())])
                    for i in csv_data[:, 2]
                ]
            else:
                print("This should not happen")
            # do we have only non maxint data?
            show_from = max(data.count(args.maxvalue), show_from)
            performance[-1].append(data)
            time_.append([float(i.strip()) for i in csv_data[:, 0]])
            # Check whether we have the same times for all runs
            if len(time_) == 2:
                if time_[0] == time_[1]:
                    time_ = [
                        time_[0],
                    ]
                else:
                    raise NotImplementedError(".csv are not using the same "
                                              "times")

    performance = [np.array(i) for i in performance]

    # print time_
    time_ = np.array(time_).flatten()

    if args.train:
        print("Plot TRAIN performance")
    elif args.test:
        print("Plot TEST performance")
    else:
        print("Don't know what I'm printing")

    if args.xmin is None and show_from != 0:
        args.xmin = show_from

    if args.scale_y != 1:
        print("Scale Y values with %g", args.scale_y)
        performance = np.array(performance)
        performance = args.scale_y * performance
    properties = helper.fill_property_dict(arguments=args, defaults=defaults)
    print(properties)
    new_time_list = [time_ for i in range(len(performance))]
    fig = plot_methods.\
        plot_optimization_trace_mult_exp(time_list=new_time_list,
                                         performance_list=performance,
                                         title=args.title,
                                         name_list=name_list,
                                         logx=args.logx, logy=args.logy,
                                         y_min=args.ymin,
                                         y_max=args.ymax,
                                         x_min=args.xmin,
                                         x_max=args.xmax,
                                         agglomeration=args.agglomeration,
                                         legend=(args.legendlocation is None),
                                         ylabel=args.ylabel, xlabel=args.xlabel,
                                         properties=properties)
    if args.save != "":
        print("Save plot to %s" % args.save)
        plot_util.save_plot(fig, args.save, plot_util.get_defaults()['dpi'])
    else:
        fig.show()
def main():
    prog = "python plot_scatter.py"
    description = "Plots performances of the best config at one time for two " \
                  "configuration runs"

    parser = ArgumentParser(description=description, prog=prog)

    # General Options
    parser.add_argument("--max",
                        dest="max",
                        type=float,
                        default=1000,
                        help="Maximum of both axes")
    parser.add_argument("--min",
                        dest="min",
                        type=float,
                        default=None,
                        help="Minimum of both axes")
    parser.add_argument("-s",
                        "--save",
                        dest="save",
                        default="",
                        help="Where to save plot instead of showing it?")
    parser.add_argument("--title",
                        dest="title",
                        default="",
                        help="Optional supertitle for plot")
    parser.add_argument("--greyFactor",
                        dest="grey_factor",
                        type=float,
                        default=1,
                        help="If an algorithms is not greyFactor-times better"
                        " than the other, show this point less salient, > 1")
    parser.add_argument("-v",
                        "--verbose",
                        dest="verbose",
                        action="store_true",
                        default=False,
                        help="Plot some debug info")
    parser.add_argument(
        "-f",
        "--lineFactors",
        dest="linefactors",
        default=None,
        help="Plot X speedup/slowdown, format 'X,..,X' (no spaces)")
    parser.add_argument("--time",
                        dest="time",
                        default=None,
                        type=float,
                        help="Plot config at which time?")
    parser.add_argument(
        "--obj1",
        dest="obj1",
        default=None,
        required=True,
        help="Path to validationObjectiveMatrix-traj-run-* file")
    parser.add_argument("--res1",
                        dest="res1",
                        required=True,
                        help="Path to validationResults-traj-run-* file")
    parser.add_argument(
        "--obj2",
        dest="obj2",
        default=None,
        required=True,
        help="Path to validationObjectiveMatrix-traj-run-* file")
    parser.add_argument("--res2",
                        dest="res2",
                        required=True,
                        help="Path to validationResults-traj-run-* file")
    parser.add_argument(
        "--minvalue",
        dest="minvalue",
        type=float,
        default=None,
        help="Replace all values smaller than this",
    )
    parser.add_argument(
        "--fontsize",
        dest="fontsize",
        type=int,
        default=20,
        help="Use this fontsize for plotting",
    )

    args, unknown = parser.parse_known_args()

    if len(unknown) != 0:
        print "Wrong number of arguments"
        parser.print_help()
        sys.exit(1)

    if args.grey_factor < 1:
        print "A grey-factor lower than one makes no sense"
        parser.print_help()
        sys.exit(1)

    # Load validationResults
    res1_header, res1_data = read_util.read_csv(args.res1, has_header=True)
    res2_header, res2_data = read_util.read_csv(args.res2, has_header=True)

    av_times = [float(row[0]) for row in res1_data]
    if args.time is None:
        # Print available times and quit
        print "Choose a time from"
        print "\n".join(["* %s" % i for i in av_times])
        sys.exit(0)

    # Now extract data
    config_1 = [
        int(float(row[len(res1_header) - 2].strip('"'))) for row in res1_data
        if int(float(row[0])) == int(args.time)
    ]
    config_2 = [
        int(float(row[len(res2_header) - 2].strip('"'))) for row in res2_data
        if int(float(row[0])) == int(args.time)
    ]
    if len(config_1) == 0 or len(config_2) == 0:
        print "Time int(%s) not found. Choose a time from:" % (args.time)
        print "\n".join(["* %s" % i for i in av_times])
        sys.exit(1)
    config_1 = config_1[0]
    config_2 = config_2[0]

    obj1_header, obj1_data = read_util.read_csv(args.obj1, has_header=True)
    obj2_header, obj2_data = read_util.read_csv(args.obj2, has_header=True)

    head_template = '"Objective of validation config #%s"'
    idx_1 = obj1_header.index(head_template % config_1)
    idx_2 = obj2_header.index(head_template % config_2)

    data_one = np.array([float(row[idx_1].strip('"')) for row in obj1_data])
    data_two = np.array([float(row[idx_2].strip('"')) for row in obj2_data])

    print "Found %s points for config %d and %s points for config %d" % \
          (str(data_one.shape), config_1, str(data_two.shape), config_2)

    linefactors = list()
    if args.linefactors is not None:
        linefactors = [float(i) for i in args.linefactors.split(",")]
        if len(linefactors) < 1:
            print "Something is wrong with linefactors: %s" % args.linefactors
            sys.exit(1)
        if min(linefactors) < 1:
            print "A line-factor lower than one makes no sense"
            sys.exit(1)
    if args.grey_factor > 1 and args.grey_factor not in linefactors:
        linefactors.append(args.grey_factor)

    label_template = '%s %20s at %s sec'
    l1 = label_template % ("obj1", os.path.basename(
        args.obj1)[:20], str(args.time))
    l2 = label_template % ("obj2", os.path.basename(
        args.obj2)[:20], str(args.time))

    if args.minvalue is not None:
        print "Replace all values lower than %f" % args.minvalue
        data_one = np.array([max(args.minvalue, i) for i in data_one])
        data_two = np.array([max(args.minvalue, i) for i in data_two])

    fig = scatter.plot_scatter_plot(x_data=data_one,
                                    y_data=data_two,
                                    labels=[l1, l2],
                                    title=args.title,
                                    max_val=args.max,
                                    min_val=args.min,
                                    grey_factor=args.grey_factor,
                                    linefactors=linefactors,
                                    user_fontsize=args.fontsize,
                                    debug=args.verbose)

    if args.save != "":
        print "Save plot to %s" % args.save
        plot_util.save_plot(fig=fig,
                            save=args.save,
                            dpi=plot_util.get_defaults()['dpi'])
    else:
        plt.show()