rm_lite.utils.arrays

Array utils

Attributes

Functions

arange(→ numpy.typing.NDArray[numpy.float64])

Combines numpy.arange and numpy.isclose to mimic open, half-open and closed intervals.

nd_to_two_d(→ numpy.typing.NDArray[DType])

Convert an array to 2D.

two_d_to_nd(→ numpy.typing.NDArray[DType])

Reverse the to_2d operation.

Module Contents

rm_lite.utils.arrays.arange(start: float | int, stop: float | int, step: float | int, rtol: float = 1e-05, atol: float = 1e-08, include_start: bool = True, include_stop: bool = False, **kwargs) numpy.typing.NDArray[numpy.float64]

Combines numpy.arange and numpy.isclose to mimic open, half-open and closed intervals.

Avoids also floating point rounding errors as with >>> np.arange(1, 1.3, 0.1) array([1., 1.1, 1.2, 1.3])

Parameters:
  • start (float | int) – Start of the interval.

  • stop (float | int) – End of the interval.

  • step (float | int) – Spacing between values.

  • rtol (float, optional) – if last element of array is within this relative tolerance to stop and include[0]==False, it is skipped. Defaults to 1e-05.

  • atol (float, optional) – if last element of array is within this relative tolerance to stop and include[1]==False, it is skipped. Defaults to 1e-08.

  • include_start (bool, optional) – if first element is included in the returned array. Defaults to True.

  • include_stop (bool, optional) – if last elements are included in the returned array if stop equals last element. Defaults to False.

  • kwargs – passed to np.arange

Returns:

as np.arange but eventually with first and last element stripped/added

Return type:

_type_

rm_lite.utils.arrays.nd_to_two_d(arr: numpy.typing.NDArray[DType]) numpy.typing.NDArray[DType]

Convert an array to 2D.

  • If arr is 1D, it will be reshaped as a column vector (shape: (N, 1)).

  • If arr is already 2D, it is returned as is.

  • If arr has more than 2 dimensions, the first axis is kept intact and all remaining axes are flattened. For example, an array with shape (a, b, c, d) will become shape (a, b*c*d).

Parameters:

arr (NDArray[Any]) – N-dimensional array.

Returns:

2D array.

Return type:

NDArray[Any]

rm_lite.utils.arrays.two_d_to_nd(arr2d: numpy.typing.NDArray[DType], original_shape: tuple[int, Ellipsis]) numpy.typing.NDArray[DType]

Reverse the to_2d operation.

Parameters:
  • arr2d (array-like) – the 2D array (result from to_2d).

  • original_shape (tuple) – the shape of the original array before flattening.

Returns:

The array reshaped back to its original shape.

The function assumes:
  • For an original 1D array, original_shape is (N,). In this case, arr2d is of shape (N, 1) and will be flattened back to (N,).

  • For an original 2D array, original_shape is (M, N) and arr2d is already (M, N).

  • For an original N_D array (N_D > 2) with shape (a, b, c, …), arr2d is assumed to have shape (a, b*c*…) and will be reshaped back to (a, b, c, …).

rm_lite.utils.arrays.DType
rm_lite.utils.arrays.TQDM_OUT