NSQIP-ACC / app.py
mertkarabacak's picture
Update app.py
3490bc2 verified
import os
HF_TOKEN = os.getenv("HF_TOKEN")
import numpy as np
import pandas as pd
import sklearn
import sklearn.metrics
from math import sqrt
from scipy import stats as st
from matplotlib import pyplot as plt
from sklearn.model_selection import StratifiedKFold
from sklearn.utils import resample
from tabpfn import TabPFNClassifier
from sklearn.calibration import CalibratedClassifierCV
from imblearn.over_sampling import SMOTE
import shap
import gradio as gr
import random
import re
import textwrap
from datasets import load_dataset
#Read data.
x1 = load_dataset("mertkarabacak/NSQIP-ACC", data_files="gradio_los_data.csv", use_auth_token = HF_TOKEN)
x1 = pd.DataFrame(x1['train'])
x1 = x1.iloc[:, 1:]
x2 = load_dataset("mertkarabacak/NSQIP-ACC", data_files="gradio_discharge_data.csv", use_auth_token = HF_TOKEN)
x2 = pd.DataFrame(x2['train'])
x2 = x2.iloc[:, 1:]
x3 = load_dataset("mertkarabacak/NSQIP-ACC", data_files="gradio_complication_data.csv", use_auth_token = HF_TOKEN)
x3 = pd.DataFrame(x3['train'])
x3 = x3.iloc[:, 1:]
#Split predictors and the outcome.
y1 = x1.pop('OUTCOME')
y2 = x2.pop('OUTCOME')
y3 = x3.pop('OUTCOME')
#Define resampler and cv.
resampler = SMOTE(random_state = 0)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=31)
#Train models.
for train_index, test_index in cv.split(x1, y1):
x1_train_fold, x1_valid_fold = x1.iloc[train_index], x1.iloc[test_index]
y1_train_fold, y1_valid_fold = y1.iloc[train_index], y1.iloc[test_index]
x1_train_fold, y1_train_fold = resampler.fit_resample(x1_train_fold, y1_train_fold)
y1_model = TabPFNClassifier(device='cuda', N_ensemble_configurations=8)
y1_model.fit(x1_train_fold, y1_train_fold, overwrite_warning=True)
y1_calib_model = CalibratedClassifierCV(y1_model, method='isotonic', cv='prefit')
y1_calib_model.fit(x1_valid_fold, y1_valid_fold)
for train_index, test_index in cv.split(x2, y2):
x2_train_fold, x2_valid_fold = x2.iloc[train_index], x2.iloc[test_index]
y2_train_fold, y2_valid_fold = y2.iloc[train_index], y2.iloc[test_index]
x2_train_fold, y2_train_fold = resampler.fit_resample(x2_train_fold, y2_train_fold)
y2_model = TabPFNClassifier(device='cuda', N_ensemble_configurations=8)
y2_model.fit(x2_train_fold, y2_train_fold, overwrite_warning=True)
y2_calib_model = CalibratedClassifierCV(y2_model, method='isotonic', cv='prefit')
y2_calib_model.fit(x2_valid_fold, y2_valid_fold)
for train_index, test_index in cv.split(x3, y3):
x3_train_fold, x3_valid_fold = x3.iloc[train_index], x3.iloc[test_index]
y3_train_fold, y3_valid_fold = y3.iloc[train_index], y3.iloc[test_index]
x3_train_fold, y3_train_fold = resampler.fit_resample(x3_train_fold, y3_train_fold)
y3_model = TabPFNClassifier(device='cuda', N_ensemble_configurations=8)
y3_model.fit(x3_train_fold, y3_train_fold, overwrite_warning=True)
y3_calib_model = CalibratedClassifierCV(y3_model, method='isotonic', cv='prefit')
y3_calib_model.fit(x3_valid_fold, y3_valid_fold)
#Fit explainers.
y1_explainer = shap.Explainer(y1_calib_model.predict, x1)
y2_explainer = shap.Explainer(y2_calib_model.predict, x2)
y3_explainer = shap.Explainer(y3_calib_model.predict, x3)
#Define output functions.
output_y1 = (
"""
<br/>
<center><h3 style='font-size: 24px;'>The predicted probability of prolonged LOS:</center>
<br/>
<center><h1 style='font-size: 36px;'>{}%</h1></center>
"""
)
output_y2 = (
"""
<br/>
<center><h3 style='font-size: 24px;'>The predicted probability of non-home discharge:</center>
<br/>
<center><h1 style='font-size: 36px;'>{}%</h1></center>
"""
)
output_y3 = (
"""
<br/>
<center><h3 style='font-size: 24px;'>The predicted probability of major complications:</center>
<br/>
<center><h1 style='font-size: 36px;'>{}%</h1></center>
"""
)
#Define predict functions.
def y1_predict(*args):
df1 = pd.DataFrame([args], columns=x1.columns)
pos_pred = y1_calib_model.predict_proba(df1)
prob = pos_pred[0][1]
prob_percent = round(prob * 100)
output = output_y1.format(prob_percent)
return output
def y2_predict(*args):
df2 = pd.DataFrame([args], columns=x2.columns)
pos_pred = y2_calib_model.predict_proba(df2)
prob = pos_pred[0][1]
prob_percent = round(prob * 100)
output = output_y2.format(prob_percent)
return output
def y3_predict(*args):
df3 = pd.DataFrame([args], columns=x3.columns)
pos_pred = y3_calib_model.predict_proba(df3)
prob = pos_pred[0][1]
prob_percent = round(prob * 100)
output = output_y3.format(prob_percent)
return output
#Define feature names.
f_names = ['Sex', 'Race', 'Hispanic Ethnicity', 'Transfer Status', 'Surgical Specialty', 'Diabetes Mellitus', 'Dyspnea', 'History of Severe COPD', 'Congestive Heart Failure', 'Hypertension', 'Acute Renal Failure', 'Currently Requiring or on Dialysis', 'Disseminated Cancer', 'Steroid or Immunosuppressant for a Chronic Condition', 'Malnourishment', 'Bleeding Disorder', 'RBC Transfusion', 'Preoperative Serum Sodium', 'Preoperative Serum BUN', 'Preoperative Serum Creatinine', 'Preoperative WBC Count', 'Preoperative Hematocrit', 'Preoperative Platelet Count', 'ASA Physical Status', 'BMI', 'Single or Multiple Level Surgery']
#Define function for wrapping feature labels.
def wrap_labels(ax, width, break_long_words=False):
labels = []
for label in ax.get_yticklabels():
text = label.get_text()
labels.append(textwrap.fill(text, width=width, break_long_words=break_long_words))
ax.set_yticklabels(labels, rotation=0)
#Define interpret functions
def y1_interpret(*args):
df1 = pd.DataFrame([args], columns=x1.columns)
shap_values1 = y1_explainer(df1).values
shap_values1 = np.abs(shap_values1)
shap.bar_plot(shap_values1[0], max_display = 10, show = False, feature_names = f_names)
fig = plt.gcf()
ax = plt.gca()
wrap_labels(ax, 50)
ax.figure
plt.tight_layout()
fig.set_figheight(9)
fig.set_figwidth(9)
plt.xlabel("SHAP value (impact on model output)", fontsize =12, fontweight = 'heavy', labelpad = 8)
plt.tick_params(axis="y",direction="out", labelsize = 12)
plt.tick_params(axis="x",direction="out", labelsize = 12)
return fig
def y2_interpret(*args):
df2 = pd.DataFrame([args], columns=x2.columns)
shap_values1 = y2_explainer(df2).values
shap_values1 = np.abs(shap_values2)
shap.bar_plot(shap_values1[0], max_display = 10, show = False, feature_names = f_names)
fig = plt.gcf()
ax = plt.gca()
wrap_labels(ax, 50)
ax.figure
plt.tight_layout()
fig.set_figheight(9)
fig.set_figwidth(9)
plt.xlabel("SHAP value (impact on model output)", fontsize =12, fontweight = 'heavy', labelpad = 8)
plt.tick_params(axis="y",direction="out", labelsize = 12)
plt.tick_params(axis="x",direction="out", labelsize = 12)
return fig
def y3_interpret(*args):
df3 = pd.DataFrame([args], columns=x3.columns)
shap_values3 = y3_explainer(df3).values
shap_values3 = np.abs(shap_values3)
shap.bar_plot(shap_values1[0], max_display = 10, show = False, feature_names = f_names)
fig = plt.gcf()
ax = plt.gca()
wrap_labels(ax, 50)
ax.figure
plt.tight_layout()
fig.set_figheight(9)
fig.set_figwidth(9)
plt.xlabel("SHAP value (impact on model output)", fontsize =12, fontweight = 'heavy', labelpad = 8)
plt.tick_params(axis="y",direction="out", labelsize = 12)
plt.tick_params(axis="x",direction="out", labelsize = 12)
return fig
with gr.Blocks(title = "NSQIP-ACC") as demo:
gr.Markdown(
"""
<br/>
<center><h2>NOT FOR CLINICAL USE</h2><center>
<br/>
<center><h1>Anterior Cervical Corpectomy Outcomes</h1></center>
<center><h2>Prediction Tool</h2></center>
<br/>
<center><h3>This web application should not be used to guide any clinical decisions.</h3><center>
<br/>
<center><i>The publication describing the details of this prediction tool will be posted here upon the acceptance of publication.</i><center>
"""
)
gr.Markdown(
"""
<center><h3>Model Performance</h3></center>
<table style="margin-left: auto; margin-right: auto;">
<tr>
<th>Outcome</th>
<th>Weighted Precision</th>
<th>Weighted Recall</th>
<th>F1 Score</th>
<th>Accuracy</th>
<th>AUROC</th>
<th>AUPRC</th>
<th>Brier Score</th>
</tr>
<tr>
<td>Prolonged LOS</td>
<td>0.822 (0.782 - 0.861)</td>
<td>0.882 (0.876 - 0.888)</td>
<td>0.178 (0.032 - 0.316)</td>
<td>0.882 (0.876 - 0.888)</td>
<td>0.802 (0.770 - 0.433)</td>
<td>0.437 (0.409 - 0.464)</td>
<td>0.091 (0.089 - 0.093)</td>
</tr>
<tr>
<td>Non-home Discharges</td>
<td>0.918 (0.900 - 0.937)</td>
<td>0.946 (0.945 - 0.949)</td>
<td>0.161 (0.053 - 0.275)</td>
<td>0.946 (0.945 - 0.949)</td>
<td>0.816 (0.775 - 0.857)</td>
<td>0.392 (0.349 - 0.433)</td>
<td>0.045 (0.042 - 0.049)</td>
</tr>
<tr>
<td>Major Complications</td>
<td>0.944 (0.943 - 0.945)</td>
<td>0.972 (0.971 - 0.973)</td>
<td>0.144 (0.021 - 0.179)</td>
<td>0.972 (0.971 - 0.973)</td>
<td>0.702 (0.610 - 0.754)</td>
<td>0.214 (0.156 - 0.273)</td>
<td>0.025 (0.024 - 0.026)</td>
</tr>
</table>
"""
)
with gr.Row():
with gr.Column():
Age = gr.Slider(label = "Age", minimum = 18, maximum = 99, step = 1, value = 55)
Sex = gr.Dropdown(label = "Sex", choices = ['Male', 'Female'], type = 'index', value = 'Male')
Race = gr.Dropdown(label = "Race", choices = ['White', 'Black', 'Asian', 'Other/Unknown'], type = 'index', value = 'White')
Hispanic_Ethnicity = gr.Dropdown(label = "Hispanic Ethnicity", choices = ['No', 'Yes', 'Unknown'], type = 'index', value = 'No')
BMI = gr.Slider(label = "BMI", minimum = 15, maximum = 60, step = 0.1, value = 25)
Transfer_Status = gr.Dropdown(label = "Transfer Status", choices = ['Not transferred', 'Transferred'], type = 'index', value = 'No')
Diabetes_Mellitus_Requiring_Therapy = gr.Dropdown(label = "Diabetes", choices = ['No', 'Yes'], type = 'index', value = 'No')
Dyspnea = gr.Dropdown(label = "Dyspnea", choices = ['No', 'Yes'], type = 'index', value = 'No')
History_of_Severe_COPD = gr.Dropdown(label = "Severe COPD History", choices = ['No', 'Yes'], type = 'index', value = 'No')
CHF_within_30_Days_Prior_to_Surgery = gr.Dropdown(label = "Congestive Heart Failure", choices = ['No', 'Yes'], type = 'index', value = 'No')
Hypertension_Requiring_Medication = gr.Dropdown(label = "Hypertension", choices = ['No', 'Yes'], type = 'index', value = 'No')
Acute_Renal_Failure = gr.Dropdown(label = "Acute Kidney Injury", choices = ['No', 'Yes'], type = 'index', value = 'No')
Currently_Requiring_or_on_Dialysis = gr.Dropdown(label = "Currently Requiring or on Dialysis", choices = ['No', 'Yes'], type = 'index', value = 'No')
Disseminated_Cancer = gr.Dropdown(label = "Disseminated Cancer", choices = ['No', 'Yes'], type = 'index', value = 'No')
Steroid_or_Immunosuppressant_for_a_Chronic_Condition = gr.Dropdown(label = "Steroids or Immunosuppressants for a Chronic Disease", choices = ['No', 'Yes'], type = 'index', value = 'No')
Malnourishment = gr.Dropdown(label = "> 10% Weight Loss Over 6 Months", choices = ['No', 'Yes'], type = 'index', value = 'No')
Bleeding_Disorder = gr.Dropdown(label = "Bleeding Disorder", choices = ['No', 'Yes'], type = 'index', value = 'No')
RBC_Transfusion_within_72_Hours_Prior_to_Surgery = gr.Dropdown(label = "Bleeding Disorder", choices = ['No', 'Yes'], type = 'index', value = 'No')
Bleeding_Disorder = gr.Dropdown(label = "Bleeding Disorder", choices = ['No', 'Yes'], type = 'index', value = 'No')
RBC_Transfusion_within_72_Hours_Prior_to_Surgery = gr.Dropdown(label = "≥1 Unit of RBC Transfusion in the 72 Hours Preceding Surgery", choices = ['No', 'Yes'], type = 'index', value = 'No')
ASA_Classification = gr.Dropdown(label = "ASA Classification", choices = ['1', '2', '3'], type = 'index', value = '1')
Functional_Status = gr.Dropdown(label = "Preoperative Functional Status", choices = ['Independent', 'Partially Dependent', 'Totally Dependent', 'Unknown'], type = 'index', value = 'Independent')
Preoperative_Serum_Sodium = gr.Slider(label="Preoperative Serum Sodium", minimum = 120, maximum = 150, step = 1, value = 135)
Preoperative_Serum_BUN = gr.Slider(label="Preoperative Serum BUN", minimum = 5, maximum = 50, step = 0.1, value = 15)
Preoperative_Serum_Creatinine = gr.Slider(label="Preoperative Serum Creatinine", minimum = 0.3, maximum = 5, step = 0.1, value = 0.8)
Preoperative_WBC_Count = gr.Slider(label="Preoperative WBC Count (x1000)", minimum = 1, maximum = 20, step = 0.1, value = 5)
Preoperative_Hematocrit = gr.Slider(label="Preoperative Hematocrit", minimum = 20, maximum = 60, step = 1, value = 45)
Preoperative_Platelet_Count = gr.Slider(label="Preoperative Platelet Count (x1000)", minimum = 50, maximum = 1000, step = 1, value = 150)
Surgical_Specialty = gr.Dropdown(label = "Surgical Specialty", choices = ['Neurosurgery', 'Orthopedics'], type = 'index', value = 'Neurosurgery')
Single_or_Multiple_Level_Surgery = gr.Dropdown(label = "Single- or Multiple-Level Surgery", choices = ['Single', 'Multiple'], type = 'index', value = 'Single')
with gr.Column():
with gr.Box():
with gr.Row():
y1_predict_btn = gr.Button(value="Predict")
gr.Markdown(
"""
<br/>
"""
)
label1 = gr.Markdown()
gr.Markdown(
"""
<br/>
"""
)
with gr.Row():
y1_interpret_btn = gr.Button(value="Explain")
gr.Markdown(
"""
<br/>
"""
)
plot1 = gr.Plot()
gr.Markdown(
"""
<br/>
"""
)
y1_predict_btn.click(
y1_predict,
inputs = [Sex, Race, Hispanic_Ethnicity, Transfer_Status, Age, Surgical_Specialty, Diabetes_Mellitus_Requiring_Therapy, Dyspnea, Functional_Status, History_of_Severe_COPD, CHF_within_30_Days_Prior_to_Surgery, Hypertension_Requiring_Medication, Acute_Renal_Failure, Currently_Requiring_or_on_Dialysis, Disseminated_Cancer, Steroid_or_Immunosuppressant_for_a_Chronic_Condition, Malnourishment, Bleeding_Disorder, RBC_Transfusion_within_72_Hours_Prior_to_Surgery, Preoperative_Serum_Sodium, Preoperative_Serum_BUN, Preoperative_Serum_Creatinine, Preoperative_WBC_Count, Preoperative_Hematocrit, Preoperative_Platelet_Count, ASA_Classification, BMI, Single_or_Multiple_Level_Surgery],
outputs = [label1]
)
y1_interpret_btn.click(
y1_interpret,
inputs = [Sex, Race, Hispanic_Ethnicity, Transfer_Status, Age, Surgical_Specialty, Diabetes_Mellitus_Requiring_Therapy, Dyspnea, Functional_Status, History_of_Severe_COPD, CHF_within_30_Days_Prior_to_Surgery, Hypertension_Requiring_Medication, Acute_Renal_Failure, Currently_Requiring_or_on_Dialysis, Disseminated_Cancer, Steroid_or_Immunosuppressant_for_a_Chronic_Condition, Malnourishment, Bleeding_Disorder, RBC_Transfusion_within_72_Hours_Prior_to_Surgery, Preoperative_Serum_Sodium, Preoperative_Serum_BUN, Preoperative_Serum_Creatinine, Preoperative_WBC_Count, Preoperative_Hematocrit, Preoperative_Platelet_Count, ASA_Classification, BMI, Single_or_Multiple_Level_Surgery],
outputs = [plot1],
)
with gr.Box():
with gr.Row():
y2_predict_btn = gr.Button(value="Predict")
gr.Markdown(
"""
<br/>
"""
)
label2 = gr.Markdown()
gr.Markdown(
"""
<br/>
"""
)
with gr.Row():
y2_interpret_btn = gr.Button(value="Explain")
gr.Markdown(
"""
<br/>
"""
)
plot2 = gr.Plot()
gr.Markdown(
"""
<br/>
"""
)
y2_predict_btn.click(
y2_predict,
inputs = [Sex, Race, Hispanic_Ethnicity, Transfer_Status, Age, Surgical_Specialty, Diabetes_Mellitus_Requiring_Therapy, Dyspnea, Functional_Status, History_of_Severe_COPD, CHF_within_30_Days_Prior_to_Surgery, Hypertension_Requiring_Medication, Acute_Renal_Failure, Currently_Requiring_or_on_Dialysis, Disseminated_Cancer, Steroid_or_Immunosuppressant_for_a_Chronic_Condition, Malnourishment, Bleeding_Disorder, RBC_Transfusion_within_72_Hours_Prior_to_Surgery, Preoperative_Serum_Sodium, Preoperative_Serum_BUN, Preoperative_Serum_Creatinine, Preoperative_WBC_Count, Preoperative_Hematocrit, Preoperative_Platelet_Count, ASA_Classification, BMI, Single_or_Multiple_Level_Surgery],
outputs = [label2]
)
y2_interpret_btn.click(
y2_interpret,
inputs = [Sex, Race, Hispanic_Ethnicity, Transfer_Status, Age, Surgical_Specialty, Diabetes_Mellitus_Requiring_Therapy, Dyspnea, Functional_Status, History_of_Severe_COPD, CHF_within_30_Days_Prior_to_Surgery, Hypertension_Requiring_Medication, Acute_Renal_Failure, Currently_Requiring_or_on_Dialysis, Disseminated_Cancer, Steroid_or_Immunosuppressant_for_a_Chronic_Condition, Malnourishment, Bleeding_Disorder, RBC_Transfusion_within_72_Hours_Prior_to_Surgery, Preoperative_Serum_Sodium, Preoperative_Serum_BUN, Preoperative_Serum_Creatinine, Preoperative_WBC_Count, Preoperative_Hematocrit, Preoperative_Platelet_Count, ASA_Classification, BMI, Single_or_Multiple_Level_Surgery],
outputs = [plot2],
)
with gr.Box():
with gr.Row():
y3_predict_btn = gr.Button(value="Predict")
gr.Markdown(
"""
<br/>
"""
)
label3 = gr.Markdown()
gr.Markdown(
"""
<br/>
"""
)
with gr.Row():
y3_interpret_btn = gr.Button(value="Explain")
gr.Markdown(
"""
<br/>
"""
)
plot3 = gr.Plot()
gr.Markdown(
"""
<br/>
"""
)
y3_predict_btn.click(
y3_predict,
inputs = [Sex, Race, Hispanic_Ethnicity, Transfer_Status, Age, Surgical_Specialty, Diabetes_Mellitus_Requiring_Therapy, Dyspnea, Functional_Status, History_of_Severe_COPD, CHF_within_30_Days_Prior_to_Surgery, Hypertension_Requiring_Medication, Acute_Renal_Failure, Currently_Requiring_or_on_Dialysis, Disseminated_Cancer, Steroid_or_Immunosuppressant_for_a_Chronic_Condition, Malnourishment, Bleeding_Disorder, RBC_Transfusion_within_72_Hours_Prior_to_Surgery, Preoperative_Serum_Sodium, Preoperative_Serum_BUN, Preoperative_Serum_Creatinine, Preoperative_WBC_Count, Preoperative_Hematocrit, Preoperative_Platelet_Count, ASA_Classification, BMI, Single_or_Multiple_Level_Surgery],
outputs = [label3]
)
y3_interpret_btn.click(
y3_interpret,
inputs = [Sex, Race, Hispanic_Ethnicity, Transfer_Status, Age, Surgical_Specialty, Diabetes_Mellitus_Requiring_Therapy, Dyspnea, Functional_Status, History_of_Severe_COPD, CHF_within_30_Days_Prior_to_Surgery, Hypertension_Requiring_Medication, Acute_Renal_Failure, Currently_Requiring_or_on_Dialysis, Disseminated_Cancer, Steroid_or_Immunosuppressant_for_a_Chronic_Condition, Malnourishment, Bleeding_Disorder, RBC_Transfusion_within_72_Hours_Prior_to_Surgery, Preoperative_Serum_Sodium, Preoperative_Serum_BUN, Preoperative_Serum_Creatinine, Preoperative_WBC_Count, Preoperative_Hematocrit, Preoperative_Platelet_Count, ASA_Classification, BMI, Single_or_Multiple_Level_Surgery],
outputs = [plot3],
)
gr.Markdown(
"""
<center><h2>Disclaimer</h2>
<center>
This predictive tool, available on this webpage, is designed to provide general health information only and is not a substitute for professional medical advice, diagnosis, or treatment. It is strongly recommended that users consult with their own healthcare provider for any health-related concerns or issues. The authors make no warranties or representations, express or implied, regarding the accuracy, timeliness, relevance, or utility of the information contained in this tool. The health information in the prediction tool is subject to change and can be affected by various confounders, therefore it may be outdated, incomplete, or incorrect. No doctor-patient relationship is created by using this prediction tool and the authors have not validated its content. The authors do not record any specific user information or initiate contact with users. Before making any healthcare decisions or taking or refraining from any action based on the information in this prediction tool, it is advisable to seek professional advice from a healthcare provider. By using the prediction tool, users acknowledge and agree that neither the authors nor any other party will be liable for any decisions made, actions taken or not taken as a result of the information provided herein.
<br/>
<h4>By using this tool, you accept all of the above terms.<h4/>
</center>
"""
)
demo.launch()