-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathweb_interface.py
351 lines (302 loc) · 12.4 KB
/
web_interface.py
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
import pandas as pd
import numpy as np
import re
from datetime import datetime
from dateutil.relativedelta import relativedelta
import web_services as ws
import streamlit as st
# Make Predictions Services
def get_prediction() -> int:
"""_summary_
Takes users Input from User Interface returns a Singular Prediction
Returns:
_type_: _description_
"""
features = input_details_to_json()
prediction = ws.get_prediction(features)
if prediction is not None:
return prediction
else:
st.error("An error occurred while getting the prediction!")
def get_prediction_document(filename: str, data: pd.DataFrame) -> pd.DataFrame:
"""_summary_
Takes File Input from User Interface returns a Prediction Dataframe
Args:
data (pd.DataFrame): _description_
"""
predictions_file, sucess = ws.get_prediction_document(filename, data)
if not sucess:
st.error(predictions_file)
return pd.DataFrame([])
elif predictions_file is not None:
return predictions_file
else:
st.error("An error occurred while getting the file's predictions !")
return predictions_file
def input_details_to_json() -> dict:
"""_summary_
Takes Useer input and converts it to json format
Returns:
_type_: _description_
"""
myform_json = {"record": {"id": 0,
"file_name": "-",
"doctor_first_name": doctor_first_name if len(doctor_first_name.strip()) else "N/A",
"doctor_last_name": doctor_last_name if len(doctor_last_name.strip()) else "N/A"},
"patient": {"id": 0,
"firstname": first_name,
"lastname": last_name,
"gender": gender,
"age": age,
"hypertension": 1 if hypertension == "yes" else 0,
"heart_disease": 1 if heart_disease == "yes" else 0,
"ever_married": ever_married,
"work_type": work_type,
"Residence_type": residence_type,
"avg_glucose_level": avg_glucose_level,
"bmi": bmi,
"smoking_status": smoking_status
}
}
return myform_json
# Data Frame Stylers
def data_frame_style_color_neg(val):
"""_summary_
Pandas Dataframe Styler
Args:
val (_type_): _description_
Returns:
_type_: _description_
"""
color = 'red' if type(val) == str and val == "Risk of Stroke" else 'green'
return 'color: %s' % color
def float_format(val):
"""_summary_
Pandas Dataframe Styler
Args:
val (_type_): _description_
Returns:
_type_: _description_
"""
return "{:.2f}".format(val)
def data_frame_style_display(data: pd.DataFrame) -> pd.DataFrame:
"""_summary_
Pandas Dataframe Styler
Args:
data (_type_): _description_
Returns:
_type_: _description_
"""
data["prediction"] = data["prediction"].apply(
lambda x: "Risk of Stroke" if x == 1 else "Normal")
data["age"] = data["age"].apply(lambda x: int(x))
data["hypertension"] = data["hypertension"].apply(
lambda x: "Yes" if x == 1 else "No")
data["heart_disease"] = data["heart_disease"].apply(
lambda x: "Yes" if x == 1 else "No")
data["bmi"] = data["bmi"].map(float_format)
data["avg_glucose_level"] = data["avg_glucose_level"].map(float_format)
data.drop('record_id', axis=1, inplace=True, errors='ignore')
data.drop('id', axis=1, inplace=True, errors='ignore')
st.dataframe(data.style.applymap(
data_frame_style_color_neg, subset=['prediction']))
# Data Base Services
def search_patient_by_fullname() -> pd.DataFrame:
"""_summary_
Search for a patient by fullname
Returns:
_type_: _description_
"""
search_results = ws.search_patient_by_fullname(search_patient_first_name,
search_patient_last_name)
if search_results is not None:
return search_results
else:
st.error("An error occurred while searching for Patient(s) Record(s)!")
def search_patient_by_window_period() -> pd.DataFrame:
"""_summary_
Search patient records by Window Period
Returns:
pd.DataFrame: _description_
"""
search_results = ws.search_patient_by_window_period(search_patient_from_date,
search_patient_to_date)
if search_results is not None:
return search_results
else:
st.error("An error occurred while searching for Patient(s) Record(s)!")
def search_patients_file_by_date() -> pd.DataFrame:
"""_summary_
Search File Patients records by Date Created On
Returns:
_type_: _description_
"""
search_results = ws.search_patients_file_by_date(search_file_name,
search_created_on)
if search_results is not None:
return search_results
else:
st.error("An error occurred while searching for Patient(s) Record(s)!")
# Form Validations
def validate_search_input_details() -> bool:
"""_summary_
Validate Inputs from User Interface
Returns:
_type_: _description_
"""
if option == 'Per Patient':
if len(search_patient_first_name.strip()) == 0 and\
len(search_patient_last_name.strip()) == 0:
st.warning("First Name or Last Name is required to Search!")
return False
elif option == 'Window Period':
if search_patient_from_date > search_patient_to_date:
st.warning(
"From Date should be strictly greater than To Date to Search!")
return False
elif option == 'Per file':
if len(search_file_name.strip()) == 0:
st.warning("File name is required to Search!")
return False
pattern = re.compile(r'^[a-zA-Z0-9_]+$')
if not pattern.match(search_file_name):
st.warning(
"File name is not a match to the accepted format to Search!")
return False
return True
def validate_patient_input_details() -> bool:
"""_summary_
Validate Patient Inputs from User Interface
Returns:
_type_: _description_
"""
if len(first_name.strip()) == 0:
st.warning("First Name is required to Add a Patient!")
return False
elif len(last_name.strip()) == 0:
st.warning("Last Name is required to Add a Patient!")
return False
return True
# Web Interface Section
st.title("Heart Stroke Prediction")
# CSS Changes for Side Bar
st.markdown(
"""
<style>
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child {
width: 450px;
}
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child {
width: 450px;
margin-left: -450px;
}
</style>
""",
unsafe_allow_html=True,
)
# Singular Prediction Page Section
with st.sidebar.expander("Single Predictions"):
with st.form(key='my_form', clear_on_submit=True):
st.title("Patient Form")
gender_list = np.array(["Male", "Female"])
yes_no = np.array(["Yes", "No"])
work_type_lit = np.array(
['Private', 'Self employed', 'Govt job', 'children', 'Never worked'])
residence_type_list = np.array(['Urban', 'Rural'])
first_name = st.text_input(label='First Name')
last_name = st.text_input(label='Last Name')
gender = st.radio("Select your Gender", gender_list)
age = st.number_input(label='Age', min_value=0, step=1, max_value=150)
hypertension = st.radio(
"Did you had Hypertension in the past ?", yes_no)
heart_disease = st.radio(
"Did you had Heart Problem in the past ?", yes_no)
ever_married = st.radio("Have you ever been married ?", yes_no)
work_type = st.radio("What is your Work Type ?", work_type_lit)
residence_type = st.radio(
"What is your Living Environment?", residence_type_list)
avg_glucose_level = st.number_input(
label='Enter your Average Glucose Level', min_value=0.0, step=0.1)
bmi = st.number_input(
label='Enter your Body Mass Index (bmi)', min_value=0.0, step=0., format="%.2f")
smoking_status = st.radio(
"Do you Smoke ?", ('smoked', 'formerly smoked', 'Unknown'))
doctor_first_name = st.text_input(label='Dc. First Name')
doctor_last_name = st.text_input(label='Dc. Last Name')
submit_button = st.form_submit_button(label='Predict')
if submit_button:
if validate_patient_input_details():
prediciton = get_prediction()
message = f"{first_name} {last_name} You are at risk of a stroke !" if prediciton == 1 else f"{first_name} {last_name} You are safe to slay another day :)"
message_color = 'red' if prediciton == 1 else 'green'
st.markdown(
f"<h3 style='text-align: left;color:{message_color}'> {(message)} </h3>", unsafe_allow_html=True)
if prediciton == 1:
link_to_visit = "Visit this website for more details on how to prevent Heart Strokes\nhttps://www.cdc.gov/stroke/prevention.htm"
st.info(link_to_visit)
# File Prediction Page Section
with st.sidebar.expander("Upload File for Predictions"):
with st.form(key="predictions", clear_on_submit=True) as form:
st.title("Select a File to Generate Predictions")
uploaded_files = st.file_uploader("Choose a CSV file", type={"csv"})
predict_button = st.form_submit_button("Submit")
if uploaded_files:
filename = uploaded_files.name
if predict_button:
if uploaded_files:
data = pd.read_csv(uploaded_files)
data = get_prediction_document(filename, data)
if data.shape[0] > 0:
data_frame_style_display(data)
st.success("Uploaded Successfully!")
else:
st.warning("Please Upload a CSV File")
# Prediction Retrival Page Section
with st.sidebar.expander("Retrieve Past Predictions"):
button = None
st.title("Select a Search Mode")
option = st.selectbox('Search Mode', ('< Select Option >',
'Per Patient', 'Window Period', 'Per file'))
with st.form(key="Retrieve patients predictions by full name", clear_on_submit=True) as form_1:
# Per Patient Full Name Search
if option == 'Per Patient':
st.title("Patient Full Name")
search_patient_first_name = st.text_input(label='First Name')
search_patient_last_name = st.text_input(label='Last Name')
button = st.form_submit_button("Get Patient Records")
elif option == 'Window Period':
st.title("Select a Window Period")
search_patient_from_date = st.date_input(
"From Date", datetime.today() - relativedelta(years=1))
search_patient_to_date = st.date_input("To Date", datetime.today())
button = st.form_submit_button("Get Patients Records")
elif option == 'Per file':
st.title("Enter File Details")
st.text(
" No File extension is Required\nExample:\n'myfile.csv' => your input 'myfile'")
search_file_name = st.text_input(label='File Name')
search_created_on = st.date_input("Created On", datetime.today())
button = st.form_submit_button("Get File Records")
if button:
if validate_search_input_details():
if option == 'Per Patient':
data = search_patient_by_fullname()
data = pd.DataFrame(data)
if data.shape[0] > 0:
data_frame_style_display(data)
else:
st.warning("No Records Found")
elif option == 'Window Period':
data = search_patient_by_window_period()
data = pd.DataFrame(data)
if data.shape[0] > 0:
data_frame_style_display(data)
else:
st.warning("No Records Found")
elif option == 'Per file':
data = search_patients_file_by_date()
data = pd.DataFrame(data)
if data.shape[0] > 0:
data_frame_style_display(data)
else:
st.warning("No Records Found")