acia.segm.formats#
Functions for different segmentation formats
- acia.segm.formats.parse_simple_segmentation(file_content)[source]#
Parse simple segmentation format from string (json)
- acia.segm.formats.gen_simple_segmentation(overlay)[source]#
Create a simple segmentation string from an Overlay
- acia.segm.formats.save_segmentation(path, overlay)[source]#
Store a segmentation
Overlayas a compressed binary polygon archive.The counterpart of
load_segmentation(). Contours are written as one flatfloat32coordinate array plus per-detection offsets, alongside theid,labelandframeof each detection – so ids and sub-pixel coordinates survive exactly and a reloaded overlay joins against a property table exported from the same segmentation.Why this rather than the alternatives, measured on a dense 150k-detection scene (1024x1024, 300 cells x 500 frames): gzipped
gen_simple_segmentation()JSON is 106 MiB / 43 s to write, this format is 41 MiB / 1.7 s, and a zlib label-mask stack is 18 MiB / 2.0 s. The mask stack is smaller still, but rasterizing renumbers detection ids (overlay_from_masks()) and yields mask-backedInstanceobjects whose geometry is derived from the full frame, making downstream property extraction ~20x slower. Keeping polygons keeps both the ids and the fast extraction path.What is not stored: the overlay’s time model (attach it on load from the image source, see
load_segmentation()), per-detection scores, and mask topology –Instancemasks are serialized as polygons.- Parameters:
- Returns:
The path actually written (with the normalized suffix).
- Raises:
TypeError – if detection ids or labels are of mixed types, which numpy cannot store without pickling.
- Return type:
- acia.segm.formats.load_segmentation(path, source=None)[source]#
Load a segmentation stored by
save_segmentation().The format is detected from the file’s magic bytes, not its suffix, so the binary archive written by
save_segmentation()and a plain or gzipped simple-segmentation JSON (gen_simple_segmentation(), the interchange format other tools read) all load through this one function.Passing
source– the image sequence the segmentation was computed on – restores what the artifact cannot carry, and takes precedence over anything stored in the file:the frame extent: the returned overlay spans
source.size_t. This matters because a movie whose last frames hold no surviving cells would otherwise reload shorter than it really is (the JSON interchange format drops empty frames entirely).the time model:
source.timepointsis attached (stamping each detection’stime) when the source is time-calibrated. An uncalibrated source leaves the overlay uncalibrated – no time is invented.
Detection ids are stable across this round-trip (see
save_segmentation()).- Parameters:
path (str | Path) – the artifact to read. If the literal path does not exist, the
.npz-normalized name is tried, so this mirrors whateversave_segmentation()accepted.source (ImageSequenceSource | None) – image sequence used to restore frame extent and time calibration.
Nonefalls back to the extent stored in the archive (or, for JSON input, the last populated frame).
- Returns:
The segmentation overlay.
- Raises:
FileNotFoundError – if neither the literal nor the normalized path exists.
- Return type: