File size: 13,019 Bytes
b27f097
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import streamlit as st
import plotly.graph_objects as go
import pandas as pd
import numpy as np

# Page setup
st.set_page_config(page_title="IMPROVE Framework", layout="wide")
st.markdown("""
<style>
    .stApp { background-color: #FFFFFF; }
    h1 { color: #000000; font-size: 28px; font-weight: bold; border-bottom: 2px solid #0366D6; padding-bottom: 10px; }
    h2 { color: #000000; font-size: 20px; font-weight: 600; margin-top: 30px; }
    h3 { color: #000000; font-size: 16px; font-weight: 600; }
    p, div, label, span { color: #000000 !important; }
    .stSlider > div > div > div { color: #000000 !important; }
    .metric-box { background: #F5F5F5; border: 1px solid #DDDDDD; border-radius: 8px; padding: 15px; margin: 5px; }
    .metric-label { font-size: 12px; color: #666666; text-transform: uppercase; font-weight: 600; }
    .metric-value { font-size: 24px; color: #000000; font-weight: bold; font-family: monospace; }
    .recommendation-box { padding: 20px; border-radius: 8px; margin-top: 20px; }
    .success { background: #D4EDDA; border-left: 5px solid #28A745; }
    .warning { background: #FFF3CD; border-left: 5px solid #FFC107; }
    .info { background: #D1ECF1; border-left: 5px solid #17A2B8; }
    
    /* SIDEBAR WHITE TEXT */
    .css-1d391kg, .css-1lcbmhc, section[data-testid="stSidebar"] {
        background-color: #1a1a1a !important;
    }
    .css-1d391kg .stMarkdown, .css-1d391kg p, .css-1d391kg label, 
    .css-1d391kg h1, .css-1d391kg h2, .css-1d391kg h3,
    section[data-testid="stSidebar"] p, section[data-testid="stSidebar"] label,
    section[data-testid="stSidebar"] .stMarkdown, section[data-testid="stSidebar"] div {
        color: #FFFFFF !important;
    }
    .css-1d391kg .stSelectbox label, section[data-testid="stSidebar"] .stSelectbox label {
        color: #FFFFFF !important;
    }
    
    /* Slider current value indicator */
    .current-value-marker {
        background: #0366D6;
        color: white;
        padding: 2px 8px;
        border-radius: 4px;
        font-size: 11px;
        font-weight: 600;
        font-family: monospace;
        display: inline-block;
        margin-bottom: 5px;
    }
</style>
""", unsafe_allow_html=True)

# Patient data
PATIENTS = [
    {"id": "ICU-001", "age": 58, "sex": "M", "weight": 78, "diagnosis": "ARDS", "rr": 18, "vt": 450, "peep": 8, "p_plat": 25, "mort_7d": 0.15, "mort_48h": 0.09},
    {"id": "ICU-002", "age": 67, "sex": "F", "weight": 65, "diagnosis": "Sepsis", "rr": 24, "vt": 380, "peep": 12, "p_plat": 28, "mort_7d": 0.28, "mort_48h": 0.17},
    {"id": "ICU-003", "age": 45, "sex": "M", "weight": 82, "diagnosis": "Trauma", "rr": 12, "vt": 550, "peep": 5, "p_plat": 20, "mort_7d": 0.08, "mort_48h": 0.05},
    {"id": "ICU-004", "age": 72, "sex": "F", "weight": 60, "diagnosis": "Pneumonia", "rr": 32, "vt": 320, "peep": 15, "p_plat": 32, "mort_7d": 0.45, "mort_48h": 0.27},
    {"id": "ICU-005", "age": 55, "sex": "M", "weight": 90, "diagnosis": "ARDS", "rr": 16, "vt": 500, "peep": 10, "p_plat": 22, "mort_7d": 0.12, "mort_48h": 0.07},
    {"id": "ICU-006", "age": 63, "sex": "F", "weight": 55, "diagnosis": "Sepsis", "rr": 28, "vt": 350, "peep": 14, "p_plat": 30, "mort_7d": 0.35, "mort_48h": 0.21},
    {"id": "ICU-007", "age": 49, "sex": "M", "weight": 75, "diagnosis": "Trauma", "rr": 20, "vt": 480, "peep": 8, "p_plat": 26, "mort_7d": 0.18, "mort_48h": 0.11},
    {"id": "ICU-008", "age": 38, "sex": "F", "weight": 68, "diagnosis": "Pneumonia", "rr": 14, "vt": 600, "peep": 6, "p_plat": 18, "mort_7d": 0.05, "mort_48h": 0.03},
    {"id": "ICU-009", "age": 71, "sex": "M", "weight": 70, "diagnosis": "ARDS", "rr": 26, "vt": 400, "peep": 12, "p_plat": 29, "mort_7d": 0.32, "mort_48h": 0.19},
    {"id": "ICU-010", "age": 52, "sex": "F", "weight": 62, "diagnosis": "Sepsis", "rr": 22, "vt": 420, "peep": 9, "p_plat": 24, "mort_7d": 0.22, "mort_48h": 0.13},
]

def calculate_mp(rr, vt, p_plat, peep):
    """Gattinoni formula"""
    return 0.098 * rr * (vt/1000) * (p_plat - peep/2)

def predict_outcomes(base_mort_7d, base_mort_48h, current_mp, new_mp):
    """Simple prediction model"""
    ratio = new_mp / current_mp if current_mp > 0 else 1
    
    if ratio < 0.85:  # Good reduction
        mort_7d = base_mort_7d * 0.85
        mort_48h = base_mort_48h * 0.80
        oxy = 0.75
    elif ratio < 1.0:  # Small reduction
        mort_7d = base_mort_7d * 0.95
        mort_48h = base_mort_48h * 0.92
        oxy = 0.70
    else:  # Increase
        mort_7d = base_mort_7d * min(1.3, (1 + (ratio - 1) * 1.5))
        mort_48h = base_mort_48h * min(1.4, (1 + (ratio - 1) * 2))
        oxy = max(0.40, 0.65 - (ratio - 1) * 0.5)
    
    return (max(0.01, min(0.99, mort_7d)), 
            max(0.01, min(0.99, mort_48h)), 
            oxy)

# Main App
st.markdown("<h1>IMPROVE Framework</h1>", unsafe_allow_html=True)
st.markdown("<p style='color: #000000;'>Mechanical Power Optimization for Ventilator Settings</p>", unsafe_allow_html=True)

# Sidebar - Patient Selection (with white text on dark background)
selected_id = st.sidebar.selectbox("Select Patient", [p["id"] for p in PATIENTS])
patient = next(p for p in PATIENTS if p["id"] == selected_id)

# Calculate baseline MP
current_mp = calculate_mp(patient["rr"], patient["vt"], patient["p_plat"], patient["peep"])

# Sidebar - Patient Details
st.sidebar.markdown("---")
st.sidebar.subheader("Demographics")
st.sidebar.write(f"**Age:** {patient['age']} years")
st.sidebar.write(f"**Sex:** {patient['sex']}")
st.sidebar.write(f"**Weight:** {patient['weight']} kg")
st.sidebar.write(f"**Diagnosis:** {patient['diagnosis']}")

st.sidebar.markdown("---")
st.sidebar.subheader("Current Ventilator Settings")
st.sidebar.write(f"**Respiratory Rate:** {patient['rr']} b/min")
st.sidebar.write(f"**Tidal Volume:** {patient['vt']} mL")
st.sidebar.write(f"**PEEP:** {patient['peep']} cmH₂O")
st.sidebar.write(f"**Plateau Pressure:** {patient['p_plat']} cmH₂O")
st.sidebar.write(f"**Mechanical Power:** {current_mp:.1f} J/min")
st.sidebar.write(f"**Mortality (7d):** {patient['mort_7d']:.0%}")

# Main content
col1, col2 = st.columns([1, 2])

with col1:
    st.markdown("<h2 style='color: #000000;'>Adjust Parameters</h2>", unsafe_allow_html=True)
    st.markdown(f"<p style='color: #000000;'>Baseline MP: <b>{current_mp:.1f}</b> J/min</p>", unsafe_allow_html=True)
    
    # RR Slider with current value marker
    st.markdown(f"""
    <div style='margin-bottom: -10px;'>
        <span style='color: #666; font-size: 12px;'>Current: </span>
        <span class='current-value-marker'>{patient['rr']} b/min</span>
    </div>
    """, unsafe_allow_html=True)
    new_rr = st.slider("Respiratory Rate", min_value=8, max_value=40, value=patient["rr"], step=1, label_visibility="collapsed")
    
    # VT Slider with current value marker
    st.markdown(f"""
    <div style='margin-bottom: -10px; margin-top: 15px;'>
        <span style='color: #666; font-size: 12px;'>Current: </span>
        <span class='current-value-marker'>{patient['vt']} mL</span>
    </div>
    """, unsafe_allow_html=True)
    new_vt = st.slider("Tidal Volume", min_value=250, max_value=700, value=patient["vt"], step=10, label_visibility="collapsed")
    
    # PEEP Slider with current value marker
    st.markdown(f"""
    <div style='margin-bottom: -10px; margin-top: 15px;'>
        <span style='color: #666; font-size: 12px;'>Current: </span>
        <span class='current-value-marker'>{patient['peep']} cmH₂O</span>
    </div>
    """, unsafe_allow_html=True)
    new_peep = st.slider("PEEP", min_value=0, max_value=20, value=patient["peep"], step=1, label_visibility="collapsed")

with col2:
    # Calculate new values
    new_mp = calculate_mp(new_rr, new_vt, patient["p_plat"], new_peep)
    mp_change = new_mp - current_mp
    mp_reduction = ((current_mp - new_mp) / current_mp * 100)
    
    # Predict outcomes
    mort_7d, mort_48h, oxy = predict_outcomes(
        patient["mort_7d"], patient["mort_48h"], current_mp, new_mp
    )
    
    st.markdown("<h2 style='color: #000000;'>Predicted Outcomes</h2>", unsafe_allow_html=True)
    
    # Metrics in boxes
    m1, m2, m3, m4 = st.columns(4)
    
    with m1:
        color = "#28A745" if mp_reduction > 5 else "#FFC107" if mp_reduction > 0 else "#6C757D"
        st.markdown(f"""
        <div class="metric-box">
            <div class="metric-label">Mechanical Power</div>
            <div class="metric-value" style="color: {color};">{new_mp:.1f}</div>
            <div style="font-family: monospace; font-size: 12px; color: #666;">
                Δ {mp_change:+.1f} ({mp_reduction:+.1f}%)
            </div>
        </div>
        """, unsafe_allow_html=True)
    
    with m2:
        delta = mort_7d - patient["mort_7d"]
        color = "#DC3545" if delta > 0.01 else "#28A745"
        st.markdown(f"""
        <div class="metric-box">
            <div class="metric-label">Mortality (7 days)</div>
            <div class="metric-value" style="color: {color};">{mort_7d:.1%}</div>
            <div style="font-family: monospace; font-size: 12px; color: #666;">
                Δ {delta:+.3f}
            </div>
        </div>
        """, unsafe_allow_html=True)
    
    with m3:
        delta = mort_48h - patient["mort_48h"]
        color = "#DC3545" if delta > 0.01 else "#28A745"
        st.markdown(f"""
        <div class="metric-box">
            <div class="metric-label">Mortality (48 hours)</div>
            <div class="metric-value" style="color: {color};">{mort_48h:.1%}</div>
            <div style="font-family: monospace; font-size: 12px; color: #666;">
                Δ {delta:+.3f}
            </div>
        </div>
        """, unsafe_allow_html=True)
    
    with m4:
        st.markdown(f"""
        <div class="metric-box">
            <div class="metric-label">Oxygenation</div>
            <div class="metric-value">{oxy:.0%}</div>
            <div style="font-family: monospace; font-size: 12px; color: #666;">
                Probability
            </div>
        </div>
        """, unsafe_allow_html=True)
    
    # Recommendation
    st.markdown("<h2 style='color: #000000;'>Recommendation</h2>", unsafe_allow_html=True)
    
    # Check if changed
    if new_rr == patient["rr"] and new_vt == patient["vt"] and new_peep == patient["peep"]:
        st.markdown("""
        <div class="recommendation-box info">
            <b style='color: #0c5460;'>CURRENT SETTINGS</b><br><br>
            Adjust sliders to see predicted effects of ventilator parameter changes.<br>
            Target: Reduce Mechanical Power while keeping mortality risk increase ≤ 1%.
        </div>
        """, unsafe_allow_html=True)
    
    elif mp_reduction > 5 and (mort_7d - patient["mort_7d"]) <= 0.01:
        st.markdown(f"""
        <div class="recommendation-box success">
            <b style='color: #155724;'>REDUCE MECHANICAL POWER</b><br><br>
            <b>Current:</b> RR={patient['rr']}, VT={patient['vt']}, PEEP={patient['peep']}<br>
            <b>Recommended:</b> RR={new_rr}, VT={new_vt}, PEEP={new_peep}<br><br>
            Expected outcome: MP reduced by {mp_reduction:.1f}%<br>
            Mortality risk: {patient['mort_7d']:.1%} → {mort_7d:.1%}<br>
            48h mortality: {patient['mort_48h']:.1%} → {mort_48h:.1%}
        </div>
        """, unsafe_allow_html=True)
    
    elif (mort_7d - patient["mort_7d"]) > 0.01:
        st.markdown("""
        <div class="recommendation-box warning">
            <b style='color: #856404;'>MAINTAIN CURRENT SETTINGS</b><br><br>
            Proposed changes would increase mortality risk beyond safe threshold (+1% limit).<br>
            Current settings are safer for this patient.
        </div>
        """, unsafe_allow_html=True)
    
    else:
        st.markdown(f"""
        <div class="recommendation-box info">
            <b style='color: #0c5460;'>MARGINAL BENEFIT</b><br><br>
            MP reduction of {mp_reduction:.1f}% is achievable but minimal.<br>
            Consider maintaining current settings unless other factors apply.
        </div>
        """, unsafe_allow_html=True)
    
    # Simple chart
    fig = go.Figure()
    categories = ['MP (J/min)', 'Mortality 7d (%)', 'Mortality 48h (%)']
    current_vals = [current_mp, patient["mort_7d"]*100, patient["mort_48h"]*100]
    new_vals = [new_mp, mort_7d*100, mort_48h*100]
    
    fig.add_trace(go.Bar(name='Current', x=categories, y=current_vals, marker_color='#6C757D'))
    fig.add_trace(go.Bar(name='Proposed', x=categories, y=new_vals, marker_color='#0366D6'))
    
    fig.update_layout(
        barmode='group',
        height=300,
        paper_bgcolor='white',
        plot_bgcolor='white',
        font=dict(color='black'),
        legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
        margin=dict(l=40, r=40, t=60, b=40)
    )
    
    st.plotly_chart(fig, use_container_width=True)

st.markdown("---")
st.caption("IMPROVE Framework | CMC Vellore & Ashoka University | ESICM Datathon 2025")