Spaces:
Running
Running
cleaned and updated
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Shape2Force (S2F) - GUI for force map prediction from bright field microscopy images.
|
| 3 |
"""
|
| 4 |
-
import csv
|
| 5 |
-
import io
|
| 6 |
import os
|
| 7 |
import sys
|
| 8 |
import traceback
|
|
@@ -12,519 +10,100 @@ cv2.utils.logging.setLogLevel(cv2.utils.logging.LOG_LEVEL_ERROR)
|
|
| 12 |
|
| 13 |
import numpy as np
|
| 14 |
import streamlit as st
|
| 15 |
-
from PIL import Image
|
| 16 |
-
import plotly.graph_objects as go
|
| 17 |
-
from plotly.subplots import make_subplots
|
| 18 |
|
| 19 |
S2F_ROOT = os.path.dirname(os.path.abspath(__file__))
|
| 20 |
if S2F_ROOT not in sys.path:
|
| 21 |
sys.path.insert(0, S2F_ROOT)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
from utils.substrate_settings import list_substrates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
try:
|
| 26 |
from streamlit_drawable_canvas import st_canvas
|
| 27 |
-
HAS_DRAWABLE_CANVAS = True
|
| 28 |
except (ImportError, AttributeError):
|
| 29 |
-
HAS_DRAWABLE_CANVAS
|
| 30 |
-
|
| 31 |
-
# Constants
|
| 32 |
-
MODEL_TYPE_LABELS = {"single_cell": "Single cell", "spheroid": "Spheroid"}
|
| 33 |
-
DRAW_TOOLS = ["polygon", "rect", "circle"]
|
| 34 |
-
TOOL_LABELS = {"polygon": "Polygon", "rect": "Rectangle", "circle": "Circle"}
|
| 35 |
-
CANVAS_SIZE = 320
|
| 36 |
-
SAMPLE_EXTENSIONS = (".tif", ".tiff", ".png", ".jpg", ".jpeg")
|
| 37 |
-
COLORMAPS = {
|
| 38 |
-
"Jet": cv2.COLORMAP_JET,
|
| 39 |
-
"Viridis": cv2.COLORMAP_VIRIDIS,
|
| 40 |
-
"Plasma": cv2.COLORMAP_PLASMA,
|
| 41 |
-
"Inferno": cv2.COLORMAP_INFERNO,
|
| 42 |
-
"Magma": cv2.COLORMAP_MAGMA,
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
def _cv_colormap_to_plotly_colorscale(colormap_name, n_samples=64):
|
| 47 |
-
"""Build a Plotly colorscale from OpenCV colormap so UI matches download/PDF exactly."""
|
| 48 |
-
cv2_cmap = COLORMAPS.get(colormap_name, cv2.COLORMAP_JET)
|
| 49 |
-
gradient = np.linspace(0, 255, n_samples, dtype=np.uint8).reshape(1, -1)
|
| 50 |
-
rgb = cv2.applyColorMap(gradient, cv2_cmap)
|
| 51 |
-
rgb = cv2.cvtColor(rgb, cv2.COLOR_BGR2RGB)
|
| 52 |
-
# Plotly colorscale: [[position 0..1, 'rgb(r,g,b)'], ...]
|
| 53 |
-
scale = []
|
| 54 |
-
for i in range(n_samples):
|
| 55 |
-
r, g, b = rgb[0, i]
|
| 56 |
-
scale.append([i / (n_samples - 1), f"rgb({r},{g},{b})"])
|
| 57 |
-
return scale
|
| 58 |
-
|
| 59 |
|
| 60 |
CITATION = (
|
| 61 |
"Lautaro Baro, Kaveh Shahhosseini, Amparo Andrés-Bordería, Claudio Angione, and Maria Angeles Juanes. "
|
| 62 |
"**\"Shape-to-force (S2F): Predicting Cell Traction Forces from LabelFree Imaging\"**, 2026."
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
""
|
| 68 |
-
annotated = heatmap_rgb.copy()
|
| 69 |
-
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 70 |
-
# Semi-transparent orange fill
|
| 71 |
-
overlay = annotated.copy()
|
| 72 |
-
cv2.fillPoly(overlay, contours, stroke_color)
|
| 73 |
-
mask_3d = np.stack([mask] * 3, axis=-1).astype(bool)
|
| 74 |
-
annotated[mask_3d] = (
|
| 75 |
-
(1 - fill_alpha) * annotated[mask_3d].astype(np.float32)
|
| 76 |
-
+ fill_alpha * overlay[mask_3d].astype(np.float32)
|
| 77 |
-
).astype(np.uint8)
|
| 78 |
-
# Orange contour
|
| 79 |
-
cv2.drawContours(annotated, contours, -1, stroke_color, stroke_width)
|
| 80 |
-
return annotated
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
def _parse_canvas_shapes_to_mask(json_data, canvas_h, canvas_w, heatmap_h, heatmap_w):
|
| 84 |
-
"""
|
| 85 |
-
Parse drawn shapes from streamlit-drawable-canvas json_data and create a binary mask
|
| 86 |
-
in heatmap coordinates. Returns (mask, num_shapes) or (None, 0) if no valid shapes.
|
| 87 |
-
"""
|
| 88 |
-
if not json_data or "objects" not in json_data or not json_data["objects"]:
|
| 89 |
-
return None, 0
|
| 90 |
-
scale_x = heatmap_w / canvas_w
|
| 91 |
-
scale_y = heatmap_h / canvas_h
|
| 92 |
-
mask = np.zeros((heatmap_h, heatmap_w), dtype=np.uint8)
|
| 93 |
-
count = 0
|
| 94 |
-
for obj in json_data["objects"]:
|
| 95 |
-
obj_type = obj.get("type", "")
|
| 96 |
-
pts = []
|
| 97 |
-
if obj_type == "rect":
|
| 98 |
-
left = obj.get("left", 0)
|
| 99 |
-
top = obj.get("top", 0)
|
| 100 |
-
w = obj.get("width", 0)
|
| 101 |
-
h = obj.get("height", 0)
|
| 102 |
-
pts = np.array([
|
| 103 |
-
[left, top], [left + w, top], [left + w, top + h], [left, top + h]
|
| 104 |
-
], dtype=np.float32)
|
| 105 |
-
elif obj_type == "circle" or obj_type == "ellipse":
|
| 106 |
-
left = obj.get("left", 0)
|
| 107 |
-
top = obj.get("top", 0)
|
| 108 |
-
width = obj.get("width", 0)
|
| 109 |
-
height = obj.get("height", 0)
|
| 110 |
-
radius = obj.get("radius", 0)
|
| 111 |
-
angle_deg = obj.get("angle", 0)
|
| 112 |
-
if radius > 0:
|
| 113 |
-
# Circle: (left, top) is mouse start point, not center.
|
| 114 |
-
# Center = start + radius * (cos(angle), sin(angle))
|
| 115 |
-
rx = ry = radius
|
| 116 |
-
angle_rad = np.deg2rad(angle_deg)
|
| 117 |
-
cx = left + radius * np.cos(angle_rad)
|
| 118 |
-
cy = top + radius * np.sin(angle_rad)
|
| 119 |
-
else:
|
| 120 |
-
# Ellipse: left, top = top-left of bounding box
|
| 121 |
-
rx = width / 2 if width > 0 else 0
|
| 122 |
-
ry = height / 2 if height > 0 else 0
|
| 123 |
-
if rx <= 0 or ry <= 0:
|
| 124 |
-
continue
|
| 125 |
-
cx = left + rx
|
| 126 |
-
cy = top + ry
|
| 127 |
-
if rx <= 0 or ry <= 0:
|
| 128 |
-
continue
|
| 129 |
-
n = 32
|
| 130 |
-
angles = np.linspace(0, 2 * np.pi, n, endpoint=False)
|
| 131 |
-
pts = np.column_stack([cx + rx * np.cos(angles), cy + ry * np.sin(angles)]).astype(np.float32)
|
| 132 |
-
elif obj_type == "path":
|
| 133 |
-
path = obj.get("path", [])
|
| 134 |
-
for cmd in path:
|
| 135 |
-
if isinstance(cmd, (list, tuple)) and len(cmd) >= 3:
|
| 136 |
-
if cmd[0] in ("M", "L"):
|
| 137 |
-
pts.append([float(cmd[1]), float(cmd[2])])
|
| 138 |
-
elif cmd[0] == "Q" and len(cmd) >= 5:
|
| 139 |
-
pts.append([float(cmd[3]), float(cmd[4])])
|
| 140 |
-
elif cmd[0] == "C" and len(cmd) >= 7:
|
| 141 |
-
pts.append([float(cmd[5]), float(cmd[6])])
|
| 142 |
-
if len(pts) < 3:
|
| 143 |
-
continue
|
| 144 |
-
pts = np.array(pts, dtype=np.float32)
|
| 145 |
-
else:
|
| 146 |
-
continue
|
| 147 |
-
pts[:, 0] *= scale_x
|
| 148 |
-
pts[:, 1] *= scale_y
|
| 149 |
-
pts = np.clip(pts, 0, [heatmap_w - 1, heatmap_h - 1]).astype(np.int32)
|
| 150 |
-
cv2.fillPoly(mask, [pts], 1)
|
| 151 |
-
count += 1
|
| 152 |
-
return mask, count
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
def _heatmap_to_rgb(scaled_heatmap, colormap_name="Jet"):
|
| 156 |
-
"""Convert scaled heatmap (float 0-1) to RGB array using the given colormap."""
|
| 157 |
-
heatmap_uint8 = (np.clip(scaled_heatmap, 0, 1) * 255).astype(np.uint8)
|
| 158 |
-
cv2_colormap = COLORMAPS.get(colormap_name, cv2.COLORMAP_JET)
|
| 159 |
-
heatmap_rgb = cv2.cvtColor(cv2.applyColorMap(heatmap_uint8, cv2_colormap), cv2.COLOR_BGR2RGB)
|
| 160 |
-
return heatmap_rgb
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
def _heatmap_to_png_bytes(scaled_heatmap, colormap_name="Jet"):
|
| 164 |
-
"""Convert scaled heatmap (float 0-1) to PNG bytes buffer."""
|
| 165 |
-
heatmap_rgb = _heatmap_to_rgb(scaled_heatmap, colormap_name)
|
| 166 |
-
buf = io.BytesIO()
|
| 167 |
-
Image.fromarray(heatmap_rgb).save(buf, format="PNG")
|
| 168 |
-
buf.seek(0)
|
| 169 |
-
return buf
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
def _create_pdf_report(img, scaled_heatmap, pixel_sum, force, force_scale, base_name, colormap_name="Jet"):
|
| 173 |
-
"""Create a PDF report with input image, heatmap, and metrics."""
|
| 174 |
-
from datetime import datetime
|
| 175 |
-
from reportlab.lib.pagesizes import A4
|
| 176 |
-
from reportlab.lib.units import inch
|
| 177 |
-
from reportlab.pdfgen import canvas
|
| 178 |
-
from reportlab.lib.utils import ImageReader
|
| 179 |
-
|
| 180 |
-
buf = io.BytesIO()
|
| 181 |
-
c = canvas.Canvas(buf, pagesize=A4)
|
| 182 |
-
c.setTitle("Shape2Force")
|
| 183 |
-
c.setAuthor("Angione-Lab")
|
| 184 |
-
w, h = A4
|
| 185 |
-
img_w, img_h = 2.5 * inch, 2.5 * inch
|
| 186 |
-
|
| 187 |
-
# Footer area (reserve space at bottom)
|
| 188 |
-
footer_y = 40
|
| 189 |
-
c.setFont("Helvetica", 8)
|
| 190 |
-
c.setFillColorRGB(0.4, 0.4, 0.4)
|
| 191 |
-
gen_date = datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 192 |
-
c.drawString(72, footer_y, f"Generated by Shape2Force (S2F) on {gen_date}")
|
| 193 |
-
c.drawString(72, footer_y - 12, "Model: https://huggingface.co/Angione-Lab/Shape2Force")
|
| 194 |
-
c.drawString(72, footer_y - 24, "Web app: https://huggingface.co/spaces/Angione-Lab/Shape2force")
|
| 195 |
-
c.setFillColorRGB(0, 0, 0)
|
| 196 |
-
|
| 197 |
-
# Images first (drawn lower so title can go on top)
|
| 198 |
-
img_top = h - 70
|
| 199 |
-
img_pil = Image.fromarray(img) if img.ndim == 2 else Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 200 |
-
img_buf = io.BytesIO()
|
| 201 |
-
img_pil.save(img_buf, format="PNG")
|
| 202 |
-
img_buf.seek(0)
|
| 203 |
-
c.drawImage(ImageReader(img_buf), 72, img_top - img_h, width=img_w, height=img_h, preserveAspectRatio=True)
|
| 204 |
-
c.setFont("Helvetica", 9)
|
| 205 |
-
c.drawString(72, img_top - img_h - 12, "Input: Bright-field")
|
| 206 |
-
|
| 207 |
-
heatmap_rgb = _heatmap_to_rgb(scaled_heatmap, colormap_name)
|
| 208 |
-
hm_buf = io.BytesIO()
|
| 209 |
-
Image.fromarray(heatmap_rgb).save(hm_buf, format="PNG")
|
| 210 |
-
hm_buf.seek(0)
|
| 211 |
-
c.drawImage(ImageReader(hm_buf), 72 + img_w + 20, img_top - img_h, width=img_w, height=img_h, preserveAspectRatio=True)
|
| 212 |
-
c.drawString(72 + img_w + 20, img_top - img_h - 12, "Output: Force map")
|
| 213 |
-
|
| 214 |
-
# Title above images
|
| 215 |
-
c.setFont("Helvetica-Bold", 16)
|
| 216 |
-
c.drawString(72, img_top + 25, "Shape2Force (S2F) - Prediction Report")
|
| 217 |
-
c.setFont("Helvetica", 10)
|
| 218 |
-
c.drawString(72, img_top + 8, f"Image: {base_name}")
|
| 219 |
-
|
| 220 |
-
# Metrics table below images
|
| 221 |
-
y = img_top - img_h - 45
|
| 222 |
-
c.setFont("Helvetica-Bold", 10)
|
| 223 |
-
c.drawString(72, y, "Metrics")
|
| 224 |
-
c.setFont("Helvetica", 9)
|
| 225 |
-
y -= 18
|
| 226 |
-
metrics = [
|
| 227 |
-
("Sum of all pixels", f"{pixel_sum * force_scale:.2f}"),
|
| 228 |
-
("Cell force (scaled)", f"{force * force_scale:.2f}"),
|
| 229 |
-
("Heatmap max", f"{np.max(scaled_heatmap):.4f}"),
|
| 230 |
-
("Heatmap mean", f"{np.mean(scaled_heatmap):.4f}"),
|
| 231 |
-
]
|
| 232 |
-
for label, val in metrics:
|
| 233 |
-
c.drawString(72, y, f"{label}: {val}")
|
| 234 |
-
y -= 16
|
| 235 |
-
|
| 236 |
-
c.save()
|
| 237 |
-
buf.seek(0)
|
| 238 |
-
return buf.getvalue()
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
def _build_original_vals(scaled_heatmap, pixel_sum, force, force_scale):
|
| 242 |
-
"""Build original_vals dict for measure tool."""
|
| 243 |
-
return {
|
| 244 |
-
"pixel_sum": pixel_sum * force_scale,
|
| 245 |
-
"force": force * force_scale,
|
| 246 |
-
"max": float(np.max(scaled_heatmap)),
|
| 247 |
-
"mean": float(np.mean(scaled_heatmap)),
|
| 248 |
-
}
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
def _render_result_display(img, scaled_heatmap, pixel_sum, force, force_scale, key_img, download_key_suffix="", colormap_name="Jet"):
|
| 252 |
-
"""Render prediction result: plot, metrics, expander, and download/measure buttons."""
|
| 253 |
-
buf_hm = _heatmap_to_png_bytes(scaled_heatmap, colormap_name)
|
| 254 |
-
base_name = os.path.splitext(key_img or "image")[0]
|
| 255 |
-
main_csv_rows = [
|
| 256 |
-
["image", "Sum of all pixels", "Cell force (scaled)", "Heatmap max", "Heatmap mean"],
|
| 257 |
-
[base_name, f"{pixel_sum * force_scale:.2f}", f"{force * force_scale:.2f}",
|
| 258 |
-
f"{np.max(scaled_heatmap):.4f}", f"{np.mean(scaled_heatmap):.4f}"],
|
| 259 |
-
]
|
| 260 |
-
buf_main_csv = io.StringIO()
|
| 261 |
-
csv.writer(buf_main_csv).writerows(main_csv_rows)
|
| 262 |
-
|
| 263 |
-
tit1, tit2 = st.columns(2)
|
| 264 |
-
with tit1:
|
| 265 |
-
st.markdown('<p style="font-size: 1.1rem; color: black; font-weight: 600;">Input: Bright-field image</p>', unsafe_allow_html=True)
|
| 266 |
-
with tit2:
|
| 267 |
-
st.markdown('<p style="font-size: 1.1rem; color: black; font-weight: 600;">Output: Predicted traction force map</p>', unsafe_allow_html=True)
|
| 268 |
-
fig_pl = make_subplots(rows=1, cols=2)
|
| 269 |
-
fig_pl.add_trace(go.Heatmap(z=img, colorscale="gray", showscale=False), row=1, col=1)
|
| 270 |
-
plotly_colorscale = _cv_colormap_to_plotly_colorscale(colormap_name)
|
| 271 |
-
fig_pl.add_trace(go.Heatmap(z=scaled_heatmap, colorscale=plotly_colorscale, zmin=0, zmax=1, showscale=True,
|
| 272 |
-
colorbar=dict(len=0.4, thickness=12)), row=1, col=2)
|
| 273 |
-
fig_pl.update_layout(
|
| 274 |
-
height=400,
|
| 275 |
-
margin=dict(l=10, r=10, t=10, b=10),
|
| 276 |
-
xaxis=dict(scaleanchor="y", scaleratio=1),
|
| 277 |
-
xaxis2=dict(scaleanchor="y2", scaleratio=1),
|
| 278 |
-
)
|
| 279 |
-
fig_pl.update_xaxes(showticklabels=False)
|
| 280 |
-
fig_pl.update_yaxes(showticklabels=False, autorange="reversed")
|
| 281 |
-
st.plotly_chart(fig_pl, use_container_width=True, config={"displayModeBar": True, "responsive": True})
|
| 282 |
-
|
| 283 |
-
col1, col2, col3, col4 = st.columns(4)
|
| 284 |
-
with col1:
|
| 285 |
-
st.metric("Sum of all pixels", f"{pixel_sum * force_scale:.2f}", help="Raw sum of all pixel values in the force map")
|
| 286 |
-
with col2:
|
| 287 |
-
st.metric("Cell force (scaled)", f"{force * force_scale:.2f}", help="Total traction force in physical units")
|
| 288 |
-
with col3:
|
| 289 |
-
st.metric("Heatmap max", f"{np.max(scaled_heatmap):.4f}", help="Peak force intensity in the map")
|
| 290 |
-
with col4:
|
| 291 |
-
st.metric("Heatmap mean", f"{np.mean(scaled_heatmap):.4f}", help="Average force intensity")
|
| 292 |
-
|
| 293 |
-
with st.expander("How to read the results"):
|
| 294 |
-
st.markdown("""
|
| 295 |
-
**Input (left):** Bright-field microscopy image of a cell or spheroid on a substrate.
|
| 296 |
-
This is the raw image you provided—it shows cell shape but not forces.
|
| 297 |
-
|
| 298 |
-
**Output (right):** Predicted traction force map.
|
| 299 |
-
- **Color** indicates force magnitude: blue = low, red = high
|
| 300 |
-
- **Brighter/warmer colors** = stronger forces exerted by the cell on the substrate
|
| 301 |
-
- Values are normalized to [0, 1] for visualization
|
| 302 |
-
|
| 303 |
-
**Metrics:**
|
| 304 |
-
- **Sum of all pixels:** Total force is the sum of all pixels in the force map. Each pixel represents the magnitude of force at that location; summing them gives the overall traction.
|
| 305 |
-
- **Cell force (scaled):** Total traction force in physical units (scaled by substrate stiffness)
|
| 306 |
-
- **Heatmap max/mean:** Peak and average force intensity in the map
|
| 307 |
-
""")
|
| 308 |
-
|
| 309 |
-
original_vals = _build_original_vals(scaled_heatmap, pixel_sum, force, force_scale)
|
| 310 |
-
pdf_bytes = _create_pdf_report(img, scaled_heatmap, pixel_sum, force, force_scale, base_name, colormap_name)
|
| 311 |
-
btn_col1, btn_col2, btn_col3, btn_col4 = st.columns(4)
|
| 312 |
-
with btn_col1:
|
| 313 |
-
if HAS_DRAWABLE_CANVAS and st_dialog:
|
| 314 |
-
if st.button("Measure tool", key="open_measure", icon=":material/straighten:"):
|
| 315 |
-
st.session_state["open_measure_dialog"] = True
|
| 316 |
-
st.rerun()
|
| 317 |
-
elif HAS_DRAWABLE_CANVAS:
|
| 318 |
-
with st.expander("Measure tool"):
|
| 319 |
-
_render_region_canvas(
|
| 320 |
-
scaled_heatmap,
|
| 321 |
-
bf_img=img,
|
| 322 |
-
original_vals=original_vals,
|
| 323 |
-
key_suffix="expander",
|
| 324 |
-
input_filename=key_img,
|
| 325 |
-
colormap_name=colormap_name,
|
| 326 |
-
)
|
| 327 |
-
else:
|
| 328 |
-
st.caption("Install `streamlit-drawable-canvas-fix` for region measurement: `pip install streamlit-drawable-canvas-fix`")
|
| 329 |
-
with btn_col2:
|
| 330 |
-
st.download_button(
|
| 331 |
-
"Download heatmap",
|
| 332 |
-
width="stretch",
|
| 333 |
-
data=buf_hm.getvalue(),
|
| 334 |
-
file_name="s2f_heatmap.png",
|
| 335 |
-
mime="image/png",
|
| 336 |
-
key=f"download_heatmap{download_key_suffix}",
|
| 337 |
-
icon=":material/download:",
|
| 338 |
-
)
|
| 339 |
-
with btn_col3:
|
| 340 |
-
st.download_button(
|
| 341 |
-
"Download values",
|
| 342 |
-
width="stretch",
|
| 343 |
-
data=buf_main_csv.getvalue(),
|
| 344 |
-
file_name=f"{base_name}_main_values.csv",
|
| 345 |
-
mime="text/csv",
|
| 346 |
-
key=f"download_main_values{download_key_suffix}",
|
| 347 |
-
icon=":material/download:",
|
| 348 |
-
)
|
| 349 |
-
with btn_col4:
|
| 350 |
-
st.download_button(
|
| 351 |
-
"Download report",
|
| 352 |
-
width="stretch",
|
| 353 |
-
data=pdf_bytes,
|
| 354 |
-
file_name=f"{base_name}_report.pdf",
|
| 355 |
-
mime="application/pdf",
|
| 356 |
-
key=f"download_pdf{download_key_suffix}",
|
| 357 |
-
icon=":material/picture_as_pdf:",
|
| 358 |
-
)
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
def _compute_region_metrics(scaled_heatmap, mask, original_vals=None):
|
| 362 |
-
"""Compute region metrics from mask. Returns dict with area_px, force_sum, density, etc."""
|
| 363 |
-
area_px = int(np.sum(mask))
|
| 364 |
-
region_values = scaled_heatmap * mask
|
| 365 |
-
region_nonzero = region_values[mask > 0]
|
| 366 |
-
force_sum = float(np.sum(region_values))
|
| 367 |
-
density = force_sum / area_px if area_px > 0 else 0
|
| 368 |
-
region_max = float(np.max(region_nonzero)) if len(region_nonzero) > 0 else 0
|
| 369 |
-
region_mean = float(np.mean(region_nonzero)) if len(region_nonzero) > 0 else 0
|
| 370 |
-
region_force_scaled = (
|
| 371 |
-
force_sum * (original_vals["force"] / original_vals["pixel_sum"])
|
| 372 |
-
if original_vals and original_vals.get("pixel_sum", 0) > 0
|
| 373 |
-
else force_sum
|
| 374 |
-
)
|
| 375 |
-
return {
|
| 376 |
-
"area_px": area_px,
|
| 377 |
-
"force_sum": force_sum,
|
| 378 |
-
"density": density,
|
| 379 |
-
"max": region_max,
|
| 380 |
-
"mean": region_mean,
|
| 381 |
-
"force_scaled": region_force_scaled,
|
| 382 |
-
}
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
def _render_region_metrics_and_downloads(metrics, heatmap_rgb, mask, input_filename, key_suffix, has_original_vals):
|
| 386 |
-
"""Render region metrics and download buttons."""
|
| 387 |
-
base_name = os.path.splitext(input_filename or "image")[0]
|
| 388 |
-
st.markdown("**Region (drawn)**")
|
| 389 |
-
if has_original_vals:
|
| 390 |
-
r1, r2, r3, r4, r5, r6 = st.columns(6)
|
| 391 |
-
with r1:
|
| 392 |
-
st.metric("Area", f"{metrics['area_px']:,}")
|
| 393 |
-
with r2:
|
| 394 |
-
st.metric("F.sum", f"{metrics['force_sum']:.3f}")
|
| 395 |
-
with r3:
|
| 396 |
-
st.metric("Force", f"{metrics['force_scaled']:.1f}")
|
| 397 |
-
with r4:
|
| 398 |
-
st.metric("Max", f"{metrics['max']:.3f}")
|
| 399 |
-
with r5:
|
| 400 |
-
st.metric("Mean", f"{metrics['mean']:.3f}")
|
| 401 |
-
with r6:
|
| 402 |
-
st.metric("Density", f"{metrics['density']:.4f}")
|
| 403 |
-
csv_rows = [
|
| 404 |
-
["image", "Area", "F.sum", "Force", "Max", "Mean", "Density"],
|
| 405 |
-
[base_name, metrics["area_px"], f"{metrics['force_sum']:.3f}", f"{metrics['force_scaled']:.1f}",
|
| 406 |
-
f"{metrics['max']:.3f}", f"{metrics['mean']:.3f}", f"{metrics['density']:.4f}"],
|
| 407 |
-
]
|
| 408 |
-
else:
|
| 409 |
-
c1, c2, c3 = st.columns(3)
|
| 410 |
-
with c1:
|
| 411 |
-
st.metric("Area (px²)", f"{metrics['area_px']:,}")
|
| 412 |
-
with c2:
|
| 413 |
-
st.metric("Force sum", f"{metrics['force_sum']:.4f}")
|
| 414 |
-
with c3:
|
| 415 |
-
st.metric("Density", f"{metrics['density']:.6f}")
|
| 416 |
-
csv_rows = [
|
| 417 |
-
["image", "Area", "Force sum", "Density"],
|
| 418 |
-
[base_name, metrics["area_px"], f"{metrics['force_sum']:.4f}", f"{metrics['density']:.6f}"],
|
| 419 |
-
]
|
| 420 |
-
buf_csv = io.StringIO()
|
| 421 |
-
csv.writer(buf_csv).writerows(csv_rows)
|
| 422 |
-
buf_img = io.BytesIO()
|
| 423 |
-
Image.fromarray(_make_annotated_heatmap(heatmap_rgb, mask)).save(buf_img, format="PNG")
|
| 424 |
-
buf_img.seek(0)
|
| 425 |
-
dl_col1, dl_col2 = st.columns(2)
|
| 426 |
-
with dl_col1:
|
| 427 |
-
st.download_button("Download values", data=buf_csv.getvalue(),
|
| 428 |
-
file_name=f"{base_name}_region_values.csv", mime="text/csv",
|
| 429 |
-
key=f"download_region_values_{key_suffix}", icon=":material/download:")
|
| 430 |
-
with dl_col2:
|
| 431 |
-
st.download_button("Download annotated heatmap", data=buf_img.getvalue(),
|
| 432 |
-
file_name=f"{base_name}_annotated_heatmap.png", mime="image/png",
|
| 433 |
-
key=f"download_annotated_{key_suffix}", icon=":material/image:")
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
def _render_region_canvas(scaled_heatmap, bf_img=None, original_vals=None, key_suffix="", input_filename=None, colormap_name="Jet"):
|
| 437 |
-
"""Render drawable canvas and region metrics. Used in dialog or expander."""
|
| 438 |
-
h, w = scaled_heatmap.shape
|
| 439 |
-
heatmap_rgb = _heatmap_to_rgb(scaled_heatmap, colormap_name)
|
| 440 |
-
pil_bg = Image.fromarray(heatmap_rgb).resize((CANVAS_SIZE, CANVAS_SIZE), Image.Resampling.LANCZOS)
|
| 441 |
-
|
| 442 |
-
st.markdown("""
|
| 443 |
-
<style>
|
| 444 |
-
[data-testid="stDialog"] [data-testid="stSelectbox"], [data-testid="stExpander"] [data-testid="stSelectbox"],
|
| 445 |
-
[data-testid="stDialog"] [data-testid="stSelectbox"] > div, [data-testid="stExpander"] [data-testid="stSelectbox"] > div {
|
| 446 |
-
width: 100% !important; max-width: 100% !important;
|
| 447 |
-
}
|
| 448 |
-
[data-testid="stDialog"] [data-testid="stMetric"] label, [data-testid="stDialog"] [data-testid="stMetric"] [data-testid="stMetricValue"],
|
| 449 |
-
[data-testid="stExpander"] [data-testid="stMetric"] label, [data-testid="stExpander"] [data-testid="stMetric"] [data-testid="stMetricValue"] {
|
| 450 |
-
font-size: 0.95rem !important;
|
| 451 |
-
}
|
| 452 |
-
[data-testid="stDialog"] img, [data-testid="stExpander"] img { border-radius: 0 !important; }
|
| 453 |
-
</style>
|
| 454 |
-
""", unsafe_allow_html=True)
|
| 455 |
-
|
| 456 |
-
if bf_img is not None:
|
| 457 |
-
bf_resized = cv2.resize(bf_img, (CANVAS_SIZE, CANVAS_SIZE))
|
| 458 |
-
bf_rgb = cv2.cvtColor(bf_resized, cv2.COLOR_GRAY2RGB) if bf_img.ndim == 2 else cv2.cvtColor(bf_resized, cv2.COLOR_BGR2RGB)
|
| 459 |
-
left_col, right_col = st.columns(2, gap=None)
|
| 460 |
-
with left_col:
|
| 461 |
-
draw_mode = st.selectbox("Tool", DRAW_TOOLS, format_func=lambda x: TOOL_LABELS[x], key=f"draw_mode_region_{key_suffix}")
|
| 462 |
-
st.caption("Left-click add, right-click close. \nForce map (draw region)")
|
| 463 |
-
canvas_result = st_canvas(
|
| 464 |
-
fill_color="rgba(255, 165, 0, 0.3)", stroke_width=2, stroke_color="#ff6600",
|
| 465 |
-
background_image=pil_bg, drawing_mode=draw_mode, update_streamlit=True,
|
| 466 |
-
height=CANVAS_SIZE, width=CANVAS_SIZE, display_toolbar=True,
|
| 467 |
-
key=f"region_measure_canvas_{key_suffix}",
|
| 468 |
-
)
|
| 469 |
-
with right_col:
|
| 470 |
-
if original_vals:
|
| 471 |
-
st.markdown('<p style="font-weight: 400; color: #334155; font-size: 0.95rem; margin: 0 20px 4px 4px;">Full map</p>', unsafe_allow_html=True)
|
| 472 |
-
st.markdown(f"""
|
| 473 |
-
<div style="width: 100%; box-sizing: border-box; border: 1px solid #e2e8f0; border-radius: 10px;
|
| 474 |
-
padding: 10px 12px; margin: 0 10px 20px 10px; background: linear-gradient(145deg, #f8fafc 0%, #f1f5f9 100%);
|
| 475 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.06);">
|
| 476 |
-
<div style="display: flex; flex-wrap: wrap; gap: 5px; font-size: 0.9rem;">
|
| 477 |
-
<span><strong>Sum:</strong> {original_vals['pixel_sum']:.1f}</span>
|
| 478 |
-
<span><strong>Force:</strong> {original_vals['force']:.1f}</span>
|
| 479 |
-
<span><strong>Max:</strong> {original_vals['max']:.3f}</span>
|
| 480 |
-
<span><strong>Mean:</strong> {original_vals['mean']:.3f}</span>
|
| 481 |
-
</div>
|
| 482 |
-
</div>
|
| 483 |
-
""", unsafe_allow_html=True)
|
| 484 |
-
st.caption("Bright-field")
|
| 485 |
-
st.image(bf_rgb, width=CANVAS_SIZE)
|
| 486 |
-
else:
|
| 487 |
-
st.markdown("**Draw a region** on the heatmap.")
|
| 488 |
-
draw_mode = st.selectbox("Drawing tool", DRAW_TOOLS,
|
| 489 |
-
format_func=lambda x: "Polygon (free shape)" if x == "polygon" else TOOL_LABELS[x],
|
| 490 |
-
key=f"draw_mode_region_{key_suffix}")
|
| 491 |
-
st.caption("Polygon: left-click to add points, right-click to close.")
|
| 492 |
-
canvas_result = st_canvas(
|
| 493 |
-
fill_color="rgba(255, 165, 0, 0.3)", stroke_width=2, stroke_color="#ff6600",
|
| 494 |
-
background_image=pil_bg, drawing_mode=draw_mode, update_streamlit=True,
|
| 495 |
-
height=CANVAS_SIZE, width=CANVAS_SIZE, display_toolbar=True,
|
| 496 |
-
key=f"region_measure_canvas_{key_suffix}",
|
| 497 |
-
)
|
| 498 |
-
|
| 499 |
-
if canvas_result.json_data:
|
| 500 |
-
mask, n = _parse_canvas_shapes_to_mask(canvas_result.json_data, CANVAS_SIZE, CANVAS_SIZE, h, w)
|
| 501 |
-
if mask is not None and n > 0:
|
| 502 |
-
metrics = _compute_region_metrics(scaled_heatmap, mask, original_vals)
|
| 503 |
-
_render_region_metrics_and_downloads(metrics, heatmap_rgb, mask, input_filename, key_suffix, original_vals is not None)
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
st_dialog = getattr(st, "dialog", None) or getattr(st, "experimental_dialog", None)
|
| 507 |
-
if HAS_DRAWABLE_CANVAS and st_dialog:
|
| 508 |
-
@st_dialog("Measure tool", width="medium")
|
| 509 |
def measure_region_dialog():
|
| 510 |
-
|
| 511 |
-
if
|
| 512 |
st.warning("No prediction available to measure.")
|
| 513 |
return
|
|
|
|
|
|
|
| 514 |
bf_img = st.session_state.get("measure_bf_img")
|
| 515 |
original_vals = st.session_state.get("measure_original_vals")
|
|
|
|
|
|
|
| 516 |
input_filename = st.session_state.get("measure_input_filename", "image")
|
| 517 |
colormap_name = st.session_state.get("measure_colormap", "Jet")
|
| 518 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 519 |
else:
|
| 520 |
def measure_region_dialog():
|
| 521 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
|
| 523 |
|
| 524 |
st.set_page_config(page_title="Shape2Force (S2F)", page_icon="🦠", layout="centered")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
st.markdown("""
|
| 526 |
<style>
|
| 527 |
section[data-testid="stSidebar"] { width: 380px !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
div[data-testid="stHorizontalBlock"]:has([data-testid="stDownloadButton"]):has([data-testid="stButton"]) > div {
|
| 529 |
flex: 1 1 0 !important; min-width: 0 !important;
|
| 530 |
}
|
|
@@ -539,12 +118,12 @@ div[data-testid="stHorizontalBlock"]:has([data-testid="stDownloadButton"]):has([
|
|
| 539 |
}
|
| 540 |
</style>
|
| 541 |
""", unsafe_allow_html=True)
|
|
|
|
| 542 |
st.title("🦠 Shape2Force (S2F)")
|
| 543 |
st.caption("Predict traction force maps from bright-field microscopy images of cells or spheroids")
|
| 544 |
|
| 545 |
-
# Folders
|
| 546 |
ckp_base = os.path.join(S2F_ROOT, "ckp")
|
| 547 |
-
# Fallback: use project root ckp when running from S2F repo (ckp at S2F/ckp/)
|
| 548 |
if not os.path.isdir(ckp_base):
|
| 549 |
project_root = os.path.dirname(S2F_ROOT)
|
| 550 |
if os.path.isdir(os.path.join(project_root, "ckp")):
|
|
@@ -557,7 +136,6 @@ sample_spheroid = os.path.join(sample_base, "spheroid")
|
|
| 557 |
|
| 558 |
|
| 559 |
def get_ckp_files_for_model(model_type):
|
| 560 |
-
"""Return list of .pth files in the checkpoint folder for the given model type."""
|
| 561 |
folder = ckp_single_cell if model_type == "single_cell" else ckp_spheroid
|
| 562 |
if os.path.isdir(folder):
|
| 563 |
return sorted(f for f in os.listdir(folder) if f.endswith(".pth"))
|
|
@@ -565,15 +143,32 @@ def get_ckp_files_for_model(model_type):
|
|
| 565 |
|
| 566 |
|
| 567 |
def get_sample_files_for_model(model_type):
|
| 568 |
-
"""Return list of sample images in the sample folder for the given model type."""
|
| 569 |
folder = sample_single_cell if model_type == "single_cell" else sample_spheroid
|
| 570 |
if os.path.isdir(folder):
|
| 571 |
return sorted(f for f in os.listdir(folder) if f.lower().endswith(SAMPLE_EXTENSIONS))
|
| 572 |
return []
|
| 573 |
|
| 574 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
with st.sidebar:
|
| 576 |
-
st.header("
|
|
|
|
| 577 |
model_type = st.radio(
|
| 578 |
"Model type",
|
| 579 |
["single_cell", "spheroid"],
|
|
@@ -581,7 +176,6 @@ with st.sidebar:
|
|
| 581 |
horizontal=False,
|
| 582 |
help="Single cell: substrate-aware force prediction. Spheroid: spheroid force maps.",
|
| 583 |
)
|
| 584 |
-
st.caption(f"Inference mode: **{MODEL_TYPE_LABELS[model_type]}**")
|
| 585 |
|
| 586 |
ckp_files = get_ckp_files_for_model(model_type)
|
| 587 |
ckp_folder = ckp_single_cell if model_type == "single_cell" else ckp_spheroid
|
|
@@ -598,37 +192,43 @@ with st.sidebar:
|
|
| 598 |
checkpoint = None
|
| 599 |
|
| 600 |
substrate_config = None
|
| 601 |
-
substrate_val = "
|
| 602 |
-
use_manual =
|
| 603 |
if model_type == "single_cell":
|
| 604 |
try:
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
"
|
| 608 |
-
|
| 609 |
-
|
|
|
|
| 610 |
)
|
| 611 |
-
|
| 612 |
-
if
|
| 613 |
-
|
| 614 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
value=3.0769, step=0.1, format="%.4f")
|
| 616 |
-
manual_young = st.number_input("
|
| 617 |
value=6000.0, step=100.0, format="%.0f")
|
| 618 |
substrate_config = {"pixelsize": manual_pixelsize, "young": manual_young}
|
|
|
|
| 619 |
except FileNotFoundError:
|
| 620 |
st.error("config/substrate_settings.json not found")
|
| 621 |
|
| 622 |
-
st.
|
| 623 |
-
st.header("Display options")
|
| 624 |
-
force_scale = st.slider(
|
| 625 |
"Force scale",
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
step=0.01,
|
| 630 |
-
format="%.2f",
|
| 631 |
-
help="Scale the displayed force values. 1 = full intensity, 0.5 = half the pixel values.",
|
| 632 |
)
|
| 633 |
colormap_name = st.selectbox(
|
| 634 |
"Heatmap colormap",
|
|
@@ -636,6 +236,15 @@ with st.sidebar:
|
|
| 636 |
help="Color scheme for the force map. Viridis is often preferred for accessibility.",
|
| 637 |
)
|
| 638 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
# Main area: image input
|
| 640 |
img_source = st.radio("Image source", ["Upload", "Example"], horizontal=True, label_visibility="collapsed")
|
| 641 |
img = None
|
|
@@ -646,13 +255,13 @@ if img_source == "Upload":
|
|
| 646 |
uploaded = st.file_uploader(
|
| 647 |
"Upload bright-field image",
|
| 648 |
type=["tif", "tiff", "png", "jpg", "jpeg"],
|
| 649 |
-
help="Bright-field microscopy image of a cell or spheroid on a substrate (grayscale or RGB).
|
| 650 |
)
|
| 651 |
if uploaded:
|
| 652 |
bytes_data = uploaded.read()
|
| 653 |
nparr = np.frombuffer(bytes_data, np.uint8)
|
| 654 |
img = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
|
| 655 |
-
uploaded.seek(0)
|
| 656 |
else:
|
| 657 |
sample_files = get_sample_files_for_model(model_type)
|
| 658 |
sample_folder = sample_single_cell if model_type == "single_cell" else sample_spheroid
|
|
@@ -667,15 +276,14 @@ else:
|
|
| 667 |
if selected_sample:
|
| 668 |
sample_path = os.path.join(sample_folder, selected_sample)
|
| 669 |
img = cv2.imread(sample_path, cv2.IMREAD_GRAYSCALE)
|
| 670 |
-
#
|
| 671 |
-
|
|
|
|
| 672 |
cols = st.columns(n_cols)
|
| 673 |
-
for i, fname in enumerate(
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
if sample_img is not None:
|
| 678 |
-
st.image(sample_img, caption=fname, width='content')
|
| 679 |
else:
|
| 680 |
st.info(f"No example images in samples/{sample_subfolder_name}/. Add images or use Upload.")
|
| 681 |
|
|
@@ -689,28 +297,36 @@ with col_path:
|
|
| 689 |
st.markdown(f"<span style='display: inline-flex; align-items: center; height: 38px;'>Checkpoint: <code>{ckp_path}</code></span>", unsafe_allow_html=True)
|
| 690 |
has_image = img is not None
|
| 691 |
|
| 692 |
-
# Persist results in session state so they survive re-runs (e.g. when clicking Download)
|
| 693 |
if "prediction_result" not in st.session_state:
|
| 694 |
st.session_state["prediction_result"] = None
|
| 695 |
|
| 696 |
-
# Show results if we just ran prediction OR we have cached results from a previous run
|
| 697 |
just_ran = run and checkpoint and has_image
|
| 698 |
cached = st.session_state["prediction_result"]
|
| 699 |
key_img = (uploaded.name if uploaded else None) if img_source == "Upload" else selected_sample
|
| 700 |
current_key = (model_type, checkpoint, key_img)
|
| 701 |
has_cached = cached is not None and cached.get("cache_key") == current_key
|
| 702 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 703 |
if just_ran:
|
| 704 |
-
st.session_state["prediction_result"] = None
|
| 705 |
with st.spinner("Loading model and predicting..."):
|
| 706 |
try:
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
model_type=model_type,
|
| 710 |
-
checkpoint_path=checkpoint,
|
| 711 |
-
ckp_folder=ckp_folder,
|
| 712 |
-
)
|
| 713 |
-
sub_val = substrate_val if model_type == "single_cell" and not use_manual else "fibroblasts_PDMS"
|
| 714 |
heatmap, force, pixel_sum = predictor.predict(
|
| 715 |
image_array=img,
|
| 716 |
substrate=sub_val,
|
|
@@ -719,7 +335,7 @@ if just_ran:
|
|
| 719 |
|
| 720 |
st.success("Prediction complete!")
|
| 721 |
|
| 722 |
-
|
| 723 |
|
| 724 |
cache_key = (model_type, checkpoint, key_img)
|
| 725 |
st.session_state["prediction_result"] = {
|
|
@@ -729,13 +345,24 @@ if just_ran:
|
|
| 729 |
"pixel_sum": pixel_sum,
|
| 730 |
"cache_key": cache_key,
|
| 731 |
}
|
| 732 |
-
st.session_state["
|
|
|
|
| 733 |
st.session_state["measure_bf_img"] = img.copy()
|
| 734 |
st.session_state["measure_input_filename"] = key_img or "image"
|
| 735 |
-
st.session_state["measure_original_vals"] =
|
| 736 |
st.session_state["measure_colormap"] = colormap_name
|
| 737 |
-
|
| 738 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 739 |
|
| 740 |
except Exception as e:
|
| 741 |
st.error(f"Prediction failed: {e}")
|
|
@@ -744,19 +371,31 @@ if just_ran:
|
|
| 744 |
elif has_cached:
|
| 745 |
r = st.session_state["prediction_result"]
|
| 746 |
img, heatmap, force, pixel_sum = r["img"], r["heatmap"], r["force"], r["pixel_sum"]
|
| 747 |
-
|
| 748 |
|
| 749 |
-
st.session_state["
|
|
|
|
| 750 |
st.session_state["measure_bf_img"] = img.copy()
|
| 751 |
st.session_state["measure_input_filename"] = key_img or "image"
|
| 752 |
-
st.session_state["measure_original_vals"] =
|
| 753 |
st.session_state["measure_colormap"] = colormap_name
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
|
| 755 |
if st.session_state.pop("open_measure_dialog", False):
|
| 756 |
measure_region_dialog()
|
| 757 |
|
| 758 |
st.success("Prediction complete!")
|
| 759 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 760 |
|
| 761 |
elif run and not checkpoint:
|
| 762 |
st.warning("Please add checkpoint files to the ckp/ folder and select one.")
|
|
@@ -764,6 +403,5 @@ elif run and not has_image:
|
|
| 764 |
st.warning("Please upload an image or select an example.")
|
| 765 |
|
| 766 |
st.sidebar.divider()
|
| 767 |
-
st.sidebar.caption(f"Examples: `samples/{ckp_subfolder_name}/`")
|
| 768 |
st.sidebar.caption("If you find this software useful, please cite:")
|
| 769 |
st.sidebar.caption(CITATION)
|
|
|
|
| 1 |
"""
|
| 2 |
Shape2Force (S2F) - GUI for force map prediction from bright field microscopy images.
|
| 3 |
"""
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
import traceback
|
|
|
|
| 10 |
|
| 11 |
import numpy as np
|
| 12 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
S2F_ROOT = os.path.dirname(os.path.abspath(__file__))
|
| 15 |
if S2F_ROOT not in sys.path:
|
| 16 |
sys.path.insert(0, S2F_ROOT)
|
| 17 |
|
| 18 |
+
from config.constants import (
|
| 19 |
+
COLORMAPS,
|
| 20 |
+
MODEL_TYPE_LABELS,
|
| 21 |
+
SAMPLE_EXTENSIONS,
|
| 22 |
+
)
|
| 23 |
+
from utils.segmentation import estimate_cell_mask
|
| 24 |
from utils.substrate_settings import list_substrates
|
| 25 |
+
from utils.display import apply_display_scale
|
| 26 |
+
from ui.components import (
|
| 27 |
+
build_original_vals,
|
| 28 |
+
build_cell_vals,
|
| 29 |
+
render_result_display,
|
| 30 |
+
render_region_canvas,
|
| 31 |
+
ST_DIALOG,
|
| 32 |
+
HAS_DRAWABLE_CANVAS,
|
| 33 |
+
)
|
| 34 |
|
| 35 |
try:
|
| 36 |
from streamlit_drawable_canvas import st_canvas
|
|
|
|
| 37 |
except (ImportError, AttributeError):
|
| 38 |
+
pass # HAS_DRAWABLE_CANVAS from ui.components
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
CITATION = (
|
| 41 |
"Lautaro Baro, Kaveh Shahhosseini, Amparo Andrés-Bordería, Claudio Angione, and Maria Angeles Juanes. "
|
| 42 |
"**\"Shape-to-force (S2F): Predicting Cell Traction Forces from LabelFree Imaging\"**, 2026."
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Measure tool dialog: defined early so it exists before render_result_display uses it
|
| 46 |
+
if HAS_DRAWABLE_CANVAS and ST_DIALOG:
|
| 47 |
+
@ST_DIALOG("Measure tool", width="medium")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def measure_region_dialog():
|
| 49 |
+
raw_heatmap = st.session_state.get("measure_raw_heatmap")
|
| 50 |
+
if raw_heatmap is None:
|
| 51 |
st.warning("No prediction available to measure.")
|
| 52 |
return
|
| 53 |
+
display_mode = st.session_state.get("measure_display_mode", "Auto")
|
| 54 |
+
display_heatmap = apply_display_scale(raw_heatmap, display_mode)
|
| 55 |
bf_img = st.session_state.get("measure_bf_img")
|
| 56 |
original_vals = st.session_state.get("measure_original_vals")
|
| 57 |
+
cell_vals = st.session_state.get("measure_cell_vals")
|
| 58 |
+
cell_mask = st.session_state.get("measure_cell_mask")
|
| 59 |
input_filename = st.session_state.get("measure_input_filename", "image")
|
| 60 |
colormap_name = st.session_state.get("measure_colormap", "Jet")
|
| 61 |
+
render_region_canvas(
|
| 62 |
+
display_heatmap, raw_heatmap=raw_heatmap, bf_img=bf_img,
|
| 63 |
+
original_vals=original_vals, cell_vals=cell_vals, cell_mask=cell_mask,
|
| 64 |
+
key_suffix="dialog", input_filename=input_filename, colormap_name=colormap_name,
|
| 65 |
+
)
|
| 66 |
else:
|
| 67 |
def measure_region_dialog():
|
| 68 |
+
pass
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _get_measure_dialog_fn():
|
| 72 |
+
"""Return measure dialog callable if available, else None (fixes st_dialog ordering)."""
|
| 73 |
+
return measure_region_dialog if (HAS_DRAWABLE_CANVAS and ST_DIALOG) else None
|
| 74 |
|
| 75 |
|
| 76 |
st.set_page_config(page_title="Shape2Force (S2F)", page_icon="🦠", layout="centered")
|
| 77 |
+
|
| 78 |
+
# Theme CSS (inject based on sidebar selection)
|
| 79 |
+
def _inject_theme_css(theme):
|
| 80 |
+
if theme == "Dark":
|
| 81 |
+
st.markdown("""
|
| 82 |
+
<style>
|
| 83 |
+
.stApp { background-color: #0e1117 !important; }
|
| 84 |
+
.stApp header { background-color: #0e1117 !important; }
|
| 85 |
+
section[data-testid="stSidebar"] { background-color: #1a1a2e !important; }
|
| 86 |
+
section[data-testid="stSidebar"] .stMarkdown { color: #fafafa !important; }
|
| 87 |
+
section[data-testid="stSidebar"] [data-testid="stWidgetLabel"] { color: #e2e8f0 !important; }
|
| 88 |
+
h1, h2, h3 { color: #fafafa !important; }
|
| 89 |
+
p { color: #e2e8f0 !important; }
|
| 90 |
+
.stCaption { color: #94a3b8 !important; }
|
| 91 |
+
</style>
|
| 92 |
+
""", unsafe_allow_html=True)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
st.markdown("""
|
| 96 |
<style>
|
| 97 |
section[data-testid="stSidebar"] { width: 380px !important; }
|
| 98 |
+
section[data-testid="stSidebar"] h2 {
|
| 99 |
+
font-size: 1.25rem !important;
|
| 100 |
+
font-weight: 600 !important;
|
| 101 |
+
}
|
| 102 |
+
section[data-testid="stSidebar"] [data-testid="stWidgetLabel"],
|
| 103 |
+
section[data-testid="stSidebar"] [data-testid="stWidgetLabel"] p {
|
| 104 |
+
font-size: 0.95rem !important;
|
| 105 |
+
font-weight: 500 !important;
|
| 106 |
+
}
|
| 107 |
div[data-testid="stHorizontalBlock"]:has([data-testid="stDownloadButton"]):has([data-testid="stButton"]) > div {
|
| 108 |
flex: 1 1 0 !important; min-width: 0 !important;
|
| 109 |
}
|
|
|
|
| 118 |
}
|
| 119 |
</style>
|
| 120 |
""", unsafe_allow_html=True)
|
| 121 |
+
|
| 122 |
st.title("🦠 Shape2Force (S2F)")
|
| 123 |
st.caption("Predict traction force maps from bright-field microscopy images of cells or spheroids")
|
| 124 |
|
| 125 |
+
# Folders
|
| 126 |
ckp_base = os.path.join(S2F_ROOT, "ckp")
|
|
|
|
| 127 |
if not os.path.isdir(ckp_base):
|
| 128 |
project_root = os.path.dirname(S2F_ROOT)
|
| 129 |
if os.path.isdir(os.path.join(project_root, "ckp")):
|
|
|
|
| 136 |
|
| 137 |
|
| 138 |
def get_ckp_files_for_model(model_type):
|
|
|
|
| 139 |
folder = ckp_single_cell if model_type == "single_cell" else ckp_spheroid
|
| 140 |
if os.path.isdir(folder):
|
| 141 |
return sorted(f for f in os.listdir(folder) if f.endswith(".pth"))
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
def get_sample_files_for_model(model_type):
|
|
|
|
| 146 |
folder = sample_single_cell if model_type == "single_cell" else sample_spheroid
|
| 147 |
if os.path.isdir(folder):
|
| 148 |
return sorted(f for f in os.listdir(folder) if f.lower().endswith(SAMPLE_EXTENSIONS))
|
| 149 |
return []
|
| 150 |
|
| 151 |
+
|
| 152 |
+
def get_cached_sample_thumbnails(model_type, sample_folder, sample_files):
|
| 153 |
+
"""Return cached sample thumbnails. Key by (model_type, tuple(files))."""
|
| 154 |
+
cache_key = (model_type, tuple(sample_files))
|
| 155 |
+
if "sample_thumbnails" not in st.session_state:
|
| 156 |
+
st.session_state["sample_thumbnails"] = {}
|
| 157 |
+
cache = st.session_state["sample_thumbnails"]
|
| 158 |
+
if cache_key not in cache:
|
| 159 |
+
thumbnails = []
|
| 160 |
+
for fname in sample_files[:8]:
|
| 161 |
+
path = os.path.join(sample_folder, fname)
|
| 162 |
+
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
|
| 163 |
+
thumbnails.append((fname, img))
|
| 164 |
+
cache[cache_key] = thumbnails
|
| 165 |
+
return cache[cache_key]
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
# Sidebar
|
| 169 |
with st.sidebar:
|
| 170 |
+
st.header("Settings")
|
| 171 |
+
|
| 172 |
model_type = st.radio(
|
| 173 |
"Model type",
|
| 174 |
["single_cell", "spheroid"],
|
|
|
|
| 176 |
horizontal=False,
|
| 177 |
help="Single cell: substrate-aware force prediction. Spheroid: spheroid force maps.",
|
| 178 |
)
|
|
|
|
| 179 |
|
| 180 |
ckp_files = get_ckp_files_for_model(model_type)
|
| 181 |
ckp_folder = ckp_single_cell if model_type == "single_cell" else ckp_spheroid
|
|
|
|
| 192 |
checkpoint = None
|
| 193 |
|
| 194 |
substrate_config = None
|
| 195 |
+
substrate_val = "Fibroblasts_Fibronectin_6KPa"
|
| 196 |
+
use_manual = True
|
| 197 |
if model_type == "single_cell":
|
| 198 |
try:
|
| 199 |
+
st.markdown('<p style="font-size: 0.95rem; font-weight: 500; margin-bottom: 0.5rem;">Conditions</p>', unsafe_allow_html=True)
|
| 200 |
+
conditions_source = st.radio(
|
| 201 |
+
"Conditions",
|
| 202 |
+
["Manually", "From config"],
|
| 203 |
+
horizontal=True,
|
| 204 |
+
label_visibility="collapsed",
|
| 205 |
)
|
| 206 |
+
from_config = conditions_source == "From config"
|
| 207 |
+
if from_config:
|
| 208 |
+
substrate_config = None
|
| 209 |
+
substrates = list_substrates()
|
| 210 |
+
substrate_val = st.selectbox(
|
| 211 |
+
"Conditions (from config)",
|
| 212 |
+
substrates,
|
| 213 |
+
help="Select a preset from config/substrate_settings.json",
|
| 214 |
+
label_visibility="collapsed",
|
| 215 |
+
)
|
| 216 |
+
use_manual = False
|
| 217 |
+
else:
|
| 218 |
+
manual_pixelsize = st.number_input("Pixel size (µm/px)", min_value=0.1, max_value=50.0,
|
| 219 |
value=3.0769, step=0.1, format="%.4f")
|
| 220 |
+
manual_young = st.number_input("Pascals", min_value=100.0, max_value=100000.0,
|
| 221 |
value=6000.0, step=100.0, format="%.0f")
|
| 222 |
substrate_config = {"pixelsize": manual_pixelsize, "young": manual_young}
|
| 223 |
+
use_manual = True
|
| 224 |
except FileNotFoundError:
|
| 225 |
st.error("config/substrate_settings.json not found")
|
| 226 |
|
| 227 |
+
display_mode = st.radio(
|
|
|
|
|
|
|
| 228 |
"Force scale",
|
| 229 |
+
["Auto", "Fixed"],
|
| 230 |
+
help="Auto: map data range to full color scale (Fiji-style). Fixed: use 0-1 range. Metrics always show raw values.",
|
| 231 |
+
horizontal=True,
|
|
|
|
|
|
|
|
|
|
| 232 |
)
|
| 233 |
colormap_name = st.selectbox(
|
| 234 |
"Heatmap colormap",
|
|
|
|
| 236 |
help="Color scheme for the force map. Viridis is often preferred for accessibility.",
|
| 237 |
)
|
| 238 |
|
| 239 |
+
auto_cell_boundary = st.checkbox(
|
| 240 |
+
"Auto boundary",
|
| 241 |
+
value=True,
|
| 242 |
+
help="When on: estimate cell region from force map and use it for metrics (red contour). When off: use entire map.",
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
theme = st.radio("Theme", ["Light", "Dark"], horizontal=True, key="theme_selector")
|
| 246 |
+
_inject_theme_css(theme)
|
| 247 |
+
|
| 248 |
# Main area: image input
|
| 249 |
img_source = st.radio("Image source", ["Upload", "Example"], horizontal=True, label_visibility="collapsed")
|
| 250 |
img = None
|
|
|
|
| 255 |
uploaded = st.file_uploader(
|
| 256 |
"Upload bright-field image",
|
| 257 |
type=["tif", "tiff", "png", "jpg", "jpeg"],
|
| 258 |
+
help="Bright-field microscopy image of a cell or spheroid on a substrate (grayscale or RGB).",
|
| 259 |
)
|
| 260 |
if uploaded:
|
| 261 |
bytes_data = uploaded.read()
|
| 262 |
nparr = np.frombuffer(bytes_data, np.uint8)
|
| 263 |
img = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
|
| 264 |
+
uploaded.seek(0)
|
| 265 |
else:
|
| 266 |
sample_files = get_sample_files_for_model(model_type)
|
| 267 |
sample_folder = sample_single_cell if model_type == "single_cell" else sample_spheroid
|
|
|
|
| 276 |
if selected_sample:
|
| 277 |
sample_path = os.path.join(sample_folder, selected_sample)
|
| 278 |
img = cv2.imread(sample_path, cv2.IMREAD_GRAYSCALE)
|
| 279 |
+
# Cached thumbnails
|
| 280 |
+
thumbnails = get_cached_sample_thumbnails(model_type, sample_folder, sample_files)
|
| 281 |
+
n_cols = min(5, len(thumbnails))
|
| 282 |
cols = st.columns(n_cols)
|
| 283 |
+
for i, (fname, sample_img) in enumerate(thumbnails):
|
| 284 |
+
if sample_img is not None:
|
| 285 |
+
with cols[i % n_cols]:
|
| 286 |
+
st.image(sample_img, caption=fname, width=120)
|
|
|
|
|
|
|
| 287 |
else:
|
| 288 |
st.info(f"No example images in samples/{sample_subfolder_name}/. Add images or use Upload.")
|
| 289 |
|
|
|
|
| 297 |
st.markdown(f"<span style='display: inline-flex; align-items: center; height: 38px;'>Checkpoint: <code>{ckp_path}</code></span>", unsafe_allow_html=True)
|
| 298 |
has_image = img is not None
|
| 299 |
|
|
|
|
| 300 |
if "prediction_result" not in st.session_state:
|
| 301 |
st.session_state["prediction_result"] = None
|
| 302 |
|
|
|
|
| 303 |
just_ran = run and checkpoint and has_image
|
| 304 |
cached = st.session_state["prediction_result"]
|
| 305 |
key_img = (uploaded.name if uploaded else None) if img_source == "Upload" else selected_sample
|
| 306 |
current_key = (model_type, checkpoint, key_img)
|
| 307 |
has_cached = cached is not None and cached.get("cache_key") == current_key
|
| 308 |
|
| 309 |
+
|
| 310 |
+
def get_or_create_predictor(model_type, checkpoint, ckp_folder):
|
| 311 |
+
"""Cache predictor in session state. Invalidate when model/checkpoint changes."""
|
| 312 |
+
cache_key = (model_type, checkpoint)
|
| 313 |
+
if "predictor" not in st.session_state or st.session_state.get("predictor_key") != cache_key:
|
| 314 |
+
from predictor import S2FPredictor
|
| 315 |
+
st.session_state["predictor"] = S2FPredictor(
|
| 316 |
+
model_type=model_type,
|
| 317 |
+
checkpoint_path=checkpoint,
|
| 318 |
+
ckp_folder=ckp_folder,
|
| 319 |
+
)
|
| 320 |
+
st.session_state["predictor_key"] = cache_key
|
| 321 |
+
return st.session_state["predictor"]
|
| 322 |
+
|
| 323 |
+
|
| 324 |
if just_ran:
|
| 325 |
+
st.session_state["prediction_result"] = None
|
| 326 |
with st.spinner("Loading model and predicting..."):
|
| 327 |
try:
|
| 328 |
+
predictor = get_or_create_predictor(model_type, checkpoint, ckp_folder)
|
| 329 |
+
sub_val = substrate_val if model_type == "single_cell" and not use_manual else "Fibroblasts_Fibronectin_6KPa"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
heatmap, force, pixel_sum = predictor.predict(
|
| 331 |
image_array=img,
|
| 332 |
substrate=sub_val,
|
|
|
|
| 335 |
|
| 336 |
st.success("Prediction complete!")
|
| 337 |
|
| 338 |
+
display_heatmap = apply_display_scale(heatmap, display_mode)
|
| 339 |
|
| 340 |
cache_key = (model_type, checkpoint, key_img)
|
| 341 |
st.session_state["prediction_result"] = {
|
|
|
|
| 345 |
"pixel_sum": pixel_sum,
|
| 346 |
"cache_key": cache_key,
|
| 347 |
}
|
| 348 |
+
st.session_state["measure_raw_heatmap"] = heatmap.copy()
|
| 349 |
+
st.session_state["measure_display_mode"] = display_mode
|
| 350 |
st.session_state["measure_bf_img"] = img.copy()
|
| 351 |
st.session_state["measure_input_filename"] = key_img or "image"
|
| 352 |
+
st.session_state["measure_original_vals"] = build_original_vals(heatmap, pixel_sum, force)
|
| 353 |
st.session_state["measure_colormap"] = colormap_name
|
| 354 |
+
cell_mask = estimate_cell_mask(heatmap)
|
| 355 |
+
st.session_state["measure_auto_cell_on"] = auto_cell_boundary
|
| 356 |
+
st.session_state["measure_cell_vals"] = build_cell_vals(heatmap, cell_mask, pixel_sum, force) if auto_cell_boundary else None
|
| 357 |
+
st.session_state["measure_cell_mask"] = cell_mask if auto_cell_boundary else None
|
| 358 |
+
|
| 359 |
+
render_result_display(
|
| 360 |
+
img, heatmap, display_heatmap, pixel_sum, force, key_img,
|
| 361 |
+
colormap_name=colormap_name,
|
| 362 |
+
display_mode=display_mode,
|
| 363 |
+
measure_region_dialog=_get_measure_dialog_fn(),
|
| 364 |
+
auto_cell_boundary=auto_cell_boundary,
|
| 365 |
+
)
|
| 366 |
|
| 367 |
except Exception as e:
|
| 368 |
st.error(f"Prediction failed: {e}")
|
|
|
|
| 371 |
elif has_cached:
|
| 372 |
r = st.session_state["prediction_result"]
|
| 373 |
img, heatmap, force, pixel_sum = r["img"], r["heatmap"], r["force"], r["pixel_sum"]
|
| 374 |
+
display_heatmap = apply_display_scale(heatmap, display_mode)
|
| 375 |
|
| 376 |
+
st.session_state["measure_raw_heatmap"] = heatmap.copy()
|
| 377 |
+
st.session_state["measure_display_mode"] = display_mode
|
| 378 |
st.session_state["measure_bf_img"] = img.copy()
|
| 379 |
st.session_state["measure_input_filename"] = key_img or "image"
|
| 380 |
+
st.session_state["measure_original_vals"] = build_original_vals(heatmap, pixel_sum, force)
|
| 381 |
st.session_state["measure_colormap"] = colormap_name
|
| 382 |
+
cell_mask = estimate_cell_mask(heatmap)
|
| 383 |
+
st.session_state["measure_auto_cell_on"] = auto_cell_boundary
|
| 384 |
+
st.session_state["measure_cell_vals"] = build_cell_vals(heatmap, cell_mask, pixel_sum, force) if auto_cell_boundary else None
|
| 385 |
+
st.session_state["measure_cell_mask"] = cell_mask if auto_cell_boundary else None
|
| 386 |
|
| 387 |
if st.session_state.pop("open_measure_dialog", False):
|
| 388 |
measure_region_dialog()
|
| 389 |
|
| 390 |
st.success("Prediction complete!")
|
| 391 |
+
render_result_display(
|
| 392 |
+
img, heatmap, display_heatmap, pixel_sum, force, key_img,
|
| 393 |
+
download_key_suffix="_cached",
|
| 394 |
+
colormap_name=colormap_name,
|
| 395 |
+
display_mode=display_mode,
|
| 396 |
+
measure_region_dialog=_get_measure_dialog_fn(),
|
| 397 |
+
auto_cell_boundary=auto_cell_boundary,
|
| 398 |
+
)
|
| 399 |
|
| 400 |
elif run and not checkpoint:
|
| 401 |
st.warning("Please add checkpoint files to the ckp/ folder and select one.")
|
|
|
|
| 403 |
st.warning("Please upload an image or select an example.")
|
| 404 |
|
| 405 |
st.sidebar.divider()
|
|
|
|
| 406 |
st.sidebar.caption("If you find this software useful, please cite:")
|
| 407 |
st.sidebar.caption(CITATION)
|