| """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) |
|
|
| |
| 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() |
|
|