Write results to an Excel file

This commit is contained in:
coolneng 2021-04-20 13:22:01 +02:00
parent 3e0dbb9168
commit b8b812d011
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
2 changed files with 21 additions and 6 deletions

View File

@ -3,5 +3,10 @@
with pkgs; with pkgs;
mkShell { mkShell {
buildInputs = [ python39 python39Packages.numpy python39Packages.pandas ]; buildInputs = [
python39
python39Packages.numpy
python39Packages.pandas
python39Packages.XlsxWriter
];
} }

View File

@ -1,8 +1,9 @@
from glob import glob from glob import glob
from subprocess import run from subprocess import run
from sys import executable from sys import executable
from numpy import mean, std from numpy import mean, std
from pandas import DataFrame from pandas import DataFrame, ExcelWriter
def file_list(path): def file_list(path):
@ -32,12 +33,12 @@ def populate_dataframes(greedy, local, greedy_list, local_list, dataset):
greedy_results = process_output(greedy_list) greedy_results = process_output(greedy_list)
local_results = process_output(local_list) local_results = process_output(local_list)
greedy_dict = { greedy_dict = {
"dataset": dataset, "dataset": dataset.removeprefix("data/"),
"distancia": mean(greedy_results), "distancia": mean(greedy_results),
"desviacion": std(greedy_results), "desviacion": std(greedy_results),
} }
local_dict = { local_dict = {
"dataset": dataset, "dataset": dataset.removeprefix("data/"),
"distancia": mean(local_results), "distancia": mean(local_results),
"desviacion": std(local_results), "desviacion": std(local_results),
} }
@ -68,11 +69,20 @@ def script_execution(filenames, greedy, local, iterations=3):
def export_results(greedy, local): def export_results(greedy, local):
greedy.to_excel() dataframes = {"Greedy": greedy, "Local search": local}
local.to_excel() 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__": if __name__ == "__main__":
datasets = file_list(path="data/*.txt") datasets = file_list(path="data/*.txt")
greedy, local = create_dataframes() greedy, local = create_dataframes()
populated_greedy, populated_local = script_execution(datasets, greedy, local) populated_greedy, populated_local = script_execution(datasets, greedy, local)
export_results(populated_greedy, populated_local)