-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
168 lines (142 loc) · 5.64 KB
/
main.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
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
from pathlib import Path
import re
import shutil
# import subprocess
import sys
import numpy as np
from constants import TEMP_PATH
def cleanTempPath():
BAK_PATH = TEMP_PATH / "bak"
if BAK_PATH.exists():
shutil.rmtree(BAK_PATH)
pass
BAK_PATH.mkdir(parents=True, exist_ok=True)
for item in TEMP_PATH.iterdir():
if item != BAK_PATH:
shutil.move(str(item), str(BAK_PATH))
print(f"All files and folders in '{TEMP_PATH}' have been moved to '{BAK_PATH}'.")
def run(modelNames: list[str] | None = None):
from notebook_wrapper import NotebookWrapper
from constants import ARCHIVED_NOTEBOOKS_PATH
HOWS = ["first", "last", "avg", "max", "min", "med"]
def _getModels():
pattern = re.compile(r"tabular_model_(.*?)_template\.ipynb")
for file in ARCHIVED_NOTEBOOKS_PATH.iterdir():
if file.is_file():
modelName = pattern.match(file.name)
if modelName:
if modelNames is None or modelName.group(1) in modelNames:
yield modelName.group(1)
for modelName in _getModels():
mlNb = NotebookWrapper(
ARCHIVED_NOTEBOOKS_PATH / f"tabular_model_{modelName}_template.ipynb",
["how"],
[
"auc_score_list",
"accuracy_score_list",
"precision_score_list",
"recall_score_list",
"auc_score_list_knn",
"accuracy_score_list_knn",
"precision_score_list_knn",
"recall_score_list_knn",
"auc_score_list_val",
"accuracy_score_list_val",
"precision_score_list_val",
"recall_score_list_val",
"auc_score_list_val_knn",
"accuracy_score_list_val_knn",
"precision_score_list_val_knn",
"recall_score_list_val_knn",
],
allowError=True,
nbContext=Path(__file__).parent,
)
for how in HOWS:
(
auc_score_list,
accuracy_score_list,
precision_score_list,
recall_score_list,
auc_score_list_knn,
accuracy_score_list_knn,
precision_score_list_knn,
recall_score_list_knn,
auc_score_list_val,
accuracy_score_list_val,
precision_score_list_val,
recall_score_list_val,
auc_score_list_val_knn,
accuracy_score_list_val_knn,
precision_score_list_val_knn,
recall_score_list_val_knn,
) = mlNb.export(
ARCHIVED_NOTEBOOKS_PATH / modelName / f"tabular-model_{modelName}_{how}.ipynb",
how=how,
)
def calculate_mean_and_error(array):
mean = np.mean(array)
standard_error = np.std(array) / np.sqrt(len(array))
return mean, standard_error
print(f"Model: {modelName}, How: {how}")
print("==== RAW ====")
print(f"AUC: {calculate_mean_and_error(auc_score_list)}")
print(f"Accuracy: {np.mean(accuracy_score_list)}")
print(f"Precision: {np.mean(precision_score_list)}")
print(f"Recall: {np.mean(recall_score_list)}")
print("==== KNN ====")
print(f"AUC: {calculate_mean_and_error(auc_score_list_knn)}")
print(f"Accuracy: {np.mean(accuracy_score_list_knn)}")
print(f"Precision: {np.mean(precision_score_list_knn)}")
print(f"Recall: {np.mean(recall_score_list_knn)}")
print("==== WITH VALIDATE ====")
print(f"AUC: {calculate_mean_and_error(auc_score_list_val)}")
print(f"Accuracy: {np.mean(accuracy_score_list_val)}")
print(f"Precision: {np.mean(precision_score_list_val)}")
print(f"Recall: {np.mean(recall_score_list_val)}")
print("==== KNN WITH VALIDATE ====")
print(f"AUC: {calculate_mean_and_error(auc_score_list_val_knn)}")
print(f"Accuracy: {np.mean(accuracy_score_list_val_knn)}")
print(f"Precision: {np.mean(precision_score_list_val_knn)}")
print(f"Recall: {np.mean(recall_score_list_val_knn)}")
print("=============================")
pass
def runGui():
import utils.gui
if __name__ == "__main__":
if any("run" in argv for argv in sys.argv):
paramId = sys.argv.index("run")
if len(sys.argv) > paramId + 1:
mode = sys.argv[paramId + 1]
if mode == "--gui":
runGui()
exit()
if any("clean" in argv for argv in sys.argv):
cleanTempPath()
pass
if "train" in sys.argv:
paramId = sys.argv.index("train")
if len(sys.argv) > paramId + 1:
modelNames = sys.argv[paramId + 1:]
run(modelNames)
pass
else:
run()
pass
if "--copy-tabular-template" in sys.argv or "-ctt" in sys.argv:
paramId = (
sys.argv.index("--copy-tabular-template")
if "--copy-tabular-template" in sys.argv
else sys.argv.index("-ctt")
)
if len(sys.argv) > paramId + 1:
modelName = sys.argv[paramId + 1]
from constants import ARCHIVED_NOTEBOOKS_PATH
shutil.copy(
"./machine_learning.ipynb",
ARCHIVED_NOTEBOOKS_PATH / f"tabular_model_{modelName}_template.ipynb",
)
pass
else:
print("Please provide the model name.")
pass