Commit 27b4eb58 authored by Dennis Willers's avatar Dennis Willers
Browse files

Fix Excel File Bug - Save Total Excel Results

parent 5df14c66
Loading
Loading
Loading
Loading
+133 −0
Original line number Diff line number Diff line
import os
import openpyxl


def swap_excel_cols(folder_path):
    # Iteriere durch alle Excel-Dateien im Ordner
    for filename in os.listdir(folder_path):
        if filename.endswith(".xlsx"):
            file_path = os.path.join(folder_path, filename)
            swap_file_cols(file_path)


def swap_file_cols(file_path):
    # Lade das Workbook
    wb = openpyxl.load_workbook(file_path)

    # Wähle das erste Blatt aus
    ws = wb.worksheets[0]

    # Definiert die Zeilen, in denen die Spalten getauscht werden sollen
    rows = [19, 42, 65, 88, 111, 134, 140]

    # Tausche die Spalten in jeder Zeile
    for row in rows:
        col_d = ws.cell(row=row + 1, column=4).value
        col_e = ws.cell(row=row + 1, column=5).value
        ws.cell(row=row + 1, column=4).value = col_e
        ws.cell(row=row + 1, column=5).value = col_d

    # Speichere das Workbook
    wb.save(file_path)


def read_metrics(path):
    # Save unique values of opt and act in a dictionary
    unique_opt = dict()
    unique_act = dict()
    final_matrix_loss = dict()
    final_matrix_accuracy = dict()
    final_matrix_recall_oos = dict()
    final_matrix_recall_not_oos = dict()
    final_matrix_duration = dict()

    for file in os.listdir(path):
        if file.endswith(".xlsx"):
            filename = os.path.join(path, file)
            wb = openpyxl.load_workbook(filename)
            sheet = wb.active

            # Split file name and get opt, act2, and act_128
            opt, act2, act_128 = file.split("_")[0], file.split("_")[1], file.split("_")[2]

            # Combine act2 and act_128
            act = act2 + " " + act_128

            # Get metrics
            loss = sheet["B141"].value
            accuracy = sheet["C141"].value
            recall_oos = sheet["D141"].value
            recall_not_oos = sheet["E141"].value
            duration = sheet["F141"].value
            duration, _ = duration.split(".")

            # Add opt and act to the final matrix
            if opt not in unique_opt:
                count_opt = len(unique_opt) + 2
                unique_opt[opt] = count_opt

            if act not in unique_act:
                count_act = len(unique_act) + 2
                unique_act[act] = count_act

            # Write accuracy to the final matrix
            final_matrix_loss[opt + act] = loss
            final_matrix_accuracy[opt + act] = accuracy
            final_matrix_recall_oos[opt + act] = recall_oos
            final_matrix_recall_not_oos[opt + act] = recall_not_oos
            final_matrix_duration[opt + act] = duration

    create_metriken_matrix(unique_opt, unique_act,
                           final_matrix_loss, final_matrix_accuracy, final_matrix_recall_oos,
                           final_matrix_recall_not_oos, final_matrix_duration)


def create_metriken_matrix(
        unique_opt, unique_act, final_matrix_loss, final_matrix_accuracy,
        final_matrix_recall_oos, final_matrix_recall_not_oos, final_matrix_duration):
    # Create a new Excel workbook and write the final matrix
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet = write_matrix_in_excel(sheet, unique_opt, unique_act, final_matrix_loss, 0)
    sheet = write_matrix_in_excel(sheet, unique_opt, unique_act, final_matrix_accuracy, 6)
    sheet = write_matrix_in_excel(sheet, unique_opt, unique_act, final_matrix_recall_oos, 12)
    sheet = write_matrix_in_excel(sheet, unique_opt, unique_act, final_matrix_recall_not_oos, 18)
    sheet = write_matrix_in_excel(sheet, unique_opt, unique_act, final_matrix_duration, 24)
    wb.save("evaluation_oos_erkennung.xlsx")


def write_matrix_in_excel(sheet, unique_opt, unique_act, final_matrix, c):

    sheet.cell(row=1, column=c+1, value="2")
    sheet.cell(row=1, column=c+2, value="128")

    # Write opt names
    count_opt = 3
    for opt_key in sorted(unique_opt.keys()):
        sheet.cell(row=1, column=c+count_opt, value=opt_key)
        count_opt += 1

    # Write act names
    count_act = 2
    for act_key in sorted(unique_act.keys()):
        act_2, act128 = act_key.split(" ")
        sheet.cell(row=count_act, column=c+1, value=act_2)
        sheet.cell(row=count_act, column=c+2, value=act128)
        count_act += 1

    count_opt = 3
    # Write accuracy values
    for opt_key in sorted(unique_opt.keys()):
        count_act = 2
        for act_key in sorted(unique_act.keys()):
            if opt_key + act_key in final_matrix:
                sheet.cell(row=count_act, column=c+count_opt, value=final_matrix[opt_key + act_key])
            count_act += 1
        count_opt += 1
    return sheet


# Call the function with the folder path
# swap_excel_cols("/Users/dwillers/Programmierung/Master/Okan-Server/Results Kopie/5050")
# swap_file_cols("/Users/dwillers/Programmierung/Master/Okan-Server/Results Kopie/5050/Ftrl_softmax_softmax_03_02_23__03_19.xlsx")
# read_metrics("/Users/dwillers/Programmierung/Master/Okan-Server/Results Kopie/5050")
+4 −4
Original line number Diff line number Diff line
@@ -73,8 +73,8 @@ def create_excel_result(worksheet, r, callback, config, config_knn, evaluate_met
    worksheet.cell(row=r, column=1).value = config_knn.excluded_folder.name
    worksheet.cell(row=r, column=2).value = evaluate_metrics[0]
    worksheet.cell(row=r, column=3).value = evaluate_metrics[1]
    worksheet.cell(row=r, column=4).value = evaluate_metrics[2]
    worksheet.cell(row=r, column=5).value = evaluate_metrics[3]
    worksheet.cell(row=r, column=4).value = evaluate_metrics[3]
    worksheet.cell(row=r, column=5).value = evaluate_metrics[2]
    r = r + 4
    return worksheet, r

@@ -98,8 +98,8 @@ def average_evaluate_cross_validation(worksheet, r, model_evaluate_metrics, trai
    worksheet.cell(row=r, column=1).value = len(training_duration_models)
    worksheet.cell(row=r, column=2).value = total_information[0]
    worksheet.cell(row=r, column=3).value = total_information[1]
    worksheet.cell(row=r, column=4).value = total_information[2]
    worksheet.cell(row=r, column=5).value = total_information[3]
    worksheet.cell(row=r, column=4).value = total_information[3]
    worksheet.cell(row=r, column=5).value = total_information[2]
    # Durchschnittswert wieder in Datetime-Objekt umwandeln
    average_datetime = format(total_information[4])
    worksheet.cell(row=r, column=6).value = average_datetime
+11.5 KiB

File added.

No diff preview for this file type.