Skip to content

Array

analora.array

Contain functions for numpy.ndarrays.

analora.array.check_same_shape

check_same_shape(arrays: Iterable[ndarray]) -> None

Check if arrays have the same shape.

Parameters:

Name Type Description Default
arrays Iterable[ndarray]

The arrays to check.

required

Raises:

Type Description
RuntimeError

if the arrays have different shapes.

Example usage:

>>> import numpy as np
>>> from analora.array import check_same_shape
>>> check_same_shape([np.array([1, 0, 0, 1]), np.array([0, 1, 0, 1])])

analora.array.check_square_matrix

check_square_matrix(name: str, array: ndarray) -> None

Check if the input array is a square matrix.

Parameters:

Name Type Description Default
name str

The name of the variable.

required
array ndarray

The array to check.

required

Raises:

Type Description
ValueError

if the array is not a square matrix.

Example usage:

>>> import numpy as np
>>> from analora.array import check_square_matrix
>>> check_square_matrix("var", np.ones((3, 3)))

analora.array.filter_range

filter_range(
    array: ndarray, xmin: float, xmax: float
) -> ndarray

Filter in the values in a given range.

Parameters:

Name Type Description Default
array ndarray

The input array.

required
xmin float

The lower bound of the range.

required
xmax float

The upper bound of the range.

required

Returns:

Type Description
ndarray

A 1-d array with only the values in the given range.

Example usage:

>>> import numpy as np
>>> from analora.array import filter_range
>>> out = filter_range(np.arange(10), xmin=-1, xmax=5)
>>> out
array([0, 1, 2, 3, 4, 5])

analora.array.find_range

find_range(
    values: ndarray,
    xmin: float | str | None = None,
    xmax: float | str | None = None,
) -> tuple[float, float]

Find a valid range of value.

Parameters:

Name Type Description Default
values ndarray

The values used to find the quantiles.

required
xmin float | str | None

The minimum value of the range or its associated quantile. q0.1 means the 10% quantile. 0 is the minimum value and 1 is the maximum value.

None
xmax float | str | None

The maximum value of the range or its associated quantile. q0.9 means the 90% quantile. 0 is the minimum value and 1 is the maximum value.

None

Returns:

Type Description
tuple[float, float]

The range of values in the format (min, max). It returns (nan, nan) if the input array is empty.

Example usage:

>>> import numpy as np
>>> from analora.array import find_range
>>> data = np.arange(101)
>>> find_range(data)
(0, 100)
>>> find_range(data, xmin=5, xmax=50)
(5, 50)
>>> find_range(data, xmin="q0.1", xmax="q0.9")
(10.0, 90.0)

analora.array.multi_isnan

multi_isnan(arrays: Sequence[ndarray]) -> ndarray

Test element-wise for NaN for all input arrays and return result as a boolean array.

Parameters:

Name Type Description Default
arrays Sequence[ndarray]

The input arrays to test. All the arrays must have the same shape.

required

Returns:

Type Description
ndarray

A boolean array. True where any array is NaN, False otherwise.

Example usage:

>>> import numpy as np
>>> from analora.array import multi_isnan
>>> mask = multi_isnan(
...     [np.array([1, 0, 0, 1, float("nan")]), np.array([1, float("nan"), 0, 1, 1])]
... )
>>> mask
array([False,  True, False, False,  True])

analora.array.nonnan

nonnan(array: ndarray) -> ndarray

Return the non-NaN values of an array.

Parameters:

Name Type Description Default
array ndarray

The input array.

required

Returns:

Type Description
ndarray

A 1d array with the non-NaN values of the input array.

Example usage:

>>> import numpy as np
>>> from analora.array import nonnan
>>> nonnan(np.asarray([1, 2, float("nan"), 5, 6]))
array([1., 2., 5., 6.])
>>> nonnan(np.asarray([[1, 2, float("nan")], [4, 5, 6]]))
array([1., 2., 4., 5., 6.])

analora.array.rand_replace

rand_replace(
    arr: ndarray,
    value: Any,
    prob: float = 0.5,
    rng: Generator | None = None,
) -> ndarray

Return an array that contains the same values as the input array, excepts some values are replaced by value.

Parameters:

Name Type Description Default
arr ndarray

The array with the original values.

required
value Any

The value used to replace existing values.

required
prob float

The probability of value replacement. If the value is 0.2, it means each value as 20% chance to be replaced.

0.5
rng Generator | None

The random number generator used to decide which values are replaced or not. If None, the default random number generator is used.

None

Returns:

Type Description
ndarray

The generated array.

Example usage:

>>> import numpy as np
>>> from analora.array import rand_replace
>>> rng = np.random.default_rng(42)
>>> out = rand_replace(np.arange(10), value=-1, prob=0.4, rng=rng)