Spaces:
Running
Running
File size: 7,096 Bytes
f70eb93 | 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 | import streamlit as st
import pandas as pd
import numpy as np
from modules.pre_processing import pre_processing
from modules.nested_calibration_cv import nested_cv_calibration
# 設定頁面配置
st.set_page_config(
page_title="CLL Prediction",
layout="wide",
initial_sidebar_state="expanded",
)
# # CSS 客製化 sidebar 寬度
# st.markdown(
# """
# <style>
# section[data-testid="stSidebar"] {
# width: 500px !important;
# }
# </style>
# """,
# unsafe_allow_html=True,
# )
@st.cache_resource
def get_calibration():
return nested_cv_calibration()
# Initialize session state
if "predict_clicked" not in st.session_state:
st.session_state.predict_clicked = False
if "input_payload" not in st.session_state:
st.session_state.input_payload = None
st.title("Chronic Lymphocytic Leukemia Prediction")
st.caption("Please fill in the fields on the left and click \"Predict\".")
with st.expander("Explanation of HL p-value / Brier Score / AUC"):
col1, col2, col3 = st.columns(3)
with col1:
st.markdown("**HL p-value**: The p-value of the Hosmer-Lemeshow test. A p-value **higher than 0.05** indicates that the model is fit well.")
with col2:
st.markdown("**Brier Score**: The Brier score is a measure of the accuracy of probability predictions. A **closer to 0** indicates a better model.")
with col3:
st.markdown("**AUC**: The area under the receiver operating characteristic curve. A **higher AUC** indicates a better model.")
st.sidebar.title(" ⚙️Input")
with st.sidebar.form("prediction_form"):
st.subheader("Lymphocyte count(×10⁹/L)", help="Normal range is around 1.0-4.8 ×10⁹/L.")
lym_count = st.number_input(
"",
min_value=0.0,
max_value=100.0,
value=1.0,
step=0.1
)
st.subheader("Sex")
sex = st.radio("", ("Male", "Female"))
st.subheader("Past chemotherapy treatment")
past_chemo = st.radio("", ("Yes", "No"), key="past_chemo")
st.subheader("Sleep duration (hours)")
slp_duration = st.slider("", min_value=0, max_value=24, value=8)
st.subheader("Enlarged liver and spleen")
enlarge_liver_spleen = st.radio("", ("Yes", "No"), key="enlarge_liver_spleen")
st.subheader("Unusual blood test results")
unusual_blood_test = st.radio("", ("Yes", "No"), key="unusual_blood_test")
st.subheader("History of smoking")
smoking_history = st.radio("In the past, how often have you smoked tobacco?", (
"Smoked on most or all days",
"Smoked occasionally",
"Just tried once or twice",
"I have never smoked",
), key="smoking_history")
st.subheader("History of major muscle & joint conditions", help="e.g., Rheumatoid Arthritis, Lupus, Chronic Disc Herniation, or past surgeries. Do not include temporary muscle soreness.")
muscle_joint_conditions = st.radio("", ("Yes", "No"),key="muscle_joint_conditions")
st.subheader("Metastasis (cancer spread to lungs or digestive system)")
metastasis = st.radio("", ("Yes", "No"), key="metastasis")
submitted = st.form_submit_button("Predict")
st.sidebar.markdown("The UK Biobank CLL Risk Prediction tool was established based on UK Biobank study cohort. The tool was developed on research purpose and cannot be used as clinical evidence.")
if submitted:
st.session_state.predict_clicked = True
st.session_state.input_payload = {
"Lymphocyte count": lym_count,
"Sex": sex,
"Past chemotherapy treatment": past_chemo,
"Sleep duration": slp_duration,
"Enlarged liver and spleen": enlarge_liver_spleen,
"Unusual blood test results": unusual_blood_test,
"Smoking history": smoking_history,
"History of muscle & joint conditions": muscle_joint_conditions,
"Metastasis": metastasis,
}
# numeric_columns = ['Lymphocyte count', 'Sleep duration']
# categorical_columns = ['Sex', 'Past chemotherapy treatment', 'Enlarged liver and spleen', 'Unusual blood test results', 'Smoking history', 'History of muscle & joint conditions', 'Metastasis']
if st.session_state.predict_clicked and st.session_state.input_payload is not None:
msg = st.empty() # 建立一個可被清空的區塊
msg.success("Data submitted, starting prediction process.")
Fix_Param = {
'learning_rate': 0.05,
'n_estimators': 200,
'num_leaves': 6,
'boosting_type' : 'gbdt',
'objective' : 'binary',
'max_bin':225,
'random_state' : 500,
'colsample_bytree' : 0.64,
'subsample' : 0.7,
'reg_alpha' : 0.8,
'reg_lambda' : 1.8,
'is_unbalance': True, # imbalanced data
}
input_df = pd.DataFrame([st.session_state.input_payload])
processed_df = pre_processing(input_df) # already scaled and encoded
X_np_5y = np.load("./plot_reference/5y/5y_balanced_X.npy"); y_np_5y = np.load("./plot_reference/5y/5y_balanced_y.npy");
X_np_10y = np.load("./plot_reference/10y/10y_balanced_X.npy"); y_np_10y = np.load("./plot_reference/10y/10y_balanced_y.npy");
X_np_all = np.load("./plot_reference/all/all_balanced_X.npy"); y_np_all = np.load("./plot_reference/all/all_balanced_y.npy");
# get risk from model (use processed features to match training data)
fig_5y = nested_cv_calibration(X_np_5y, y_np_5y, Fix_Param, "5-year CLL Risk",
calibration_method="isotonic",
input_df=processed_df,
num_groups=4,
output_name=None,
feature_names=None)
col_left, col_right = st.columns([5, 1])
with col_right:
st.markdown("**5-year model AUC: 0.876**", unsafe_allow_html=True)
st.pyplot(fig_5y)
fig_10y = nested_cv_calibration(X_np_10y, y_np_10y, Fix_Param, "10-year CLL Risk",
calibration_method="isotonic",
input_df=processed_df,
num_groups=4,
output_name=None,
feature_names=None)
col_left, col_right = st.columns([5, 1])
with col_right:
st.markdown("**10-year model AUC: 0.873**", unsafe_allow_html=True)
st.pyplot(fig_10y)
fig_all = nested_cv_calibration(X_np_all, y_np_all, Fix_Param, "All CLL Risk",
calibration_method="isotonic",
input_df=processed_df,
num_groups=4,
output_name=None,
feature_names=None)
col_left, col_right = st.columns([5, 1])
with col_right:
st.markdown("**All model AUC: 0.884**", unsafe_allow_html=True)
st.pyplot(fig_all)
msg.empty() # 清空成功訊息
|