def generate_mirroring_port_utilization_compact_bar_plot(results_repository): width = 0.25 ind = np.arange(11) fig, ax = plt.subplots() solution_labels = SOLUTION_LABELS colors = cfg.BAR_PLOT_COLORS hatch = cfg.BAR_PLOT_TEXTURES bar_locations = [w for w in np.arange((width/2), len(solution_labels)*width, width)] # mean_utils :: solution_type -> switch_id -> util_list trial_name = "sub-trial-4" mean_utils = defaultdict(lambda: defaultdict(list)) for run_name in ["run-%d" % run_idx for run_idx in range(3)]: for solution_name in solution_labels: topo, flows, switches, solutions, link_utilization_data = read_results( results_repository, run_name, solution_name, trial_name) link_ids = [(s, d) for s, t in link_utilization_data[0].items() for d in t.keys()] collector_switch_dpid = topo_mapper.get_collector_switch_dpid() id_to_dpid = topo_mapper.get_and_validate_onos_topo(topo) dpid_to_id = {v: k for k, v in id_to_dpid.items()} for s, d in [(s, d) for s, d in link_ids if d == collector_switch_dpid]: link_utils_over_time = [] for time_idx, net_snapshot in enumerate(link_utilization_data): try: link_utils_over_time.append(net_snapshot[s][d]) except KeyError: print("net_snapshot at time %d did not contain link %s -> %s" % (time_idx, s, d)) source_switch_id = dpid_to_id[s] mean_utils[solution_name][source_switch_id].append( util.bytes_per_second_to_mbps(mean(link_utils_over_time[1:]))) for bar_idx, solution_name_to_switch_id in enumerate(mean_utils.items()): solution_name, switch_id_to_util_list = solution_name_to_switch_id data_tuples = sorted([(switch_id, mean(util_list)) for switch_id, util_list in switch_id_to_util_list.items()], key=lambda kvp: kvp[0]) std_dev_tuples = sorted([(switch_id, np.std(util_list)) for switch_id, util_list in switch_id_to_util_list.items()], key=lambda kvp: kvp[0]) ys = [d_i[1] for d_i in data_tuples] y_err = [s_i[1] for s_i in std_dev_tuples] ax.bar(ind+bar_locations[bar_idx], ys, width, color=colors[bar_idx], hatch=hatch[bar_idx], label=solution_labels[solution_name], align="center", ecolor="black", yerr=y_err) plt.rc('text', usetex=True) plt.rc('font', **cfg.FONT) plt.xlabel("Switch ID", **cfg.AXIS_LABELS) plt.ylabel("Switch load (Mbps)", **cfg.AXIS_LABELS) plt.xticks(ind+(width*len(solution_labels))/2, (ind+1)) plt.grid() plt.xlim(0, max(ind) + (width*len(solution_labels))) # plt.legend(ncol=len(solution_labels), **cfg.LEGEND) helpers.save_figure("sfm-plotthree.pdf", len(solution_labels))
def dropout_vs_no_dropout_plot(): with_dropout = log_file_dir / "with-dropout.log" no_dropout = log_file_dir / "no-dropout.log" more_dropout = log_file_dir / "dropout-0.75.log" _, dropout = parse_log(str(with_dropout)) _, no_dropout = parse_log(str(no_dropout)) _, more_dropout = parse_log(str(more_dropout)) xs = [d_i["NumIters"] for d_i in dropout] dropout_ys = [100 * (1 - d_i["accuracy"]) for d_i in dropout] no_dropout_ys = [100 * (1 - d_i["accuracy"]) for d_i in no_dropout] more_dropout_ys = [100 * (1 - d_i["accuracy"]) for d_i in more_dropout] helpers.plot_a_line(xs, no_dropout_ys, label=r"$\mathbb{P}\{\text{dropout}\} = 0.0$", idx=3, plot_markers=False) helpers.plot_a_line(xs, dropout_ys, label=r"$\mathbb{P}\{\text{dropout}\} = 0.5$", idx=2, plot_markers=False) helpers.plot_a_line(xs, more_dropout_ys, label=r"$\mathbb{P}\{\text{dropout}\} = 0.75$", idx=4, plot_markers=False) helpers.ylim((5, 30)) helpers.xlabel("Training Iteration") helpers.ylabel(r"Validation Error (\%)") helpers.save_figure(str(figure_output_dir / "dropout-comparison.pdf"), num_cols=2)
def generate_mean_link_utilization_over_time_plot(parameter_name, parameter_value, trials): """ Generate a graph that shows the mean utilization across all the links over time for each trial in the trial provider """ path_capacity = 50.0 for trial_idx, the_trial in enumerate(trials): print(f"generate_mean_utilization_over_time_plot: {trial_idx}, {the_trial.name}") link_utilization_over_time = the_trial.get_parameter("link-utilization-over-time") data_for_links = {link_tuple: util_list for link_tuple, util_list in link_tuple_to_util_list(link_utilization_over_time).items() if link_tuple[0] == "of:0000000000000001"} ys = {link_tuple: [min(path_capacity, util_val) / path_capacity for util_val in util_val_list] for link_tuple, util_val_list in data_for_links.items()} # The next line assumes that the same number of network snapshots were captured # for each of the links, I think this will always happen but this will throw # if that is not the case. throughputs_over_time = [np.mean([util_list[time_idx] for util_list in ys.values()]) for time_idx in range(len(next(iter(data_for_links.values()))))] xs = [idx for idx in range(len(next(iter(data_for_links.values()))))] helpers.plot_a_scatter(xs, throughputs_over_time, idx=trial_idx, label=the_trial.name) helpers.xlabel(helpers.axis_label_font("Time")) helpers.ylabel(helpers.axis_label_font("Mean link utilization")) helpers.save_figure(f"mean-utilization-over-time-{parameter_name}-{parameter_value}.pdf", num_cols=3)
def generate_measured_link_utilization_cdf(trial_provider): trial = trial_provider.get_first_trial_that_matches( lambda t_i: t_i.name == "greedy-path-hopping") link_utilization = trial.get_parameter("measured-link-utilization") link_utilization_data = sorted(list(link_utilization.values())) helpers.plot_a_cdf(link_utilization_data) helpers.save_figure("test-cdf.pdf")
def augment_vs_no_augment(): with_augment = log_file_dir / "training-with-flip-dataset.log" no_augment = log_file_dir / "with-dropout.log" augment_train, augment = parse_log(str(with_augment)) no_augment_train, no_augment = parse_log(str(no_augment)) xs = [d_i["NumIters"] for d_i in augment] augment_ys = [100 * (1 - d_i["accuracy"]) for d_i in augment] no_augment_ys = [100 * (1 - d_i["accuracy"]) for d_i in no_augment] helpers.plot_a_line(xs, augment_ys, label="Dataset augmentations", idx=4, plot_markers=False) helpers.plot_a_line(xs, no_augment_ys, label="Original dataset", idx=5, plot_markers=False) plt.ylim(7.5, 20.0) # augment_ys = [1 - d_i["loss"] for d_i in augment_train] # no_augment_ys = [1 - d_i["loss"] for d_i in no_augment_train] # xs = [d_i["NumIters"] for d_i in augment_train] # helpers.plot_a_line(xs, augment_ys, label="Dataset augmentations", idx=6, plot_markers=False) # helpers.plot_a_line(xs, no_augment_ys, label="Original dataset", idx=7, plot_markers=False) helpers.xlabel("Training Iteration") helpers.ylabel(r"Validation Error (\%)") helpers.save_figure(str(figure_output_dir / "dataset-augmentation-comparison.pdf"), num_cols=2)
def generate_mean_throughput_over_time_plot(parameter_name, parameter_value, trials): """ Generate a graph that shows the mean throughput across all the links over time for each trial in trial provider. """ path_capacity = 50.0 for trial_idx, the_trial in enumerate(trials): print(f"generate_mean_throughput_over_time: {trial_idx}, {the_trial.name}") # number_of_paths = the_trial.get_parameter("number-of-paths") link_utilization_over_time = the_trial.get_parameter("link-utilization-over-time") data_for_links = {link_tuple: util_list for link_tuple, util_list in link_tuple_to_util_list(link_utilization_over_time).items() if link_tuple[0] == "of:0000000000000001"} ys = {link_tuple: [min(path_capacity, util_val) for util_val in util_val_list] for link_tuple, util_val_list in data_for_links.items()} throughputs_over_time = [] for time_idx in range(len(next(iter(data_for_links.values())))): total_throughput = sum(util_list[time_idx] for util_list in ys.values()) throughputs_over_time.append(total_throughput) xs = [idx for idx in range(len(next(iter(data_for_links.values()))))] helpers.plot_a_scatter(xs, throughputs_over_time, idx=trial_idx, label=the_trial.name) helpers.xlabel(helpers.axis_label_font("Time")) helpers.ylabel(helpers.axis_label_font("Mean throughput (Mi-bps)")) helpers.save_figure(f"throughput-over-time-{parameter_name}-{parameter_value}.pdf", num_cols=3)
def generate_learning_rate_plots(): # - step: return base_lr * gamma ^ (floor(iter / step)) # - exp: return base_lr * gamma ^ iter # - inv: return base_lr * (1 + gamma * iter) ^ (- power) base_lr = 0.01 gamma_step = 0.9999 gamma_inv = 0.0001 step = 1 power = 0.75 xs = [x for x in range(1, 50001, 10)] inv_learning_rate = [ base_lr * (1 + gamma_inv * iteration)**(-power) for iteration in xs ] step_learning_rate = [ base_lr * (gamma_step**(floor(iteration / step))) for iteration in xs ] helpers.plot_a_line(xs, step_learning_rate, label="Step", idx=6, plot_markers=False) helpers.plot_a_line(xs, inv_learning_rate, label="Inverse", idx=7, plot_markers=False) helpers.xlabel("Training Iteration") helpers.ylabel("Learning Rate") helpers.save_figure(figure_output_dir / "learning-rate.pdf", num_cols=2)
def generate_link_utilization_cdf(results_repository, provider_name): """ Generate a CDF of mean link utilization """ trial_provider = results_repository.read_trial_provider(provider_name) for idx, trial in enumerate(trial_provider): # print(trial) utilization_results = trial.get_parameter("utilization-results") links = get_link_set(utilization_results) print("Number of links based on utilization results: %d" % len(links)) byte_counts_per_time_period = compute_byte_counts_per_time_period(utilization_results) pp.pprint([len(b_i) * len(next(iter(b_i.values()))) for b_i in byte_counts_per_time_period]) network_utilization_per_time_period = compute_network_utilization_per_time_period( byte_counts_per_time_period) pp.pprint([len(m_i) * len(next(iter(m_i.values()))) for m_i in network_utilization_per_time_period]) mean_network_utilization = compute_mean_network_utilization( network_utilization_per_time_period) print(len(mean_network_utilization)) link_utilizations = sorted(mean_network_utilization.values()) plot_a_cdf(link_utilizations) plt.xlabel("Link throughput (Mbps)") plt.ylabel("P{x < X}") plt.legend(**cfg.LEGEND) helpers.save_figure("%s-cdf.pdf" % provider_name, no_legend=True)
def generate_link_utilization_box_plot(results_repository, provider_name): """ Generate a box and whisker plot showing the utilization of all the links in the network for a set of trials. """ trial_provider = results_repository.read_trial_provider(provider_name) link_utilization_results_for_trials = [] x_axis_labels = [] for idx, trial in enumerate(trial_provider): utilization_results = trial.get_parameter("utilization-results") byte_counts_per_time_period = compute_byte_counts_per_time_period(utilization_results) network_utilization_per_time_period = compute_network_utilization_per_time_period( byte_counts_per_time_period) mean_network_utilization = compute_mean_network_utilization( network_utilization_per_time_period) link_utilization_results_for_trials.append(list(mean_network_utilization.values())) x_axis_labels.append("K=%d" % trial.get_parameter("K")) bp = plt.boxplot(link_utilization_results_for_trials, labels=x_axis_labels, whiskerprops={"linestyle": "--"}, flierprops={"marker":"x", "markerfacecolor": "red", "markeredgecolor": "red"}) for element in ["boxes"]: plt.setp(bp[element], color="blue") plt.setp(bp["medians"], color="red") plt.ylabel("Link Throughput (Mbps)") helpers.save_figure("%s-box-plot.pdf" % provider_name, 3, no_legend=True)
def generate_loss_rate_cdf(results_repository): trial_name = "test-trial-830656277" seed_numbers = [trial_name] for idx, trial_type in enumerate(TRIAL_TYPES): loss_rates_for_seed_number = [] for seed_number in seed_numbers: loss_rate_per_flow = generate_loss_rates(results_repository, trial_type, seed_number) xs = [0.0] ys = [0.0] for ctr, loss_rate in enumerate(sorted(loss_rate_per_flow)): xs.append(loss_rate) ys.append((ctr + 1) / len(loss_rate_per_flow)) plt.plot(xs, ys, linestyle="-", marker=helpers.marker_style(idx), label=LABEL_NAMES[idx], color=helpers.line_color(idx)) plt.rc("font", **cfg.FONT) plt.rc("legend", edgecolor="black") # xtick_locations = [200, 400, 600, 800, 1000] plt.xlim(0.0) plt.ylim(0.0, 1.0) xtick_locations, xtick_labels = plt.xticks() ytick_locations, ytick_labels = plt.yticks() plt.xticks(xtick_locations[1:], [helpers.tick_font(x_i, "%.2f") for x_i in xtick_locations][1:]) plt.yticks(ytick_locations[1:], [helpers.tick_font(y_i, "%.1f") for y_i in ytick_locations][1:]) plt.ylabel("CDF", **cfg.AXIS_LABELS) helpers.save_figure("loss-rate-cdf.pdf", 1)
def generate_heterogeneous_links_instantaneous_rate_plot(): link_rate_count = 100 labels = { (100, 0): "\\LARGE{$\\mu=100$, $\\text{CoV}=0.0$}", (100, 50): "\\LARGE{$\\mu=100$, $\\text{CoV}=0.5$}", (100, 100): "\\LARGE{$\\mu=100$, $\\text{CoV}=1.0$}" } colors = {(100, 0): "red", (100, 50): "lime", (100, 100): "blue"} plt.plot(range(link_rate_count), [100 for _ in range(link_rate_count)], label=labels[100, 0], color=colors[100, 0]) for mu, sigma in [(100, 50), (100, 100)]: link_rates = heterogeneous_links.get_heterogeneous_link_rates( mu, sigma**2, link_rate_count) xs = range(len(link_rates)) ys = link_rates plt.plot(xs, ys, label=labels[mu, sigma], color=colors[mu, sigma]) plt.rc('font', **cfg.FONT) plt.xlim(0, link_rate_count - 1) plt.xlabel("Time", **cfg.AXIS_LABELS) plt.ylabel("Transmission Rate (Mbps)", **cfg.AXIS_LABELS) xtick_locations, xtick_labels = plt.xticks() ytick_locations, ytick_lables = plt.yticks() plt.ylim(0, ytick_locations[-1]) plt.xticks(xtick_locations[1:], ["" for x_i in xtick_locations]) plt.yticks(ytick_locations[1:], [helpers.tick_font(y_i, "%.0f") for y_i in ytick_locations][1:]) helpers.save_figure("actual-model-tx-rates.pdf", 1, no_legend=False)
def generate_max_mirror_port_utilization_bar_plot(results_repository): # utilization_data, _ = compute_theoretical_and_actual_mean_utilization(results_repository) # actual_error = compute_theoretical_and_actual_error(results_repository) run_count = 3 solution_names = ["approx", "optimal"] trial_count = 5 utilization_data, _ = compute_theoretical_and_actual_utilization_by_run( results_repository, run_count, solution_names, trial_count) # mean_util_lists :: solution_name -> flow_count -> [util_vals] mean_util_lists = defaultdict(lambda: defaultdict(list)) for run_name, solution_name_to_flow_count in utilization_data.items(): for solution_name, flow_count_to_util_list in solution_name_to_flow_count.items(): for flow_count, util_list in flow_count_to_util_list.items(): mean_util_value = util.bytes_per_second_to_mbps(mean(util_list[1:])) mean_util_lists[solution_name][flow_count].append(mean_util_value) # std_deviations :: solution_name -> flow_count -> std_dev std_deviations = defaultdict(dict) # mean_utils :: solution_name -> flow_count -> mean_util mean_utils = defaultdict(dict) for solution_name, flow_count_to_util_list in mean_util_lists.items(): for flow_count, util_list in flow_count_to_util_list.items(): std_deviations[solution_name][flow_count] = np.std(util_list) mean_utils[solution_name][flow_count] = mean(util_list) pp.pprint(mean_utils) width = 0.35 ind = np.arange(1, 6) fig, ax = plt.subplots() labels = SOLUTION_LABELS half = len(labels) // 2 bar_locations = [w for w in np.arange((width/2), len(labels)*width, width)] colors = cfg.BAR_PLOT_COLORS hatch = cfg.BAR_PLOT_TEXTURES for bar_idx, solution_name_to_flow_count in enumerate(mean_utils.items()): solution_name, flow_count_to_mean_util = solution_name_to_flow_count data_tuples = sorted([(flow_count, util_val) for flow_count, util_val in flow_count_to_mean_util.items()], key=lambda kvp: kvp[0]) yerr_tuples = sorted([(flow_count, std_dev) for flow_count, std_dev in std_deviations[solution_name].items()], key=lambda kvp: kvp[0]) xs = [d_i[0] for d_i in data_tuples] ys = [d_i[1] for d_i in data_tuples] yerr_values = [s_i[1] for s_i in yerr_tuples] ax.bar(ind+bar_locations[bar_idx], ys, width, color=colors[bar_idx], hatch=hatch[bar_idx], label=labels[solution_name], yerr=yerr_values, align="center", ecolor="black") plt.rc('text', usetex=True) plt.rc('font', **cfg.FONT) plt.grid(**cfg.GRID) plt.xticks(ind+(width*len(labels))/2, ind*10) plt.xlabel("Number of Flows", **cfg.AXIS_LABELS) plt.ylabel("Maximum switch load (Mbps)", **cfg.AXIS_LABELS) # plt.legend(ncol=2, **cfg.LEGEND) helpers.save_figure("sfm-objective.pdf", 2)
def generate_data_recovery_vs_time_cdf(trial_provider): for trial in trial_provider: if trial.get_parameter("path-length") == 10: random_path_attacker = trial.get_parameter( "random-path-hopping-attacker-recovered-messages") random_node_attacker = trial.get_parameter( "random-node-hopping-attacker-recovered-messages") ideal_path_attacker = trial.get_parameter( "ideal-random-path-hopping-attacker-recovered-messages") one_node_per_path_attacker_captured = trial.get_parameter( "one-node-per-path-attacker") fixed_attacker_captured = trial.get_parameter("fixed-attacker") helpers.plot_a_cdf(sorted(random_path_attacker), idx=0, label="Random Path Attacker", plot_markers=False) helpers.plot_a_cdf(sorted(random_node_attacker), idx=1, label="Random Node Attacker", plot_markers=False) helpers.plot_a_cdf(sorted(one_node_per_path_attacker_captured), idx=2, label="One Node per Path Attacker", plot_markers=False) helpers.plot_a_cdf(sorted(fixed_attacker_captured), idx=3, label="Fixed Attacker", plot_markers=False) helpers.save_figure("data-recovery-cdf.pdf", num_cols=2)
def generate_theoretical_vs_actual_utilization_bar_plot(results_repository): utilization_data, theoretical_data = compute_theoretical_and_actual_mean_utilization(results_repository) actual_error = compute_theoretical_and_actual_error(results_repository) std_deviations = defaultdict(list) for provider_name, data_list in actual_error.items(): for flow_count, data in data_list: std_deviations[provider_name].append(np.std( [util.bytes_per_second_to_mbps(d_i) for d_i in data])) width = 0.35 ind = np.arange(1, 6) fig, ax = plt.subplots() xs = [t[0] for t in utilization_data["approx"]] ys = [util.bytes_per_second_to_mbps(t[1]) for t in utilization_data["approx"]] ax.bar(ind-(width/2), ys, width, color="skyblue", hatch=".", tick_label=xs, label="Measured", yerr=std_deviations["approx"], ecolor="black") xs = [t[0] for t in theoretical_data["approx"]] ys = [t[1] * 100 for t in theoretical_data["approx"]] ax.bar(ind+(width/2), ys, width, color="green", hatch="\\", tick_label=xs, label="Expected") plt.rc('text', usetex=True) plt.rc('font', **cfg.FONT) plt.xlabel("Number of Flows") plt.ylabel("Maximum mirroring port rate ($\\frac{Mb}{s}$)") plt.legend(loc="upper center", bbox_to_anchor=(0.5, cfg.LEGEND_HEIGHT), shadow=True, ncol=2) helpers.save_figure("sfm-theorypractice.pdf")
def generate_per_class_accuracy_bar_plot(): conf_mat_1 = more_dropout conf_mat_2 = undersampled fig, (ax1, ax2) = plt.subplots(2) bar_width = 0.1 bar_1_xs = np.arange(0.0, 0.3 * 6.5, 0.3) bar_2_xs = [x_i + bar_width for x_i in bar_1_xs] print(bar_1_xs, bar_2_xs) bar_1_ys = [ 100 * (conf_mat_1[idx][idx] / sum(get_samples_that_are(conf_mat_1, idx + 1))) for idx in range(7) ] bar_2_ys = [ 100 * (conf_mat_2[idx][idx] / sum(get_samples_that_are(conf_mat_2, idx + 1))) for idx in range(7) ] plt.xticks([x_i + 0.5 * bar_width for x_i in bar_1_xs], [1, 2, 3, 4, 5, 6, "Outlier"]) helpers.plot_a_bar(bar_1_xs, bar_1_ys, idx=0, label="Augmented Dataset", bar_width=bar_width, axis_to_plot_on=ax2) helpers.plot_a_bar(bar_2_xs, bar_2_ys, idx=1, label="Undersampled Dataset", bar_width=bar_width, axis_to_plot_on=ax2) helpers.xlabel("Class label") helpers.ylabel(r"Validation Accuracy (\%)", formatter=lambda x: x, ax=ax2) undersampled_dataset = json.loads( path.Path("./segments-undersampled.json").read_text()) histogram = Counter([ d_i["data"]["segment_type"]["data"] for d_i in undersampled_dataset.values() ]) xs = [x_i for x_i in range(1, 8)] total_samples = sum(histogram.values()) ys = [c[1] / total_samples for c in sorted(histogram.items())] helpers.plot_a_bar(xs, ys, idx=1, axis_to_plot_on=ax1, label_data=False) helpers.ylabel(r"$\mathbb{P}\{x = \mathcal{X}\}$", formatter=lambda x: x, ax=ax1) ax1.set_yticks([0.1, 0.2, 0.3]) ax1.grid(**cfg.GRID) ax1.xaxis.set_ticklabels([]) legend_params = deepcopy(cfg.LEGEND) legend_params["bbox_to_anchor"] = (0.5, 0.975) fig.legend(**legend_params, ncol=2) helpers.save_figure(figure_output_dir / "per-class-error-bar-plot.pdf", no_legend=True)
def generate_simulation_run_time_plot(results_repository): def mean_confidence_interval(data, confidence=0.95): a = 1.0 * np.array(data) n = len(a) m, se = np.mean(a), scipy.stats.sem(a) h = se * scipy.stats.t.ppf((1 + confidence) / 2., n - 1) # return m, m-h, m+h return h series_data = {} for flow_count in [100, 500, 1000]: data_for_node_counts = [] for node_count in [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]: data_for_single_count = [] seed_numbers = [ 742349042, 2787879137, 3102739139, 2303690384, 1672982999, 2501915964, 2853959085, 3163893110, 462786850, 4124106680 ] for seed_number in seed_numbers: sim = read_results(results_repository, flow_count, node_count, seed_number) data_for_single_count.append(sim.solution_time) pp.pprint(data_for_single_count) ci = mean_confidence_interval(data_for_single_count) data_for_node_counts.append( (node_count, np.mean(data_for_single_count), ci)) series_data[flow_count] = data_for_node_counts for idx, (flow_count, data_for_node_counts) in enumerate( sorted(series_data.items(), key=lambda t: t[0])): xs = [d_i[0] for d_i in data_for_node_counts] ys = [d_i[1] for d_i in data_for_node_counts] std_dev = [d_i[2] for d_i in data_for_node_counts] plt.errorbar(xs, ys, label="\\LARGE{%d VLs}" % flow_count, marker=cfg.MARKER_STYLE[idx], linestyle=helpers.line_style(idx), color=helpers.line_color(idx), yerr=std_dev, capsize=2) plt.rc("text", usetex=True) matplotlib.rcParams['text.latex.preamble'] = [ r'\usepackage{amsmath}', r'\usepackage{amssymb}' ] plt.rc("font", **cfg.FONT) plt.rc("legend", edgecolor="black") xtick_locations = [200, 400, 600, 800, 1000] ytick_locations = [2, 4, 6, 8, 10, 12, 14] plt.xticks(xtick_locations, [helpers.tick_font(x_i) for x_i in xtick_locations]) plt.yticks(ytick_locations, [helpers.tick_font(y_i) for y_i in ytick_locations]) plt.xlabel("Number of Nodes", **cfg.AXIS_LABELS) plt.ylabel("Running Time (s)", **cfg.AXIS_LABELS) helpers.save_figure("sim-running-time.pdf", 1)
def generate_substrate_topology_graph(): # trial_provider = results_repository.read_trial_provider("multiflow-tests") # topo = trial_provider.get_metadata("substrate-topology") topo = nx.complete_graph(10) the_layout = nx.circular_layout(topo) # nx.draw(topo, node_color="skyblue", layout=the_layout) nx.draw_circular(topo, node_color="skyblue") helpers.save_figure("network-topology.pdf", no_legend=True)
def generate_learning_rate_comparison_plot(): _, validation_data_step = parse_log( str(log_file_dir / "lr-step-training.log")) _, validation_data_inv = parse_log( str(log_file_dir / "training-with-flip-dataset.log")) fig, (plot1, plot2) = plt.subplots(2) xs = [d_i["NumIters"] for d_i in validation_data_step] ys_step = [100 * (1 - d_i["accuracy"]) for d_i in validation_data_step] ys_inv = [100 * (1 - d_i["accuracy"]) for d_i in validation_data_inv] plot2.set_ylim(7.5, 20) helpers.xlabel("Training Iterations", ax=plot2) helpers.ylabel(r"Validation Error (\%)", ax=plot2, formatter=lambda x: x) helpers.plot_a_line(xs, ys_inv, label="Step", plot_markers=False, idx=6, axis_to_plot_on=plot2) helpers.plot_a_line(xs, ys_step, label="Inverse", plot_markers=False, idx=7, axis_to_plot_on=plot2) base_lr = 0.01 gamma_step = 0.9999 gamma_inv = 0.0001 step = 1 power = 0.75 xs = [x for x in range(1, 50001, 10)] inv_learning_rate = [ base_lr * (1 + gamma_inv * iteration)**(-power) for iteration in xs ] step_learning_rate = [ base_lr * (gamma_step**(floor(iteration / step))) for iteration in xs ] helpers.plot_a_line(xs, step_learning_rate, idx=6, plot_markers=False, axis_to_plot_on=plot1) helpers.plot_a_line(xs, inv_learning_rate, idx=7, plot_markers=False, axis_to_plot_on=plot1) plot1.xaxis.set_ticklabels([]) plot1.grid(**cfg.GRID) helpers.ylabel("Learning Rate", ax=plot1, formatter=lambda x: x) legend_params = deepcopy(cfg.LEGEND) legend_params["bbox_to_anchor"] = (0.5, 0.975) fig.legend(ncol=2, **legend_params) helpers.save_figure(figure_output_dir / "learning-rate-comparison.pdf", no_legend=True)
def generate_expected_link_utilization_cdf(results_repository, provider_name): trial_provider = results_repository.read_trial_provider(provider_name) the_trial = trial_provider.get_first_trial_that_matches( lambda t: t.get_parameter("trial-name") == "optimal-3") expected_utilization_results = the_trial.get_parameter("link-utilization") link_utilizations = sorted(expected_utilization_results.values()) plot_a_cdf(link_utilizations) helpers.save_figure("expected-link-utilization-cdf.pdf", no_legend=True)
def generate_port_mirroring_port_utilization_cdf(results_repository): labels = ["rnd", "det", "df", "greedy", "optimal"] legend_labels = ["$\\epsilon$-LPR", "LPR", "DuFi", "Greedy", "Optimal"] markers = ["^", "*", "^", "o", "x"] colors = ["red", "green", "blue", "orange", "purple"] trial_name = "sub-trial-4" run_name = "run-0" for solution_idx, solution_name in enumerate(labels): topo, flows, switches, solutions, link_utilization_data, ports = read_results( results_repository, run_name, solution_name, trial_name) link_ids = [(s, d) for s, t in link_utilization_data[0].items() for d in t.keys()] mean_utils = [] labels = [] errors = [] collector_switch_dpid = topo_mapper.get_collector_switch_dpid() id_to_dpid = topo_mapper.get_and_validate_onos_topo(topo) dpid_to_id = {v: k for k, v in id_to_dpid.items()} for s, d in [(s, d) for s, d in link_ids if d == collector_switch_dpid]: link_utils_over_time = [] for time_idx, net_snapshot in enumerate(link_utilization_data): try: link_utils_over_time.append(net_snapshot[s][d]) except KeyError: print( "net_snapshot at time %d did not contain link %s -> %s" % (time_idx, s, d)) mean_utils.append( util.bytes_per_second_to_mbps(mean(link_utils_over_time))) errors.append( util.bytes_per_second_to_mbps(np.std(link_utils_over_time))) labels.append(dpid_to_id[s]) cdf_data = sorted(mean_utils) xs = [0.0] ys = [0.0] for idx, d in enumerate(cdf_data): xs.append(d) ys.append((1 + idx) / len(cdf_data)) plt.plot(xs, ys, label=legend_labels[solution_idx], marker=markers[solution_idx], color=colors[solution_idx]) plt.legend(loc="upper center", bbox_to_anchor=(0.5, cfg.LEGEND_HEIGHT), shadow=True, ncol=len(labels)) plt.rc('text', usetex=True) plt.rc('font', **cfg.FONT) plt.grid() plt.xlabel("Mirroring Port Rate $\\frac{Mb}{s}$") plt.ylabel("$\\mathbb{P}\\{x < \\mathcal{X}\\}$") helpers.save_figure("pm-plot-three-cdf.pdf")
def generate_node_probability_histogram(trial_provider): discrete_probability_distribution = trial_provider.get_metadata("node-probability-distribution") bar_x_locations = np.arange(len(discrete_probability_distribution)) plt.bar(bar_x_locations, discrete_probability_distribution, hatch="/") plt.xticks(bar_x_locations, ["" for _ in bar_x_locations]) plt.xlabel("Node ID") plt.ylabel(r"$\mathbb{P}\{n \in \{s, t\}\}$") node_selection_type = trial_provider.get_metadata("node-selection-type") helpers.save_figure("node-probability-distribution-%s.pdf" % node_selection_type, no_legend=True)
def generate_dataset_histogram(dataset, output_path): histogram = Counter( [d_i["data"]["segment_type"]["data"] for d_i in dataset.values()]) xs = [1, 2, 3, 4, 5, 6, 7] ys = [c[1] / sum(histogram.values()) for c in sorted(histogram.items())] plt.xticks(xs, [1, 2, 3, 4, 5, 6, "Outlier"]) helpers.ylabel(r"$\mathbb{P}\{x = \mathcal{X}\}$") helpers.xlabel("Class Label") helpers.plot_a_bar(xs, ys, idx=1) helpers.save_figure(output_path, no_legend=True)
def generate_traffic_on_path_bar_plot(data_file): def read_literal_from_file(data_file): with data_file.open("r") as fd: python_literal = eval(fd.read()) return python_literal path_data = read_literal_from_file(data_file) xs = [] ys = [] target_path_ratios = [0.5, 0.3, 0.2] cs = [] bar_width = 0.2 pos = 0.1 for path_idx, (k, v) in enumerate( sorted(path_data.items(), key=lambda t: t[1], reverse=True)): y_val = v / sum(path_data.values()) cs.append("blue") cs.append("green") plt.bar(pos - 0.01, y_val, bar_width, color="gray", edgecolor="black") pos += bar_width plt.bar(pos + 0.01, target_path_ratios[path_idx], bar_width, color="blue", edgecolor="black") pos += bar_width pos += 0.1 print("VALUE: %f" % (abs(y_val - target_path_ratios[path_idx]) / target_path_ratios[path_idx])) # labels=["Path %d" % path_idx for path_idx in range(1, 4)] # plt.bar(xs, ys, edgecolor="black", color=cs) legend_labels = [ "\\Large{expected data volume}", "\\Large{actual data volume}" ] legend_colors = ["blue", "gray"] patches = [ matplotlib.patches.Patch(color=c, label=l, edgecolor="black") for c, l in zip(legend_colors, legend_labels) ] plt.legend(handles=patches, ncol=2, **cfg.LEGEND) xtick_labels = ["Path %d" % path_id for path_id in range(1, 4)] xtick_locations = [l_i for l_i in np.arange(0.2, 1.5, 0.5)] plt.xticks(xtick_locations, xtick_labels) ytick_locations, ytick_labels = plt.yticks() plt.yticks(ytick_locations, [helpers.tick_font(y_i, "%.1f") for y_i in ytick_locations]) plt.ylabel("Normalized Data Volume", **cfg.AXIS_LABELS) helpers.save_figure("per-path-data-volume.pdf", no_legend=True)
def generate_mirroring_port_utilization_bar_plot(results_repository): topo, flows, switches, solutions, link_utilization_data = read_results(results_repository, "approx", "sub-trial-4") link_ids = [(s, d) for s, t in link_utilization_data[0].items() for d in t.keys()] mean_utils = [] labels = [] errors = [] collector_switch_dpid = topo_mapper.get_collector_switch_dpid() id_to_dpid = topo_mapper.get_and_validate_onos_topo(topo) dpid_to_id = {v: k for k, v in id_to_dpid.items()} for s, d in [(s, d) for s, d in link_ids if d == collector_switch_dpid]: link_utils_over_time = [] for time_idx, net_snapshot in enumerate(link_utilization_data): try: link_utils_over_time.append(net_snapshot[s][d]) except KeyError: print("net_snapshot at time %d did not contain link %s -> %s" % (time_idx, s, d)) mean_utils.append(util.bytes_per_second_to_mbps(mean(link_utils_over_time))) errors.append(util.bytes_per_second_to_mbps(np.std(link_utils_over_time))) labels.append(dpid_to_id[s]) ind = np.arange(1, 12) width = 0.35 fig, ax = plt.subplots() ys = [mu for mu, l in sorted(zip(mean_utils, labels), key=lambda t: t[1])] xs = labels errors = [e for e, l in sorted(zip(errors, labels), key=lambda t: t[1])] ax.set_xticks(ind+(width/2)) ax.set_xticklabels(range(1, 12)) ax.bar(ind-(width/2), ys, width, label="Measured", color="skyblue", hatch=".", yerr=errors, ecolor="black") mirroring_utils = defaultdict(float) for flow in flows.values(): mirroring_switch_for_flow = solutions[flow.flow_id].mirror_switch_id mirroring_utils[mirroring_switch_for_flow] += flow.traffic_rate ys = [] xs = [] for switch_id in range(1, 12): aggregate_rate = mirroring_utils[switch_id] xs.append(switch_id) ys.append(aggregate_rate * 100) ax.bar(ind+(width/2), ys, width, color="green", hatch="\\", label="Expected") plt.legend(loc="upper center", bbox_to_anchor=(0.5, cfg.LEGEND_HEIGHT), shadow=True, ncol=2) plt.rc('text', usetex=True) plt.rc('font', **cfg.FONT) plt.xlabel("Switch ID") plt.ylabel("Mean Mirroring Port Rate ($\\frac{Mb}{s}$)") helpers.save_figure("plot-three.pdf") plt.clf()
def generate_theoretical_vs_actual_utilization_bar_plot(results_repository): utilization_data, theoretical_data = compute_theoretical_and_actual_mean_utilization( results_repository) actual_std_deviation = compute_actual_std_deviation(results_repository) width = 0.35 labels = ["rnd", "det", "df", "greedy", "optimal"] legend_labels = ["\\epsilon-LPR", "LPR", "DuFi", "Greedy", "Optimal"] colors = ["orange", "green", "skyblue", "purple", "yellow"] hatch = ["//", "\\", "//", "\\", "//"] for solution_name in labels: ind = np.arange(1, 6) fig, ax = plt.subplots() data_tuples = sorted( [(k, v) for k, v in utilization_data[solution_name].items()], key=lambda kvp: kvp[0]) yerr_values = actual_std_deviation[solution_name] xs = [flow_count for flow_count, _ in data_tuples] ys = [util.bytes_per_second_to_mbps(data) for _, data in data_tuples] ax.bar(ind - (width / 2), ys, width, color="green", hatch="\\", tick_label=xs, label="Measured", yerr=yerr_values, ecolor="black") theoretical_tuples = sorted( [(k, v) for k, v in theoretical_data[solution_name].items()], key=lambda kvp: kvp[0]) xs = [flow_count for flow_count, _ in theoretical_tuples] ys = [ util_val * pm_cfg.rate_factor for _, util_val in theoretical_tuples ] ax.bar(ind + (width / 2), ys, width, color="skyblue", hatch=".", tick_label=xs, label="Expected") plt.rc('text', usetex=True) plt.rc('font', **cfg.FONT) plt.xlabel("Number of Flows") plt.ylabel("Maximum mirroring port rate ($\\frac{Mb}{s}$)") plt.grid() plt.legend(ncol=len(labels), **cfg.LEGEND) helpers.save_figure("plot-two-%s.pdf" % solution_name)
def generate_link_utilization_box_plot(results_repository): list_of_mean_utils = [] for trial_type in TRIAL_TYPES: mean_link_utils_per_seed_number = [] for idx, seed_number in enumerate(["test-trial-830656277"]): utilization_results, the_vle_trial, end_host_results = read_results( results_repository, trial_type, seed_number) link_utilization_data = compute_network_util_over_time( utilization_results) link_ids = [(s, d) for s, t in link_utilization_data[0].items() for d in t.keys()] mean_utils = {} for s, d in link_ids: link_utils_over_time = [] for time_idx, net_snapshot in enumerate(link_utilization_data): try: link_utils_over_time.append(net_snapshot[s][d]) except KeyError: print( "net_snapshot at time %d did not contain link %s -> %s" % (time_idx, s, d)) mean_utils[s, d] = ((mean(link_utils_over_time) * 8) / 10**6 / 1000) list_of_mean_utils.append(mean_utils) pp.pprint([list(v.values()) for v in list_of_mean_utils]) bp = plt.boxplot([list(v.values()) for v in list_of_mean_utils], labels=LABEL_NAMES, whiskerprops={"linestyle": "--"}, flierprops={ "marker": "x", "markerfacecolor": "red", "markeredgecolor": "red" }) for element in ["boxes"]: plt.setp(bp[element], color="blue") plt.setp(bp["medians"], color="red") plt.rc("font", **cfg.FONT) plt.rc("legend", edgecolor="black") # xtick_locations = [200, 400, 600, 800, 1000] # ytick_locations = [2, 4, 6, 8, 10, 12, 14] ytick_locations, _ = plt.yticks() plt.yticks(ytick_locations, [helpers.tick_font(y_i, "%.1f") for y_i in ytick_locations]) plt.ylabel("Link Utilization", **cfg.AXIS_LABELS) helpers.save_figure("link-utilization-box-plot.pdf", 3, no_legend=True)
def generate_theoretical_vs_actual_compact_bar_plot(results_repository): utilization_data, theoretical_data = compute_theoretical_and_actual_mean_utilization(results_repository) actual_error = compute_theoretical_and_actual_error(results_repository) std_deviations = defaultdict(list) for provider_name, data_list in actual_error.items(): for flow_count, data in data_list: std_deviations[provider_name].append(np.std( [util.bytes_per_second_to_mbps(d_i) for d_i in data])) width = 0.35 ind = np.arange(5) fig, ax = plt.subplots() labels = ["optimal", "approx"] half = len(labels)//2 legend_labels = ["Optimal", "BiSec"] colors = cfg.BAR_PLOT_COLORS hatch = [".", "\\"] bar_locations = [w for w in np.arange((width/2), len(labels)*width, width)] pp.pprint(utilization_data) pp.pprint(theoretical_data) for bar_idx, solution_name in enumerate(labels): data_tuples = sorted([(k, v) for k, v in utilization_data[solution_name]], key=lambda kvp: kvp[0]) theoretical_tuples = sorted([(k, v) for k, v in theoretical_data[solution_name]], key=lambda kvp: kvp[0]) xs = [flow_count for flow_count, _ in data_tuples] measured_ys = [util.bytes_per_second_to_mbps(data) for _, data in data_tuples] theoretical_ys = [util_val * pm_cfg.rate_factor for _, util_val in theoretical_tuples] theoretical_ys = [abs(theoretical_ys[idx] - measured_ys[idx]) for idx in range(len(theoretical_ys))] ax.bar(ind+bar_locations[bar_idx], theoretical_ys, width, color=colors[bar_idx], hatch=hatch[bar_idx], label=legend_labels[bar_idx], align="center", ecolor="black") plt.rc('text', usetex=True) plt.rc('font', **cfg.FONT) plt.xlabel("Number of Flows") plt.ylabel("Maximum switch load (Mbps)") plt.xticks(ind+(width*len(labels))/2, (ind+1)*10) plt.grid() plt.xlim(0, max(ind) + (width*len(labels))) plt.legend(loc="upper center", bbox_to_anchor=(0.5, cfg.LEGEND_HEIGHT), shadow=True, ncol=len(labels)) helpers.save_figure("sfm-theorypractice.pdf")
def generate_number_of_successful_requests_bar_plot(parameter_name, parameter_value, trials): """ Generate a bar plot showing the number of requests that completed successfully in each of the trials. """ bar_width = 0.2 bar_x_locations = np.arange(0.0, (len(trials)+1)*2*bar_width, 2*bar_width) bar_labels = [the_trial.name.replace("-approximate", "") for the_trial in trials] for idx, the_trial in enumerate(trials): print(f"generate_number_of_successful_requests_bar_plot: {idx}, {the_trial.name}") y_value = the_trial.get_parameter("number-of-successful-flows") helpers.plot_a_bar(bar_x_locations[idx], y_value, label=bar_labels[idx], bar_width=bar_width, idx=idx) plt.xticks(bar_x_locations, bar_labels) helpers.save_figure(f"successful-requests-bar-{parameter_name}-{parameter_value}.pdf", no_legend=True)
def generate_computed_link_utilization_box_plot(trial_provider): grouped_by_name = collect_trials_based_on_name(trial_provider) labels = [helpers.axis_label_font(multiflow_label(group[0].name)) for group in grouped_by_name] # Make lists of link utilization data link_utilization_data = [reduce(op.add, [list(t_i.get_parameter("measured-link-utilization").values()) for t_i in group if t_i.has_parameter("measured-link-utilization")], []) for group in grouped_by_name] plot_a_box_plot(link_utilization_data, labels) plt.ylabel(helpers.axis_label_font("Link utilization")) node_selection_type = trial_provider.get_metadata("node-selection-type") helpers.save_figure("computed-link-utilization-%s-box.pdf" % node_selection_type, no_legend=True)
def plot_share_delay_histogram(packets): """ Takes a list of PacketInfo tuples as input and plots the PDF of the intershare delay. """ seq_num_to_ts = defaultdict(list) for p_i in packets: seq_num_to_ts[p_i.seq_num].append(p_i.timestamp) inter_share_delays = [] for seq_num, ts in seq_num_to_ts.items(): inter_share_delay = max(ts) - min(ts) inter_share_delays.append(inter_share_delay) plt.histogram(inter_share_delays, density=True) helpers.save_figure("share-delay-pdf.pdf")