File size: 7,925 Bytes
b00d5d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""
Visualisation utilities β€” training curves and comparison plots.

Uses only Matplotlib (no Seaborn / Plotly dependency).
All plots are saved to disk (non-interactive backend).
"""

from __future__ import annotations

from pathlib import Path

import numpy as np

try:
    import matplotlib
    matplotlib.use("Agg")          # Non-interactive backend (safe on servers)
    import matplotlib.pyplot as plt
    _MPL_OK = True
except ImportError:
    _MPL_OK = False
    plt = None                     # type: ignore


# ── Helper ────────────────────────────────────────────────────────────────────

def _check_mpl():
    if not _MPL_OK:
        raise ImportError(
            "matplotlib is required for plotting.\n"
            "Install with:  pip install matplotlib"
        )


def _moving_average(values: list, window: int) -> list:
    """Simple unweighted moving average."""
    result = []
    for i in range(len(values)):
        start = max(0, i - window + 1)
        result.append(float(np.mean(values[start : i + 1])))
    return result


# ── Public functions ──────────────────────────────────────────────────────────

def plot_training_curves(metrics, save_path: str | Path | None = None) -> bool:
    """
    Plot four training metrics in a 2Γ—2 grid and save to *save_path*.

    Args:
        metrics:   A :class:`MetricsTracker` instance.
        save_path: Destination PNG path.  Shown interactively if None.

    Returns:
        True on success, False on failure.
    """
    try:
        _check_mpl()

        panel_cfg = [
            ("episode_reward",       "Episode Reward",       "blue",   "Reward"),
            ("average_waiting_time", "Avg Waiting Time",     "orange", "Waiting Time (s)"),
            ("average_queue_length", "Avg Queue Length",     "red",    "Queue Length"),
            ("throughput",           "Throughput",           "green",  "Vehicles Passed"),
        ]

        has_any = any(metrics.has(k) for k, *_ in panel_cfg)
        if not has_any:
            print("[WARN] No data available for plotting.")
            return False

        fig, axes = plt.subplots(2, 2, figsize=(15, 10))
        fig.suptitle("Training Progress", fontsize=16, fontweight="bold")
        axes_flat = axes.flatten()

        for ax, (key, title, colour, ylabel) in zip(axes_flat, panel_cfg):
            ax.set_title(title, fontsize=12, fontweight="bold")
            ax.set_xlabel("Episode", fontsize=10)
            ax.set_ylabel(ylabel, fontsize=10)
            ax.grid(True, alpha=0.3)

            if not metrics.has(key):
                ax.text(0.5, 0.5, "No data", ha="center", va="center",
                        transform=ax.transAxes, color="grey")
                continue

            vals = metrics.get(key)
            eps = range(1, len(vals) + 1)
            ax.plot(eps, vals, alpha=0.4, color=colour, linewidth=1, label="Raw")

            if len(vals) >= 10:
                w = min(50, max(10, len(vals) // 10))
                ma = _moving_average(vals, w)
                ax.plot(eps, ma, color=colour, linewidth=2,
                        label=f"MA-{w}")
                ax.legend(loc="best", fontsize=8)

        plt.tight_layout()

        if save_path:
            save_path = Path(save_path)
            save_path.parent.mkdir(parents=True, exist_ok=True)
            plt.savefig(save_path, dpi=150, bbox_inches="tight", facecolor="white")
            print(f"[OK] Plot saved -> {save_path}")
        else:
            plt.show()

        plt.close(fig)
        return True

    except ImportError as exc:
        print(f"[WARN] {exc}")
        return False
    except Exception as exc:
        print(f"[WARN] Plotting error: {exc}")
        try:
            plt.close("all")
        except Exception:
            pass
        return False


def plot_comparison(
    results_dict: dict[str, list],
    metric_name: str,
    save_path: str | Path | None = None,
) -> bool:
    """
    Overlay multiple result series on a single axes.

    Args:
        results_dict: ``{"Method Name": [values, …], …}``
        metric_name:  Y-axis label / title suffix.
        save_path:    Destination PNG path.

    Returns:
        True on success.
    """
    try:
        _check_mpl()

        if not results_dict:
            print("[WARN] No data for comparison plot.")
            return False

        fig, ax = plt.subplots(figsize=(12, 6))

        colours = ["blue", "green", "red", "orange", "purple"]
        for i, (name, vals) in enumerate(results_dict.items()):
            if vals:
                ax.plot(range(1, len(vals) + 1), vals,
                        label=name, linewidth=2, alpha=0.8,
                        color=colours[i % len(colours)])

        ax.set_xlabel("Episode", fontsize=12)
        ax.set_ylabel(metric_name, fontsize=12)
        ax.set_title(f"{metric_name} - Method Comparison",
                     fontsize=14, fontweight="bold")
        ax.legend(loc="best")
        ax.grid(True, alpha=0.3)

        plt.tight_layout()

        if save_path:
            save_path = Path(save_path)
            save_path.parent.mkdir(parents=True, exist_ok=True)
            plt.savefig(save_path, dpi=150, bbox_inches="tight", facecolor="white")
            print(f"[OK] Comparison plot saved -> {save_path}")
        else:
            plt.show()

        plt.close(fig)
        return True

    except ImportError as exc:
        print(f"[WARN] {exc}")
        return False
    except Exception as exc:
        print(f"[WARN] Comparison plot error: {exc}")
        try:
            plt.close("all")
        except Exception:
            pass
        return False


def plot_bar_comparison(
    method_scores: dict[str, float],
    title: str = "Method Comparison",
    ylabel: str = "Mean Reward",
    save_path: str | Path | None = None,
) -> bool:
    """
    Bar chart comparing scalar scores for different methods.

    Args:
        method_scores: {"Method": score, ...}
        title:         Chart title.
        ylabel:        Y-axis label.
        save_path:     Destination PNG path.

    Returns:
        True on success.
    """
    try:
        _check_mpl()

        if not method_scores:
            return False

        names = list(method_scores.keys())
        scores = [method_scores[n] for n in names]
        colours = ["#4472C4", "#ED7D31", "#A9D18E"]

        fig, ax = plt.subplots(figsize=(8, 5))
        bars = ax.bar(names, scores,
                      color=colours[: len(names)],
                      edgecolor="white", linewidth=1.5)

        # Value labels
        for bar, score in zip(bars, scores):
            ax.text(
                bar.get_x() + bar.get_width() / 2,
                bar.get_height() + (max(scores) - min(scores)) * 0.01,
                f"{score:.2f}",
                ha="center", va="bottom", fontsize=11, fontweight="bold",
            )

        ax.set_title(title, fontsize=14, fontweight="bold")
        ax.set_ylabel(ylabel, fontsize=12)
        ax.grid(axis="y", alpha=0.3)
        ax.set_ylim(min(scores) * 1.05, max(scores) * 0.95)  # Tight y-range

        plt.tight_layout()

        if save_path:
            save_path = Path(save_path)
            save_path.parent.mkdir(parents=True, exist_ok=True)
            plt.savefig(save_path, dpi=150, bbox_inches="tight", facecolor="white")
            print(f"[OK] Bar chart saved -> {save_path}")
        else:
            plt.show()

        plt.close(fig)
        return True

    except ImportError as exc:
        print(f"[WARN] {exc}")
        return False
    except Exception as exc:
        print(f"[WARN] Bar chart error: {exc}")
        return False