import os import dash from dash import html, dcc import dash_bootstrap_components as dbc import pandas as pd import plotly.graph_objects as go import plotly.express as px dash.register_page(__name__, path="/data", name="The Data") PAGE_DIR = os.path.dirname(os.path.abspath(__file__)) healthy = pd.read_csv(os.path.join(PAGE_DIR, "sample_healthy.csv")) faults = pd.read_csv(os.path.join(PAGE_DIR, "sample_faults.csv")) FEATURES = ["coolant_temp", "manifold_pressure", "rpm", "speed", "intake_temp", "mass_air_flow", "throttle_pos", "ambient_temp", "accel_pedal"] 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=40, b=40)) def correlation_fig(): corr = healthy[FEATURES].corr() fig = px.imshow(corr, text_auto=".2f", color_continuous_scale="RdBu_r", zmin=-1, zmax=1, aspect="auto") fig.update_layout(title="Sensor correlation (healthy driving)", **DARK) return fig def scatter_fig(): fig = go.Figure() fig.add_trace(go.Scatter( x=healthy["manifold_pressure"], y=healthy["mass_air_flow"], mode="markers", marker=dict(size=4, color="#1d9e75", opacity=0.4), name="healthy")) fig.update_layout(title="MAP vs MAF - the intake-health relationship", xaxis_title="manifold_pressure (kPa)", yaxis_title="mass_air_flow (g/s)", **DARK) return fig def coolant_timeline(): fig = go.Figure() fig.add_trace(go.Scatter(y=faults["coolant_temp"].values, mode="lines", line=dict(color="#e24b4a", width=1), name="coolant_temp")) fig.add_hrect(y0=85, y1=100, fillcolor="green", opacity=0.12, line_width=0, annotation_text="normal band") fig.add_vrect(x0=300, x1=500, fillcolor="red", opacity=0.10, line_width=0, annotation_text="injected fault") fig.update_layout(title="Fault 1 - coolant temperature spike", xaxis_title="row offset", yaxis_title="coolant_temp (C)", **DARK) return fig def intake_timeline(): fig = go.Figure() fig.add_trace(go.Scatter(y=faults["manifold_pressure"].values, mode="lines", line=dict(color="#993c1d", width=1), name="manifold_pressure")) fig.add_trace(go.Scatter(y=faults["mass_air_flow"].values, mode="lines", line=dict(color="#378add", width=1), name="mass_air_flow", yaxis="y2")) fig.add_vrect(x0=1600, x1=1800, fillcolor="red", opacity=0.10, line_width=0, annotation_text="injected fault") fig.update_layout(title="Fault 2 - MAP collapses while MAF stays high", xaxis_title="row offset", yaxis=dict(title="manifold_pressure (kPa)"), yaxis2=dict(title="mass_air_flow (g/s)", overlaying="y", side="right"), **DARK) return fig layout = html.Div([ html.Div([ html.H1("The Data", className="text-white fw-bold mb-2"), html.P("Real OBD-II logs from a 2017 Seat Leon - 30,807 readings across 9 sensors.", className="lead text-info"), ], className="mb-5"), dbc.Card(dbc.CardBody([ html.H4("Sensor reference", className="text-white mb-3"), dbc.Table([ html.Thead(html.Tr([html.Th("Sensor"), html.Th("Unit"), html.Th("Meaning")])), html.Tbody([ html.Tr([html.Td("coolant_temp"), html.Td("C"), html.Td("Engine coolant temperature")]), html.Tr([html.Td("manifold_pressure"), html.Td("kPa"), html.Td("Intake manifold absolute pressure")]), html.Tr([html.Td("rpm"), html.Td("RPM"), html.Td("Engine speed")]), html.Tr([html.Td("speed"), html.Td("km/h"), html.Td("Vehicle speed")]), html.Tr([html.Td("intake_temp"), html.Td("C"), html.Td("Intake air temperature")]), html.Tr([html.Td("mass_air_flow"), html.Td("g/s"), html.Td("Mass air flow rate")]), html.Tr([html.Td("throttle_pos"), html.Td("%"), html.Td("Throttle position")]), html.Tr([html.Td("ambient_temp"), html.Td("C"), html.Td("Ambient air temperature")]), html.Tr([html.Td("accel_pedal"), html.Td("%"), html.Td("Accelerator pedal position")]), ]), ], bordered=False, hover=True, responsive=True, size="sm"), ]), className="modern-card mb-4"), dbc.Row([ dbc.Col(dbc.Card(dbc.CardBody(dcc.Graph(figure=correlation_fig())), className="modern-card"), md=6, className="mb-4"), dbc.Col(dbc.Card(dbc.CardBody(dcc.Graph(figure=scatter_fig())), className="modern-card"), md=6, className="mb-4"), ]), html.H3("The two injected faults", className="text-white mb-3"), html.P("To validate detection we injected two known faults into a copy of the drive. " "These give us ground truth to measure against.", className="text-light mb-4"), dbc.Card(dbc.CardBody(dcc.Graph(figure=coolant_timeline())), className="modern-card mb-4"), dbc.Card(dbc.CardBody(dcc.Graph(figure=intake_timeline())), className="modern-card mb-4"), ])