Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,18 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
import numpy as np
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
-
import
|
| 7 |
-
from ipywidgets import FloatSlider, VBox, HBox, Layout, HTML
|
| 8 |
-
from IPython.display import display
|
| 9 |
|
| 10 |
# ββ style ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
-
BG
|
| 12 |
-
PANEL
|
| 13 |
-
GRID
|
| 14 |
-
ACCENT
|
| 15 |
-
C_INC
|
| 16 |
-
C_ZS
|
| 17 |
-
C_RR
|
| 18 |
-
C_TEXT
|
| 19 |
-
C_DIM
|
| 20 |
-
C_BORDER= '#cbd5e1'
|
| 21 |
|
| 22 |
plt.rcParams.update({
|
| 23 |
'figure.facecolor': BG, 'axes.facecolor': PANEL,
|
|
@@ -32,153 +27,133 @@ EP_RANGE = np.linspace(0, 15, 600)
|
|
| 32 |
def calc_paf(episodes, incidence, zscore, rr):
|
| 33 |
return 1 - 1 / np.exp(episodes * incidence * zscore * rr)
|
| 34 |
|
| 35 |
-
def make_slider(desc, mn, mx, val, step, color):
|
| 36 |
-
return FloatSlider(
|
| 37 |
-
value=val, min=mn, max=mx, step=step,
|
| 38 |
-
description=desc, continuous_update=True,
|
| 39 |
-
layout=Layout(width='520px'),
|
| 40 |
-
style={'description_width': '160px', 'handle_color': color}
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
# ββ widgets ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 44 |
-
s_ep = make_slider('Episodes', 0, 15, 4.2, 0.1, ACCENT)
|
| 45 |
-
s_inc = make_slider('Pathogen Incidence', 0.01, 1, 0.3, 0.01, C_INC)
|
| 46 |
-
s_zs = make_slider('Ξz per Episode', 0.01, 2, 0.2, 0.01, C_ZS)
|
| 47 |
-
s_rr = make_slider('Relative Risk (RR)', 1.01, 5, 1.5, 0.05, C_RR)
|
| 48 |
-
|
| 49 |
-
btn_reset = widgets.Button(
|
| 50 |
-
description='Reset Defaults',
|
| 51 |
-
layout=Layout(width='160px', height='34px'),
|
| 52 |
-
style={'button_color': '#e2e8f0'}
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
info_box = HTML()
|
| 56 |
-
out = widgets.Output()
|
| 57 |
-
|
| 58 |
# ββ plot function ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
def draw(ep, inc, zs, rr):
|
| 60 |
paf_curve = calc_paf(EP_RANGE, inc, zs, rr) * 100
|
| 61 |
cur_paf = calc_paf(ep, inc, zs, rr)
|
| 62 |
exponent = ep * inc * zs * rr
|
| 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 |
-
draw
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
+
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# ββ style ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
+
BG = '#f8fafc'
|
| 7 |
+
PANEL = '#ffffff'
|
| 8 |
+
GRID = '#e2e8f0'
|
| 9 |
+
ACCENT = '#2563eb'
|
| 10 |
+
C_INC = '#d97706'
|
| 11 |
+
C_ZS = '#7c3aed'
|
| 12 |
+
C_RR = '#059669'
|
| 13 |
+
C_TEXT = '#1e293b'
|
| 14 |
+
C_DIM = '#64748b'
|
| 15 |
+
C_BORDER = '#cbd5e1'
|
| 16 |
|
| 17 |
plt.rcParams.update({
|
| 18 |
'figure.facecolor': BG, 'axes.facecolor': PANEL,
|
|
|
|
| 27 |
def calc_paf(episodes, incidence, zscore, rr):
|
| 28 |
return 1 - 1 / np.exp(episodes * incidence * zscore * rr)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# ββ plot function ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 31 |
def draw(ep, inc, zs, rr):
|
| 32 |
paf_curve = calc_paf(EP_RANGE, inc, zs, rr) * 100
|
| 33 |
cur_paf = calc_paf(ep, inc, zs, rr)
|
| 34 |
exponent = ep * inc * zs * rr
|
| 35 |
|
| 36 |
+
fig, axes = plt.subplots(1, 2, figsize=(15, 6),
|
| 37 |
+
gridspec_kw={'width_ratios': [3, 1]})
|
| 38 |
+
fig.patch.set_facecolor(BG)
|
| 39 |
+
|
| 40 |
+
# ββ left: curve βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 41 |
+
ax = axes[0]
|
| 42 |
+
ax.set_facecolor(PANEL)
|
| 43 |
+
for spine in ax.spines.values():
|
| 44 |
+
spine.set_color(C_BORDER)
|
| 45 |
+
|
| 46 |
+
# shadow + main curve
|
| 47 |
+
ax.plot(EP_RANGE, paf_curve, color=ACCENT, linewidth=5, alpha=0.15)
|
| 48 |
+
ax.plot(EP_RANGE, paf_curve, color=ACCENT, linewidth=2.2)
|
| 49 |
+
|
| 50 |
+
# crosshairs
|
| 51 |
+
ax.axvline(ep, color=ACCENT, linestyle='--', linewidth=1.2, alpha=0.6)
|
| 52 |
+
ax.axhline(cur_paf*100, color=ACCENT, linestyle='--', linewidth=1.2, alpha=0.3)
|
| 53 |
+
|
| 54 |
+
# dot at current point
|
| 55 |
+
ax.scatter([ep], [cur_paf*100], color=ACCENT, s=80, zorder=5,
|
| 56 |
+
edgecolors='white', linewidths=1.5)
|
| 57 |
+
|
| 58 |
+
# shaded area under curve up to current episode
|
| 59 |
+
mask = EP_RANGE <= ep
|
| 60 |
+
ax.fill_between(EP_RANGE[mask], paf_curve[mask], color=ACCENT, alpha=0.08)
|
| 61 |
+
|
| 62 |
+
ax.set_xlim(0, 15)
|
| 63 |
+
ax.set_ylim(0, 102)
|
| 64 |
+
ax.set_xlabel('Episodes', fontsize=11, labelpad=8)
|
| 65 |
+
ax.set_ylabel('PAF (%)', fontsize=11, labelpad=8)
|
| 66 |
+
ax.set_title('Population Attributable Fraction',
|
| 67 |
+
color=C_TEXT, fontsize=13, pad=12, fontweight='normal')
|
| 68 |
+
ax.grid(True, linewidth=0.8)
|
| 69 |
+
|
| 70 |
+
# annotation
|
| 71 |
+
ax.annotate(
|
| 72 |
+
f' {cur_paf*100:.1f}%',
|
| 73 |
+
xy=(ep, cur_paf*100),
|
| 74 |
+
xytext=(min(ep + 1, 13), cur_paf*100 + 4),
|
| 75 |
+
color=ACCENT, fontsize=15, fontweight='bold',
|
| 76 |
+
arrowprops=dict(arrowstyle='->', color=ACCENT, lw=1.2)
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# formula watermark
|
| 80 |
+
ax.text(0.98, 0.05,
|
| 81 |
+
'PAF = 1 β exp(βep Γ inc Γ Ξz Γ RR)',
|
| 82 |
+
transform=ax.transAxes, fontsize=11,
|
| 83 |
+
color=C_DIM, ha='right', va='bottom')
|
| 84 |
+
|
| 85 |
+
# ββ right: breakdown panel ββββββββββββββββββββββββββββββββββββββββββββ
|
| 86 |
+
ax2 = axes[1]
|
| 87 |
+
ax2.set_facecolor('#f1f5f9')
|
| 88 |
+
for spine in ax2.spines.values():
|
| 89 |
+
spine.set_color(C_BORDER)
|
| 90 |
+
ax2.set_xticks([])
|
| 91 |
+
ax2.set_yticks([])
|
| 92 |
+
|
| 93 |
+
rows = [
|
| 94 |
+
('episodes', f'{ep:.2f}', ACCENT),
|
| 95 |
+
('incidence', f'{inc:.3f}', C_INC),
|
| 96 |
+
('Ξz / ep.', f'{zs:.3f}', C_ZS),
|
| 97 |
+
('RR', f'{rr:.3f}', C_RR),
|
| 98 |
+
('β' * 10, 'β' * 6, C_BORDER),
|
| 99 |
+
('exponent', f'{exponent:.4f}', '#475569'),
|
| 100 |
+
('exp(βx)', f'{1/np.exp(exponent):.4f}', '#475569'),
|
| 101 |
+
('β' * 10, 'β' * 6, C_BORDER),
|
| 102 |
+
('PAF', f'{cur_paf*100:.2f}%', ACCENT),
|
| 103 |
+
]
|
| 104 |
+
|
| 105 |
+
ax2.text(0.5, 0.97, 'BREAKDOWN', transform=ax2.transAxes,
|
| 106 |
+
ha='center', fontsize=13, color=ACCENT, fontweight='bold')
|
| 107 |
+
|
| 108 |
+
y_start = 0.88
|
| 109 |
+
for i, (k, v, c) in enumerate(rows):
|
| 110 |
+
y = y_start - i * 0.095
|
| 111 |
+
ax2.text(0.08, y, k, transform=ax2.transAxes,
|
| 112 |
+
fontsize=11, color=C_DIM, va='center')
|
| 113 |
+
ax2.text(0.92, y, v, transform=ax2.transAxes,
|
| 114 |
+
fontsize=11, color=c, va='center', ha='right', fontweight='bold')
|
| 115 |
+
|
| 116 |
+
ax2.set_title('Parameters', color=C_TEXT, fontsize=13, pad=8)
|
| 117 |
+
|
| 118 |
+
plt.tight_layout(pad=1.5)
|
| 119 |
+
return fig
|
| 120 |
+
|
| 121 |
+
# ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 122 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="PAF Visualizer") as demo:
|
| 123 |
+
|
| 124 |
+
gr.HTML("""
|
| 125 |
+
<div style="background:#eff6ff;border:1px solid #bfdbfe;border-radius:10px;
|
| 126 |
+
padding:12px 20px;margin-bottom:12px;font-family:monospace;">
|
| 127 |
+
<span style="color:#1e293b;font-size:20px;font-weight:600;">
|
| 128 |
+
Population Attributable Fraction Visualizer
|
| 129 |
+
</span><br>
|
| 130 |
+
<span style="color:#64748b;font-size:13px;">
|
| 131 |
+
PAF = 1 β exp(βepisodes Γ incidence Γ Ξz Γ RR)
|
| 132 |
+
</span>
|
| 133 |
+
</div>""")
|
| 134 |
+
|
| 135 |
+
with gr.Row():
|
| 136 |
+
with gr.Column():
|
| 137 |
+
ep = gr.Slider(0, 15, value=4.2, step=0.1, label="Episodes")
|
| 138 |
+
inc = gr.Slider(0.01, 1, value=0.3, step=0.01, label="Pathogen Incidence")
|
| 139 |
+
zs = gr.Slider(0.01, 2, value=0.2, step=0.01, label="Ξz per Episode")
|
| 140 |
+
rr = gr.Slider(1.01, 5, value=1.5, step=0.05, label="Relative Risk (RR)")
|
| 141 |
+
|
| 142 |
+
reset_btn = gr.Button("Reset Defaults", variant="secondary")
|
| 143 |
+
|
| 144 |
+
plot = gr.Plot(label="")
|
| 145 |
+
|
| 146 |
+
# live update on any slider change
|
| 147 |
+
for slider in [ep, inc, zs, rr]:
|
| 148 |
+
slider.change(fn=draw, inputs=[ep, inc, zs, rr], outputs=plot)
|
| 149 |
+
|
| 150 |
+
# reset button
|
| 151 |
+
def reset():
|
| 152 |
+
return 5.0, 0.3, 1.2, 2.5
|
| 153 |
+
|
| 154 |
+
reset_btn.click(fn=reset, outputs=[ep, inc, zs, rr])
|
| 155 |
+
|
| 156 |
+
# draw on load
|
| 157 |
+
demo.load(fn=draw, inputs=[ep, inc, zs, rr], outputs=plot)
|
| 158 |
+
|
| 159 |
+
demo.launch()
|