-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
401 lines (336 loc) · 12.6 KB
/
streamlit_app.py
File metadata and controls
401 lines (336 loc) · 12.6 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import json
from pathlib import Path
import pandas as pd
import streamlit as st
ROOT = Path(__file__).resolve().parent
RESULTS_DIR = ROOT / "experiments" / "results"
TRAINING_DIR = ROOT / "training_data"
@st.cache_data(show_spinner=False)
def load_json(path: Path) -> dict:
if not path.exists():
return {}
with path.open("r", encoding="utf-8") as f:
return json.load(f)
@st.cache_data(show_spinner=False)
def load_relevance_df(payload: dict) -> pd.DataFrame:
rows = payload.get("metrics", [])
if not rows:
return pd.DataFrame()
wanted = [
"model",
"accuracy",
"f1",
"precision",
"recall",
"pr_auc",
"brier",
"tn",
"fp",
"fn",
"tp",
]
available = [c for c in wanted if c in rows[0]]
df = pd.DataFrame(rows)[available].copy()
rename_map = {
"model": "Model",
"accuracy": "Accuracy",
"f1": "F1",
"precision": "Precision",
"recall": "Recall",
"pr_auc": "PR-AUC",
"brier": "Brier",
"tn": "TN",
"fp": "FP",
"fn": "FN",
"tp": "TP",
}
df = df.rename(columns=rename_map)
return df
@st.cache_data(show_spinner=False)
def load_threshold_df(payload: dict) -> pd.DataFrame:
rows = payload.get("xgboost_confidence_thresholds", [])
if not rows:
return pd.DataFrame()
df = pd.DataFrame(rows)
df = df.rename(
columns={
"threshold": "Threshold",
"coverage": "Coverage",
"f1": "F1",
"precision": "Precision",
"recall": "Recall",
}
)
return df
@st.cache_data(show_spinner=False)
def load_per_dataset_confusion(payload: dict) -> pd.DataFrame:
block = payload.get("xgboost_per_dataset_confusion", {})
if not block:
return pd.DataFrame()
rows = []
for name, metrics in block.items():
rows.append(
{
"Dataset": name,
"Rows": metrics.get("rows"),
"TN": metrics.get("tn"),
"FP": metrics.get("fp"),
"FN": metrics.get("fn"),
"TP": metrics.get("tp"),
}
)
return pd.DataFrame(rows)
@st.cache_data(show_spinner=False)
def load_shift_df(payload: dict) -> pd.DataFrame:
rows = payload.get("results", [])
if not rows:
return pd.DataFrame()
df = pd.DataFrame(rows)
keep = [
"mode",
"train_dataset",
"eval_dataset",
"status",
"rows",
"accuracy",
"f1",
"precision",
"recall",
]
keep = [c for c in keep if c in df.columns]
df = df[keep].copy()
df = df.rename(
columns={
"mode": "Mode",
"train_dataset": "Train",
"eval_dataset": "Eval",
"status": "Status",
"rows": "Rows",
"accuracy": "Accuracy",
"f1": "F1",
"precision": "Precision",
"recall": "Recall",
}
)
return df
def format_metric(x):
if x is None:
return "-"
if isinstance(x, (int, float)):
return f"{x:.4f}"
return str(x)
def find_model(df: pd.DataFrame, names: list[str]) -> pd.Series | None:
for name in names:
match = df[df["Model"] == name]
if not match.empty:
return match.iloc[0]
return None
def render_header():
st.set_page_config(
page_title="Hybrid SQL/Graph Query Routing System - Results Dashboard",
layout="wide",
)
st.title("Hybrid SQL/Graph Query Routing System")
st.caption(
"HiFUN-inspired router: read-only evaluation artifacts aligned with the latest project report."
)
def render_overview_tab(relevance_payload: dict, quality_payload: dict):
st.subheader("Dataset Engineering and Quality Gates")
c1, c2, c3, c4 = st.columns(4)
c1.metric("Train rows", str(relevance_payload.get("train_rows", "-")))
c2.metric("Eval rows", str(relevance_payload.get("eval_rows", "-")))
train_dist = relevance_payload.get("train_label_distribution", {})
eval_dist = relevance_payload.get("eval_label_distribution", {})
c3.metric("Train label mix", f"SQL {train_dist.get('SQL', '-')}, GRAPH {train_dist.get('GRAPH', '-')}")
c4.metric("Eval label mix", f"SQL {eval_dist.get('SQL', '-')}, GRAPH {eval_dist.get('GRAPH', '-')}")
st.markdown("### System in one line")
st.info(
"DSL -> Parser/Validator -> DAG Decomposer -> 22-feature extractor -> Router (Rule/ML) -> SQL or Graph execution -> Result composer"
)
summary = quality_payload.get("summary", {})
checks = quality_payload.get("checks", [])
passed = sum(1 for c in checks if c.get("passed"))
total = len(checks)
q1, q2, q3 = st.columns(3)
q1.metric("Quality gates", f"{passed}/{total}" if total else "-")
q2.metric("Graph ratio", format_metric(summary.get("graph_ratio")) if summary else "-")
q3.metric("Real measurement share", "1.0000" if checks else "-")
with st.expander("Show quality gate details"):
if checks:
qdf = pd.DataFrame(checks)
if "passed" in qdf.columns:
qdf["passed"] = qdf["passed"].map(lambda x: "PASS" if x else "FAIL")
st.dataframe(qdf[[c for c in ["name", "passed", "detail"] if c in qdf.columns]], use_container_width=True)
else:
st.info("No data quality report found.")
def render_metrics_tab(relevance_payload: dict, compact_view: bool):
st.subheader("Relevance Evaluation")
rdf = load_relevance_df(relevance_payload)
if rdf.empty:
st.warning("No strict relevance metrics found.")
return
top_row = rdf.sort_values(["F1", "PR-AUC"], ascending=False).iloc[0]
rule_row = find_model(rdf, ["TraversalRule", "Trivial Rule"])
xgb_row = find_model(rdf, ["XGBoostBalanced", "XGBoost", "Learned ML (DT)"])
c1, c2, c3, c4 = st.columns(4)
c1.metric("Top model", str(top_row["Model"]))
c2.metric("Top F1", format_metric(top_row.get("F1")))
c3.metric("Top PR-AUC", format_metric(top_row.get("PR-AUC")))
if rule_row is not None and xgb_row is not None:
delta = float(xgb_row.get("F1", 0.0)) - float(rule_row.get("F1", 0.0))
c4.metric("XGBoost - Rule (F1)", f"{delta:+.4f}")
else:
c4.metric("XGBoost - Rule (F1)", "-")
st.markdown("### Interpretation note")
st.info(
"This panel reports model comparison from saved strict evaluation artifacts. Use raw tables below for verification."
)
chart_df = rdf[["Model", "F1"]].set_index("Model").sort_values("F1", ascending=False)
st.bar_chart(chart_df)
show_cols = [c for c in ["Model", "Accuracy", "F1", "Precision", "Recall", "PR-AUC", "Brier"] if c in rdf.columns]
st.dataframe(rdf[show_cols], use_container_width=True)
st.markdown("### XGBoost per-dataset confusion")
cdf = load_per_dataset_confusion(relevance_payload)
if cdf.empty:
st.info("Per-dataset confusion block unavailable.")
else:
st.dataframe(cdf, use_container_width=True)
if not compact_view:
st.markdown("### Confidence-threshold stability")
tdf = load_threshold_df(relevance_payload)
if tdf.empty:
st.info("Threshold stability block unavailable.")
else:
st.dataframe(tdf, use_container_width=True)
def render_robustness_tab(robust_payload: dict, compact_view: bool):
st.subheader("Strict Robustness Evaluation")
core_f1 = robust_payload.get("xgboost_eval_f1")
bootstrap = robust_payload.get("bootstrap_95_ci", {})
perm = robust_payload.get("permutation_sanity", {})
c1, c2, c3 = st.columns(3)
c1.metric("Eval F1", format_metric(core_f1))
c2.metric(
"Bootstrap F1 CI",
f"[{format_metric(bootstrap.get('f1_low'))}, {format_metric(bootstrap.get('f1_high'))}]",
)
c3.metric(
"Permuted mean F1",
format_metric(perm.get("mean_f1")),
)
details = []
if bootstrap:
details.append(
{
"Measure": "Bootstrap F1 median",
"Value": bootstrap.get("f1_median"),
}
)
details.append(
{
"Measure": "Bootstrap precision CI",
"Value": f"[{format_metric(bootstrap.get('precision_low'))}, {format_metric(bootstrap.get('precision_high'))}]",
}
)
details.append(
{
"Measure": "Bootstrap recall CI",
"Value": f"[{format_metric(bootstrap.get('recall_low'))}, {format_metric(bootstrap.get('recall_high'))}]",
}
)
if perm:
details.append(
{
"Measure": "Permuted std",
"Value": perm.get("std_f1"),
}
)
details.append(
{
"Measure": "Permuted range",
"Value": f"[{format_metric(perm.get('min_f1'))}, {format_metric(perm.get('max_f1'))}]",
}
)
if details:
ddf = pd.DataFrame(details)
st.dataframe(ddf, use_container_width=True)
if not compact_view:
imp = robust_payload.get("permutation_importance", [])
if imp:
idf = pd.DataFrame(imp)
if "mean_drop" in idf.columns and "feature" in idf.columns:
st.markdown("### Top permutation-importance features")
idf = idf.sort_values("mean_drop", ascending=False)
st.dataframe(idf.head(10), use_container_width=True)
def render_shift_tab(shift_payload: dict, compact_view: bool):
st.subheader("Cross-Dataset Generalization")
sdf = load_shift_df(shift_payload)
if sdf.empty:
st.warning("No dataset shift results found.")
return
ok_df = sdf[sdf["Status"] == "ok"] if "Status" in sdf.columns else sdf
c1, c2, c3 = st.columns(3)
c1.metric("Transfer runs", str(len(ok_df)))
if not ok_df.empty:
c2.metric("Best transfer F1", format_metric(ok_df["F1"].max()))
c3.metric("Worst transfer F1", format_metric(ok_df["F1"].min()))
view_cols = [c for c in ["Train", "Eval", "Rows", "Accuracy", "F1", "Precision", "Recall"] if c in sdf.columns]
st.dataframe(sdf[view_cols], use_container_width=True)
if not ok_df.empty and not compact_view:
chart_cols = ["Train", "Eval", "F1"]
if all(c in ok_df.columns for c in chart_cols):
tmp = ok_df[chart_cols].copy()
tmp["Pair"] = tmp["Train"] + " -> " + tmp["Eval"]
st.markdown("### Transfer F1 by train/eval pair")
st.bar_chart(tmp.set_index("Pair")["F1"])
def main():
render_header()
with st.sidebar:
st.header("Run Profile")
profile = st.radio(
"Select artifact set",
["strict", "fast"],
index=1,
help="Use fast profile for quick reruns with reduced robustness settings.",
)
compact_view = st.toggle(
"Compact view",
value=True,
help="Hide secondary tables/charts and keep core metrics visible.",
)
st.caption("Dashboard is read-only: metrics are loaded from generated result files.")
relevance_file = "relevance_eval_strict_runtime.json"
robust_file = "strict_robustness_eval_runtime.json"
shift_file = "dataset_shift_eval_strict_runtime.json"
if profile == "fast":
# Relevance currently has strict runtime output only; robustness/shift have fast variants.
robust_file = "strict_robustness_eval_fast_runtime.json"
shift_file = "dataset_shift_eval_fast_runtime.json"
relevance_payload = load_json(RESULTS_DIR / relevance_file)
robust_payload = load_json(RESULTS_DIR / robust_file)
shift_payload = load_json(RESULTS_DIR / shift_file)
quality_payload = load_json(TRAINING_DIR / "dataset_quality_report_strict_curated.json")
tabs = st.tabs(
[
"Dataset and Quality",
"Relevance Evaluation",
"Robustness Evaluation",
"Cross-Dataset Generalization",
]
)
with tabs[0]:
render_overview_tab(relevance_payload, quality_payload)
with tabs[1]:
render_metrics_tab(relevance_payload, compact_view)
with tabs[2]:
render_robustness_tab(robust_payload, compact_view)
with tabs[3]:
render_shift_tab(shift_payload, compact_view)
with st.sidebar:
st.header("Artifacts")
st.write("Loaded from:")
st.code(str(RESULTS_DIR / relevance_file))
st.code(str(RESULTS_DIR / robust_file))
st.code(str(RESULTS_DIR / shift_file))
st.code(str(TRAINING_DIR / "dataset_quality_report_strict_curated.json"))
if __name__ == "__main__":
main()