added different scales
Browse filesFiles changed (2)
- S2FApp/app.py+65-14
- S2FApp/utils/display.py+42-9
48 unmodified lines495051525354555616 unmodified lines737475767778798081828384131 unmodified lines21621721821922022122222322422522622722822923023123223323423523623723823987 unmodified lines3273283293303313323337 unmodified lines34134234334434534611 unmodified lines35835936036136236336436536636736848 unmodified linesif raw_heatmap is None:st.warning("No prediction available to measure.")returndisplay_mode = st.session_state.get("measure_display_mode", "Auto")display_heatmap = apply_display_scale(raw_heatmap, display_mode)bf_img = st.session_state.get("measure_bf_img")original_vals = st.session_state.get("measure_original_vals")cell_vals = st.session_state.get("measure_cell_vals")16 unmodified linesdef _populate_measure_session_state(heatmap, img, pixel_sum, force, key_img, colormap_name,display_mode, auto_cell_boundary, cell_mask=None):"""Populate session state for the measure tool. If cell_mask is None and auto_cell_boundary, computes it."""if cell_mask is None and auto_cell_boundary:cell_mask = estimate_cell_mask(heatmap)st.session_state["measure_raw_heatmap"] = heatmap.copy()st.session_state["measure_display_mode"] = display_modest.session_state["measure_bf_img"] = img.copy()st.session_state["measure_input_filename"] = key_img or "image"st.session_state["measure_original_vals"] = build_original_vals(heatmap, pixel_sum, force)131 unmodified linesexcept FileNotFoundError:st.error("config/substrate_settings.json not found")display_mode = st.radio("Force scale",["Auto", "Fixed"],help="Auto: map data range to full color scale (Fiji-style). Fixed: use 0-1 range. Metrics always show raw values.",horizontal=True,)colormap_name = st.selectbox("Heatmap colormap",list(COLORMAPS.keys()),help="Color scheme for the force map. Viridis is often preferred for accessibility.",)auto_cell_boundary = st.checkbox("Auto boundary",value=False,help="When on: estimate cell region from force map and use it for metrics (red contour). When off: use entire map.",)theme = st.radio("Theme", ["Light", "Dark"], horizontal=True, key="theme_selector")_inject_theme_css(theme)87 unmodified linesst.success("Prediction complete!")display_heatmap = apply_display_scale(heatmap, display_mode)cache_key = (model_type, checkpoint, key_img)st.session_state["prediction_result"] = {7 unmodified lines_populate_measure_session_state(heatmap, img, pixel_sum, force, key_img, colormap_name,display_mode, auto_cell_boundary, cell_mask=cell_mask,)render_result_display(img, heatmap, display_heatmap, pixel_sum, force, key_img,11 unmodified lineselif has_cached:r = st.session_state["prediction_result"]img, heatmap, force, pixel_sum = r["img"], r["heatmap"], r["force"], r["pixel_sum"]display_heatmap = apply_display_scale(heatmap, display_mode)cell_mask = estimate_cell_mask(heatmap) if auto_cell_boundary else None_populate_measure_session_state(heatmap, img, pixel_sum, force, key_img, colormap_name,display_mode, auto_cell_boundary, cell_mask=cell_mask,)if st.session_state.pop("open_measure_dialog", False):48 unmodified lines495051525354555657585960616216 unmodified lines7980818283848586878889909192939495131 unmodified lines22722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327487 unmodified lines3623633643653663673683693703713723733747 unmodified lines38238338438538638738838911 unmodified lines40140240340440540640740840941041141241341441541641741841948 unmodified linesif raw_heatmap is None:st.warning("No prediction available to measure.")returndisplay_mode = st.session_state.get("measure_display_mode", "Full")display_heatmap = apply_display_scale(raw_heatmap, display_mode,min_percentile=st.session_state.get("measure_min_percentile", 0),max_percentile=st.session_state.get("measure_max_percentile", 100),clip_min=st.session_state.get("measure_clip_min", 0),clip_max=st.session_state.get("measure_clip_max", 1),)bf_img = st.session_state.get("measure_bf_img")original_vals = st.session_state.get("measure_original_vals")cell_vals = st.session_state.get("measure_cell_vals")16 unmodified linesdef _populate_measure_session_state(heatmap, img, pixel_sum, force, key_img, colormap_name,display_mode, auto_cell_boundary, cell_mask=None,min_percentile=0, max_percentile=100, clip_min=0, clip_max=1):"""Populate session state for the measure tool. If cell_mask is None and auto_cell_boundary, computes it."""if cell_mask is None and auto_cell_boundary:cell_mask = estimate_cell_mask(heatmap)st.session_state["measure_raw_heatmap"] = heatmap.copy()st.session_state["measure_display_mode"] = display_modest.session_state["measure_min_percentile"] = min_percentilest.session_state["measure_max_percentile"] = max_percentilest.session_state["measure_clip_min"] = clip_minst.session_state["measure_clip_max"] = clip_maxst.session_state["measure_bf_img"] = img.copy()st.session_state["measure_input_filename"] = key_img or "image"st.session_state["measure_original_vals"] = build_original_vals(heatmap, pixel_sum, force)131 unmodified linesexcept FileNotFoundError:st.error("config/substrate_settings.json not found")auto_cell_boundary = st.toggle("Auto boundary",value=False,help="When on: estimate cell region from force map and use it for metrics (red contour). When off: use entire map.",)st.markdown('<p style="font-size: 0.95rem; font-weight: 500; margin-bottom: 0.5rem;">Heatmap display</p>', unsafe_allow_html=True)display_mode = st.radio("Mode",["Full", "Percentile", "Rescale", "Clip", "Filter"],help="Full: 0–1 as-is. Percentile: min/max percentiles. Rescale: stretch range to colors. Clip: clip, keep scale. Filter: show only in range.",horizontal=True,label_visibility="collapsed",)min_percentile, max_percentile = 0, 100clip_min, clip_max = 0.0, 1.0if display_mode == "Percentile":col_pmin, col_pmax = st.columns(2)with col_pmin:min_percentile = st.slider("Min percentile", 0, 100, 2, 1, help="Values below this percentile → black")with col_pmax:max_percentile = st.slider("Max percentile", 0, 100, 99, 1, help="Values above this percentile → white")if min_percentile >= max_percentile:st.warning("Min percentile must be less than max. Using min=0, max=100.")min_percentile, max_percentile = 0, 100elif display_mode in ("Rescale", "Clip", "Filter"):col_cmin, col_cmax = st.columns(2)with col_cmin:clip_min = st.number_input("Min", value=0.0, min_value=None, max_value=None, step=0.01, format="%.3f",help="Rescale: below → black. Clip: clamp to min. Filter: below → discarded.")with col_cmax:clip_max = st.number_input("Max", value=1.0, min_value=None, max_value=None, step=0.01, format="%.3f",help="Rescale: above → white. Clip: clamp to max. Filter: above → discarded.")if clip_min >= clip_max:st.warning("Min must be less than max. Using min=0, max=1.")clip_min, clip_max = 0.0, 1.0colormap_name = st.selectbox("Heatmap colormap",list(COLORMAPS.keys()),help="Color scheme for the force map. Viridis is often preferred for accessibility.",)theme = st.radio("Theme", ["Light", "Dark"], horizontal=True, key="theme_selector")_inject_theme_css(theme)87 unmodified linesst.success("Prediction complete!")display_heatmap = apply_display_scale(heatmap, display_mode,min_percentile=min_percentile,max_percentile=max_percentile,clip_min=clip_min,clip_max=clip_max,)cache_key = (model_type, checkpoint, key_img)st.session_state["prediction_result"] = {7 unmodified lines_populate_measure_session_state(heatmap, img, pixel_sum, force, key_img, colormap_name,display_mode, auto_cell_boundary, cell_mask=cell_mask,min_percentile=min_percentile, max_percentile=max_percentile,clip_min=clip_min, clip_max=clip_max,)render_result_display(img, heatmap, display_heatmap, pixel_sum, force, key_img,11 unmodified lineselif has_cached:r = st.session_state["prediction_result"]img, heatmap, force, pixel_sum = r["img"], r["heatmap"], r["force"], r["pixel_sum"]display_heatmap = apply_display_scale(heatmap, display_mode,min_percentile=min_percentile,max_percentile=max_percentile,clip_min=clip_min,clip_max=clip_max,)cell_mask = estimate_cell_mask(heatmap) if auto_cell_boundary else None_populate_measure_session_state(heatmap, img, pixel_sum, force, key_img, colormap_name,display_mode, auto_cell_boundary, cell_mask=cell_mask,min_percentile=min_percentile, max_percentile=max_percentile,clip_min=clip_min, clip_max=clip_max,)if st.session_state.pop("open_measure_dialog", False):
18 unmodified lines1920212223242526272829303132333418 unmodified linesreturn scaledef apply_display_scale(heatmap, mode):"""Apply display scaling (Fiji-style). Display only—does not change underlying values.- Auto: map data min..max to 0..1 (full color range)- Fixed: use 0-1 range as-is"""if mode == "Fixed":return np.clip(heatmap, 0, 1).astype(np.float32)hmin, hmax = float(np.min(heatmap)), float(np.max(heatmap))if hmax > hmin:out = (heatmap.astype(np.float32) - hmin) / (hmax - hmin)return np.clip(out, 0, 1).astype(np.float32)return np.clip(heatmap, 0, 1).astype(np.float32)18 unmodified lines1920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666718 unmodified linesreturn scaledef apply_display_scale(heatmap, mode, min_percentile=0, max_percentile=100, clip_min=0, clip_max=1):"""Apply display scaling (Fiji ImageJ B&C style). Display only—does not change underlying values.- Full: use 0–1 range as-is (clip values outside)- Percentile: map data at min_percentile..max_percentile to 0..1 (values outside clipped).- Rescale: map [clip_min, clip_max] to [0, 1]; values outside → black/white. Stretches range for contrast.- Clip: clip values to [clip_min, clip_max], display with original data scale (no stretching).- Filter: discard values outside [clip_min, clip_max] (black); only values in range show color."""if mode == "Full":return np.clip(heatmap, 0, 1).astype(np.float32)if mode == "Percentile":pmin = float(np.percentile(heatmap, min_percentile))pmax = float(np.percentile(heatmap, max_percentile))if pmax > pmin:out = (heatmap.astype(np.float32) - pmin) / (pmax - pmin)return np.clip(out, 0, 1).astype(np.float32)return np.clip(heatmap, 0, 1).astype(np.float32)if mode == "Rescale":vmin, vmax = float(clip_min), float(clip_max)if vmax > vmin:out = (heatmap.astype(np.float32) - vmin) / (vmax - vmin)return np.clip(out, 0, 1).astype(np.float32)return np.clip(heatmap, 0, 1).astype(np.float32)if mode == "Clip":vmin, vmax = float(clip_min), float(clip_max)if vmax > vmin:h = np.clip(heatmap.astype(np.float32), vmin, vmax)dmin, dmax = float(np.min(heatmap)), float(np.max(heatmap))if dmax > dmin:out = (h - dmin) / (dmax - dmin)return np.clip(out, 0, 1).astype(np.float32)return np.clip(h, 0, 1).astype(np.float32)return np.clip(heatmap, 0, 1).astype(np.float32)if mode == "Threshold":mode = "Filter" # backward compatif mode == "Filter":vmin, vmax = float(clip_min), float(clip_max)if vmax > vmin:h = heatmap.astype(np.float32)mask = (h >= vmin) & (h <= vmax)out = np.zeros_like(h)out[mask] = (h[mask] - vmin) / (vmax - vmin)return np.clip(out, 0, 1).astype(np.float32)return np.clip(heatmap, 0, 1).astype(np.float32)return np.clip(heatmap, 0, 1).astype(np.float32)
S2FApp/app.py+65-14
S2FApp/utils/display.py+42-9