diff --git "a/src/streamlit_app.py" "b/src/streamlit_app.py" --- "a/src/streamlit_app.py" +++ "b/src/streamlit_app.py" @@ -1,40 +1,40 @@ -# src/streamlit_app.py -""" -MyoSeg — Myotube & Nuclei Analyzer -======================================== -Drop-in replacement for streamlit_app.py on Hugging Face Spaces. - -Features: - ✦ Animated count-up metrics (9 counters) - ✦ Instance overlay — nucleus IDs (1,2,3…) + myotube IDs (M1,M2…) - ✦ Separate nuclei outline + myotube outline tabs - ✦ Watershed nuclei splitting for accurate counts - ✦ Myotube surface area (total, mean, max µm²) + per-tube bar chart - ✦ Active learning — upload corrected masks → saved to corrections/ - ✦ Low-confidence auto-flagging → image queued for retraining - ✦ Retraining queue status panel - ✦ All original sidebar controls preserved - - v9 changes: - ✦ FIXED: SVG viewer myotube ID count now matches live metrics count. - Root cause: viewer showed all connected components (myo_lab), but - metrics only counted those with ≥1 MyHC+ nucleus. Now the viewer - badge shows the biological myotube_count from compute_bio_metrics, - and non-bio myotube regions are shown as faint outlines (not labelled). - ✦ Outlines split into two separate tabs: "Nuclei outlines" and - "Myotube outlines" per collaborator request. - ✦ Privacy mode: sidebar toggle for "Private mode — do not use my data - for training". When enabled, images are NOT queued for retraining - (no low_confidence queue, no corrections submission). - ✦ Training contribution mode: explicit user-initiated action to submit - current image + tuned parameters as a training contribution. Only - runs when user clicks "Submit for training" after finding good params. - ✦ Parameter learning: when user submits, the current sidebar parameter - set (thresholds, postprocessing knobs) is saved alongside the image - so self_train.py can learn optimal parameters per image type. - ✦ All v8 fixes preserved (no closing, shape filter, erode+dilate, etc). -""" - +# src/streamlit_app.py +""" +MyoSeg — Myotube & Nuclei Analyzer +======================================== +Drop-in replacement for streamlit_app.py on Hugging Face Spaces. + +Features: + ✦ Animated count-up metrics (9 counters) + ✦ Instance overlay — nucleus IDs (1,2,3…) + myotube IDs (M1,M2…) + ✦ Separate nuclei outline + myotube outline tabs + ✦ Watershed nuclei splitting for accurate counts + ✦ Myotube surface area (total, mean, max µm²) + per-tube bar chart + ✦ Active learning — upload corrected masks → saved to corrections/ + ✦ Low-confidence auto-flagging → image queued for retraining + ✦ Retraining queue status panel + ✦ All original sidebar controls preserved + + v9 changes: + ✦ FIXED: SVG viewer myotube ID count now matches live metrics count. + Root cause: viewer showed all connected components (myo_lab), but + metrics only counted those with ≥1 MyHC+ nucleus. Now the viewer + badge shows the biological myotube_count from compute_bio_metrics, + and non-bio myotube regions are shown as faint outlines (not labelled). + ✦ Outlines split into two separate tabs: "Nuclei outlines" and + "Myotube outlines" per collaborator request. + ✦ Privacy mode: sidebar toggle for "Private mode — do not use my data + for training". When enabled, images are NOT queued for retraining + (no low_confidence queue, no corrections submission). + ✦ Training contribution mode: explicit user-initiated action to submit + current image + tuned parameters as a training contribution. Only + runs when user clicks "Submit for training" after finding good params. + ✦ Parameter learning: when user submits, the current sidebar parameter + set (thresholds, postprocessing knobs) is saved alongside the image + so self_train.py can learn optimal parameters per image type. + ✦ All v8 fixes preserved (no closing, shape filter, erode+dilate, etc). +""" + import io import os import json @@ -44,25 +44,25 @@ import hashlib import re from datetime import datetime from pathlib import Path - -import numpy as np -import pandas as pd -from PIL import Image - -import streamlit as st -import streamlit.components.v1 -import torch -import torch.nn as nn -import matplotlib -matplotlib.use("Agg") -import matplotlib.pyplot as plt -import matplotlib.patches as mpatches -from huggingface_hub import hf_hub_download - -import scipy.ndimage as ndi + +import numpy as np +import pandas as pd +from PIL import Image + +import streamlit as st +import streamlit.components.v1 +import torch +import torch.nn as nn +import matplotlib +matplotlib.use("Agg") +import matplotlib.pyplot as plt +import matplotlib.patches as mpatches +from huggingface_hub import hf_hub_download + +import scipy.ndimage as ndi from skimage.morphology import remove_small_objects, disk -from skimage import measure -from skimage.segmentation import watershed, find_boundaries +from skimage import measure +from skimage.segmentation import watershed, find_boundaries from skimage.feature import peak_local_max @@ -96,1015 +96,1026 @@ def _safe_binary_closing(mask, footprint): _as_bool_c_array(mask), structure=np.asarray(footprint, dtype=bool), ) - - -# ───────────────────────────────────────────────────────────────────────────── -# CONFIG ← edit these two lines to match your HF model repo -# ───────────────────────────────────────────────────────────────────────────── + + +# ───────────────────────────────────────────────────────────────────────────── +# CONFIG ← edit these two lines to match your HF model repo +# ───────────────────────────────────────────────────────────────────────────── MODEL_REPO_ID = "skarugu/myotube-unet" MODEL_FILENAME = "model_final.pt" APP_VERSION = "handover-2026-08-10" - -CONF_FLAG_THR = 0.60 # images below this confidence are queued for retraining -QUEUE_DIR = Path("retrain_queue") -CORRECTIONS_DIR = Path("corrections") - - -# ───────────────────────────────────────────────────────────────────────────── -# Helpers (identical to originals so nothing breaks) -# ───────────────────────────────────────────────────────────────────────────── - -def sha256_file(path: str) -> str: - h = hashlib.sha256() - with open(path, "rb") as f: - for chunk in iter(lambda: f.read(1024 * 1024), b""): - h.update(chunk) - return h.hexdigest() - - -def png_bytes(arr_u8: np.ndarray) -> bytes: - buf = io.BytesIO() - Image.fromarray(arr_u8).save(buf, format="PNG") - return buf.getvalue() - - -def resize_u8_to_float01(ch_u8: np.ndarray, W: int, H: int, - resample=Image.BILINEAR) -> np.ndarray: - im = Image.fromarray(ch_u8, mode="L").resize((W, H), resample=resample) - return np.array(im, dtype=np.float32) / 255.0 - - -def get_channel(rgb_u8: np.ndarray, source: str) -> np.ndarray: - if source == "Red": return rgb_u8[..., 0] - if source == "Green": return rgb_u8[..., 1] - if source == "Blue": return rgb_u8[..., 2] - return (0.299*rgb_u8[...,0] + 0.587*rgb_u8[...,1] + 0.114*rgb_u8[...,2]).astype(np.uint8) - - -def hex_to_rgb(h: str): - h = h.lstrip("#") - return tuple(int(h[i:i+2], 16) for i in (0, 2, 4)) - - -# ───────────────────────────────────────────────────────────────────────────── -# Postprocessing -# ───────────────────────────────────────────────────────────────────────────── - -def postprocess_masks(nuc_mask, myo_mask, - min_nuc_area=20, min_myo_area=500, - nuc_close_radius=2, - myo_open_radius=2, - myo_erode_radius=2, - min_myo_aspect_ratio=0.0): - """ - Clean up raw predicted masks. - - v8 — unified postprocessing (matches training script): - Nuclei: optional closing to fill gaps, then remove small objects. - Myotubes: opening (noise removal) → erode+dilate (bridge breaking) → - size filter → optional aspect-ratio shape filter. - - NO closing for myotubes — closing merges adjacent myotubes into single - connected components, causing severe undercounting in dense cultures. - Validation showed r=0.245 with closing vs manual counts. - - myo_open_radius — disk radius for morphological opening. Removes small - noise/debris without merging separate objects. - myo_erode_radius — disk radius for erode+dilate bridge-breaking. Separates - touching myotubes that share thin pixel bridges. - Start at 2 px; increase for dense cultures. Set 0 to disable. - min_myo_aspect_ratio — minimum major/minor axis ratio. Myotubes are elongated - (aspect > 2); round debris blobs (aspect ~1) are rejected. - Set 0 to disable. Recommended: 1.5–2.0 for sparse cultures. - """ - # Nuclei — closing fills small gaps, then size filter - nuc_bin = nuc_mask.astype(bool) - if int(nuc_close_radius) > 0: + +CONF_FLAG_THR = 0.60 # images below this confidence are queued for retraining +QUEUE_DIR = Path("retrain_queue") +CORRECTIONS_DIR = Path("corrections") + + +# ───────────────────────────────────────────────────────────────────────────── +# Helpers (identical to originals so nothing breaks) +# ───────────────────────────────────────────────────────────────────────────── + +def sha256_file(path: str) -> str: + h = hashlib.sha256() + with open(path, "rb") as f: + for chunk in iter(lambda: f.read(1024 * 1024), b""): + h.update(chunk) + return h.hexdigest() + + +def png_bytes(arr_u8: np.ndarray) -> bytes: + buf = io.BytesIO() + Image.fromarray(arr_u8).save(buf, format="PNG") + return buf.getvalue() + + +def resize_u8_to_float01(ch_u8: np.ndarray, W: int, H: int, + resample=Image.BILINEAR) -> np.ndarray: + im = Image.fromarray(ch_u8, mode="L").resize((W, H), resample=resample) + return np.array(im, dtype=np.float32) / 255.0 + + +def get_channel(rgb_u8: np.ndarray, source: str) -> np.ndarray: + if source == "Red": return rgb_u8[..., 0] + if source == "Green": return rgb_u8[..., 1] + if source == "Blue": return rgb_u8[..., 2] + return (0.299*rgb_u8[...,0] + 0.587*rgb_u8[...,1] + 0.114*rgb_u8[...,2]).astype(np.uint8) + + +def hex_to_rgb(h: str): + h = h.lstrip("#") + return tuple(int(h[i:i+2], 16) for i in (0, 2, 4)) + + +# ───────────────────────────────────────────────────────────────────────────── +# Postprocessing +# ───────────────────────────────────────────────────────────────────────────── + +def postprocess_masks(nuc_mask, myo_mask, + min_nuc_area=20, min_myo_area=500, + nuc_close_radius=2, + myo_open_radius=2, + myo_erode_radius=2, + min_myo_aspect_ratio=0.0): + """ + Clean up raw predicted masks. + + v8 — unified postprocessing (matches training script): + Nuclei: optional closing to fill gaps, then remove small objects. + Myotubes: opening (noise removal) → erode+dilate (bridge breaking) → + size filter → optional aspect-ratio shape filter. + + NO closing for myotubes — closing merges adjacent myotubes into single + connected components, causing severe undercounting in dense cultures. + Validation showed r=0.245 with closing vs manual counts. + + myo_open_radius — disk radius for morphological opening. Removes small + noise/debris without merging separate objects. + myo_erode_radius — disk radius for erode+dilate bridge-breaking. Separates + touching myotubes that share thin pixel bridges. + Start at 2 px; increase for dense cultures. Set 0 to disable. + min_myo_aspect_ratio — minimum major/minor axis ratio. Myotubes are elongated + (aspect > 2); round debris blobs (aspect ~1) are rejected. + Set 0 to disable. Recommended: 1.5–2.0 for sparse cultures. + """ + # Nuclei — closing fills small gaps, then size filter + nuc_bin = nuc_mask.astype(bool) + if int(nuc_close_radius) > 0: nuc_bin = _safe_binary_closing(nuc_bin, disk(int(nuc_close_radius))) - nuc_clean = remove_small_objects(nuc_bin, min_size=int(min_nuc_area)).astype(np.uint8) - - # Myotubes — opening + erode/dilate + size filter + shape filter - myo_bin = myo_mask.astype(bool) - if int(myo_open_radius) > 0: + nuc_clean = remove_small_objects(nuc_bin, min_size=int(min_nuc_area)).astype(np.uint8) + + # Myotubes — opening + erode/dilate + size filter + shape filter + myo_bin = myo_mask.astype(bool) + if int(myo_open_radius) > 0: myo_bin = _safe_binary_opening(myo_bin, disk(int(myo_open_radius))) - if int(myo_erode_radius) > 0: - se = disk(int(myo_erode_radius)) + if int(myo_erode_radius) > 0: + se = disk(int(myo_erode_radius)) myo_bin = _safe_binary_erosion(myo_bin, se) myo_bin = _safe_binary_dilation(myo_bin, se) # re-dilate to restore size - myo_bin = remove_small_objects(myo_bin, min_size=int(min_myo_area)) - if float(min_myo_aspect_ratio) > 0: - myo_bin = _filter_by_aspect_ratio(myo_bin, float(min_myo_aspect_ratio)) - myo_clean = myo_bin.astype(np.uint8) - - return nuc_clean, myo_clean - - -def _filter_by_aspect_ratio(mask_bin: np.ndarray, min_aspect: float) -> np.ndarray: - """Keep only regions with major/minor axis ratio >= min_aspect.""" - lab, _ = ndi.label(mask_bin.astype(np.uint8)) - keep = np.zeros_like(mask_bin, dtype=bool) - for prop in measure.regionprops(lab): - if prop.minor_axis_length > 0: - aspect = prop.major_axis_length / prop.minor_axis_length - if aspect >= min_aspect: - keep[lab == prop.label] = True - else: - # Degenerate (line-like) — keep it (very elongated) - keep[lab == prop.label] = True - return keep - - -def label_cc(mask: np.ndarray) -> np.ndarray: - lab, _ = ndi.label(mask.astype(np.uint8)) - return lab - - -def split_large_myotubes(myo_lab: np.ndarray, - nuc_lab: np.ndarray, - max_area_px: int = 0, - min_seeds: int = 2) -> np.ndarray: - """ - Fix 2 + 3: Split oversized myotube regions using nucleus-seeded watershed. - - Addresses the core myotube merging problem: when adjacent or branching - myotubes form a single connected region, this function splits them using - nuclei centroids as seeds — the same principle as nucleus watershed splitting, - applied at the myotube level. - - Algorithm - --------- - For each myotube region larger than max_area_px: - 1. Find all nucleus centroids inside the region - 2. If ≥ min_seeds nuclei found, run distance-transform watershed on - the myotube mask using nucleus centroids as seeds - 3. Replace the merged region with the resulting split sub-regions - 4. Remove any resulting fragment smaller than min_myo_area - - Parameters - ---------- - myo_lab : 2D int array — labelled myotube instances (from label_cc) - nuc_lab : 2D int array — labelled nuclei (from label_nuclei_watershed) - max_area_px : regions larger than this (in pixels) are candidates for splitting. - Set to 0 to disable. - min_seeds : minimum nucleus seeds required to attempt a split (default 2) - - Returns - ------- - New labelled myotube array with split regions re-numbered sequentially. - """ - if max_area_px <= 0: - return myo_lab - - out = myo_lab.copy() - next_id = int(myo_lab.max()) + 1 - H, W = myo_lab.shape - - for prop in measure.regionprops(myo_lab): - if prop.area <= max_area_px: - continue - - # Build binary mask for this single myotube region - region_mask = (myo_lab == prop.label) - - # Find nucleus centroids inside this region - seeds_img = np.zeros((H, W), dtype=np.int32) - seed_count = 0 - for nuc_prop in measure.regionprops(nuc_lab): - r, c = int(nuc_prop.centroid[0]), int(nuc_prop.centroid[1]) - if 0 <= r < H and 0 <= c < W and region_mask[r, c]: - seeds_img[r, c] = nuc_prop.label - seed_count += 1 - - if seed_count < min_seeds: - # Not enough nuclei to split — leave as is - continue - - # Distance-transform watershed using nucleus seeds - dist = ndi.distance_transform_edt(region_mask) - result = watershed(-dist, seeds_img, mask=region_mask) - - # Clear the original region and write split sub-regions - out[region_mask] = 0 - for sub_id in np.unique(result): - if sub_id == 0: - continue - sub_mask = (result == sub_id) - if sub_mask.sum() < 10: # discard tiny slivers - continue - out[sub_mask] = next_id - next_id += 1 - - # Re-number sequentially 1..N - final = np.zeros_like(out) - for new_id, old_id in enumerate(np.unique(out)[1:], start=1): - final[out == old_id] = new_id - - return final - - -def label_nuclei_watershed(nuc_bin: np.ndarray, - min_distance: int = 3, - min_nuc_area: int = 6) -> np.ndarray: - """Split touching nuclei via distance-transform watershed.""" - nuc_bin = remove_small_objects(nuc_bin.astype(bool), min_size=min_nuc_area) - if nuc_bin.sum() == 0: - return np.zeros_like(nuc_bin, dtype=np.int32) - - dist = ndi.distance_transform_edt(nuc_bin) - coords = peak_local_max(dist, labels=nuc_bin, - min_distance=min_distance, exclude_border=False) - markers = np.zeros_like(nuc_bin, dtype=np.int32) - for i, (r, c) in enumerate(coords, start=1): - markers[r, c] = i - - if markers.max() == 0: - return ndi.label(nuc_bin.astype(np.uint8))[0].astype(np.int32) - - return watershed(-dist, markers, mask=nuc_bin).astype(np.int32) - - -# ──────────────��────────────────────────────────────────────────────────────── -# Surface area (new) -# ───────────────────────────────────────────────────────────────────────────── - -def compute_surface_area(myo_mask: np.ndarray, px_um: float = 1.0) -> dict: - lab = label_cc(myo_mask) - px_area = px_um ** 2 - per = [round(prop.area * px_area, 2) for prop in measure.regionprops(lab)] - return { - "total_area_um2" : round(sum(per), 2), - "mean_area_um2" : round(float(np.mean(per)) if per else 0.0, 2), - "max_area_um2" : round(float(np.max(per)) if per else 0.0, 2), - "per_myotube_areas" : per, - } - - -# ───────────────────────────────────────────────────────────────────────────── -# Cytoplasm-hole nucleus classifier (MyoFuse method, Lair et al. 2025) -# ───────────────────────────────────────────────────────────────────────────── - -def classify_nucleus_in_myotube(nuc_coords: np.ndarray, - myc_channel: np.ndarray, - myo_mask_full: np.ndarray, - ring_width: int = 6, - hole_ratio_thr: float = 0.85) -> bool: - """ - Determine whether a nucleus is GENUINELY inside a myotube - using the cytoplasm-hole method (MyoFuse, Lair et al. 2025). - - A fused nucleus inside a myotube physically displaces the cytoplasm, - creating a local dip (dark "hole") in the MyHC signal beneath it. - An unfused nucleus sitting on top of a myotube in Z does NOT create - this dip — its underlying MyHC signal stays bright. - - Algorithm - --------- - 1. Check the nucleus pixel footprint overlaps the myotube mask at all. - If not — definitely not fused. - 2. Measure mean MyHC intensity under the nucleus pixels (I_nuc). - 3. Build a ring around the nucleus (dilated - eroded footprint) clipped - to the myotube mask — this is the local cytoplasm reference (I_ring). - 4. Compute hole_ratio = I_nuc / I_ring. - If hole_ratio < hole_ratio_thr → nucleus has created a cytoplasmic - hole → genuinely fused. - If hole_ratio ≥ hole_ratio_thr → nucleus sits on top in Z → not fused. - - Parameters - ---------- - nuc_coords : (N,2) array of (row, col) pixel coords for this nucleus - myc_channel : 2D float32 array of MyHC channel at FULL image resolution - myo_mask_full : 2D binary mask of myotubes at FULL image resolution - ring_width : dilation radius (px) for the cytoplasm ring - hole_ratio_thr: threshold below which the nucleus is counted as fused - (default 0.85, consistent with MyoFuse calibration) - - Returns - ------- - True if nucleus is genuinely fused (inside myotube cytoplasm) - """ - rows, cols = nuc_coords[:, 0], nuc_coords[:, 1] - H, W = myc_channel.shape - - # Step 1 — must overlap myotube mask at all - in_myo = myo_mask_full[rows, cols] - if in_myo.sum() == 0: - return False - - # Step 2 — mean MyHC under nucleus - I_nuc = float(myc_channel[rows, cols].mean()) - - # Step 3 — build ring around nucleus footprint, clipped to myotube mask - nuc_footprint = np.zeros((H, W), dtype=bool) - nuc_footprint[rows, cols] = True - + myo_bin = remove_small_objects(myo_bin, min_size=int(min_myo_area)) + if float(min_myo_aspect_ratio) > 0: + myo_bin = _filter_by_aspect_ratio(myo_bin, float(min_myo_aspect_ratio)) + myo_clean = myo_bin.astype(np.uint8) + + return nuc_clean, myo_clean + + +def _filter_by_aspect_ratio(mask_bin: np.ndarray, min_aspect: float) -> np.ndarray: + """Keep only regions with major/minor axis ratio >= min_aspect.""" + lab, _ = ndi.label(mask_bin.astype(np.uint8)) + keep = np.zeros_like(mask_bin, dtype=bool) + for prop in measure.regionprops(lab): + if prop.minor_axis_length > 0: + aspect = prop.major_axis_length / prop.minor_axis_length + if aspect >= min_aspect: + keep[lab == prop.label] = True + else: + # Degenerate (line-like) — keep it (very elongated) + keep[lab == prop.label] = True + return keep + + +def label_cc(mask: np.ndarray) -> np.ndarray: + lab, _ = ndi.label(mask.astype(np.uint8)) + return lab + + +def split_large_myotubes(myo_lab: np.ndarray, + nuc_lab: np.ndarray, + max_area_px: int = 0, + min_seeds: int = 2) -> np.ndarray: + """ + Fix 2 + 3: Split oversized myotube regions using nucleus-seeded watershed. + + Addresses the core myotube merging problem: when adjacent or branching + myotubes form a single connected region, this function splits them using + nuclei centroids as seeds — the same principle as nucleus watershed splitting, + applied at the myotube level. + + Algorithm + --------- + For each myotube region larger than max_area_px: + 1. Find all nucleus centroids inside the region + 2. If ≥ min_seeds nuclei found, run distance-transform watershed on + the myotube mask using nucleus centroids as seeds + 3. Replace the merged region with the resulting split sub-regions + 4. Remove any resulting fragment smaller than min_myo_area + + Parameters + ---------- + myo_lab : 2D int array — labelled myotube instances (from label_cc) + nuc_lab : 2D int array — labelled nuclei (from label_nuclei_watershed) + max_area_px : regions larger than this (in pixels) are candidates for splitting. + Set to 0 to disable. + min_seeds : minimum nucleus seeds required to attempt a split (default 2) + + Returns + ------- + New labelled myotube array with split regions re-numbered sequentially. + """ + if max_area_px <= 0: + return myo_lab + + out = myo_lab.copy() + next_id = int(myo_lab.max()) + 1 + H, W = myo_lab.shape + + for prop in measure.regionprops(myo_lab): + if prop.area <= max_area_px: + continue + + # Build binary mask for this single myotube region + region_mask = (myo_lab == prop.label) + + # Find nucleus centroids inside this region + seeds_img = np.zeros((H, W), dtype=np.int32) + seed_count = 0 + for nuc_prop in measure.regionprops(nuc_lab): + r, c = int(nuc_prop.centroid[0]), int(nuc_prop.centroid[1]) + if 0 <= r < H and 0 <= c < W and region_mask[r, c]: + seeds_img[r, c] = nuc_prop.label + seed_count += 1 + + if seed_count < min_seeds: + # Not enough nuclei to split — leave as is + continue + + # Distance-transform watershed using nucleus seeds + dist = ndi.distance_transform_edt(region_mask) + result = watershed(-dist, seeds_img, mask=region_mask) + + # Clear the original region and write split sub-regions + out[region_mask] = 0 + for sub_id in np.unique(result): + if sub_id == 0: + continue + sub_mask = (result == sub_id) + if sub_mask.sum() < 10: # discard tiny slivers + continue + out[sub_mask] = next_id + next_id += 1 + + # Re-number sequentially 1..N + final = np.zeros_like(out) + for new_id, old_id in enumerate(np.unique(out)[1:], start=1): + final[out == old_id] = new_id + + return final + + +def label_nuclei_watershed(nuc_bin: np.ndarray, + min_distance: int = 3, + min_nuc_area: int = 6) -> np.ndarray: + """Split touching nuclei via distance-transform watershed.""" + nuc_bin = remove_small_objects(nuc_bin.astype(bool), min_size=min_nuc_area) + if nuc_bin.sum() == 0: + return np.zeros_like(nuc_bin, dtype=np.int32) + + dist = ndi.distance_transform_edt(nuc_bin) + coords = peak_local_max(dist, labels=nuc_bin, + min_distance=min_distance, exclude_border=False) + markers = np.zeros_like(nuc_bin, dtype=np.int32) + for i, (r, c) in enumerate(coords, start=1): + markers[r, c] = i + + if markers.max() == 0: + return ndi.label(nuc_bin.astype(np.uint8))[0].astype(np.int32) + + return watershed(-dist, markers, mask=nuc_bin).astype(np.int32) + + +# ───────────────────────────────────────────────────────────────────────────── +# Surface area (new) +# ───────────────────────────────────────────────────────────────────────────── + +def compute_surface_area(myo_mask: np.ndarray, px_um: float = 1.0) -> dict: + lab = label_cc(myo_mask) + px_area = px_um ** 2 + per = [round(prop.area * px_area, 2) for prop in measure.regionprops(lab)] + return { + "total_area_um2" : round(sum(per), 2), + "mean_area_um2" : round(float(np.mean(per)) if per else 0.0, 2), + "max_area_um2" : round(float(np.max(per)) if per else 0.0, 2), + "per_myotube_areas" : per, + } + + +# ──────────────────────────────────────────────────────────────���────────────── +# Cytoplasm-hole nucleus classifier (MyoFuse method, Lair et al. 2026) +# ───────────────────────────────────────────────────────────────────────────── + +def classify_nucleus_in_myotube(nuc_coords: np.ndarray, + myc_channel: np.ndarray, + myo_mask_full: np.ndarray, + ring_width: int = 6, + hole_ratio_thr: float = 0.85) -> bool: + """ + Determine whether a nucleus is GENUINELY inside a myotube + using the cytoplasm-hole method (MyoFuse, Lair et al. 2026). + + A fused nucleus inside a myotube physically displaces the cytoplasm, + creating a local dip (dark "hole") in the MyHC signal beneath it. + An unfused nucleus sitting on top of a myotube in Z does NOT create + this dip — its underlying MyHC signal stays bright. + + Algorithm + --------- + 1. Check the nucleus pixel footprint overlaps the myotube mask at all. + If not — definitely not fused. + 2. Measure mean MyHC intensity under the nucleus pixels (I_nuc). + 3. Build a ring around the nucleus (dilated - eroded footprint) clipped + to the myotube mask — this is the local cytoplasm reference (I_ring). + 4. Compute hole_ratio = I_nuc / I_ring. + If hole_ratio < hole_ratio_thr → nucleus has created a cytoplasmic + hole → genuinely fused. + If hole_ratio ≥ hole_ratio_thr → nucleus sits on top in Z → not fused. + + Parameters + ---------- + nuc_coords : (N,2) array of (row, col) pixel coords for this nucleus + myc_channel : 2D float32 array of MyHC channel at FULL image resolution + myo_mask_full : 2D binary mask of myotubes at FULL image resolution + ring_width : dilation radius (px) for the cytoplasm ring + hole_ratio_thr: threshold below which the nucleus is counted as fused + (default 0.85, consistent with MyoFuse calibration) + + Returns + ------- + True if nucleus is genuinely fused (inside myotube cytoplasm) + """ + rows, cols = nuc_coords[:, 0], nuc_coords[:, 1] + H, W = myc_channel.shape + + # Step 1 — must overlap myotube mask at all + in_myo = myo_mask_full[rows, cols] + if in_myo.sum() == 0: + return False + + # Step 2 — mean MyHC under nucleus + I_nuc = float(myc_channel[rows, cols].mean()) + + # Step 3 — build ring around nucleus footprint, clipped to myotube mask + nuc_footprint = np.zeros((H, W), dtype=bool) + nuc_footprint[rows, cols] = True + nuc_dilated = _safe_binary_dilation(nuc_footprint, disk(ring_width)) - ring_mask = nuc_dilated & ~nuc_footprint & myo_mask_full.astype(bool) - - if ring_mask.sum() < 4: - # Ring too small (nucleus near edge of myotube) — fall back to overlap test - return in_myo.mean() >= 0.10 - - I_ring = float(myc_channel[ring_mask].mean()) - - if I_ring < 1e-6: - # No myotube signal at all in ring — something is wrong, use overlap - return in_myo.mean() >= 0.10 - - # Step 4 — hole ratio test - hole_ratio = I_nuc / I_ring - return hole_ratio < hole_ratio_thr - - -# ───────────────────────────────────────────────────────────────────────────── -# Biological metrics (counting + fusion + surface area) -# ───────────────────────────────────────────────────────────────────────────── - -def compute_bio_metrics(nuc_mask, myo_mask, - myc_channel_full=None, - min_overlap_frac=0.10, - nuc_ws_min_distance=3, - nuc_ws_min_area=6, - px_um=1.0, - ring_width=6, - hole_ratio_thr=0.85) -> dict: - """ - Compute all biological metrics. - - If myc_channel_full (the raw MyHC grayscale image at original resolution) - is supplied, uses the cytoplasm-hole method (MyoFuse, Lair et al. 2025) - to classify each nucleus — eliminates Z-stack overlap false positives and - gives an accurate, non-overestimated fusion index. - - If myc_channel_full is None, falls back to the original pixel-overlap - method for backward compatibility. - """ - nuc_lab = label_nuclei_watershed(nuc_mask, - min_distance=nuc_ws_min_distance, - min_nuc_area=nuc_ws_min_area) - myo_lab = label_cc(myo_mask) - total = int(nuc_lab.max()) - - # Resize masks/channel to the SAME space for comparison - # nuc_lab and myo_mask are at model resolution (e.g. 512×512). - # myc_channel_full is at original image resolution. - # We resize everything to original resolution for the cytoplasm-hole test. - if myc_channel_full is not None: - H_full, W_full = myc_channel_full.shape - # Resize label maps up to original resolution - nuc_lab_full = np.array( - Image.fromarray(nuc_lab.astype(np.int32)) - .resize((W_full, H_full), Image.NEAREST) - ) - myo_mask_full = np.array( - Image.fromarray((myo_mask * 255).astype(np.uint8)) - .resize((W_full, H_full), Image.NEAREST) - ) > 0 - # Normalise MyHC channel to 0-1 float - myc_f = myc_channel_full.astype(np.float32) - if myc_f.max() > 1.0: - myc_f = myc_f / 255.0 - else: - nuc_lab_full = nuc_lab - myo_mask_full = myo_mask.astype(bool) - myc_f = None - - pos, nm = 0, {} - for prop in measure.regionprops(nuc_lab_full): - coords = prop.coords # (N,2) in full-res space - - if myc_f is not None: - # ── Cytoplasm-hole method (accurate, MyoFuse 2025) ──────────────── - is_fused = classify_nucleus_in_myotube( - coords, myc_f, myo_mask_full, - ring_width=ring_width, - hole_ratio_thr=hole_ratio_thr, - ) - else: - # ── Legacy pixel-overlap fallback ───────────────────────────────── - ids = myo_mask_full.astype(np.uint8)[coords[:, 0], coords[:, 1]] - frac = ids.sum() / max(len(coords), 1) - is_fused = frac >= min_overlap_frac - - if is_fused: - # Find which myotube this nucleus belongs to (use model-res myo_lab) - # Scale coords back to model resolution - if myc_f is not None: - r_m = np.clip((coords[:, 0] * nuc_lab.shape[0] / H_full).astype(int), - 0, nuc_lab.shape[0] - 1) - c_m = np.clip((coords[:, 1] * nuc_lab.shape[1] / W_full).astype(int), - 0, nuc_lab.shape[1] - 1) - ids_mt = myo_lab[r_m, c_m] - else: - ids_mt = myo_lab[coords[:, 0], coords[:, 1]] - - ids_mt = ids_mt[ids_mt > 0] - if ids_mt.size > 0: - unique, counts = np.unique(ids_mt, return_counts=True) - mt = int(unique[np.argmax(counts)]) - nm.setdefault(mt, []).append(prop.label) - pos += 1 - - per = [len(v) for v in nm.values()] - fused = sum(n for n in per if n >= 2) - fi = 100.0 * fused / total if total else 0.0 - pct = 100.0 * pos / total if total else 0.0 - avg = float(np.mean(per)) if per else 0.0 - - sa = compute_surface_area(myo_mask, px_um=px_um) - - return { - "total_nuclei" : total, - "myHC_positive_nuclei" : int(pos), - "myHC_positive_percentage" : round(pct, 2), - "nuclei_fused" : int(fused), - "myotube_count" : int(len(per)), - "avg_nuclei_per_myotube" : round(avg, 2), - "fusion_index" : round(fi, 2), - "total_area_um2" : sa["total_area_um2"], - "mean_area_um2" : sa["mean_area_um2"], - "max_area_um2" : sa["max_area_um2"], - "_per_myotube_areas" : sa["per_myotube_areas"], - "_bio_myo_ids" : set(nm.keys()), # myotube label IDs with ≥1 MyHC+ nucleus - "_total_cc_count" : int(myo_lab.max()), # total connected components (for reference) - } - - -# ───────────────────────────────────────────────────────────────────────────── -# Overlay helpers -# ───────────────────────────────────────────────────────────────────────────── - -def make_simple_overlay(rgb_u8, nuc_mask, myo_mask, nuc_color, myo_color, alpha): - """Flat colour overlay — used for the ZIP export (fast, no matplotlib).""" - base = rgb_u8.astype(np.float32) - H0, W0 = rgb_u8.shape[:2] - nuc = np.array(Image.fromarray((nuc_mask*255).astype(np.uint8)) - .resize((W0, H0), Image.NEAREST)) > 0 - myo = np.array(Image.fromarray((myo_mask*255).astype(np.uint8)) - .resize((W0, H0), Image.NEAREST)) > 0 - out = base.copy() - for mask, color in [(myo, myo_color), (nuc, nuc_color)]: - c = np.array(color, dtype=np.float32) - out[mask] = (1 - alpha) * out[mask] + alpha * c - return np.clip(out, 0, 255).astype(np.uint8) - - -def make_coloured_overlay(rgb_u8: np.ndarray, - nuc_lab: np.ndarray, - myo_lab: np.ndarray, - alpha: float = 0.45, - nuc_color: tuple = None, - myo_color: tuple = None) -> np.ndarray: - """ - Colour the mask regions only — NO text baked in. - Returns an RGB uint8 array at original image resolution. - - nuc_color / myo_color: RGB tuple e.g. (0, 255, 255). - If None, uses per-instance colourmaps (cool / autumn). - If provided, uses a flat solid colour for all instances of that type — - this is what the sidebar colour pickers control. - """ - orig_h, orig_w = rgb_u8.shape[:2] - nuc_cmap = plt.colormaps["cool"] - myo_cmap = plt.colormaps["autumn"] - - def _resize_lab(lab, h, w): - return np.array( - Image.fromarray(lab.astype(np.int32)).resize((w, h), Image.NEAREST) - ) - - nuc_disp = _resize_lab(nuc_lab, orig_h, orig_w) - myo_disp = _resize_lab(myo_lab, orig_h, orig_w) - n_nuc = int(nuc_disp.max()) - n_myo = int(myo_disp.max()) - - base = rgb_u8.astype(np.float32).copy() - if n_myo > 0: - mask = myo_disp > 0 - if myo_color is not None: - colour_layer = np.array(myo_color, dtype=np.float32) - base[mask] = (1 - alpha) * base[mask] + alpha * colour_layer - else: - myo_norm = (myo_disp / max(n_myo, 1)).astype(np.float32) - myo_rgba = (myo_cmap(myo_norm)[:, :, :3] * 255).astype(np.float32) - base[mask] = (1 - alpha) * base[mask] + alpha * myo_rgba[mask] - - if n_nuc > 0: - mask = nuc_disp > 0 - if nuc_color is not None: - colour_layer = np.array(nuc_color, dtype=np.float32) - base[mask] = (1 - alpha) * base[mask] + alpha * colour_layer - else: - nuc_norm = (nuc_disp / max(n_nuc, 1)).astype(np.float32) - nuc_rgba = (nuc_cmap(nuc_norm)[:, :, :3] * 255).astype(np.float32) - base[mask] = (1 - alpha) * base[mask] + alpha * nuc_rgba[mask] - - return np.clip(base, 0, 255).astype(np.uint8) - - -def make_outline_overlay(rgb_u8: np.ndarray, - nuc_lab: np.ndarray, - myo_lab: np.ndarray, - nuc_color: tuple = (0, 255, 255), - myo_color: tuple = (0, 255, 0), - line_width: int = 2) -> np.ndarray: - """ - Draw contour outlines around each detected instance on the original image. - Shows exactly what the model considers each myotube/nucleus boundary. - """ - orig_h, orig_w = rgb_u8.shape[:2] - - def _resize_lab(lab, h, w): - return np.array( - Image.fromarray(lab.astype(np.int32)).resize((w, h), Image.NEAREST) - ) - - nuc_disp = _resize_lab(nuc_lab, orig_h, orig_w) - myo_disp = _resize_lab(myo_lab, orig_h, orig_w) - - out = rgb_u8.copy() - - # Myotube outlines - if myo_disp.max() > 0: - myo_bounds = find_boundaries(myo_disp, mode='outer') - if line_width > 1: + ring_mask = nuc_dilated & ~nuc_footprint & myo_mask_full.astype(bool) + + if ring_mask.sum() < 4: + # Ring too small (nucleus near edge of myotube) — fall back to overlap test + return in_myo.mean() >= 0.10 + + I_ring = float(myc_channel[ring_mask].mean()) + + if I_ring < 1e-6: + # No myotube signal at all in ring — something is wrong, use overlap + return in_myo.mean() >= 0.10 + + # Step 4 — hole ratio test + hole_ratio = I_nuc / I_ring + return hole_ratio < hole_ratio_thr + + +# ───────────────────────────────────────────────────────────────────────────── +# Biological metrics (counting + fusion + surface area) +# ───────────────────────────────────────────────────────────────────────────── + +def compute_bio_metrics(nuc_mask, myo_mask, + myc_channel_full=None, + min_overlap_frac=0.10, + nuc_ws_min_distance=3, + nuc_ws_min_area=6, + px_um=1.0, + ring_width=6, + hole_ratio_thr=0.85) -> dict: + """ + Compute all biological metrics. + + If myc_channel_full (the raw MyHC grayscale image at original resolution) + is supplied, uses the cytoplasm-hole method (MyoFuse, Lair et al. 2025) + to classify each nucleus — eliminates Z-stack overlap false positives and + gives an accurate, non-overestimated fusion index. + + If myc_channel_full is None, falls back to the original pixel-overlap + method for backward compatibility. + """ + nuc_lab = label_nuclei_watershed(nuc_mask, + min_distance=nuc_ws_min_distance, + min_nuc_area=nuc_ws_min_area) + myo_lab = label_cc(myo_mask) + total = int(nuc_lab.max()) + + # Resize masks/channel to the SAME space for comparison + # nuc_lab and myo_mask are at model resolution (e.g. 512×512). + # myc_channel_full is at original image resolution. + # We resize everything to original resolution for the cytoplasm-hole test. + if myc_channel_full is not None: + H_full, W_full = myc_channel_full.shape + # Resize label maps up to original resolution + nuc_lab_full = np.array( + Image.fromarray(nuc_lab.astype(np.int32)) + .resize((W_full, H_full), Image.NEAREST) + ) + myo_mask_full = np.array( + Image.fromarray((myo_mask * 255).astype(np.uint8)) + .resize((W_full, H_full), Image.NEAREST) + ) > 0 + # Normalise MyHC channel to 0-1 float + myc_f = myc_channel_full.astype(np.float32) + if myc_f.max() > 1.0: + myc_f = myc_f / 255.0 + else: + nuc_lab_full = nuc_lab + myo_mask_full = myo_mask.astype(bool) + myc_f = None + + pos, nm = 0, {} + for prop in measure.regionprops(nuc_lab_full): + coords = prop.coords # (N,2) in full-res space + + if myc_f is not None: + # ── Cytoplasm-hole method (accurate, MyoFuse 2025) ──────────────── + is_fused = classify_nucleus_in_myotube( + coords, myc_f, myo_mask_full, + ring_width=ring_width, + hole_ratio_thr=hole_ratio_thr, + ) + else: + # ── Legacy pixel-overlap fallback ───────────────────────────────── + ids = myo_mask_full.astype(np.uint8)[coords[:, 0], coords[:, 1]] + frac = ids.sum() / max(len(coords), 1) + is_fused = frac >= min_overlap_frac + + if is_fused: + # Find which myotube this nucleus belongs to (use model-res myo_lab) + # Scale coords back to model resolution + if myc_f is not None: + r_m = np.clip((coords[:, 0] * nuc_lab.shape[0] / H_full).astype(int), + 0, nuc_lab.shape[0] - 1) + c_m = np.clip((coords[:, 1] * nuc_lab.shape[1] / W_full).astype(int), + 0, nuc_lab.shape[1] - 1) + ids_mt = myo_lab[r_m, c_m] + else: + ids_mt = myo_lab[coords[:, 0], coords[:, 1]] + + ids_mt = ids_mt[ids_mt > 0] + if ids_mt.size > 0: + unique, counts = np.unique(ids_mt, return_counts=True) + mt = int(unique[np.argmax(counts)]) + nm.setdefault(mt, []).append(prop.label) + pos += 1 + + per = [len(v) for v in nm.values()] + fused = sum(n for n in per if n >= 2) + fi = 100.0 * fused / total if total else 0.0 + pct = 100.0 * pos / total if total else 0.0 + avg = float(np.mean(per)) if per else 0.0 + + sa = compute_surface_area(myo_mask, px_um=px_um) + + return { + "total_nuclei" : total, + "myHC_positive_nuclei" : int(pos), + "myHC_positive_percentage" : round(pct, 2), + "nuclei_fused" : int(fused), + "myotube_count" : int(len(per)), + "avg_nuclei_per_myotube" : round(avg, 2), + "fusion_index" : round(fi, 2), + "total_area_um2" : sa["total_area_um2"], + "mean_area_um2" : sa["mean_area_um2"], + "max_area_um2" : sa["max_area_um2"], + "_per_myotube_areas" : sa["per_myotube_areas"], + "_bio_myo_ids" : set(nm.keys()), # myotube label IDs with ≥1 MyHC+ nucleus + "_total_cc_count" : int(myo_lab.max()), # total connected components (for reference) + } + + +# ───────────────────────────────────────────────────────────────────────────── +# Overlay helpers +# ───────────────────────────────────────────────────────────────────────────── + +def make_simple_overlay(rgb_u8, nuc_mask, myo_mask, nuc_color, myo_color, alpha): + """Flat colour overlay — used for the ZIP export (fast, no matplotlib).""" + base = rgb_u8.astype(np.float32) + H0, W0 = rgb_u8.shape[:2] + nuc = np.array(Image.fromarray((nuc_mask*255).astype(np.uint8)) + .resize((W0, H0), Image.NEAREST)) > 0 + myo = np.array(Image.fromarray((myo_mask*255).astype(np.uint8)) + .resize((W0, H0), Image.NEAREST)) > 0 + out = base.copy() + for mask, color in [(myo, myo_color), (nuc, nuc_color)]: + c = np.array(color, dtype=np.float32) + out[mask] = (1 - alpha) * out[mask] + alpha * c + return np.clip(out, 0, 255).astype(np.uint8) + + +def make_coloured_overlay(rgb_u8: np.ndarray, + nuc_lab: np.ndarray, + myo_lab: np.ndarray, + alpha: float = 0.45, + nuc_color: tuple = None, + myo_color: tuple = None) -> np.ndarray: + """ + Colour the mask regions only — NO text baked in. + Returns an RGB uint8 array at original image resolution. + + nuc_color / myo_color: RGB tuple e.g. (0, 255, 255). + If None, uses per-instance colourmaps (cool / autumn). + If provided, uses a flat solid colour for all instances of that type — + this is what the sidebar colour pickers control. + """ + orig_h, orig_w = rgb_u8.shape[:2] + nuc_cmap = plt.colormaps["cool"] + myo_cmap = plt.colormaps["autumn"] + + def _resize_lab(lab, h, w): + return np.array( + Image.fromarray(lab.astype(np.int32)).resize((w, h), Image.NEAREST) + ) + + nuc_disp = _resize_lab(nuc_lab, orig_h, orig_w) + myo_disp = _resize_lab(myo_lab, orig_h, orig_w) + n_nuc = int(nuc_disp.max()) + n_myo = int(myo_disp.max()) + + base = rgb_u8.astype(np.float32).copy() + if n_myo > 0: + mask = myo_disp > 0 + if myo_color is not None: + colour_layer = np.array(myo_color, dtype=np.float32) + base[mask] = (1 - alpha) * base[mask] + alpha * colour_layer + else: + myo_norm = (myo_disp / max(n_myo, 1)).astype(np.float32) + myo_rgba = (myo_cmap(myo_norm)[:, :, :3] * 255).astype(np.float32) + base[mask] = (1 - alpha) * base[mask] + alpha * myo_rgba[mask] + + if n_nuc > 0: + mask = nuc_disp > 0 + if nuc_color is not None: + colour_layer = np.array(nuc_color, dtype=np.float32) + base[mask] = (1 - alpha) * base[mask] + alpha * colour_layer + else: + nuc_norm = (nuc_disp / max(n_nuc, 1)).astype(np.float32) + nuc_rgba = (nuc_cmap(nuc_norm)[:, :, :3] * 255).astype(np.float32) + base[mask] = (1 - alpha) * base[mask] + alpha * nuc_rgba[mask] + + return np.clip(base, 0, 255).astype(np.uint8) + + +def make_outline_overlay(rgb_u8: np.ndarray, + nuc_lab: np.ndarray, + myo_lab: np.ndarray, + nuc_color: tuple = (0, 255, 255), + myo_color: tuple = (0, 255, 0), + line_width: int = 2) -> np.ndarray: + """ + Draw contour outlines around each detected instance on the original image. + Shows exactly what the model considers each myotube/nucleus boundary. + """ + orig_h, orig_w = rgb_u8.shape[:2] + + def _resize_lab(lab, h, w): + return np.array( + Image.fromarray(lab.astype(np.int32)).resize((w, h), Image.NEAREST) + ) + + nuc_disp = _resize_lab(nuc_lab, orig_h, orig_w) + myo_disp = _resize_lab(myo_lab, orig_h, orig_w) + + out = rgb_u8.copy() + + # Myotube outlines + if myo_disp.max() > 0: + myo_bounds = find_boundaries(myo_disp, mode='outer') + if line_width > 1: myo_bounds = _safe_binary_dilation(myo_bounds, disk(line_width - 1)) - out[myo_bounds] = myo_color - - # Nuclei outlines - if nuc_disp.max() > 0: - nuc_bounds = find_boundaries(nuc_disp, mode='outer') - if line_width > 1: + out[myo_bounds] = myo_color + + # Nuclei outlines + if nuc_disp.max() > 0: + nuc_bounds = find_boundaries(nuc_disp, mode='outer') + if line_width > 1: nuc_bounds = _safe_binary_dilation(nuc_bounds, disk(max(line_width - 2, 0))) - out[nuc_bounds] = nuc_color - - return out - - -def collect_label_positions(nuc_lab: np.ndarray, - myo_lab: np.ndarray, - img_w: int, img_h: int, - bio_myo_ids: set = None) -> dict: - """ - Collect centroid positions for every nucleus and myotube, - scaled to the original image pixel dimensions. - - bio_myo_ids: set of myotube label IDs that have ≥1 MyHC+ nucleus. - If provided, only these are labelled as "M1", "M2", … - (renumbered sequentially). Non-bio regions get no label - and are stored separately for faint outline rendering. - Returns: - { "nuclei": [ {"id": "1", "x": 123.4, "y": 56.7}, ... ], - "myotubes": [ {"id": "M1", "x": 200.1, "y": 300.5, "orig_label": 5}, ... ], - "myotubes_nonbio": [ {"id": "", "x": ..., "y": ..., "orig_label": 3}, ... ] } - """ - sx = img_w / nuc_lab.shape[1] - sy = img_h / nuc_lab.shape[0] - - nuclei = [] - for prop in measure.regionprops(nuc_lab): - r, c = prop.centroid - nuclei.append({"id": str(prop.label), "x": round(c * sx, 1), "y": round(r * sy, 1)}) - - sx2 = img_w / myo_lab.shape[1] - sy2 = img_h / myo_lab.shape[0] - myotubes = [] - myotubes_nonbio = [] - - if bio_myo_ids is not None and len(bio_myo_ids) > 0: - # Renumber biological myotubes sequentially: M1, M2, M3… - sorted_bio = sorted(bio_myo_ids) - bio_remap = {orig: idx + 1 for idx, orig in enumerate(sorted_bio)} - for prop in measure.regionprops(myo_lab): - r, c = prop.centroid - pos = {"x": round(c * sx2, 1), "y": round(r * sy2, 1), "orig_label": prop.label} - if prop.label in bio_remap: - pos["id"] = f"M{bio_remap[prop.label]}" - myotubes.append(pos) - else: - pos["id"] = "" - myotubes_nonbio.append(pos) - else: - # Fallback: label all connected components (backward compat) - for prop in measure.regionprops(myo_lab): - r, c = prop.centroid - myotubes.append({"id": f"M{prop.label}", "x": round(c * sx2, 1), - "y": round(r * sy2, 1), "orig_label": prop.label}) - - return {"nuclei": nuclei, "myotubes": myotubes, "myotubes_nonbio": myotubes_nonbio} - - -def make_svg_viewer(img_b64: str, - img_w: int, img_h: int, - label_data: dict, - show_nuclei: bool = True, - show_myotubes: bool = True, - nuc_font_size: int = 11, - myo_font_size: int = 22, - viewer_height: int = 620) -> str: - """ - Build a self-contained HTML string with: - - A pan-and-zoom SVG viewer (mouse wheel + click-drag) - - The coloured overlay PNG as the background - - SVG labels that stay pixel-perfect at any zoom level - - A font-size slider that updates label sizes live - - Toggle buttons for nuclei / myotubes labels - - Count badges in the top-right corner - - Parameters - ---------- - img_b64 : base64-encoded PNG of the coloured overlay (no text) - img_w, img_h : original pixel dimensions of the image - label_data : output of collect_label_positions() - show_nuclei : initial visibility of nucleus labels - show_myotubes : initial visibility of myotube labels - nuc_font_size : initial nucleus label font size (px) - myo_font_size : initial myotube label font size (px) - viewer_height : height of the viewer div in pixels - """ - import json as _json - labels_json = _json.dumps(label_data) - n_nuc = len(label_data.get("nuclei", [])) - n_myo = len(label_data.get("myotubes", [])) - - show_nuc_js = "true" if show_nuclei else "false" - show_myo_js = "true" if show_myotubes else "false" - - html = f""" - - -
-
-
🔵 Nuclei  {n_nuc}
-
🔴 Myotubes  {n_myo}
- - - -
- Nucleus size: - - {nuc_font_size}px -
-
- Myotube size: - - {myo_font_size}px -
-
Scroll to zoom  ·  Drag to pan
-
- -
- - - - - - - - - - - - - - - - - - -
-
- - -""" - return html - - -# ───────────────────────────────────────────────────────────────────────────── -# Animated counter -# ───────────────────────────────────────────────────────────────────────────── - -def animated_metric(placeholder, label: str, final_val, - color: str = "#4fc3f7", steps: int = 20, delay: float = 0.025): - is_float = isinstance(final_val, float) - for i in range(1, steps + 1): - v = final_val * i / steps - display = f"{v:.1f}" if is_float else str(int(v)) - placeholder.markdown( - f""" -
-
{display}
-
{label}
-
- """, - unsafe_allow_html=True, - ) - time.sleep(delay) - - -# ───────────────────────────────────────────────────────────────────────────── -# Active-learning queue helpers -# ───────────────────────────────────────────────────────────────────────────── - -def _ensure_dirs(): - QUEUE_DIR.mkdir(parents=True, exist_ok=True) - CORRECTIONS_DIR.mkdir(parents=True, exist_ok=True) - - -def add_to_queue(image_array: np.ndarray, reason: str = "batch", - nuc_mask=None, myo_mask=None, metadata: dict = None): - _ensure_dirs() - ts = datetime.now().strftime("%Y%m%d_%H%M%S_%f") - meta = {**(metadata or {}), "reason": reason, "timestamp": ts} - - if nuc_mask is not None and myo_mask is not None: - folder = CORRECTIONS_DIR / ts - folder.mkdir(parents=True, exist_ok=True) - Image.fromarray(image_array).save(folder / "image.png") - Image.fromarray((nuc_mask > 0).astype(np.uint8) * 255).save(folder / "nuclei_mask.png") - Image.fromarray((myo_mask > 0).astype(np.uint8) * 255).save(folder / "myotube_mask.png") - (folder / "meta.json").write_text(json.dumps({**meta, "has_masks": True}, indent=2)) - else: - Image.fromarray(image_array).save(QUEUE_DIR / f"{ts}.png") - (QUEUE_DIR / f"{ts}.json").write_text(json.dumps({**meta, "has_masks": False}, indent=2)) - - + out[nuc_bounds] = nuc_color + + return out + + +def collect_label_positions(nuc_lab: np.ndarray, + myo_lab: np.ndarray, + img_w: int, img_h: int, + bio_myo_ids: set = None) -> dict: + """ + Collect centroid positions for every nucleus and myotube, + scaled to the original image pixel dimensions. + + bio_myo_ids: set of myotube label IDs that have ≥1 MyHC+ nucleus. + If provided, only these are labelled as "M1", "M2", … + (renumbered sequentially). Non-bio regions get no label + and are stored separately for faint outline rendering. + Returns: + { "nuclei": [ {"id": "1", "x": 123.4, "y": 56.7}, ... ], + "myotubes": [ {"id": "M1", "x": 200.1, "y": 300.5, "orig_label": 5}, ... ], + "myotubes_nonbio": [ {"id": "", "x": ..., "y": ..., "orig_label": 3}, ... ] } + """ + sx = img_w / nuc_lab.shape[1] + sy = img_h / nuc_lab.shape[0] + + nuclei = [] + for prop in measure.regionprops(nuc_lab): + r, c = prop.centroid + nuclei.append({"id": str(prop.label), "x": round(c * sx, 1), "y": round(r * sy, 1)}) + + sx2 = img_w / myo_lab.shape[1] + sy2 = img_h / myo_lab.shape[0] + myotubes = [] + myotubes_nonbio = [] + + if bio_myo_ids is not None and len(bio_myo_ids) > 0: + # Renumber biological myotubes sequentially: M1, M2, M3… + sorted_bio = sorted(bio_myo_ids) + bio_remap = {orig: idx + 1 for idx, orig in enumerate(sorted_bio)} + for prop in measure.regionprops(myo_lab): + r, c = prop.centroid + pos = {"x": round(c * sx2, 1), "y": round(r * sy2, 1), "orig_label": prop.label} + if prop.label in bio_remap: + pos["id"] = f"M{bio_remap[prop.label]}" + myotubes.append(pos) + else: + pos["id"] = "" + myotubes_nonbio.append(pos) + else: + # Fallback: label all connected components (backward compat) + for prop in measure.regionprops(myo_lab): + r, c = prop.centroid + myotubes.append({"id": f"M{prop.label}", "x": round(c * sx2, 1), + "y": round(r * sy2, 1), "orig_label": prop.label}) + + return {"nuclei": nuclei, "myotubes": myotubes, "myotubes_nonbio": myotubes_nonbio} + + +def make_svg_viewer(img_b64: str, + img_w: int, img_h: int, + label_data: dict, + show_nuclei: bool = True, + show_myotubes: bool = True, + nuc_font_size: int = 11, + myo_font_size: int = 22, + viewer_height: int = 620) -> str: + """ + Build a self-contained HTML string with: + - A pan-and-zoom SVG viewer (mouse wheel + click-drag) + - The coloured overlay PNG as the background + - SVG labels that stay pixel-perfect at any zoom level + - A font-size slider that updates label sizes live + - Toggle buttons for nuclei / myotubes labels + - Count badges in the top-right corner + + Parameters + ---------- + img_b64 : base64-encoded PNG of the coloured overlay (no text) + img_w, img_h : original pixel dimensions of the image + label_data : output of collect_label_positions() + show_nuclei : initial visibility of nucleus labels + show_myotubes : initial visibility of myotube labels + nuc_font_size : initial nucleus label font size (px) + myo_font_size : initial myotube label font size (px) + viewer_height : height of the viewer div in pixels + """ + import json as _json + labels_json = _json.dumps(label_data) + n_nuc = len(label_data.get("nuclei", [])) + n_myo = len(label_data.get("myotubes", [])) + + show_nuc_js = "true" if show_nuclei else "false" + show_myo_js = "true" if show_myotubes else "false" + + html = f""" + + +
+
+
🔵 Nuclei  {n_nuc}
+
🔴 Myotubes  {n_myo}
+ + + +
+ Nucleus size: + + {nuc_font_size}px +
+
+ Myotube size: + + {myo_font_size}px +
+
Scroll to zoom  ·  Drag to pan
+
+ +
+ + + + + + + + + + + + + + + + + + +
+
+ + +""" + return html + + +# ───────────────────────────────────────────────────────────────────────────── +# Animated counter +# ───────────────────────────────────────────────────────────────────────────── + +def animated_metric(placeholder, label: str, final_val, + color: str = "#4fc3f7", steps: int = 20, delay: float = 0.025): + is_float = isinstance(final_val, float) + for i in range(1, steps + 1): + v = final_val * i / steps + display = f"{v:.1f}" if is_float else str(int(v)) + placeholder.markdown( + f""" +
+
{display}
+
{label}
+
+ """, + unsafe_allow_html=True, + ) + time.sleep(delay) + + +# ───────────────────────────────────────────────────────────────────────────── +# Active-learning queue helpers +# ───────────────────────────────────────────────────────────────────────────── + +def _ensure_dirs(): + QUEUE_DIR.mkdir(parents=True, exist_ok=True) + CORRECTIONS_DIR.mkdir(parents=True, exist_ok=True) + + +def add_to_queue(image_array: np.ndarray, reason: str = "batch", + nuc_mask=None, myo_mask=None, metadata: dict = None): + _ensure_dirs() + ts = datetime.now().strftime("%Y%m%d_%H%M%S_%f") + meta = {**(metadata or {}), "reason": reason, "timestamp": ts} + + if myo_mask is not None: + folder = CORRECTIONS_DIR / ts + folder.mkdir(parents=True, exist_ok=True) + Image.fromarray(image_array).save(folder / "image.png") + Image.fromarray((myo_mask > 0).astype(np.uint8) * 255).save(folder / "myotube_mask.png") + if nuc_mask is not None: + Image.fromarray((nuc_mask > 0).astype(np.uint8) * 255).save(folder / "nuclei_mask.png") + meta["nuc_source"] = "user_corrected" + else: + meta["nuc_source"] = "model_predicted" + (folder / "meta.json").write_text(json.dumps({**meta, "has_masks": True}, indent=2)) + elif nuc_mask is not None: + folder = CORRECTIONS_DIR / ts + folder.mkdir(parents=True, exist_ok=True) + Image.fromarray(image_array).save(folder / "image.png") + Image.fromarray((nuc_mask > 0).astype(np.uint8) * 255).save(folder / "nuclei_mask.png") + meta["myo_source"] = "model_predicted" + (folder / "meta.json").write_text(json.dumps({**meta, "has_masks": True}, indent=2)) + else: + Image.fromarray(image_array).save(QUEUE_DIR / f"{ts}.png") + (QUEUE_DIR / f"{ts}.json").write_text(json.dumps({**meta, "has_masks": False}, indent=2)) + + def safe_zip_stem(name: str) -> str: keep = [] for ch in Path(str(name)).stem: @@ -1150,91 +1161,91 @@ def artifact_lookup_keys(display_name: str, artifact: dict = None) -> set[str]: keys.add(sample_match_key(value)) keys.add(sample_match_key(safe_zip_stem(value))) return {k for k in keys if k} - - -def build_mask_batch_zip(selected_names, artifacts: dict) -> bytes: - buf = io.BytesIO() - manifest = [] - with zipfile.ZipFile(buf, "w", compression=zipfile.ZIP_DEFLATED) as zf: - for name in selected_names: - if name not in artifacts: - continue - art = artifacts[name] - folder = safe_zip_stem(name) - rgb = art.get("rgb_u8") - nuc = art.get("nuc_pp_arr") - myo = art.get("myo_pp_arr") - if rgb is None or nuc is None or myo is None: - continue - zf.writestr(f"{folder}/image.png", png_bytes(rgb.astype(np.uint8))) - zf.writestr(f"{folder}/nuclei_mask.png", png_bytes((nuc > 0).astype(np.uint8) * 255)) - zf.writestr(f"{folder}/myotube_mask.png", png_bytes((myo > 0).astype(np.uint8) * 255)) - meta = { - "source_image": name, - "folder": folder, - "expected_files": ["image.png", "nuclei_mask.png", "myotube_mask.png"], - } - zf.writestr(f"{folder}/meta.json", json.dumps(meta, indent=2)) - manifest.append(meta) - zf.writestr("manifest.json", json.dumps(manifest, indent=2)) - return buf.getvalue() - - -def _zip_png_mask(zf: zipfile.ZipFile, member: str) -> np.ndarray: - with zf.open(member) as f: - return (np.array(Image.open(f).convert("L")) > 0).astype(np.uint8) - - -def _pick_mask_member(files: dict, base_name: str): - for ext in (".png", ".tif", ".tiff"): - member = files.get(f"{base_name}{ext}") - if member: - return member - return None - - -def import_correction_zip(zip_file, artifacts: dict) -> tuple[int, list[str]]: - submitted = 0 - errors = [] + + +def build_mask_batch_zip(selected_names, artifacts: dict) -> bytes: + buf = io.BytesIO() + manifest = [] + with zipfile.ZipFile(buf, "w", compression=zipfile.ZIP_DEFLATED) as zf: + for name in selected_names: + if name not in artifacts: + continue + art = artifacts[name] + folder = safe_zip_stem(name) + rgb = art.get("rgb_u8") + nuc = art.get("nuc_pp_arr") + myo = art.get("myo_pp_arr") + if rgb is None or nuc is None or myo is None: + continue + zf.writestr(f"{folder}/image.png", png_bytes(rgb.astype(np.uint8))) + zf.writestr(f"{folder}/nuclei_mask.png", png_bytes((nuc > 0).astype(np.uint8) * 255)) + zf.writestr(f"{folder}/myotube_mask.png", png_bytes((myo > 0).astype(np.uint8) * 255)) + meta = { + "source_image": name, + "folder": folder, + "expected_files": ["image.png", "nuclei_mask.png", "myotube_mask.png"], + } + zf.writestr(f"{folder}/meta.json", json.dumps(meta, indent=2)) + manifest.append(meta) + zf.writestr("manifest.json", json.dumps(manifest, indent=2)) + return buf.getvalue() + + +def _zip_png_mask(zf: zipfile.ZipFile, member: str) -> np.ndarray: + with zf.open(member) as f: + return (np.array(Image.open(f).convert("L")) > 0).astype(np.uint8) + + +def _pick_mask_member(files: dict, base_name: str): + for ext in (".png", ".tif", ".tiff"): + member = files.get(f"{base_name}{ext}") + if member: + return member + return None + + +def import_correction_zip(zip_file, artifacts: dict) -> tuple[int, list[str]]: + submitted = 0 + errors = [] artifact_by_key = {} for name, art in artifacts.items(): for key in artifact_lookup_keys(name, art): artifact_by_key[key] = name - - try: - data = zip_file.getvalue() - zf = zipfile.ZipFile(io.BytesIO(data)) - except Exception as exc: - return 0, [f"Could not read ZIP: {exc}"] - - members = [m for m in zf.namelist() if not m.endswith("/")] - used_sources = set() - - # Preferred format: folders produced by build_mask_batch_zip. - folders = sorted({str(Path(m).parent).replace("\\", "/") for m in members if "/" in m}) - for folder in folders: - if folder in ("", "."): - continue - files = {Path(m).name.lower(): m for m in members if str(Path(m).parent).replace("\\", "/") == folder} - nuc_member = _pick_mask_member(files, "nuclei_mask") - myo_member = _pick_mask_member(files, "myotube_mask") - if not (nuc_member and myo_member): - continue + + try: + data = zip_file.getvalue() + zf = zipfile.ZipFile(io.BytesIO(data)) + except Exception as exc: + return 0, [f"Could not read ZIP: {exc}"] + + members = [m for m in zf.namelist() if not m.endswith("/")] + used_sources = set() + + # Preferred format: folders produced by build_mask_batch_zip. + folders = sorted({str(Path(m).parent).replace("\\", "/") for m in members if "/" in m}) + for folder in folders: + if folder in ("", "."): + continue + files = {Path(m).name.lower(): m for m in members if str(Path(m).parent).replace("\\", "/") == folder} + nuc_member = _pick_mask_member(files, "nuclei_mask") + myo_member = _pick_mask_member(files, "myotube_mask") + if not (nuc_member and myo_member): + continue source = artifact_by_key.get(sample_match_key(Path(folder).name)) - meta_member = files.get("meta.json") - if meta_member: - try: - with zf.open(meta_member) as f: - meta = json.load(f) - source = meta.get("source_image", source) - except Exception: - pass - if source not in artifacts: - errors.append(f"Skipped {folder}: source image is not in current analysis.") - continue - try: - nuc_arr = _zip_png_mask(zf, nuc_member) - myo_arr = _zip_png_mask(zf, myo_member) + meta_member = files.get("meta.json") + if meta_member: + try: + with zf.open(meta_member) as f: + meta = json.load(f) + source = meta.get("source_image", source) + except Exception: + pass + if source not in artifacts: + errors.append(f"Skipped {folder}: source image is not in current analysis.") + continue + try: + nuc_arr = _zip_png_mask(zf, nuc_member) + myo_arr = _zip_png_mask(zf, myo_member) art = artifacts[source] orig_rgb = art["rgb_u8"] add_to_queue(orig_rgb, nuc_mask=nuc_arr, myo_mask=myo_arr, @@ -1244,36 +1255,36 @@ def import_correction_zip(zip_file, artifacts: dict) -> tuple[int, list[str]]: "original_filename": art.get("original_filename", source), "batch_folder": folder, "timestamp": datetime.now().isoformat()}) - submitted += 1 - used_sources.add(source) - except Exception as exc: - errors.append(f"Skipped {folder}: {exc}") - - # Also accept flat files like sample_nuclei_mask.png + sample_myotube_mask.png. - flat = {Path(m).name: m for m in members if "/" not in m} - stems = {} - for filename, member in flat.items(): - lower = filename.lower() - for suffix, key in ( - ("_nuclei_mask.png", "nuc"), - ("_nuclei_mask.tif", "nuc"), - ("_nuclei_mask.tiff", "nuc"), - ("_myotube_mask.png", "myo"), - ("_myotube_mask.tif", "myo"), - ("_myotube_mask.tiff", "myo"), - ): - if lower.endswith(suffix): - stem = filename[: -len(suffix)] - stems.setdefault(stem, {})[key] = member - for stem, pair in stems.items(): - if not {"nuc", "myo"}.issubset(pair): - continue + submitted += 1 + used_sources.add(source) + except Exception as exc: + errors.append(f"Skipped {folder}: {exc}") + + # Also accept flat files like sample_nuclei_mask.png + sample_myotube_mask.png. + flat = {Path(m).name: m for m in members if "/" not in m} + stems = {} + for filename, member in flat.items(): + lower = filename.lower() + for suffix, key in ( + ("_nuclei_mask.png", "nuc"), + ("_nuclei_mask.tif", "nuc"), + ("_nuclei_mask.tiff", "nuc"), + ("_myotube_mask.png", "myo"), + ("_myotube_mask.tif", "myo"), + ("_myotube_mask.tiff", "myo"), + ): + if lower.endswith(suffix): + stem = filename[: -len(suffix)] + stems.setdefault(stem, {})[key] = member + for stem, pair in stems.items(): + if not {"nuc", "myo"}.issubset(pair): + continue source = artifact_by_key.get(sample_match_key(stem)) - if source not in artifacts or source in used_sources: - continue - try: - nuc_arr = _zip_png_mask(zf, pair["nuc"]) - myo_arr = _zip_png_mask(zf, pair["myo"]) + if source not in artifacts or source in used_sources: + continue + try: + nuc_arr = _zip_png_mask(zf, pair["nuc"]) + myo_arr = _zip_png_mask(zf, pair["myo"]) art = artifacts[source] add_to_queue(art["rgb_u8"], nuc_mask=nuc_arr, myo_mask=myo_arr, reason="user_correction_batch", @@ -1282,18 +1293,18 @@ def import_correction_zip(zip_file, artifacts: dict) -> tuple[int, list[str]]: "original_filename": art.get("original_filename", source), "batch_stem": stem, "timestamp": datetime.now().isoformat()}) - submitted += 1 - except Exception as exc: - errors.append(f"Skipped {stem}: {exc}") - - return submitted, errors - - -# ───────────────────────────────────────────────────────────────────────────── -# Model (architecture identical to training script) -# ───────────────────────────────────────────────────────────────────────────── - - + submitted += 1 + except Exception as exc: + errors.append(f"Skipped {stem}: {exc}") + + return submitted, errors + + +# ───────────────────────────────────────────────────────────────────────────── +# Model (architecture identical to training script) +# ───────────────────────────────────────────────────────────────────────────── + + def correction_stem(upload_name: str) -> str: stem = normalize_sample_id(upload_name) lower = stem.lower() @@ -1305,18 +1316,22 @@ def correction_stem(upload_name: str) -> str: if lower.endswith(suffix): return normalize_sample_id(stem[: -len(suffix)]) return normalize_sample_id(stem) - - -def uploaded_mask_to_array(uploaded_file) -> np.ndarray: - return (np.array(Image.open(uploaded_file).convert("L")) > 0).astype(np.uint8) - - -def submit_uploaded_correction_pairs(nuc_files, myo_files, artifacts: dict, selected_name: str): - nuc_files = nuc_files or [] - myo_files = myo_files or [] - if len(nuc_files) != len(myo_files): - return 0, [f"Upload the same number of nuclei and myotube masks ({len(nuc_files)} nuclei, {len(myo_files)} myotube)."] - + + +def uploaded_mask_to_array(uploaded_file) -> np.ndarray: + return (np.array(Image.open(uploaded_file).convert("L")) > 0).astype(np.uint8) + + +def submit_uploaded_correction_pairs(nuc_files, myo_files, artifacts: dict, selected_name: str): + """Submit corrections. Either myotube masks, nuclei masks, or both can be uploaded. + Uploaded masks immediately replace the model predictions in the current session + and are also saved to corrections/ for future retraining.""" + nuc_files = nuc_files or [] + myo_files = myo_files or [] + + if not myo_files and not nuc_files: + return 0, ["Please upload at least one corrected mask."] + artifact_by_key = {} for name, art in artifacts.items(): for key in artifact_lookup_keys(name, art): @@ -1324,88 +1339,172 @@ def submit_uploaded_correction_pairs(nuc_files, myo_files, artifacts: dict, sele nuc_by_stem = {sample_match_key(correction_stem(f.name)): f for f in nuc_files} myo_by_stem = {sample_match_key(correction_stem(f.name)): f for f in myo_files} - common_stems = sorted(set(nuc_by_stem) & set(myo_by_stem)) - if len(nuc_files) == len(myo_files) == 1 and not common_stems: + # All unique stems from either upload + all_stems = sorted(set(nuc_by_stem) | set(myo_by_stem)) + + # Single-file shortcut: map to the selected image + if len(myo_files) + len(nuc_files) == 1: + selected_key = sample_match_key(selected_name) + all_stems = [selected_key] + if myo_files: + myo_by_stem[selected_key] = myo_files[0] + if nuc_files: + nuc_by_stem[selected_key] = nuc_files[0] + elif len(myo_files) == 1 and len(nuc_files) == 1: selected_key = sample_match_key(selected_name) - common_stems = [selected_key] - nuc_by_stem[selected_key] = nuc_files[0] - myo_by_stem[selected_key] = myo_files[0] - - submitted = 0 - errors = [] - missing_nuc = sorted(set(myo_by_stem) - set(nuc_by_stem)) - missing_myo = sorted(set(nuc_by_stem) - set(myo_by_stem)) - for stem in missing_nuc: - errors.append(f"Skipped {stem}: no matching nuclei mask.") - for stem in missing_myo: - errors.append(f"Skipped {stem}: no matching myotube mask.") - - for stem in common_stems: + if selected_key not in myo_by_stem and selected_key not in nuc_by_stem: + all_stems = [selected_key] + myo_by_stem[selected_key] = myo_files[0] + nuc_by_stem[selected_key] = nuc_files[0] + + submitted = 0 + errors = [] + + for stem in all_stems: source = artifact_by_key.get(stem) - if source is None and len(common_stems) == 1: + if source is None and len(all_stems) == 1: source = selected_name - if source not in artifacts: - errors.append(f"Skipped {stem}: no matching analyzed image is loaded.") - continue - + if source not in artifacts: + errors.append(f"Skipped {stem}: no matching analyzed image is loaded.") + continue + try: - nuc_arr = uploaded_mask_to_array(nuc_by_stem[stem]) - myo_arr = uploaded_mask_to_array(myo_by_stem[stem]) art = artifacts[source] + + # Get corrected masks — fall back to model prediction for whichever wasn't uploaded + has_myo = stem in myo_by_stem + has_nuc = stem in nuc_by_stem + + if has_myo: + myo_arr = uploaded_mask_to_array(myo_by_stem[stem]) + else: + myo_pp = art.get("myo_pp_arr") + myo_arr = (myo_pp > 0).astype(np.uint8) if myo_pp is not None else None + + if has_nuc: + nuc_arr = uploaded_mask_to_array(nuc_by_stem[stem]) + else: + nuc_pp = art.get("nuc_pp_arr") + nuc_arr = (nuc_pp > 0).astype(np.uint8) if nuc_pp is not None else None + + # Save to corrections/ for future retraining add_to_queue( art["rgb_u8"], nuc_mask=nuc_arr, myo_mask=myo_arr, - reason="user_correction_batch" if len(common_stems) > 1 else "user_correction", + reason="user_correction_batch" if len(all_stems) > 1 else "user_correction", metadata={ "source_image": source, "sample_id": art.get("sample_id", source), "original_filename": art.get("original_filename", source), - "nuclei_file": nuc_by_stem[stem].name, - "myotube_file": myo_by_stem[stem].name, - "timestamp": datetime.now().isoformat(), - }, - ) - submitted += 1 - except Exception as exc: - errors.append(f"Skipped {stem}: {exc}") - - return submitted, errors - -class DoubleConv(nn.Module): - def __init__(self, in_ch, out_ch): - super().__init__() - self.net = nn.Sequential( - nn.Conv2d(in_ch, out_ch, 3, padding=1), nn.BatchNorm2d(out_ch), nn.ReLU(True), - nn.Conv2d(out_ch, out_ch, 3, padding=1), nn.BatchNorm2d(out_ch), nn.ReLU(True), - ) - def forward(self, x): return self.net(x) - - + "nuclei_file": nuc_by_stem[stem].name if has_nuc else f"model_predicted ({source})", + "myotube_file": myo_by_stem[stem].name if has_myo else f"model_predicted ({source})", + "nuc_source": "user_corrected" if has_nuc else "model_predicted", + "myo_source": "user_corrected" if has_myo else "model_predicted", + "timestamp": datetime.now().isoformat(), + }, + ) + + # ── Immediately apply corrections to the current session ── + # Replace the mask(s) in the artifact so displayed results update + rgb_u8 = art["rgb_u8"] + + # Resize corrected mask to model resolution if needed + model_h, model_w = art.get("nuc_pp_arr", art.get("myo_pp_arr", np.zeros((512,512)))).shape[:2] + + if has_myo and myo_arr is not None: + myo_resized = np.array( + Image.fromarray((myo_arr * 255).astype(np.uint8)) + .resize((model_w, model_h), Image.NEAREST) + ) > 0 + myo_resized = myo_resized.astype(np.uint8) + art["myo_pp_arr"] = myo_resized + art["myo_pp"] = png_bytes((myo_resized * 255).astype(np.uint8)) + + if has_nuc and nuc_arr is not None: + nuc_resized = np.array( + Image.fromarray((nuc_arr * 255).astype(np.uint8)) + .resize((model_w, model_h), Image.NEAREST) + ) > 0 + nuc_resized = nuc_resized.astype(np.uint8) + art["nuc_pp_arr"] = nuc_resized + art["nuc_pp"] = png_bytes((nuc_resized * 255).astype(np.uint8)) + + # Recompute bio metrics with the corrected mask(s) + current_nuc = art["nuc_pp_arr"] + current_myo = art["myo_pp_arr"] + + # Get MyHC channel if available for cytoplasm-hole method + myc_full = art.get("myc_channel_full") + + bio = compute_bio_metrics( + current_nuc, current_myo, + myc_channel_full=myc_full, + ) + per_areas = bio.pop("_per_myotube_areas", []) + bio_myo_ids = bio.pop("_bio_myo_ids", set()) + total_cc_count = bio.pop("_total_cc_count", 0) + bio["image"] = source + + # Update stored metrics + if "bio_metrics" in st.session_state: + st.session_state.bio_metrics[source] = {**bio, "_per_myotube_areas": per_areas} + + # Recompute overlays and labels + nuc_lab = label_nuclei_watershed(current_nuc) + myo_lab = label_cc(current_myo) + + orig_h, orig_w = rgb_u8.shape[:2] + label_positions = collect_label_positions( + nuc_lab, myo_lab, orig_w, orig_h, + bio_myo_ids=bio_myo_ids, + ) + art.update({ + "nuc_lab": nuc_lab, + "myo_lab": myo_lab, + "label_positions": label_positions, + }) + + submitted += 1 + except Exception as exc: + errors.append(f"Skipped {stem}: {exc}") + + return submitted, errors + +class DoubleConv(nn.Module): + def __init__(self, in_ch, out_ch): + super().__init__() + self.net = nn.Sequential( + nn.Conv2d(in_ch, out_ch, 3, padding=1), nn.BatchNorm2d(out_ch), nn.ReLU(True), + nn.Conv2d(out_ch, out_ch, 3, padding=1), nn.BatchNorm2d(out_ch), nn.ReLU(True), + ) + def forward(self, x): return self.net(x) + + class UNet(nn.Module): - def __init__(self, in_ch=2, out_ch=2, base=32): - super().__init__() - self.d1 = DoubleConv(in_ch, base); self.p1 = nn.MaxPool2d(2) - self.d2 = DoubleConv(base, base*2); self.p2 = nn.MaxPool2d(2) - self.d3 = DoubleConv(base*2, base*4); self.p3 = nn.MaxPool2d(2) - self.d4 = DoubleConv(base*4, base*8); self.p4 = nn.MaxPool2d(2) - self.bn = DoubleConv(base*8, base*16) - self.u4 = nn.ConvTranspose2d(base*16, base*8, 2, 2); self.du4 = DoubleConv(base*16, base*8) - self.u3 = nn.ConvTranspose2d(base*8, base*4, 2, 2); self.du3 = DoubleConv(base*8, base*4) - self.u2 = nn.ConvTranspose2d(base*4, base*2, 2, 2); self.du2 = DoubleConv(base*4, base*2) - self.u1 = nn.ConvTranspose2d(base*2, base, 2, 2); self.du1 = DoubleConv(base*2, base) - self.out = nn.Conv2d(base, out_ch, 1) - - def forward(self, x): - d1=self.d1(x); p1=self.p1(d1) - d2=self.d2(p1); p2=self.p2(d2) - d3=self.d3(p2); p3=self.p3(d3) - d4=self.d4(p3); p4=self.p4(d4) - b=self.bn(p4) - x=self.u4(b); x=torch.cat([x,d4],1); x=self.du4(x) - x=self.u3(x); x=torch.cat([x,d3],1); x=self.du3(x) - x=self.u2(x); x=torch.cat([x,d2],1); x=self.du2(x) + def __init__(self, in_ch=2, out_ch=2, base=32): + super().__init__() + self.d1 = DoubleConv(in_ch, base); self.p1 = nn.MaxPool2d(2) + self.d2 = DoubleConv(base, base*2); self.p2 = nn.MaxPool2d(2) + self.d3 = DoubleConv(base*2, base*4); self.p3 = nn.MaxPool2d(2) + self.d4 = DoubleConv(base*4, base*8); self.p4 = nn.MaxPool2d(2) + self.bn = DoubleConv(base*8, base*16) + self.u4 = nn.ConvTranspose2d(base*16, base*8, 2, 2); self.du4 = DoubleConv(base*16, base*8) + self.u3 = nn.ConvTranspose2d(base*8, base*4, 2, 2); self.du3 = DoubleConv(base*8, base*4) + self.u2 = nn.ConvTranspose2d(base*4, base*2, 2, 2); self.du2 = DoubleConv(base*4, base*2) + self.u1 = nn.ConvTranspose2d(base*2, base, 2, 2); self.du1 = DoubleConv(base*2, base) + self.out = nn.Conv2d(base, out_ch, 1) + + def forward(self, x): + d1=self.d1(x); p1=self.p1(d1) + d2=self.d2(p1); p2=self.p2(d2) + d3=self.d3(p2); p3=self.p3(d3) + d4=self.d4(p3); p4=self.p4(d4) + b=self.bn(p4) + x=self.u4(b); x=torch.cat([x,d4],1); x=self.du4(x) + x=self.u3(x); x=torch.cat([x,d3],1); x=self.du3(x) + x=self.u2(x); x=torch.cat([x,d2],1); x=self.du2(x) x=self.u1(x); x=torch.cat([x,d1],1); x=self.du1(x) return self.out(x) @@ -1417,10 +1516,10 @@ def load_model(device: str): filename=MODEL_FILENAME, force_download=False, ) - file_sha = sha256_file(local) - mtime = time.ctime(os.path.getmtime(local)) - size_mb = os.path.getsize(local) / 1e6 - + file_sha = sha256_file(local) + mtime = time.ctime(os.path.getmtime(local)) + size_mb = os.path.getsize(local) / 1e6 + with st.sidebar.expander("Model details", expanded=False): st.caption(f"App version: `{APP_VERSION}`") st.caption(f"Repo: `{MODEL_REPO_ID}`") @@ -1429,7 +1528,7 @@ def load_model(device: str): st.caption(f"Modified: {mtime}") st.caption(f"SHA256: `{file_sha[:20]}…`") st.caption("Architecture: `unet_v1`") - + ckpt = torch.load(local, map_location=device, weights_only=False) state = ckpt["model"] if isinstance(ckpt, dict) and "model" in ckpt else ckpt model = UNet(in_ch=2, out_ch=2, base=32) @@ -1526,12 +1625,12 @@ def build_validation_report_zip(df: pd.DataFrame, settings: dict, errors=None, m ).encode("utf-8"), ) return out.getvalue() - - -# ───────────────────────────────────────────────────────────────────────────── -# PAGE CONFIG + CSS -# ───────────────────────────────────────────────────────────────────────────── - + + +# ───────────────────────────────────────────────────────────────────────────── +# PAGE CONFIG + CSS +# ───────────────────────────────────────────────────────────────────────────── + st.set_page_config(page_title="MyoSeg — Myotube & Nuclei Analyzer", layout="wide", page_icon="🔬") @@ -1607,24 +1706,24 @@ h1,h2,h3,h4 {{ color:{_theme["heading"]}; }} }} """, unsafe_allow_html=True) - + st.title("🔬 MyoSeg — Myotube & Nuclei Analyzer") device = "cuda" if torch.cuda.is_available() else "cpu" -# ───────────────────────────────────────────────────────────────────────────── -# SIDEBAR -# ───────────────────────────────────────────────────────────────────────────── -with st.sidebar: - st.caption(f"Device: **{device}**") - - st.header("Input mapping") - src1 = st.selectbox("Model channel 1 (MyHC / myotubes)", - ["Red", "Green", "Blue", "Grayscale"], index=0) - inv1 = st.checkbox("Invert channel 1", value=False) - src2 = st.selectbox("Model channel 2 (DAPI / nuclei)", - ["Red", "Green", "Blue", "Grayscale"], index=2) - inv2 = st.checkbox("Invert channel 2", value=False) - +# ───────────────────────────────────────────────────────────────────────────── +# SIDEBAR +# ───────────────────────────────────────────────────────────────────────────── +with st.sidebar: + st.caption(f"Device: **{device}**") + + st.header("Input mapping") + src1 = st.selectbox("Model channel 1 (MyHC / myotubes)", + ["Red", "Green", "Blue", "Grayscale"], index=0) + inv1 = st.checkbox("Invert channel 1", value=False) + src2 = st.selectbox("Model channel 2 (DAPI / nuclei)", + ["Red", "Green", "Blue", "Grayscale"], index=2) + inv2 = st.checkbox("Invert channel 2", value=False) + st.header("Preprocessing") image_size = st.select_slider("Model input size", options=[256, 384, 512, 640, 768, 1024], value=512) @@ -1647,10 +1746,10 @@ with st.sidebar: "Preset", ["Training defaults", "Standard", "Dense culture rescue", "Sparse cleanup"], index=0, - help=( - "Dense culture rescue lowers the myotube threshold and uses more " - "aggressive bridge-breaking/splitting to reduce myotube undercounting." - ), + help=( + "Dense culture rescue lowers the myotube threshold and uses more " + "aggressive bridge-breaking/splitting to reduce myotube undercounting." + ), ) preset_defaults = { "Training defaults": {"thr_nuc": 0.45, "thr_myo": 0.23, "min_nuc_area": 20, @@ -1658,130 +1757,130 @@ with st.sidebar: "myo_erode_radius": 7, "min_myo_aspect_ratio": 0.0, "myo_max_area_px": 3200, "myo_split_min_seeds": 2}, "Standard": {"thr_nuc": 0.45, "thr_myo": 0.40, "min_nuc_area": 20, - "min_myo_area": 500, "nuc_close_radius": 2, "myo_open_radius": 2, - "myo_erode_radius": 2, "min_myo_aspect_ratio": 0.0, - "myo_max_area_px": 20000, "myo_split_min_seeds": 2}, - "Dense culture rescue": {"thr_nuc": 0.42, "thr_myo": 0.28, "min_nuc_area": 15, - "min_myo_area": 250, "nuc_close_radius": 2, "myo_open_radius": 1, - "myo_erode_radius": 4, "min_myo_aspect_ratio": 0.0, - "myo_max_area_px": 6000, "myo_split_min_seeds": 2}, - "Sparse cleanup": {"thr_nuc": 0.45, "thr_myo": 0.45, "min_nuc_area": 20, - "min_myo_area": 800, "nuc_close_radius": 2, "myo_open_radius": 2, - "myo_erode_radius": 2, "min_myo_aspect_ratio": 1.5, - "myo_max_area_px": 25000, "myo_split_min_seeds": 3}, - } - defaults = preset_defaults[analysis_preset] - preset_key = analysis_preset.lower().replace(" ", "_") - - st.header("Thresholds") - thr_nuc = st.slider( - "Nuclei threshold", 0.05, 0.95, defaults["thr_nuc"], 0.01, - key=f"thr_nuc_{preset_key}" - ) - thr_myo = st.slider( - "Myotube threshold", 0.05, 0.95, defaults["thr_myo"], 0.01, - key=f"thr_myo_{preset_key}" - ) - - st.header("Fusion Index method") - fi_method = st.radio( - "FI classification method", - ["Cytoplasm-hole (accurate, Lair 2025)", "Pixel-overlap (legacy)"], - index=0, - help=( - "Cytoplasm-hole: checks for a MyHC signal dip beneath each nucleus — " - "eliminates false positives from nuclei sitting above/below myotubes in Z. " - "Pixel-overlap: legacy method that overestimates FI (Lair et al. 2025)." - ) - ) - use_hole_method = fi_method.startswith("Cytoplasm") - hole_ratio_thr = st.slider( + "min_myo_area": 500, "nuc_close_radius": 2, "myo_open_radius": 2, + "myo_erode_radius": 2, "min_myo_aspect_ratio": 0.0, + "myo_max_area_px": 20000, "myo_split_min_seeds": 2}, + "Dense culture rescue": {"thr_nuc": 0.42, "thr_myo": 0.28, "min_nuc_area": 15, + "min_myo_area": 250, "nuc_close_radius": 2, "myo_open_radius": 1, + "myo_erode_radius": 4, "min_myo_aspect_ratio": 0.0, + "myo_max_area_px": 6000, "myo_split_min_seeds": 2}, + "Sparse cleanup": {"thr_nuc": 0.45, "thr_myo": 0.45, "min_nuc_area": 20, + "min_myo_area": 800, "nuc_close_radius": 2, "myo_open_radius": 2, + "myo_erode_radius": 2, "min_myo_aspect_ratio": 1.5, + "myo_max_area_px": 25000, "myo_split_min_seeds": 3}, + } + defaults = preset_defaults[analysis_preset] + preset_key = analysis_preset.lower().replace(" ", "_") + + st.header("Thresholds") + thr_nuc = st.slider( + "Nuclei threshold", 0.05, 0.95, defaults["thr_nuc"], 0.01, + key=f"thr_nuc_{preset_key}" + ) + thr_myo = st.slider( + "Myotube threshold", 0.05, 0.95, defaults["thr_myo"], 0.01, + key=f"thr_myo_{preset_key}" + ) + + st.header("Fusion Index method") + fi_method = st.radio( + "FI classification method", + ["Cytoplasm-hole (accurate, Lair 2026)", "Pixel-overlap (legacy)"], + index=0, + help=( + "Cytoplasm-hole: checks for a MyHC signal dip beneath each nucleus — " + "eliminates false positives from nuclei sitting above/below myotubes in Z. " + "Pixel-overlap: legacy method that overestimates FI (Lair et al. 2026)." + ) + ) + use_hole_method = fi_method.startswith("Cytoplasm") + hole_ratio_thr = st.slider( "Hole ratio threshold", 0.50, 0.99, 0.78, 0.01, - help=( - "A nucleus is counted as fused if its MyHC signal is less than " - "this fraction of the surrounding cytoplasm ring signal. " - "Lower = stricter (fewer nuclei counted as fused). " - "0.85 is the value validated by Lair et al. 2025." - ), - disabled=not use_hole_method, - ) - ring_width_px = st.number_input( - "Cytoplasm ring width (px)", 2, 20, 6, 1, - help="Width of the ring around each nucleus used to measure local MyHC intensity.", - disabled=not use_hole_method, - ) - - st.header("Postprocessing") - min_nuc_area = st.number_input( - "Min nucleus area (px)", 0, 10000, defaults["min_nuc_area"], 1, - key=f"min_nuc_area_{preset_key}" - ) - min_myo_area = st.number_input( - "Min myotube area (px)", 0, 200000, defaults["min_myo_area"], 10, - key=f"min_myo_area_{preset_key}" - ) - nuc_close_radius = st.number_input( - "Nuclei close radius", 0, 50, defaults["nuc_close_radius"], 1, - key=f"nuc_close_radius_{preset_key}" - ) - myo_open_radius = st.number_input( - "Myotube open radius", 0, 50, defaults["myo_open_radius"], 1, - key=f"myo_open_radius_{preset_key}", - help="Opening removes small noise without merging separate myotubes. " - "Replaces the old closing radius which was merging adjacent myotubes.") - - st.header("Myotube separation") - st.caption( - "These controls break apart touching/bridged myotubes that would " - "otherwise be counted as a single object." - ) - myo_erode_radius = st.number_input( - "Myotube erode radius (px)", 0, 15, defaults["myo_erode_radius"], 1, - key=f"myo_erode_radius_{preset_key}", - help=( - "Erode + re-dilate breaks thin pixel bridges between adjacent " - "myotubes while preserving their overall size. " - "Start at 2 px; increase to 3–4 px for very dense cultures. " - "Set 0 to disable." - ) - ) - min_myo_aspect_ratio = st.number_input( - "Min myotube aspect ratio", 0.0, 10.0, defaults["min_myo_aspect_ratio"], 0.1, - key=f"min_myo_aspect_ratio_{preset_key}", - help=( - "Rejects round blobs (debris/artifacts) that are not real myotubes. " - "Myotubes are elongated (aspect ratio > 3). Round objects have ~1. " - "Set to 1.5–2.0 to filter false positives in sparse cultures. " - "Set to 0 to disable (default)." - ) - ) - myo_max_area_px = st.number_input( - "Max myotube area before split (px)", 0, 500000, defaults["myo_max_area_px"], 500, - key=f"myo_max_area_px_{preset_key}", - help=( - "Any connected myotube region larger than this is split using " - "nucleus-seeded watershed. Set to 0 to disable. " - "Increase for cultures with legitimately large single myotubes." - ) - ) - myo_split_min_seeds = st.number_input( - "Min nuclei seeds to split", 2, 20, defaults["myo_split_min_seeds"], 1, - key=f"myo_split_min_seeds_{preset_key}", - help=( - "Minimum nucleus centroids required before splitting a large region. " - "Set to 2 to split merged pairs; increase if single large myotubes " - "are being incorrectly split." - ) - ) - - st.header("Watershed (nuclei splitting)") - nuc_ws_min_dist = st.number_input("Min watershed distance", 1, 30, 3, 1) - nuc_ws_min_area = st.number_input("Min watershed area (px)", 1, 500, 6, 1) - - st.header("Overlay") - nuc_hex = st.color_picker("Nuclei colour", "#00FFFF") - myo_hex = st.color_picker("Myotube colour", "#FF0000") - alpha = st.slider("Overlay alpha", 0.0, 1.0, 0.45, 0.01) + help=( + "A nucleus is counted as fused if its MyHC signal is less than " + "this fraction of the surrounding cytoplasm ring signal. " + "Lower = stricter (fewer nuclei counted as fused). " + "0.85 is the value validated by Lair et al. 2026." + ), + disabled=not use_hole_method, + ) + ring_width_px = st.number_input( + "Cytoplasm ring width (px)", 2, 20, 6, 1, + help="Width of the ring around each nucleus used to measure local MyHC intensity.", + disabled=not use_hole_method, + ) + + st.header("Postprocessing") + min_nuc_area = st.number_input( + "Min nucleus area (px)", 0, 10000, defaults["min_nuc_area"], 1, + key=f"min_nuc_area_{preset_key}" + ) + min_myo_area = st.number_input( + "Min myotube area (px)", 0, 200000, defaults["min_myo_area"], 10, + key=f"min_myo_area_{preset_key}" + ) + nuc_close_radius = st.number_input( + "Nuclei close radius", 0, 50, defaults["nuc_close_radius"], 1, + key=f"nuc_close_radius_{preset_key}" + ) + myo_open_radius = st.number_input( + "Myotube open radius", 0, 50, defaults["myo_open_radius"], 1, + key=f"myo_open_radius_{preset_key}", + help="Opening removes small noise without merging separate myotubes. " + "Replaces the old closing radius which was merging adjacent myotubes.") + + st.header("Myotube separation") + st.caption( + "These controls break apart touching/bridged myotubes that would " + "otherwise be counted as a single object." + ) + myo_erode_radius = st.number_input( + "Myotube erode radius (px)", 0, 15, defaults["myo_erode_radius"], 1, + key=f"myo_erode_radius_{preset_key}", + help=( + "Erode + re-dilate breaks thin pixel bridges between adjacent " + "myotubes while preserving their overall size. " + "Start at 2 px; increase to 3–4 px for very dense cultures. " + "Set 0 to disable." + ) + ) + min_myo_aspect_ratio = st.number_input( + "Min myotube aspect ratio", 0.0, 10.0, defaults["min_myo_aspect_ratio"], 0.1, + key=f"min_myo_aspect_ratio_{preset_key}", + help=( + "Rejects round blobs (debris/artifacts) that are not real myotubes. " + "Myotubes are elongated (aspect ratio > 3). Round objects have ~1. " + "Set to 1.5–2.0 to filter false positives in sparse cultures. " + "Set to 0 to disable (default)." + ) + ) + myo_max_area_px = st.number_input( + "Max myotube area before split (px)", 0, 500000, defaults["myo_max_area_px"], 500, + key=f"myo_max_area_px_{preset_key}", + help=( + "Any connected myotube region larger than this is split using " + "nucleus-seeded watershed. Set to 0 to disable. " + "Increase for cultures with legitimately large single myotubes." + ) + ) + myo_split_min_seeds = st.number_input( + "Min nuclei seeds to split", 2, 20, defaults["myo_split_min_seeds"], 1, + key=f"myo_split_min_seeds_{preset_key}", + help=( + "Minimum nucleus centroids required before splitting a large region. " + "Set to 2 to split merged pairs; increase if single large myotubes " + "are being incorrectly split." + ) + ) + + st.header("Watershed (nuclei splitting)") + nuc_ws_min_dist = st.number_input("Min watershed distance", 1, 30, 3, 1) + nuc_ws_min_area = st.number_input("Min watershed area (px)", 1, 500, 6, 1) + + st.header("Overlay") + nuc_hex = st.color_picker("Nuclei colour", "#00FFFF") + myo_hex = st.color_picker("Myotube colour", "#FF0000") + alpha = st.slider("Overlay alpha", 0.0, 1.0, 0.45, 0.01) nuc_rgb = hex_to_rgb(nuc_hex) myo_rgb = hex_to_rgb(myo_hex) label_nuc = st.checkbox("Show nucleus IDs on overlay", value=False) @@ -1791,41 +1890,41 @@ with st.sidebar: label_myo = False st.header("Surface area") - px_um = st.number_input("Pixel size (µm) — set for real µm²", - value=1.0, min_value=0.01, step=0.01) - + px_um = st.number_input("Pixel size (µm) — set for real µm²", + value=1.0, min_value=0.01, step=0.01) + st.header("Active learning") enable_al = st.toggle("Enable correction upload", value=False) - - st.header("Privacy & Training") + + st.header("Privacy & Training") private_mode = st.toggle( "🔒 Private mode", value=True, - help=( - "When enabled, your images are processed locally only. " - "They are NOT added to the retraining queue, NOT saved to " - "corrections/, and NOT used for model improvement in any way. " - "Use this for unpublished data or sensitive research images." - ) - ) - if private_mode: - st.info( - "🔒 **Private mode ON** — your images will not be used for " - "training or stored beyond this session." - ) - - st.header("Metric definitions") - with st.expander("Fusion Index"): - st.write("100 × (nuclei in myotubes with ≥2 nuclei) / total nuclei") - with st.expander("MyHC-positive nucleus"): - st.write("Counted if ≥10% of nucleus pixels overlap a myotube.") - with st.expander("Surface area"): - st.write("Pixel count × px_um². Set pixel size for real µm² values.") - - -# ───────────────────────────────────────────────────────────────────────────── + help=( + "When enabled, your images are processed locally only. " + "They are NOT added to the retraining queue, NOT saved to " + "corrections/, and NOT used for model improvement in any way. " + "Use this for unpublished data or sensitive research images." + ) + ) + if private_mode: + st.info( + "🔒 **Private mode ON** — your images will not be used for " + "training or stored beyond this session." + ) + + st.header("Metric definitions") + with st.expander("Fusion Index"): + st.write("100 × (nuclei in myotubes with ≥2 nuclei) / total nuclei") + with st.expander("MyHC-positive nucleus"): + st.write("Counted if ≥10% of nucleus pixels overlap a myotube.") + with st.expander("Surface area"): + st.write("Pixel count × px_um². Set pixel size for real µm² values.") + + +# ───────────────────────────────────────────────────────────────────────────── # FILE UPLOADER -# ───────────────────────────────────────────────────────────────────────────── +# ───────────────────────────────────────────────────────────────────────────── _uploader_label = ( "Upload 1+ images (png / jpg / tif). Any filename is accepted. 🔒 Private mode is ON — images will not be stored." if private_mode else @@ -1861,7 +1960,7 @@ if uploads: if not uploads: st.info("👆 Upload one or more fluorescence images to get started.") st.stop() - + model = load_model(device=device) analysis_settings = { @@ -1895,13 +1994,13 @@ analysis_settings = { "pixel_size_um": float(px_um), "private_mode": bool(private_mode), } - -# ───────────────────────────────────────────────────────────────────────────── -# RUN ANALYSIS -# ───────────────────────────────────────────────────────────────────────────── -with st.form("run_form"): - run = st.form_submit_button("▶ Run / Rerun analysis", type="primary") - + +# ───────────────────────────────────────────────────────────────────────────── +# RUN ANALYSIS +# ───────────────────────────────────────────────────────────────────────────── +with st.form("run_form"): + run = st.form_submit_button("▶ Run / Rerun analysis", type="primary") + if run: results = [] artifacts = {} @@ -1923,32 +2022,32 @@ if run: original_stem = Path(original_filename).stem name = unique_sample_id(original_filename, used_sample_ids) rgb_u8 = np.array( - Image.open(io.BytesIO(up.getvalue())).convert("RGB"), - dtype=np.uint8 - ) - - ch1 = get_channel(rgb_u8, src1) # MyHC channel - ch2 = get_channel(rgb_u8, src2) # DAPI / nuclei channel - if inv1: ch1 = 255 - ch1 - if inv2: ch2 = 255 - ch2 - - # Keep the full-resolution MyHC channel for the cytoplasm-hole - # FI classifier — must be at original image resolution - myc_full = ch1.copy() # uint8, original resolution - - H = W = int(image_size) - x1 = resize_u8_to_float01(ch1, W, H, Image.BILINEAR) - x2 = resize_u8_to_float01(ch2, W, H, Image.BILINEAR) - x = np.stack([x1, x2], 0).astype(np.float32) - - x_t = torch.from_numpy(x).unsqueeze(0).to(device) - with torch.no_grad(): - probs = torch.sigmoid(model(x_t)).cpu().numpy()[0] - - # Confidence check — only queue for training if NOT in private mode - conf = float(np.mean([probs[0].max(), probs[1].max()])) - if conf < CONF_FLAG_THR: - low_conf_flags.append((name, conf)) + Image.open(io.BytesIO(up.getvalue())).convert("RGB"), + dtype=np.uint8 + ) + + ch1 = get_channel(rgb_u8, src1) # MyHC channel + ch2 = get_channel(rgb_u8, src2) # DAPI / nuclei channel + if inv1: ch1 = 255 - ch1 + if inv2: ch2 = 255 - ch2 + + # Keep the full-resolution MyHC channel for the cytoplasm-hole + # FI classifier — must be at original image resolution + myc_full = ch1.copy() # uint8, original resolution + + H = W = int(image_size) + x1 = resize_u8_to_float01(ch1, W, H, Image.BILINEAR) + x2 = resize_u8_to_float01(ch2, W, H, Image.BILINEAR) + x = np.stack([x1, x2], 0).astype(np.float32) + + x_t = torch.from_numpy(x).unsqueeze(0).to(device) + with torch.no_grad(): + probs = torch.sigmoid(model(x_t)).cpu().numpy()[0] + + # Confidence check — only queue for training if NOT in private mode + conf = float(np.mean([probs[0].max(), probs[1].max()])) + if conf < CONF_FLAG_THR: + low_conf_flags.append((name, conf)) if not private_mode: add_to_queue(rgb_u8, reason="low_confidence", metadata={ @@ -1956,33 +2055,33 @@ if run: "filename": original_filename, "sample_id": name, }) - - nuc_raw = (probs[0] > float(thr_nuc)).astype(np.uint8) - myo_raw = (probs[1] > float(thr_myo)).astype(np.uint8) - - nuc_pp, myo_pp = postprocess_masks( - nuc_raw, myo_raw, - min_nuc_area=int(min_nuc_area), - min_myo_area=int(min_myo_area), - nuc_close_radius=int(nuc_close_radius), - myo_open_radius=int(myo_open_radius), - myo_erode_radius=int(myo_erode_radius), - min_myo_aspect_ratio=float(min_myo_aspect_ratio), - ) - + + nuc_raw = (probs[0] > float(thr_nuc)).astype(np.uint8) + myo_raw = (probs[1] > float(thr_myo)).astype(np.uint8) + + nuc_pp, myo_pp = postprocess_masks( + nuc_raw, myo_raw, + min_nuc_area=int(min_nuc_area), + min_myo_area=int(min_myo_area), + nuc_close_radius=int(nuc_close_radius), + myo_open_radius=int(myo_open_radius), + myo_erode_radius=int(myo_erode_radius), + min_myo_aspect_ratio=float(min_myo_aspect_ratio), + ) + # Compute bio metrics FIRST so we know which myotubes are biological bio = compute_bio_metrics( nuc_pp, myo_pp, - myc_channel_full=myc_full if use_hole_method else None, - nuc_ws_min_distance=int(nuc_ws_min_dist), - nuc_ws_min_area=int(nuc_ws_min_area), - px_um=float(px_um), - ring_width=int(ring_width_px), - hole_ratio_thr=float(hole_ratio_thr), - ) - bio["fi_method"] = "cytoplasm-hole" if use_hole_method else "pixel-overlap" - per_areas = bio.pop("_per_myotube_areas", []) - bio_myo_ids = bio.pop("_bio_myo_ids", set()) + myc_channel_full=myc_full if use_hole_method else None, + nuc_ws_min_distance=int(nuc_ws_min_dist), + nuc_ws_min_area=int(nuc_ws_min_area), + px_um=float(px_um), + ring_width=int(ring_width_px), + hole_ratio_thr=float(hole_ratio_thr), + ) + bio["fi_method"] = "cytoplasm-hole" if use_hole_method else "pixel-overlap" + per_areas = bio.pop("_per_myotube_areas", []) + bio_myo_ids = bio.pop("_bio_myo_ids", set()) total_cc_count = bio.pop("_total_cc_count", 0) bio["image"] = name bio["source_filename"] = original_filename @@ -2057,9 +2156,9 @@ if run: zf.writestr(f"{name}/myotube_pp.png", myo_pp_bytes) zf.writestr(f"{name}/nuclei_raw.png", nuc_raw_bytes) zf.writestr(f"{name}/myotube_raw.png", myo_raw_bytes) - - prog.progress((i + 1) / len(uploads)) - + + prog.progress((i + 1) / len(uploads)) + df = pd.DataFrame(results).sort_values("image") zf.writestr("metrics.csv", df.to_csv(index=False).encode("utf-8")) zf.writestr("settings.json", json.dumps(analysis_settings, indent=2, default=str).encode("utf-8")) @@ -2073,22 +2172,22 @@ if run: ) st.session_state.analysis_errors = analysis_errors st.session_state.run_mode = run_mode - - if low_conf_flags: - names_str = ", ".join(f"{n} (conf={c:.2f})" for n, c in low_conf_flags) - st.markdown( - f"
⚠️ Low-confidence images auto-queued for retraining: " - f"{names_str}
", - unsafe_allow_html=True, - ) - -if st.session_state.df is None: - st.info("Click **▶ Run / Rerun analysis** to generate results.") - st.stop() - -# ───────────────────────────────────────────────────────────────────────────── -# RESULTS TABLE + DOWNLOADS -# ───────────────────────────────────────────────────────────────────────────── + + if low_conf_flags: + names_str = ", ".join(f"{n} (conf={c:.2f})" for n, c in low_conf_flags) + st.markdown( + f"
⚠️ Low-confidence images auto-queued for retraining: " + f"{names_str}
", + unsafe_allow_html=True, + ) + +if st.session_state.df is None: + st.info("Click **▶ Run / Rerun analysis** to generate results.") + st.stop() + +# ───────────────────────────────────────────────────────────────────────────── +# RESULTS TABLE + DOWNLOADS +# ───────────────────────────────────────────────────────────────────────────── st.subheader("Run summary") summary_cols = st.columns(4) summary_cols[0].metric("Images analyzed", len(st.session_state.df)) @@ -2128,155 +2227,155 @@ with c4: "Regenerates the overlay images in the ZIP using the current " "colour picker and alpha values from the sidebar." )): - import base64 as _b64_zip - new_zip_buf = io.BytesIO() - with zipfile.ZipFile(new_zip_buf, "w", compression=zipfile.ZIP_DEFLATED) as zf: - for img_name, art in st.session_state.artifacts.items(): - _r = art["rgb_u8"] - _nl = art["nuc_lab"] - _ml = art["myo_lab"] - _zn = np.zeros_like(_nl) - _zm = np.zeros_like(_ml) - ov_comb = make_coloured_overlay(_r, _nl, _ml, - alpha=float(alpha), - nuc_color=nuc_rgb, myo_color=myo_rgb) - ov_nuc = make_coloured_overlay(_r, _nl, _zm, - alpha=float(alpha), - nuc_color=nuc_rgb, myo_color=None) - ov_myo = make_coloured_overlay(_r, _zn, _ml, - alpha=float(alpha), - nuc_color=None, myo_color=myo_rgb) - simple = make_simple_overlay(_r, - (_nl > 0).astype(np.uint8), - (_ml > 0).astype(np.uint8), - nuc_rgb, myo_rgb, float(alpha)) - outline = make_outline_overlay(_r, _nl, _ml, - nuc_color=nuc_rgb, myo_color=(0, 255, 0), - line_width=2) - zf.writestr(f"{img_name}/overlay_combined.png", png_bytes(simple)) - zf.writestr(f"{img_name}/overlay_instance.png", png_bytes(ov_comb)) - zf.writestr(f"{img_name}/overlay_nuclei.png", png_bytes(ov_nuc)) - zf.writestr(f"{img_name}/overlay_myotubes.png", png_bytes(ov_myo)) - zf.writestr(f"{img_name}/overlay_outlines.png", png_bytes(outline)) + import base64 as _b64_zip + new_zip_buf = io.BytesIO() + with zipfile.ZipFile(new_zip_buf, "w", compression=zipfile.ZIP_DEFLATED) as zf: + for img_name, art in st.session_state.artifacts.items(): + _r = art["rgb_u8"] + _nl = art["nuc_lab"] + _ml = art["myo_lab"] + _zn = np.zeros_like(_nl) + _zm = np.zeros_like(_ml) + ov_comb = make_coloured_overlay(_r, _nl, _ml, + alpha=float(alpha), + nuc_color=nuc_rgb, myo_color=myo_rgb) + ov_nuc = make_coloured_overlay(_r, _nl, _zm, + alpha=float(alpha), + nuc_color=nuc_rgb, myo_color=None) + ov_myo = make_coloured_overlay(_r, _zn, _ml, + alpha=float(alpha), + nuc_color=None, myo_color=myo_rgb) + simple = make_simple_overlay(_r, + (_nl > 0).astype(np.uint8), + (_ml > 0).astype(np.uint8), + nuc_rgb, myo_rgb, float(alpha)) + outline = make_outline_overlay(_r, _nl, _ml, + nuc_color=nuc_rgb, myo_color=(0, 255, 0), + line_width=2) + zf.writestr(f"{img_name}/overlay_combined.png", png_bytes(simple)) + zf.writestr(f"{img_name}/overlay_instance.png", png_bytes(ov_comb)) + zf.writestr(f"{img_name}/overlay_nuclei.png", png_bytes(ov_nuc)) + zf.writestr(f"{img_name}/overlay_myotubes.png", png_bytes(ov_myo)) + zf.writestr(f"{img_name}/overlay_outlines.png", png_bytes(outline)) zf.writestr(f"{img_name}/nuclei_pp.png", png_bytes((art["nuc_pp_arr"] > 0).astype(np.uint8) * 255)) zf.writestr(f"{img_name}/myotube_pp.png", png_bytes((art["myo_pp_arr"] > 0).astype(np.uint8) * 255)) - zf.writestr(f"{img_name}/nuclei_raw.png", art["nuc_raw_bytes"]) - zf.writestr(f"{img_name}/myotube_raw.png", art["myo_raw_bytes"]) - df_cols = [c for c in st.session_state.df.columns if not c.startswith("_")] - zf.writestr("metrics.csv", st.session_state.df[df_cols].to_csv(index=False).encode()) - st.session_state.zip_bytes = new_zip_buf.getvalue() - st.success("ZIP rebuilt with current colours. Click Download results.zip above to save.") - + zf.writestr(f"{img_name}/nuclei_raw.png", art["nuc_raw_bytes"]) + zf.writestr(f"{img_name}/myotube_raw.png", art["myo_raw_bytes"]) + df_cols = [c for c in st.session_state.df.columns if not c.startswith("_")] + zf.writestr("metrics.csv", st.session_state.df[df_cols].to_csv(index=False).encode()) + st.session_state.zip_bytes = new_zip_buf.getvalue() + st.success("ZIP rebuilt with current colours. Click Download results.zip above to save.") + st.divider() -# ───────────────────────────────────────────────────────────────────────────── +# ───────────────────────────────────────────────────────────────────────────── if (st.session_state.run_mode or run_mode) != "Review": st.info("Batch run complete. Switch Run mode to Review and rerun if you want overlays, per-image tabs, and live preview.") st.stop() # PER-IMAGE PREVIEW + ANIMATED METRICS -# ───────────────────────────────────────────────────────────────────────────── -st.subheader("🖼️ Image preview & live metrics") -names = list(st.session_state.artifacts.keys()) -pick = st.selectbox("Select image", names) - -col_img, col_metrics = st.columns([3, 2], gap="large") - -with col_img: - tabs = st.tabs([ - "🔵 Combined", - "📐 Nuclei outlines", - "📐 Myotube outlines", - "🟣 Nuclei only", - "🟠 Myotubes only", - "📷 Original", - "⬜ Nuclei mask", - "⬜ Myotube mask", - ]) - art = st.session_state.artifacts[pick] - bio_cur = st.session_state.bio_metrics.get(pick, {}) - lpos = art["label_positions"] - iw = art["img_w"] - ih = art["img_h"] - - # Build coloured overlays RIGHT NOW using the current sidebar colour / alpha. - # This means changing colour picker or alpha slider instantly updates the - # viewer — no rerun needed for display changes. - import base64 as _b64_disp - def _b64png_disp(arr): - return _b64_disp.b64encode(png_bytes(arr)).decode() - - _rgb = art["rgb_u8"] - _nl = art["nuc_lab"] - _ml = art["myo_lab"] - _zero_nuc = np.zeros_like(_nl) - _zero_myo = np.zeros_like(_ml) - - inst_b64 = _b64png_disp(make_coloured_overlay(_rgb, _nl, _ml, - alpha=float(alpha), - nuc_color=nuc_rgb, myo_color=myo_rgb)) - nuc_only_b64 = _b64png_disp(make_coloured_overlay(_rgb, _nl, _zero_myo, - alpha=float(alpha), - nuc_color=nuc_rgb, myo_color=None)) - myo_only_b64 = _b64png_disp(make_coloured_overlay(_rgb, _zero_nuc, _ml, - alpha=float(alpha), - nuc_color=None, myo_color=myo_rgb)) - - with tabs[0]: - html_combined = make_svg_viewer( - inst_b64, iw, ih, lpos, - show_nuclei=True, show_myotubes=True, - ) - st.components.v1.html(html_combined, height=680, scrolling=False) - - with tabs[1]: - # Nuclei-only outlines - nuc_outline_img = make_outline_overlay( - _rgb, _nl, np.zeros_like(_ml), - nuc_color=nuc_rgb, myo_color=(0, 255, 0), - line_width=2, - ) - nuc_outline_b64 = _b64png_disp(nuc_outline_img) - nuc_outline_lpos = {"nuclei": lpos["nuclei"], "myotubes": [], "myotubes_nonbio": []} - html_nuc_outline = make_svg_viewer( - nuc_outline_b64, iw, ih, nuc_outline_lpos, - show_nuclei=True, show_myotubes=False, - ) - st.components.v1.html(html_nuc_outline, height=680, scrolling=False) - - with tabs[2]: - # Myotube-only outlines - myo_outline_img = make_outline_overlay( - _rgb, np.zeros_like(_nl), _ml, - nuc_color=nuc_rgb, myo_color=(0, 255, 0), - line_width=2, - ) - myo_outline_b64 = _b64png_disp(myo_outline_img) - myo_outline_lpos = {"nuclei": [], "myotubes": lpos["myotubes"], - "myotubes_nonbio": lpos.get("myotubes_nonbio", [])} - html_myo_outline = make_svg_viewer( - myo_outline_b64, iw, ih, myo_outline_lpos, - show_nuclei=False, show_myotubes=True, - ) - st.components.v1.html(html_myo_outline, height=680, scrolling=False) - - with tabs[3]: - nuc_only_lpos = {"nuclei": lpos["nuclei"], "myotubes": [], "myotubes_nonbio": []} - html_nuc = make_svg_viewer( - nuc_only_b64, iw, ih, nuc_only_lpos, - show_nuclei=True, show_myotubes=False, - ) - st.components.v1.html(html_nuc, height=680, scrolling=False) - - with tabs[4]: - myo_only_lpos = {"nuclei": [], "myotubes": lpos["myotubes"], - "myotubes_nonbio": lpos.get("myotubes_nonbio", [])} - html_myo = make_svg_viewer( - myo_only_b64, iw, ih, myo_only_lpos, - show_nuclei=False, show_myotubes=True, - ) - st.components.v1.html(html_myo, height=680, scrolling=False) - +# ───────────────────────────────────────────────────────────────────────────── +st.subheader("🖼️ Image preview & live metrics") +names = list(st.session_state.artifacts.keys()) +pick = st.selectbox("Select image", names) + +col_img, col_metrics = st.columns([3, 2], gap="large") + +with col_img: + tabs = st.tabs([ + "🔵 Combined", + "📐 Nuclei outlines", + "📐 Myotube outlines", + "🟣 Nuclei only", + "🟠 Myotubes only", + "📷 Original", + "⬜ Nuclei mask", + "⬜ Myotube mask", + ]) + art = st.session_state.artifacts[pick] + bio_cur = st.session_state.bio_metrics.get(pick, {}) + lpos = art["label_positions"] + iw = art["img_w"] + ih = art["img_h"] + + # Build coloured overlays RIGHT NOW using the current sidebar colour / alpha. + # This means changing colour picker or alpha slider instantly updates the + # viewer — no rerun needed for display changes. + import base64 as _b64_disp + def _b64png_disp(arr): + return _b64_disp.b64encode(png_bytes(arr)).decode() + + _rgb = art["rgb_u8"] + _nl = art["nuc_lab"] + _ml = art["myo_lab"] + _zero_nuc = np.zeros_like(_nl) + _zero_myo = np.zeros_like(_ml) + + inst_b64 = _b64png_disp(make_coloured_overlay(_rgb, _nl, _ml, + alpha=float(alpha), + nuc_color=nuc_rgb, myo_color=myo_rgb)) + nuc_only_b64 = _b64png_disp(make_coloured_overlay(_rgb, _nl, _zero_myo, + alpha=float(alpha), + nuc_color=nuc_rgb, myo_color=None)) + myo_only_b64 = _b64png_disp(make_coloured_overlay(_rgb, _zero_nuc, _ml, + alpha=float(alpha), + nuc_color=None, myo_color=myo_rgb)) + + with tabs[0]: + html_combined = make_svg_viewer( + inst_b64, iw, ih, lpos, + show_nuclei=True, show_myotubes=True, + ) + st.components.v1.html(html_combined, height=680, scrolling=False) + + with tabs[1]: + # Nuclei-only outlines + nuc_outline_img = make_outline_overlay( + _rgb, _nl, np.zeros_like(_ml), + nuc_color=nuc_rgb, myo_color=(0, 255, 0), + line_width=2, + ) + nuc_outline_b64 = _b64png_disp(nuc_outline_img) + nuc_outline_lpos = {"nuclei": lpos["nuclei"], "myotubes": [], "myotubes_nonbio": []} + html_nuc_outline = make_svg_viewer( + nuc_outline_b64, iw, ih, nuc_outline_lpos, + show_nuclei=True, show_myotubes=False, + ) + st.components.v1.html(html_nuc_outline, height=680, scrolling=False) + + with tabs[2]: + # Myotube-only outlines + myo_outline_img = make_outline_overlay( + _rgb, np.zeros_like(_nl), _ml, + nuc_color=nuc_rgb, myo_color=(0, 255, 0), + line_width=2, + ) + myo_outline_b64 = _b64png_disp(myo_outline_img) + myo_outline_lpos = {"nuclei": [], "myotubes": lpos["myotubes"], + "myotubes_nonbio": lpos.get("myotubes_nonbio", [])} + html_myo_outline = make_svg_viewer( + myo_outline_b64, iw, ih, myo_outline_lpos, + show_nuclei=False, show_myotubes=True, + ) + st.components.v1.html(html_myo_outline, height=680, scrolling=False) + + with tabs[3]: + nuc_only_lpos = {"nuclei": lpos["nuclei"], "myotubes": [], "myotubes_nonbio": []} + html_nuc = make_svg_viewer( + nuc_only_b64, iw, ih, nuc_only_lpos, + show_nuclei=True, show_myotubes=False, + ) + st.components.v1.html(html_nuc, height=680, scrolling=False) + + with tabs[4]: + myo_only_lpos = {"nuclei": [], "myotubes": lpos["myotubes"], + "myotubes_nonbio": lpos.get("myotubes_nonbio", [])} + html_myo = make_svg_viewer( + myo_only_b64, iw, ih, myo_only_lpos, + show_nuclei=False, show_myotubes=True, + ) + st.components.v1.html(html_myo, height=680, scrolling=False) + with tabs[5]: st.image(art["rgb_u8"], use_container_width=True) with tabs[6]: @@ -2289,98 +2388,98 @@ with col_img: if int(myo_mask_view.max()) == 0: st.warning("The myotube mask is empty for this image.") st.image(myo_mask_view, clamp=True, use_container_width=True) - -with col_metrics: - st.markdown("#### 📊 Live metrics") - bio = st.session_state.bio_metrics.get(pick, {}) - per_areas = bio.get("_per_myotube_areas", []) - - r1c1, r1c2, r1c3 = st.columns(3) - r2c1, r2c2, r2c3 = st.columns(3) - r3c1, r3c2, r3c3 = st.columns(3) - - placeholders = { - "total_nuclei" : r1c1.empty(), - "myotube_count" : r1c2.empty(), - "myHC_positive_nuclei" : r1c3.empty(), - "myHC_positive_percentage": r2c1.empty(), - "fusion_index" : r2c2.empty(), - "avg_nuclei_per_myotube" : r2c3.empty(), - "total_area_um2" : r3c1.empty(), - "mean_area_um2" : r3c2.empty(), - "max_area_um2" : r3c3.empty(), - } - - specs = [ - ("total_nuclei", "Total nuclei", "#4fc3f7", False), - ("myotube_count", "Myotubes", "#ff8a65", False), - ("myHC_positive_nuclei", "MyHC⁺ nuclei", "#a5d6a7", False), - ("myHC_positive_percentage", "MyHC⁺ %", "#ce93d8", True), - ("fusion_index", "Fusion index %", "#80cbc4", True), - ("avg_nuclei_per_myotube", "Avg nuc/myotube", "#80deea", True), - ("total_area_um2", f"Total area (µm²)", "#fff176", True), - ("mean_area_um2", f"Mean area (µm²)", "#ffcc80", True), - ("max_area_um2", f"Max area (µm²)", "#ef9a9a", True), - ] - - for key, label, color, is_float in specs: - val = bio.get(key, 0) - animated_metric(placeholders[key], label, - float(val) if is_float else int(val), - color=color) - - if per_areas: - st.markdown("#### 📐 Per-myotube area") - area_df = pd.DataFrame({ - "Myotube" : [f"M{i+1}" for i in range(len(per_areas))], - f"Area (µm²)" : per_areas, - }).set_index("Myotube") - st.bar_chart(area_df, height=220) - -st.divider() - -# ───────────────────────────────────────────────────────────────────────────── -# TRAINING CONTRIBUTION — User-initiated parameter + image submission -# ───────────────────────────────────────────────────────────────────────────── -if not private_mode and names: - st.subheader("📤 Submit image for training") - st.caption( - "Once you've tuned the sidebar parameters to get the best results for " - "this image, you can download the current image/mask batch or submit " - "the parameter snapshot. FIJI-corrected masks submitted below are " - "used as supervised correction data." - ) - - st.download_button( - "Download all current masks as ZIP", - data=build_mask_batch_zip(names, st.session_state.artifacts), - file_name=f"myoseg_current_masks_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip", - mime="application/zip", - key="download_current_masks_batch", - help="Downloads every analyzed image with the current postprocessed nuclei and myotube masks.", - ) - - train_pick = st.selectbox("Image to submit", names, key="train_pick") - - if st.button("📤 Submit for training", type="primary"): - _ensure_dirs() - ts = datetime.now().strftime("%Y%m%d_%H%M%S_%f") - folder = CORRECTIONS_DIR / f"params_{ts}" - folder.mkdir(parents=True, exist_ok=True) - - # Save the original image - train_art = st.session_state.artifacts[train_pick] - Image.fromarray(train_art["rgb_u8"]).save(folder / "image.png") - - # Save the postprocessed masks (from current parameter settings) - nuc_pp_arr = train_art.get("nuc_pp_arr") - myo_pp_arr = train_art.get("myo_pp_arr") - if nuc_pp_arr is not None: - Image.fromarray((nuc_pp_arr > 0).astype(np.uint8) * 255).save(folder / "nuclei_mask.png") - if myo_pp_arr is not None: - Image.fromarray((myo_pp_arr > 0).astype(np.uint8) * 255).save(folder / "myotube_mask.png") - - # Save the current parameter set — self_train can learn from these + +with col_metrics: + st.markdown("#### 📊 Live metrics") + bio = st.session_state.bio_metrics.get(pick, {}) + per_areas = bio.get("_per_myotube_areas", []) + + r1c1, r1c2, r1c3 = st.columns(3) + r2c1, r2c2, r2c3 = st.columns(3) + r3c1, r3c2, r3c3 = st.columns(3) + + placeholders = { + "total_nuclei" : r1c1.empty(), + "myotube_count" : r1c2.empty(), + "myHC_positive_nuclei" : r1c3.empty(), + "myHC_positive_percentage": r2c1.empty(), + "fusion_index" : r2c2.empty(), + "avg_nuclei_per_myotube" : r2c3.empty(), + "total_area_um2" : r3c1.empty(), + "mean_area_um2" : r3c2.empty(), + "max_area_um2" : r3c3.empty(), + } + + specs = [ + ("total_nuclei", "Total nuclei", "#4fc3f7", False), + ("myotube_count", "Myotubes", "#ff8a65", False), + ("myHC_positive_nuclei", "MyHC⁺ nuclei", "#a5d6a7", False), + ("myHC_positive_percentage", "MyHC⁺ %", "#ce93d8", True), + ("fusion_index", "Fusion index %", "#80cbc4", True), + ("avg_nuclei_per_myotube", "Avg nuc/myotube", "#80deea", True), + ("total_area_um2", f"Total area (µm²)", "#fff176", True), + ("mean_area_um2", f"Mean area (µm²)", "#ffcc80", True), + ("max_area_um2", f"Max area (µm²)", "#ef9a9a", True), + ] + + for key, label, color, is_float in specs: + val = bio.get(key, 0) + animated_metric(placeholders[key], label, + float(val) if is_float else int(val), + color=color) + + if per_areas: + st.markdown("#### 📐 Per-myotube area") + area_df = pd.DataFrame({ + "Myotube" : [f"M{i+1}" for i in range(len(per_areas))], + f"Area (µm²)" : per_areas, + }).set_index("Myotube") + st.bar_chart(area_df, height=220) + +st.divider() + +# ───────────────────────────────────────────────────────────────────────────── +# TRAINING CONTRIBUTION — User-initiated parameter + image submission +# ───────────────────────────────────────────────────────────────────────────── +if not private_mode and names: + st.subheader("📤 Submit image for training") + st.caption( + "Once you've tuned the sidebar parameters to get the best results for " + "this image, you can download the current image/mask batch or submit " + "the parameter snapshot. FIJI-corrected masks submitted below are " + "used as supervised correction data." + ) + + st.download_button( + "Download all current masks as ZIP", + data=build_mask_batch_zip(names, st.session_state.artifacts), + file_name=f"myoseg_current_masks_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip", + mime="application/zip", + key="download_current_masks_batch", + help="Downloads every analyzed image with the current postprocessed nuclei and myotube masks.", + ) + + train_pick = st.selectbox("Image to submit", names, key="train_pick") + + if st.button("📤 Submit for training", type="primary"): + _ensure_dirs() + ts = datetime.now().strftime("%Y%m%d_%H%M%S_%f") + folder = CORRECTIONS_DIR / f"params_{ts}" + folder.mkdir(parents=True, exist_ok=True) + + # Save the original image + train_art = st.session_state.artifacts[train_pick] + Image.fromarray(train_art["rgb_u8"]).save(folder / "image.png") + + # Save the postprocessed masks (from current parameter settings) + nuc_pp_arr = train_art.get("nuc_pp_arr") + myo_pp_arr = train_art.get("myo_pp_arr") + if nuc_pp_arr is not None: + Image.fromarray((nuc_pp_arr > 0).astype(np.uint8) * 255).save(folder / "nuclei_mask.png") + if myo_pp_arr is not None: + Image.fromarray((myo_pp_arr > 0).astype(np.uint8) * 255).save(folder / "myotube_mask.png") + + # Save the current parameter set — self_train can learn from these param_snapshot = { "reason": "user_optimized_params", "has_masks": nuc_pp_arr is not None and myo_pp_arr is not None, @@ -2389,179 +2488,181 @@ if not private_mode and names: "sample_id": train_art.get("sample_id", train_pick), "original_filename": train_art.get("original_filename", train_pick), "parameters": { - "thr_nuc": float(thr_nuc), - "thr_myo": float(thr_myo), - "min_nuc_area": int(min_nuc_area), - "min_myo_area": int(min_myo_area), - "nuc_close_radius": int(nuc_close_radius), - "myo_open_radius": int(myo_open_radius), - "myo_erode_radius": int(myo_erode_radius), - "min_myo_aspect_ratio": float(min_myo_aspect_ratio), - "myo_max_area_px": int(myo_max_area_px), - "myo_split_min_seeds": int(myo_split_min_seeds), - "image_size": int(image_size), - }, - "metrics": st.session_state.bio_metrics.get(train_pick, {}), - } - (folder / "meta.json").write_text(json.dumps(param_snapshot, indent=2, default=str)) - - st.success( - f"✅ **{train_pick}** submitted for training with your optimized parameters. " - "The safe retraining script stores these settings for parameter review; only FIJI-corrected masks are used as ground-truth labels by default." - ) - -# ───────────────────────────────────────────────────────────────────────────── -# ACTIVE LEARNING — CORRECTION UPLOAD -# ───────────────────────────────────────────────────────────────────────────── -if enable_al and not private_mode: - st.subheader("🧠 Submit corrected labels (Active Learning)") - flash_msg = st.session_state.pop("correction_flash", None) - if flash_msg: - st.success(flash_msg) - st.caption( - "Download predicted masks, correct them in ImageJ/FIJI, napari, or any " - "image editor (white = foreground, black = background), then upload " - "one corrected pair or many corrected pairs below." - ) - - al_pick = st.selectbox("Correct masks for image", names, key="al_pick") - if "correction_upload_reset_id" not in st.session_state: - st.session_state.correction_upload_reset_id = 0 - - # Download buttons for current masks — lets users edit and re-upload - if al_pick in st.session_state.artifacts: - al_art = st.session_state.artifacts[al_pick] - dl1, dl2 = st.columns(2) - with dl1: - nuc_pp = al_art.get("nuc_pp_arr") - if nuc_pp is not None: - st.download_button( - "⬇️ Download nuclei mask", - data=png_bytes((nuc_pp * 255).astype(np.uint8)), - file_name=f"{al_pick}_nuclei_mask.png", - mime="image/png", - key="dl_nuc_mask", - ) - else: - st.download_button( - "⬇️ Download nuclei mask", - data=al_art["nuc_pp"], - file_name=f"{al_pick}_nuclei_mask.png", - mime="image/png", - key="dl_nuc_mask", - ) - with dl2: - myo_pp = al_art.get("myo_pp_arr") - if myo_pp is not None: - st.download_button( - "⬇️ Download myotube mask", - data=png_bytes((myo_pp * 255).astype(np.uint8)), - file_name=f"{al_pick}_myotube_mask.png", - mime="image/png", - key="dl_myo_mask", - ) - else: - st.download_button( - "⬇️ Download myotube mask", - data=al_art["myo_pp"], - file_name=f"{al_pick}_myotube_mask.png", - mime="image/png", - key="dl_myo_mask", - ) - - clear_col, _ = st.columns([1, 3]) - with clear_col: - if st.button( - "Remove uploaded masks", - help=( - "Clears masks currently sitting in the upload boxes. " - "This does not delete corrections that were already submitted." - ), - key="clear_pending_correction_uploads", - ): - st.session_state.correction_upload_reset_id += 1 - st.rerun() - - upload_key_suffix = f"{al_pick}_{st.session_state.correction_upload_reset_id}" - acol1, acol2 = st.columns(2) - with acol1: - corr_nuc = st.file_uploader( - "Upload corrected NUCLEI mask(s)", - type=["png", "tif", "tiff"], - accept_multiple_files=True, - key=f"nuc_corr_{upload_key_suffix}", - help="Upload one nuclei mask for the selected image, or select many nuclei masks for batch correction.", - ) - with acol2: - corr_myo = st.file_uploader( - "Upload corrected MYOTUBE mask(s)", - type=["png", "tif", "tiff"], - accept_multiple_files=True, - key=f"myo_corr_{upload_key_suffix}", - help="Upload the matching myotube mask(s). Batch files are paired by filename stem.", - ) - - if st.button("✅ Submit corrections", type="primary"): - if not corr_nuc or not corr_myo: - st.error("Please upload BOTH nuclei and myotube mask files.") - else: - n_submitted, correction_errors = submit_uploaded_correction_pairs( - corr_nuc, corr_myo, st.session_state.artifacts, al_pick - ) - if n_submitted == 1: - st.session_state.correction_flash = "Saved 1 corrected image pair to corrections/." - elif n_submitted > 1: - st.session_state.correction_flash = f"Saved {n_submitted} corrected image pairs to corrections/." - if correction_errors: - st.warning("Some files were skipped:\n" + "\n".join(f"- {e}" for e in correction_errors[:12])) - if not n_submitted and not correction_errors: - st.error("No matching correction pairs were found.") - if n_submitted: - st.session_state.correction_upload_reset_id += 1 - st.rerun() - -elif enable_al and private_mode: - st.info( - "🔒 Active learning and training submissions are disabled in Private mode. " - "Toggle off Private mode in the sidebar to enable." - ) - -st.divider() - -# ───────────────────────────────────────────────────────────────────────────── -# RETRAINING QUEUE STATUS -# ───────────────────────────────────────────────────────────────────────────── -with st.expander("🔧 Self-training queue status"): - _ensure_dirs() - q_items = list(QUEUE_DIR.glob("*.json")) - c_items = list(CORRECTIONS_DIR.glob("*/meta.json")) - - sq1, sq2 = st.columns(2) - sq1.metric("Images in retraining queue", len(q_items)) - sq2.metric("Corrected label pairs", len(c_items)) - - if q_items: - reasons = {} - for p in q_items: - try: - r = json.loads(p.read_text()).get("reason", "unknown") - reasons[r] = reasons.get(r, 0) + 1 - except Exception: - pass - st.write("Queue breakdown:", reasons) - - manifest = Path("manifest.json") - if manifest.exists(): - try: - history = json.loads(manifest.read_text()) - if history: - st.markdown("**Last 5 retraining runs:**") - hist_df = pd.DataFrame(history[-5:]) - st.dataframe(hist_df, use_container_width=True) - except Exception: - pass - - if st.button("🔄 Trigger retraining now"): - import subprocess - subprocess.Popen(["python", "self_train.py", "--manual"]) - st.info("Retraining started in the background. Check terminal / logs for progress.") + "thr_nuc": float(thr_nuc), + "thr_myo": float(thr_myo), + "min_nuc_area": int(min_nuc_area), + "min_myo_area": int(min_myo_area), + "nuc_close_radius": int(nuc_close_radius), + "myo_open_radius": int(myo_open_radius), + "myo_erode_radius": int(myo_erode_radius), + "min_myo_aspect_ratio": float(min_myo_aspect_ratio), + "myo_max_area_px": int(myo_max_area_px), + "myo_split_min_seeds": int(myo_split_min_seeds), + "image_size": int(image_size), + }, + "metrics": st.session_state.bio_metrics.get(train_pick, {}), + } + (folder / "meta.json").write_text(json.dumps(param_snapshot, indent=2, default=str)) + + st.success( + f"✅ **{train_pick}** submitted for training with your optimized parameters. " + "The safe retraining script stores these settings for parameter review; only FIJI-corrected masks are used as ground-truth labels by default." + ) + +# ───────────────────────────────────────────────────────────────────────────── +# ACTIVE LEARNING — CORRECTION UPLOAD +# ───────────────────────────────────────────────────────────────────────────── +if enable_al and not private_mode: + st.subheader("🧠 Submit corrected labels (Active Learning)") + flash_msg = st.session_state.pop("correction_flash", None) + if flash_msg: + st.success(flash_msg) + st.caption( + "Download predicted masks, correct them in ImageJ/FIJI, napari, or any " + "image editor (white = foreground, black = background), then upload " + "corrected masks below. **Upload either or both** — whichever you corrected. " + "The uploaded mask will **immediately replace** the model's prediction and " + "update the displayed counts and overlays." + ) + + al_pick = st.selectbox("Correct masks for image", names, key="al_pick") + if "correction_upload_reset_id" not in st.session_state: + st.session_state.correction_upload_reset_id = 0 + + # Download buttons for current masks — lets users edit and re-upload + if al_pick in st.session_state.artifacts: + al_art = st.session_state.artifacts[al_pick] + dl1, dl2 = st.columns(2) + with dl1: + nuc_pp = al_art.get("nuc_pp_arr") + if nuc_pp is not None: + st.download_button( + "⬇️ Download nuclei mask", + data=png_bytes((nuc_pp * 255).astype(np.uint8)), + file_name=f"{al_pick}_nuclei_mask.png", + mime="image/png", + key="dl_nuc_mask", + ) + else: + st.download_button( + "⬇️ Download nuclei mask", + data=al_art["nuc_pp"], + file_name=f"{al_pick}_nuclei_mask.png", + mime="image/png", + key="dl_nuc_mask", + ) + with dl2: + myo_pp = al_art.get("myo_pp_arr") + if myo_pp is not None: + st.download_button( + "⬇️ Download myotube mask", + data=png_bytes((myo_pp * 255).astype(np.uint8)), + file_name=f"{al_pick}_myotube_mask.png", + mime="image/png", + key="dl_myo_mask", + ) + else: + st.download_button( + "⬇️ Download myotube mask", + data=al_art["myo_pp"], + file_name=f"{al_pick}_myotube_mask.png", + mime="image/png", + key="dl_myo_mask", + ) + + clear_col, _ = st.columns([1, 3]) + with clear_col: + if st.button( + "Remove uploaded masks", + help=( + "Clears masks currently sitting in the upload boxes. " + "This does not delete corrections that were already submitted." + ), + key="clear_pending_correction_uploads", + ): + st.session_state.correction_upload_reset_id += 1 + st.rerun() + + upload_key_suffix = f"{al_pick}_{st.session_state.correction_upload_reset_id}" + acol1, acol2 = st.columns(2) + with acol1: + corr_myo = st.file_uploader( + "Upload corrected MYOTUBE mask(s)", + type=["png", "tif", "tiff"], + accept_multiple_files=True, + key=f"myo_corr_{upload_key_suffix}", + help="Upload corrected myotube mask(s). Can be uploaded alone without nuclei masks.", + ) + with acol2: + corr_nuc = st.file_uploader( + "Upload corrected NUCLEI mask(s)", + type=["png", "tif", "tiff"], + accept_multiple_files=True, + key=f"nuc_corr_{upload_key_suffix}", + help="Upload corrected nuclei mask(s). Can be uploaded alone without myotube masks.", + ) + + if st.button("✅ Submit corrections", type="primary"): + if not corr_myo and not corr_nuc: + st.error("Please upload at least one corrected mask (myotube, nuclei, or both).") + else: + n_submitted, correction_errors = submit_uploaded_correction_pairs( + corr_nuc, corr_myo, st.session_state.artifacts, al_pick + ) + if n_submitted == 1: + st.session_state.correction_flash = "Saved 1 corrected mask to corrections/. Results updated with your correction." + elif n_submitted > 1: + st.session_state.correction_flash = f"Saved {n_submitted} corrected masks to corrections/. Results updated." + if correction_errors: + st.warning("Some files were skipped:\n" + "\n".join(f"- {e}" for e in correction_errors[:12])) + if not n_submitted and not correction_errors: + st.error("No matching correction pairs were found.") + if n_submitted: + st.session_state.correction_upload_reset_id += 1 + st.rerun() + +elif enable_al and private_mode: + st.info( + "🔒 Active learning and training submissions are disabled in Private mode. " + "Toggle off Private mode in the sidebar to enable." + ) + +st.divider() + +# ───────────────────────────────────────────────────────────────────────────── +# RETRAINING QUEUE STATUS +# ───────────────────────────────────────────────────────────────────────────── +with st.expander("🔧 Self-training queue status"): + _ensure_dirs() + q_items = list(QUEUE_DIR.glob("*.json")) + c_items = list(CORRECTIONS_DIR.glob("*/meta.json")) + + sq1, sq2 = st.columns(2) + sq1.metric("Images in retraining queue", len(q_items)) + sq2.metric("Corrected label pairs", len(c_items)) + + if q_items: + reasons = {} + for p in q_items: + try: + r = json.loads(p.read_text()).get("reason", "unknown") + reasons[r] = reasons.get(r, 0) + 1 + except Exception: + pass + st.write("Queue breakdown:", reasons) + + manifest = Path("manifest.json") + if manifest.exists(): + try: + history = json.loads(manifest.read_text()) + if history: + st.markdown("**Last 5 retraining runs:**") + hist_df = pd.DataFrame(history[-5:]) + st.dataframe(hist_df, use_container_width=True) + except Exception: + pass + + if st.button("🔄 Trigger retraining now"): + import subprocess + subprocess.Popen(["python", "self_train.py", "--manual"]) + st.info("Retraining started in the background. Check terminal / logs for progress.") \ No newline at end of file