File size: 1,993 Bytes
3404d44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Reorder exp2a similarity CSVs and heatmaps to: left, right, above, under, far, close."""

import os
import glob
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

DESIRED_ORDER = ["left", "right", "above", "under", "far", "close"]
RESULTS_DIR = "/data/shared/Qwen/experiments/exp2a_results"


def reorder_and_save_csv(csv_path):
    """Read a similarity CSV, reorder rows/cols, overwrite."""
    df = pd.read_csv(csv_path, index_col=0)

    # Filter to only categories that exist in this CSV
    order = [c for c in DESIRED_ORDER if c in df.columns]
    df = df.loc[order, order]

    df.to_csv(csv_path)
    print(f"  Reordered CSV: {csv_path}")
    return df


def draw_heatmap(df, png_path, title):
    """Draw and save a heatmap from a similarity DataFrame."""
    fig, ax = plt.subplots(figsize=(8, 6))
    sns.heatmap(
        df.astype(float),
        annot=True,
        fmt=".4f",
        cmap="RdYlBu_r",
        vmin=0.5,
        vmax=1.0,
        square=True,
        linewidths=0.5,
        ax=ax,
    )
    ax.set_title(title, fontsize=14)
    plt.tight_layout()
    plt.savefig(png_path, dpi=150, bbox_inches="tight")
    plt.close()
    print(f"  Saved heatmap: {png_path}")


def main():
    model_dirs = sorted(glob.glob(os.path.join(RESULTS_DIR, "*")))

    for model_dir in model_dirs:
        if not os.path.isdir(model_dir):
            continue
        model_name = os.path.basename(model_dir)
        print(f"\n=== {model_name} ===")

        csv_files = sorted(glob.glob(os.path.join(model_dir, "similarity_*.csv")))
        for csv_path in csv_files:
            scale = os.path.basename(csv_path).replace("similarity_", "").replace(".csv", "")

            df = reorder_and_save_csv(csv_path)

            png_path = os.path.join(model_dir, f"heatmap_{scale}.png")
            title = f"{model_name} - {scale} (Cosine Similarity)"
            draw_heatmap(df, png_path, title)

    print("\nDone.")


if __name__ == "__main__":
    main()