sparrow.im.normalize

Contents

sparrow.im.normalize#

sparrow.im.normalize(sdata, img_layer, output_layer, q_min=5.0, q_max=95.0, eps=1e-20, internal_method='tdigest', scale_factors=None, overwrite=False)#

Normalize the intensity of an image layer in a SpatialData object using specified percentiles.

The normalization can be applied globally or individually to each channel, depending on whether q_min and q_max are provided as single values or as lists. This allows for flexible intensity scaling across multiple channels.

Parameters:
  • sdata (SpatialData) – SpatialData object.

  • img_layer (str) – The image layer in sdata to normalize.

  • output_layer (str) – The name of the output layer where the normalized image will be stored.

  • q_min (float | list[float] (default: 5.0)) – The lower percentile for normalization. If provided as a list, the length must match the number of channels.

  • q_max (float | list[float] (default: 95.0)) – The upper percentile for normalization. If provided as a list, the length must match the number of channels.

  • eps (float, optional) – A small epsilon value added to the denominator to avoid division by zero. Default is 1e-20.

  • internal_method (str, optional) – The method dask uses for computing percentiles. Default is “tdigest”. Can be “dask” or “tdigest”.

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

  • overwrite (bool (default: False)) – If True, overwrites the element if it already exists.

Return type:

SpatialData

Returns:

: The sdata object with the normalized image added.

Raises:

ValueError – If q_min and q_max are provided as lists and their lengths do not match the number of channels.

Examples

Normalize using a single percentile range for all channels:

>>> sdata = normalize(sdata, img_layer='my_image', output_layer='normalized_image', q_min=5, q_max=95)

Normalize using different percentile ranges for each channel:

>>> sdata = normalize(sdata, img_layer='my_image', output_layer='normalized_image', q_min=[5, 10, 15], q_max=[95, 90, 85])