-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
426 lines (380 loc) · 14.9 KB
/
app.py
File metadata and controls
426 lines (380 loc) · 14.9 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
from __future__ import annotations
import base64
import io
import os
import pandas as pd
from dash import Dash, html, dcc, Input, Output, State, dash_table, no_update
from phenotyper.extract import extract_note
from phenotyper.aggregate import aggregate_patient
APP_TITLE = "Breast Cancer Phenotyper (medspaCy/scispaCy MVP)"
def parse_txt_upload(contents: str) -> str:
"""Dash upload contents: 'data:...;base64,XXXXX'"""
if not contents:
return ""
_, content_string = contents.split(",", 1)
decoded = base64.b64decode(content_string)
try:
return decoded.decode("utf-8")
except UnicodeDecodeError:
return decoded.decode("latin-1")
def df_from_upload_csv(contents: str) -> pd.DataFrame:
_, content_string = contents.split(",", 1)
decoded = base64.b64decode(content_string)
return pd.read_csv(io.BytesIO(decoded))
def normalize_mapping_df(df: pd.DataFrame) -> pd.DataFrame:
df = df.copy()
df.columns = [c.strip() for c in df.columns]
cols = set(df.columns)
if "note_id" not in cols and "filename" not in cols:
raise ValueError("Mapping CSV must include 'note_id' or 'filename'.")
if "patient_id" not in cols:
raise ValueError("Mapping CSV must include 'patient_id'.")
for c in ["note_date", "note_type"]:
if c not in cols:
df[c] = None
return df
app = Dash(__name__, title=APP_TITLE)
server = app.server
app.layout = html.Div(
style={"maxWidth": "1200px", "margin": "18px auto", "padding": "0 10px"},
children=[
html.H1(APP_TITLE),
html.Div(
"Upload multiple .txt notes. Optionally upload a mapping CSV (note_id/filename → patient_id, note_date, note_type).",
className="small",
),
html.Div(
className="row",
children=[
html.Div(
className="card col",
children=[
html.H3("1) Upload notes (.txt)"),
dcc.Upload(
id="upload-notes",
children=html.Div(["Drag and drop or ", html.A("select notes")]),
multiple=True,
style={
"width": "100%",
"height": "80px",
"lineHeight": "80px",
"borderWidth": "2px",
"borderStyle": "dashed",
"borderRadius": "12px",
"textAlign": "center",
},
),
html.Div(id="notes-status", className="small", style={"marginTop": "8px"}),
],
),
html.Div(
className="card col",
children=[
html.H3("2) Upload mapping CSV (optional)"),
dcc.Upload(
id="upload-mapping",
children=html.Div(["Drag and drop or ", html.A("select mapping CSV")]),
multiple=False,
style={
"width": "100%",
"height": "80px",
"lineHeight": "80px",
"borderWidth": "2px",
"borderStyle": "dashed",
"borderRadius": "12px",
"textAlign": "center",
},
),
html.Div(id="mapping-status", className="small", style={"marginTop": "8px"}),
],
),
],
),
html.Div(
className="card",
style={"marginTop": "12px"},
children=[
html.H3("3) Run extraction + patient aggregation"),
html.Div(
className="row",
children=[
html.Div(
className="col",
children=[
html.Label("spaCy base model name"),
dcc.Input(
id="model-name",
value="en_core_web_sm",
type="text",
style={"width": "100%"},
),
html.Div(
"Tip: start with en_core_web_sm. You can later swap to a scispaCy model.",
className="small",
),
],
),
html.Div(
className="col",
children=[
html.Label(""),
html.Button(
"Run extraction",
id="btn-run",
n_clicks=0,
style={"width": "100%", "height": "40px", "marginTop": "22px"},
),
html.Div(id="run-status", className="small", style={"marginTop": "8px"}),
],
),
],
),
],
),
dcc.Store(id="store-patient"),
dcc.Store(id="store-evidence"),
dcc.Store(id="store-notesmeta"),
html.Div(
className="card",
style={"marginTop": "12px"},
children=[
html.H3("Patient phenotypes"),
dash_table.DataTable(
id="tbl-patient",
page_size=12,
filter_action="native",
sort_action="native",
row_selectable="single",
style_table={"overflowX": "auto"},
style_cell={"textAlign": "left", "fontSize": "13px", "padding": "6px"},
style_header={"fontWeight": "700"},
),
html.Div(className="small", children="Select a patient row to see evidence mentions below."),
],
),
html.Div(
className="card",
style={"marginTop": "12px"},
children=[
html.H3("Evidence (mentions) for selected patient"),
dash_table.DataTable(
id="tbl-evidence",
page_size=10,
filter_action="native",
sort_action="native",
style_table={"overflowX": "auto"},
style_cell={
"textAlign": "left",
"fontSize": "13px",
"padding": "6px",
"whiteSpace": "normal",
"height": "auto",
},
style_header={"fontWeight": "700"},
),
],
),
html.Div(
className="card",
style={"marginTop": "12px"},
children=[
html.H3("Export"),
html.Div(
className="row",
children=[
html.Div(
className="col",
children=[
html.Button("Download patient table CSV", id="btn-dl-patient"),
dcc.Download(id="dl-patient"),
],
),
html.Div(
className="col",
children=[
html.Button("Download evidence CSV", id="btn-dl-evidence"),
dcc.Download(id="dl-evidence"),
],
),
],
),
],
),
],
)
@app.callback(
Output("notes-status", "children"),
Input("upload-notes", "filename"),
)
def show_notes_status(filenames):
if not filenames:
return "No notes uploaded yet."
return f"{len(filenames)} note(s) ready: " + ", ".join(filenames[:6]) + (" ..." if len(filenames) > 6 else "")
@app.callback(
Output("mapping-status", "children"),
Input("upload-mapping", "filename"),
)
def show_mapping_status(filename):
if not filename:
return "No mapping uploaded (optional)."
return f"Mapping file ready: {filename}"
@app.callback(
Output("store-patient", "data"),
Output("store-evidence", "data"),
Output("store-notesmeta", "data"),
Output("run-status", "children"),
Input("btn-run", "n_clicks"),
State("upload-notes", "contents"),
State("upload-notes", "filename"),
State("upload-mapping", "contents"),
State("upload-mapping", "filename"),
State("model-name", "value"),
prevent_initial_call=True,
)
def run_pipeline(n_clicks, note_contents, note_filenames, mapping_contents, mapping_filename, model_name):
if not note_contents or not note_filenames:
return no_update, no_update, no_update, "Please upload at least one .txt note."
# Load mapping if present
mapping_df = None
if mapping_contents:
try:
mapping_df = normalize_mapping_df(df_from_upload_csv(mapping_contents))
except Exception as e:
return no_update, no_update, no_update, f"Mapping CSV error: {e}"
# Build per-note metadata table
rows_meta = []
for fn in note_filenames:
note_id = os.path.splitext(fn)[0]
patient_id = note_id
note_date = None
note_type = "Unknown"
if mapping_df is not None:
if "note_id" in mapping_df.columns:
hit = mapping_df[mapping_df["note_id"].astype(str) == str(note_id)]
else:
hit = mapping_df[mapping_df["filename"].astype(str) == str(fn)]
if len(hit) >= 1:
h = hit.iloc[0]
patient_id = str(h["patient_id"])
note_date = None if pd.isna(h.get("note_date")) else str(h.get("note_date"))
note_type = "Unknown" if pd.isna(h.get("note_type")) else str(h.get("note_type"))
rows_meta.append(
{
"filename": fn,
"note_id": note_id,
"patient_id": patient_id,
"note_date": note_date,
"note_type": note_type,
}
)
meta_df = pd.DataFrame(rows_meta)
# Extract note-level phenotypes + evidence
note_pheno_rows = []
ev_rows = [] # dicts for UI table + downloads
for contents, fn in zip(note_contents, note_filenames):
note_text = parse_txt_upload(contents)
meta = meta_df[meta_df["filename"] == fn].iloc[0].to_dict()
phenos, evidence = extract_note(
note_text,
patient_id=meta["patient_id"],
note_id=meta["note_id"],
note_date=meta.get("note_date"),
note_type=meta.get("note_type"),
model_name=model_name or "en_core_web_sm",
)
note_pheno_rows.append(phenos)
ev_rows.extend([e.to_dict() for e in evidence])
note_pheno_df = pd.DataFrame(note_pheno_rows)
ev_df = pd.DataFrame(ev_rows) if ev_rows else pd.DataFrame()
# Rebuild Evidence objects for aggregation scoring (so aggregate_patient can use evidence flags)
evidence_objs = []
if not ev_df.empty:
# Evidence dataclass in your project expects:
# patient_id, note_id, note_date, note_type, field, value, start, end, snippet, label, confidence, is_negated, is_uncertain
# We'll pass only what exists; missing keys default safely.
for r in ev_df.to_dict("records"):
try:
from phenotyper.evidence import Evidence # local import avoids circulars
evidence_objs.append(Evidence(**r))
except Exception:
# If evidence schema differs, aggregation will still work (it just won't evidence-score).
pass
# Aggregate by patient (IMPORTANT: pass evidence_objs, not [])
patient_rows = []
for pid, grp in note_pheno_df.groupby("patient_id", dropna=False):
patient_rows.append(aggregate_patient(grp.to_dict("records"), evidence_objs))
patient_df = pd.DataFrame(patient_rows)
return (
patient_df.to_dict("records"),
ev_rows,
meta_df.to_dict("records"),
f"Done. Notes processed: {len(note_filenames)} | Patients: {patient_df.shape[0]} | Mentions: {len(ev_rows)}",
)
@app.callback(
Output("tbl-patient", "data"),
Output("tbl-patient", "columns"),
Input("store-patient", "data"),
)
def render_patient_table(rows):
if not rows:
return [], []
df = pd.DataFrame(rows)
cols = [{"name": c, "id": c} for c in df.columns]
return df.to_dict("records"), cols
@app.callback(
Output("tbl-evidence", "data"),
Output("tbl-evidence", "columns"),
Input("tbl-patient", "selected_rows"),
State("tbl-patient", "data"),
State("store-evidence", "data"),
)
def render_evidence(selected_rows, patient_rows, ev_rows):
if not ev_rows:
return [], []
ev_df = pd.DataFrame(ev_rows)
if not selected_rows or not patient_rows:
cols = [{"name": c, "id": c} for c in ev_df.columns]
return ev_df.to_dict("records"), cols
sel_idx = selected_rows[0]
pid = patient_rows[sel_idx].get("patient_id")
ev_df = ev_df[ev_df["patient_id"].astype(str) == str(pid)].copy()
preferred = [
"patient_id",
"note_id",
"note_date",
"note_type",
"field",
"value",
"label",
"confidence",
"is_negated",
"is_uncertain",
"snippet",
]
cols = [c for c in preferred if c in ev_df.columns] + [c for c in ev_df.columns if c not in preferred]
ev_df = ev_df[cols]
return ev_df.to_dict("records"), [{"name": c, "id": c} for c in ev_df.columns]
@app.callback(
Output("dl-patient", "data"),
Input("btn-dl-patient", "n_clicks"),
State("store-patient", "data"),
prevent_initial_call=True,
)
def download_patient(n, rows):
if not rows:
return no_update
df = pd.DataFrame(rows)
return dcc.send_data_frame(df.to_csv, "patient_phenotypes_v1.csv", index=False)
@app.callback(
Output("dl-evidence", "data"),
Input("btn-dl-evidence", "n_clicks"),
State("store-evidence", "data"),
prevent_initial_call=True,
)
def download_evidence(n, rows):
if not rows:
return no_update
df = pd.DataFrame(rows)
return dcc.send_data_frame(df.to_csv, "extraction_evidence.csv", index=False)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=8050)