File size: 10,126 Bytes
cede853 f075328 cede853 f075328 cede853 f075328 cede853 3cbe9f8 f075328 cede853 efebdf7 cede853 3cbe9f8 cede853 bfaadf3 cede853 f919cf9 f075328 cede853 fefa547 cede853 24fc251 cede853 b9bbc30 cede853 c1fc42d 24fc251 cede853 ed4544a cede853 144efcd cede853 144efcd cede853 144efcd cede853 1517327 cede853 1517327 541c63a e8bd60b cede853 e8bd60b cede853 e8bd60b cede853 e8bd60b cede853 e8bd60b cede853 e8bd60b cede853 e8bd60b cede853 e8bd60b cede853 e8bd60b cede853 541c63a cede853 620bb03 cede853 1517327 cede853 1517327 cede853 b9bbc30 cede853 1517327 cede853 1517327 cede853 1517327 cede853 620bb03 cede853 620bb03 cede853 |
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 |
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.utils import resample
from sklearn.calibration import CalibratedClassifierCV
import shap
import gradio as gr
import random
import re
import textwrap
from datasets import load_dataset
#Read data.
x1 = load_dataset("mertkarabacak/DMVO-mRS", data_files="gradio_data_x.csv", use_auth_token = HF_TOKEN)
x1 = pd.DataFrame(x1['train'])
x1 = x1.iloc[:, 1:]
print(x1.columns)
print(x1.shape)
#Define feature names.
f1_names = ['Age', 'Current or Former Smoker', 'Diabetes', 'History of Malignancy', 'Prior DVT or PE', 'Admission BMI', 'Admission NIHSS Score', 'Premorbid mRS Score', 'Occlusion Laterality', 'Hyperdense MCA', 'MT - Type of Thrombectomy', 'MT - mTICI Score', 'Hypertension', 'Antiplatelet Use', 'Admission Hemoglobin', 'Intravenous Thrombolysis']
#Prepare training data for the outcome 1.
y1 = x1.pop('OUTCOME')
#Training model.
from tabpfn import TabPFNClassifier
tabpfn = TabPFNClassifier(device='cuda', N_ensemble_configurations=8)
y1_calib_model = CalibratedClassifierCV(tabpfn, method='isotonic', cv=5)
y1_calib_model = y1_calib_model.fit(x1, y1)
y1_explainer = shap.Explainer(y1_calib_model.predict, x1)
output_y1 = (
"""
<br/>
<center><h3 style='font-size: 24px;'>The probability of 90-day mRS score of 3-6:</center>
<br/>
<center><h1 style='font-size: 36px;'>{}%</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_percent = round(prob * 100) # Round to nearest whole number
output = output_y1.format(prob_percent)
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 = 16, show = False, feature_names = f1_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 = "DMVO-mRS") as demo:
gr.Markdown(
"""
<br/>
<center><h2>NOT FOR CLINICAL USE</h2><center>
<br/>
<center><h1>DMVO 90-Day mRS Score</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>Precision</th>
<td>0.711 (0.634 - 0.765)</td>
</tr>
<tr>
<th>Recall</th>
<td>0.628 (0.553 - 0.694)</td>
</tr>
<tr>
<th>F1 Score</th>
<td>0.656 (0.585 - 0.708)</td>
</tr>
<tr>
<th>Accuracy</th>
<td>0.724 (0.696 - 0.752)</td>
</tr>
<tr>
<th>Matthew's Correlation Coefficient</th>
<td>0.450 (0.390 - 0.503)</td>
</tr>
<tr>
<th>AUROC</th>
<td>0.815 (0.790 - 0.841)</td>
</tr>
<tr>
<th>AUPRC</th>
<td>0.808 (0.781 - 0.832)</td>
</tr>
<tr>
<th>Brier Score</th>
<td>0.190 (0.177 - 0.202)</td>
</tr>
</table>
"""
)
with gr.Row():
with gr.Column():
Age = gr.Slider(label="Age", minimum = 18, maximum = 99, step = 1, value = 55)
Current_or_Former_Smoker = gr.Dropdown(label = "Current or Former Smoker", choices = ['No', 'Yes'], type = 'index', value = 'No')
Diabetes = gr.Dropdown(label = "Diabetes", choices = ['No', 'Yes'], type = 'index', value = 'No')
Hypertension = gr.Dropdown(label = "Hypertension", choices = ['No', 'Yes'], type = 'index', value = 'No')
History_of_Malignancy = gr.Dropdown(label = "History of Malignancy", choices = ['No', 'Yes'], type = 'index', value = 'No')
DVT_or_PE = gr.Dropdown(label = "Prior DVT or PE", choices = ['No', 'Yes'], type = 'index', value = 'No')
Antiplatelet_Use = gr.Dropdown(label = "Antiplatelet Use", choices = ['No', 'Yes'], type = 'index', value = 'No')
Admission_BMI = gr.Slider(label="Admission BMI", minimum = 15, maximum = 60, step = 0.1, value = 25)
Admission_Hemoglobin = gr.Slider(label="Admission Hemoglobin", minimum = 5, maximum = 25, step = 0.1, value = 15)
Admission_NIHSS = gr.Slider(label="Admission NIHSS Score", minimum = 0, maximum = 42, step = 1, value = 1)
Premorbid_mRS = gr.Slider(label="Premorbid mRS Score", minimum = 0, maximum = 5, step = 1, value = 0)
Occlusion_Laterality = gr.Dropdown(label = "Occlusion Laterality", choices = ['Left', 'Right'], type = 'index', value = 'Left')
Hyperdense_MCA = gr.Dropdown(label = "Hyperdense MCA", choices = ['No', 'Yes'], type = 'index', value = 'No')
IVTPA = gr.Dropdown(label = "Intravenous Thrombolysis", choices = ['No', 'Yes'], type = 'index', value = 'No')
Type_of_Thrombectomy = gr.Dropdown(label = "MT - Type of Thrombectomy", choices = ['MT not attempted', 'Direct aspiration', 'Stent retriever', 'Combined'], type = 'index', value = 'Stent retriever')
mTICI = gr.Dropdown(label = 'MT - mTICI Score', choices = ['MT not attempted' '0', '1', '2a', '2b', '2c', '3'], type = 'index', value = '3')
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 = [Age, Current_or_Former_Smoker, Diabetes, History_of_Malignancy, DVT_or_PE, Admission_BMI, Admission_NIHSS, Premorbid_mRS, Occlusion_Laterality, Hyperdense_MCA, Type_of_Thrombectomy, mTICI, Hypertension, Antiplatelet_Use, Admission_Hemoglobin, IVTPA],
outputs = [label1]
)
y1_interpret_btn.click(
y1_interpret,
inputs = [Age, Current_or_Former_Smoker, Diabetes, History_of_Malignancy, DVT_or_PE, Admission_BMI, Admission_NIHSS, Premorbid_mRS, Occlusion_Laterality, Hyperdense_MCA, Type_of_Thrombectomy, mTICI, Hypertension, Antiplatelet_Use, Admission_Hemoglobin, IVTPA],
outputs = [plot1],
)
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() |