updating frontend
Browse files- frontend/app.py +250 -185
frontend/app.py
CHANGED
|
@@ -1,184 +1,247 @@
|
|
| 1 |
import dash
|
| 2 |
-
from dash import dcc, html, Input, Output, State, dash_table
|
| 3 |
import dash_bootstrap_components as dbc
|
| 4 |
import pandas as pd
|
| 5 |
-
import json
|
| 6 |
import base64
|
| 7 |
import io
|
| 8 |
import os
|
| 9 |
|
| 10 |
# --- IN-MEMORY BACKEND HOOKS ---
|
| 11 |
-
# Importing directly from backend skips the internal network interface barriers
|
| 12 |
from backend.main import storage
|
| 13 |
from backend.PvDeepSetNet.src.DeepSets import run_prediction
|
| 14 |
|
| 15 |
-
# 1. Initialize App with
|
| 16 |
app = dash.Dash(
|
| 17 |
__name__,
|
| 18 |
-
external_stylesheets=[dbc.themes.
|
| 19 |
-
title="PvRecNet",
|
| 20 |
suppress_callback_exceptions=True,
|
| 21 |
-
# Crucial parameters for WSGI middleware hosting:
|
| 22 |
requests_pathname_prefix="/",
|
| 23 |
routes_pathname_prefix="/"
|
| 24 |
)
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
app.layout = html.Div([
|
| 28 |
-
dcc.Location(id='url', refresh=False),
|
|
|
|
| 29 |
dcc.Store(id='dataset-id-store', storage_type='session'),
|
| 30 |
-
html.Div(id='page-content'
|
| 31 |
])
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
def render_help_page():
|
| 35 |
return dbc.Container([
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
dbc.Row([
|
| 37 |
dbc.Col([
|
| 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 |
-
dbc.Col([
|
| 68 |
-
html.H5("Data Requirements", className="text-info"),
|
| 69 |
-
html.P("Your dataset must contain these exact columns:"),
|
| 70 |
-
html.Div([
|
| 71 |
-
dbc.Badge("patient_id", color="primary", className="me-1"),
|
| 72 |
-
dbc.Badge("episode", color="primary", className="me-1"),
|
| 73 |
-
dbc.Badge("marker", color="primary", className="me-1"),
|
| 74 |
-
dbc.Badge("allele", color="primary", className="me-1"),
|
| 75 |
-
dbc.Badge("region", color="primary", className="me-1"),
|
| 76 |
-
], className="mb-3"),
|
| 77 |
-
html.Small("Note: marker = loci, allele = haplotype, region = country.", className="text-muted")
|
| 78 |
-
], md=6),
|
| 79 |
-
], className="mb-4"),
|
| 80 |
-
|
| 81 |
-
html.Hr(),
|
| 82 |
-
|
| 83 |
-
# Section: Limitations
|
| 84 |
-
html.H4("⚠️ Scope & Limitations", className="mt-4 text-warning"),
|
| 85 |
-
html.Ul([
|
| 86 |
-
html.Li([html.B("Dependency on Pv3Rs: "), "Since the model is trained on the probabilistic outputs of Pv3Rs, its accuracy is bounded by Pv3Rs. Systematic errors in Pv3Rs may be replicated."]),
|
| 87 |
-
html.Li([html.B("Data Requirements: "), "Currently works only with amplicon sequencing datasets containing multiple markers and alleles."]),
|
| 88 |
-
html.Li([html.B("Episode Constraints: "), "Designed specifically for paired-episode classifications; it does not address scenarios with more than two episodes per individual as a single classification."]),
|
| 89 |
-
html.Li([html.B("Generalization: "), "The model may not generalize well to genetic markers or alleles unobserved during training."]),
|
| 90 |
-
html.Li([html.B("Protocol Dependent: "), "Trained specifically on ", html.A("pvAmpSeq", href="https://pubmed.ncbi.nlm.nih.gov/40492072/", target="_blank"), "; it only generalizes when using the same markers and allele names specified in that protocol."]),
|
| 91 |
-
], className="text-secondary"),
|
| 92 |
-
|
| 93 |
-
# Sample Dataset Section
|
| 94 |
-
dbc.Card([
|
| 95 |
-
dbc.CardBody([
|
| 96 |
-
dbc.Row([
|
| 97 |
-
dbc.Col([
|
| 98 |
-
html.H4("Practice with Sample Data", className="card-title"),
|
| 99 |
-
html.P("Download our demonstration file to see how the markers and episodes should be formatted."),
|
| 100 |
-
], md=8),
|
| 101 |
-
dbc.Col([
|
| 102 |
-
dbc.Button(
|
| 103 |
-
"📥 Download Sample CSV",
|
| 104 |
-
id="download-sample-btn",
|
| 105 |
-
color="success",
|
| 106 |
-
className="w-100 mt-2"
|
| 107 |
-
),
|
| 108 |
-
dcc.Download(id="download-sample-csv"),
|
| 109 |
-
], md=4),
|
| 110 |
-
])
|
| 111 |
-
])
|
| 112 |
-
], color="light", className="mt-5 border-start border-success border-4"),
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
], fluid=True)
|
| 118 |
|
| 119 |
-
|
|
|
|
| 120 |
def render_main_app():
|
| 121 |
return dbc.Container([
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
]),
|
| 126 |
|
| 127 |
-
dbc.
|
| 128 |
-
#
|
| 129 |
-
dbc.
|
| 130 |
-
dbc.
|
| 131 |
-
dbc.
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
])
|
| 146 |
-
],
|
| 147 |
-
]),
|
| 148 |
|
| 149 |
-
#
|
| 150 |
-
dbc.
|
| 151 |
-
dbc.
|
| 152 |
-
dbc.
|
| 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 |
], fluid=True)
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
@app.callback(
|
| 183 |
Output("download-sample-csv", "data"),
|
| 184 |
Input("download-sample-btn", "n_clicks"),
|
|
@@ -186,47 +249,23 @@ def render_main_app():
|
|
| 186 |
)
|
| 187 |
def download_sample_data(n_clicks):
|
| 188 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 189 |
-
|
| 190 |
-
# Adapt structural finding context for either local execution or Docker volumes
|
| 191 |
if os.path.basename(current_dir) == 'frontend':
|
| 192 |
base_dir = os.path.dirname(current_dir)
|
| 193 |
else:
|
| 194 |
base_dir = current_dir
|
| 195 |
|
| 196 |
sample_path = os.path.join(base_dir, 'backend', 'PvDeepSetNet', 'datasets', 'GUI_sample_dataset.csv')
|
| 197 |
-
|
| 198 |
-
# Fallback to internal module package tracking if paths nested differently
|
| 199 |
if not os.path.exists(sample_path):
|
| 200 |
sample_path = os.path.join(base_dir, 'backend', 'PvDeepSetNet', 'src', 'DeepSets', 'datasets', 'GUI_sample_dataset.csv')
|
| 201 |
|
| 202 |
sample_df = pd.read_csv(sample_path)
|
| 203 |
return dcc.send_data_frame(sample_df.to_csv, "pvrecnet_sample_data.csv", index=False)
|
| 204 |
|
| 205 |
-
# --- ROUTER CALLBACK ---
|
| 206 |
-
@app.callback(
|
| 207 |
-
Output('page-content', 'children'),
|
| 208 |
-
Input('url', 'pathname')
|
| 209 |
-
)
|
| 210 |
-
def display_page(pathname):
|
| 211 |
-
if pathname == '/help':
|
| 212 |
-
return render_help_page()
|
| 213 |
-
else:
|
| 214 |
-
return render_main_app()
|
| 215 |
-
|
| 216 |
-
# --- NAVIGATION CALLBACK ---
|
| 217 |
-
@app.callback(
|
| 218 |
-
Output("main-tabs", "active_tab"),
|
| 219 |
-
Input("get-started-btn", "n_clicks"),
|
| 220 |
-
prevent_initial_call=True
|
| 221 |
-
)
|
| 222 |
-
def navigate_to_predict(n_clicks):
|
| 223 |
-
return "tab-predict" if n_clicks else "tab-intro"
|
| 224 |
-
|
| 225 |
-
# --- FUNCTIONAL CALLBACKS ---
|
| 226 |
|
|
|
|
| 227 |
@app.callback(
|
| 228 |
[Output('dataset-id-store', 'data'), Output('upload-status', 'children'), Output('predict-btn', 'disabled'),
|
| 229 |
-
Output('preview-table', 'data'), Output('preview-table', 'columns')
|
| 230 |
[Input('upload-data', 'contents')], [State('upload-data', 'filename')], prevent_initial_call=True
|
| 231 |
)
|
| 232 |
def handle_upload(contents, filename):
|
|
@@ -234,16 +273,25 @@ def handle_upload(contents, filename):
|
|
| 234 |
try:
|
| 235 |
decoded = base64.b64decode(contents.split(',')[1])
|
| 236 |
df = pd.read_csv(io.BytesIO(decoded))
|
| 237 |
-
cols = [{"name": i, "id": i, "editable": True
|
| 238 |
|
| 239 |
-
# Save
|
| 240 |
dataset_id = "ds_001"
|
| 241 |
storage[dataset_id] = decoded
|
| 242 |
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
except Exception as e:
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
|
|
|
| 247 |
@app.callback(
|
| 248 |
Output('prediction-output', 'children'),
|
| 249 |
Input('predict-btn', 'n_clicks'),
|
|
@@ -252,19 +300,28 @@ def handle_upload(contents, filename):
|
|
| 252 |
)
|
| 253 |
def run_predict(n_clicks, dataset_id):
|
| 254 |
try:
|
| 255 |
-
#
|
| 256 |
file_bytes = storage.get(dataset_id)
|
| 257 |
if not file_bytes:
|
| 258 |
-
return dbc.Alert("Error:
|
| 259 |
|
| 260 |
df = pd.read_csv(io.BytesIO(file_bytes))
|
| 261 |
|
| 262 |
-
#
|
| 263 |
model_output = run_prediction(df)
|
| 264 |
|
| 265 |
fig1 = model_output.get('donut_plot')
|
| 266 |
fig2 = model_output.get('distribution_plot')
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
df_res = pd.DataFrame(model_output['results_table'])
|
| 269 |
prob_cols = ['C_marg', 'L_marg', 'I_marg']
|
| 270 |
|
|
@@ -276,21 +333,29 @@ def run_predict(n_clicks, dataset_id):
|
|
| 276 |
data=df_res.to_dict('records'),
|
| 277 |
columns=[{"name": i, "id": i} for i in df_res.columns],
|
| 278 |
export_format='csv',
|
| 279 |
-
page_size=10,
|
| 280 |
-
|
| 281 |
-
style_cell={'textAlign': 'left'}
|
| 282 |
)
|
| 283 |
|
| 284 |
return html.Div([
|
| 285 |
dbc.Row([
|
| 286 |
-
dbc.Col([
|
| 287 |
-
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
])
|
| 290 |
-
])
|
|
|
|
| 291 |
except Exception as e:
|
| 292 |
-
return dbc.Alert(
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
if __name__ == '__main__':
|
| 295 |
-
# Local fallback runner
|
| 296 |
app.run_server(debug=True, port=8050)
|
|
|
|
| 1 |
import dash
|
| 2 |
+
from dash import dcc, html, Input, Output, State, dash_table
|
| 3 |
import dash_bootstrap_components as dbc
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
import base64
|
| 6 |
import io
|
| 7 |
import os
|
| 8 |
|
| 9 |
# --- IN-MEMORY BACKEND HOOKS ---
|
|
|
|
| 10 |
from backend.main import storage
|
| 11 |
from backend.PvDeepSetNet.src.DeepSets import run_prediction
|
| 12 |
|
| 13 |
+
# 1. Initialize App with a high-contrast dark theme and FontAwesome icons
|
| 14 |
app = dash.Dash(
|
| 15 |
__name__,
|
| 16 |
+
external_stylesheets=[dbc.themes.SLATE, dbc.icons.FONT_AWESOME],
|
| 17 |
+
title="PvRecNet | Diagnostic Companion",
|
| 18 |
suppress_callback_exceptions=True,
|
|
|
|
| 19 |
requests_pathname_prefix="/",
|
| 20 |
routes_pathname_prefix="/"
|
| 21 |
)
|
| 22 |
|
| 23 |
+
# --- COMPONENT STYLING DICTIONARIES ---
|
| 24 |
+
SIDEBAR_STYLE = {
|
| 25 |
+
"position": "fixed",
|
| 26 |
+
"top": 0,
|
| 27 |
+
"left": 0,
|
| 28 |
+
"bottom": 0,
|
| 29 |
+
"width": "20rem",
|
| 30 |
+
"padding": "3rem 1.5rem",
|
| 31 |
+
"background-color": "#1e293b", # Dark Slate Blue
|
| 32 |
+
"borderRight": "1px solid rgba(45, 212, 191, 0.1)",
|
| 33 |
+
"zIndex": 100
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
CONTENT_STYLE = {
|
| 37 |
+
"margin-left": "22rem",
|
| 38 |
+
"margin-right": "2rem",
|
| 39 |
+
"padding": "3rem 1rem",
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# --- UNIFIED LAYOUT TEMPLATE ---
|
| 43 |
+
sidebar = html.Div(
|
| 44 |
+
[
|
| 45 |
+
html.Div([
|
| 46 |
+
html.H2("PvRecNet", className="display-6 font-weight-bold mb-0", style={"color": "#2dd4bf", "letterSpacing": "-1px"}),
|
| 47 |
+
html.P("Malaria Recurrence Platform", className="small text-muted text-uppercase tracking-wider mt-1"),
|
| 48 |
+
], className="mb-5"),
|
| 49 |
+
|
| 50 |
+
html.Hr(style={"borderTop": "1px solid rgba(248, 250, 252, 0.1)", "marginBottom": "2rem"}),
|
| 51 |
+
|
| 52 |
+
dbc.Nav(
|
| 53 |
+
[
|
| 54 |
+
dbc.NavLink([html.I(className="fas fa-microscope me-3"), "Analytical Hub"], href="/", active="exact", className="py-3 px-4 mb-2 rounded"),
|
| 55 |
+
dbc.NavLink([html.I(className="fas fa-book-medical me-3"), "Guidelines"], href="/help", active="exact", className="py-3 px-4 mb-2 rounded"),
|
| 56 |
+
dbc.NavLink([html.I(className="fas fa-flask me-3"), "pvAmpSeq Protocol"], href="https://pubmed.ncbi.nlm.nih.gov/40492072/", target="_blank", className="py-3 px-4 mb-2 rounded"),
|
| 57 |
+
],
|
| 58 |
+
vertical=True,
|
| 59 |
+
pills=True,
|
| 60 |
+
className="flex-column"
|
| 61 |
+
),
|
| 62 |
+
|
| 63 |
+
html.Div(
|
| 64 |
+
html.Small("System Context: Operational (2026)", className="text-muted"),
|
| 65 |
+
style={"position": "absolute", "bottom": "2rem", "left": "1.5rem"}
|
| 66 |
+
)
|
| 67 |
+
],
|
| 68 |
+
style=SIDEBAR_STYLE,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
app.layout = html.Div([
|
| 72 |
+
dcc.Location(id='url', refresh=False),
|
| 73 |
+
sidebar,
|
| 74 |
dcc.Store(id='dataset-id-store', storage_type='session'),
|
| 75 |
+
html.Div(id='page-content', style=CONTENT_STYLE)
|
| 76 |
])
|
| 77 |
|
| 78 |
+
|
| 79 |
+
# --- PAGE 1: RE-STYLED HELP & GUIDELINES PAGE ---
|
| 80 |
def render_help_page():
|
| 81 |
return dbc.Container([
|
| 82 |
+
html.Div([
|
| 83 |
+
html.H1("System Guidelines & Documentation", className="display-5 font-weight-bold", style={"color": "#2dd4bf"}),
|
| 84 |
+
html.P([
|
| 85 |
+
"PvRecNet utilizes a Deep Learning architecture to accurately differentiate between ",
|
| 86 |
+
html.B("Recrudescence, Relapse, and Reinfection"),
|
| 87 |
+
" from complex genetic datasets."
|
| 88 |
+
], className="lead text-muted mb-5"),
|
| 89 |
+
]),
|
| 90 |
+
|
| 91 |
dbc.Row([
|
| 92 |
dbc.Col([
|
| 93 |
+
dbc.Card([
|
| 94 |
+
dbc.CardHeader(html.H5("Data Specifications", className="mb-0 text-info"), style={"background": "#1e293b"}),
|
| 95 |
+
dbc.CardBody([
|
| 96 |
+
html.P("Your uploaded dataset must contain these exact case-sensitive column properties:"),
|
| 97 |
+
html.Div([
|
| 98 |
+
dbc.Badge("patient_id", color="secondary", className="me-2 mb-2 p-2"),
|
| 99 |
+
dbc.Badge("episode", color="secondary", className="me-2 mb-2 p-2"),
|
| 100 |
+
dbc.Badge("marker", color="secondary", className="me-2 mb-2 p-2"),
|
| 101 |
+
dbc.Badge("allele", color="secondary", className="me-2 mb-2 p-2"),
|
| 102 |
+
dbc.Badge("region", color="secondary", className="me-2 mb-2 p-2"),
|
| 103 |
+
], className="my-3"),
|
| 104 |
+
html.Small("Note: 'marker' refers to genetic loci; 'allele' mapped to haplotype sequences.", className="text-muted d-block mt-2")
|
| 105 |
+
])
|
| 106 |
+
], className="border-0 bg-dark shadow-sm rounded mb-4")
|
| 107 |
+
], md=6),
|
| 108 |
+
|
| 109 |
+
dbc.Col([
|
| 110 |
+
dbc.Card([
|
| 111 |
+
dbc.CardHeader(html.H5("Scope & Constraints", className="mb-0 text-warning"), style={"background": "#1e293b"}),
|
| 112 |
+
dbc.CardBody([
|
| 113 |
+
html.Ul([
|
| 114 |
+
html.Li("Bound by Pv3Rs baseline probabilistic training distributions."),
|
| 115 |
+
html.Li("Configured specifically for paired-episode sample auditing."),
|
| 116 |
+
html.Li("Trained on clinical amplicon sequencing parameters.")
|
| 117 |
+
], className="text-muted pl-3")
|
| 118 |
+
])
|
| 119 |
+
], className="border-0 bg-dark shadow-sm rounded mb-4")
|
| 120 |
+
], md=6)
|
| 121 |
+
]),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
dbc.Card([
|
| 124 |
+
dbc.CardBody([
|
| 125 |
+
dbc.Row([
|
| 126 |
+
dbc.Col([
|
| 127 |
+
html.H4("Practice Workspace", className="mb-2 text-white"),
|
| 128 |
+
html.P("Download our validation configuration matrix template to evaluate platform layout structures locally.", className="text-muted mb-0")
|
| 129 |
+
], md=8),
|
| 130 |
+
dbc.Col([
|
| 131 |
+
dbc.Button(
|
| 132 |
+
[html.I(className="fas fa-file-download me-2"), "Download Sample CSV"],
|
| 133 |
+
id="download-sample-btn",
|
| 134 |
+
color="success",
|
| 135 |
+
className="w-100 h-100 py-3 font-weight-bold"
|
| 136 |
+
),
|
| 137 |
+
dcc.Download(id="download-sample-csv"),
|
| 138 |
+
], md=4, className="d-flex align-items-center")
|
| 139 |
+
])
|
| 140 |
+
])
|
| 141 |
+
], color="light", outline=True, className="mt-4 border-start border-success border-4 bg-dark shadow-sm")
|
| 142 |
], fluid=True)
|
| 143 |
|
| 144 |
+
|
| 145 |
+
# --- PAGE 2: MAIN WORKBENCH DASHBOARD ---
|
| 146 |
def render_main_app():
|
| 147 |
return dbc.Container([
|
| 148 |
+
html.Div([
|
| 149 |
+
html.H1("Analytical Workbench", className="display-5 font-weight-bold mb-1"),
|
| 150 |
+
html.P("Upload evaluation matrices to pass to the Deep Learning prediction runner.", className="text-muted mb-5"),
|
| 151 |
]),
|
| 152 |
|
| 153 |
+
dbc.Row([
|
| 154 |
+
# Control / Upload Column
|
| 155 |
+
dbc.Col([
|
| 156 |
+
dbc.Card([
|
| 157 |
+
dbc.CardHeader([
|
| 158 |
+
html.I(className="fas fa-cloud-upload-alt me-2 text-teal"),
|
| 159 |
+
html.B("1. INPUT MATRIX")
|
| 160 |
+
], style={"background": "#1e293b", "color": "#2dd4bf"}),
|
| 161 |
+
dbc.CardBody([
|
| 162 |
+
dcc.Upload(
|
| 163 |
+
id='upload-data',
|
| 164 |
+
children=html.Div([
|
| 165 |
+
html.I(className="fas fa-file-csv display-6 d-block mb-2 text-muted"),
|
| 166 |
+
'Drag & Drop Matrix or ', html.Span('Browse Files', style={"color": "#2dd4bf", "textDecoration": "underline"})
|
| 167 |
+
]),
|
| 168 |
+
style={
|
| 169 |
+
'width': '100%', 'height': '140px', 'lineHeight': '24px',
|
| 170 |
+
'borderWidth': '2px', 'borderStyle': 'dashed', 'borderRadius': '12px',
|
| 171 |
+
'textAlign': 'center', 'background': '#0f172a', 'paddingTop': '30px',
|
| 172 |
+
'borderColor': 'rgba(45, 212, 191, 0.2)', 'cursor': 'pointer'
|
| 173 |
+
}
|
| 174 |
+
),
|
| 175 |
+
html.Div(id='upload-status', className="mt-3"),
|
| 176 |
+
dbc.Button(
|
| 177 |
+
[html.I(className="fas fa-bolt me-2"), "Run Pipeline Prediction"],
|
| 178 |
+
id="predict-btn", disabled=True, className="w-100 mt-4 py-2 font-weight-bold shadow-sm",
|
| 179 |
+
style={"backgroundColor": "#2dd4bf", "border": "none", "color": "#0f172a"}
|
| 180 |
+
)
|
| 181 |
])
|
| 182 |
+
], className="border-0 shadow rounded mb-4 bg-dark"),
|
| 183 |
+
], md=4),
|
| 184 |
|
| 185 |
+
# Data Preview Workbench Column
|
| 186 |
+
dbc.Col([
|
| 187 |
+
dbc.Card([
|
| 188 |
+
dbc.CardHeader([
|
| 189 |
+
html.I(className="fas fa-table me-2"),
|
| 190 |
+
html.B("2. GENOMIC AUDIT BENCH")
|
| 191 |
+
], style={"background": "#1e293b"}),
|
| 192 |
+
dbc.CardBody(
|
| 193 |
+
dcc.Loading(dash_table.DataTable(
|
| 194 |
+
id='preview-table',
|
| 195 |
+
columns=[],
|
| 196 |
+
data=[],
|
| 197 |
+
export_format='csv',
|
| 198 |
+
editable=True,
|
| 199 |
+
filter_action="native",
|
| 200 |
+
sort_action="native",
|
| 201 |
+
page_size=7,
|
| 202 |
+
style_table={'height': '350px', 'overflowY': 'auto'},
|
| 203 |
+
style_header={'backgroundColor': '#1e293b', 'color': '#2dd4bf', 'fontWeight': 'bold', 'border': '1px solid #334155'},
|
| 204 |
+
style_cell={'backgroundColor': '#0f172a', 'color': '#cbd5e1', 'textAlign': 'left', 'border': '1px solid #334155', 'padding': '12px'}
|
| 205 |
+
), type="dot")
|
| 206 |
+
)
|
| 207 |
+
], className="border-0 shadow rounded mb-4 bg-dark"),
|
| 208 |
+
], md=8),
|
| 209 |
+
]),
|
| 210 |
+
|
| 211 |
+
# Results Section Layout Row
|
| 212 |
+
dbc.Row([
|
| 213 |
+
dbc.Col(
|
| 214 |
+
dbc.Card([
|
| 215 |
+
dbc.CardHeader([
|
| 216 |
+
html.I(className="fas fa-chart-pie me-2 text-teal"),
|
| 217 |
+
html.B("3. INTERPRETATIVE PREDICTIONS")
|
| 218 |
+
], style={"background": "#1e293b", "color": "#2dd4bf"}),
|
| 219 |
+
dbc.CardBody(
|
| 220 |
+
dcc.Loading(
|
| 221 |
+
html.Div(id="prediction-output", className="p-3 text-center text-muted"),
|
| 222 |
+
type="graph"
|
| 223 |
+
)
|
| 224 |
+
)
|
| 225 |
+
], className="border-0 shadow rounded mb-5 bg-dark"),
|
| 226 |
+
width=12
|
| 227 |
+
)
|
| 228 |
])
|
| 229 |
], fluid=True)
|
| 230 |
|
| 231 |
+
|
| 232 |
+
# --- ROUTER ROUTING LOGIC CALLBACK ---
|
| 233 |
+
@app.callback(
|
| 234 |
+
Output('page-content', 'children'),
|
| 235 |
+
Input('url', 'pathname')
|
| 236 |
+
)
|
| 237 |
+
def display_page(pathname):
|
| 238 |
+
if pathname == '/help':
|
| 239 |
+
return render_help_page()
|
| 240 |
+
else:
|
| 241 |
+
return render_main_app()
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
# --- SAMPLE TEMPLATE DOWNLOAD CONTROLLER ---
|
| 245 |
@app.callback(
|
| 246 |
Output("download-sample-csv", "data"),
|
| 247 |
Input("download-sample-btn", "n_clicks"),
|
|
|
|
| 249 |
)
|
| 250 |
def download_sample_data(n_clicks):
|
| 251 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
| 252 |
if os.path.basename(current_dir) == 'frontend':
|
| 253 |
base_dir = os.path.dirname(current_dir)
|
| 254 |
else:
|
| 255 |
base_dir = current_dir
|
| 256 |
|
| 257 |
sample_path = os.path.join(base_dir, 'backend', 'PvDeepSetNet', 'datasets', 'GUI_sample_dataset.csv')
|
|
|
|
|
|
|
| 258 |
if not os.path.exists(sample_path):
|
| 259 |
sample_path = os.path.join(base_dir, 'backend', 'PvDeepSetNet', 'src', 'DeepSets', 'datasets', 'GUI_sample_dataset.csv')
|
| 260 |
|
| 261 |
sample_df = pd.read_csv(sample_path)
|
| 262 |
return dcc.send_data_frame(sample_df.to_csv, "pvrecnet_sample_data.csv", index=False)
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
# --- IN-MEMORY DATA UPLOAD PIPELINE CALLBACK ---
|
| 266 |
@app.callback(
|
| 267 |
[Output('dataset-id-store', 'data'), Output('upload-status', 'children'), Output('predict-btn', 'disabled'),
|
| 268 |
+
Output('preview-table', 'data'), Output('preview-table', 'columns')],
|
| 269 |
[Input('upload-data', 'contents')], [State('upload-data', 'filename')], prevent_initial_call=True
|
| 270 |
)
|
| 271 |
def handle_upload(contents, filename):
|
|
|
|
| 273 |
try:
|
| 274 |
decoded = base64.b64decode(contents.split(',')[1])
|
| 275 |
df = pd.read_csv(io.BytesIO(decoded))
|
| 276 |
+
cols = [{"name": i, "id": i, "editable": True} for i in df.columns]
|
| 277 |
|
| 278 |
+
# Save raw binary stream into global runtime application space
|
| 279 |
dataset_id = "ds_001"
|
| 280 |
storage[dataset_id] = decoded
|
| 281 |
|
| 282 |
+
status = dbc.Alert([
|
| 283 |
+
html.I(className="fas fa-check-circle me-2"), f"Loaded: {filename}"
|
| 284 |
+
], color="success", className="mt-2 py-2 text-center rounded")
|
| 285 |
+
|
| 286 |
+
return dataset_id, status, False, df.to_dict('records'), cols
|
| 287 |
except Exception as e:
|
| 288 |
+
error_status = dbc.Alert([
|
| 289 |
+
html.I(className="fas fa-exclamation-triangle me-2"), f"Parse Failure: {str(e)}"
|
| 290 |
+
], color="danger", className="mt-2 py-2 text-center rounded")
|
| 291 |
+
return None, error_status, True, [], []
|
| 292 |
+
|
| 293 |
|
| 294 |
+
# --- DEEP SETS WORKFLOW MATHEMATICAL INFERENCE PIPELINE ---
|
| 295 |
@app.callback(
|
| 296 |
Output('prediction-output', 'children'),
|
| 297 |
Input('predict-btn', 'n_clicks'),
|
|
|
|
| 300 |
)
|
| 301 |
def run_predict(n_clicks, dataset_id):
|
| 302 |
try:
|
| 303 |
+
# Pull transactional context instantly from in-memory cache
|
| 304 |
file_bytes = storage.get(dataset_id)
|
| 305 |
if not file_bytes:
|
| 306 |
+
return dbc.Alert("Error: Target matrix could not be resolved from working memory.", color="danger")
|
| 307 |
|
| 308 |
df = pd.read_csv(io.BytesIO(file_bytes))
|
| 309 |
|
| 310 |
+
# Invoke DeepSets Machine Learning core package natively
|
| 311 |
model_output = run_prediction(df)
|
| 312 |
|
| 313 |
fig1 = model_output.get('donut_plot')
|
| 314 |
fig2 = model_output.get('distribution_plot')
|
| 315 |
|
| 316 |
+
# Format Plotly layout backgrounds to blend natively with dark dashboard cards
|
| 317 |
+
for fig in [fig1, fig2]:
|
| 318 |
+
if fig:
|
| 319 |
+
fig.update_layout(
|
| 320 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 321 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 322 |
+
font_color='#cbd5e1'
|
| 323 |
+
)
|
| 324 |
+
|
| 325 |
df_res = pd.DataFrame(model_output['results_table'])
|
| 326 |
prob_cols = ['C_marg', 'L_marg', 'I_marg']
|
| 327 |
|
|
|
|
| 333 |
data=df_res.to_dict('records'),
|
| 334 |
columns=[{"name": i, "id": i} for i in df_res.columns],
|
| 335 |
export_format='csv',
|
| 336 |
+
page_size=10,
|
| 337 |
+
style_header={'backgroundColor': '#1e293b', 'color': '#2dd4bf', 'fontWeight': 'bold'},
|
| 338 |
+
style_cell={'backgroundColor': '#0f172a', 'color': '#cbd5e1', 'textAlign': 'left', 'border': '1px solid #334155', 'padding': '10px'}
|
| 339 |
)
|
| 340 |
|
| 341 |
return html.Div([
|
| 342 |
dbc.Row([
|
| 343 |
+
dbc.Col([
|
| 344 |
+
html.Div(dcc.Graph(figure=fig1, config={'displayModeBar': False}) if fig1 else html.Div(), className="mb-4"),
|
| 345 |
+
html.Div(dcc.Graph(figure=fig2, config={'displayModeBar': False}) if fig2 else html.Div())
|
| 346 |
+
], md=5, className="border-end", style={"borderColor": "rgba(248,250,252,0.1) !important"}),
|
| 347 |
+
dbc.Col([
|
| 348 |
+
html.H5("Classification Breakdown Matrix", className="text-teal mb-3", style={"color": "#2dd4bf"}),
|
| 349 |
+
table
|
| 350 |
+
], md=7, className="ps-4")
|
| 351 |
])
|
| 352 |
+
], className="text-start")
|
| 353 |
+
|
| 354 |
except Exception as e:
|
| 355 |
+
return dbc.Alert([
|
| 356 |
+
html.I(className="fas fa-radiation me-2"), f"Pipeline Engine Exception: {str(e)}"
|
| 357 |
+
], color="danger", className="text-start")
|
| 358 |
+
|
| 359 |
|
| 360 |
if __name__ == '__main__':
|
|
|
|
| 361 |
app.run_server(debug=True, port=8050)
|