-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrieval_fiqa_generowanie_20.py
More file actions
217 lines (185 loc) · 5.69 KB
/
Copy pathretrieval_fiqa_generowanie_20.py
File metadata and controls
217 lines (185 loc) · 5.69 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
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
# === FIQA: porównanie metryk dla 20 pierwszych (bez/adaptive K) ===
fiqa_first_20 = {
"NDCG": {
"NDCG@4": 0.38466,
"NDCG@8": 0.4044,
"NDCG@16": 0.4044,
},
"MAP": {
"MAP@4": 0.32708,
"MAP@8": 0.33667,
"MAP@16": 0.33667,
},
"Recall": {
"Recall@4": 0.48333,
"Recall@8": 0.54167,
"Recall@16": 0.54167,
},
"Precision": {
"P@4": 0.15,
"P@8": 0.0875,
"P@16": 0.04375,
},
}
# z adaptive K
fiqa_first_20_adaptive_k_buffer_5 = {
"NDCG": {
"NDCG@4": 0.38466,
"NDCG@8": 0.38863,
"NDCG@16": 0.38863,
},
"MAP": {
"MAP@4": 0.32708,
"MAP@8": 0.33042,
"MAP@16": 0.33042,
},
"Recall": {
"Recall@4": 0.48333,
"Recall@8": 0.49167,
"Recall@16": 0.49167,
},
"Precision": {
"P@4": 0.15,
"P@8": 0.08125,
"P@16": 0.04063,
},
}
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Patch
import re
BASE = fiqa_first_20
ADAPT = fiqa_first_20_adaptive_k_buffer_5
def _parse_ks(metric_dict):
ks = []
for key in metric_dict.keys():
m = re.search(r"@(\d+)", key)
if m:
ks.append(int(m.group(1)))
return sorted(set(ks))
def _prefix(metric):
return "P" if metric == "Precision" else metric
def _vals(dct, metric, ks):
pref = _prefix(metric)
return [dct[metric][f"{pref}@{k}"] for k in ks]
def plot_grouped_pair(metrics_subset, base_dict, adapt_dict, title, outfile):
ks = _parse_ks(base_dict[metrics_subset[0]])
# Ustal pozycje: dla każdej metryki 3 podgrupy (k) i w każdej para (baseline/adaptive)
# x_metrics = np.arange(len(metrics_subset))
group_sep = 0.8 # >1 zwiększa przerwę między grupami, np. 1.15–1.35
x_metrics = np.arange(len(metrics_subset)) * group_sep
bar_width = 0.10 # węższe słupki
inner_gap = 0.04 # mniejszy odstęp między podgrupami @k
pair_gap = 0.02 # mniejszy odstęp w parze baseline/adaptive
# Kolory dla k (spójne w obu wykresach)
colors_k = {ks[0]: "#0EA5E9", ks[1]: "#F59E0B", ks[2]: "#10B981"}
# Hatching: baseline vs adaptive
hatch_base, hatch_adapt = "", "//"
fig, ax = plt.subplots(figsize=(10, 5))
# Rysuj słupki
for m_idx, metric in enumerate(metrics_subset):
base_vals = _vals(base_dict, metric, ks)
adapt_vals = _vals(adapt_dict, metric, ks)
for i, k in enumerate(ks):
# centrum podgrupy k w obrębie metryki
center = x_metrics[m_idx] + (i - (len(ks) - 1) / 2) * (
2 * bar_width + inner_gap
)
x_base = center - (bar_width / 2 + pair_gap / 2)
x_adapt = center + (bar_width / 2 + pair_gap / 2)
b1 = ax.bar(
x_base,
base_vals[i],
width=bar_width,
color=colors_k[k],
edgecolor="black",
linewidth=0.4,
hatch=hatch_base,
label=None,
)
b2 = ax.bar(
x_adapt,
adapt_vals[i],
width=bar_width,
color=colors_k[k],
edgecolor="black",
linewidth=0.4,
hatch=hatch_adapt,
label=None,
)
# adnotacje
for b, val in [(b1[0], base_vals[i]), (b2[0], adapt_vals[i])]:
ax.annotate(
f"{val:.3f}",
(b.get_x() + b.get_width() / 2, val),
ha="center",
va="bottom",
fontsize=8,
xytext=(0, 6),
textcoords="offset points",
)
# Oś X i opis
ax.set_title(title)
ax.set_xlabel("Metryka")
ax.set_ylabel("Wartość (0–1)")
ax.set_xticks(x_metrics)
ax.set_xticklabels(metrics_subset)
ax.set_ylim(0, 1.0)
ax.set_axisbelow(True)
ax.grid(axis="y", alpha=0.25)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
# Dwie legendy: k (kolory) oraz system (hatch)
# --- LEGENDA: obie w prawym górnym rogu ---
k_patches = [
Patch(facecolor=colors_k[k], edgecolor="black", label=f"@{k}") for k in ks
]
sys_patches = [
Patch(
facecolor="#CCCCCC",
edgecolor="black",
hatch=hatch_base,
label="Baseline (bez adaptive)",
),
Patch(
facecolor="#CCCCCC",
edgecolor="black",
hatch=hatch_adapt,
label="Adaptive K (buffer=5)",
),
]
# pierwsza legenda (k) w samym rogu
leg1 = ax.legend(
handles=k_patches,
title="Głębokość @k",
loc="upper right",
bbox_to_anchor=(1.0, 1.0),
)
ax.add_artist(leg1)
# druga legenda (system) tuż pod pierwszą
ax.legend(
handles=sys_patches,
title="System",
loc="upper right",
bbox_to_anchor=(1.0, 0.72),
)
plt.tight_layout()
plt.savefig(outfile, dpi=200, bbox_inches="tight", pad_inches=0.1)
plt.show()
print(f"Zapisano: {outfile}")
# --- WYKRES 1: NDCG + MAP ---
plot_grouped_pair(
metrics_subset=["NDCG", "MAP"],
base_dict=BASE,
adapt_dict=ADAPT,
title="FIQA (Top-20): NDCG i MAP – fixed vs adaptive K",
outfile="fiqa_ndcg_map_baseline_vs_adaptive.png",
)
# --- WYKRES 2: Recall + Precision ---
plot_grouped_pair(
metrics_subset=["Recall", "Precision"],
base_dict=BASE,
adapt_dict=ADAPT,
title="FIQA (Top-20): " "Recall i Precision – fixed vs adaptive K",
outfile="fiqa_recall_precision_baseline_vs_adaptive.png",
)