Skip to content

util

adjust_shape #

adjust_shape(array, scale_factors)

Pad or crop array such that its new dimensions are evenly divisible by a set of integers.

Parameters:

Name Type Description Default
array ndarray

Array that will be padded.

required
scale_factors Sequence of ints

The output array is guaranteed to have dimensions that are each evenly divisible by the corresponding scale factor, and chunks that are smaller than or equal to the scale factor (if the array has chunks)

required

Returns:

Type Description
DataArray
Source code in src/xarray_multiscale/util.py
def adjust_shape(array: DataArray, scale_factors: Sequence[int]) -> DataArray:
    """
    Pad or crop array such that its new dimensions are evenly
    divisible by a set of integers.

    Parameters
    ----------
    array : ndarray
        Array that will be padded.

    scale_factors : Sequence of ints
        The output array is guaranteed to have dimensions that are each
        evenly divisible by the corresponding scale factor, and chunks
        that are smaller than or equal to the scale factor
        (if the array has chunks)

    Returns
    -------
    DataArray
    """
    result = array
    misalignment = np.any(np.mod(array.shape, scale_factors))
    if misalignment:
        new_shape = np.subtract(array.shape, np.mod(array.shape, scale_factors))
        result = array.isel({d: slice(s) for d, s in zip(array.dims, new_shape)})
    return result

logn #

logn(x, n)

Compute the logarithm of x base n.

Parameters:

Name Type Description Default
x float or int.
required
n ArrayLike
required

Returns:

Type Description
float

np.log(x) / np.log(n)

Source code in src/xarray_multiscale/util.py
def logn(x: npt.ArrayLike, n: npt.ArrayLike) -> npt.NDArray[np.float64]:
    """
    Compute the logarithm of x base n.

    Parameters
    ----------
    x : float or int.
    n: float or int.

    Returns
    -------
    float
        np.log(x) / np.log(n)

    """
    result: npt.NDArray[np.float64] = np.log(x) / np.log(n)
    return result