Skip to content

afmslicer.slicer

Module for slicing two-dimensional arrays to three-dimensional arrays of stacked masks.

calculate_region_properties(array, spacing)

Calculate the region properties on a segmented array.

The arrays can be either individual slices from a three-dimensional image or the full three-dimensional array itself. If the later then the resulting attribute area will be a "volume". By including the spacing argument, which should be the pixel_to_nm_scaling attribute of the AFMSlicer object the area/volume is in the actual units measured rather than pixels.

Parameters:

Name Type Description Default
array NDArray

Array of labelled regions.

required
spacing float

Pixel to nm scaling.

required

Returns:

Type Description
list[RegionProperties]

A list of RegionProperties.

Source code in src/afmslicer/slicer.py
def calculate_region_properties(array: npt.NDArray[np.int32], spacing: float) -> Any:
    """
    Calculate the region properties on a segmented array.

    The arrays can be either individual slices from a three-dimensional image or the full three-dimensional array
    itself. If the later then the resulting attribute ``area`` will be a "volume". By including the ``spacing``
    argument, which should be the ``pixel_to_nm_scaling`` attribute of the ``AFMSlicer`` object the area/volume is
    in the actual units measured rather than pixels.

    Parameters
    ----------
    array : npt.NDArray
        Array of labelled regions.
    spacing : float
        Pixel to nm scaling.

    Returns
    -------
    list[RegionProperties]
        A list of ``RegionProperties``.
    """
    return regionprops(array.astype(np.int8), spacing=spacing)

mask_slices(stacked_array, slices=None, layers=None, min_height=None, max_height=None)

Convert a three-dimensional sliced array into masks based.

A three-dimensional array is converted to masked layers where each layer indicates whether a position in the two-dimensional cross-section is above the threshold for that layer. Thresholds are determined from the data itself if not explicitly provided.

Parameters:

Name Type Description Default
stacked_array NDArray

Three-dimensional numpy array of image heights, each layer should be a copy of the original.

required
slices int

Number of slices to mask. Determined directly from data if not provided (i.e. depth of three-dimensional array).

None
layers NDArray

Array of height thresholds for each slice. Determined directly from data if not provided.

None
min_height float64

Minimum height. Determined directly from data if not provided.

None
max_height float64

Maximum height. Determined directly from data if not provided.

None

Returns:

Type Description
NDArray[bool]

Three-dimensional array of masks.

Source code in src/afmslicer/slicer.py
def mask_slices(
    stacked_array: npt.NDArray[np.float64],
    slices: int | None = None,
    layers: npt.NDArray | None = None,
    min_height: np.float64 | None = None,
    max_height: np.float64 | None = None,
) -> npt.NDArray[bool]:
    """
    Convert a three-dimensional sliced array into masks based.

    A three-dimensional array is converted to masked layers where each layer indicates whether a position in the
    two-dimensional cross-section is above the threshold for that layer. Thresholds are determined from the data itself
    if not explicitly provided.

    Parameters
    ----------
    stacked_array : npt.NDArray
        Three-dimensional numpy array of image heights, each layer should be a copy of the original.
    slices : int, optional
        Number of slices to mask. Determined directly from data if not provided (i.e. depth of three-dimensional array).
    layers : npt.NDArray, optional
        Array of height thresholds for each slice. Determined directly from data if not provided.
    min_height : np.float64, optional
        Minimum height. Determined directly from data if not provided.
    max_height : np.float64, optional
        Maximum height. Determined directly from data if not provided.

    Returns
    -------
    npt.NDArray[bool]
        Three-dimensional array of masks.
    """
    slices = stacked_array.shape[2] if slices is None else slices
    min_height = np.min(stacked_array) if min_height is None else min_height
    max_height = np.max(stacked_array) if max_height is None else max_height
    layers = np.linspace(min_height, max_height, slices) if layers is None else layers
    sliced_mask = stacked_array.copy()
    for layer, height in enumerate(layers):
        sliced_mask[:, :, layer] = np.where(stacked_array[:, :, layer] > height, 1, 0)
    # We want to capture the maximum...
    sliced_mask[:, :, slices - 1] = np.where(
        stacked_array[:, :, slices - 1] >= max_height, 1, 0
    )
    return sliced_mask

mask_small_artefacts(labelled_array, properties, minimum_size)

Mask labelled features that are less than a specified size from two-dimensional array.

Parameters:

Name Type Description Default
labelled_array NDArray[int32]

A three-dimensional array with labelled regions.

required
properties dict[int, Any]

The properties of labelled objects for each layer.

required
minimum_size float

Minimum size below which labelled regions are removed.

required

Returns:

Type Description
NDArray[int32]

Masked array with labelled regions smaller than minimum_size masked.

Source code in src/afmslicer/slicer.py
def mask_small_artefacts(
    labelled_array: npt.NDArray[np.int32],
    properties: dict[int, Any],
    minimum_size: float,
) -> npt.NDArray[np.int32]:
    """
    Mask labelled features that are less than a specified size from two-dimensional array.

    Parameters
    ----------
    labelled_array : npt.NDArray[np.int32]
        A three-dimensional array with labelled regions.
    properties : dict[int, Any]
        The properties of labelled objects for each layer.
    minimum_size : float
        Minimum size below which labelled regions are removed.

    Returns
    -------
    npt.NDArray[np.int32]
        Masked array with labelled regions smaller than ``minimum_size`` masked.
    """
    assert len(labelled_array.shape) == 2, (
        "The labelled array should only have two dimensions."
    )
    masked_array = np.ma.MaskedArray(
        labelled_array, mask=np.zeros_like(labelled_array, dtype=bool), copy=True
    )
    for region, prop in enumerate(properties, start=1):
        if prop.area <= np.float64(minimum_size):  # type: ignore[attr-defined]
            masked_array.mask = np.where(
                masked_array.data == region, True, masked_array.mask
            )
    return masked_array

mask_small_artefacts_all_layers(labelled_array, properties, minimum_size)

Mask labelled features that are less than a specified size from three-dimensional array.

Parameters:

Name Type Description Default
labelled_array NDArray[int32]

A three-dimensional array with labelled regions.

required
properties dict[int, Any]

The properties of labelled objects for each layer.

required
minimum_size float

Minimum size below which labelled regions are removed.

required

Returns:

Type Description
NDArray[int32]

Masked array with labelled regions smaller than minimum_size masked.

Source code in src/afmslicer/slicer.py
def mask_small_artefacts_all_layers(
    labelled_array: npt.NDArray[np.int32],
    properties: list[Any],
    minimum_size: float,
):
    """
    Mask labelled features that are less than a specified size from three-dimensional array.

    Parameters
    ----------
    labelled_array : npt.NDArray[np.int32]
        A three-dimensional array with labelled regions.
    properties : dict[int, Any]
        The properties of labelled objects for each layer.
    minimum_size : float
        Minimum size below which labelled regions are removed.

    Returns
    -------
    npt.NDArray[np.int32]
        Masked array with labelled regions smaller than ``minimum_size`` masked.
    """
    assert labelled_array.shape[2] == len(properties), (
        f"The three-dimensional array has {labelled_array.shape[2]} layers which differs from the number of layer "
        f"properties {len(properties)}"
    )
    masked_array = np.ma.masked_all_like(labelled_array)
    # For each layer extract the area to a dictionary
    for layer in range(labelled_array.shape[2]):
        masked_array[:, :, layer] = mask_small_artefacts(
            labelled_array=labelled_array[:, :, layer],
            properties=properties[layer],
            minimum_size=minimum_size,
        )
    return masked_array

region_properties_by_slices(array, spacing)

Calculate region properties for each layer in a three-dimensinoal sliced array.

Parameters:

Name Type Description Default
array NDArray[int32]

Three-dimensional sliced and labelled array.

required
spacing float

Pixel to nanometer scaling.

required

Returns:

Type Description
dict[int, Any]

Dictionary of regionprops calculated using skimage.

Source code in src/afmslicer/slicer.py
def region_properties_by_slices(
    array: npt.NDArray[np.int32], spacing: float
) -> list[Any]:
    """
    Calculate region properties for each layer in a three-dimensinoal sliced array.

    Parameters
    ----------
    array : npt.NDArray[np.int32]
        Three-dimensional sliced and labelled array.
    spacing : float
        Pixel to nanometer scaling.

    Returns
    -------
    dict[int, Any]
        Dictionary of ``regionprops`` calculated using skimage.
    """

    slice_properties = []
    for layer in range(array.shape[2]):
        slice_properties.append(
            regionprops(array[:, :, layer].astype(np.int32), spacing=spacing)
        )
    return slice_properties

segment(array, method='label', tidy_border=True, **kwargs)

Segment an array.

Parameters:

Name Type Description Default
array NDArray

Two-dimensional numpy array to segment.

required
method str

Segmentation method, supports the label (default) and watershed methods implemented by Scikit Image.

'label'
tidy_border bool

Whether to remove objects that straddle the border of the image.

True
**kwargs dict[str, Any]

Additional arguments to pass for segmentation.

{}

Returns:

Type Description
NDArray

Labelled array of objects.

Source code in src/afmslicer/slicer.py
def segment(
    array: npt.NDArray,
    method: str | None = "label",
    tidy_border: bool = True,
    **kwargs: dict[str, Any] | None,
) -> npt.NDArray:
    """
    Segment an array.

    Parameters
    ----------
    array : npt.NDArray
        Two-dimensional numpy array to segment.
    method : str, optional
        Segmentation method, supports the ``label`` (default) and ``watershed`` methods implemented by Scikit Image.
    tidy_border : bool
        Whether to remove objects that straddle the border of the image.
    **kwargs : dict[str, Any], optional
        Additional arguments to pass for segmentation.

    Returns
    -------
    npt.NDArray
        Labelled array of objects.
    """
    if method is None:
        method = "label"
        # logger.info("No segmentation method specified, defaulting to 'label'.")
    if tidy_border:
        array = clear_border(array)
        # logger.info("")
    segmenter = _get_segments(method)
    return segmenter(array, **kwargs)

segment_slices(array, method='label', tidy_border=False)

Segment individual layers of a three-dimensional numpy array.

Parameters:

Name Type Description Default
array NDArray[bool]

Three-dimensional boolean array to be segmented.

required
method str

Sgementation method to use. Currne options are label (default) and watershed.

'label'
tidy_border bool

Whether to tidy the border.

False

Returns:

Type Description
NDArray[bool]

Three-dimensional array of labelled layers.

Source code in src/afmslicer/slicer.py
def segment_slices(
    array: npt.NDArray[np.bool], method: str | None = "label", tidy_border: bool = False
) -> npt.NDArray[np.bool]:
    """
    Segment individual layers of a three-dimensional numpy array.

    Parameters
    ----------
    array : npt.NDArray[np.bool]
        Three-dimensional boolean array to be segmented.
    method : str
        Sgementation method to use. Currne options are ``label`` (default) and ``watershed``.
    tidy_border : bool
        Whether to tidy the border.

    Returns
    -------
    npt.NDArray[np.bool]
        Three-dimensional array of labelled layers.
    """
    for layer in np.arange(array.shape[2]):
        array[:, :, layer] = segment(array[:, :, layer], method, tidy_border)
    return array

show_layers(array)

Helper function for debugging which shows individual layers of a three-dimensional numpy array.

Parameters:

Name Type Description Default
array NDArray

Three-dimensional numpy array.

required
Source code in src/afmslicer/slicer.py
def show_layers(array: npt.NDArray) -> None:
    """
    Helper function for debugging which shows individual layers of a three-dimensional numpy array.

    Parameters
    ----------
    array : npt.NDArray
        Three-dimensional numpy array.
    """
    assert len(array.shape) == 3, f"Array is not 3D : {array.shape=}"
    for layer in np.arange(0, array.shape[-1]):
        print(f"\nLayer {layer} \n{array[...,layer]=}\n")  # noqa: T201

slice_3d(array, scaling, binary=False)

Convert a two-dimensional array of heights into a three-dimensional binary array.

Rather than taking a fixed number of slices as with slicer() we here use the observed range and the scaling, which typically comes from the scan itself to determine the appropriate number of slices to take so that they are correctly scaled to the same as the x/y axis of the array.

Parameters:

Name Type Description Default
array NDArray[float64]

A two-dimensional array of heights.

required
scaling float

Pixel to nanometre scaling.

required
binary bool

Whether to create a boolean array. If True each pixel is dichotomised to 0/1 if the height is within the range for that layer. If False the value for that layer is returned, this is useful if you want to use colour gradients when viewing three dimensional plots.

False

Returns:

Type Description
NDArray[int8, float64]

A three-dimensional array scaled by scaling.

Source code in src/afmslicer/slicer.py
def slice_3d(
    array: npt.NDArray[np.float64], scaling: float, binary: bool = False
) -> npt.NDArray[np.int8, np.float64]:
    """
    Convert a two-dimensional array of heights into a three-dimensional binary array.

    Rather than taking a fixed number of slices as with ``slicer()`` we here use the observed range and the scaling,
    which typically comes from the scan itself to determine the appropriate number of slices to take so that they are
    correctly scaled to the same as the x/y axis of the array.

    Parameters
    ----------
    array : npt.NDArray[np.float64]
        A two-dimensional array of heights.
    scaling : float
        Pixel to nanometre scaling.
    binary : bool
        Whether to create a boolean array. If `True` each pixel is dichotomised to `0`/`1` if the height is within the
        range for that layer. If `False` the value for that layer is returned, this is useful if you want to use colour
        gradients when viewing three dimensional plots.

    Returns
    -------
    npt.NDArray[np.int8, np.float64]
        A three-dimensional array scaled by `scaling`.
    """
    min_height = array.min()
    max_height = array.max()
    # We want at least one layer even if all points are the same height (highly unlikely though!)
    layers = (
        1
        if max_height - min_height == 0
        else int(np.ceil(max_height - min_height) / scaling)
    )
    # Make two arrays of the lower and upper bounds for each layer
    lower_bounds = min_height + (np.arange(layers) * scaling)
    upper_bounds = lower_bounds + scaling
    # Build a 3D boolean array of which values need using in each layer
    in_slice = (array[np.newaxis, ...] >= lower_bounds[:, np.newaxis, np.newaxis]) & (
        array[np.newaxis, ...] < upper_bounds[:, np.newaxis, np.newaxis]
    )
    # Add the final slice element by element
    in_slice[-1] |= array == max_height
    if binary:
        logger.info("Creating a binary 3D array.")
        # Zero bottom layer
        in_slice[0] = False
        return in_slice.astype(np.int8)
    logger.info("Using heights in 3D array.")
    # Return the values in each layer, otherwise replace with zero
    return np.where(in_slice, array[np.newaxis, ...], 0)

slicer(heights, slices)

Convert a two-dimensional array to a three-dimensional stacked array with copies of the original in each layer.

Parameters:

Name Type Description Default
heights NDArray[float64]

Two-dimensional numpy array of heights.

required
slices int

Number of slices to make.

required

Returns:

Type Description
NDArray[float64]

Expanded numpy array with the original two-dimensional array copied slices in the third dimension.

Examples:

>>> import numpy as np
>>> simple = np.asarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> slicer(heights=simple, slices=2)
array([[[1, 1],
    [2, 2],
    [3, 3]],

[[4, 4], [5, 5], [6, 6]],

[[7, 7], [8, 8], [9, 9]]])

Source code in src/afmslicer/slicer.py
def slicer(heights: npt.NDArray[np.float64], slices: int) -> npt.NDArray[np.float64]:
    """
    Convert a two-dimensional array to a three-dimensional stacked array with copies of the original in each layer.

    Parameters
    ----------
    heights : npt.NDArray[np.float64]
        Two-dimensional numpy array of heights.
    slices : int
        Number of slices to make.

    Returns
    -------
    npt.NDArray[np.float64]
        Expanded numpy array with the original two-dimensional array copied ``slices`` in the third dimension.

    Examples
    --------
    >>> import numpy as np
    >>> simple = np.asarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    >>> slicer(heights=simple, slices=2)
    array([[[1, 1],
        [2, 2],
        [3, 3]],

       [[4, 4],
        [5, 5],
        [6, 6]],

       [[7, 7],
        [8, 8],
        [9, 9]]])
    """
    return np.repeat(
        heights[
            :,
            :,
            np.newaxis,
        ],
        repeats=slices,
        axis=2,
    )