-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheval.py
More file actions
146 lines (124 loc) · 4.56 KB
/
Copy patheval.py
File metadata and controls
146 lines (124 loc) · 4.56 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
import os
import json
import pandas as pd
import numpy as np
from pathlib import Path
from collections import Counter
import matplotlib.pyplot as plt
from argparse import ArgumentParser
def safe_open_json(file_path):
try:
with open(file_path, "r") as f:
return json.load(f)
except json.JSONDecodeError:
print(f"Removing corrupted log file: {file_path}")
os.remove(file_path)
return None
def load_metrics_df(logs_dir, max_agents=None):
json_files = sorted(logs_dir.glob("*.json"), key=lambda f: f.stat().st_ctime)
data = []
for json_file in json_files:
log_data = safe_open_json(json_file)
if log_data is None:
continue # Skip corrupted files
# Extract key information from each log entry
record = {
"question_id": str(json_file).split("/")[-1].replace(".json", ""),
"question": log_data.get("question", ""),
"answer_indices": log_data.get("answer_indices", []),
"trajectories": log_data.get("trajectories", []),
"time_taken": log_data.get("time_taken", None),
}
if max_agents is None:
max_agents = len(record["trajectories"])
if len(record["trajectories"]) < max_agents:
print(
f"Warning: Expected {max_agents} agents but found {len(record['trajectories'])} in {json_file}"
)
max_agents = len(record["trajectories"])
agents_answer_indices = [
traj.get("agent_answer_indices", []) for traj in record["trajectories"]
][:max_agents]
flat = [idx for sublist in agents_answer_indices for idx in sublist]
counts = Counter(flat)
first_seen = {}
for i, idx in enumerate(flat):
first_seen.setdefault(idx, i)
record["combined_answer_indices"] = sorted(
counts.keys(), key=lambda x: (-counts[x], first_seen[x])
)
data.append(record)
df = pd.DataFrame(data).reset_index(drop=True)
df["recall@all"] = df.apply(
lambda row: len(
set(row["answer_indices"]).intersection(set(row["combined_answer_indices"]))
)
/ len(set(row["answer_indices"])),
axis=1,
)
df["hit@1"] = df.apply(
lambda row: (
row["combined_answer_indices"][0] in row["answer_indices"]
if row["combined_answer_indices"]
else False
),
axis=1,
)
df["hit@5"] = df.apply(
lambda row: len(
set(row["answer_indices"]).intersection(set(row["combined_answer_indices"][:5]))
)
> 0,
axis=1,
)
df["hit@10"] = df.apply(
lambda row: len(
set(row["answer_indices"]).intersection(set(row["combined_answer_indices"][:10]))
)
> 0,
axis=1,
)
df["recall@10"] = df.apply(
lambda row: len(
set(row["answer_indices"]).intersection(set(row["combined_answer_indices"][:10]))
)
/ len(set(row["answer_indices"])),
axis=1,
)
df["recall@20"] = df.apply(
lambda row: len(
set(row["answer_indices"]).intersection(set(row["combined_answer_indices"][:20]))
)
/ len(set(row["answer_indices"])),
axis=1,
)
def reciprocal_rank(row):
for rank, idx in enumerate(row["combined_answer_indices"], 1):
if idx in row["answer_indices"]:
return 1 / rank
return 0
df["MRR"] = df.apply(reciprocal_rank, axis=1)
return df
parser = ArgumentParser()
parser.add_argument("--graph_name", type=str, default="mag")
parser.add_argument("--model_name", type=str, default="azure/gpt-4.1")
parser.add_argument("--split", type=str, default="test")
args = parser.parse_args()
performance_df = pd.DataFrame()
logs_dir = Path(f"data/experiments/{args.graph_name}/graph_explorer_{args.model_name.split('/')[-1]}/{args.split}")
df = load_metrics_df(logs_dir, max_agents=3)
metrics = [
("n", len(df)),
(f"Hit@1", float(round(df[f"hit@1"].mean(), 3))),
(f"Hit@5", float(round(df[f"hit@5"].mean(), 3))),
(f"Recall@10", float(round(df[f"recall@10"].mean(), 3))),
(f"Recall@20", float(round(df[f"recall@20"].mean(), 3))),
(f"Recall@all", float(round(df[f"recall@all"].mean(), 3))),
(f"MRR", float(round(df[f"MRR"].mean(), 3))),
(f"TimeTakenMean", float(round(df[f"time_taken"].mean(), 3))),
(f"TimeTakenStd", float(round(df[f"time_taken"].std(), 3))),
]
print(f"Model: graph_explorer_{args.model_name.split('/')[-1]}")
for k, v in metrics:
print(f" {k}: {v}")
print()