Skip to content

Validation

batcharray.validation

Contain reusable input validation functions.

batcharray.validation.validate_first_array

validate_first_array(data: Any) -> ndarray

Check that the first item of the data is an array and return it.

Parameters:

Name Type Description Default
data Any

The data to check. It can be an array or a nested structure of arrays.

required

Returns:

Type Description
ndarray

The first array of the data, in depth-first order.

Raises:

Type Description
ValueError

if the data is empty or its first item is not an array.

Example
>>> import numpy as np
>>> from batcharray.validation import validate_first_array
>>> validate_first_array({"a": np.ones((2, 3)), "b": np.zeros(2)}).shape
(2, 3)

batcharray.validation.validate_min_ndim

validate_min_ndim(
    array: ndarray, min_ndim: int = 2
) -> None

Check that the array has at least the given number of dimensions.

Parameters:

Name Type Description Default
array ndarray

The array to check.

required
min_ndim int

The minimum number of dimensions.

2

Raises:

Type Description
ValueError

if the array has fewer than min_ndim dimensions.

Example
>>> import numpy as np
>>> from batcharray.validation import validate_min_ndim
>>> validate_min_ndim(np.ones((2, 3)))

batcharray.validation.validate_same_batch_size

validate_same_batch_size(data: Any) -> None

Check that all the arrays have the same batch size.

This check is opt-in: the functions of batcharray.nested do not call it, because some valid calls involve arrays with different batch sizes (e.g. sum_along_batch).

Note

This function assumes the batch axis is the first axis.

Parameters:

Name Type Description Default
data Any

The data to check. It can be an array or a nested structure of arrays.

required

Raises:

Type Description
TypeError

if an item of the data is not an array.

ValueError

if some arrays have different batch sizes or if some arrays are 0-d.

Example
>>> import numpy as np
>>> from batcharray.validation import validate_same_batch_size
>>> validate_same_batch_size({"a": np.ones((2, 3)), "b": np.zeros(2)})

batcharray.validation.validate_same_keys

validate_same_keys(
    data: Sequence[dict[Hashable, Any]],
) -> None

Check that all the dictionaries have the same keys.

Parameters:

Name Type Description Default
data Sequence[dict[Hashable, Any]]

The dictionaries to check.

required

Raises:

Type Description
ValueError

if some dictionaries have different keys.

Example
>>> from batcharray.validation import validate_same_keys
>>> validate_same_keys([{"a": 1, "b": 2}, {"b": 3, "a": 4}])

batcharray.validation.validate_same_seq_size

validate_same_seq_size(data: Any) -> None

Check that all the arrays have the same sequence size.

This check is opt-in: the functions of batcharray.nested do not call it.

Note

This function assumes the sequence axis is the second axis.

Parameters:

Name Type Description Default
data Any

The data to check. It can be an array or a nested structure of arrays.

required

Raises:

Type Description
TypeError

if an item of the data is not an array.

ValueError

if some arrays have different sequence sizes or if some arrays have fewer than 2 dimensions.

Example
>>> import numpy as np
>>> from batcharray.validation import validate_same_seq_size
>>> validate_same_seq_size({"a": np.ones((2, 3)), "b": np.zeros((4, 3))})

batcharray.validation.validate_split_sections

validate_split_sections(
    size: int, split_size_or_sections: int | Sequence[int]
) -> None

Check that the split size or sections are valid for an axis.

Parameters:

Name Type Description Default
size int

The size of the axis to split.

required
split_size_or_sections int | Sequence[int]

The size of a single chunk, or the sizes of each chunk.

required

Raises:

Type Description
ValueError

if the split size is not positive, or if the sections are negative or do not sum to size.

Example
>>> from batcharray.validation import validate_split_sections
>>> validate_split_sections(5, 2)
>>> validate_split_sections(5, [2, 3])