carisackc commited on
Commit
4ab7d39
1 Parent(s): 9e85dc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -149
app.py CHANGED
@@ -12,15 +12,21 @@ from spacy.matcher import PhraseMatcher
12
  from spacy.tokens import Span
13
  #import en_ner_bc5cdr_md
14
  import re
15
-
16
-
17
  from streamlit.components.v1 import html
18
 
19
- # Store the initial value of widgets in session state
20
- if "visibility" not in st.session_state:
21
- st.session_state.visibility = "visible"
22
- st.session_state.disabled = False
 
 
 
 
23
 
 
 
 
 
24
  #nlp = en_core_web_lg.load()
25
  nlp = spacy.load("en_ner_bc5cdr_md")
26
 
@@ -108,9 +114,8 @@ countOfAdmission = len(pastHistoryEpDate)
108
 
109
 
110
  # List of Model available
111
- #model = st.sidebar.selectbox('Select Model', ('BertSummarizer'))
112
  st.sidebar.markdown("Model: BertSummarizer")
113
- model = 'BertSummarizer'
114
 
115
  # ===== to display selected patient and admission id on main page
116
  col3,col4 = st.columns(2)
@@ -137,6 +142,7 @@ dailyNoteChange.rename(columns={'STORETIME':'Time of Record',
137
  dailyNote = df4['Full Text'].loc[(df4['Patient_ID'] == patient) & (df4['Admission_ID']==HospitalAdmission)]
138
  dailyNote = dailyNote.unique()
139
 
 
140
  ##========= Buttons to the 5 tabs ======== Temp disabled Discharge Plan and Social Notes
141
  ##col1, col2, col3, col4, col5 = st.columns([1,1,1,1,1]) -- to uncomment and comment below line to include discharge plan and social notes
142
  col1, col2, col5 = st.columns([1,1,1])
@@ -144,14 +150,11 @@ col6, col7 =st.columns([2,2])
144
  with st.container():
145
  with col1:
146
  btnAdmission = st.button("🏥 Admission")
147
- if btnAdmission:
148
- #nav_page('Admission')
149
- inputNote = "Input Admission Note"
150
  with col2:
151
  btnDailyNarrative = st.button('📆Daily Narrative')
152
- if btnDailyNarrative:
153
- inputNote = "Input Daily Narrative Note"
154
- # with col3:
155
  # btnDischargePlan = st.button('🗒️Discharge Plan')
156
  # if btnDischargePlan:
157
  # inputNote = "Input Discharge Plan"
@@ -161,62 +164,11 @@ with st.container():
161
  # inputNote = "Input Social Note"
162
  with col5:
163
  btnPastHistory = st.button('📇Past History (6 Mths)')
164
-
165
-
166
-
167
- ##========= on Past History Tab =========
168
 
169
- with st.container():
170
- if btnPastHistory:
171
- with col6:
172
- st.markdown('**No. of admission past 6 months:**')
173
- st.markdown(countOfAdmission)
174
-
175
- with col7:
176
- #st.date_input('Select Admission Date') # To replace with a dropdown filter instead
177
- #st.selectbox('Past Episodes',pastHistoryEp)
178
- pastHistory = st.selectbox('Select Past History Admission', pastHistoryEpDate, format_func=lambda x: 'Select an option' if x == '' else x)
179
-
180
- if btnPastHistory:
181
-
182
- #st.write('Past History')
183
- historyAdmission = df3.query(
184
- "Patient_ID == @patient & CHARTDATE_HADM_ID == @pastHistory"
185
- )
186
- runtext = historyAdmission['hospital_course_processed'].values[0]
187
 
188
- if not(btnPastHistory) and not(btnDailyNarrative):
189
- runtext =st.text_area(inputNote, str(original_text2)[1:-1], height=300)
190
-
191
-
192
-
193
-
194
- ##========= END on Past History Tab =========
195
-
196
- ## ===== Commented out as no longer in use =====
197
- # Extract words associated with each entity
198
- #def genEntities(ann, entity):
199
- # # entity colour dict
200
- # #ent_col = {'DISEASE':'#B42D1B', 'CHEMICAL':'#F06292'}
201
- # ent_col = {'DISEASE':'pink', 'CHEMICAL':'orange'}
202
- # # separate into the different entities
203
- # entities = trans_df['Class'].unique()
204
- #
205
- # if entity in entities:
206
- # ent = list(trans_df[trans_df['Class']==entity]['Entity'].unique())
207
- # entlist = ",".join(ent)
208
- # st.markdown(f'<p style="background-color:{ent_col[entity]};color:#080808;font-size:16px;">{entlist}</p>', #unsafe_allow_html=True)
209
-
210
- ##===== BERT Summary tokenizer =====
211
 
212
  ##======================== Start of NER Tagging ========================
213
- # ====== Old NER ======
214
- # doc = nlp(str(original_text2))
215
- # colors = { "DISEASE": "pink","CHEMICAL": "orange"}
216
- # options = {"ents": [ "DISEASE", "CHEMICAL"],"colors": colors}
217
- # ent_html = displacy.render(doc, style="ent", options=options)
218
- # ====== End of Old NER ======
219
-
220
  #lemmatizing the notes to capture all forms of negation(e.g., deny: denies, denying)
221
  def lemmatize(note, nlp):
222
  doc = nlp(note)
@@ -284,6 +236,63 @@ def dedupe(items):
284
  yield item
285
  seen.add(item)
286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  lem_clinical_note= lemmatize(runtext, nlp)
288
  #creating a doc object using BC5CDR model
289
  doc = nlp(lem_clinical_note)
@@ -300,112 +309,104 @@ doc0 = overwrite_ent_lbl(matcher,doc)
300
  #visualizing identified Named Entities in clinical input text
301
  ent_html = displacy.render(doc0, style='ent', options=options)
302
 
303
- ##======================== End of NER Tagging ========================
304
-
305
- def run_model(input_text):
306
- if model == "BertSummarizer":
307
- output = original_text['BertSummarizer2s'].values
308
- st.write('Summary on History of Presenting illness')
309
-
310
- # elif model == "BertGPT2":
311
- # output = original_text['BertGPT2'].values
312
- # st.write('Summary')
313
- #
314
- # elif model == "t5seq2eq":
315
- # output = original_text['t5seq2eq'].values
316
- # st.write('Summary')
317
- #
318
- # elif model == "t5":
319
- # output = original_text['t5'].values
320
- # st.write('Summary')
321
- #
322
- # elif model == "gensim":
323
- # output = original_text['gensim'].values
324
- # st.write('Summary')
325
- #
326
- # elif model == "pysummarizer":
327
- # output = original_text['pysummarizer'].values
328
- # st.write('Summary')#
329
-
330
 
331
 
332
- st.success(output)
333
-
334
-
335
  col1, col2 = st.columns([1,1])
336
 
337
  #to not show summary and references text for Past History and Daily Narrative
338
- if not(btnPastHistory) and not(btnDailyNarrative):
339
- with col1:
340
- st.button('Summarize')
341
- run_model(runtext)
342
- #sentences=runtext.split('.')
343
- st.text_area('Reference text', str(reference_text), height=150)
344
- with col2:
345
- st.button('NER')
346
- # ===== Adding the Disease/Chemical into a list =====
347
- problem_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'DISEASE']))
348
- medication_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'CHEMICAL']))
349
- st.markdown('**CHIEF COMPLAINT:**')
350
- st.write(str(AdmissionChiefCom)[1:-1])
351
- st.markdown('**ADMISSION DIAGNOSIS:**')
352
- st.markdown(str(diagnosis)[1:-1].capitalize())
353
- st.markdown('**PROBLEM/ISSUE**')
354
- #st.markdown(problem_entities)
355
- st.markdown(f'<p style="background-color:PINK;color:#080808;font-size:16px;">{str(problem_entities)[1:-1]}</p>', unsafe_allow_html=True)
356
- #genEntities(trans_df, 'DISEASE')
357
- st.markdown('**MEDICATION**')
358
- st.markdown(f'<p style="background-color:orange;color:#080808;font-size:16px;">{str(medication_entities)[1:-1]}</p>', unsafe_allow_html=True)
359
- #genEntities(trans_df, 'CHEMICAL')
360
- #st.table(trans_df)
361
- st.markdown('**NER**')
362
- with st.expander("See NER Details"):
363
- st.markdown(ent_html, unsafe_allow_html=True)
364
- if btnDailyNarrative:
 
 
 
 
 
 
 
 
 
 
 
 
 
365
  with st.container():
366
  st.markdown('Daily Progress Note (24 hour event only):')
367
  st.markdown(str(dailyNote)[1:-1])
368
 
369
 
370
  with st.container():
371
- # hide_table_row_index = """
372
- # <style>
373
- # thead tr th:first-child {display:none}
374
- # tbody th {display:none}
375
- # </style>
376
- # """
377
- #
378
- # # Inject CSS with Markdown
379
- # st.markdown(hide_table_row_index, unsafe_allow_html=True)
380
- # st.table(dailyNoteChange)
381
  styler = dailyNoteChange.style.hide_index()
382
  st.write(styler.to_html(), unsafe_allow_html=True)
383
  st.markdown(f'<p style="color:#828080;font-size:12px;">*Current prototype displays only a single section within the daily progress note, could also potentially include all sections within each progress note and allow user to select the section changes the user wants to look at</p>', unsafe_allow_html=True)
384
 
385
  #else:
386
- if btnPastHistory:
 
 
 
387
  # ===== Adding the Disease/Chemical into a list =====
388
  problem_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'DISEASE']))
389
  medication_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'CHEMICAL']))
390
- st.markdown('Admission Date: ' + historyAdmission['ADMITTIME'].values[0])
391
- st.markdown('Date of Discharge: ' + historyAdmission['DISCHTIME'].values[0])
392
- st.markdown('Days from current admission: ' + str(historyAdmission['days_from_index'].values[0]) +' days')
 
 
 
 
 
 
393
  #st.markdown('Summary: ')
394
  st.markdown(f'<p style="color:#080808;font-size:16px;"><b>Summary: </b></p>', unsafe_allow_html=True)
395
 
396
 
397
  if model == "BertSummarizer":
398
- st.markdown(str(historyAdmission['BertSummarizer'].values[0]))
 
 
 
399
  elif model == "t5seq2eq":
400
- st.markdown(str(historyAdmission['t5seq2eq'].values[0]))
 
 
 
401
  st.markdown(f'<p style="color:#080808;font-size:16px;"><b>Diagnosis: </b></p>', unsafe_allow_html=True)
402
- st.markdown(str(historyAdmission['Diagnosis_Description'].values[0]))
403
- st.markdown('**PROBLEM/ISSUE**')
404
- st.markdown(f'<p style="background-color:PINK;color:#080808;font-size:16px;">{str(problem_entities)[1:-1]}</p>', unsafe_allow_html=True)
405
- st.markdown('**MEDICATION**')
406
- st.markdown(f'<p style="background-color:orange;color:#080808;font-size:16px;">{str(medication_entities)[1:-1]}</p>', unsafe_allow_html=True)
407
- st.markdown('Discharge Disposition: ' + str(historyAdmission['DISCHARGE_LOCATION'].values[0]))
408
- with st.expander('Full Discharge Summary'):
409
- #st.write("line 1 \n line 2 \n line 3")
410
- fulldischargesummary = historyAdmission['TEXT'].values[0]
411
- st.write(fulldischargesummary)
 
 
 
 
 
12
  from spacy.tokens import Span
13
  #import en_ner_bc5cdr_md
14
  import re
 
 
15
  from streamlit.components.v1 import html
16
 
17
+ if "load_state" not in st.session_state:
18
+ st.session_state.load_state = False
19
+
20
+ #if "button_clicked" not in st.session_state:
21
+ # st.session_state.button_clicked = True
22
+ #
23
+ #if "daily_button_clicked" not in st.session_state:
24
+ # st.session_state.daily_button_clicked = False
25
 
26
+ if "past_button_clicked" not in st.session_state:
27
+ st.session_state.past_button_clicked = False
28
+
29
+
30
  #nlp = en_core_web_lg.load()
31
  nlp = spacy.load("en_ner_bc5cdr_md")
32
 
 
114
 
115
 
116
  # List of Model available
117
+ #model = st.sidebar.selectbox('Select Model', ('BertSummarizer','BertGPT2','t5seq2eq','t5','gensim','pysummarizer'))
118
  st.sidebar.markdown("Model: BertSummarizer")
 
119
 
120
  # ===== to display selected patient and admission id on main page
121
  col3,col4 = st.columns(2)
 
142
  dailyNote = df4['Full Text'].loc[(df4['Patient_ID'] == patient) & (df4['Admission_ID']==HospitalAdmission)]
143
  dailyNote = dailyNote.unique()
144
 
145
+
146
  ##========= Buttons to the 5 tabs ======== Temp disabled Discharge Plan and Social Notes
147
  ##col1, col2, col3, col4, col5 = st.columns([1,1,1,1,1]) -- to uncomment and comment below line to include discharge plan and social notes
148
  col1, col2, col5 = st.columns([1,1,1])
 
150
  with st.container():
151
  with col1:
152
  btnAdmission = st.button("🏥 Admission")
153
+ inputNote = "Input Admission Note"
154
+
 
155
  with col2:
156
  btnDailyNarrative = st.button('📆Daily Narrative')
157
+ # with col3:what
 
 
158
  # btnDischargePlan = st.button('🗒️Discharge Plan')
159
  # if btnDischargePlan:
160
  # inputNote = "Input Discharge Plan"
 
164
  # inputNote = "Input Social Note"
165
  with col5:
166
  btnPastHistory = st.button('📇Past History (6 Mths)')
 
 
 
 
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
  ##======================== Start of NER Tagging ========================
171
+
 
 
 
 
 
 
172
  #lemmatizing the notes to capture all forms of negation(e.g., deny: denies, denying)
173
  def lemmatize(note, nlp):
174
  doc = nlp(note)
 
236
  yield item
237
  seen.add(item)
238
 
239
+
240
+
241
+ ##======================== End of NER Tagging ========================
242
+
243
+ def run_model(input_text):
244
+ if model == "BertSummarizer":
245
+ output = original_text['BertSummarizer2s'].values
246
+ st.write('Summary on History of Presenting illness')
247
+
248
+
249
+
250
+ st.success(output)
251
+
252
+ ##========= on Past History Tab =========
253
+
254
+ if btnPastHistory or st.session_state["past_button_clicked"]:
255
+ #st.session_state["button_clicked"] = False
256
+ #st.session_state["daily_button_clicked"] = False
257
+ st.session_state["past_button_clicked"] = True
258
+
259
+ with st.container():
260
+ with col6:
261
+
262
+ st.markdown('**No. of admission past 6 months:**')
263
+ st.markdown(countOfAdmission)
264
+
265
+ with col7:
266
+ #st.date_input('Select Admission Date') # To replace with a dropdown filter instead
267
+ #st.selectbox('Past Episodes',pastHistoryEp)
268
+ pastHistory = st.selectbox('Select Past History Admission', pastHistoryEpDate, format_func=lambda x: 'Select an option' if x == '' else x)
269
+
270
+ historyAdmission = df3.query(
271
+ "Patient_ID == @patient & CHARTDATE_HADM_ID == @pastHistory"
272
+ )
273
+
274
+
275
+ if historyAdmission.shape[0] == 0:
276
+ runtext = "No past episodes"
277
+ else:
278
+ #runtext = historyAdmission['hospital_course_processed'].values[0]
279
+ runtext = historyAdmission['hospital_course_processed'].values[0]
280
+
281
+ if btnAdmission:
282
+ #st.session_state["daily_button_clicked"] = False
283
+ #st.session_state["past_button_clicked"] = False
284
+ #st.session_state["button_clicked"] = True
285
+ runtext =st.text_area(inputNote, str(original_text2)[1:-1], height=300)
286
+
287
+ #if btnDailyNarrative:
288
+ #st.session_state["button_clicked"] = False
289
+ #st.session_state["past_button_clicked"] = False
290
+ #st.session_state["daily_button_clicked"] = True
291
+
292
+
293
+
294
+
295
+
296
  lem_clinical_note= lemmatize(runtext, nlp)
297
  #creating a doc object using BC5CDR model
298
  doc = nlp(lem_clinical_note)
 
309
  #visualizing identified Named Entities in clinical input text
310
  ent_html = displacy.render(doc0, style='ent', options=options)
311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
 
313
 
 
 
 
314
  col1, col2 = st.columns([1,1])
315
 
316
  #to not show summary and references text for Past History and Daily Narrative
317
+ if btnAdmission :
318
+
319
+ #st.session_state["daily_button_clicked"] = False
320
+ st.session_state["past_button_clicked"] = False
321
+ #st.session_state["button_clicked"] = True
322
+
323
+
324
+ with st.container():
325
+ with col1:
326
+ st.button('Summarize')
327
+ run_model(runtext)
328
+ #sentences=runtext.split('.')
329
+ st.text_area('Reference text', str(reference_text), height=150)
330
+ with col2:
331
+ st.button('NER')
332
+ # ===== Adding the Disease/Chemical into a list =====
333
+ problem_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'DISEASE']))
334
+ medication_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'CHEMICAL']))
335
+ st.markdown('**CHIEF COMPLAINT:**')
336
+ st.write(str(AdmissionChiefCom)[1:-1])
337
+ st.markdown('**ADMISSION DIAGNOSIS:**')
338
+ st.markdown(str(diagnosis)[1:-1].capitalize())
339
+ st.markdown('**PROBLEM/ISSUE**')
340
+ #st.markdown(problem_entities)
341
+ st.markdown(f'<p style="background-color:PINK;color:#080808;font-size:16px;">{str(problem_entities)[1:-1]}</p>', unsafe_allow_html=True)
342
+ #genEntities(trans_df, 'DISEASE')
343
+ st.markdown('**MEDICATION**')
344
+ st.markdown(f'<p style="background-color:orange;color:#080808;font-size:16px;">{str(medication_entities)[1:-1]}</p>', unsafe_allow_html=True)
345
+ #genEntities(trans_df, 'CHEMICAL')
346
+ #st.table(trans_df)
347
+ st.markdown('**NER**')
348
+ with st.expander("See NER Details"):
349
+ st.markdown(ent_html, unsafe_allow_html=True)
350
+
351
+
352
+ elif btnDailyNarrative :
353
+ # st.session_state["daily_button_clicked"] = True
354
+ st.session_state["past_button_clicked"] = False
355
+ # st.session_state["button_clicked"] = False
356
+
357
  with st.container():
358
  st.markdown('Daily Progress Note (24 hour event only):')
359
  st.markdown(str(dailyNote)[1:-1])
360
 
361
 
362
  with st.container():
 
 
 
 
 
 
 
 
 
 
363
  styler = dailyNoteChange.style.hide_index()
364
  st.write(styler.to_html(), unsafe_allow_html=True)
365
  st.markdown(f'<p style="color:#828080;font-size:12px;">*Current prototype displays only a single section within the daily progress note, could also potentially include all sections within each progress note and allow user to select the section changes the user wants to look at</p>', unsafe_allow_html=True)
366
 
367
  #else:
368
+ elif btnPastHistory or st.session_state["past_button_clicked"]:
369
+ st.session_state["past_button_clicked"] = True
370
+ # st.session_state["button_clicked"] = False
371
+ # st.session_state["daily_button_clicked"] = False
372
  # ===== Adding the Disease/Chemical into a list =====
373
  problem_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'DISEASE']))
374
  medication_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'CHEMICAL']))
375
+ if historyAdmission.shape[0] == 0:
376
+ st.markdown('Admission Date: NA')
377
+ st.markdown('Date of Discharge: NA')
378
+ st.markdown('Days from current admission: NA')
379
+ else:
380
+ st.markdown('Admission Date: ' + historyAdmission['ADMITTIME'].values[0])
381
+ st.markdown('Date of Discharge: ' + historyAdmission['DISCHTIME'].values[0])
382
+ st.markdown('Days from current admission: ' + str(historyAdmission['days_from_index'].values[0]) +' days')
383
+
384
  #st.markdown('Summary: ')
385
  st.markdown(f'<p style="color:#080808;font-size:16px;"><b>Summary: </b></p>', unsafe_allow_html=True)
386
 
387
 
388
  if model == "BertSummarizer":
389
+ if historyAdmission.shape[0] == 0:
390
+ st.markdown('NA')
391
+ else:
392
+ st.markdown(str(historyAdmission['BertSummarizer'].values[0]))
393
  elif model == "t5seq2eq":
394
+ if historyAdmission.shape[0] == 0:
395
+ st.markdown('NA')
396
+ else:
397
+ st.markdown(str(historyAdmission['t5seq2eq'].values[0]))
398
  st.markdown(f'<p style="color:#080808;font-size:16px;"><b>Diagnosis: </b></p>', unsafe_allow_html=True)
399
+
400
+ if historyAdmission.shape[0] == 0:
401
+ st.markdown('NA')
402
+ else:
403
+ st.markdown(str(historyAdmission['Diagnosis_Description'].values[0]))
404
+ st.markdown('**PROBLEM/ISSUE**')
405
+ st.markdown(f'<p style="background-color:PINK;color:#080808;font-size:16px;">{str(problem_entities)[1:-1]}</p>', unsafe_allow_html=True)
406
+ st.markdown('**MEDICATION**')
407
+ st.markdown(f'<p style="background-color:orange;color:#080808;font-size:16px;">{str(medication_entities)[1:-1]}</p>', unsafe_allow_html=True)
408
+ st.markdown('Discharge Disposition: ' + str(historyAdmission['DISCHARGE_LOCATION'].values[0]))
409
+ with st.expander('Full Discharge Summary'):
410
+ #st.write("line 1 \n line 2 \n line 3")
411
+ fulldischargesummary = historyAdmission['TEXT'].values[0]
412
+ st.write(fulldischargesummary)