Repository navigation
Expand file tree
/
Copy pathrun_test.py
More file actions
144 lines (116 loc) · 5.21 KB
/
Copy pathrun_test.py
File metadata and controls
144 lines (116 loc) · 5.21 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
import joblib
import pandas as pd
import numpy as np
from pathlib import Path
from omegaconf import OmegaConf
from load_preprocess import apply_preprocessing
from sklearn.metrics import (
accuracy_score,
confusion_matrix,
f1_score,
)
import seaborn as sns
import matplotlib.pyplot as plt
import csv
def main():
outputs_dir = Path("outputs")
test_csv_rows = []
for run_dir in outputs_dir.iterdir():
if not run_dir.is_dir():
continue
model_path = run_dir / "model.joblib"
if not model_path.exists():
continue
print(f"Processing: {run_dir.name}")
try:
cfg = OmegaConf.load(run_dir / ".hydra" / "config.yaml")
model = joblib.load(model_path)
le = joblib.load(run_dir / "label_encoder.joblib")
scaler_path = run_dir / "scaler.joblib"
scaler = joblib.load(scaler_path) if scaler_path.exists() else None
imputer_path = run_dir / "imputer.joblib"
imputer = joblib.load(imputer_path) if imputer_path.exists() else None
if "5c" in run_dir.name:
df_test = pd.read_csv("datasets_new/dbnfs_5c_test.csv", low_memory=False)
df_unc = None
elif "4c" in run_dir.name:
df_test = pd.read_csv("datasets_new/dbnfs_4c_test.csv", low_memory=False)
df_unc = pd.read_csv("datasets_new/dbnfs_uncertain.csv")
elif "3c" in run_dir.name:
df_test = pd.read_csv("datasets_new/dbnfs_3c_test.csv", low_memory=False)
df_unc = None
elif "2c" in run_dir.name:
df_test = pd.read_csv("datasets_new/dbnfs_2c_test.csv", low_memory=False)
df_unc = pd.read_csv("datasets_new/dbnfs_uncertain.csv")
target_col = cfg.dataset.target_column
y_test = le.transform(df_test[target_col])
X = df_test.drop(columns=[target_col])
X_processed = apply_preprocessing(X, cfg)
if imputer:
numeric_cols = X_processed.select_dtypes(include=["number"]).columns
X_processed[numeric_cols] = imputer.transform(X_processed[numeric_cols])
if scaler:
X_scaled = scaler.transform(X_processed)
X_processed = pd.DataFrame(X_scaled, columns=X_processed.columns, index=X_processed.index)
y_pred = model.predict(X_processed)
accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred, average="macro")
cm_path = Path(run_dir / "test_confusion_matrix.png")
cm = confusion_matrix(y_test, y_pred)
class_names = le.classes_.astype(str)
plt.figure(figsize=(10, 8))
sns.heatmap(
cm,
annot=True,
fmt="g", # Use integer formatting for annotations
cmap="Blues",
xticklabels=class_names,
yticklabels=class_names,
)
plt.xlabel("Predicted Labels")
plt.ylabel("True Labels")
plt.title("Confusion Matrix")
plt.savefig(cm_path)
plt.close()
dataset = cfg.dataset.name
ds_parts = dataset.split("_")
ds_size = int(ds_parts[-1])
ds_classes = ds_parts[1]
test_csv_rows.append({
"model": cfg.model.name,
"DS": ds_classes,
"size": ds_size,
"preprocessing": cfg.preprocessing.name,
"F1": f1,
"Accuracy": accuracy
})
if df_unc is not None:
print(f"Testing {run_dir.name} on uncertains")
target_col = cfg.dataset.target_column
X = df_unc.drop(columns=[target_col])
X_processed = apply_preprocessing(X, cfg)
if imputer:
numeric_cols = X_processed.select_dtypes(include=["number"]).columns
X_processed[numeric_cols] = imputer.transform(X_processed[numeric_cols])
if scaler:
X_scaled = scaler.transform(X_processed)
X_processed = pd.DataFrame(X_scaled, columns=X_processed.columns, index=X_processed.index)
preds = model.predict(X_processed)
probs = model.predict_proba(X_processed)
results = pd.DataFrame({
"original_index": df_unc.index,
"predicted_label": le.inverse_transform(preds),
"confidence": np.max(probs, axis=1)
})
output_file = run_dir / "uncertain_predictions.csv"
results.to_csv(output_file, index=False)
except Exception as e:
print(f" -> Error: {e}")
fieldnames = ["model", "DS", "size", "preprocessing", "F1", "Accuracy"]
with open("experiments/test_results.csv", "w", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(test_csv_rows)
print("Wrote test results to experiments/test_results.csv")
if __name__ == "__main__":
main()