File size: 3,292 Bytes
00ff826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
"""Demon Attack training cross-task evaluation companion figure."""

from __future__ import annotations

import csv
from pathlib import Path
import sys

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.ticker import FuncFormatter


HERE = Path(__file__).resolve().parent
PAPER = HERE.parents[1]
sys.path.insert(0, str(PAPER))
import paper_colors


TASKS = ("Demon Attack", "Asterix", "Atlantis", "AirRaid")
SERIES = (
    ("zero_latency_train", "Zero-latency training", paper_colors.GRAY),
    ("mixed_latency_train", "Mixed-latency training", paper_colors.PURPLE),
)


def read_rows() -> dict[str, list[dict[str, float]]]:
    by_task: dict[str, list[dict[str, float]]] = {task: [] for task in TASKS}
    with (HERE / "data.csv").open(newline="", encoding="utf-8") as stream:
        for row in csv.DictReader(stream):
            by_task[row["task"]].append({
                "eval_latency": float(row["eval_latency"]),
                "zero_latency_train": float(row["zero_latency_train"]),
                "mixed_latency_train": float(row["mixed_latency_train"]),
            })
    for rows in by_task.values():
        rows.sort(key=lambda item: item["eval_latency"])
    return by_task


def compact_int(value: float, _position: int) -> str:
    if abs(value) >= 1000:
        return f"{value / 1000:g}k"
    return f"{value:g}"


def main() -> None:
    plt.style.use(PAPER / "plot_style.mplstyle")
    plt.rcParams.update(paper_colors.RC_COLORS)
    data = read_rows()

    fig, axes = plt.subplots(1, 4, figsize=(7.2, 2.25), sharex=True)
    fig.subplots_adjust(left=0.075, right=0.995, top=0.76, bottom=0.24, wspace=0.32)

    for ax, task in zip(axes, TASKS):
        rows = data[task]
        xs = [row["eval_latency"] for row in rows]
        max_value = max(row[key] for row in rows for key, _label, _color in SERIES)
        ymin = 0
        ymax = max_value * 1.18

        for key, label, color in SERIES:
            ys = [row[key] for row in rows]
            ax.plot(
                xs,
                ys,
                marker="o",
                markersize=3.1,
                linewidth=1.25,
                color=color,
                label=label,
            )
        ax.set_title(task, fontsize=8.4, weight="bold", pad=6)
        ax.set_xlim(-0.2, 4.2)
        ax.set_ylim(ymin, ymax)
        ax.set_xticks((0, 2, 4))
        ax.yaxis.set_major_formatter(FuncFormatter(compact_int))
        ax.tick_params(axis="both", labelsize=6.5)
        ax.grid(True, axis="y")
        ax.grid(False, axis="x")

    axes[0].set_ylabel("Mean return", fontsize=7.2)
    fig.supxlabel("Evaluation latency (raw frames)", fontsize=7.2, y=0.07)
    handles = [
        Line2D([0], [0], color=color, marker="o", markersize=3.2, linewidth=1.25, label=label)
        for _key, label, color in SERIES
    ]
    fig.legend(
        handles=handles,
        loc="upper center",
        bbox_to_anchor=(0.52, 0.995),
        ncols=2,
        columnspacing=1.6,
        handlelength=1.8,
        fontsize=6.5,
    )

    for extension in ("pdf", "png"):
        fig.savefig(HERE / f"fig.{extension}", dpi=300, bbox_inches="tight", pad_inches=0.025)
    plt.close(fig)


if __name__ == "__main__":
    main()