-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.py
131 lines (103 loc) · 5.54 KB
/
report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
import datetime
def read_training_data(file_path, type):
print(f"Reading {type}ing data from {file_path}")
if type=="train":
with open(file_path, 'r') as file:
lines = file.readlines()
training_data = []
for line in lines:
if line.startswith("Epoch"):
epoch_data = {}
line_data = line.split(", ")
#print(line_data)
epoch_data["Epoch"] = int(line_data[0].split(" ")[1].split("/")[0])
epoch_data["Train Accuracy"] = float(line_data[2].split(": ")[1])
epoch_data["Train AUC-ROC"] = float(line_data[3].split(": ")[1])
epoch_data["Train F1 Score"] = float(line_data[4].split(": ")[1])
epoch_data["Train Precision"] = float(line_data[5].split(": ")[1])
epoch_data["Train Recall"] = float(line_data[6].split(": ")[1])
training_data.append(epoch_data)
return training_data
else:
with open(file_path, 'r') as file:
lines = file.readlines()
training_data = []
for line in lines:
if line.startswith("Epoch"):
epoch_data = {}
line_data = line.split(",")
#print(line_data)
epoch_data["Epoch"] = int(line_data[0].split(" ")[1].split("/")[0])
epoch_data["Test Accuracy"] = float(line_data[1].split(": ")[1])
epoch_data["Test AUC-ROC"] = float(line_data[2].split(": ")[1])
epoch_data["Test F1 Score"] = float(line_data[3].split(": ")[1])
epoch_data["Test Precision"] = float(line_data[4].split(": ")[1])
epoch_data["Test Recall"] = float(line_data[5].split(": ")[1])
training_data.append(epoch_data)
return training_data
def find_highest_metrics(training_data,type):
if type=="train":
highest_accuracy = max(training_data, key=lambda x: x["Train Accuracy"])
highest_auc_roc = max(training_data, key=lambda x: x["Train AUC-ROC"])
highest_f1_score = max(training_data, key=lambda x: x["Train F1 Score"])
highest_precision = max(training_data, key=lambda x: x["Train Precision"])
highest_recall = max(training_data, key=lambda x: x["Train Recall"])
return highest_accuracy, highest_auc_roc, highest_f1_score, highest_precision, highest_recall
else:
highest_accuracy = max(training_data, key=lambda x: x["Test Accuracy"])
highest_auc_roc = max(training_data, key=lambda x: x["Test AUC-ROC"])
highest_f1_score = max(training_data, key=lambda x: x["Test F1 Score"])
highest_precision = max(training_data, key=lambda x: x["Test Precision"])
highest_recall = max(training_data, key=lambda x: x["Test Recall"])
return highest_accuracy, highest_auc_roc, highest_f1_score, highest_precision, highest_recall
def generate_report(dataset_name, today_date, highest_accuracy, highest_auc_roc, highest_f1_score, highest_precision, highest_recall,type,file_path):
if type=="train":
report = f"Dataset: {dataset_name}\n"
report +=f"Log path: {file_path}\n"
report += f"Date: {today_date}\n\n"
report += "Epoch with highest Train Accuracy:\n"
report += str(highest_accuracy) + "\n\n"
report += "Epoch with highest Train AUC-ROC:\n"
report += str(highest_auc_roc) + "\n\n"
report += "Epoch with highest Train F1 Score:\n"
report += str(highest_f1_score) + "\n\n"
report += "Epoch with highest Train Precision:\n"
report += str(highest_precision) + "\n\n"
report += "Epoch with highest Train Recall:\n"
report += str(highest_recall) + "\n\n"
return report
else:
report = f"Dataset: {dataset_name}\n"
report += f"Date: {today_date}\n\n"
report += "Epoch with highest Test Accuracy:\n"
report += str(highest_accuracy) + "\n\n"
report += "Epoch with highest Test AUC-ROC:\n"
report += str(highest_auc_roc) + "\n\n"
report += "Epoch with highest Test F1 Score:\n"
report += str(highest_f1_score) + "\n\n"
report += "Epoch with highest Test Precision:\n"
report += str(highest_precision) + "\n\n"
report += "Epoch with highest Test Recall:\n"
report += str(highest_recall) + "\n\n"
return report
def save_report(report, folder_path):
os.makedirs(folder_path, exist_ok=True)
today_date = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
file_name = f"{folder_path}/report_{today_date}.txt"
with open(file_name, 'w') as file:
file.write(report)
print(f"Report saved successfully at {file_name}")
def main():
file_path = "output/fda/test/test_accuracy_details_2024-05-03_05-16-01.txt"
dataset_name = "fda"
type = "test"
loss="bin" # bin or recon
today_date = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
training_data = read_training_data(file_path,type)
highest_accuracy, highest_auc_roc, highest_f1_score, highest_precision, highest_recall = find_highest_metrics(training_data,type)
report = generate_report(dataset_name, today_date, highest_accuracy, highest_auc_roc, highest_f1_score, highest_precision, highest_recall,type,file_path)
folder_path = f"Report/{dataset_name}/{type}/{loss}"
save_report(report, folder_path)
if __name__ == "__main__":
main()