jacinle.nd.meta#

Functions

is_ndarray(thing)

isndarray(thing)

Check if the given object is a numpy array.

nd_batch_size(thing)

Get the batch size of a numpy array.

nd_concat(list_of_arrays)

Concatenate a list of numpy arrays.

nd_len(thing)

Get the length of a numpy array.

nd_split_n(ndarray, n)

Split a numpy array into n parts.

size_split_n(full_size, n)

Split a size into n parts.

Functions

is_ndarray(thing)[source]#
Parameters:

thing (Any)

Return type:

bool

isndarray(thing)[source]#

Check if the given object is a numpy array.

Parameters:

thing (Any)

Return type:

bool

nd_batch_size(thing)[source]#

Get the batch size of a numpy array. This function handles the case when the input a nested list or dict.

Examples

>>> nd_batch_size(np.array([1, 2, 3]))
3
>>> nd_batch_size([np.zeros((2, 3)), np.zeros((2, 5))])
2
>>> nd_batch_size({'a': np.zeros((2, 3)), 'b': np.zeros((2, 5))})
2
Parameters:

thing (Any) – the input array or nested list/dict.

Returns:

the batch size of the array.

Return type:

int

nd_concat(list_of_arrays)[source]#

Concatenate a list of numpy arrays. This function handles the case when the list is empty or contains only one element.

Parameters:

list_of_arrays (Sequence[ndarray]) – a list of numpy arrays.

Returns:

the concatenated array, or None if the list is empty.

Return type:

ndarray | None

nd_len(thing)[source]#

Get the length of a numpy array. This function handles the case when the input is a scalar or plain Python objects.

Parameters:

thing (Any) – the input array.

Returns:

the length of the array, or 1 if the input is a scalar or plain Python objects.

Return type:

int

nd_split_n(ndarray, n)[source]#

Split a numpy array into n parts. If the size is not divisible by n, the last part will be larger.

Parameters:
  • ndarray (ndarray) – the array to be split.

  • n (int) – the number of parts.

Returns:

a list of arrays.

Return type:

List[ndarray]

size_split_n(full_size, n)[source]#

Split a size into n parts. If the size is not divisible by n, the last part will be larger. When the size is None, None will be returned.

Parameters:
  • full_size (int | None) – the size to be split.

  • n (int) – the number of parts.

Returns:

a list of sizes.

Return type:

List[int] | None