NCDB-SCG / app.py
mertkarabacak's picture
Update app.py
3f288b2
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.calibration import CalibratedClassifierCV
import shap
import gradio as gr
import random
import re
import textwrap
from datasets import load_dataset
#Read data training data.
x1 = load_dataset("mertkarabacak/NCDB-SCG", data_files="12m_data_train.csv", use_auth_token = HF_TOKEN)
x1 = pd.DataFrame(x1['train'])
x1 = x1.iloc[:, 1:]
x2 = load_dataset("mertkarabacak/NCDB-SCG", data_files="36m_data_train.csv", use_auth_token = HF_TOKEN)
x2 = pd.DataFrame(x2['train'])
x2 = x2.iloc[:, 1:]
x3 = load_dataset("mertkarabacak/NCDB-SCG", data_files="60m_data_train.csv", use_auth_token = HF_TOKEN)
x3 = pd.DataFrame(x3['train'])
x3 = x3.iloc[:, 1:]
#Read validation data.
x1_valid = load_dataset("mertkarabacak/NCDB-SCG", data_files="12m_data_valid.csv", use_auth_token = HF_TOKEN)
x1_valid = pd.DataFrame(x1_valid['train'])
x1_valid = x1_valid.iloc[:, 1:]
x2_valid = load_dataset("mertkarabacak/NCDB-SCG", data_files="36m_data_valid.csv", use_auth_token = HF_TOKEN)
x2_valid = pd.DataFrame(x2_valid['train'])
x2_valid = x2_valid.iloc[:, 1:]
x3_valid = load_dataset("mertkarabacak/NCDB-SCG", data_files="60m_data_valid.csv", use_auth_token = HF_TOKEN)
x3_valid = pd.DataFrame(x3_valid['train'])
x3_valid = x3_valid.iloc[:, 1:]
#Define feature names.
f1_names = list(x1.columns)
f1_names = [f1.replace('__', ' - ') for f1 in f1_names]
f1_names = [f1.replace('_', ' ') for f1 in f1_names]
f2_names = list(x2.columns)
f2_names = [f2.replace('__', ' - ') for f2 in f2_names]
f2_names = [f2.replace('_', ' ') for f2 in f2_names]
f3_names = list(x3.columns)
f3_names = [f3.replace('__', ' - ') for f3 in f3_names]
f3_names = [f3.replace('_', ' ') for f3 in f3_names]
#Prepare training data for the outcome 1.
y1 = x1.pop('OUTCOME')
#Prepare validation data for the outcome 1.
y1_valid = x1_valid.pop('OUTCOME')
#Prepare training data for the outcome 2.
y2 = x2.pop('OUTCOME')
#Prepare validation data for the outcome 2.
y2_valid = x2_valid.pop('OUTCOME')
#Prepare training data for the outcome 3.
y3 = x3.pop('OUTCOME')
#Prepare validation data for the outcome 3.
y3_valid = x3_valid.pop('OUTCOME')
#Assign hyperparameters.
y2_params = {'objective': 'binary', 'boosting_type': 'gbdt', 'lambda_l1': 3.7636038063165382, 'lambda_l2': 0.0038702955294852687, 'num_leaves': 2, 'feature_fraction': 0.6314330964042962, 'bagging_fraction': 0.46203297103058527, 'bagging_freq': 1, 'min_child_samples': 30, 'metric': 'binary_logloss', 'verbosity': -1, 'random_state': 31}
y3_params = {'criterion': 'entropy', 'max_features': 'sqrt', 'max_depth': 5, 'n_estimators': 200, 'min_samples_leaf': 2, 'min_samples_split': 4, 'random_state': 31}
#Training models.
from tabpfn import TabPFNClassifier
tabpfn = TabPFNClassifier(device='cpu', N_ensemble_configurations=2)
y1_model = tabpfn
y1_model = y1_model.fit(x1, y1, overwrite_warning=True)
y1_calib_model = CalibratedClassifierCV(y1_model, method='sigmoid', cv='prefit')
y1_calib_model = y1_calib_model.fit(x1_valid, y1_valid)
y1_explainer = shap.Explainer(y1_model.predict, x1)
from lightgbm import LGBMClassifier
lgb = LGBMClassifier(**y2_params)
y2_model = lgb
y2_model = y2_model.fit(x2, y2)
y2_calib_model = CalibratedClassifierCV(y2_model, method='sigmoid', cv='prefit')
y2_calib_model = y2_calib_model.fit(x2_valid, y2_valid)
y2_explainer = shap.Explainer(y2_model.predict, x2)
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(**y3_params)
y3_model = rf
y3_model = y3_model.fit(x3, y3)
y3_calib_model = CalibratedClassifierCV(y3_model, method='sigmoid', cv='prefit')
y3_calib_model = y3_calib_model.fit(x3_valid, y3_valid)
y3_explainer = shap.Explainer(y3_model.predict, x3)
output_y1 = (
"""
<br/>
<center>The probability of 12-month survival:</center>
<br/>
<center><h1>{:.2f}%</h1></center>
"""
)
output_y2 = (
"""
<br/>
<center>The probability of 36-month survival:</center>
<br/>
<center><h1>{:.2f}%</h1></center>
"""
)
output_y3 = (
"""
<br/>
<center>The probability of 60-month survival:</center>
<br/>
<center><h1>{:.2f}%</h1></center>
"""
)
#Define predict for y1.
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 = 1-prob
output = output_y1.format(prob * 100)
return output
#Define predict for y2.
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 = 1-prob
output = output_y2.format(prob * 100)
return output
#Define predict for y3.
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 = 1-prob
output = output_y3.format(prob * 100)
return output
#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 for y1.
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 = f1_names)
fig = plt.gcf()
ax = plt.gca()
wrap_labels(ax, 20)
ax.figure
plt.tight_layout()
fig.set_figheight(7)
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
#Define interpret for y2.
def y2_interpret(*args):
df2 = pd.DataFrame([args], columns=x2.columns)
shap_values2 = y2_explainer(df2).values
shap_values2 = np.abs(shap_values2)
shap.bar_plot(shap_values2[0], max_display = 10, show = False, feature_names = f2_names)
fig = plt.gcf()
ax = plt.gca()
wrap_labels(ax, 20)
ax.figure
plt.tight_layout()
fig.set_figheight(7)
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
#Define interpret for y3.
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_values3[0], max_display = 10, show = False, feature_names = f3_names)
fig = plt.gcf()
ax = plt.gca()
wrap_labels(ax, 20)
ax.figure
plt.tight_layout()
fig.set_figheight(7)
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 = "NCDB-SCG") as demo:
gr.Markdown(
"""
<br/>
<center><h2>NOT FOR CLINICAL USE</h2></center>
<br/>
<center><h1>Spinal Cord Gliomas</h1></center>
<center><h2>Survival Outcomes Prediction Tool</h2></center>
<br/>
<center><h3>This web application should not be used to guide any clinical decisions.</h3><center>
"""
)
gr.Markdown(
"""
<center><h3>Model Performances</h3></center>
<div style="text-align:center;">
<table style="width:100%;">
<tr>
<th>Outcome</th>
<th>Algorithm</th>
<th>Sensitivity</th>
<th>Specificity</th>
<th>Accuracy</th>
<th>AUPRC</th>
<th>AUROC</th>
<th>Brier Score</th>
</tr>
<tr>
<td>1-Year Mortality</td>
<td>TabPFN</td>
<td>0.354 (0.324 - 0.384)</td>
<td>0.983 (0.975 - 0.991)</td>
<td>0.952 (0.939 - 0.965)</td>
<td>0.543 (0.512 - 0.574)</td>
<td>0.938 (0.917 - 0.970)</td>
<td>0.035 (0.024 - 0.046)</td>
</tr>
<tr>
<td>3-Year Mortality</td>
<td>LightGBM</td>
<td>0.474 (0.440 - 0.508)</td>
<td>0.970 (0.958 - 0.982)</td>
<td>0.911 (0.891 - 0.931)</td>
<td>0.635 (0.602 - 0.668)</td>
<td>0.907 (0.840 - 0.916)</td>
<td>0.069 (0.052 - 0.086)</td>
</tr>
<tr>
<td>5-Year Mortality</td>
<td>Random Forest</td>
<td>0.584 (0.544 - 0.624)</td>
<td>0.963 (0.948 - 0.978)</td>
<td>0.888 (0.862 - 0.914)</td>
<td>0.741 (0.705 - 0.777)</td>
<td>0.902 (0.844 - 0.920)</td>
<td>0.089 (0.066 - 0.112)</td>
</tr>
</table>
</div>
"""
)
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', 'Other or Unknown'], type = 'index', value = 'White')
Hispanic_Ethnicity = gr.Dropdown(label = "Hispanic Ethnicity", choices = ['No', 'Yes', 'Unknown'], type = 'index', value = 'No')
Insurance_Status = gr.Dropdown(label = "Insurance Status", choices = ['Private insurance', 'Medicare', 'Medicaid', 'Other government', 'Not insured', 'Unknown'], type = 'index', value = 'Private insurance')
Facility_Type = gr.Dropdown(label = "Facility Type", choices = ['Academic/Research Program', 'Community Cancer Program', 'Integrated Network Cancer Program', 'Other or Unknown'], type = 'index', value = 'Academic/Research Program')
Facility_Location = gr.Dropdown(label = "Facility Location", choices = ['Central', 'Atlantic', 'Pacific', 'Mountain', 'New England', 'Unknown or Other'], type = 'index', value = 'Central')
CharlsonDeyo_Score = gr.Dropdown(label = "Charlson-Deyo Score", choices = ['0', '1', '2', 'Greater than 3'], type = 'index', value = '0')
Diagnostic_Biopsy = gr.Dropdown(label = "Diagnostic Biopsy", choices = ['No', 'Yes', 'Unknown'], type = 'index', value = 'No')
Tumor_Size = gr.Dropdown(label = "Tumor Size", choices = ['< 2 cm', '2 - 3.9 cm', '4 - 5.9 cm', '6 - 7.9 cm', '8 - 9.9 cm', '> 10 cm', 'Unknown'], type = 'index', value = '< 2 cm')
Ki67_Labeling_Index = gr.Dropdown(label = 'Ki-67 Labeling Index', choices = ['0-20%', '21-40%', '41-60%', '61-80%', '81-100%', 'Normal (no percentage available)', 'Slightly elevated (no percentage available)', 'Elevated (no percentage available)', 'Unknown'], type = 'index', value = '0-20%')
Histology = gr.Dropdown(label = 'Histology', choices = ['Ependymoma', 'Astrocytoma','Glioblastoma', 'Malignant glioma (NOS)'], type = 'index', value = 'Ependymoma')
Grade = gr.Dropdown(label = 'Grade', choices = ['Grade 1', 'Grade 2', 'Grade 3', 'Grade 4', 'Unknown'], type = 'index', value = 'Grade 1')
Surgery = gr.Dropdown(label = "Surgery", choices = ['No', 'Yes', 'Unknown'], type = 'index', value = 'No')
Radiotherapy = gr.Dropdown(label = "Radiotherapy", choices = ['No', 'Yes', 'Unknown'], type = 'index', value = 'No')
Chemotherapy = gr.Dropdown(label = "Chemotherapy", choices = ['No', 'Yes', 'Unknown'], type = 'index', value = 'No')
with gr.Column():
with gr.Box():
gr.Markdown(
"""
<center> <h2>1-Year Survival</h2> </center>
<br/>
<center> This model uses the TabPFN algorithm.</center>
<br/>
"""
)
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/>
"""
)
with gr.Box():
gr.Markdown(
"""
<center> <h2>3-Year Survival</h2> </center>
<br/>
<center> This model uses the LightGBM algorithm.</center>
<br/>
"""
)
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/>
"""
)
with gr.Box():
gr.Markdown(
"""
<center> <h2>5-Year Survival</h2> </center>
<br/>
<center> This model uses the Random Forest algorithm.</center>
<br/>
"""
)
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/>
"""
)
y1_predict_btn.click(
y1_predict,
inputs = [Facility_Type, Facility_Location, Age, Sex, Race, Hispanic_Ethnicity, Insurance_Status, CharlsonDeyo_Score, Histology, Diagnostic_Biopsy, Grade, Ki67_Labeling_Index, Tumor_Size, Surgery, Chemotherapy, Radiotherapy],
outputs = [label1]
)
y2_predict_btn.click(
y2_predict,
inputs = [Facility_Type, Facility_Location, Age, Sex, Race, Hispanic_Ethnicity, Insurance_Status, CharlsonDeyo_Score, Histology, Diagnostic_Biopsy, Grade, Ki67_Labeling_Index, Tumor_Size, Surgery, Chemotherapy, Radiotherapy],
outputs = [label2]
)
y3_predict_btn.click(
y3_predict,
inputs = [Facility_Type, Facility_Location, Age, Sex, Race, Hispanic_Ethnicity, Insurance_Status, CharlsonDeyo_Score, Histology, Diagnostic_Biopsy, Grade, Ki67_Labeling_Index, Tumor_Size, Surgery, Chemotherapy, Radiotherapy],
outputs = [label3]
)
y1_interpret_btn.click(
y1_interpret,
inputs = [Facility_Type, Facility_Location, Age, Sex, Race, Hispanic_Ethnicity, Insurance_Status, CharlsonDeyo_Score, Histology, Diagnostic_Biopsy, Grade, Ki67_Labeling_Index, Tumor_Size, Surgery, Chemotherapy, Radiotherapy],
outputs = [plot1],
)
y2_interpret_btn.click(
y2_interpret,
inputs = [Facility_Type, Facility_Location, Age, Sex, Race, Hispanic_Ethnicity, Insurance_Status, CharlsonDeyo_Score, Histology, Diagnostic_Biopsy, Grade, Ki67_Labeling_Index, Tumor_Size, Surgery, Chemotherapy, Radiotherapy],
outputs = [plot2],
)
y3_interpret_btn.click(
y3_interpret,
inputs = [Facility_Type, Facility_Location, Age, Sex, Race, Hispanic_Ethnicity, Insurance_Status, CharlsonDeyo_Score, Histology, Diagnostic_Biopsy, Grade, Ki67_Labeling_Index, Tumor_Size, Surgery, Chemotherapy, Radiotherapy],
outputs = [plot3],
)
gr.Markdown(
"""
<center><h2>Disclaimer</h2>
<center>
The data utilized for this tool is sourced from the Commission on Cancer (CoC) of the American College of Surgeons and the American Cancer Society. These institutions, however, have not verified the information and are not responsible for the statistical validity of the data analysis or the conclusions drawn by the authors. This predictive tool, available on this webpage, is designed for healthcare professionals 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()