File size: 4,765 Bytes
289aafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7909656
289aafa
7909656
 
289aafa
 
7909656
 
 
 
 
 
 
289aafa
7909656
 
289aafa
 
7909656
 
289aafa
 
 
7909656
6ba05f3
7909656
 
 
289aafa
7909656
 
 
 
 
6ba05f3
7909656
 
289aafa
 
7909656
289aafa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ba05f3
 
289aafa
 
 
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
import gradio as gr
import numpy as np
import plotly.graph_objects as go

# Final model coefficients from Elastic Net
INTERCEPT = 2.384
COEFS = {
    "Age": -0.112,
    "C3": -0.327,
    "C4": -0.053,
    "C3/C4": -0.166,
    "Ln_C3C4": 0.135,
    "Ln_C4C3": -0.135
}
THRESHOLD = 0.461

# ------------------------
# Prediction Function
# ------------------------
def predict(age, c3, c4):
    if c4 == 0 or c3 == 0:
        return "Invalid input", None, None, "C3 and C4 must be > 0", ""

    c3_c4 = c3 / c4
    ln_c3c4 = np.log(c3_c4)
    ln_c4c3 = np.log(c4 / c3)

    logit = INTERCEPT + (
        COEFS["Age"] * age +
        COEFS["C3"] * c3 +
        COEFS["C4"] * c4 +
        COEFS["C3/C4"] * c3_c4 +
        COEFS["Ln_C3C4"] * ln_c3c4 +
        COEFS["Ln_C4C3"] * ln_c4c3
    )
    prob = 1 / (1 + np.exp(-logit))

    label = "Likely To Develop Renal Complications" if prob > THRESHOLD else "Not Likely To Develop Renal Complications"
    confidence = f"Model confidence: {round(prob * 100 if prob > THRESHOLD else (1 - prob) * 100)}%"
    interpretation = (
        "This result indicates a higher-than-threshold probability of developing renal complications. "
        "Further clinical evaluation and monitoring is advised."
        if prob > THRESHOLD else
        "The probability of abnormal renal function appears low. Routine follow-up is sufficient unless other risk factors exist."
    )

    return label, create_logit_meter(prob), create_prob_gauge(prob), confidence, interpretation

# ------------------------
# Linear Logit Meter
# ------------------------
def create_logit_meter(prob):
    fig = go.Figure(go.Indicator(
        mode="gauge+number+delta",
        value=prob,
        number={'valueformat': ".3f", 'font': {'size': 36, 'color': "black"}},
        delta={'reference': THRESHOLD, 'increasing': {'color': "red"}, 'decreasing': {'color': "green"}},
        gauge={
            'shape': "bullet",
            'axis': {
                'range': [0, 1],
                'tickwidth': 2,
                'tickcolor': "#666",
                'tickfont': {'size': 16}
            },
            'bar': {'color': "blue", 'thickness': 0.4},
            'steps': [
                {'range': [0, THRESHOLD], 'color': "#ccffcc"},
                {'range': [THRESHOLD, 1], 'color': "#ad3d46"}
            ],
            'threshold': {
                'line': {'color': "black", 'width': 4},
                'thickness': 0.8,
                'value': THRESHOLD
            },
        },
        title={
            "text": f"Logit Risk Probability",
            "font": {'size': 18}
        },
        domain={'x': [0, 1], 'y': [0, 1]}
    ))

    fig.update_layout(
        height=240,
        paper_bgcolor="white",
        font=dict(family="Arial", size=16, color="black"),
        margin=dict(t=40, b=30, l=10, r=10)
    )

    return fig


# ------------------------
# Dynamic Probability Gauge
# ------------------------
def create_prob_gauge(prob):
    if prob > THRESHOLD:
        gauge_value = prob
        title = "Probability of Abnormal Renal Function"
        steps = [
            {'range': [0, 0.33], 'color': "lightgreen"},
            {'range': [0.33, 0.66], 'color': "orange"},
            {'range': [0.66, 1], 'color': "red"}
        ]
    else:
        gauge_value = 1 - prob
        title = "Probability of Normal Renal Function"
        steps = [
            {'range': [0, 0.33], 'color': "red"},
            {'range': [0.33, 0.66], 'color': "orange"},
            {'range': [0.66, 1], 'color': "lightgreen"}
        ]

    fig = go.Figure(go.Indicator(
        mode="gauge+number",
        value=gauge_value,
        number={'valueformat': ".0%"},
        gauge={
            'axis': {'range': [0, 1]},
            'bar': {'color': "black"},
            'steps': steps,
        },
        title={"text": title}
    ))
    fig.update_layout(height=500, margin=dict(t=10, b=10, l=10, r=10))
    return fig

# ------------------------
# Gradio Interface
# ------------------------
demo = gr.Interface(
    fn=predict,
    inputs=[
        gr.Slider(5, 18, step=0.5, label="Age (Years)"),
        gr.Number(label="C3 Level (mg/dL)"),
        gr.Number(label="C4 Level (mg/dL)")
    ],
    outputs=[
        gr.Text(label="Screening Verdict"),
        gr.Plot(label="Logit Risk Meter"),
        gr.Plot(label="Risk Probability Gauge"),
        gr.Text(label="Model Confidence"),
        gr.Textbox(label="Interpretation", lines=3)
    ],
    title="🧪 Renal Complication Prediction Tool In Cases With Paediatric SLE",
    description="Enter Age, C3, and C4 levels to Predict Probability of Future Renal Complication based on a validated dataset of Paeditric SLE dataset by robust Elastic Net Regression."
)

demo.launch()