itasc.segmentation.cell_divergence_segmentation¶
Unary-only cell segmentation from cached divergence maps.
Chains the simplified cell pipeline validated in
scripts/experiment_divergence_icm.py into a single, Qt-free helper so the
cell widget can drive both its live single-frame preview and its full-stack run
through the same code path:
- Map cleanup — local-mean residual + threshold on foreground and
contours (the nucleus/atom
residualscheme applied symmetrically to both maps).
- Temporal smooth — bidirectional signal-adaptive EMA on the cleaned
contours (full-stack only; needs the whole movie).
Foreground mask —
(foreground_clean > fg_threshold) | (nucleus > 0).
- Segmentation — unary-only geodesic Voronoi:
initialize_icmassignseach foreground pixel to its nearest nucleus seed through the contour-aware cost field (per-pixel argmin of the unary).
The divergence maps themselves (cell_contours.tif + cell_foreground.tif)
are produced upstream by DivergenceMapsWidget; this helper only consumes
them.
The returned CellDivergenceResult carries every intermediate plus the
weighted cost field, so the widget can drop each into a preview layer without
recomputing.
Functions
|
Full-stack contour cleanup + temporal smoothing — pipeline stages 1+2. |
|
Foreground cleanup + fill mask, independent of contours and the geodesic. |
|
Run the unary-only divergence pipeline and return all intermediates. |
Classes
|
Parameters for the unary-only divergence cell pipeline. |
|
All pipeline intermediates plus the final labels. |
|
Foreground-path intermediates — pipeline stages 1 + 3, foreground only. |
- class itasc.segmentation.cell_divergence_segmentation.CellDivergenceParams(fg_window=51, fg_strength=0.0, fg_threshold=0.1, contour_window=51, contour_strength=1.0, contour_threshold=0.0, contour_norm_pct=99.0, memory_tau=0.0, memory_floor=0.01, balance=0.98, feature_strength=100.0, n_workers=4)[source]¶
Bases:
objectParameters for the unary-only divergence cell pipeline.
Defaults match the values that held across pos00/pos01 in the prototype.
- Parameters:
- contour_strength: float = 1.0¶
0 = raw, 1 = full local-mean subtraction.
- Type:
Contour residual strength
- class itasc.segmentation.cell_divergence_segmentation.CellDivergenceResult(foreground_raw, foreground_clean, contours_raw, contours_clean, foreground_mask, cost_field, labels)[source]¶
Bases:
objectAll pipeline intermediates plus the final labels.
Arrays are
(T, Y, X)for a full-stack run and(Y, X)for a single-frame (framegiven) run.- Parameters:
- class itasc.segmentation.cell_divergence_segmentation.CellForegroundResult(foreground_raw, foreground_clean, foreground_mask)[source]¶
Bases:
objectForeground-path intermediates — pipeline stages 1 + 3, foreground only.
Arrays are
(T, Y, X)for a full-stack run and(Y, X)for a single-frame (framegiven) run.
- itasc.segmentation.cell_divergence_segmentation.clean_and_smooth_contours(contours, params)[source]¶
Full-stack contour cleanup + temporal smoothing — pipeline stages 1+2.
Returns the
(T, Y, X)cleaned (residual → global-percentile normalize → floor) and, whenmemory_tau > 0and there is more than one frame, temporally smoothed contour stack — exactly thecontours_cleanthe full run feeds the segmenter.The widget’s live preview computes this once over the whole movie, caches it, and slices the current frame back into
segment_cells_divergence()viacontours_clean_overrideso the previewed cost field / labels for a frame match the full run (which the per-frame path cannot, since both the global percentile and the bidirectional EMA need every frame).- Return type:
- Parameters:
contours (ndarray)
params (CellDivergenceParams)
- itasc.segmentation.cell_divergence_segmentation.compute_cell_foreground(foreground, params, nuc=None, *, frame=None)[source]¶
Foreground cleanup + fill mask, independent of contours and the geodesic.
Drives the cell widget’s dedicated foreground-tuning stage. It runs exactly the foreground half of
segment_cells_divergence()— the local-mean residual cleanup (stage 1) and the fill-mask threshold (stage 3) — with no contour cleanup, cost field, or geodesic walk, so foreground tuning stays cheap and does not require the contour map.nucis optional: when given, its seeds are unioned into the mask exactly as the full run does ((foreground_clean > fg_threshold) | (nuc > 0)), so the previewed mask matches the territory segmentation will fill; whenNonethe mask is the bare threshold, letting the foreground be tuned before nucleus tracking exists.frameselects a single frame (2-D result);Noneprocesses the whole stack (3-D result).- Return type:
- Parameters:
foreground (ndarray)
params (CellDivergenceParams)
nuc (ndarray | None)
frame (int | None)
- itasc.segmentation.cell_divergence_segmentation.segment_cells_divergence(contours, foreground, nuc, params, *, frame=None, with_labels=True, contours_clean_override=None, foreground_mask_override=None, progress_cb=None)[source]¶
Run the unary-only divergence pipeline and return all intermediates.
- Parameters:
contours (
ndarray) – Cached divergence maps (raw positive divergence and the sigmoid foreground, respectively).foreground (
ndarray) – Cached divergence maps (raw positive divergence and the sigmoid foreground, respectively).nuc (
ndarray) – Tracked nucleus seeds.params (
CellDivergenceParams)frame (
int|None) – When given, only that frame is processed and temporal smoothing is skipped (it needs the whole stack); the result arrays are 2-D. WhenNone, the whole stack is processed including temporal smoothing.with_labels (
bool) – WhenFalse, the geodesic Voronoi label assignment (the single slowest step) is skipped: every cleanup intermediate plus the weighted cost field is still returned, butresult.labelsisNone. The live preview uses this to stay responsive — the cost field already explains every boundary the labels would land on.contours_clean_override (
ndarray|None) – Single-frame-only. When given (withframeset), this pre-cleaned — and, when temporal smoothing is on, pre-smoothed — contour frame is used ascontours_cleaninstead of re-running the per-frame cleanup. The widget passes a frame sliced fromclean_and_smooth_contours()so the single-frame cost field / labels match the full run exactly (the per-frame path cannot, as it lacks the whole-movie percentile and EMA). Ignored whenframeisNone.foreground_mask_override (
ndarray|None) – Precomputed fill mask to segment inside, replacing the internally derived(foreground_clean > fg_threshold) | (nuc > 0). The cell widget’s Foreground stage writes this mask to disk and the Segmentation stage feeds it back here, so the territory the walk fills is exactly the one tuned in the Foreground stage (not re-derived from the current knobs).(T, Y, X)for a full-stack run,(Y, X)(or a(T, Y, X)stack sliced atframe) for a single-frame run.foreground_cleanis still computed for the cost field’s foreground score regardless.progress_cb (
Callable[[str],None] |None) – Receives short status strings.
- Return type: