Spaces:
Sleeping
Sleeping
Update app.py
Browse files250 unmodified lines2512522532542552562572582593 unmodified lines263264265266267268269270230 unmodified lines50150250350450550650750883 unmodified lines592593594595596597598250 unmodified linesdef get_top_shap_features(clinical_feat, top_n=6):"""Compute SHAP values for the clinical (GB) model's prediction andreturn the top_n features ranked by absolute impact.clinical_feat must be the scaled numpy array in ALL_COLS order.Returns a list of (feature_name, shap_value) tuples."""shap_values = shap_explainer.shap_values(clinical_feat)3 unmodified lineselse:shap_row = shap_values[0]top_idx = np.argsort(np.abs(shap_row))[::-1][:top_n]return [(ALL_COLS[i], float(shap_row[i])) for i in top_idx]# ββ Grad-CAM++ ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββclass GradCAMPlusPlus:230 unmodified lines# 5. GB clinical predictionclinical_prob = gb_model.predict_proba(clinical_feat)[0, 1]# 5.b Top-6 SHAP features driving the clinical predictiontop_shap_features = get_top_shap_features(clinical_feat, top_n=6)# 6. Fusion model predictionretinal_tensor = torch.tensor(retinal_feat_pca, dtype=torch.float32)83 unmodified linesRisk Level: {risk_label}β βββββββββββββββββββββββββββββββββββββββ£TOP 6 CLINICAL FEATURES (SHAP){chr(10).join(f" {i+1}. {name:<8s} {'+' if val >= 0 else ''}{val:.4f}" for i, (name, val) in enumerate(top_shap_features))}ββββββββββββββββββββββββββββββββββββββββ"""250 unmodified lines2512522532542552562572582592602612622632643 unmodified lines268269270271272273274275276277278279280281282283284285286287288289290291292293230 unmodified lines52452552652752852953053183 unmodified lines615616617618619620621622250 unmodified linesdef get_top_shap_features(clinical_feat, top_n=6):"""Compute SHAP values for the clinical (GB) model's prediction andreturn the top_n features ranked by absolute impact, together witheach feature's percentage share of total |SHAP| impact and thecombined percentage share of all remaining features.clinical_feat must be the scaled numpy array in ALL_COLS order.Returns:top_features: list of (feature_name, shap_value, pct_impact) tuplesother_pct: combined percentage impact of the remaining features"""shap_values = shap_explainer.shap_values(clinical_feat)3 unmodified lineselse:shap_row = shap_values[0]abs_vals = np.abs(shap_row)total_abs = abs_vals.sum()# Avoid division by zero in the (unlikely) case all SHAP values are 0if total_abs == 0:pct_vals = np.zeros_like(abs_vals)else:pct_vals = (abs_vals / total_abs) * 100.0sorted_idx = np.argsort(abs_vals)[::-1]top_idx = sorted_idx[:top_n]other_idx = sorted_idx[top_n:]top_features = [(ALL_COLS[i], float(shap_row[i]), float(pct_vals[i]))for i in top_idx]other_pct = float(pct_vals[other_idx].sum()) if len(other_idx) else 0.0return top_features, other_pct# ββ Grad-CAM++ ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββclass GradCAMPlusPlus:230 unmodified lines# 5. GB clinical predictionclinical_prob = gb_model.predict_proba(clinical_feat)[0, 1]# 5.b Top-6 SHAP features driving the clinical prediction (with % impact)top_shap_features, other_shap_pct = get_top_shap_features(clinical_feat, top_n=6)# 6. Fusion model predictionretinal_tensor = torch.tensor(retinal_feat_pca, dtype=torch.float32)83 unmodified linesRisk Level: {risk_label}β βββββββββββββββββββββββββββββββββββββββ£TOP 6 CLINICAL FEATURES (SHAP){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))}Other 18 factors combined: {other_shap_pct:.1f}% impactββββββββββββββββββββββββββββββββββββββββ"""
app.py+31-7