Skip to content

plot_config_delta

Applying a small edit to the trial viewer's plot config, the way SensAI changes the plot.

The agent never retypes the whole config. It sends only what changes, in the shape of a JSON Merge Patch (RFC 7386): an object merges into the config key by key, and anything else, a value or a list, replaces what was there. One difference from the RFC: null never deletes anything. Models write null for "nothing here", so reading it as a removal wiped signals and reset fields that were meant to stay. null is only accepted as a value, for the few fields that allow it (an automatic range, say), and a signal is removed with remove_plot_signals. The edit is applied to the current config and the result is checked, so a field the config does not have, a signal the trial does not have, or a value out of range is refused with a message the agent can act on, instead of landing in the plot.

PlotConfigDeltaError

Bases: ValueError

An edit that cannot be applied, worded for the agent that wrote it so it can correct itself.

compact_plot_config

Python
compact_plot_config(config: PlotConfig) -> dict[str, Any]

The config without anything that has its default value, so a prompt carries only what has been set.

Filters stay whole when there are any, since their type is a default value too and the filter would be unreadable without it. A signal always keeps its color, which has no default.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/plot_config_delta.py
Python
def compact_plot_config(config: PlotConfig) -> dict[str, Any]:
    """
    The config without anything that has its default value, so a prompt carries only what has been set.

    Filters stay whole when there are any, since their `type` is a default value too and the filter would be unreadable without it. A signal always keeps its color, which has no default.
    """
    full = config.model_dump(mode="json")
    default = PlotConfig().model_dump(mode="json")
    compact: dict[str, Any] = {
        key: value
        for key, value in full.items()
        if key not in ("xAxis", "yAxes") and value != default[key]
    }
    x_axis = {
        key: value
        for key, value in full["xAxis"].items()
        if value != default["xAxis"][key]
    }
    if x_axis:
        compact["xAxis"] = x_axis
    signals: dict[str, Any] = {}
    for name, entry in full["yAxes"].items():
        base = YAxisConfig(color=entry["color"]).model_dump(mode="json")
        signals[name] = {
            key: value
            for key, value in entry.items()
            if key == "color" or value != base[key]
        }
    if signals:
        compact["yAxes"] = signals
    return compact

apply_plot_config_delta

Python
apply_plot_config_delta(
    config: PlotConfig,
    delta: Mapping[str, Any],
    columns: Collection[str] = (),
) -> PlotConfig

The config with the edit delta applied. config itself is not changed.

Parameters:

Name Type Description Default
config PlotConfig

The current plot config.

required
delta Mapping[str, Any]

The edit, as a JSON Merge Patch in which null is never a removal. Keys may be written as field names or in camelCase.

required
columns Collection[str]

The columns of the trial. When there are any, a signal or x-axis column the edit names has to be one of them; when there are none (nothing is known about the trial) names are not checked.

()

Raises:

Type Description
PlotConfigDeltaError

If a field does not exist, a signal is not in the trial, or the result is not a valid config. The message says what to fix.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/plot_config_delta.py
Python
def apply_plot_config_delta(
    config: PlotConfig, delta: Mapping[str, Any], columns: Collection[str] = ()
) -> PlotConfig:
    """
    The config with the edit `delta` applied. `config` itself is not changed.

    Args:
        config: The current plot config.
        delta: The edit, as a JSON Merge Patch in which `null` is never a removal. Keys may be written as field names or in camelCase.
        columns: The columns of the trial. When there are any, a signal or x-axis column the edit names has to be one of them; when there are none (nothing is known about the trial) names are not checked.

    Raises:
        PlotConfigDeltaError: If a field does not exist, a signal is not in the trial, or the result is not a valid config. The message says what to fix.

    """
    normalized = _normalize_patch(delta, config, columns)
    merged = _merge_patch(config.model_dump(mode="json"), normalized)
    try:
        return PlotConfig.model_validate(merged)
    except ValidationError as exc:
        raise PlotConfigDeltaError(_describe(exc)) from exc

remove_plot_signals

Python
remove_plot_signals(
    config: PlotConfig, names: Collection[str]
) -> PlotConfig

The config without the signals named. config itself is not changed.

Raises:

Type Description
PlotConfigDeltaError

If no name is given, or a name is not plotted. The message lists what is plotted.

Source code in src/mujoco_mojo/utils/layers/dojo/sensai/plot_config_delta.py
Python
def remove_plot_signals(config: PlotConfig, names: Collection[str]) -> PlotConfig:
    """
    The config without the signals named. `config` itself is not changed.

    Raises:
        PlotConfigDeltaError: If no name is given, or a name is not plotted. The message lists what is plotted.

    """
    plotted = config.y_axes
    wanted = list(dict.fromkeys(names))
    if not wanted:
        raise PlotConfigDeltaError("Name at least one signal to remove.")
    missing = [name for name in wanted if name not in plotted]
    if missing:
        raise PlotConfigDeltaError(
            f"Not plotted, so there is nothing to remove: {', '.join(missing)}. "
            f"Plotted signals: {', '.join(plotted) or 'none'}."
        )
    data = config.model_dump(mode="json")
    for name in wanted:
        del data["yAxes"][name]
    return PlotConfig.model_validate(data)