mani_skill.utils.common#
Common utilities often reused for internal code and task building for users.
Functions#
|
|
|
|
|
|
|
Append x2 in front of x1 and returns the result. Tries to do this in place if possible. |
|
Adds one dimension in front of everything. If given a dictionary, every leaf in the dictionary |
|
Compute angle (radian) between two torch tensors |
|
In place recursive merge of merge_dct into dct |
|
Flatten a dict by expanding its keys recursively. |
|
Flatten a dictionary containing states recursively. Expects all data to be either torch or numpy |
|
Indexes every array in x1 with slice and returns result. |
|
Merge multiple dicts with the same keys to a single one. |
|
normalizes a given torch tensor x and if the norm is less than eps, set the norm to 0 |
|
Compute angle (radian) between two numpy arrays |
|
normalizes a given numpy array x and if the norm is less than eps, set the norm to 0 |
|
Get the difference in radians between two quaternions. |
|
Maps any given sequence to a torch tensor on the CPU. |
|
|
|
Maps any given sequence to a torch tensor on the CPU/GPU. If physx gpu is not enabled then we use CPU, otherwise GPU, unless specified |
|
Recursively clones all torch tensors in a dictionary. |
|
Module Contents#
- mani_skill.utils.common._batch(array)[source]#
- Parameters:
array (Union[mani_skill.utils.structs.types.Array, Sequence]) –
- mani_skill.utils.common._to_numpy(array)[source]#
- Parameters:
array (Union[mani_skill.utils.structs.types.Array, Sequence]) –
- Return type:
numpy.ndarray
- mani_skill.utils.common._unbatch(array)[source]#
- Parameters:
array (Union[mani_skill.utils.structs.types.Array, Sequence]) –
- mani_skill.utils.common.append_dict_array(x1, x2)[source]#
Append x2 in front of x1 and returns the result. Tries to do this in place if possible. Assumes both x1, x2 have the same dictionary structure if they are dictionaries. They may also both be lists/sequences in which case this is just appending like normal
- Parameters:
x1 (Union[dict, Sequence, mani_skill.utils.structs.types.Array]) –
x2 (Union[dict, Sequence, mani_skill.utils.structs.types.Array]) –
- mani_skill.utils.common.batch(*args)[source]#
Adds one dimension in front of everything. If given a dictionary, every leaf in the dictionary has a new dimension. If given a tuple, returns the same tuple with each element batched
- Parameters:
args (Tuple[Union[mani_skill.utils.structs.types.Array, Sequence]]) –
- mani_skill.utils.common.compute_angle_between(x1, x2)[source]#
Compute angle (radian) between two torch tensors
- Parameters:
x1 (torch.Tensor) –
x2 (torch.Tensor) –
- mani_skill.utils.common.dict_merge(dct, merge_dct)[source]#
In place recursive merge of merge_dct into dct
- Parameters:
dct (dict) –
merge_dct (dict) –
- mani_skill.utils.common.flatten_dict_keys(d, prefix='')[source]#
Flatten a dict by expanding its keys recursively.
- Parameters:
d (dict) –
- mani_skill.utils.common.flatten_state_dict(state_dict, use_torch=False, device=None)[source]#
Flatten a dictionary containing states recursively. Expects all data to be either torch or numpy
- Parameters:
state_dict (dict) – a dictionary containing scalars or 1-dim vectors.
use_torch (bool) – Whether to convert the data to torch tensors.
device (Optional[mani_skill.utils.structs.types.Device]) –
- Raises:
AssertionError – If a value of @state_dict is an ndarray with ndim > 2.
- Returns:
flattened states.
- Return type:
np.ndarray | torch.Tensor
Notes
The input is recommended to be ordered (e.g. dict). However, since python 3.7, dictionary order is guaranteed to be insertion order.
- mani_skill.utils.common.index_dict_array(x1, idx, inplace=True)[source]#
Indexes every array in x1 with slice and returns result.
- Parameters:
idx (Union[int, slice]) –
- mani_skill.utils.common.merge_dicts(ds, asarray=False)[source]#
Merge multiple dicts with the same keys to a single one.
- Parameters:
ds (Sequence[dict]) –
- mani_skill.utils.common.normalize_vector(x, eps=1e-06)[source]#
normalizes a given torch tensor x and if the norm is less than eps, set the norm to 0
- Parameters:
x (torch.Tensor) –
- mani_skill.utils.common.np_compute_angle_between(x1, x2)[source]#
Compute angle (radian) between two numpy arrays
- Parameters:
x1 (numpy.ndarray) –
x2 (numpy.ndarray) –
- mani_skill.utils.common.np_normalize_vector(x, eps=1e-06)[source]#
normalizes a given numpy array x and if the norm is less than eps, set the norm to 0
- mani_skill.utils.common.quat_diff_rad(a, b)[source]#
Get the difference in radians between two quaternions.
- Parameters:
a (torch.Tensor) – first quaternion, shape (N, 4)
b (torch.Tensor) – second quaternion, shape (N, 4)
- Returns:
Difference in radians, shape (N,)
- Return type:
torch.Tensor
- mani_skill.utils.common.to_cpu_tensor(array)[source]#
Maps any given sequence to a torch tensor on the CPU.
- Parameters:
array (mani_skill.utils.structs.types.Array) –
- mani_skill.utils.common.to_numpy(array, dtype=None)[source]#
- Parameters:
array (Union[mani_skill.utils.structs.types.Array, Sequence]) –
- Return type:
numpy.ndarray
- mani_skill.utils.common.to_tensor(array, device=None)[source]#
Maps any given sequence to a torch tensor on the CPU/GPU. If physx gpu is not enabled then we use CPU, otherwise GPU, unless specified by the device argument
- Parameters:
array (mani_skill.utils.structs.types.Array) – The data to map to a tensor
device (Optional[mani_skill.utils.structs.types.Device]) – The device to put the tensor on. By default this is None and to_tensor will put the device on the GPU if physx is enabled and CPU otherwise