spacr.segmentation_uncertainty

Headless segmentation-uncertainty scoring and lossless map export.

Four test-time transforms of the primary model define reference objects. An optional second model contributes four equally weighted label sets; near-miss probabilities and flow diagnostics remain those of the primary model, whose threshold and object identities the user selected. Scores rank review effort; they are not calibrated probabilities of biological error.

Functions

compute_queue_uncertainty(queue, *[, model, ...])

Score a bounded curation queue headlessly and persist its ranking.

compute_uncertainty(image, segment, *[, ...])

Score aligned TTA passes, optionally adding a second-model ensemble.

save_uncertainty_map(path, result, *[, provenance, ...])

Atomically save a float32 TIFF map with embedded JSON provenance.

Module Contents

spacr.segmentation_uncertainty.compute_queue_uncertainty(queue, *, model='cpsam', second_model=None, device='cpu', map_folder=None, parameters=None, progress=None, segmenter_factory=None)[source]

Score a bounded curation queue headlessly and persist its ranking.

Parameters:
  • queue – an existing CurationQueue; only its pending selected fields run.

  • model – primary model name or checkpoint, default cpsam.

  • second_model – optional distinct second model; absent means four passes.

  • device – Device used for model predictions. The default is cpu.

  • map_folder – optional folder for lossless maps and embedded provenance.

  • parameters – inference overrides for diameter, normalize and thresholds.

  • progress – optional callback receiving one completed field’s stem.

  • segmenter_factory – injectable model/device/parameters loader for tests.

Returns:

Dictionary of score summaries keyed by image filename without its extension. Each summary excludes the pixel map.

Raises:

ValueError – for duplicate ensemble models or a field that cannot run.

This is pure image I/O: mask_engine imports no PySide6 and creates no Qt objects. Each completed field is saved as it finishes, so it is recoverable if a later field or model fails.

spacr.segmentation_uncertainty.compute_uncertainty(image, segment, *, second_segment=None, probability_threshold=0.0)[source]

Score aligned TTA passes, optionally adding a second-model ensemble.

Parameters:
  • image – one source field, unchanged by this function.

  • segment – primary callable returning labels or labels/probability/flows.

  • second_segment – optional second callable with the same contract.

  • probability_threshold – the primary model’s cell-probability threshold.

Returns:

the existing uncertainty score dictionary, including a float32 map.

spacr.segmentation_uncertainty.save_uncertainty_map(path, result, *, provenance=None, protected_paths=())[source]

Atomically save a float32 TIFF map with embedded JSON provenance.

Parameters:
  • path – destination TIFF; parent directories are created if necessary.

  • result – uncertainty score dictionary containing a finite 2-D map.

  • provenance – source/model/settings metadata to embed in the TIFF.

  • protected_paths – scientific source images or masks never overwritten.

Returns:

the written Path; failed writes retain the previous destination.

Raises:

ValueError – for invalid maps or a protected destination.

Metadata is validated before any file is opened; integer-keyed object scores are JSON-safe.

Nested helpers

_make_segmenter.segment(image)

Infer one transformed field through the shared output parser.

Parameters:

image – 2-D field supplied by the TTA scorer.

Returns:

labels, logits and vector flows in the input orientation.

spacr/segmentation_uncertainty.py:121