Spaces:
Running on Zero
Running on Zero
Replace the explainer with a pipeline flow diagram
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ import gradio as gr
|
|
| 12 |
import matplotlib
|
| 13 |
matplotlib.use("Agg")
|
| 14 |
import matplotlib.pyplot as plt
|
|
|
|
| 15 |
import numpy as np
|
| 16 |
import pandas as pd
|
| 17 |
|
|
@@ -87,83 +88,100 @@ def _synthetic_cell(condensed: float, size: int = 64, seed: int = 0) -> np.ndarr
|
|
| 87 |
return np.clip(img + 0.02 * rng.random((size, size)), 0, 1)
|
| 88 |
|
| 89 |
|
| 90 |
-
def
|
| 91 |
-
"""
|
| 92 |
x = pipeline.intensity_ladder(pipeline.NUM_IMAGES).numpy()
|
| 93 |
lx = (np.log10(x) - np.log10(x[0])) / (np.log10(x[-1]) - np.log10(x[0]))
|
| 94 |
-
|
| 95 |
-
# a schematic reentrant curve: condenses, plateaus, dissolves again
|
| 96 |
p = np.clip(1.55 * np.exp(-((lx - 0.52) ** 2) / (2 * 0.17 ** 2)) - 0.16, 0, 1)
|
| 97 |
-
|
| 98 |
-
calls = (rng.random(p.size) < p).astype(int)
|
| 99 |
curve = metrics.moving_average(calls, pipeline.NUM_IMAGES // pipeline.WINDOW_DIVISOR)
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
fig = plt.figure(figsize=(9.6, 5.6), facecolor=SURFACE)
|
| 103 |
-
grid = fig.add_gridspec(2, 1, height_ratios=[1, 2.5], hspace=0.32)
|
| 104 |
|
| 105 |
-
# --- row 1: the ladder of generated images -----------------------------
|
| 106 |
-
ladder = grid[0].subgridspec(1, 6, wspace=0.12)
|
| 107 |
-
for k, frac in enumerate(np.linspace(0.06, 0.97, 6)):
|
| 108 |
-
ax = fig.add_subplot(ladder[k])
|
| 109 |
-
idx = int(frac * (len(x) - 1))
|
| 110 |
-
ax.imshow(_synthetic_cell(float(curve[idx]) ** 1.8, seed=k), cmap="magma", vmin=0, vmax=1)
|
| 111 |
-
ax.set_xticks([]); ax.set_yticks([])
|
| 112 |
-
for sp in ax.spines.values():
|
| 113 |
-
sp.set_color(MUTED); sp.set_linewidth(0.6)
|
| 114 |
-
ax.set_title("diffuse" if curve[idx] < 0.5 else "condensed",
|
| 115 |
-
fontsize=8.5, color=MUTED, pad=3)
|
| 116 |
-
if k == 0:
|
| 117 |
-
ax.set_ylabel("1. generate\n2. classify", fontsize=9, color=INK,
|
| 118 |
-
rotation=0, ha="right", va="center", labelpad=12)
|
| 119 |
-
|
| 120 |
-
# --- row 2: the curve those calls become, and its two areas ------------
|
| 121 |
-
ax = fig.add_subplot(grid[1])
|
| 122 |
-
ax.set_facecolor(SURFACE)
|
| 123 |
-
ax.scatter(x, calls, s=7, alpha=0.16, color=MUTED, linewidths=0, zorder=1)
|
| 124 |
-
ax.fill_between(x, 0, curve, color=CURVE, alpha=0.18, linewidth=0, zorder=2)
|
| 125 |
-
ax.fill_between(x, curve, flat, where=flat > curve, color=GAP, alpha=0.30,
|
| 126 |
-
linewidth=0, zorder=3)
|
| 127 |
-
ax.plot(x, flat, color=GAP, linewidth=1.2, linestyle="--", alpha=0.85, zorder=4)
|
| 128 |
-
ax.plot(x, curve, color=CURVE, linewidth=2.0, zorder=5)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
ax.text(x[auc_i], 0.45, "AUC\nrange spent condensed", fontsize=10, color=CURVE,
|
| 135 |
-
ha="center", va="center", fontweight="bold", zorder=8)
|
| 136 |
-
|
| 137 |
-
# Sit the AAC label where the gap is genuinely open -- past the falling edge and
|
| 138 |
-
# clear of the right spine -- rather than at a fixed fraction of the gap.
|
| 139 |
-
clear = np.flatnonzero((flat - curve > 0.45) & (curve < 0.35))
|
| 140 |
-
if clear.size:
|
| 141 |
-
g = clear[max(0, len(clear) // 2 - len(clear) // 5)]
|
| 142 |
-
ax.text(x[g], 0.58, "AAC\ncondensation lost to\nreentrant dissolution",
|
| 143 |
-
fontsize=10, color=GAP, ha="center", va="center",
|
| 144 |
-
fontweight="bold", zorder=8)
|
| 145 |
-
|
| 146 |
-
ax.annotate("raw condensed / diffuse calls", xy=(x[2], 0), xytext=(4, 16),
|
| 147 |
-
textcoords="offset points", fontsize=8.5, color=MUTED,
|
| 148 |
-
ha="left", zorder=8)
|
| 149 |
-
rise = int(np.argmax(curve > 0.5))
|
| 150 |
-
ax.annotate("3. moving average", xy=(x[rise], curve[rise]), xytext=(-14, 10),
|
| 151 |
-
textcoords="offset points", fontsize=9, color=CURVE, ha="right",
|
| 152 |
-
fontweight="bold", zorder=8,
|
| 153 |
-
arrowprops=dict(arrowstyle="->", lw=1, color=CURVE))
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
fig.suptitle("Schematic — not model output", fontsize=9, color=MUTED, y=0.02)
|
| 167 |
return fig
|
| 168 |
|
| 169 |
|
|
|
|
| 12 |
import matplotlib
|
| 13 |
matplotlib.use("Agg")
|
| 14 |
import matplotlib.pyplot as plt
|
| 15 |
+
from matplotlib.patches import FancyBboxPatch
|
| 16 |
import numpy as np
|
| 17 |
import pandas as pd
|
| 18 |
|
|
|
|
| 88 |
return np.clip(img + 0.02 * rng.random((size, size)), 0, 1)
|
| 89 |
|
| 90 |
|
| 91 |
+
def _schematic_curve():
|
| 92 |
+
"""A reentrant titration curve used only for the diagram's thumbnails."""
|
| 93 |
x = pipeline.intensity_ladder(pipeline.NUM_IMAGES).numpy()
|
| 94 |
lx = (np.log10(x) - np.log10(x[0])) / (np.log10(x[-1]) - np.log10(x[0]))
|
|
|
|
|
|
|
| 95 |
p = np.clip(1.55 * np.exp(-((lx - 0.52) ** 2) / (2 * 0.17 ** 2)) - 0.16, 0, 1)
|
| 96 |
+
calls = (np.random.default_rng(0).random(p.size) < p).astype(int)
|
|
|
|
| 97 |
curve = metrics.moving_average(calls, pipeline.NUM_IMAGES // pipeline.WINDOW_DIVISOR)
|
| 98 |
+
return x, curve
|
|
|
|
|
|
|
|
|
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
def plot_explainer():
|
| 102 |
+
"""The pipeline as a flow: inputs -> CELL-FM -> image -> ViT -> curve -> areas."""
|
| 103 |
+
x, curve = _schematic_curve()
|
| 104 |
+
flat = metrics.no_reentrant_curve(curve)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
fig = plt.figure(figsize=(11.2, 2.35), facecolor=SURFACE)
|
| 107 |
+
stage = fig.add_axes([0, 0, 1, 1]); stage.set_axis_off()
|
| 108 |
+
stage.set_xlim(0, 1); stage.set_ylim(0, 1)
|
| 109 |
+
|
| 110 |
+
# node centres and the panel each one draws into
|
| 111 |
+
xs = [0.098, 0.368, 0.632, 0.902]
|
| 112 |
+
box_y, box_h, box_w = 0.06, 0.70, 0.155
|
| 113 |
+
|
| 114 |
+
def node(cx, title):
|
| 115 |
+
stage.add_patch(FancyBboxPatch(
|
| 116 |
+
(cx - box_w / 2, box_y), box_w, box_h,
|
| 117 |
+
boxstyle="round,pad=0.012,rounding_size=0.02",
|
| 118 |
+
linewidth=1.1, edgecolor=MUTED, facecolor="white", zorder=2))
|
| 119 |
+
stage.text(cx, box_y + box_h + 0.045, title, ha="center", va="bottom",
|
| 120 |
+
fontsize=11, color=INK, fontweight="bold", zorder=4)
|
| 121 |
+
|
| 122 |
+
def arrow(x0, x1, label):
|
| 123 |
+
stage.annotate("", xy=(x1, box_y + box_h / 2), xytext=(x0, box_y + box_h / 2),
|
| 124 |
+
arrowprops=dict(arrowstyle="-|>", lw=1.6, color=INK,
|
| 125 |
+
shrinkA=0, shrinkB=0), zorder=3)
|
| 126 |
+
if label:
|
| 127 |
+
stage.text((x0 + x1) / 2, box_y + box_h / 2 + 0.05, label, ha="center",
|
| 128 |
+
va="bottom", fontsize=8.5, color=CURVE, fontweight="bold",
|
| 129 |
+
linespacing=1.35, zorder=4)
|
| 130 |
+
|
| 131 |
+
for cx, title in zip(xs, ["IDP sequence\n+ expression level", "Image", "Curve", "AUC, AAC"]):
|
| 132 |
+
node(cx, title)
|
| 133 |
+
arrow(xs[0] + box_w / 2, xs[1] - box_w / 2, "CELL-FM")
|
| 134 |
+
arrow(xs[1] + box_w / 2, xs[2] - box_w / 2, "ViT classification\nmodel")
|
| 135 |
+
arrow(xs[2] + box_w / 2, xs[3] - box_w / 2, "integrate")
|
| 136 |
+
|
| 137 |
+
def panel(cx, pad_x=0.022, pad_y=0.05):
|
| 138 |
+
return fig.add_axes([cx - box_w / 2 + pad_x, box_y + pad_y,
|
| 139 |
+
box_w - 2 * pad_x, box_h - 2 * pad_y])
|
| 140 |
+
|
| 141 |
+
# 1. the two inputs
|
| 142 |
+
a = panel(xs[0]); a.set_axis_off(); a.set_xlim(0, 1); a.set_ylim(0, 1)
|
| 143 |
+
a.text(0.5, 0.78, "KSFGTPFGGG…", ha="center", va="center", fontsize=8.5,
|
| 144 |
+
family="monospace", color=INK)
|
| 145 |
+
a.text(0.5, 0.58, "66 aa", ha="center", va="center", fontsize=7.5, color=MUTED)
|
| 146 |
+
grad = np.linspace(0, 1, 128)[None, :]
|
| 147 |
+
a.imshow(grad, cmap="magma", extent=(0.06, 0.94, 0.16, 0.34), aspect="auto")
|
| 148 |
+
a.text(0.5, 0.05, "low → high expression", ha="center", va="center",
|
| 149 |
+
fontsize=7.5, color=MUTED)
|
| 150 |
+
|
| 151 |
+
# 2. one generated image
|
| 152 |
+
b = panel(xs[1])
|
| 153 |
+
b.imshow(_synthetic_cell(0.85, seed=3), cmap="magma", vmin=0, vmax=1)
|
| 154 |
+
b.set_xticks([]); b.set_yticks([])
|
| 155 |
+
for sp in b.spines.values():
|
| 156 |
+
sp.set_visible(False)
|
| 157 |
+
|
| 158 |
+
# 3. the curve those images produce
|
| 159 |
+
c = panel(xs[2])
|
| 160 |
+
c.plot(x, curve, color=CURVE, linewidth=2.0)
|
| 161 |
+
c.set_xscale("log"); c.set_xlim(x.min(), x.max()); c.set_ylim(-0.05, 1.08)
|
| 162 |
+
c.set_xticks([]); c.set_yticks([]); c.minorticks_off()
|
| 163 |
+
c.tick_params(which="both", length=0)
|
| 164 |
+
c.set_facecolor("white")
|
| 165 |
+
for sp in c.spines.values():
|
| 166 |
+
sp.set_color(MUTED); sp.set_linewidth(0.6)
|
| 167 |
+
|
| 168 |
+
# 4. the same curve, split into the two areas
|
| 169 |
+
d = panel(xs[3])
|
| 170 |
+
d.fill_between(x, 0, curve, color=CURVE, alpha=0.22, linewidth=0)
|
| 171 |
+
d.fill_between(x, curve, flat, where=flat > curve, color=GAP, alpha=0.34, linewidth=0)
|
| 172 |
+
d.plot(x, curve, color=CURVE, linewidth=1.6)
|
| 173 |
+
d.plot(x, flat, color=GAP, linewidth=1.0, linestyle="--", alpha=0.85)
|
| 174 |
+
d.set_xscale("log"); d.set_xlim(x.min(), x.max()); d.set_ylim(-0.05, 1.08)
|
| 175 |
+
d.set_xticks([]); d.set_yticks([]); d.minorticks_off()
|
| 176 |
+
d.tick_params(which="both", length=0)
|
| 177 |
+
d.set_facecolor("white")
|
| 178 |
+
for sp in d.spines.values():
|
| 179 |
+
sp.set_color(MUTED); sp.set_linewidth(0.6)
|
| 180 |
+
d.text(0.34, 0.30, "AUC", transform=d.transAxes, fontsize=9, color=CURVE,
|
| 181 |
+
fontweight="bold", ha="center")
|
| 182 |
+
d.text(0.80, 0.62, "AAC", transform=d.transAxes, fontsize=9, color=GAP,
|
| 183 |
+
fontweight="bold", ha="center")
|
| 184 |
|
|
|
|
| 185 |
return fig
|
| 186 |
|
| 187 |
|