updating frontend/app.py
Browse files- frontend/app.py +23 -53
frontend/app.py
CHANGED
|
@@ -5,14 +5,20 @@ import pandas as pd
|
|
| 5 |
import json
|
| 6 |
import base64
|
| 7 |
import io
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
| 11 |
app = dash.Dash(
|
| 12 |
__name__,
|
| 13 |
external_stylesheets=[dbc.themes.FLATLY],
|
| 14 |
title="PvRecNet",
|
| 15 |
-
suppress_callback_exceptions=True
|
|
|
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
# 2. Define the Main App Layout (Navigation + Page Container)
|
|
@@ -22,7 +28,6 @@ app.layout = html.Div([
|
|
| 22 |
html.Div(id='page-content') # Where different pages will be rendered
|
| 23 |
])
|
| 24 |
|
| 25 |
-
# --- PAGE 1: THE HELP / GUIDELINE PAGE ---
|
| 26 |
# --- PAGE 1: THE HELP / GUIDELINE PAGE ---
|
| 27 |
def render_help_page():
|
| 28 |
return dbc.Container([
|
|
@@ -37,12 +42,12 @@ def render_help_page():
|
|
| 37 |
html.H1("System Guidelines & Documentation", className="display-5 text-primary"),
|
| 38 |
html.P([
|
| 39 |
"PvRecNet is a deep learning-based model for ",
|
| 40 |
-
html.B(html.I("Plasmodium vivax")),
|
| 41 |
" recurrence classification."
|
| 42 |
], className="lead"),
|
| 43 |
], className="mb-5"),
|
| 44 |
|
| 45 |
-
#
|
| 46 |
dbc.Row([
|
| 47 |
# Section: Description
|
| 48 |
dbc.Col([
|
|
@@ -51,7 +56,7 @@ def render_help_page():
|
|
| 51 |
"Our architecture differentiates between ",
|
| 52 |
html.B("Recrudescence, Relapse, and Reinfection"),
|
| 53 |
". It is trained on probabilistic outputs from ",
|
| 54 |
-
html.A("Pv3Rs", href="https://github.com/aimeertaylor/Pv3Rs", target="_blank"),
|
| 55 |
" to ensure robust genomic classification."
|
| 56 |
]),
|
| 57 |
], md=6),
|
|
@@ -81,10 +86,9 @@ def render_help_page():
|
|
| 81 |
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."]),
|
| 82 |
html.Li([html.B("Generalization: "), "The model may not generalize well to genetic markers or alleles unobserved during training."]),
|
| 83 |
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."]),
|
| 84 |
-
|
| 85 |
], className="text-secondary"),
|
| 86 |
|
| 87 |
-
#
|
| 88 |
dbc.Card([
|
| 89 |
dbc.CardBody([
|
| 90 |
dbc.Row([
|
|
@@ -115,7 +119,6 @@ def render_main_app():
|
|
| 115 |
return dbc.Container([
|
| 116 |
dbc.Row([
|
| 117 |
dbc.Col(html.H1("PvRecNet", className="text-center my-4", style={"color":'#113d59'}), width=10),
|
| 118 |
-
# Help Link in Top Right
|
| 119 |
dbc.Col(html.A("Help & Guidelines", href="/help", className="text-muted", style={"marginTop": "30px", "display": "block"}), width=2)
|
| 120 |
]),
|
| 121 |
|
|
@@ -171,18 +174,6 @@ def render_main_app():
|
|
| 171 |
], className="mt-4"),
|
| 172 |
dbc.Row(dbc.Col(dbc.Card([dbc.CardHeader(html.B("Step 3: Model Results")), dbc.CardBody(dcc.Loading(id="prediction-output"))], className="mt-4 mb-5"), width=12))
|
| 173 |
]),
|
| 174 |
-
|
| 175 |
-
# # Tab 2: Analytics Tools
|
| 176 |
-
# dbc.Tab(label="Analytics Tools", tab_id="tab-analytics", children=[
|
| 177 |
-
# dbc.Row(dbc.Col(dbc.Card([
|
| 178 |
-
# dbc.CardHeader(html.B("Genomic Analytics")),
|
| 179 |
-
# dbc.CardBody([
|
| 180 |
-
# dbc.Button("Compute MOI", id="moi-btn", color="info", className="me-2"),
|
| 181 |
-
# dbc.Button("Compute Allele Freq", id="freq-btn", color="secondary"),
|
| 182 |
-
# html.Hr(), dcc.Loading(id="analytics-output")
|
| 183 |
-
# ])
|
| 184 |
-
# ], className="mt-4"), md=12))
|
| 185 |
-
# ])
|
| 186 |
])
|
| 187 |
], fluid=True)
|
| 188 |
|
|
@@ -192,8 +183,11 @@ def render_main_app():
|
|
| 192 |
prevent_initial_call=True,
|
| 193 |
)
|
| 194 |
def download_sample_data(n_clicks):
|
| 195 |
-
#
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
| 197 |
return dcc.send_data_frame(sample_df.to_csv, "pvrecnet_sample_data.csv", index=False)
|
| 198 |
|
| 199 |
# --- ROUTER CALLBACK ---
|
|
@@ -216,7 +210,7 @@ def display_page(pathname):
|
|
| 216 |
def navigate_to_predict(n_clicks):
|
| 217 |
return "tab-predict" if n_clicks else "tab-intro"
|
| 218 |
|
| 219 |
-
# --- FUNCTIONAL CALLBACKS
|
| 220 |
|
| 221 |
@app.callback(
|
| 222 |
[Output('dataset-id-store', 'data'), Output('upload-status', 'children'), Output('predict-btn', 'disabled'),
|
|
@@ -240,32 +234,26 @@ def handle_upload(contents, filename):
|
|
| 240 |
State('dataset-id-store', 'data'),
|
| 241 |
prevent_initial_call=True
|
| 242 |
)
|
| 243 |
-
|
| 244 |
def run_predict(n_clicks, dataset_id):
|
| 245 |
try:
|
| 246 |
res = PvRecNetAPI.run_prediction(dataset_id)
|
| 247 |
fig1 = json.loads(res['plot_json1'])
|
| 248 |
fig2 = json.loads(res['plot_json2'])
|
| 249 |
|
| 250 |
-
# 1. Convert the results list to a DataFrame
|
| 251 |
df = pd.DataFrame(res['results_table'])
|
| 252 |
-
|
| 253 |
-
# 2. Identify the probability columns (adjust names based on your actual API output)
|
| 254 |
-
prob_cols = ['C_marg', 'L_marg', 'I_marg'] # or 'post_C', etc.
|
| 255 |
|
| 256 |
-
# 3. Apply string formatting for consistent visual alignment
|
| 257 |
for col in prob_cols:
|
| 258 |
if col in df.columns:
|
| 259 |
df[col] = df[col].map(lambda x: f"{x:.4f}" if pd.notnull(x) else x)
|
| 260 |
|
| 261 |
-
# 4. Create the DataTable using the formatted data
|
| 262 |
table = dash_table.DataTable(
|
| 263 |
data=df.to_dict('records'),
|
| 264 |
columns=[{"name": i, "id": i} for i in df.columns],
|
| 265 |
export_format='csv',
|
| 266 |
page_size=10,
|
| 267 |
style_table={'height': '400px', 'overflowY': 'auto'},
|
| 268 |
-
style_cell={'textAlign': 'left'}
|
| 269 |
)
|
| 270 |
|
| 271 |
return html.Div([
|
|
@@ -277,24 +265,6 @@ def run_predict(n_clicks, dataset_id):
|
|
| 277 |
except Exception as e:
|
| 278 |
return dbc.Alert(f"Error: {str(e)}", color="danger")
|
| 279 |
|
| 280 |
-
# @app.callback(
|
| 281 |
-
# Output('analytics-output', 'children'),
|
| 282 |
-
# [Input('moi-btn', 'n_clicks'), Input('freq-btn', 'n_clicks')],
|
| 283 |
-
# State('dataset-id-store', 'data'),
|
| 284 |
-
# prevent_initial_call=True
|
| 285 |
-
# )
|
| 286 |
-
# def handle_analytics(moi_n, freq_n, dataset_id):
|
| 287 |
-
# ctx = callback_context
|
| 288 |
-
# bid = ctx.triggered[0]['prop_id'].split('.')[0]
|
| 289 |
-
# try:
|
| 290 |
-
# if bid == "moi-btn":
|
| 291 |
-
# res = PvRecNetAPI.get_moi(dataset_id)
|
| 292 |
-
# return dash_table.DataTable(data=res, columns=[{"name": i, "id": i} for i in pd.DataFrame(res).columns], export_format='csv')
|
| 293 |
-
# else:
|
| 294 |
-
# res = PvRecNetAPI.get_allele_freq(dataset_id)
|
| 295 |
-
# import plotly.express as px
|
| 296 |
-
# return dcc.Graph(figure=px.pie(res, names='allele', values='frequency'))
|
| 297 |
-
# except Exception as e: return dbc.Alert(str(e), color="danger")
|
| 298 |
-
|
| 299 |
if __name__ == '__main__':
|
| 300 |
-
|
|
|
|
|
|
| 5 |
import json
|
| 6 |
import base64
|
| 7 |
import io
|
| 8 |
+
import os # Added for robust path parsing
|
| 9 |
|
| 10 |
+
# Ensure Python treats imports seamlessly relative to our app root folder / adding frontend/
|
| 11 |
+
from frontend.services.api import PvRecNetAPI
|
| 12 |
+
|
| 13 |
+
# 1. Initialize App with Hugging Face proxy configurations
|
| 14 |
app = dash.Dash(
|
| 15 |
__name__,
|
| 16 |
external_stylesheets=[dbc.themes.FLATLY],
|
| 17 |
title="PvRecNet",
|
| 18 |
+
suppress_callback_exceptions=True,
|
| 19 |
+
# Crucial parameters for WSGI middleware hosting:
|
| 20 |
+
requests_pathname_prefix="/",
|
| 21 |
+
routes_pathname_prefix="/"
|
| 22 |
)
|
| 23 |
|
| 24 |
# 2. Define the Main App Layout (Navigation + Page Container)
|
|
|
|
| 28 |
html.Div(id='page-content') # Where different pages will be rendered
|
| 29 |
])
|
| 30 |
|
|
|
|
| 31 |
# --- PAGE 1: THE HELP / GUIDELINE PAGE ---
|
| 32 |
def render_help_page():
|
| 33 |
return dbc.Container([
|
|
|
|
| 42 |
html.H1("System Guidelines & Documentation", className="display-5 text-primary"),
|
| 43 |
html.P([
|
| 44 |
"PvRecNet is a deep learning-based model for ",
|
| 45 |
+
html.B(html.I("Plasmodium vivax")),
|
| 46 |
" recurrence classification."
|
| 47 |
], className="lead"),
|
| 48 |
], className="mb-5"),
|
| 49 |
|
| 50 |
+
# Visual Appeal: Use Cards for Sections
|
| 51 |
dbc.Row([
|
| 52 |
# Section: Description
|
| 53 |
dbc.Col([
|
|
|
|
| 56 |
"Our architecture differentiates between ",
|
| 57 |
html.B("Recrudescence, Relapse, and Reinfection"),
|
| 58 |
". It is trained on probabilistic outputs from ",
|
| 59 |
+
html.A("Pv3Rs", href="https://github.com/aimeertaylor/Pv3Rs", target="_blank"),
|
| 60 |
" to ensure robust genomic classification."
|
| 61 |
]),
|
| 62 |
], md=6),
|
|
|
|
| 86 |
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."]),
|
| 87 |
html.Li([html.B("Generalization: "), "The model may not generalize well to genetic markers or alleles unobserved during training."]),
|
| 88 |
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."]),
|
|
|
|
| 89 |
], className="text-secondary"),
|
| 90 |
|
| 91 |
+
# Sample Dataset Section
|
| 92 |
dbc.Card([
|
| 93 |
dbc.CardBody([
|
| 94 |
dbc.Row([
|
|
|
|
| 119 |
return dbc.Container([
|
| 120 |
dbc.Row([
|
| 121 |
dbc.Col(html.H1("PvRecNet", className="text-center my-4", style={"color":'#113d59'}), width=10),
|
|
|
|
| 122 |
dbc.Col(html.A("Help & Guidelines", href="/help", className="text-muted", style={"marginTop": "30px", "display": "block"}), width=2)
|
| 123 |
]),
|
| 124 |
|
|
|
|
| 174 |
], className="mt-4"),
|
| 175 |
dbc.Row(dbc.Col(dbc.Card([dbc.CardHeader(html.B("Step 3: Model Results")), dbc.CardBody(dcc.Loading(id="prediction-output"))], className="mt-4 mb-5"), width=12))
|
| 176 |
]),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
])
|
| 178 |
], fluid=True)
|
| 179 |
|
|
|
|
| 183 |
prevent_initial_call=True,
|
| 184 |
)
|
| 185 |
def download_sample_data(n_clicks):
|
| 186 |
+
# Safe path constructor that works perfectly both locally and inside the Hugging Face Docker Container
|
| 187 |
+
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 188 |
+
sample_path = os.path.join(base_dir, 'backend', 'PvDeepSetNet', 'datasets', 'GUI_sample_dataset.csv')
|
| 189 |
+
|
| 190 |
+
sample_df = pd.read_csv(sample_path)
|
| 191 |
return dcc.send_data_frame(sample_df.to_csv, "pvrecnet_sample_data.csv", index=False)
|
| 192 |
|
| 193 |
# --- ROUTER CALLBACK ---
|
|
|
|
| 210 |
def navigate_to_predict(n_clicks):
|
| 211 |
return "tab-predict" if n_clicks else "tab-intro"
|
| 212 |
|
| 213 |
+
# --- FUNCTIONAL CALLBACKS ---
|
| 214 |
|
| 215 |
@app.callback(
|
| 216 |
[Output('dataset-id-store', 'data'), Output('upload-status', 'children'), Output('predict-btn', 'disabled'),
|
|
|
|
| 234 |
State('dataset-id-store', 'data'),
|
| 235 |
prevent_initial_call=True
|
| 236 |
)
|
|
|
|
| 237 |
def run_predict(n_clicks, dataset_id):
|
| 238 |
try:
|
| 239 |
res = PvRecNetAPI.run_prediction(dataset_id)
|
| 240 |
fig1 = json.loads(res['plot_json1'])
|
| 241 |
fig2 = json.loads(res['plot_json2'])
|
| 242 |
|
|
|
|
| 243 |
df = pd.DataFrame(res['results_table'])
|
| 244 |
+
prob_cols = ['C_marg', 'L_marg', 'I_marg']
|
|
|
|
|
|
|
| 245 |
|
|
|
|
| 246 |
for col in prob_cols:
|
| 247 |
if col in df.columns:
|
| 248 |
df[col] = df[col].map(lambda x: f"{x:.4f}" if pd.notnull(x) else x)
|
| 249 |
|
|
|
|
| 250 |
table = dash_table.DataTable(
|
| 251 |
data=df.to_dict('records'),
|
| 252 |
columns=[{"name": i, "id": i} for i in df.columns],
|
| 253 |
export_format='csv',
|
| 254 |
page_size=10,
|
| 255 |
style_table={'height': '400px', 'overflowY': 'auto'},
|
| 256 |
+
style_cell={'textAlign': 'left'}
|
| 257 |
)
|
| 258 |
|
| 259 |
return html.Div([
|
|
|
|
| 265 |
except Exception as e:
|
| 266 |
return dbc.Alert(f"Error: {str(e)}", color="danger")
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
if __name__ == '__main__':
|
| 269 |
+
# Local fallback runner
|
| 270 |
+
app.run_server(debug=True, port=8050)
|