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(""" """, 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("

IMPROVE Framework

", unsafe_allow_html=True) st.markdown("

Mechanical Power Optimization for Ventilator Settings

", 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("

Adjust Parameters

", unsafe_allow_html=True) st.markdown(f"

Baseline MP: {current_mp:.1f} J/min

", unsafe_allow_html=True) # RR Slider with current value marker st.markdown(f"""
Current: {patient['rr']} b/min
""", 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"""
Current: {patient['vt']} mL
""", 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"""
Current: {patient['peep']} cmH₂O
""", 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("

Predicted Outcomes

", 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"""
Mechanical Power
{new_mp:.1f}
Δ {mp_change:+.1f} ({mp_reduction:+.1f}%)
""", unsafe_allow_html=True) with m2: delta = mort_7d - patient["mort_7d"] color = "#DC3545" if delta > 0.01 else "#28A745" st.markdown(f"""
Mortality (7 days)
{mort_7d:.1%}
Δ {delta:+.3f}
""", unsafe_allow_html=True) with m3: delta = mort_48h - patient["mort_48h"] color = "#DC3545" if delta > 0.01 else "#28A745" st.markdown(f"""
Mortality (48 hours)
{mort_48h:.1%}
Δ {delta:+.3f}
""", unsafe_allow_html=True) with m4: st.markdown(f"""
Oxygenation
{oxy:.0%}
Probability
""", unsafe_allow_html=True) # Recommendation st.markdown("

Recommendation

", unsafe_allow_html=True) # Check if changed if new_rr == patient["rr"] and new_vt == patient["vt"] and new_peep == patient["peep"]: st.markdown("""
CURRENT SETTINGS

Adjust sliders to see predicted effects of ventilator parameter changes.
Target: Reduce Mechanical Power while keeping mortality risk increase ≤ 1%.
""", unsafe_allow_html=True) elif mp_reduction > 5 and (mort_7d - patient["mort_7d"]) <= 0.01: st.markdown(f"""
REDUCE MECHANICAL POWER

Current: RR={patient['rr']}, VT={patient['vt']}, PEEP={patient['peep']}
Recommended: RR={new_rr}, VT={new_vt}, PEEP={new_peep}

Expected outcome: MP reduced by {mp_reduction:.1f}%
Mortality risk: {patient['mort_7d']:.1%} → {mort_7d:.1%}
48h mortality: {patient['mort_48h']:.1%} → {mort_48h:.1%}
""", unsafe_allow_html=True) elif (mort_7d - patient["mort_7d"]) > 0.01: st.markdown("""
MAINTAIN CURRENT SETTINGS

Proposed changes would increase mortality risk beyond safe threshold (+1% limit).
Current settings are safer for this patient.
""", unsafe_allow_html=True) else: st.markdown(f"""
MARGINAL BENEFIT

MP reduction of {mp_reduction:.1f}% is achievable but minimal.
Consider maintaining current settings unless other factors apply.
""", 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")