Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -251,9 +251,14 @@ def prepare_clinical_features(
|
|
| 251 |
def get_top_shap_features(clinical_feat, top_n=6):
|
| 252 |
"""
|
| 253 |
Compute SHAP values for the clinical (GB) model's prediction and
|
| 254 |
-
return the top_n features ranked by absolute impact
|
|
|
|
|
|
|
| 255 |
clinical_feat must be the scaled numpy array in ALL_COLS order.
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
| 257 |
"""
|
| 258 |
shap_values = shap_explainer.shap_values(clinical_feat)
|
| 259 |
|
|
@@ -263,8 +268,26 @@ def get_top_shap_features(clinical_feat, top_n=6):
|
|
| 263 |
else:
|
| 264 |
shap_row = shap_values[0]
|
| 265 |
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
# ββ Grad-CAM++ ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 270 |
class GradCAMPlusPlus:
|
|
@@ -501,8 +524,8 @@ def predict_ckd_risk(
|
|
| 501 |
# 5. GB clinical prediction
|
| 502 |
clinical_prob = gb_model.predict_proba(clinical_feat)[0, 1]
|
| 503 |
|
| 504 |
-
# 5.b Top-6 SHAP features driving the clinical prediction
|
| 505 |
-
top_shap_features = get_top_shap_features(clinical_feat, top_n=6)
|
| 506 |
|
| 507 |
# 6. Fusion model prediction
|
| 508 |
retinal_tensor = torch.tensor(retinal_feat_pca, dtype=torch.float32)
|
|
@@ -592,7 +615,8 @@ def predict_ckd_risk(
|
|
| 592 |
Risk Level: {risk_label}
|
| 593 |
β βββββββββββββββββββββββββββββββββββββββ£
|
| 594 |
TOP 6 CLINICAL FEATURES (SHAP)
|
| 595 |
-
{chr(10).join(f" {i+1}. {name:<8s} {'+' if val >= 0 else ''}{val:.4f}" for i, (name, val) in enumerate(top_shap_features))}
|
|
|
|
| 596 |
ββββββββββββββββββββββββββββββββββββββββ
|
| 597 |
"""
|
| 598 |
|
|
|
|
| 251 |
def get_top_shap_features(clinical_feat, top_n=6):
|
| 252 |
"""
|
| 253 |
Compute SHAP values for the clinical (GB) model's prediction and
|
| 254 |
+
return the top_n features ranked by absolute impact, together with
|
| 255 |
+
each feature's percentage share of total |SHAP| impact and the
|
| 256 |
+
combined percentage share of all remaining features.
|
| 257 |
clinical_feat must be the scaled numpy array in ALL_COLS order.
|
| 258 |
+
|
| 259 |
+
Returns:
|
| 260 |
+
top_features: list of (feature_name, shap_value, pct_impact) tuples
|
| 261 |
+
other_pct: combined percentage impact of the remaining features
|
| 262 |
"""
|
| 263 |
shap_values = shap_explainer.shap_values(clinical_feat)
|
| 264 |
|
|
|
|
| 268 |
else:
|
| 269 |
shap_row = shap_values[0]
|
| 270 |
|
| 271 |
+
abs_vals = np.abs(shap_row)
|
| 272 |
+
total_abs = abs_vals.sum()
|
| 273 |
+
|
| 274 |
+
# Avoid division by zero in the (unlikely) case all SHAP values are 0
|
| 275 |
+
if total_abs == 0:
|
| 276 |
+
pct_vals = np.zeros_like(abs_vals)
|
| 277 |
+
else:
|
| 278 |
+
pct_vals = (abs_vals / total_abs) * 100.0
|
| 279 |
+
|
| 280 |
+
sorted_idx = np.argsort(abs_vals)[::-1]
|
| 281 |
+
top_idx = sorted_idx[:top_n]
|
| 282 |
+
other_idx = sorted_idx[top_n:]
|
| 283 |
+
|
| 284 |
+
top_features = [
|
| 285 |
+
(ALL_COLS[i], float(shap_row[i]), float(pct_vals[i]))
|
| 286 |
+
for i in top_idx
|
| 287 |
+
]
|
| 288 |
+
other_pct = float(pct_vals[other_idx].sum()) if len(other_idx) else 0.0
|
| 289 |
+
|
| 290 |
+
return top_features, other_pct
|
| 291 |
|
| 292 |
# ββ Grad-CAM++ ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 293 |
class GradCAMPlusPlus:
|
|
|
|
| 524 |
# 5. GB clinical prediction
|
| 525 |
clinical_prob = gb_model.predict_proba(clinical_feat)[0, 1]
|
| 526 |
|
| 527 |
+
# 5.b Top-6 SHAP features driving the clinical prediction (with % impact)
|
| 528 |
+
top_shap_features, other_shap_pct = get_top_shap_features(clinical_feat, top_n=6)
|
| 529 |
|
| 530 |
# 6. Fusion model prediction
|
| 531 |
retinal_tensor = torch.tensor(retinal_feat_pca, dtype=torch.float32)
|
|
|
|
| 615 |
Risk Level: {risk_label}
|
| 616 |
β βββββββββββββββββββββββββββββββββββββββ£
|
| 617 |
TOP 6 CLINICAL FEATURES (SHAP)
|
| 618 |
+
{chr(10).join(f" {i+1}. {name:<8s} {'+' if val >= 0 else ''}{val:.4f} ({pct:.1f}% impact)" for i, (name, val, pct) in enumerate(top_shap_features))}
|
| 619 |
+
Other 18 factors combined: {other_shap_pct:.1f}% impact
|
| 620 |
ββββββββββββββββββββββββββββββββββββββββ
|
| 621 |
"""
|
| 622 |
|