myoconverter.xml.utils
This module contains a collection of utility functions useful for parsing and converting the OpenSim model.
@author: Aleksi Ikkala
Module Contents
Functions
|
Find a name from given XML element. |
|
|
|
|
|
|
|
Filter keys with given prefix from a dictionary |
Filter nan values from dictionary |
|
|
Filter elements starting with given prefix from set. |
|
Convert dict values (numbers/vectors/bools) to strings. |
|
|
|
|
Create a symmetric matrix from given upper triangle values. |
|
|
Convert element to a number, or return default if the element is missing. |
|
Fit a piecewise linear function. |
|
Fit a spline, check validity, return polycoef. |
|
Create a 4x4 transformation matrix. |
|
Calculate position of an element in MuJoCo model |
|
Check if polycoef represents a linear function. |
|
Get MuJoCo body corresponding to and OpenSim Body. |
|
Create a keyframe for the MuJoCo model. |
|
Set default value for a joint. |
Attributes
- myoconverter.xml.utils.find_element_by_name(xml, names)[source]
Find a name from given XML element.
- Parameters:
xml – XML element
names – A list of names
- Returns:
- myoconverter.xml.utils.filter_keys(d, prefix='_')[source]
Filter keys with given prefix from a dictionary
- Parameters:
d – A dictionary
prefix – Prefix of keys to be filtered out
- Returns:
A dictionary without keys starting with prefix
- myoconverter.xml.utils.filter_nan_values(d)[source]
Filter nan values from dictionary
- Parameters:
d – A dictionary
- Returns:
A dictionary without nan values
- myoconverter.xml.utils.filter_set(s, prefix='_')[source]
Filter elements starting with given prefix from set.
- Parameters:
s – A set
prefix – Prefix of elements to be filtered
- Returns:
A set with elements starting with given prefix filtered
- myoconverter.xml.utils.val2str(d)[source]
Convert dict values (numbers/vectors/bools) to strings.
- Parameters:
d – A dictionary
- Returns:
Dictionary with numbers/vectors/bools converted to strings
- myoconverter.xml.utils.create_symmetric_matrix(vec)[source]
Create a symmetric matrix from given upper triangle values.
- Parameters:
vec – A vector of upper triangle values (xx,yy,zz,xy,xz,yz)
- Returns:
A symmetric 3x3 matrix
- myoconverter.xml.utils.element_txt2num(xml, element_name, default=np.nan)[source]
Convert element to a number, or return default if the element is missing.
- Parameters:
xml – XML element
element_name – Name of element to be retrieved
default – Default value if element is not found
- Returns:
Float if element is not found, otherwise default
- myoconverter.xml.utils.fit_piecewise_linear(x_values, y_values)[source]
Fit a piecewise linear function.
Useful when we don’t need to model the relationship as a quartic function.
- Parameters:
x_values – x coordinate values
y_values – y coordinate values
- Returns:
Piecewise linear model
- myoconverter.xml.utils.fit_spline(x_values, y_values)[source]
Fit a spline, check validity, return polycoef.
- Parameters:
x_values – x coordinate values
y_values – y coordinate values
- Returns:
Spline model, polycoefs, range
- Raises:
RunTimeError if not enough x/y coordinate values provided
- myoconverter.xml.utils.create_transformation_matrix(pos=None, quat=None, rotation_matrix=None, euler=None)[source]
Create a 4x4 transformation matrix.
Only one of quat, rotation_matrix, euler need to be defined (or none for identity rotation).
- Parameters:
pos – Translation vector
quat – Orientation as quaternion
rotation_matrix – Orientation as rotation matrix
euler – Orientation as euler angles
- Returns:
A 4x4 transformation matrix
- myoconverter.xml.utils.calculate_mujoco_position(tag, name, M_WORLDBODY)[source]
Calculate position of an element in MuJoCo model
- Parameters:
tag – Tag of an element
name – Name of an element
M_WORLDBODY – Pointer to MuJoCo XML worldbody
- Returns:
Position of the element in MuJoCo model (when in default pose)
- myoconverter.xml.utils.is_linear(polycoef)[source]
Check if polycoef represents a linear function.
- Parameters:
polycoef – Quartic coefficients of a function
- Returns:
Boolean indicating whether the polycoef represents a linear function
- myoconverter.xml.utils.get_body(OPENSIM, M_WORLDBODY, parent_socket_frame)[source]
Get MuJoCo body corresponding to and OpenSim Body.
- Parameters:
OPENSIM – Pointer to OpenSim XML model
M_WORLDBODY – Pointer to MuJoCo XML worldbody element
parent_socket_frame – Parent frame socket
- Returns:
MuJoCo XML element corresponding to Body
- Raises:
RuntimeError: If a body with given name is not found in the MuJoCo model
- myoconverter.xml.utils.create_keyframe(MUJOCO, M_WORLDBODY, M_EQUALITY)[source]
Create a keyframe for the MuJoCo model.
Set keyframe info into MuJoCo model based on joint default values and equality constraints. Default values are set as the “user” parameter for each joint in the MuJoCo file. Joint velocity, joint acceleration, and muscle internal states are set to zero.
- Parameters:
MUJOCO – Pointer to MuJoCo XML model
M_WORLDBODY – Pointer to MuJoCo XML worldbody element
M_EQUALITY – Pointer to MuJoCo XML equality element
- Returns:
None
- myoconverter.xml.utils._set_default_value(M_EQUALITY, joints, default_values, joint_names, index)[source]
Set default value for a joint.
- Parameters:
M_EQUALITY – Pointer to MuJoCo XML equality element
joints – A list of MuJoCo XML joint elements
default_values – A dictionary of default values for joints
joint_names – A list of joint names
index – Index of current joint in joints/joint_names
- Returns:
None