ClarusC64 commited on
Commit
e7a6bab
·
verified ·
1 Parent(s): 1f5e033

Create stability_visualizer.py

Browse files
Files changed (1) hide show
  1. stability_visualizer.py +134 -0
stability_visualizer.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import argparse
3
+ import matplotlib.pyplot as plt
4
+ from matplotlib.lines import Line2D
5
+
6
+ K = 1.0
7
+ BOUNDARY_EPS = 0.02
8
+
9
+
10
+ def load_csv(path):
11
+ with open(path, newline="", encoding="utf-8") as f:
12
+ return list(csv.DictReader(f))
13
+
14
+
15
+ def index_by_id(rows):
16
+ index = {}
17
+ for row in rows:
18
+ sid = str(row.get("scenario_id", "")).strip()
19
+ if not sid:
20
+ raise ValueError("Blank or missing scenario_id detected")
21
+ if sid in index:
22
+ raise ValueError(f"Duplicate scenario_id detected: {sid}")
23
+ index[sid] = row
24
+ return index
25
+
26
+
27
+ def parse_binary(value, field_name, sid):
28
+ try:
29
+ parsed = int(value)
30
+ except ValueError as e:
31
+ raise ValueError(f"Invalid {field_name} for {sid}: {value}") from e
32
+ if parsed not in (0, 1):
33
+ raise ValueError(f"Invalid {field_name} for {sid}: {parsed}")
34
+ return parsed
35
+
36
+
37
+ def compute_surfaces(pressure, buffer, lag, coupling):
38
+ s1 = buffer - (pressure * coupling) - (K * lag)
39
+ s2 = buffer - (pressure * (coupling ** 2)) - (K * lag)
40
+ s3 = buffer - (pressure * coupling) - (K * (lag ** 2))
41
+ return {
42
+ "baseline_surface": s1,
43
+ "coupling_surface": s2,
44
+ "lag_surface": s3,
45
+ }
46
+
47
+
48
+ def plot_projection(pred_path, truth_path):
49
+ preds = load_csv(pred_path)
50
+ truth = load_csv(truth_path)
51
+
52
+ pred_map = index_by_id(preds)
53
+ truth_map = index_by_id(truth)
54
+
55
+ if set(pred_map.keys()) != set(truth_map.keys()):
56
+ raise ValueError("scenario_id mismatch between prediction and truth files")
57
+
58
+ plt.figure(figsize=(10, 8))
59
+
60
+ for sid in sorted(truth_map):
61
+ truth_row = truth_map[sid]
62
+ pred_row = pred_map[sid]
63
+
64
+ pressure = float(truth_row["pressure"])
65
+ buffer = float(truth_row["buffer"])
66
+ lag = float(truth_row["lag"])
67
+ coupling = float(truth_row["coupling"])
68
+
69
+ pred = parse_binary(pred_row["prediction"], "prediction", sid)
70
+ label = parse_binary(truth_row["label_stable"], "label_stable", sid)
71
+
72
+ surfaces = compute_surfaces(pressure, buffer, lag, coupling)
73
+ manifold_margin = min(surfaces.values())
74
+ active_surface = min(surfaces, key=surfaces.get)
75
+
76
+ x = pressure * coupling
77
+ y = buffer
78
+
79
+ if abs(manifold_margin) <= BOUNDARY_EPS:
80
+ color = "orange"
81
+ marker = "s"
82
+ elif manifold_margin < 0:
83
+ color = "red"
84
+ marker = "x"
85
+ else:
86
+ color = "green"
87
+ marker = "o"
88
+
89
+ if label == 0 and pred == 1:
90
+ color = "purple"
91
+ marker = "*"
92
+ elif label == 1 and pred == 0:
93
+ color = "blue"
94
+ marker = "D"
95
+
96
+ size = 110
97
+ edgecolor = "black" if active_surface == "coupling_surface" else "none"
98
+ linewidth = 1.2 if active_surface == "coupling_surface" else 0.0
99
+
100
+ plt.scatter(
101
+ x,
102
+ y,
103
+ c=color,
104
+ marker=marker,
105
+ s=size,
106
+ edgecolors=edgecolor,
107
+ linewidths=linewidth,
108
+ )
109
+
110
+ plt.xlabel("Pressure × Coupling")
111
+ plt.ylabel("Buffer Capacity")
112
+ plt.title("Clarus Stability Manifold Projection (2D View)")
113
+ plt.grid(True)
114
+ plt.tight_layout()
115
+
116
+ legend_elements = [
117
+ Line2D([0], [0], marker="o", color="w", label="Stable region", markerfacecolor="green", markersize=10),
118
+ Line2D([0], [0], marker="x", color="red", label="Collapse region", linestyle="None", markersize=10),
119
+ Line2D([0], [0], marker="s", color="w", label="Near boundary", markerfacecolor="orange", markersize=10),
120
+ Line2D([0], [0], marker="*", color="w", label="False rescue", markerfacecolor="purple", markersize=12),
121
+ Line2D([0], [0], marker="D", color="w", label="False collapse", markerfacecolor="blue", markersize=10),
122
+ Line2D([0], [0], marker="o", color="black", label="Coupling surface active", markerfacecolor="white", markersize=9),
123
+ ]
124
+ plt.legend(handles=legend_elements)
125
+ plt.show()
126
+
127
+
128
+ if __name__ == "__main__":
129
+ parser = argparse.ArgumentParser()
130
+ parser.add_argument("--pred", required=True)
131
+ parser.add_argument("--truth", required=True)
132
+ args = parser.parse_args()
133
+
134
+ plot_projection(args.pred, args.truth)