added force scale
Browse filesFiles changed (2)
- S2FApp/app.py+30-14
- models/s2f_model.py-2
110 unmodified lines11111211311411511611711811912012112212312412512612712879 unmodified lines2082092102112122132142152162 unmodified lines2192202212072222232242255 unmodified lines23123223321923423523622223723822423924022624124222824324424524612 unmodified lines25926026124724826226326426526622 unmodified lines2892902912922932942952 unmodified lines2982993002853013023033043 unmodified lines3083093102953113122973133142993153163013173183193209 unmodified lines330331332317333334335336110 unmodified lines except FileNotFoundError: st.error("config/substrate_settings.json not found")
st.divider() st.header("Display options") force_scale = st.slider( "Force scale", min_value=0.0, max_value=1.0, value=1.0, step=0.01, format="%.2f", help="Scale the displayed force values. 1 = full intensity, 0.5 = half the pixel values.", )
# Main area: image inputimg_source = st.radio("Image source", ["Upload", "Example"], horizontal=True, label_visibility="collapsed")img = None79 unmodified lines
st.success("Prediction complete!")
# Apply force scale to displayed heatmap scaled_heatmap = heatmap * force_scale
# Visualization - Plotly with zoom/pan, annotated (titles in Streamlit to avoid clipping) tit1, tit2 = st.columns(2) with tit1:2 unmodified lines st.markdown('<p style="font-size: 1.1rem; color: black; font-weight: 600;">Output: Predicted traction force map</p>', unsafe_allow_html=True) fig_pl = make_subplots(rows=1, cols=2) fig_pl.add_trace(go.Heatmap(z=img, colorscale="gray", showscale=False), row=1, col=1) fig_pl.add_trace(go.Heatmap(z=heatmap, colorscale="Jet", zmin=0, zmax=1, showscale=True, fig_pl.add_trace(go.Heatmap(z=scaled_heatmap, colorscale="Jet", zmin=0, zmax=1, showscale=True, colorbar=dict(len=0.4, thickness=12)), row=1, col=2) fig_pl.update_layout( height=400,5 unmodified lines fig_pl.update_yaxes(showticklabels=False, autorange="reversed") st.plotly_chart(fig_pl, use_container_width=True)
# Metrics with help (below plot) # Metrics with help (below plot) - use scaled values col1, col2, col3, col4 = st.columns(4) with col1: st.metric("Sum of all pixels", f"{pixel_sum:.2f}", help="Raw sum of all pixel values in the force map") st.metric("Sum of all pixels", f"{pixel_sum * force_scale:.2f}", help="Raw sum of all pixel values in the force map") with col2: st.metric("Cell force (scaled)", f"{force:.2f}", help="Total traction force in physical units") st.metric("Cell force (scaled)", f"{force * force_scale:.2f}", help="Total traction force in physical units") with col3: st.metric("Heatmap max", f"{np.max(heatmap):.4f}", help="Peak force intensity in the map") st.metric("Heatmap max", f"{np.max(scaled_heatmap):.4f}", help="Peak force intensity in the map") with col4: st.metric("Heatmap mean", f"{np.mean(heatmap):.4f}", help="Average force intensity") st.metric("Heatmap mean", f"{np.mean(scaled_heatmap):.4f}", help="Average force intensity")
# How to read (below numbers) with st.expander("ℹ️ How to read the results"):12 unmodified lines- **Heatmap max/mean:** Peak and average force intensity in the map """)
# Download heatmap_uint8 = (np.clip(heatmap, 0, 1) * 255).astype(np.uint8) # Download (scaled heatmap) heatmap_uint8 = (np.clip(scaled_heatmap, 0, 1) * 255).astype(np.uint8) heatmap_rgb = cv2.applyColorMap(heatmap_uint8, cv2.COLORMAP_JET) heatmap_rgb = cv2.cvtColor(heatmap_rgb, cv2.COLOR_BGR2RGB) pil_heatmap = Image.fromarray(heatmap_rgb)22 unmodified lines # Show cached results (e.g. after clicking Download) r = st.session_state["prediction_result"] img, heatmap, force, pixel_sum = r["img"], r["heatmap"], r["force"], r["pixel_sum"] scaled_heatmap = heatmap * force_scale st.success("Prediction complete!") tit1, tit2 = st.columns(2) with tit1:2 unmodified lines st.markdown('<p style="font-size: 1.1rem; color: black; font-weight: 600;">Output: Predicted traction force map</p>', unsafe_allow_html=True) fig_pl = make_subplots(rows=1, cols=2) fig_pl.add_trace(go.Heatmap(z=img, colorscale="gray", showscale=False), row=1, col=1) fig_pl.add_trace(go.Heatmap(z=heatmap, colorscale="Jet", zmin=0, zmax=1, showscale=True, fig_pl.add_trace(go.Heatmap(z=scaled_heatmap, colorscale="Jet", zmin=0, zmax=1, showscale=True, colorbar=dict(len=0.4, thickness=12)), row=1, col=2) fig_pl.update_layout(height=400, margin=dict(l=10, r=10, t=10, b=10), xaxis=dict(scaleanchor="y", scaleratio=1),3 unmodified lines st.plotly_chart(fig_pl, use_container_width=True) col1, col2, col3, col4 = st.columns(4) with col1: st.metric("Sum of all pixels", f"{pixel_sum:.2f}", help="Raw sum of all pixel values in the force map") st.metric("Sum of all pixels", f"{pixel_sum * force_scale:.2f}", help="Raw sum of all pixel values in the force map") with col2: st.metric("Cell force (scaled)", f"{force:.2f}", help="Total traction force in physical units") st.metric("Cell force (scaled)", f"{force * force_scale:.2f}", help="Total traction force in physical units") with col3: st.metric("Heatmap max", f"{np.max(heatmap):.4f}", help="Peak force intensity in the map") st.metric("Heatmap max", f"{np.max(scaled_heatmap):.4f}", help="Peak force intensity in the map") with col4: st.metric("Heatmap mean", f"{np.mean(heatmap):.4f}", help="Average force intensity") st.metric("Heatmap mean", f"{np.mean(scaled_heatmap):.4f}", help="Average force intensity") with st.expander("ℹ️ How to read the results"): st.markdown("""**Input (left):** Bright-field microscopy image of a cell or spheroid on a substrate. 9 unmodified lines- **Cell force (scaled):** Total traction force in physical units (scaled by substrate stiffness)- **Heatmap max/mean:** Peak and average force intensity in the map """) heatmap_uint8 = (np.clip(heatmap, 0, 1) * 255).astype(np.uint8) heatmap_uint8 = (np.clip(scaled_heatmap, 0, 1) * 255).astype(np.uint8) heatmap_rgb = cv2.applyColorMap(heatmap_uint8, cv2.COLORMAP_JET) heatmap_rgb = cv2.cvtColor(heatmap_rgb, cv2.COLOR_BGR2RGB) pil_heatmap = Image.fromarray(heatmap_rgb)173 unmodified lines17417517617717717817957 unmodified lines237238239241240241242173 unmodified lines psi = F.interpolate(psi, size=x.shape[2:], mode='bilinear', align_corners=False) return x * psi
class SpheroidAttentionGate(nn.Module): """Attention Gate from ForceNet2WithAttention (s2f_spheroid). Checkpoint-compatible for ckp_spheroid_FN.pth.""" def __init__(self, F_g, F_l, F_int):57 unmodified lines x = x * self.attention(x) return self.output_conv(x)
class S2FGenerator(nn.Module): """ S2F (Shape2Force) model: U-Net generator for force map prediction.S2FApp/app.py+30-14
models/s2f_model.py-2