Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
214 lines (189 loc) · 10.1 KB
/
Copy pathapp.py
File metadata and controls
214 lines (189 loc) · 10.1 KB
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
"""
app.py — Stroke Risk Prediction Streamlit App
Trains the model at startup (cached) to avoid sklearn version mismatch with pickle.
Usage:
streamlit run app.py
"""
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st
from pathlib import Path
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import (
RandomForestClassifier, HistGradientBoostingClassifier, StackingClassifier
)
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
from sklearn.model_selection import StratifiedKFold, train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.utils import resample
warnings.filterwarnings("ignore")
RANDOM_STATE = 42
st.set_page_config(page_title="Stroke Risk Predictor", page_icon="🧠", layout="wide")
def engineer_features(df):
df = df.copy()
df["glucose_risk"] = pd.cut(
df["avg_glucose_level"], bins=[0, 100, 125, 999],
labels=["Normal", "Prediabetic", "Diabetic"]
).astype(str)
df["bmi_category"] = pd.cut(
df["bmi"], bins=[0, 18.5, 25, 30, 999],
labels=["Underweight", "Normal", "Overweight", "Obese"]
).astype(str)
df["age_group"] = pd.cut(
df["age"], bins=[0, 40, 55, 65, 999],
labels=["Under40", "40to55", "55to65", "Over65"]
).astype(str)
df["comorbidity_score"] = df["hypertension"].astype(int) + df["heart_disease"].astype(int)
df["age_x_hypertension"] = df["age"] * df["hypertension"]
df["age_x_heart_disease"] = df["age"] * df["heart_disease"]
return df
@st.cache_resource(show_spinner="Training model... (first load only, ~30 seconds)")
def load_model():
for p in [Path("data/healthcare-dataset-stroke-data.csv"),
Path("../data/healthcare-dataset-stroke-data.csv")]:
if p.exists():
df = pd.read_csv(p)
break
else:
st.error("data/healthcare-dataset-stroke-data.csv not found.")
st.stop()
df = df[df["gender"] != "Other"].reset_index(drop=True)
df = engineer_features(df)
X = df.drop(columns=["id", "stroke"])
y = df["stroke"]
num_feats = ["age","avg_glucose_level","bmi","comorbidity_score","age_x_hypertension","age_x_heart_disease"]
bin_feats = ["hypertension","heart_disease"]
cat_feats = ["gender","ever_married","work_type","Residence_type","smoking_status","glucose_risk","bmi_category","age_group"]
preprocessor = ColumnTransformer([
("num", Pipeline([("i", SimpleImputer(strategy="median")), ("s", StandardScaler())]), num_feats),
("bin", Pipeline([("i", SimpleImputer(strategy="most_frequent"))]), bin_feats),
("cat", Pipeline([("i", SimpleImputer(strategy="most_frequent")),
("e", OneHotEncoder(handle_unknown="ignore", sparse_output=False))]), cat_feats),
])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=RANDOM_STATE, stratify=y)
X_train_proc = preprocessor.fit_transform(X_train)
y_arr = np.array(y_train)
X_maj, X_min = X_train_proc[y_arr==0], X_train_proc[y_arr==1]
target = len(X_maj) // 4
X_min_over = resample(X_min, n_samples=target, replace=True, random_state=RANDOM_STATE)
X_bal = np.vstack([X_maj, X_min_over])
y_bal = np.concatenate([np.zeros(len(X_maj), dtype=int), np.ones(target, dtype=int)])
idx = np.random.RandomState(RANDOM_STATE).permutation(len(X_bal))
X_bal, y_bal = X_bal[idx], y_bal[idx]
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=RANDOM_STATE)
model = StackingClassifier(
estimators=[
("lr", LogisticRegression(max_iter=2000, class_weight="balanced", random_state=RANDOM_STATE)),
("rf", RandomForestClassifier(n_estimators=300, max_depth=8, min_samples_leaf=5,
class_weight="balanced", random_state=RANDOM_STATE, n_jobs=-1)),
("hgb", HistGradientBoostingClassifier(max_iter=200, max_depth=6, learning_rate=0.05,
class_weight="balanced", random_state=RANDOM_STATE)),
],
final_estimator=LogisticRegression(max_iter=2000, class_weight="balanced", random_state=RANDOM_STATE),
cv=cv, passthrough=False, n_jobs=-1
)
model.fit(X_bal, y_bal)
X_test_proc = preprocessor.transform(X_test)
probas = model.predict_proba(X_test_proc)[:, 1]
ts = np.linspace(0.05, 0.95, 200)
f1s = [f1_score(np.array(y_test), (probas >= t).astype(int), zero_division=0) for t in ts]
return preprocessor, model, float(ts[np.argmax(f1s)])
def risk_tier(p):
if p < 0.20: return "Low", "#27ae60", "✅"
elif p < 0.50: return "Moderate", "#f39c12", "⚠️"
else: return "High", "#e74c3c", "🚨"
def draw_gauge(prob):
fig, ax = plt.subplots(figsize=(4, 2.2), subplot_kw={"aspect": "equal"})
fig.patch.set_facecolor("none"); ax.set_facecolor("none")
theta = np.linspace(np.pi, 0, 200)
ax.plot(np.cos(theta), np.sin(theta), color="#e0e0e0", linewidth=18, solid_capstyle="round")
_, color, _ = risk_tier(prob)
theta_fill = np.linspace(np.pi, np.pi - prob * np.pi, 200)
ax.plot(np.cos(theta_fill), np.sin(theta_fill), color=color, linewidth=18, solid_capstyle="round")
angle = np.pi - prob * np.pi
ax.annotate("", xy=(0.6*np.cos(angle), 0.6*np.sin(angle)), xytext=(0,0),
arrowprops=dict(arrowstyle="-|>", color="#2c3e50", lw=2))
ax.plot(0, 0, "o", color="#2c3e50", markersize=8)
ax.text(0, -0.25, f"{prob:.1%}", ha="center", va="center", fontsize=18, fontweight="bold", color=color)
ax.set_xlim(-1.2, 1.2); ax.set_ylim(-0.4, 1.1); ax.axis("off")
return fig
def draw_factors(row):
factors = {
"Age": min(row["age"] / 100, 1.0),
"Avg Glucose": min((row["avg_glucose_level"] - 50) / 250, 1.0),
"BMI": min((row["bmi"] - 10) / 50, 1.0),
"Hypertension": float(row["hypertension"]),
"Heart Disease": float(row["heart_disease"]),
"Comorbidities": (row["hypertension"] + row["heart_disease"]) / 2,
}
vals = list(factors.values())
colors = ["#e74c3c" if v > 0.6 else "#f39c12" if v > 0.3 else "#27ae60" for v in vals]
fig, ax = plt.subplots(figsize=(4, 3))
fig.patch.set_facecolor("none"); ax.set_facecolor("none")
ax.barh(list(factors.keys()), vals, color=colors, edgecolor="white", height=0.6)
ax.set_xlim(0, 1); ax.set_xlabel("Relative Risk Level", fontsize=9)
ax.set_title("Key Risk Factors", fontsize=10, fontweight="bold", pad=8)
ax.tick_params(labelsize=9); ax.spines[["top","right"]].set_visible(False)
plt.tight_layout()
return fig
preprocessor, model, threshold = load_model()
st.title("🧠 Stroke Risk Predictor")
st.markdown("Enter patient information below to estimate stroke risk. This tool is for **educational and research purposes only** and is not a medical device.")
st.divider()
col_inputs, col_results = st.columns([1, 1], gap="large")
with col_inputs:
st.subheader("Patient Information")
c1, c2 = st.columns(2)
with c1:
age = st.slider("Age", 1, 100, 55)
gender = st.selectbox("Gender", ["Male", "Female"])
ever_married = st.selectbox("Ever Married", ["Yes", "No"])
work_type = st.selectbox("Work Type", ["Private","Self-employed","Govt_job","children","Never_worked"])
with c2:
residence = st.selectbox("Residence Type", ["Urban", "Rural"])
smoking = st.selectbox("Smoking Status", ["never smoked","formerly smoked","smokes","Unknown"])
hypertension = st.checkbox("Hypertension")
heart_disease = st.checkbox("Heart Disease")
st.markdown("**Clinical Measurements**")
c3, c4 = st.columns(2)
with c3:
glucose = st.slider("Avg Glucose Level (mg/dL)", 50.0, 300.0, 100.0, step=0.5)
with c4:
bmi_val = st.slider("BMI", 10.0, 60.0, 25.0, step=0.1)
predict_btn = st.button("Calculate Risk", type="primary", use_container_width=True)
with col_results:
st.subheader("Risk Assessment")
if predict_btn:
row = {"age": age, "gender": gender, "hypertension": int(hypertension),
"heart_disease": int(heart_disease), "ever_married": ever_married,
"work_type": work_type, "Residence_type": residence,
"avg_glucose_level": glucose, "bmi": bmi_val, "smoking_status": smoking}
X_proc = preprocessor.transform(engineer_features(pd.DataFrame([row])))
prob = float(model.predict_proba(X_proc)[0, 1])
tier, color, icon = risk_tier(prob)
st.pyplot(draw_gauge(prob), use_container_width=False)
st.markdown(
f"<div style='text-align:center;padding:10px;border-radius:8px;"
f"background:{color}22;border:2px solid {color};'>"
f"<span style='font-size:24px'>{icon}</span>"
f"<span style='font-size:20px;font-weight:bold;color:{color}'> {tier} Risk</span></div>",
unsafe_allow_html=True)
st.markdown("")
st.pyplot(draw_factors(row), use_container_width=False)
with st.expander("What does this mean?"):
msgs = {
"Low": "This patient profile shows **low relative stroke risk**. Maintaining a healthy lifestyle and routine checkups are recommended.",
"Moderate": "This patient profile shows **moderate stroke risk**. Factors such as glucose levels, BMI, or cardiovascular history may warrant closer monitoring.",
"High": "This patient profile shows **elevated stroke risk**. Multiple high-risk factors are present. Closer clinical evaluation and preventive intervention are recommended.",
}
st.markdown(msgs[tier])
st.caption("⚠️ For educational purposes only. Not validated for clinical use.")
else:
st.info("Fill in patient details and click **Calculate Risk** to see the assessment.")
st.divider()
st.caption("Model: Stacking Ensemble (LR + RF + HistGBM) | Dataset: UCI Stroke Prediction | Built by Ansh Patel — github.com/anshcpatel11")