Skip to content

sopa.utils.image

sopa.utils.image.scale_dtype(arr, dtype)

Change the dtype of an array but keep the scale compared to the type maximum value.

Example

For an array of dtype uint8 being transformed to np.uint16, the value 255 will become 65535

Parameters:

Name Type Description Default
arr ndarray

A numpy array

required
dtype dtype

Target numpy data type

required

Returns:

Type Description
ndarray

A scaled numpy array with the dtype provided.

Source code in sopa/utils/image.py
def scale_dtype(arr: np.ndarray, dtype: np.dtype) -> np.ndarray:
    """Change the dtype of an array but keep the scale compared to the type maximum value.

    !!! note "Example"
        For an array of dtype `uint8` being transformed to `np.uint16`, the value `255` will become `65535`

    Args:
        arr: A `numpy` array
        dtype: Target `numpy` data type

    Returns:
        A scaled `numpy` array with the dtype provided.
    """
    _check_integer_dtype(arr.dtype)
    _check_integer_dtype(dtype)

    if arr.dtype == dtype:
        return arr

    factor = np.iinfo(dtype).max / np.iinfo(arr.dtype).max
    return (arr * factor).astype(dtype)

sopa.utils.image.resize(xarr, scale_factor)

Resize a xarray image

Parameters:

Name Type Description Default
xarr DataArray

A xarray array

required
scale_factor float

Scale factor of resizing, e.g. 2 will decrease the width by 2

required

Returns:

Type Description
Array

Resized dask array

Source code in sopa/utils/image.py
def resize(xarr: xr.DataArray, scale_factor: float) -> da.Array:
    """Resize a xarray image

    Args:
        xarr: A `xarray` array
        scale_factor: Scale factor of resizing, e.g. `2` will decrease the width by 2

    Returns:
        Resized dask array
    """
    resize_dims = [dim in ["x", "y"] for dim in xarr.dims]
    transform = np.diag([scale_factor if resize_dim else 1 for resize_dim in resize_dims])
    output_shape = [
        size // scale_factor if resize_dim else size
        for size, resize_dim in zip(xarr.shape, resize_dims)
    ]

    return dask_image.ndinterp.affine_transform(
        xarr.data, matrix=transform, output_shape=output_shape
    )

sopa.utils.image.resize_numpy(arr, scale_factor, dims, output_shape)

Resize a numpy image

Parameters:

Name Type Description Default
arr ndarray

a numpy array

required
scale_factor float

Scale factor of resizing, e.g. 2 will decrease the width by 2

required
dims list[str]

List of dimension names. Only "x" and "y" are resized.

required
output_shape list[int]

Size of the output array

required

Returns:

Type Description
ndarray

Resized array

Source code in sopa/utils/image.py
def resize_numpy(
    arr: np.ndarray, scale_factor: float, dims: list[str], output_shape: list[int]
) -> np.ndarray:
    """Resize a numpy image

    Args:
        arr: a `numpy` array
        scale_factor: Scale factor of resizing, e.g. `2` will decrease the width by 2
        dims: List of dimension names. Only `"x"` and `"y"` are resized.
        output_shape: Size of the output array

    Returns:
        Resized array
    """
    resize_dims = [dim in ["x", "y"] for dim in dims]
    transform = np.diag([scale_factor if resize_dim else 1 for resize_dim in resize_dims])

    return dask_image.ndinterp.affine_transform(
        arr, matrix=transform, output_shape=output_shape
    ).compute()