minimal_mjx.utils¶
Configs, logging, plotting, and setup helpers.
minimal_mjx.utils.config
¶
Classes:
| Name | Description |
|---|---|
FlowSeqDumper |
|
Config |
Reads a run's YAML config and hands its values to library functions. |
Functions:
| Name | Description |
|---|---|
read_yaml |
|
read_config |
Reads the YAML config file at |
create_config_dict |
Converts a dictionary to a ConfigDict. |
save_config |
Saves a ConfigDict (or dict) to a YAML file. |
get_commit_hash |
|
deepcopy_config |
|
FlowSeqDumper
¶
Bases: Dumper
Methods:
| Name | Description |
|---|---|
represent_sequence |
|
Source code in src/minimal_mjx/utils/config.py
represent_sequence
¶
Config
¶
Reads a run's YAML config and hands its values to library functions.
This is the only place that knows the YAML layout. Library functions take plain
arguments (run_dir, ppo_params, ...), so a different config layout only
needs a subclass that overrides these properties.
Methods:
| Name | Description |
|---|---|
load |
|
save |
Write the config to |
Attributes:
| Name | Type | Description |
|---|---|---|
cfg |
|
|
algorithm |
str
|
Learning algorithm, e.g. |
env_name |
str
|
Environment class name, e.g. |
backend |
str
|
Backend for evaluation environments, |
env_params |
ConfigDict
|
The |
ppo_params |
dict
|
Brax PPO settings, passed to the trainer as keyword arguments. |
network_params |
dict
|
Network settings for the configured algorithm. |
normalize_observations |
bool
|
Whether training keeps a running observation normalizer. |
name |
str
|
Run name. |
total_timesteps |
str
|
|
is_test |
bool
|
Runs named |
save_dir |
Path
|
Parent directory of all runs for this config. |
run_dir |
Path
|
Run directory: |
Source code in src/minimal_mjx/utils/config.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
env_params
property
¶
The env_config section, given to env classes as env_params.
ppo_params
property
¶
Brax PPO settings, passed to the trainer as keyword arguments.
normalize_observations
property
¶
Whether training keeps a running observation normalizer.
load
¶
save
¶
Write the config to path (default run_dir / 'config.yaml').
Runs not named test record the current git commit as git_hash.
Source code in src/minimal_mjx/utils/config.py
read_yaml
¶
read_config
¶
create_config_dict
¶
Converts a dictionary to a ConfigDict.
Source code in src/minimal_mjx/utils/config.py
save_config
¶
Saves a ConfigDict (or dict) to a YAML file.
Source code in src/minimal_mjx/utils/config.py
get_commit_hash
¶
Source code in src/minimal_mjx/utils/config.py
minimal_mjx.utils.logging
¶
Functions:
| Name | Description |
|---|---|
flatten_config |
Flatten a nested (dict-like) config so every leaf is keyed by its path, joined by |
unflatten_config |
Inverse of |
initialize_wandb |
Initialize and return a new W&B run. |
save_model |
Save a Brax checkpoint and optionally log it as a W&B artifact. |
save_training_state |
Overwrite the run's learner state, so a continued run picks up mid-stream. |
load_training_state |
|
save_run_id |
Record which W&B run wrote |
load_run_id |
The W&B run id a previous job left in |
get_latest_artifact |
Return the last-logged artifact of |
get_earliest_artifact |
Return the first artifact of |
download_model |
Download a W&B run's config and a policy checkpoint into |
Attributes:
| Name | Type | Description |
|---|---|---|
TRAIN_STATE_DIRNAME |
|
|
RUN_ID_FNAME |
|
flatten_config
¶
Flatten a nested (dict-like) config so every leaf is keyed by its path, joined by sep.
Source code in src/minimal_mjx/utils/logging.py
unflatten_config
¶
Inverse of flatten_config: rebuild a nested config from its a/b/c keys.
Source code in src/minimal_mjx/utils/logging.py
initialize_wandb
¶
Initialize and return a new W&B run.
Source code in src/minimal_mjx/utils/logging.py
save_model
¶
Save a Brax checkpoint and optionally log it as a W&B artifact.
Source code in src/minimal_mjx/utils/logging.py
save_training_state
¶
Overwrite the run's learner state, so a continued run picks up mid-stream.
TODO: clean up this docstring¶
A brax checkpoint holds what inference needs -- the policy's params and the observation normalizer. Training also depends on the optimizer's moments and on networks the policy never runs, such as a value function, so those are written here instead. Only the newest state is kept, since a run continues where it stopped.
Source code in src/minimal_mjx/utils/logging.py
load_training_state
¶
(step, state) restored into like's pytree, or None if the run saved none.
like is a freshly built state: orbax fills its arrays, so the restored state has
the same tree structure, shapes and dtypes by construction.
Source code in src/minimal_mjx/utils/logging.py
save_run_id
¶
Record which W&B run wrote output_dir, for a later job to continue it.
load_run_id
¶
The W&B run id a previous job left in output_dir, or None.
get_latest_artifact
¶
Return the last-logged artifact of run whose name contains prefix, or raise ValueError.
Source code in src/minimal_mjx/utils/logging.py
get_earliest_artifact
¶
Return the first artifact of run matching prefix written at or past
iterations training steps, or raise ValueError.
A checkpoint artifact records the step it was written at in metadata['iteration'],
so the choice is made from the run's artifact listing without downloading any of them.
Source code in src/minimal_mjx/utils/logging.py
download_model
¶
download_model(
run_id: str,
run_dir: Path | str,
entity: str,
project: str,
prefix: str,
iterations: float | None = None,
) -> str
Download a W&B run's config and a policy checkpoint into run_dir.
The config is saved, unchanged, as run_dir / 'config.yaml' (rebuilt from the
run's logged config if it has no config artifact). Its paths still point at where
the run trained, so update them before reusing it, e.g. with
:class:minimal_mjx.utils.config.Config.
The checkpoint is the last artifact whose name contains prefix, or, with
iterations, the first one written at or past that many training steps.
Returns the checkpoint's directory.
Source code in src/minimal_mjx/utils/logging.py
minimal_mjx.utils.plotting
¶
Plotting utilities for MuJoCo simulation data, reward metrics, and video export.
Classes:
| Name | Description |
|---|---|
InfoPlotter |
Collects arbitrary info-dict fields over time and exports to HDF5. |
MujocoPlotter |
Collects MuJoCo mjData fields over time and exports to HDF5. |
RewardPlotter |
Accumulates per-step reward components and produces a summary figure. |
Progress |
Brax PPO progress callback that accumulates the eval reward curve. |
Functions:
| Name | Description |
|---|---|
set_mpl_params |
Configure matplotlib for publication-quality LaTeX-style figures. |
get_subplot_grid |
Return (nrows, ncols) for a roughly square grid that fits n subplots. |
get_mj_scene_option |
Create an MjvOption with common visualization flags toggled. |
add_text_to_frame |
Overlay text on a frame (in-place) and return it. |
infer_frame_dim |
|
load_progress |
Refill a :class: |
check_directory_exists |
|
save_metrics |
Plot metrics from a RewardPlotter and save the figure to path. |
save_video |
Write a list of frames to an MP4 video at the environment control rate. |
load_dict_from_hdf5 |
Load all datasets from an HDF5 file into a {key: ndarray} dict. |
InfoPlotter
¶
Collects arbitrary info-dict fields over time and exports to HDF5.
Methods:
| Name | Description |
|---|---|
add_row |
Append one timestep of data from an info dict. |
to_numpy |
Convert all stored lists to numpy arrays in-place. |
save_to_h5 |
Write all data to an HDF5 file. |
Attributes:
| Name | Type | Description |
|---|---|---|
DEFAULT_PLOTKEY |
list[str]
|
|
plotkey |
list[str]
|
|
data |
dict[str, list[Any]]
|
|
Source code in src/minimal_mjx/utils/plotting.py
MujocoPlotter
¶
Collects MuJoCo mjData fields over time and exports to HDF5.
Methods:
| Name | Description |
|---|---|
add_row |
Append one timestep of data from an mjData-like object. |
to_numpy |
Convert all stored lists to numpy arrays in-place. |
save_to_h5 |
Write all data to an HDF5 file. |
time_idx |
Return the index of the last entry at or before time. |
Attributes:
| Name | Type | Description |
|---|---|---|
DEFAULT_PLOTKEY |
list[str]
|
|
plotkey |
list[str]
|
|
record_time |
bool
|
|
data |
dict[str, list[Any]]
|
|
Source code in src/minimal_mjx/utils/plotting.py
DEFAULT_PLOTKEY
class-attribute
instance-attribute
¶
plotkey
instance-attribute
¶
add_row
¶
Append one timestep of data from an mjData-like object.
Source code in src/minimal_mjx/utils/plotting.py
to_numpy
¶
save_to_h5
¶
Write all data to an HDF5 file.
time_idx
classmethod
¶
Return the index of the last entry at or before time.
Source code in src/minimal_mjx/utils/plotting.py
RewardPlotter
¶
Accumulates per-step reward components and produces a summary figure.
Methods:
| Name | Description |
|---|---|
add_row |
Record one timestep of metric values and total reward. |
plot |
Create a multi-subplot figure of all metrics plus total reward. |
Attributes:
| Name | Type | Description |
|---|---|---|
axkey |
dict[str, int]
|
|
axdata |
dict[str, list[float]]
|
|
rewards |
list[float]
|
|
Source code in src/minimal_mjx/utils/plotting.py
add_row
¶
Record one timestep of metric values and total reward.
plot
¶
Create a multi-subplot figure of all metrics plus total reward.
Source code in src/minimal_mjx/utils/plotting.py
Progress
¶
Brax PPO progress callback that accumulates the eval reward curve.
Pass :meth:plot_progress as brax's progress_fn. The curve is rewritten to
save_dir/progress.csv on every eval, and every scalar metric is logged to
run (e.g. a wandb run) when one is given.
Methods:
| Name | Description |
|---|---|
load |
Refill the curves from an earlier job's |
plot_progress |
Brax PPO progress callback: accumulate eval reward, log native W&B lines. |
Attributes:
| Name | Type | Description |
|---|---|---|
run |
|
|
times |
|
|
x_data |
|
|
y_data |
|
|
y_dataerr |
|
|
save_dir |
|
|
total_timesteps |
|
Source code in src/minimal_mjx/utils/plotting.py
load
¶
Refill the curves from an earlier job's progress.csv (see :func:load_progress).
Source code in src/minimal_mjx/utils/plotting.py
plot_progress
¶
Brax PPO progress callback: accumulate eval reward, log native W&B lines.
Logs every scalar brax metric via run.log so each renders as a native,
interactive W&B line chart, and writes the raw curve to progress.csv.
Source code in src/minimal_mjx/utils/plotting.py
set_mpl_params
¶
set_mpl_params(
label_size: int = 16,
title_size: int = 18,
tick_size: int = 14,
legend_size: int = 14,
) -> None
Configure matplotlib for publication-quality LaTeX-style figures.
Source code in src/minimal_mjx/utils/plotting.py
get_subplot_grid
¶
Return (nrows, ncols) for a roughly square grid that fits n subplots.
get_mj_scene_option
¶
get_mj_scene_option(
contacts: bool = False,
perts: bool = False,
com: bool = False,
geomgroup2: bool = True,
geomgroup3: bool = False,
) -> MjvOption
Create an MjvOption with common visualization flags toggled.
Source code in src/minimal_mjx/utils/plotting.py
add_text_to_frame
¶
add_text_to_frame(
pixels: ndarray,
text: str,
org: tuple[int, int],
size: float = 1,
thickness: int = 2,
color: tuple[int, int, int] = (255, 255, 255),
outline_color: tuple[int, int, int] | None = (0, 0, 0),
outline_thickness: int = 2,
) -> ndarray
Overlay text on a frame (in-place) and return it.
Source code in src/minimal_mjx/utils/plotting.py
infer_frame_dim
¶
load_progress
¶
Refill a :class:Progress's curves from the progress.csv a previous job wrote.
The callback rewrites the file from these lists on every eval, so a continued run
that started them empty would drop the epochs it inherited. before is the step it
restarts from: rows at or past it are left out, the run evaluating that state again
as its own first row.
Source code in src/minimal_mjx/utils/plotting.py
check_directory_exists
¶
Source code in src/minimal_mjx/utils/plotting.py
save_metrics
¶
save_metrics(
plotter: RewardPlotter,
path: Path = Path("visualization/metrics.pdf"),
) -> None
Plot metrics from a RewardPlotter and save the figure to path.
Source code in src/minimal_mjx/utils/plotting.py
save_video
¶
save_video(
frames: list[ndarray],
dt: float,
path: Path = Path("visualization/policy_rollout.mp4"),
) -> None
Write a list of frames to an MP4 video at the environment control rate.
Source code in src/minimal_mjx/utils/plotting.py
load_dict_from_hdf5
¶
Load all datasets from an HDF5 file into a {key: ndarray} dict.
Source code in src/minimal_mjx/utils/plotting.py
minimal_mjx.utils.state
¶
Classes:
| Name | Description |
|---|---|
EnvState |
|
Functions:
| Name | Description |
|---|---|
get_batch_shape |
Gets the batch size of an observation Pytree. |
EnvState
dataclass
¶
Methods:
| Name | Description |
|---|---|
replace |
Replace fields in the MujocoState. |
Attributes:
| Name | Type | Description |
|---|---|---|
data |
MjData
|
|
obs |
ndarray
|
|
reward |
float
|
|
done |
bool
|
|
metrics |
dict
|
|
info |
dict
|
|
Source code in src/minimal_mjx/utils/state.py
replace
¶
Replace fields in the MujocoState.
Source code in src/minimal_mjx/utils/state.py
get_batch_shape
¶
Gets the batch size of an observation Pytree.
Returns () if obs came from a single env, and (num_envs,) if obs came from a batched rollout.
minimal_mjx.utils.setupGPU
¶
Functions:
| Name | Description |
|---|---|
setup_gpu |
|
add_ICD_config |
Attempt to write an NVIDIA ICD config for libglvnd (Linux only). |
mujoco_EGL_rendering |
|
setup_XLA_Triton |
Optionally set XLA flags to enable Triton GEMM. Harmless if XLA not used, |
setup_intel_gpu |
|
check_gpu_connection |
Lightweight GPU presence check: return True if NVIDIA tools are available and responding. |
run_setup |
|
Attributes:
| Name | Type | Description |
|---|---|---|
logger |
|
|
INTEL_GPU |
|
setup_gpu
¶
Source code in src/minimal_mjx/utils/setupGPU.py
add_ICD_config
¶
Attempt to write an NVIDIA ICD config for libglvnd (Linux only). This file typically requires root privileges. We try to create it only on Linux and catch permission errors — if we can't write it, just warn.
Source code in src/minimal_mjx/utils/setupGPU.py
mujoco_EGL_rendering
¶
Source code in src/minimal_mjx/utils/setupGPU.py
setup_XLA_Triton
¶
Optionally set XLA flags to enable Triton GEMM. Harmless if XLA not used, but only useful for GPU-backed XLA computations.
Source code in src/minimal_mjx/utils/setupGPU.py
setup_intel_gpu
¶
Source code in src/minimal_mjx/utils/setupGPU.py
check_gpu_connection
¶
Lightweight GPU presence check: return True if NVIDIA tools are available and responding. Don't call this on macOS (no nvidia-smi there).
Source code in src/minimal_mjx/utils/setupGPU.py
run_setup
¶
Source code in src/minimal_mjx/utils/setupGPU.py
minimal_mjx.utils.delete_failed_runs
¶
Functions:
| Name | Description |
|---|---|
delete_failed_runs |
Finds and deletes all failed/crashed runs in a specified W&B project, |
main |
|
delete_failed_runs
¶
Finds and deletes all failed/crashed runs in a specified W&B project,
printing the number of associated logged artifacts for each run. Each
deletion is confirmed interactively unless assume_yes is set.