File size: 2,896 Bytes
ee31ead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import random

# -----------------------------
# Deterministic Seed
# -----------------------------
SEED = 42

# -----------------------------
# Core Simulation
# -----------------------------
def generate_demo(n):
    random.seed(SEED)

    age_groups = ["young", "middle", "elderly"]
    ethnicities = ["African", "South Asian", "Caucasian"]
    comorbidity_sets = [
        "none",
        "diabetes",
        "diabetes+hypertension",
        "diabetes+hypertension+ckd"
    ]

    data = []

    for i in range(n):
        age_group = random.choice(age_groups)
        ethnicity = random.choice(ethnicities)
        comorbidity = random.choice(comorbidity_sets)

        # -----------------------------
        # Simulated Model Behavior
        # -----------------------------
        base_score = 0.9

        # Degrade based on real-world signals
        if age_group == "elderly":
            base_score -= 0.15

        if "ckd" in comorbidity:
            base_score -= 0.2
        elif "hypertension" in comorbidity:
            base_score -= 0.1

        if ethnicity == "African":
            base_score -= 0.05

        score = max(0.3, min(0.95, base_score))
        failure = 1 if score < 0.7 else 0

        data.append({
            "patient_id": i,
            "age_group": age_group,
            "ethnicity": ethnicity,
            "comorbidity": comorbidity,
            "model_score": round(score, 2),
            "failure": failure
        })

    df = pd.DataFrame(data)

    # -----------------------------
    # Failure Summary
    # -----------------------------
    summary = df.groupby(
        ["age_group", "ethnicity", "comorbidity"]
    ).agg(
        patients=("patient_id", "count"),
        failures=("failure", "sum")
    ).reset_index()

    summary["failure_rate"] = (summary["failures"] / summary["patients"]).round(2)

    return df, summary


# -----------------------------
# UI Logic
# -----------------------------
def run_demo(n):
    df, summary = generate_demo(n)
    return df, summary


# -----------------------------
# Interface
# -----------------------------
with gr.Blocks(title="HipAAsynth Lab") as demo:

    gr.Markdown("""
# HipAAsynth Lab

### Simulating real-world conditions to expose model failure

This lab demonstrates how model performance degrades across:

- patient populations  
- demographic variation  
- comorbidity complexity  

Models that perform well in controlled testing often fail under these conditions.
""")

    n = gr.Slider(50, 300, value=100, step=10, label="Number of Patients")

    run = gr.Button("Run Validation Simulation")

    gr.Markdown("## Patient-Level Output")
    table = gr.Dataframe()

    gr.Markdown("## Failure Breakdown (Where Models Break)")
    summary = gr.Dataframe()

    run.click(fn=run_demo, inputs=n, outputs=[table, summary])


demo.launch()