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.linear_model import LogisticRegression 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-Chordoma", data_files="60m_data_train.csv", use_auth_token = HF_TOKEN) x1 = pd.DataFrame(x1['train']) x1 = x1.iloc[:, 1:] print(x1.columns) x2 = load_dataset("mertkarabacak/NCDB-Chordoma", data_files="120m_data_train.csv", use_auth_token = HF_TOKEN) x2 = pd.DataFrame(x2['train']) x2 = x2.iloc[:, 1:] print(x2.columns) #Read validation data. x1_valid = load_dataset("mertkarabacak/NCDB-Chordoma", data_files="60m_data_valid.csv", use_auth_token = HF_TOKEN) x1_valid = pd.DataFrame(x1_valid['train']) x1_valid = x1_valid.iloc[:, 1:] print(x1_valid.columns) x2_valid = load_dataset("mertkarabacak/NCDB-Chordoma", data_files="120m_data_valid.csv", use_auth_token = HF_TOKEN) x2_valid = pd.DataFrame(x2_valid['train']) x2_valid = x2_valid.iloc[:, 1:] print(x2_valid.columns) #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] #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') #Assign hyperparameters. y1_params = {'objective': 'CrossEntropy', 'colsample_bylevel': 0.0724536745649557, 'depth': 4, 'boosting_type': 'Ordered', 'bootstrap_type': 'Bayesian', 'bagging_temperature': 3.19632337181607, 'random_state': 31} y2_params = {'objective': 'CrossEntropy', 'colsample_bylevel': 0.09584124096653913, 'depth': 10, 'boosting_type': 'Plain', 'bootstrap_type': 'MVS', 'random_state': 31} #Training models. from catboost import CatBoostClassifier cb = CatBoostClassifier(**y1_params) y1_model = cb y1_model = y1_model.fit(x1, y1) y1_explainer = shap.Explainer(y1_model.predict, x1) y1_calib_probs = y1_model.predict_proba(x1_valid) y1_calib_model = LogisticRegression() y1_calib_model = y1_calib_model.fit(y1_calib_probs, y1_valid) from catboost import CatBoostClassifier cb = CatBoostClassifier(**y2_params) y2_model = cb y2_model = y2_model.fit(x2, y2) y2_explainer = shap.Explainer(y2_model.predict, x2) y2_calib_probs = y2_model.predict_proba(x2_valid) y2_calib_model = LogisticRegression() y2_calib_model = y2_calib_model.fit(y2_calib_probs, y2_valid) output_y1 = ( """
The probability of 5-year survival:

{:.2f}%

""" ) output_y2 = ( """
The probability of 10-year survival:

{:.2f}%

""" ) #Define predict for y1. def y1_predict(*args): df1 = pd.DataFrame([args], columns=x1.columns) pos_pred = y1_model.predict_proba(df1) pos_pred = y1_calib_model.predict_proba(pos_pred) 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_model.predict_proba(df2) pos_pred = y2_calib_model.predict_proba(pos_pred) prob = pos_pred[0][1] prob = 1-prob output = output_y2.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 with gr.Blocks(title = "NCDB-Chordoma") as demo: gr.Markdown( """

NOT FOR CLINICAL USE


Spinal Chordoma Survival Outcomes

Prediction Tool


This web application should not be used to guide any clinical decisions.


The publication describing the details of this prediction tool will be posted here upon the acceptance of publication.
""" ) gr.Markdown( """

Model Performances

Outcome Algorithm Sensitivity Specificity Accuracy AUPRC AUROC Brier Score
5-Year Mortality CatBoost 0.648 (0.588 - 0.708) 0.721 (0.664 - 0.778) 0.665 (0.606 - 0.724) 0.694 (0.636 - 0.752) 0.801 (0.734 - 0.842) 0.185 (0.136 - 0.234)
10-Year Mortality CatBoost 0.731 (0.663 - 0.799) 0.738 (0.670 - 0.806) 0.890 (0.842 - 0.938) 0.733 (0.665 - 0.801) 0.814 (0.752 - 0.842) 0.159 (0.103 - 0.217)
""" ) with gr.Row(): with gr.Column(): Age = gr.Dropdown(label = "Age at Diagnosis", choices = ['18-44', '45-64', '64-79', '80+'], type = 'index', value = '45-64') Sex = gr.Dropdown(label = "Sex", choices = ['Male', 'Female'], type = 'index', value = 'Male') Race = gr.Dropdown(label = "Race", choices = ['White', 'Black', 'Other'], type = 'index', value = 'White') Hispanic_Ethnicity = gr.Dropdown(label = "Hispanic Ethnicity", choices = ['No', 'Yes'], type = 'index', value = 'No') Insurance_Status = gr.Dropdown(label = "Insurance Status", choices = ['Government', 'Private', 'Not insured'], type = 'index', value = 'Government') Facility_Type = gr.Dropdown(label = "Facility Type", choices = ['Academic/Research Program', 'Community Cancer Program', 'Integrated Network Cancer Program'], type = 'index', value = 'Academic/Research Program') Facility_Location = gr.Dropdown(label = "Facility Location", choices = ['Northeast', 'South', 'Midwest', 'West'], type = 'index', value = 'Northeast') CharlsonDeyo_Score = gr.Dropdown(label = "Charlson-Deyo Score", choices = ['0', '1', '≥ 2'], type = 'index', value = '0') Histology = gr.Dropdown(label = "Histology", choices = ['Chordoma, NOS', 'Chondroid Chordoma', 'Dedifferentiated Chordoma'], type = 'index', value = 'Chordoma, NOS') Primary_Site = gr.Dropdown(label = "Primary Site", choices = ['Spine', 'Sacrum/Pelvis'], type = 'index', value = 'Spine') Maximum_Tumor_Dimension = gr.Dropdown(label = "Maximum Tumor Dimension", choices = ['0-5 cm', '5-10 cm', '10+ cm'], type = 'index', value = '0-5 cm') Distant_Metastasis = gr.Dropdown(label = 'Distant Metastasis', choices = ['No distant metastasis', 'Distant metastasis',], type = 'index', value = 'No distant metastasis') Surgery = gr.Dropdown(label = "Surgery", choices = ['No', 'Yes'], type = 'index', value = 'Yes') Surgical_Margins = gr.Dropdown(label = "Surgical Margins", choices = ['No residual tumor', 'Residual tumor', 'No surgery was performed'], type = 'index', value = 'No residual tumor') Radiotherapy = gr.Dropdown(label = "Radiotherapy", choices = ['No', 'Yes'], type = 'index', value = 'No') Chemotherapy = gr.Dropdown(label = "Chemotherapy", choices = ['No', 'Yes'], type = 'index', value = 'No') with gr.Column(): with gr.Box(): gr.Markdown( """

5-Year Survival


This model uses the CatBoost algorithm.

""" ) with gr.Row(): y1_predict_btn = gr.Button(value="Predict") gr.Markdown( """
""" ) label1 = gr.Markdown() gr.Markdown( """
""" ) with gr.Row(): y1_interpret_btn = gr.Button(value="Explain") gr.Markdown( """
""" ) plot1 = gr.Plot() gr.Markdown( """
""" ) with gr.Box(): gr.Markdown( """

10-Year Survival


This model uses the CatBoost algorithm.

""" ) with gr.Row(): y2_predict_btn = gr.Button(value="Predict") gr.Markdown( """
""" ) label2 = gr.Markdown() gr.Markdown( """
""" ) with gr.Row(): y2_interpret_btn = gr.Button(value="Explain") gr.Markdown( """
""" ) plot2 = gr.Plot() gr.Markdown( """
""" ) y1_predict_btn.click( y1_predict, inputs = [Histology, Primary_Site, Age, Sex, Race, Hispanic_Ethnicity, Insurance_Status, Facility_Type, Facility_Location, CharlsonDeyo_Score, Maximum_Tumor_Dimension, Distant_Metastasis, Surgery, Surgical_Margins, Chemotherapy, Radiotherapy], outputs = [label1] ) y2_predict_btn.click( y2_predict, inputs = [Histology, Primary_Site, Age, Sex, Race, Hispanic_Ethnicity, Insurance_Status, Facility_Type, Facility_Location, CharlsonDeyo_Score, Maximum_Tumor_Dimension, Distant_Metastasis, Surgery, Surgical_Margins, Chemotherapy, Radiotherapy], outputs = [label2] ) y1_interpret_btn.click( y1_interpret, inputs = [Histology, Primary_Site, Age, Sex, Race, Hispanic_Ethnicity, Insurance_Status, Facility_Type, Facility_Location, CharlsonDeyo_Score, Maximum_Tumor_Dimension, Distant_Metastasis, Surgery, Surgical_Margins, Chemotherapy, Radiotherapy], outputs = [plot1], ) y2_interpret_btn.click( y2_interpret, inputs = [Histology, Primary_Site, Age, Sex, Race, Hispanic_Ethnicity, Insurance_Status, Facility_Type, Facility_Location, CharlsonDeyo_Score, Maximum_Tumor_Dimension, Distant_Metastasis, Surgery, Surgical_Margins, Chemotherapy, Radiotherapy], outputs = [plot2], ) gr.Markdown( """

Disclaimer

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 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.

By using this tool, you accept all of the above terms.

""" ) demo.launch()