import dash from dash import html, dcc import dash_bootstrap_components as dbc import plotly.graph_objects as go dash.register_page(__name__, path="/comparison", name="Model Comparison") # Results reproduced from Notebook 2 (2-Model_Comparison.ipynb), same data and # thresholding rule for all three methods. RESULTS = [ {"model": "Statistical baseline", "auc": 0.748, "precision": 0.033, "recall": 0.025, "f1": 0.029}, {"model": "Isolation Forest", "auc": 0.684, "precision": 0.007, "recall": 0.005, "f1": 0.006}, {"model": "Autoencoder", "auc": 0.801, "precision": 0.114, "recall": 0.095, "f1": 0.104}, ] DARK = dict(paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)", font=dict(color="#cfcfcf"), margin=dict(l=40, r=20, t=50, b=40)) def auc_bar(): models = [r["model"] for r in RESULTS] aucs = [r["auc"] for r in RESULTS] colors = ["#888780", "#888780", "#1d9e75"] fig = go.Figure(go.Bar(x=models, y=aucs, marker_color=colors, text=[f"{a:.3f}" for a in aucs], textposition="outside")) fig.add_hline(y=0.5, line_dash="dash", line_color="gray", annotation_text="random") fig.update_layout(title="ROC-AUC by method (higher is better)", yaxis=dict(range=[0, 1], title="ROC-AUC"), **DARK) return fig def metrics_grouped(): metrics = ["precision", "recall", "f1"] fig = go.Figure() palette = {"Statistical baseline": "#888780", "Isolation Forest": "#5f5e5a", "Autoencoder": "#1d9e75"} for r in RESULTS: fig.add_trace(go.Bar(name=r["model"], x=metrics, y=[r["precision"], r["recall"], r["f1"]], marker_color=palette[r["model"]])) fig.update_layout(barmode="group", title="Precision / Recall / F1", yaxis_title="score", **DARK) return fig layout = html.Div([ html.Div([ html.H1("Model Comparison", className="text-white fw-bold mb-2"), html.P("Three methods, identical data and thresholding. Why the autoencoder wins.", className="lead text-info"), ], className="mb-5"), dbc.Card(dbc.CardBody([ html.H4("The contenders", className="text-white mb-3"), dbc.Table([ html.Thead(html.Tr([html.Th("Method"), html.Th("Idea"), html.Th("Family")])), html.Tbody([ html.Tr([html.Td("Statistical baseline"), html.Td("Flag points far from each sensor's mean (max z-score)"), html.Td("Pure statistics")]), html.Tr([html.Td("Isolation Forest"), html.Td("Anomalies are isolated in fewer random splits"), html.Td("Classical ML")]), html.Tr([html.Td("Autoencoder"), html.Td("High reconstruction error means unfamiliar pattern"), html.Td("Deep learning")]), ]), ], bordered=False, hover=True, responsive=True, size="sm"), html.Small("All three are trained unsupervised on healthy data only. Fault labels are " "used purely for scoring.", className="text-muted"), ]), className="modern-card mb-4"), dbc.Row([ dbc.Col(dbc.Card(dbc.CardBody(dcc.Graph(figure=auc_bar())), className="modern-card"), md=6, className="mb-4"), dbc.Col(dbc.Card(dbc.CardBody(dcc.Graph(figure=metrics_grouped())), className="modern-card"), md=6, className="mb-4"), ]), dbc.Card(dbc.CardBody([ html.H3("Why the autoencoder was chosen", className="text-white mb-3"), html.Div([ html.Div([ html.I(className="bi bi-1-circle-fill text-success me-2"), html.B("Best ranking quality. ", className="text-white"), html.Span("Highest ROC-AUC, so it separates faulty from normal readings most " "reliably regardless of where the threshold sits.", className="text-light"), ], className="mb-3"), html.Div([ html.I(className="bi bi-2-circle-fill text-success me-2"), html.B("Catches subtle faults. ", className="text-white"), html.Span("The statistical baseline only sees single-sensor spikes. The intake " "fault keeps every sensor in range but breaks the MAP-MAF relationship - " "the autoencoder learns that joint structure and flags it.", className="text-light"), ], className="mb-3"), html.Div([ html.I(className="bi bi-3-circle-fill text-success me-2"), html.B("Deployable at the edge. ", className="text-white"), html.Span("At 168 parameters the trained model is a few kilobytes - small enough " "for an embedded controller.", className="text-light"), ], className="mb-3"), ]), html.Hr(style={"borderColor": "rgba(255,255,255,0.1)"}), html.P([ html.B("Layered in practice: "), "cheap statistics catch the obvious extreme faults, " "the autoencoder catches the subtle degradation. Isolation Forest remains a solid " "simpler fallback." ], className="text-muted mb-0"), ]), className="modern-card mb-4"), ])