vis4d.common.array

This module contains array utility functions.

Functions

array_to_numpy(data[, n_dims, dtype])

Converts a given array like object to a numpy array.

arrays_to_numpy(*args[, n_dims, dtype])

Converts a given sequence of optional ArrayLike objects to numpy.

array_to_numpy(data, n_dims=None, dtype=<class 'numpy.float32'>)[source]

Converts a given array like object to a numpy array.

Helper function to convert an array like object to a numpy array. This functions converts torch.Tensors or Sequences to numpy arrays.

If the argument is None, None will be returned.

Examples: >>> convert_to_array([1,2,3]) >>> # -> array([1,2,3]) >>> convert_to_array(None) >>> # -> None >>> convert_to_array(torch.tensor([1,2,3]).cuda()) >>> # -> array([1,2,3]) >>> convert_to_array([1,2,3], n_dims = 2).shape >>> # -> [1, 3]

Parameters:
  • data (ArrayLike | None) – ArrayLike object that should be converted to numpy.

  • n_dims (int | None, optional) – Target number of dimension of the array. If the provided array does not have this shape, it will be squeezed or exanded (from the left). If it still does not match, an error is raised.

  • | (dtype (type[NumpyBool] | type[NumpyFloat] | type[NumpyInt]) – type[NumpyUInt], optional): Target dtype of the array. Defaults to np.float32.

Raises:

ValueError – If the provied array like objects can not be converted with the target dimensions.

Returns:

The converted numpy array or None if None was

provided.

Return type:

NDArrayNumber | None

arrays_to_numpy(*args, n_dims=None, dtype=<class 'numpy.float32'>)[source]

Converts a given sequence of optional ArrayLike objects to numpy.

Parameters:
  • args (ArrayLike | None) – Provided arguments.

  • n_dims (int | None, optional) – Target number of dimension of the array. If the provided array does not have this shape, it will be squeezed or exanded (from the left). If it still does not match, an error is Raised.

  • | (dtype (type[NumpyBool] | type[NumpyFloat] | type[NumpyInt]) – type[NumpyUInt], optional): Target dtype of the array. Defaults to np.float32.

Raises:

ValueError – If the provied array like objects can not be converted with the target dimensions.

Returns:

The converted arguments as numpy array.

Return type:

tuple[NDArrayNumber | None]