Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -5,11 +5,11 @@ from math import ceil
|
|
5 |
from collections import Counter
|
6 |
from string import punctuation
|
7 |
import spacy
|
|
|
8 |
from spacy import displacy
|
9 |
from spacy.lang.en import English
|
10 |
from spacy.matcher import PhraseMatcher
|
11 |
from spacy.tokens import Span
|
12 |
-
from negspacy.negation import Negex
|
13 |
#import en_ner_bc5cdr_md
|
14 |
import re
|
15 |
|
@@ -63,6 +63,12 @@ del df2
|
|
63 |
# Remove decimal point from Admission ID
|
64 |
df['HADM_ID'] = df['HADM_ID'].astype(str).apply(lambda x: x.replace('.0',''))
|
65 |
df3['HADM_ID'] = df3['HADM_ID'].astype(str).apply(lambda x: x.replace('.0',''))
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
#Renaming column
|
68 |
df.rename(columns={'SUBJECT_ID':'Patient_ID',
|
@@ -70,7 +76,8 @@ df.rename(columns={'SUBJECT_ID':'Patient_ID',
|
|
70 |
'hpi_input_text':'Original_Text',
|
71 |
'hpi_reference_summary':'Reference_text'}, inplace = True)
|
72 |
df3.rename(columns={'SUBJECT_ID':'Patient_ID',
|
73 |
-
'HADM_ID':'
|
|
|
74 |
|
75 |
#Filter selection
|
76 |
st.sidebar.header("Search for Patient:")
|
@@ -79,8 +86,8 @@ st.sidebar.header("Search for Patient:")
|
|
79 |
patientid = df['Patient_ID']
|
80 |
patient = st.sidebar.selectbox('Select Patient ID:', patientid) #Filter Patient
|
81 |
admissionid = df['Admission_ID'].loc[df['Patient_ID'] == patient] #Filter available Admission id for patient
|
82 |
-
HospitalAdmission = st.sidebar.selectbox(' ', admissionid)
|
83 |
-
|
84 |
|
85 |
|
86 |
# List of Model available
|
@@ -108,7 +115,7 @@ reference_text = original_text['Reference_text'].values
|
|
108 |
##========= Buttons to the 5 tabs ======== Temp disabled Discharge Plan and Social Notes
|
109 |
##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
|
110 |
col1, col2, col5 = st.columns([1,1,1])
|
111 |
-
col6, col7
|
112 |
with st.container():
|
113 |
with col1:
|
114 |
btnAdmission = st.button("🏥 Admission")
|
@@ -133,41 +140,53 @@ with st.container():
|
|
133 |
inputNote = "Input History records"
|
134 |
|
135 |
##========= on Past History Tab =========
|
136 |
-
|
137 |
-
st.text_area('Past History','Date of discharge: xxxxxxxxx')
|
138 |
-
|
139 |
-
else:
|
140 |
-
runtext =st.text_area(inputNote, str(original_text2), height=300)
|
141 |
-
|
142 |
-
|
143 |
with st.container():
|
144 |
if btnPastHistory:
|
145 |
with col6:
|
146 |
st.markdown('**No. of admission past 6 months: xx**')
|
147 |
-
|
148 |
-
|
149 |
-
with col8:
|
150 |
#st.date_input('Select Admission Date') # To replace with a dropdown filter instead
|
151 |
#st.selectbox('Past Episodes',pastHistoryEp)
|
152 |
-
pastHistory = st.selectbox(' ',
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
|
156 |
##========= END on Past History Tab =========
|
157 |
|
158 |
-
|
159 |
# Extract words associated with each entity
|
160 |
-
def genEntities(ann, entity):
|
161 |
-
# entity colour dict
|
162 |
-
#ent_col = {'DISEASE':'#B42D1B', 'CHEMICAL':'#F06292'}
|
163 |
-
ent_col = {'DISEASE':'pink', 'CHEMICAL':'orange'}
|
164 |
-
# separate into the different entities
|
165 |
-
entities = trans_df['Class'].unique()
|
166 |
-
|
167 |
-
if entity in entities:
|
168 |
-
ent = list(trans_df[trans_df['Class']==entity]['Entity'].unique())
|
169 |
-
entlist = ",".join(ent)
|
170 |
-
st.markdown(f'<p style="background-color:{ent_col[entity]};color:#080808;font-size:16px;">{entlist}</p>', unsafe_allow_html=True)
|
171 |
|
172 |
|
173 |
##======================== Start of NER Tagging ========================
|
@@ -304,12 +323,11 @@ with col1:
|
|
304 |
st.text_area('Reference text', str(reference_text), height=150)
|
305 |
else:
|
306 |
with st.expander('Full Discharge Summary'):
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
##====== Storing the Diseases/Text
|
313 |
# table= {"Entity":[], "Class":[]}
|
314 |
# ent_bc = {}
|
315 |
# for x in doc.ents:
|
@@ -319,20 +337,23 @@ with col1:
|
|
319 |
# table["Class"].append(ent_bc[key])
|
320 |
# trans_df = pd.DataFrame(table)
|
321 |
|
|
|
322 |
problem_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'DISEASE']))
|
323 |
medication_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'CHEMICAL']))
|
324 |
|
325 |
with col2:
|
326 |
st.button('NER')
|
327 |
st.markdown('**CHIEF COMPLAINT:**')
|
328 |
-
st.write(str(AdmissionChiefCom))
|
329 |
st.markdown('**ADMISSION DIAGNOSIS:**')
|
330 |
-
st.markdown(str(diagnosis))
|
331 |
st.markdown('**PROBLEM/ISSUE**')
|
332 |
#st.markdown(problem_entities)
|
333 |
-
|
|
|
334 |
st.markdown('**MEDICATION**')
|
335 |
-
|
|
|
336 |
#st.table(trans_df)
|
337 |
st.markdown('**NER**')
|
338 |
with st.expander("See NER Details"):
|
|
|
5 |
from collections import Counter
|
6 |
from string import punctuation
|
7 |
import spacy
|
8 |
+
from negspacy.negation import Negex
|
9 |
from spacy import displacy
|
10 |
from spacy.lang.en import English
|
11 |
from spacy.matcher import PhraseMatcher
|
12 |
from spacy.tokens import Span
|
|
|
13 |
#import en_ner_bc5cdr_md
|
14 |
import re
|
15 |
|
|
|
63 |
# Remove decimal point from Admission ID
|
64 |
df['HADM_ID'] = df['HADM_ID'].astype(str).apply(lambda x: x.replace('.0',''))
|
65 |
df3['HADM_ID'] = df3['HADM_ID'].astype(str).apply(lambda x: x.replace('.0',''))
|
66 |
+
df3['INDEX_HADM_ID'] = df3['INDEX_HADM_ID'].astype(str).apply(lambda x: x.replace('.0',''))
|
67 |
+
df3["CHARTDATE_HADM_ID"] = df3["CHARTDATE"].astype(str) +' - '+ df3["HADM_ID"]
|
68 |
+
df3["DIAGNOSIS"] = df3["DIAGNOSIS"].str.capitalize()
|
69 |
+
|
70 |
+
df3["TEXT"] =df3["TEXT"].replace(r'\n',' \n ', regex=True)
|
71 |
+
|
72 |
|
73 |
#Renaming column
|
74 |
df.rename(columns={'SUBJECT_ID':'Patient_ID',
|
|
|
76 |
'hpi_input_text':'Original_Text',
|
77 |
'hpi_reference_summary':'Reference_text'}, inplace = True)
|
78 |
df3.rename(columns={'SUBJECT_ID':'Patient_ID',
|
79 |
+
'HADM_ID':'PAST_Admission_ID',
|
80 |
+
'INDEX_HADM_ID':'Admission_ID'}, inplace = True)
|
81 |
|
82 |
#Filter selection
|
83 |
st.sidebar.header("Search for Patient:")
|
|
|
86 |
patientid = df['Patient_ID']
|
87 |
patient = st.sidebar.selectbox('Select Patient ID:', patientid) #Filter Patient
|
88 |
admissionid = df['Admission_ID'].loc[df['Patient_ID'] == patient] #Filter available Admission id for patient
|
89 |
+
HospitalAdmission = st.sidebar.selectbox(' ', admissionid)
|
90 |
+
pastHistoryEpDate = df3['CHARTDATE_HADM_ID'].loc[(df3['Patient_ID'] == patient) & (df3['Admission_ID']== HospitalAdmission)]
|
91 |
|
92 |
|
93 |
# List of Model available
|
|
|
115 |
##========= Buttons to the 5 tabs ======== Temp disabled Discharge Plan and Social Notes
|
116 |
##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
|
117 |
col1, col2, col5 = st.columns([1,1,1])
|
118 |
+
col6, col7 =st.columns([2,2])
|
119 |
with st.container():
|
120 |
with col1:
|
121 |
btnAdmission = st.button("🏥 Admission")
|
|
|
140 |
inputNote = "Input History records"
|
141 |
|
142 |
##========= on Past History Tab =========
|
143 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
with st.container():
|
145 |
if btnPastHistory:
|
146 |
with col6:
|
147 |
st.markdown('**No. of admission past 6 months: xx**')
|
148 |
+
|
149 |
+
with col7:
|
|
|
150 |
#st.date_input('Select Admission Date') # To replace with a dropdown filter instead
|
151 |
#st.selectbox('Past Episodes',pastHistoryEp)
|
152 |
+
pastHistory = st.selectbox(' ', pastHistoryEpDate,on_change=None,)
|
153 |
+
|
154 |
+
if btnPastHistory:
|
155 |
+
st.write('Past History')
|
156 |
+
historyAdmission = df3.query(
|
157 |
+
"Patient_ID == @patient & CHARTDATE_HADM_ID == @pastHistory"
|
158 |
+
)
|
159 |
+
st.markdown('Admission Date: ' + historyAdmission['ADMITTIME'].values[0])
|
160 |
+
st.markdown('Date of Discharge: ' + historyAdmission['DISCHTIME'].values[0])
|
161 |
+
st.markdown('Days from current admission: ' + str(historyAdmission['days_from_index'].values[0]) +' days')
|
162 |
+
st.markdown('Summary: ')
|
163 |
+
if model == "BertSummarizer":
|
164 |
+
st.markdown(str(historyAdmission['BertSummarizer'].values[0]))
|
165 |
+
elif model == "t5seq2eq":
|
166 |
+
st.markdown(str(historyAdmission['t5seq2eq'].values[0]))
|
167 |
+
st.markdown('Diagnosis: ' + str(historyAdmission['DIAGNOSIS'].values[0]))
|
168 |
+
st.markdown('Discharge Disposition: ' + str(historyAdmission['DISCHARGE_LOCATION'].values[0]))
|
169 |
+
else:
|
170 |
+
runtext =st.text_area(inputNote, str(original_text2), height=300)
|
171 |
+
|
172 |
+
|
173 |
|
174 |
|
175 |
##========= END on Past History Tab =========
|
176 |
|
177 |
+
## ===== Commented out as no longer in use =====
|
178 |
# Extract words associated with each entity
|
179 |
+
#def genEntities(ann, entity):
|
180 |
+
# # entity colour dict
|
181 |
+
# #ent_col = {'DISEASE':'#B42D1B', 'CHEMICAL':'#F06292'}
|
182 |
+
# ent_col = {'DISEASE':'pink', 'CHEMICAL':'orange'}
|
183 |
+
# # separate into the different entities
|
184 |
+
# entities = trans_df['Class'].unique()
|
185 |
+
#
|
186 |
+
# if entity in entities:
|
187 |
+
# ent = list(trans_df[trans_df['Class']==entity]['Entity'].unique())
|
188 |
+
# entlist = ",".join(ent)
|
189 |
+
# st.markdown(f'<p style="background-color:{ent_col[entity]};color:#080808;font-size:16px;">{entlist}</p>', #unsafe_allow_html=True)
|
190 |
|
191 |
|
192 |
##======================== Start of NER Tagging ========================
|
|
|
323 |
st.text_area('Reference text', str(reference_text), height=150)
|
324 |
else:
|
325 |
with st.expander('Full Discharge Summary'):
|
326 |
+
#st.write("line 1 \n line 2 \n line 3")
|
327 |
+
fulldischargesummary = historyAdmission['TEXT'].values[0]
|
328 |
+
st.write(fulldischargesummary)
|
329 |
+
|
330 |
+
##====== Storing the Diseases/Text -No longer in use
|
|
|
331 |
# table= {"Entity":[], "Class":[]}
|
332 |
# ent_bc = {}
|
333 |
# for x in doc.ents:
|
|
|
337 |
# table["Class"].append(ent_bc[key])
|
338 |
# trans_df = pd.DataFrame(table)
|
339 |
|
340 |
+
# ===== Adding the Disease/Chemical into a list =====
|
341 |
problem_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'DISEASE']))
|
342 |
medication_entities = list(dedupe([t for t in doc0.ents if t.label_ == 'CHEMICAL']))
|
343 |
|
344 |
with col2:
|
345 |
st.button('NER')
|
346 |
st.markdown('**CHIEF COMPLAINT:**')
|
347 |
+
st.write(str(AdmissionChiefCom)[1:-1])
|
348 |
st.markdown('**ADMISSION DIAGNOSIS:**')
|
349 |
+
st.markdown(str(diagnosis)[1:-1])
|
350 |
st.markdown('**PROBLEM/ISSUE**')
|
351 |
#st.markdown(problem_entities)
|
352 |
+
st.markdown(f'<p style="background-color:PINK;color:#080808;font-size:16px;">{str(problem_entities)[1:-1]}</p>', unsafe_allow_html=True)
|
353 |
+
#genEntities(trans_df, 'DISEASE')
|
354 |
st.markdown('**MEDICATION**')
|
355 |
+
st.markdown(f'<p style="background-color:orange;color:#080808;font-size:16px;">{str(medication_entities)[1:-1]}</p>', unsafe_allow_html=True)
|
356 |
+
#genEntities(trans_df, 'CHEMICAL')
|
357 |
#st.table(trans_df)
|
358 |
st.markdown('**NER**')
|
359 |
with st.expander("See NER Details"):
|