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_element_by_name(xml, names)

Find a name from given XML element.

str2vec(string)

vec2str(vec)

num2str(x)

filter_keys(d[, prefix])

Filter keys with given prefix from a dictionary

filter_nan_values(d)

Filter nan values from dictionary

filter_set(s[, prefix])

Filter elements starting with given prefix from set.

val2str(d)

Convert dict values (numbers/vectors/bools) to strings.

str2bool(string)

bool2str(boolean)

create_symmetric_matrix(vec)

Create a symmetric matrix from given upper triangle values.

element_txt2num(xml, element_name[, default])

Convert element to a number, or return default if the element is missing.

fit_piecewise_linear(x_values, y_values)

Fit a piecewise linear function.

fit_spline(x_values, y_values)

Fit a spline, check validity, return polycoef.

create_transformation_matrix([pos, quat, ...])

Create a 4x4 transformation matrix.

calculate_mujoco_position(tag, name, M_WORLDBODY)

Calculate position of an element in MuJoCo model

is_linear(polycoef)

Check if polycoef represents a linear function.

get_body(OPENSIM, M_WORLDBODY, parent_socket_frame)

Get MuJoCo body corresponding to and OpenSim Body.

create_keyframe(MUJOCO, M_WORLDBODY, M_EQUALITY)

Create a keyframe for the MuJoCo model.

_set_default_value(M_EQUALITY, joints, default_values, ...)

Set default value for a joint.

Attributes

split_name

myoconverter.xml.utils.split_name[source]
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.str2vec(string)[source]
myoconverter.xml.utils.vec2str(vec)[source]
myoconverter.xml.utils.num2str(x)[source]
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.str2bool(string)[source]
myoconverter.xml.utils.bool2str(boolean)[source]
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