APIs: common¶
common.registry¶
- class common.registry.Registry(name: str = 'Registry', **kwargs)¶
A helper class for registering and managing modules
>>> # build registry >>> REGISTRY_NAME = Registry(name='REGISTRY_NAME')
>>> # register function >>> @REGISTRY_NAME.register >>> def foo(): >>> pass
>>> # register class >>> @REGISTRY_NAME.register >>> class bar(): >>> pass
>>> # fetch for class construction within builder >>> class_instance = REGISTRY_NAME[module_name](*args, **kwargs)
>>> # fetch for function call >>> result = REGISTRY_NAME[module_name](*args, **kwargs)
- __getitem__(name: str) Any¶
Return self[key].
- __init__(name: str = 'Registry', **kwargs)¶
- Parameters:
name – name of the registry
- __weakref__¶
list of weak references to the object (if defined)
- register(module: Any) Any¶
Register module (class/function/etc.) into this registry.
Typically used as decorator, thus return the input module itself.
- Parameters:
module – Python object that needs to be registered.
- Returns:
The registered module.
- Return type:
Any
- register_from_python_module(module: Any) Self¶
Register all members from a Python module.
- Parameters:
module – Python module to be processed.
- Returns:
The yielded registry.
- Return type:
Self
- common.registry.build_object_within_registry_from_config(registry: Registry, config: Dict[str, Any] = {}, **kwargs) Any¶
Builder function to build object within a registry from config.
Config should be in form of keyword arguments (dict-like). Support adding additional config items through kwargs.
NOTE: kwargs will not be deep-copied.
- Parameters:
registry – registry to retrieve class to be constructed.
config –
config function that provide the class name and the corresponding arguments, which should be arranged in the following format:
type: TYPENAME arg1: value1 arg2: value2 ...
**kwargs – key-word arguments to be passed to the retrieved class function.
- Returns:
The built object.
- Return type:
Any
common.io¶
- common.io.convert_TensorFP_to_numpy(tensor_proto: TensorFP) ndarray¶
Convert tensor proto to numpy array.
- Parameters:
tensor_proto – Tensor proto.
- Returns:
Converted numpy array.
- Return type:
np.ndarray
- common.io.convert_list_to_tuple_within_dict(dictionary: Dict, exceptions: Tuple[str] = ()) Dict¶
Recursively cast list to tuple within a dict.
Avoid modification issue (mutable / immutable). Support specified exception key.
Also deal with some type issues. e.g. in case of stable-baselines3, https://github.com/DLR-RM/stable-baselines3/blob/v2.2.1/stable_baselines3/common/off_policy_algorithm.py#L157
- Parameters:
dictionary – Dictionary to be processed.
exceptions – Exceptional keys.
- Returns:
Processed dictionary.
- Return type:
Dict
- common.io.convert_numpy_to_TensorFP(arr: ndarray) TensorFP¶
Convert numpy array to tensor proto.
- Parameters:
arr – Numpy array to be converted.
- Returns:
Converted tensor proto.
- Return type:
TensorFP
- common.io.generate_random_string(n: int) str¶
Generate a string with characters randomly chosen.
- Parameters:
n – Desired length of string.
- Returns:
Random string.
- Return type:
str
- common.io.load_and_override_configs(config_paths: List[str]) Dict¶
Load and override a series of config files.
- Parameters:
config_paths –
The base config and the overriding configs.
The first config will serve as base config.
The rest configs will override the base config, respectively.
- Returns:
The loaded and overridden config.
- Return type:
Dict
- common.io.load_config_from_yaml(config_file: str) Dict¶
Load config from yaml and handle exceptions.
- Parameters:
config_file – Path to configuration file.
- common.io.override_config(base_config: Dict, update_config: Dict, allow_new_key: bool = False) Dict¶
Override the value config.
- Parameters:
base_config – Base config to be processed.
update_config – Incremental config which contains key-value pair for overriding.
allow_new_key – Whether allow creation of new key.
- Returns:
The overridden config.
- Return type:
Dict
- common.io.save_config_to_yaml(config: Dict, config_file: str)¶
Save config to YAML file.
- Parameters:
config – Config object to be saved.
config_file – Path to YAML file.
common.gym_helper¶
- common.gym_helper.scale_action(action: ndarray, action_space: Space) ndarray¶
Scale action into range [-1, +1].
- Parameters:
action – Action to be sclaled.
action_space – Reference action space.
- Returns:
Scaled action.
- Return type:
np.ndarray
common.geometry¶
- common.geometry.normalize_angle(angle: float | ndarray) float | ndarray¶
Normalize angle to [-pi, pi), compatible with Numpy vectorization.
- Parameters:
angle – Angle to be normalized.
- Returns:
Normalized angle.
- common.geometry.transform_between_local_and_world(points: ndarray, body_state: ndarray, trans_dir: str) ndarray¶
Transform points between the local (body) frame and the world frame.
- Parameters:
points (np.ndarray) – Points to be transformed, shape=(N, 2).
body_state (np.ndarray) – Body state, format=<x, y, r>.
trans_dir (str) – Transform direction. ‘world_to_local’: from world frame to body frame. ‘local_to_world’: from body frame to world frame.
- Returns:
Transformed points, shape=(N, 2).
- Return type:
np.ndarray
- common.geometry.transform_points(points: ndarray, transform_matrix: ndarray) ndarray¶
Transform 2-D points with transform matrix.
- Parameters:
points – Points to be transformed, shape=(N, 2).
transform_matrix – Transform matrix which lies in group SO(2), shape=(3, 3).
- Returns:
Transformed points, shape=(N, 2).
- Return type:
np.ndarray
- common.geometry.transform_to_local_from_world(points: ndarray, body_state: ndarray) ndarray¶
Transform points from the world frame to the local (body) frame.
- Parameters:
points (np.ndarray) – Points in world frame, shape=(N, 2).
body_state (np.ndarray) – Body state, format=<x, y, r>.
- Returns:
Points in body frame, shape=(N, 2).
- Return type:
np.ndarray
- common.geometry.transform_to_world_from_local(points: ndarray, body_state: ndarray) ndarray¶
Transform points from the local (body) frame to the world frame.
- Parameters:
points (np.ndarray) – Points in body frame, shape=(N, 2).
body_state (np.ndarray) – Body state, format=<x, y, r>.
- Returns:
Points in world frame, shape=(N, 2).
- Return type:
np.ndarray