Muscle-Command Stages

Ordered muscle-command stages that wrappers install in a CPU env.

An env maps its action to a muscle excitation (sigmoid, or as-is for the walk envs) and then runs the installed stages before writing ctrl:

map (10) -> noise (20) -> fatigue (30) -> reroute (40) -> custom stages -> ctrl (100)

No stage is installed by default: a plain env runs only its own map. The myoFati* / myoReaf* ids and the wrappers install them; noise additionally needs a nonzero level. The three built-in stages have a fixed order (STAGE_ORDER), a property of the stage and not of the wrapper nesting, so a stack built in any order behaves the same. The wrappers in myosuite.envs.wrappers (MotorNoiseWrapper, FatigueWrapper, ReafferentationWrapper) install one stage each through CtrlStageHost.add_ctrl_stage().

Custom stages run after the built-in ones, in the order they were installed (the wrapping order, or the order of EnvConfig.features): nothing to configure. Only to insert one earlier, give it an explicit order between MAP_ORDER and WRITE_ORDER; two stages with the same explicit order run in installation order and trigger a StageOrderWarning.

Two kinds of custom stages exist:

  • a portable ExcitationStage (ExcitationStageWrapper) works on the muscle excitations only, written for numpy and torch, and runs on the CPU env and on its mjlab twin;

  • an env-aware stage (CtrlStageWrapper) gets the host env and runs on the CPU only.

myosuite.envs.muscle_stages.MAP_ORDER = 10

Priority of the env’s own action-to-excitation map; stages run after it.

myosuite.envs.muscle_stages.WRITE_ORDER = 100

Priority of the ctrl write; stages run before it.

myosuite.envs.muscle_stages.STAGE_ORDER: dict[str, int] = {'fatigue': 30, 'noise': 20, 'reroute': 40}

Priorities of the built-in stages (fixed, ascending).

myosuite.envs.muscle_stages.LATE_ORDER = 50

after the built-ins, in the order the stages were installed.

Type:

Priority of a custom stage without an explicit order

myosuite.envs.muscle_stages.CTRL_STAGE_ORDER: tuple[str, ...] = ('noise', 'fatigue', 'reroute')

Names of the built-in stages in the order they run.

exception myosuite.envs.muscle_stages.StageOrderWarning

Bases: UserWarning

Two stages have the same order, so their relative order is only the name order.

myosuite.envs.muscle_stages.check_custom_order(name, order)

Raise if an explicit order is not strictly between map and ctrl write (None is fine).

Return type:

None

Parameters:
myosuite.envs.muscle_stages.warn_order_clash(name, order, installed, stacklevel=3)

Warn prominently if name has the same order as an installed stage.

Parameters:
  • name (str) – The stage being installed.

  • order (float) – Its order.

  • installed (dict[str, float]) – {stage name: order} of the stages already installed.

  • stacklevel (int) – Frame the warning is attributed to.

Return type:

None

class myosuite.envs.muscle_stages.ExcitationStage

Bases: object

Base class of a portable custom stage on the muscle excitations.

Subclass it and implement __call__(u, xp): u holds the muscle excitations ((n_muscles,) numpy on the CPU env, (n_envs, n_muscles) torch on mjlab), xp is the matching array module (numpy or torch); return the new excitations. Use only operations both modules share (xp.clip, xp.where, xp.zeros_like, arithmetic, …) and no randomness unless it comes from xp. Keep state in arrays created from u (xp.zeros_like(u)) so that it has the right shape and device, and clear it in reset().

name

Unique stage name (not a built-in name).

order

None (default): runs after the built-in stages, in installation order; or a priority strictly between MAP_ORDER and WRITE_ORDER to insert it earlier.

reset(env_ids=None)

Clear the state of the given envs (None or slice(None): all).

Return type:

None

Parameters:

env_ids (Any | None)

class myosuite.envs.muscle_stages.LowPassStage(alpha=0.3, name='lowpass', order=None)

Bases: ExcitationStage

First-order low-pass filter on the excitations (a portable example stage).

y <- y + alpha (u - y); the first step after a reset passes u through.

Parameters:
  • alpha (float) – Smoothing factor in (0, 1]; 1 is no filtering.

  • name (str) – Stage name.

  • order (Optional[float]) – Stage order.

reset(env_ids=None)

Clear the state of the given envs (None or slice(None): all).

Return type:

None

Parameters:

env_ids (Any | None)

myosuite.envs.muscle_stages.CtrlStage

stage(env, ctrl) -> ctrl: edits (a copy of) the control vector.

myosuite.envs.muscle_stages.ResetStage

reset(env): per-episode reset, called where the env reset its muscle state.

myosuite.envs.muscle_stages.reject_removed_kwargs(env_cls, kwargs)

Raise TypeError for the constructor kwargs that wrappers replaced.

Return type:

None

Parameters:
class myosuite.envs.muscle_stages.CtrlStageHost

Bases: object

Mixin of the CPU envs whose action pipeline runs wrapper-installed stages.

The host env provides model, np_random, _muscle_act_ind (the muscle entries of ctrl) and calls _run_ctrl_stages() on the excitation vector and _run_reset_stages() in reset().

add_ctrl_stage(name, apply, reset=None, order=None)

Install a stage; it runs at its priority, after the env’s map.

Parameters:
  • name (str) – A built-in stage (STAGE_ORDER) or a new unique name.

  • apply (Callable[[Any, ndarray], ndarray]) – apply(env, ctrl) -> ctrl.

  • reset (Optional[Callable[[Any], None]]) – Optional per-episode reset of the stage’s state.

  • order (Optional[float]) – None (default) for a custom stage: after the built-in stages, in installation order. Or a priority strictly between MAP_ORDER and WRITE_ORDER to insert it earlier. Built-in stages have a fixed priority (omit it). The same explicit order as another installed stage triggers a StageOrderWarning.

Raises:

ValueError – If name is already installed, an explicit order is out of range, or a built-in stage gets another order.

Return type:

None

remove_ctrl_stage(name)

Uninstall a stage (no-op if it is not installed).

Return type:

None

Parameters:

name (str)

property ctrl_stages: tuple[str, ...]

Names of the installed stages, in the order they run.

property muscle_fatigue: Any

Fatigue model of the installed FatigueWrapper.

Raises:

AttributeError – If no fatigue stage is installed.

myosuite.envs.muscle_stages.noise_stage(get_cfg)

Signal-dependent + constant Gaussian noise on the muscle excitations.

Parameters:

get_cfg (Callable[[], Any]) – Returns the current MotorNoiseCfg (read every step, so the wrapper’s config can change). A disabled config draws nothing, so the env’s random stream is untouched.

Return type:

Callable[[Any, ndarray], ndarray]

myosuite.envs.muscle_stages.fatigue_stage(fatigue, get_reset, index=None)

3CC-r fatigue on the muscle excitations and its per-episode reset.

Parameters:
  • fatigue (Any) – A CumulativeFatigue.

  • get_reset (Callable[[], tuple[Any, bool]]) – Returns (fatigue_reset_vec, fatigue_reset_random).

  • index (Optional[Any]) – Entries of ctrl the fatigue state tracks (default: the env’s muscle excitations, _stage_muscle_index()).

Return type:

tuple[Callable[[Any, ndarray], ndarray], Callable[[Any], None]]

myosuite.envs.muscle_stages.reroute_stage(model, suffix)

Reafferentation: EIP’s command drives EPL and EIP is silenced.

Return type:

Callable[[Any, ndarray], ndarray]

Parameters:
myosuite.envs.muscle_stages.muscle_mask(model)

Boolean mask of the muscle actuators of model.

Return type:

ndarray

Parameters:

model (MjModel)