From 3e0dbb9168b33afe7b4343c855c3a2399a59d9e7 Mon Sep 17 00:00:00 2001 From: coolneng Date: Tue, 20 Apr 2021 11:58:24 +0200 Subject: [PATCH] Populate results with mean and std deviation --- src/execution.py | 56 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/src/execution.py b/src/execution.py index 43c02dd..864fbf2 100644 --- a/src/execution.py +++ b/src/execution.py @@ -12,41 +12,67 @@ def file_list(path): return file_list -def create_dataframes(datasets): - greedy = DataFrame(columns=["dataset", "distancia"]) - local = DataFrame(columns=["dataset", "distancia"]) - greedy["dataset"] = datasets - local["dataset"] = datasets +def create_dataframes(): + greedy = DataFrame(columns=["dataset", "distancia", "desviacion"]) + local = DataFrame(columns=["dataset", "distancia", "desviacion"]) return greedy, local def process_output(results): - for line in results: - if line.startswith(bytes("Total distance:", encoding="utf-8")): - line_elements = line.split(sep=bytes(":", encoding="utf-8")) - distance = float(line_elements[1]) - return distance + distances = [] + for element in results: + for line in element: + if line.startswith(bytes("Total distance:", encoding="utf-8")): + line_elements = line.split(sep=bytes(":", encoding="utf-8")) + distances.append(float(line_elements[1])) + return distances + + +def populate_dataframes(greedy, local, greedy_list, local_list, dataset): + greedy_results = process_output(greedy_list) + local_results = process_output(local_list) + greedy_dict = { + "dataset": dataset, + "distancia": mean(greedy_results), + "desviacion": std(greedy_results), + } + local_dict = { + "dataset": dataset, + "distancia": mean(local_results), + "desviacion": std(local_results), + } + greedy = greedy.append(greedy_dict, ignore_index=True) + local = local.append(local_dict, ignore_index=True) + return greedy, local def script_execution(filenames, greedy, local, iterations=3): script = "src/main.py" for dataset in filenames: + print(f"Running on dataset {dataset}") greedy_list = [] local_list = [] for _ in range(iterations): greedy_cmd = run( [executable, script, dataset, "greedy"], capture_output=True ).stdout.splitlines() - greedy_list.append(greedy_cmd) local_cmd = run( [executable, script, dataset, "local"], capture_output=True ).stdout.splitlines() + greedy_list.append(greedy_cmd) local_list.append(local_cmd) - greedy.loc[greedy["dataset"] == dataset, ["distancia"]] = mean(greedy_list) - local.loc[local["dataset"] == dataset, ["distancia"]] = mean(local_list) + greedy, local = populate_dataframes( + greedy, local, greedy_list, local_list, dataset + ) + return greedy, local + + +def export_results(greedy, local): + greedy.to_excel() + local.to_excel() if __name__ == "__main__": datasets = file_list(path="data/*.txt") - greedy, local = create_dataframes(datasets) - script_execution(filenames=datasets, greedy=greedy, local=local) + greedy, local = create_dataframes() + populated_greedy, populated_local = script_execution(datasets, greedy, local)