|
1 | 1 | # importing libs
|
2 | 2 | from tkinter import *
|
3 | 3 | import os
|
| 4 | +import csv |
4 | 5 |
|
5 | 6 |
|
6 | 7 | #######################################################################################################################
|
7 | 8 | # Main >>>
|
8 |
| -def main(classes=['cat', 'dog']): |
9 |
| - # Window Creation: Train to melNET |
| 9 | +def main(classes): |
| 10 | + # Window Creation: Create Confusion Matrix |
10 | 11 | root = Tk()
|
11 |
| - root.title("Save Confusion Matrix") # Title |
| 12 | + root.title("Confusion Matrix Creation") # Title |
12 | 13 | global applied
|
13 | 14 | applied = False
|
14 | 15 |
|
@@ -55,21 +56,95 @@ def quit_button_applied():
|
55 | 56 | exit()
|
56 | 57 |
|
57 | 58 | if class_1:
|
58 |
| - pos_class = classes[0] |
| 59 | + pos_class = classes[0].upper() |
| 60 | + neg_class = classes[1].upper() |
59 | 61 | else:
|
60 |
| - pos_class = classes[1] |
| 62 | + pos_class = classes[1].upper() |
| 63 | + neg_class = classes[0].upper() |
61 | 64 |
|
| 65 | + fold_num = 5 |
62 | 66 | if five_fold:
|
| 67 | + for fold in range(fold_num): |
| 68 | + root = os.getcwd() + "/aug_Data/Five_Fold_(Aug)/Fold_"+str(fold+1)+"/Test" |
| 69 | + result_path = root + "/_result.csv" |
| 70 | + conf_mat_path = root + "/conf_mat.csv" |
| 71 | + error_path = root + "/error_analysis.csv" |
| 72 | + err_thresh = 70 |
| 73 | + conf_mat_make(result_path, conf_mat_path, error_path, err_thresh, pos_class, neg_class) |
63 | 74 |
|
64 | 75 | else:
|
65 |
| - res_root = os.getcwd() + "/aug_Data/Single_Fold_(Aug)/Test" |
66 |
| - |
| 76 | + result_path = os.getcwd() + "/aug_Data/Single_Fold_(Aug)/Test/_result.csv" |
| 77 | + conf_mat_path = os.getcwd() + "/aug_Data/Single_Fold_(Aug)/Test/conf_mat.csv" |
| 78 | + error_path = os.getcwd() + "/aug_Data/Single_Fold_(Aug)/Test/error_analysis.csv" |
| 79 | + err_thresh = 70 |
| 80 | + conf_mat_make(result_path, conf_mat_path, error_path, err_thresh, pos_class, neg_class) |
67 | 81 |
|
68 | 82 | else:
|
69 | 83 | print("Confusion Matrix is not being SAVED!")
|
70 | 84 | exit()
|
71 | 85 |
|
72 | 86 |
|
| 87 | +def conf_mat_make(result_path, conf_mat_path, error_path, err_thresh, pos_class, neg_class): |
| 88 | + # Result Evaluation |
| 89 | + conf_mat = open(conf_mat_path, 'w') |
| 90 | + error = open(error_path, 'w') |
| 91 | + |
| 92 | + for thresh in range(0, 105, 5): |
| 93 | + tp = fp = tn = fn = 0 |
| 94 | + pos_idx = 1 |
| 95 | + |
| 96 | + with open(result_path) as result: |
| 97 | + result_reader = csv.reader(result, delimiter=',') |
| 98 | + line_count = 0 |
| 99 | + |
| 100 | + for row in result_reader: |
| 101 | + # Setting Headers and positive class |
| 102 | + if line_count == 0: |
| 103 | + if thresh == 0: |
| 104 | + conf_mat.write('Threshold, TP, FP, TN, FN, Sensitivity, Specificity, Accuracy\n') |
| 105 | + error.write(f'{", ".join(row)}' + '\n') |
| 106 | + if pos_class == str(row[2]): |
| 107 | + pos_idx = 2 |
| 108 | + line_count += 1 |
| 109 | + |
| 110 | + # Going through every row (starting from 2nd one) and evaluate |
| 111 | + else: |
| 112 | + truth = row[4].upper() |
| 113 | + if (float(row[pos_idx]) * 100) >= thresh: |
| 114 | + decision = pos_class |
| 115 | + if decision == truth: |
| 116 | + tp += 1 |
| 117 | + else: |
| 118 | + fp += 1 |
| 119 | + if thresh == err_thresh: |
| 120 | + error.write(f'{", ".join(row)}' + '\n') |
| 121 | + else: |
| 122 | + decision = neg_class |
| 123 | + if decision == truth: |
| 124 | + tn += 1 |
| 125 | + else: |
| 126 | + fn += 1 |
| 127 | + if thresh == 50: |
| 128 | + error.write(f'{", ".join(row)}' + '\n') |
| 129 | + |
| 130 | + # Calculation |
| 131 | + acc = (tp + tn) / (tp + fp + tn + fn) |
| 132 | + sen = tp / (tp + fn) |
| 133 | + spe = tn / (tn + fp) |
| 134 | + # pre = tp / (tp+fp) |
| 135 | + |
| 136 | + '''print(">>> Threshold: " + str(thresh)) |
| 137 | + print('TP: ' + str(tp) + ', FP: ' + str(fp) + ', TN: ' + str(tn) + ', FN: ' + str(fn)) |
| 138 | + print('Accuracy: ' + str(acc)) |
| 139 | + print('Sensitivity: ' + str(sen)) |
| 140 | + print('Specificity: ' + str(spe)) |
| 141 | + # print('Precision: ' + str(pre))''' |
| 142 | + |
| 143 | + # Storing calculations in a file |
| 144 | + conf_mat.write(str(thresh*0.01) + ',' + str(tp) + ',' + str(fp) + ',' + str(tn) + ',' + str(fn) + ',' + str(sen) |
| 145 | + + ',' + str(spe) + ',' + str(acc) + '\n') |
| 146 | + |
| 147 | + |
73 | 148 | #######################################################################################################################
|
74 | 149 | # Main Call Func. >>>
|
75 | 150 | if __name__ == "__main__":
|
|
0 commit comments