albumentations.core.utils


Module containing utility functions and classes for the core Albumentations framework. This module provides a collection of helper functions and base classes used throughout the Albumentations library. It includes utilities for shape handling, parameter processing, data conversion, and serialization. The module defines abstract base classes for data processors that implement the conversion logic between different data formats used in the transformation pipeline.

DataProcessorclass

Abstract base class for data processors. Data processors handle the conversion, validation, and filtering of data during transformations.

Parameters

NameTypeDefaultDescription
paramsParams-Parameters for data processing.
additional_targets
One of:
  • dict[str, str]
  • None
NoneDictionary mapping additional target names to their types.

Paramsclass

Base class for parameter handling in transforms.

Parameters

NameTypeDefaultDescription
formatAny-The format of the data this parameter object will process.
label_fields
One of:
  • Sequence[str]
  • None
-List of fields that are joined with the data, such as labels.

apply_biasfunction

Apply a bias to both values in a range.

Parameters

NameTypeDefaultDescription
min_valNumber-Minimum value.
max_valNumber-Maximum value.
biasNumber-Value to add to both min and max.

Returns

  • tuple[Number, Number]: Tuple containing (min_val + bias, max_val + bias).

ensure_contiguous_outputfunction

Ensure that numpy arrays are contiguous in memory.

Parameters

NameTypeDefaultDescription
arg
One of:
  • np.ndarray
  • Sequence[np.ndarray]
-A numpy array or sequence of numpy arrays.

Returns

  • np.ndarray | list[np.ndarray]: Contiguous array(s) with the same data.

ensure_int_outputfunction

Ensure output is of the same type (int or float) as the input parameter.

Parameters

NameTypeDefaultDescription
min_valNumber-Minimum value.
max_valNumber-Maximum value.
paramNumber-Original parameter used to determine the output type.

Returns

  • tuple[int, int] | tuple[float, float]: Tuple with values converted to int if param is int,

format_argsfunction

Format a dictionary of arguments into a string representation.

Parameters

NameTypeDefaultDescription
args_dictdict[str, Any]-Dictionary of argument names and values.

Returns

  • str: Formatted string of arguments in the form "key1='value1', key2=value2".

get_shapefunction

Extract height and width dimensions from input data dictionary. After grayscale preprocessing, all data has channel dimension at the end.

Parameters

NameTypeDefaultDescription
datadict[str, Any]-Dictionary containing image or volume data with one of: - 'volume': 3D array of shape (D, H, W, C) - 'volumes': Batch of 3D arrays of shape (N, D, H, W, C) - 'image': 2D array of shape (H, W, C) - 'images': Batch of arrays of shape (N, H, W, C)

Returns

  • tuple[int, int]: (height, width) dimensions

get_volume_shapefunction

Extract depth, height, and width dimensions from volume data.

Parameters

NameTypeDefaultDescription
datadict[str, Any]-Dictionary containing volume data

Returns

  • tuple[int, int, int] | None: (depth, height, width) dimensions if volume data exists, None otherwise

process_scalarfunction

Process a scalar value and optional low bound into a (min, max) tuple.

Parameters

NameTypeDefaultDescription
paramNumber-Scalar numeric value.
low
One of:
  • Number
  • None
-Optional lower bound.

Returns

  • tuple[Number, Number]: Tuple containing (min_value, max_value) where:

process_sequencefunction

Process a sequence and return it as a (min, max) tuple.

Parameters

NameTypeDefaultDescription
paramSequence[Number]-Sequence of numeric values.

Returns

  • tuple[Number, Number]: Tuple containing (min_value, max_value) from the sequence.

to_tuplefunction

Convert input argument to a min-max tuple. This function processes various input types and returns a tuple representing a range. It handles single values, sequences, and can apply optional low bounds or biases.

Parameters

NameTypeDefaultDescription
param
One of:
  • float
  • tuple[float, float]
  • tuple[int, int]
-The primary input value. Can be: - A single int or float: Converted to a symmetric range around zero. - A tuple of two ints or two floats: Used directly as min and max values.
low
One of:
  • float
  • tuple[float, float]
  • tuple[int, int]
  • None
NoneA lower bound value. Used when param is a single value. If provided, the result will be (low, param) or (param, low), depending on which is smaller. Cannot be used together with 'bias'. Defaults to None.
bias
One of:
  • float
  • None
NoneA value to be added to both elements of the resulting tuple. Cannot be used together with 'low'. Defaults to None.

Returns

  • tuple[int, int] | tuple[float, float]: A tuple representing the processed range.

Examples

>>> to_tuple(5)
(-5, 5)
>>> to_tuple(5.0)
(-5.0, 5.0)
>>> to_tuple((1, 10))
(1, 10)
>>> to_tuple(5, low=3)
(3, 5)
>>> to_tuple(5, bias=1)
(-4, 6)

Notes

- When 'param' is a single value and 'low' is not provided, the function creates a symmetric range around zero. - The function preserves the type (int or float) of the input in the output. - If a sequence is provided, it must contain exactly 2 elements.

validate_argsfunction

Validate that 'low' and 'bias' parameters are not used together.

Parameters

NameTypeDefaultDescription
low
One of:
  • float
  • Sequence[int]
  • Sequence[float]
  • None
-Lower bound value.
bias
One of:
  • float
  • None
-Bias value to be added to both min and max values.