From b8b812d011215154336181e2b8b5075838ca53cc Mon Sep 17 00:00:00 2001 From: coolneng Date: Tue, 20 Apr 2021 13:22:01 +0200 Subject: [PATCH] Write results to an Excel file --- shell.nix | 7 ++++++- src/execution.py | 20 +++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/shell.nix b/shell.nix index 30377b2..107409e 100644 --- a/shell.nix +++ b/shell.nix @@ -3,5 +3,10 @@ with pkgs; mkShell { - buildInputs = [ python39 python39Packages.numpy python39Packages.pandas ]; + buildInputs = [ + python39 + python39Packages.numpy + python39Packages.pandas + python39Packages.XlsxWriter + ]; } diff --git a/src/execution.py b/src/execution.py index 864fbf2..6b77e4d 100644 --- a/src/execution.py +++ b/src/execution.py @@ -1,8 +1,9 @@ from glob import glob from subprocess import run from sys import executable + from numpy import mean, std -from pandas import DataFrame +from pandas import DataFrame, ExcelWriter def file_list(path): @@ -32,12 +33,12 @@ 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, + "dataset": dataset.removeprefix("data/"), "distancia": mean(greedy_results), "desviacion": std(greedy_results), } local_dict = { - "dataset": dataset, + "dataset": dataset.removeprefix("data/"), "distancia": mean(local_results), "desviacion": std(local_results), } @@ -68,11 +69,20 @@ def script_execution(filenames, greedy, local, iterations=3): def export_results(greedy, local): - greedy.to_excel() - local.to_excel() + dataframes = {"Greedy": greedy, "Local search": local} + writer = ExcelWriter(path="docs/algorithm-results.xlsx", engine="xlsxwriter") + for name, df in dataframes.items(): + df.to_excel(writer, sheet_name=name, index=False) + worksheet = writer.sheets[name] + for index, column in enumerate(df): + series = df[column] + max_length = series.astype(str).str.len().max() + worksheet.set_column(index, index, width=max_length + 5) + writer.save() if __name__ == "__main__": datasets = file_list(path="data/*.txt") greedy, local = create_dataframes() populated_greedy, populated_local = script_execution(datasets, greedy, local) + export_results(populated_greedy, populated_local)