-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostic_validation.py
More file actions
214 lines (181 loc) · 6.02 KB
/
diagnostic_validation.py
File metadata and controls
214 lines (181 loc) · 6.02 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import json
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import os
# --------------------
# Paths
# --------------------
INPUT_DIR = "results"
OUTPUT_DIR = os.path.join(INPUT_DIR, "diagnostic")
os.makedirs(OUTPUT_DIR, exist_ok=True)
RESULT_FILES = {
"T1": "results_T1.json",
"T2": "results_T2.json",
"T3": "results_T3.json",
}
THRESHOLDS_FILE = "thresholds_t0_nadd3_pctl99.json"
THRESHOLD_OUTPUT_FILE = os.path.join(OUTPUT_DIR, "diagnostic_thresholds.json")
# --------------------
# Load rupture thresholds (per-dimension)
# --------------------
with open(os.path.join(INPUT_DIR, THRESHOLDS_FILE), "r") as f:
THRESHOLDS = json.load(f)
metrics = ['l2'] + [f'proj{i}' for i in range(10)]
distance_types = ['energy', 'w1']
# --------------------
# Structural metrics
# --------------------
def compute_R(run_results):
R = []
for metric in metrics:
for dist in distance_types:
observed = run_results[metric][dist]
tau = THRESHOLDS[metric][f"tau_{dist}"]
R.append(1 if observed > tau else 0)
return np.array(R)
def density(R):
return np.mean(R)
def stability(R_now, R_prev):
inter = np.sum(np.logical_and(R_now, R_prev))
union = np.sum(np.logical_or(R_now, R_prev))
return inter / union if union > 0 else 0
# --------------------
# Load one scenario
# --------------------
def load_case(case_file):
with open(os.path.join(INPUT_DIR, case_file), "r") as f:
data = json.load(f)
runs = []
for t in range(1, 6):
R_t = compute_R(data["runs"][str(t)]["results"])
runs.append({
"R": R_t,
"delta": density(R_t)
})
return runs
# --------------------
# Build diagnostic summary
# --------------------
def build_summary(runs, max_T=5):
summary = {"density": [], "persistence": [], "stability": []}
for T in range(1, max_T + 1):
deltas = [runs[i]["delta"] for i in range(T)]
summary["density"].append(deltas[-1])
summary["persistence"].append(np.mean(deltas))
if T > 1:
summary["stability"].append(
stability(runs[T-1]["R"], runs[T-2]["R"])
)
else:
summary["stability"].append(None) # estabilidad no definida en T=1
return summary
# --------------------
# Plotting 2D with thresholds
# --------------------
def plot_2d(metric_data, ylabel, filename, threshold=None, threshold_label=None):
plt.figure(figsize=(6, 4))
CASE_COLORS = {
"T1": "green", # benigno
"T2": "red", # ataque fuerte
"T3": "orange" # ataque sigiloso
}
for case, values in metric_data.items():
plt.plot(
range(1, 6),
values,
label=case,
color=CASE_COLORS.get(case)
)
if threshold is not None:
plt.axhline(
y=threshold,
color="gray",
linestyle="dashed",
linewidth=1.2,
label=threshold_label
)
plt.xticks(range(1, 6), fontsize=12)
plt.yticks(fontsize=12)
plt.xlabel("T", fontsize=14)
plt.ylabel(ylabel, fontsize=14)
plt.legend(fontsize=12)
plt.grid(True)
plt.tight_layout()
plt.savefig(filename)
plt.close()
# --------------------
# Plotting 3D (no thresholds)
# --------------------
def plot_3d(summary_data, filename):
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
colors = {"T1": "green", "T2": "red", "T3": "orange"}
for case, summary in summary_data.items():
xs, ys, zs = [], [], []
for d, p, s in zip(
summary["density"],
summary["persistence"],
summary["stability"]
):
if s is not None:
xs.append(d)
ys.append(p)
zs.append(s)
ax.plot(xs, ys, zs, marker='o', label=case, color=colors[case])
ax.set_xlabel("δ(T)", fontsize=12)
ax.set_ylabel("π(T)", fontsize=12)
ax.set_zlabel("S(T)", fontsize=12)
ax.legend()
plt.tight_layout()
plt.savefig(filename)
plt.close()
# --------------------
# Main
# --------------------
if __name__ == "__main__":
summaries = {}
# Build summaries
for case, file in RESULT_FILES.items():
runs = load_case(file)
summaries[case] = build_summary(runs)
# --------------------
# Save diagnostic summary
# --------------------
with open(os.path.join(OUTPUT_DIR, "diagnostic_summary.json"), "w") as f:
json.dump(summaries, f, indent=2)
# --------------------
# Compute diagnostic thresholds from T1 (percentile 95)
# --------------------
T1 = summaries["T1"]
stability_vals = [s for s in T1["stability"] if s is not None]
diagnostic_thresholds = {
"density_threshold": float(np.percentile(T1["density"], 95)),
"persistence_threshold": float(np.percentile(T1["persistence"], 95)),
"stability_threshold": float(np.percentile(stability_vals, 95))
}
with open(THRESHOLD_OUTPUT_FILE, "w") as f_out:
json.dump(diagnostic_thresholds, f_out, indent=2)
# --------------------
# Plot 2D metrics with thresholds
# --------------------
labels = {
"density": r"$\tau_\delta$",
"persistence": r"$\tau_\pi$",
"stability": r"$\tau_S$"
}
for metric in ["density", "persistence", "stability"]:
plot_2d(
metric_data={c: summaries[c][metric] for c in summaries},
ylabel=f"${metric[0]}(T)$",
filename=os.path.join(OUTPUT_DIR, f"{metric}_plot.pdf"),
threshold=diagnostic_thresholds[f"{metric}_threshold"],
threshold_label=labels[metric]
)
# --------------------
# Plot 3D structural space
# --------------------
plot_3d(
summaries,
os.path.join(OUTPUT_DIR, "structural_space_3d.pdf")
)