diff --git a/scripts/analysis.py b/scripts/analysis.py index 8af4dfb..b543fd6 100644 --- a/scripts/analysis.py +++ b/scripts/analysis.py @@ -38,9 +38,16 @@ def read_csv(fname): def write_csv(df, fname): df.to_csv(fname) -def compute_view_finalisation_times(df, conf, oprefix, tag="tag", plot=False): - num_nodes = conf["node_count"] - two3rd = math.floor(num_nodes * 2/3) + 1 +def compute_view_finalisation_times(df, conf, oprefix, simtype, tag="tag", plot=False): + if simtype == "tree": + num_nodes = conf["node_count"] + else: + num_tree_nodes = 2 ** (conf["overlay_settings"]["branch_depth"]) - 1 + num_committees = int (conf["node_count"]/conf["overlay_settings"]["branch_depth"]) + num_nodes = num_tree_nodes * num_committees + log.debug(f"num nodes: {num_nodes, num_tree_nodes, num_committees}") + + two3rd = math.floor(conf["node_count"] * 2/3) + 1 views, view2fin_time = df.current_view.unique()[:-2], {} log.debug(f'views: {views}') @@ -71,18 +78,24 @@ def compute_view_finalisation_times(df, conf, oprefix, tag="tag", plot=False): plt.show() plt.savefig(f'{oprefix}-view-finalisation-times.pdf', format="pdf", bbox_inches="tight") -def compute_view_lengths(path, oprefix): +def compute_view_times(path, oprefix): pathlen2vfins = {} conf_fnames = next(walk(f'{path}/configs'), (None, None, []))[2] for conf in conf_fnames: tag = os.path.splitext(os.path.basename(conf))[0] - cfile, dfile = f'{path}/configs/{conf}', f'{path}/output/output/{tag}.csv' + cfile, dfile = f'{path}/configs/{conf}', f'{path}/output/{tag}.csv' conf, df = read_json(cfile), read_csv(dfile) simtype = conf["stream_settings"]["path"].split("/")[1].split("_")[0] - view2fin = compute_view_finalisation_times(df, conf, oprefix, tag, plot=False) + view2fin = compute_view_finalisation_times(df, conf, oprefix, simtype, tag, plot=False) if not view2fin: # < 2 views continue - num_nodes = conf["node_count"] + if simtype == "tree": + num_nodes = conf["node_count"] + else: + num_tree_nodes = 2 ** (conf["overlay_settings"]["branch_depth"]) - 1 + num_committees = int (conf["node_count"]/conf["overlay_settings"]["branch_depth"]) + num_nodes = num_tree_nodes * num_committees + #num_nodes = conf["node_count"] if simtype == "branch": max_depth = conf["overlay_settings"]["branch_depth"] else: @@ -93,6 +106,81 @@ def compute_view_lengths(path, oprefix): pathlen2vfins[num_nodes] = [(simtype, max_depth, view2fin, tag)] return pathlen2vfins +def plot_view_times(pathlen2vfins, simtype, oprefix): + logbands = {} + logbands[simtype] = {} + logbands[simtype]["low"] = [] + logbands[simtype]["high"] = [] + + if simtype == "branch": + low, high = 5, 7 + else: + low, high = 1, 2 + data = [[], []] + print("READ FROM FILE", pathlen2vfins) + for n in sorted(list(map(int, pathlen2vfins.keys()))): + vfin = pathlen2vfins[n] + for run in vfin: + if "default" in run[3] and simtype in run[0]: + data[0].append(n) + data[1].append(int(run[2][0])) + logbands[simtype]["low"].append(int(run[1])*low) + logbands[simtype]["high"].append(int(run[1])*high) + + print(data) + fig, axes = plt.subplots(1, 1, layout='constrained', sharey=False) + fig.set_figwidth(12) + fig.set_figheight(10) + + fig.suptitle(f'View Finalisation Times - {simtype}') + axes.set_ylabel("Number of Epochs") + axes.set_xlabel("Number of Nodes") + + l1 = axes.plot(data[0], data[1], linestyle='-', marker='o', label='Carnot') + l2 = axes.plot(data[0], logbands[simtype]["low"], linestyle='--', marker='x', label=f'{low} * log(#nodes)') + l3 = axes.plot(data[0], logbands[simtype]["high"], linestyle='--', marker='x', label=f'{high} * log(#nodes)') + l = l1 + l2 + l3 + + labels = [curve.get_label() for curve in l] + axes.legend(l, labels, loc="lower right") + + plt.show() + plt.savefig(f'{oprefix}-{simtype}-output.pdf', format="pdf", bbox_inches="tight") + plt.clf() + plt.cla() + plt.close() + + +def plot_tree_vs_branch(pathlen2vfins,oprefix): + data = [[], []] + print("READ FROM FILE", pathlen2vfins) + for n in sorted(list(map(int, pathlen2vfins.keys()))): + vfin = pathlen2vfins[n] + for run in vfin: + if "default" not in run[3]: + continue + if "tree" in run[0]: + data[0].append(int(run[2][0])) + elif: + data[1].append(int(run[2][0])) + + print(data) + fig, axes = plt.subplots(1, 1, layout='constrained', sharey=False) + fig.set_figwidth(12) + fig.set_figheight(10) + + fig.suptitle(f'View Finalisation Times - {simtype}') + axes.set_ylabel("Number of Epochs") + axes.set_xlabel("Number of Nodes") + + l1 = axes.plot(data[0], data[1], linestyle='-', marker='o', label='Carnot') + + plt.show() + plt.savefig(f'{oprefix}-scatter.pdf', format="pdf", bbox_inches="tight") + plt.clf() + plt.cla() + plt.close() + app = typer.Typer() @@ -111,7 +199,7 @@ def view(ctx: typer.Context, tag = os.path.splitext(os.path.basename(data_file))[0] conf, df = read_json(config_file), read_csv(data_file) - compute_view_finalisation_times(df, conf, oprefix, tag, plot=True) + compute_view_finalisation_times(df, conf, oprefix, simtype, tag, plot=True) @app.command() def views(ctx: typer.Context, @@ -119,45 +207,18 @@ def views(ctx: typer.Context, exists=True, dir_okay=True, readable=True, help="Set the simulation config file"), oprefix: str = typer.Option("output", - help="Set the output prefix for the plots") + help="Set the output prefix for the plots"), + simtype: str = typer.Option("tree", + help="Set the type of the simulation") ): log.basicConfig(level=log.INFO) - #pathlen2vfins = compute_view_lengths(path, oprefix) - #write_json(pathlen2vfins, f'{oprefix}-viewtimes.json') - #write_dict(pathlen2vfins, f'{oprefix}-viewtimes.dict') - - pathlen2vfins = read_dict(f'{oprefix}-viewtimes.dict') - data, log4, log5 = [[], []], [], [] - print("READ FROM FILE", pathlen2vfins) - for n in sorted(list(map(int, pathlen2vfins.keys()))): - vfin = pathlen2vfins[n] - for run in vfin: - if "default" in run[3] and "tree" in run[0]: - data[0].append(n) - data[1].append(int(run[2][0])) - log4.append(int(run[1])*1) - log5.append(int(run[1])*2) - - fig, axes = plt.subplots(1, 1, layout='constrained', sharey=False) - fig.set_figwidth(12) - fig.set_figheight(10) - - fig.suptitle(f'View Finalisation Times') - axes.set_ylabel("Number of Epochs") - axes.set_xlabel("Number of Nodes") - - l1 = axes.plot(data[0], data[1], linestyle='-', marker='o', label='Carnot') - l2 = axes.plot(data[0], log4, linestyle='--', marker='x', label='1 * log(#nodes)') - l3 = axes.plot(data[0], log5, linestyle='--', marker='x', label='2 * log(#nodes)') - l = l1 + l2 + l3 - - labels = [curve.get_label() for curve in l] - axes.legend(l, labels, loc="lower right") - - plt.show() - plt.savefig(f'{oprefix}-output.pdf', format="pdf", bbox_inches="tight") + #pathlen2vfins = compute_view_times(path, oprefix) + #write_dict(pathlen2vfins, f'{oprefix}-{simtype}-viewtimes.dict') + pathlen2vfins = read_dict(f'{oprefix}-{simtype}-viewtimes.dict') + plot_view_times(pathlen2vfins, "branch", oprefix) + plot_view_times(pathlen2vfins, "tree", oprefix) @app.command() def other_commands():