forked from B-Xi/TGRS_2025_MCTGCL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelStatsRecord.py
More file actions
172 lines (137 loc) · 7.59 KB
/
Copy pathmodelStatsRecord.py
File metadata and controls
172 lines (137 loc) · 7.59 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- coding: utf-8 -*-
import numpy as np
import time
import collections
from sklearn import metrics
#import averageAccuracy
def outputRecord(ELEMENT_ACC_RES_SS4, AA_RES_SS4, OA_RES_SS4, KAPPA_RES_SS4,
ELEMENT_PRE_RES_SS4, AP_RES_SS4, TRAINING_TIME_RES_SS4, TESTING_TIME_RES_SS4,
CATEGORY, ITER, path1, dataset_name=None, hyperparameters=None):
print_matrix = np.zeros((CATEGORY * 2 + 6, ITER + 1), dtype=object)
print_matrix[0:CATEGORY, 0:ITER] = np.around(ELEMENT_ACC_RES_SS4, 4)
print_matrix[CATEGORY, 0:ITER] = np.around(AA_RES_SS4, 4)
print_matrix[CATEGORY + 1, 0:ITER] = np.around(OA_RES_SS4, 4)
print_matrix[CATEGORY + 2, 0:ITER] = np.around(KAPPA_RES_SS4, 4)
print_matrix[CATEGORY + 3:CATEGORY * 2 + 3, 0:ITER] = np.around(ELEMENT_PRE_RES_SS4, 4)
print_matrix[CATEGORY * 2 + 3, 0:ITER] = np.around(AP_RES_SS4, 4)
print_matrix[CATEGORY * 2 + 4, 0:ITER] = np.around(TRAINING_TIME_RES_SS4, 4)
print_matrix[CATEGORY * 2 + 5, 0:ITER] = np.around(TESTING_TIME_RES_SS4, 4)
element_mean = np.mean(print_matrix[:, :-1], axis=1)
element_std = np.std(np.float64(print_matrix[:, :-1]), axis=1)
for i in range(CATEGORY * 2 + 4):
print_matrix[i, ITER] = "{:.2f}".format(element_mean[i] * 100) + " ± " + "{:.2f}".format(element_std[i] * 100)
for i in range((CATEGORY * 2 + 4), (CATEGORY * 2 + 6)):
print_matrix[i, ITER] = "{:.2f}".format(element_mean[i]) + " ± " + "{:.2f}".format(element_std[i])
# Create row labels for better readability
row_labels = []
for i in range(CATEGORY):
row_labels.append(f"Class {i+1} Accuracy")
row_labels.append("Average Accuracy (AA)")
row_labels.append("Overall Accuracy (OA)")
row_labels.append("Kappa Coefficient")
for i in range(CATEGORY):
row_labels.append(f"Class {i+1} Precision")
row_labels.append("Average Precision (AP)")
row_labels.append("Training Time (s)")
row_labels.append("Testing Time (s)")
# Create column headers
column_headers = ["Metric"]
for i in range(ITER):
column_headers.append(f"Iter {i+1}")
column_headers.append("Mean ± Std")
# Write to file with formatted output
with open(path1, 'w') as f:
# Write metadata at the top
if dataset_name:
f.write(f"Dataset: {dataset_name}\n")
if hyperparameters:
if isinstance(hyperparameters, dict):
for key, value in hyperparameters.items():
f.write(f"{key}: {value}\n")
else:
f.write(f"Hyperparameters: {hyperparameters}\n")
# Write separator after metadata
if dataset_name or hyperparameters:
f.write("=" * 80 + "\n\n")
# Write header
f.write("\t".join(column_headers) + "\n")
# Write section separators and data
for i in range(CATEGORY * 2 + 6):
row_data = [row_labels[i]]
for j in range(ITER + 1):
row_data.append(str(print_matrix[i, j]))
f.write("\t".join(row_data) + "\n")
# Add separator between sections
if i == CATEGORY - 1 or i == CATEGORY + 2 or i == CATEGORY * 2 + 2:
f.write("-" * 80 + "\n")
def outputStats(KAPPA_AE, OA_AE, AA_AE, ELEMENT_ACC_AE, TRAINING_TIME_AE, TESTING_TIME_AE, history, loss_and_metrics, CATEGORY, path1, path2):
f = open(path1, 'a')
sentence0 = 'KAPPAs, mean_KAPPA ± std_KAPPA for each iteration are:' + str(KAPPA_AE) + str(np.mean(KAPPA_AE)) + ' ± ' + str(np.std(KAPPA_AE)) + '\n'
f.write(sentence0)
sentence1 = 'OAs, mean_OA ± std_OA for each iteration are:' + str(OA_AE) + str(np.mean(OA_AE)) + ' ± ' + str(np.std(OA_AE)) + '\n'
f.write(sentence1)
sentence2 = 'AAs, mean_AA ± std_AA for each iteration are:' + str(AA_AE) + str(np.mean(AA_AE)) + ' ± ' + str(np.std(AA_AE)) + '\n'
f.write(sentence2)
sentence3 = 'Total average Training time is :' + str(np.sum(TRAINING_TIME_AE)) + '\n'
f.write(sentence3)
sentence4 = 'Total average Testing time is:' + str(np.sum(TESTING_TIME_AE)) + '\n'
f.write(sentence4)
element_mean = np.mean(ELEMENT_ACC_AE, axis=0)
element_std = np.std(ELEMENT_ACC_AE, axis=0)
sentence5 = "Mean of all elements in confusion matrix:" + str(np.mean(ELEMENT_ACC_AE, axis=0)) + '\n'
f.write(sentence5)
sentence6 = "Standard deviation of all elements in confusion matrix" + str(np.std(ELEMENT_ACC_AE, axis=0)) + '\n'
f.write(sentence6)
f.close()
print_matrix = np.zeros((CATEGORY), dtype=object)
for i in range(CATEGORY):
print_matrix[i] = str(element_mean[i]) + " ± " + str(element_std[i])
np.savetxt(path2, print_matrix.astype(str), fmt='%s', delimiter="\t",
newline='\n')
print('Test score:', loss_and_metrics[0])
print('Test accuracy:', loss_and_metrics[1])
print(history.history.keys())
def outputStats_assess(KAPPA_AE, OA_AE, AA_AE, ELEMENT_ACC_AE, CATEGORY, path1, path2):
f = open(path1, 'a')
sentence0 = 'KAPPAs, mean_KAPPA ± std_KAPPA for each iteration are:' + str(KAPPA_AE) + str(np.mean(KAPPA_AE)) + ' ± ' + str(np.std(KAPPA_AE)) + '\n'
f.write(sentence0)
sentence1 = 'OAs, mean_OA ± std_OA for each iteration are:' + str(OA_AE) + str(np.mean(OA_AE)) + ' ± ' + str(np.std(OA_AE)) + '\n'
f.write(sentence1)
sentence2 = 'AAs, mean_AA ± std_AA for each iteration are:' + str(AA_AE) + str(np.mean(AA_AE)) + ' ± ' + str(np.std(AA_AE)) + '\n'
f.write(sentence2)
element_mean = np.mean(ELEMENT_ACC_AE, axis=0)
element_std = np.std(ELEMENT_ACC_AE, axis=0)
sentence5 = "Mean of all elements in confusion matrix:" + str(np.mean(ELEMENT_ACC_AE, axis=0)) + '\n'
f.write(sentence5)
sentence6 = "Standard deviation of all elements in confusion matrix" + str(np.std(ELEMENT_ACC_AE, axis=0)) + '\n'
f.write(sentence6)
f.close()
print_matrix = np.zeros((CATEGORY), dtype=object)
for i in range(CATEGORY):
print_matrix[i] = str(element_mean[i]) + " ± " + str(element_std[i])
np.savetxt(path2, print_matrix.astype(str), fmt='%s', delimiter="\t",
newline='\n')
def outputStats_SVM(KAPPA_AE, OA_AE, AA_AE, ELEMENT_ACC_AE, TRAINING_TIME_AE, TESTING_TIME_AE, CATEGORY, path1, path2):
f = open(path1, 'a')
sentence0 = 'KAPPAs, mean_KAPPA ± std_KAPPA for each iteration are:' + str(KAPPA_AE) + str(np.mean(KAPPA_AE)) + ' ± ' + str(np.std(KAPPA_AE)) + '\n'
f.write(sentence0)
sentence1 = 'OAs, mean_OA ± std_OA for each iteration are:' + str(OA_AE) + str(np.mean(OA_AE)) + ' ± ' + str(np.std(OA_AE)) + '\n'
f.write(sentence1)
sentence2 = 'AAs, mean_AA ± std_AA for each iteration are:' + str(AA_AE) + str(np.mean(AA_AE)) + ' ± ' + str(np.std(AA_AE)) + '\n'
f.write(sentence2)
sentence3 = 'Total average Training time is :' + str(np.sum(TRAINING_TIME_AE)) + '\n'
f.write(sentence3)
sentence4 = 'Total average Testing time is:' + str(np.sum(TESTING_TIME_AE)) + '\n'
f.write(sentence4)
element_mean = np.mean(ELEMENT_ACC_AE, axis=0)
element_std = np.std(ELEMENT_ACC_AE, axis=0)
sentence5 = "Mean of all elements in confusion matrix:" + str(np.mean(ELEMENT_ACC_AE, axis=0)) + '\n'
f.write(sentence5)
sentence6 = "Standard deviation of all elements in confusion matrix" + str(np.std(ELEMENT_ACC_AE, axis=0)) + '\n'
f.write(sentence6)
f.close()
print_matrix = np.zeros((CATEGORY), dtype=object)
for i in range(CATEGORY):
print_matrix[i] = str(element_mean[i]) + " ± " + str(element_std[i])
np.savetxt(path2, print_matrix.astype(str), fmt='%s', delimiter="\t",
newline='\n')