Repository navigation
Expand file tree
/
Copy pathplot_heatmap.py
More file actions
89 lines (74 loc) · 3.05 KB
/
Copy pathplot_heatmap.py
File metadata and controls
89 lines (74 loc) · 3.05 KB
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
"""Heatmap plotting for needle-in-a-haystack.
Handles two schemas:
(1) original metrics/*_heatmap.json with {baseline_heatmap, tuned_heatmap} lists
-> side-by-side base/tuned heatmap (unchanged behavior).
(2) new metrics/*_xling.json with a flat "results" list carrying
lang / needle_type / tuned / Context Length / Depth (%) / F1 Score
-> a faceted grid of Depth × Context-Length heatmaps, one panel per
(lang, needle_type, tuned) cell.
"""
import json
import glob
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
def _heatmap(ax, df, title):
pivot = df.pivot_table(index="Depth (%)", columns="Context Length",
values="F1 Score", aggfunc="mean")
sns.heatmap(pivot, annot=True, cmap="RdYlGn", vmin=0, vmax=100, ax=ax, fmt=".1f",
cbar=False)
ax.set_title(title, fontsize=9)
def plot_original(path):
data = json.load(open(path))
if "results" in data: # not the original schema
return False
model_name = data["model_id"]
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
_heatmap(axes[0], pd.DataFrame(data["baseline_heatmap"]), f"Baseline: {model_name}")
_heatmap(axes[1], pd.DataFrame(data["tuned_heatmap"]), f"Fine-Tuned: {model_name}")
plt.tight_layout()
safe = model_name.split("/")[-1]
out = f"./metrics/{safe}_heatmap.png"
plt.savefig(out, dpi=120); plt.close(fig)
print(f"Saved {out}")
return True
def plot_xling(path):
data = json.load(open(path))
if "results" not in data:
return False
df = pd.DataFrame(data["results"])
model_name = data["model_id"].split("/")[-1]
# facet rows = (lang, needle_type), cols = tuned/base present in this file
langs = sorted(df["lang"].unique())
ntypes = sorted(df["needle_type"].unique())
tuned_vals = sorted(df["tuned"].unique())
rows = [(L, nt) for L in langs for nt in ntypes]
ncol = max(1, len(tuned_vals))
fig, axes = plt.subplots(len(rows), ncol,
figsize=(6 * ncol, 4.2 * len(rows)), squeeze=False)
for r, (L, nt) in enumerate(rows):
for c, tv in enumerate(tuned_vals):
sub = df[(df["lang"] == L) & (df["needle_type"] == nt) & (df["tuned"] == tv)]
ax = axes[r][c]
if sub.empty:
ax.axis("off"); continue
tag = "tuned" if tv else "base"
_heatmap(ax, sub, f"{model_name} | {L} | {nt} | {tag}")
fig.suptitle(f"Cross-lingual NIAH — {model_name} "
f"(q_lang={data.get('question_lang')})", y=1.002)
plt.tight_layout()
out = path.replace(".json", ".png")
plt.savefig(out, dpi=120, bbox_inches="tight"); plt.close(fig)
print(f"Saved {out}")
return True
def main():
files = glob.glob("./metrics/*_heatmap.json") + glob.glob("./metrics/*_xling.json")
if not files:
print("No result files found in ./metrics/")
return
for path in files:
if plot_original(path):
continue
plot_xling(path)
if __name__ == "__main__":
main()