Datasets:

ArXiv:
File size: 11,868 Bytes
44706c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse
import glob
import os
from typing import List, Optional, Tuple

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import re


ENV_LIST: List[str] = [
    "AutoEncodeEasy",
    "BattleShipEasy",
    "BreakoutEasy",
    "CartPoleEasy",
    "CountRecallEasy",
    "MineSweeperEasy",
    "NavigatorEasy",
    "NoisyCartPoleEasy",
    "SkittlesEasy",
    "TetrisEasy",
]


plt.rcParams['text.usetex'] = True
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = ['Arial']
plt.rcParams['text.latex.preamble'] = r'\usepackage{amsmath} \usepackage{amssymb} \usepackage{amsfonts}'

def _find_csvs_for(env_name: str, partial: bool, recall_density_dir: str) -> List[str]:
    pattern = os.path.join(
        recall_density_dir, f"saliency_results_*_{env_name}_Partial={partial}_*.csv"
    )
    return sorted(glob.glob(pattern))


def _extract_pos_columns(df: pd.DataFrame) -> Tuple[np.ndarray, List[str]]:
    pos_cols = [c for c in df.columns if c.startswith("pos_")]
    # Sort by numeric suffix
    def _key(c: str) -> float:
        try:
            return float(c.split("pos_")[-1])
        except Exception:
            return 0.0

    pos_cols.sort(key=_key)
    return df[pos_cols].to_numpy(dtype=float), pos_cols


def _thirds_from_distribution_rows(pos_values: np.ndarray) -> np.ndarray:
    """Given matrix [num_rows, num_positions], compute thirds per row.

    Returns array [num_rows, 3] with per-third sums normalized so each row sums to 1
    (if a row sums to 0, it remains zeros).
    """
    if pos_values.size == 0:
        return np.zeros((pos_values.shape[0], 3), dtype=float)

    num_cols = pos_values.shape[1]
    e1 = num_cols // 3
    e2 = (num_cols * 2) // 3
    thirds = np.stack(
        [
            pos_values[:, :e1].sum(axis=1),
            pos_values[:, e1:e2].sum(axis=1),
            pos_values[:, e2:].sum(axis=1),
        ],
        axis=1,
    )
    row_sums = thirds.sum(axis=1, keepdims=True)
    norm = np.zeros_like(thirds, dtype=float)
    mask = row_sums[:, 0] > 0
    if np.any(mask):
        norm[mask] = thirds[mask] / row_sums[mask]
    return norm


def compute_env_mode_summary(env_name: str, partial: bool, recall_density_dir: str) -> np.ndarray:
    """Aggregate all CSVs for (env, partial) across files and seeds.

    Returns a vector of length 3 that sums to 1 (or zeros if nothing found).
    """
    csv_files = _find_csvs_for(env_name, partial, recall_density_dir)
    if not csv_files:
        return np.zeros(3, dtype=float)

    thirds_all = []
    for path in csv_files:
        try:
            df = pd.read_csv(path)
        except Exception:
            continue
        if df.empty:
            continue
        pos_values, _ = _extract_pos_columns(df)
        thirds_rows = _thirds_from_distribution_rows(pos_values)
        if thirds_rows.size == 0:
            continue
        thirds_all.append(thirds_rows)

    if not thirds_all:
        return np.zeros(3, dtype=float)

    thirds_concat = np.concatenate(thirds_all, axis=0)  # [num_total_rows, 3]
    mean_thirds = thirds_concat.mean(axis=0)
    mean_sum = mean_thirds.sum()
    return mean_thirds / mean_sum if mean_sum > 0 else np.zeros(3, dtype=float)


def _load_data_from_summary_csv(summary_csv: str) -> np.ndarray:
    """Load pre-aggregated bar data with columns EnvName, Partial, third_1..third_3."""
    df = pd.read_csv(summary_csv)
    if df.empty:
        raise ValueError(f"Summary CSV is empty: {summary_csv}")

    required_columns = {"EnvName", "Partial", "third_1", "third_2", "third_3"}
    missing = required_columns.difference(df.columns)
    if missing:
        raise ValueError(
            f"Summary CSV is missing required columns: {sorted(missing)}"
        )

    partial_series = df["Partial"].map(
        lambda x: x if isinstance(x, bool) else str(x).strip().lower() == "true"
    )

    data = np.zeros((len(ENV_LIST), 2, 3), dtype=float)
    partial_modes = [False, True]
    for i, env in enumerate(ENV_LIST):
        for j, part in enumerate(partial_modes):
            match = df[(df["EnvName"] == env) & (partial_series == part)]
            if match.empty:
                continue
            row = match.iloc[0]
            thirds = np.array([row["third_1"], row["third_2"], row["third_3"]], dtype=float)
            total = thirds.sum()
            data[i, j] = thirds / total if total > 0 else np.zeros(3, dtype=float)
    return data


def _load_data_from_recall_density_dir(recall_density_dir: str) -> np.ndarray:
    """Aggregate raw recall-density CSVs into plot-ready thirds data."""
    partial_modes = [False, True]
    data = np.zeros((len(ENV_LIST), len(partial_modes), 3), dtype=float)
    for i, env in enumerate(ENV_LIST):
        for j, part in enumerate(partial_modes):
            data[i, j] = compute_env_mode_summary(env, part, recall_density_dir)
    return data


def _plot_mode(env_values: np.ndarray, colors: list[str], mode_title: str, output_path: str, dpi: int) -> None:

    fig, ax = plt.subplots(figsize=(min(20, 1.8 * len(ENV_LIST)), 7))
    x = np.arange(len(ENV_LIST))

    # Stacked thirds for single mode
    left = np.zeros(len(ENV_LIST))
    for k in range(3):
        values = env_values[:, k]
        ax.bar(
            x,
            values,
            0.6,
            bottom=left,
            color=colors[k],
            edgecolor="white",
            linewidth=0.6,
            alpha=0.9,
        )
        left += values

    # x ticks and fonts
    ax.set_xticks(x)
    ax.set_xticklabels([e.replace("Easy", "") for e in ENV_LIST], rotation=30, ha="right", fontsize=30)
    ax.tick_params(axis="both", labelsize=30)

    ax.set_ylim(0, 1.05)
    ax.set_ylabel(r"Saliency mass per third", fontsize=14)

    # Legend (thirds only)
    third_labels = [r"$[0,\frac{1}{3})$", r"$[\frac{1}{3},\frac{2}{3})$", r"$[\frac{2}{3},1)$"]
    legend_handles = [
        plt.Rectangle((0, 0), 1, 1, color=colors[0]),
        plt.Rectangle((0, 0), 1, 1, color=colors[1]),
        plt.Rectangle((0, 0), 1, 1, color=colors[2]),
    ]
    ax.legend(legend_handles, third_labels, loc="center left", bbox_to_anchor=(1.02, 0.5), fontsize=30, frameon=True, fancybox=True, handlelength=1.2, handletextpad=0.6)

    ax.set_title(f"Aggregate Recall Density — {mode_title}", fontsize=40)
    plt.tight_layout(rect=[0, 0, 0.82, 1])
    out_dir = os.path.dirname(output_path)
    if out_dir and not os.path.exists(out_dir):
        os.makedirs(out_dir, exist_ok=True)
    fig.savefig(output_path, dpi=dpi)
    
    # If the user didn't request a PDF explicitly, save a copy as PDF too
    base, ext = os.path.splitext(output_path)
    if ext.lower() != ".pdf":
        fig.savefig(base + ".pdf")
    plt.close(fig)


def _plot_bars_on_ax(ax, env_values: np.ndarray, colors: list[str], show_left_axis: bool, ylabel_math: str | None = None):
    x = np.arange(len(ENV_LIST))
    left = np.zeros(len(ENV_LIST))
    for k in range(3):
        vals = env_values[:, k]
        ax.bar(
            x,
            vals,
            0.6,
            bottom=left,
            color=colors[k],
            edgecolor="white",
            linewidth=0.6,
            alpha=0.9,
        )
        left += vals
    ax.set_xticks(x)
    # Use single-line labels 

    single_line_labels = [e.replace("Easy", "") for e in ENV_LIST]
    ax.set_xticklabels(single_line_labels, rotation=30, ha="right", fontsize=30)
    # ax.tick_params(axis="x", pad=15)
    ax.set_ylim(0, 1.05)
    # Beautify: remove top/right spines
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    if show_left_axis:
        ax.tick_params(axis="y", labelsize=35, left=True, labelleft=True)
        if ylabel_math:
            ax.set_ylabel(ylabel_math, fontsize=35, rotation=90, labelpad=10)
    else:
        ax.tick_params(axis="y", left=False, labelleft=False)


def plot_summary(
    output_path: str,
    dpi: int = 300,
    recall_density_dir: Optional[str] = None,
    summary_csv: Optional[str] = None,
):
    if summary_csv is not None:
        data = _load_data_from_summary_csv(summary_csv)
    elif recall_density_dir is not None:
        data = _load_data_from_recall_density_dir(recall_density_dir)
    else:
        raise ValueError("Provide either recall_density_dir or summary_csv.")

    # Colors
    mdp_colors = ["#C6DBEF", "#6BAED6", "#2171B5"]
    pomdp_colors = ["#FDD0A2", "#FDAE6B", "#E6550D"]

    fig = plt.figure(figsize=(max(32, 1.7 * len(ENV_LIST) * 2), 8.0))
    gs = fig.add_gridspec(1, 3, width_ratios=[2.2, 0.9, 2.2], wspace=0.12)
    ax_left = fig.add_subplot(gs[0, 0])
    ax_center = fig.add_subplot(gs[0, 1])
    ax_right = fig.add_subplot(gs[0, 2])

    ylabel_math = r"$\mathbb{E}_{\pi, f}[\,\delta(Q(\mathbf{x},\tau))\,]$"
    _plot_bars_on_ax(ax_left, data[:, 0, :], mdp_colors, show_left_axis=True, ylabel_math=ylabel_math)
    _plot_bars_on_ax(ax_right, data[:, 1, :], pomdp_colors, show_left_axis=False)

    # Center block: title + legend (combined 6 items)
    ax_center.axis("off")
    ax_center.set_title("Aggregate Recall Density", fontsize=40, pad=10)
    mdp_handles = [
        plt.Rectangle((0, 0), 1, 1, color=mdp_colors[0]),
        plt.Rectangle((0, 0), 1, 1, color=mdp_colors[1]),
        plt.Rectangle((0, 0), 1, 1, color=mdp_colors[2]),
    ]
    pomdp_handles = [
        plt.Rectangle((0, 0), 1, 1, color=pomdp_colors[0]),
        plt.Rectangle((0, 0), 1, 1, color=pomdp_colors[1]),
        plt.Rectangle((0, 0), 1, 1, color=pomdp_colors[2]),
    ]
    mdp_labels = [
        r"MDP $0<\tau<0.33$",
        r"MDP $0.33\leq\,\tau<0.66$",
        r"MDP $0.66\leq\,\tau<1.0$",
    ]
    pomdp_labels = [
        r"POMDP $0<\tau<0.33$",
        r"POMDP $0.33\leq\,\tau<0.66$",
        r"POMDP $0.66\leq\,\tau<1.0$",
    ]

    common_legend_kwargs = dict(frameon=True, fancybox=True, fontsize=30, handlelength=1.8, handletextpad=0.8)

    combined_handles = mdp_handles + pomdp_handles
    combined_labels = mdp_labels + pomdp_labels
    ax_center.legend(
        combined_handles,
        combined_labels,
        loc="center",
        bbox_to_anchor=(0.44, 0.5),
        ncol=1,
        borderaxespad=0.0,
        labelspacing=0.6,
        **common_legend_kwargs,
    )

    plt.subplots_adjust(left=0.07, right=0.985, bottom=0.20, top=0.92, wspace=0.12)
    out_dir = os.path.dirname(output_path)
    if out_dir and not os.path.exists(out_dir):
        os.makedirs(out_dir, exist_ok=True)
    fig.savefig(output_path, dpi=dpi)
    
    base, _ = os.path.splitext(output_path)
    if not output_path.lower().endswith(".pdf"):
        fig.savefig(base + ".pdf")
    plt.close(fig)


def main():
    parser = argparse.ArgumentParser(description="Plot aggregated saliency thirds per env and mode")
    parser.add_argument(
        "--recall_density_dir",
        type=str,
        default=None,
        help="Directory containing per-weight density CSVs",
    )
    parser.add_argument(
        "--summary_csv",
        type=str,
        default=None,
        help="Pre-aggregated bar-summary CSV generated by density_analysis_{pqn,ppo}.py",
    )
    parser.add_argument(
        "--output",
        type=str,
        default="your_output_pdf",
        help="Path to save the summary figure (PNG & PDF)",
    )
    
    parser.add_argument("--dpi", type=int, default=300)
    args = parser.parse_args()

    if args.summary_csv is None and args.recall_density_dir is None:
        raise SystemExit("Please provide either --summary_csv or --recall_density_dir")

    plot_summary(
        output_path=args.output,
        dpi=args.dpi,
        recall_density_dir=args.recall_density_dir,
        summary_csv=args.summary_csv,
    )
    print(f"Saved summary to {args.output}")

if __name__ == "__main__":
    main()