sparrow.im.merge_labels_layers

sparrow.im.merge_labels_layers#

sparrow.im.merge_labels_layers(sdata, labels_layer_1, labels_layer_2, threshold=0.5, depth=100, chunks='auto', output_labels_layer=None, output_shapes_layer=None, scale_factors=None, overwrite=False, iou_depth=2, iou_threshold=0.7)#

Merges two labels layers within a SpatialData object based on a specified threshold.

This function applies a merge operation between two specified labels layers (labels_layer_1 and labels_layer_2) in a SpatialData object. The function will copy all labels from labels_layer_1 to output_labels_layer, and for all labels in labels_layer_2 it will check if they have less than threshold overlap with labels from labels_layer_1, if so, label in labels_layer_2 will be copied to output_labels_layer at locations where ‘labels_layer_1’ is 0.

Parameters:
  • sdata (SpatialData) – The SpatialData object containing the labels layers to be merged.

  • labels_layer_1 (str) – The name of the first labels layer. This layer will get priority.

  • labels_layer_2 (str) – The name of the second labels layer to be merged in labels_layer_1.

  • threshold (float (default: 0.5)) – The threshold value to control the merging of labels. This value determines how the merge operation is conducted based on the overlap between the labels in labels_layer_1 and labels_layer_2.

  • depth (tuple[int, int] | int (default: 100)) – The depth around the boundary of each block to load when the array is split into blocks (for alignment). This ensures that the split isn’t causing misalignment along the edges. Please set depth>cell diameter + distance to avoid chunking effects.

  • chunks (str | int | tuple[int, int] | None (default: 'auto')) – Specification for rechunking the data before applying the merge operation. This parameter defines how the data is divided into chunks for processing.

  • output_labels_layer (Optional[str] (default: None)) – The name of the output labels layer where the merged results will be stored.

  • output_shapes_layer (Optional[str] (default: None)) – The name of the output shapes layer where results will be stored if shape data is produced from the merge operation.

  • scale_factors (Optional[Sequence[Union[dict[str, int], int]]] (default: None)) – Scale factors to apply for multiscale processing.

  • overwrite (bool (default: False)) – If True, overwrites the output layer if it already exists in sdata.

  • iou_depth (tuple[int, int] | int (default: 2)) – iou depth used for linking labels.

  • iou_threshold (float (default: 0.7)) – iou threshold used for linking labels.

Return type:

SpatialData

Returns:

: The sdata object with the merged labels layer added to the specified output layer. If output_shapes_layer is provided, a shapes layer will be created corresponding to this labels layer.

Raises:

ValueError – If any of the specified labels layers cannot be found in sdata.

Notes

This function leverages dask for potential parallelism and out-of-core computation, enabling the processing of large datasets that may not fit entirely in memory. It is particularly useful in scenarios where two segmentation results need to be combined to achieve a more accurate or comprehensive segmentation outcome.