-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_csv.py
66 lines (51 loc) · 1.89 KB
/
parse_csv.py
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
import csv
import ast
from collections import defaultdict
def compute_average_eval_from_csv(csv_file):
"""
Parses a CSV file and computes the average eval/squad_exact_match for each origin.
Parameters:
csv_file (str): Path to the CSV file.
Returns:
dict: A dictionary mapping each origin to its average eval/squad_exact_match.
"""
origin_totals = defaultdict(lambda: {"sum": 0, "count": 0})
with open(csv_file, "r") as file:
reader = csv.reader(file)
# Skip the header
next(reader)
for row in reader:
if len(row) < 3 or not row[2]:
continue
# Parse the dictionary string in the third column
try:
data_dict = ast.literal_eval(row[2])
except (SyntaxError, ValueError):
print(f"Error parsing row: {row}")
continue
for key, metrics in data_dict.items():
if "eval/squad_exatct_match" in metrics:
origin = key
eval_value = metrics["eval/squad_exatct_match"]
origin_totals[origin]["sum"] += eval_value
origin_totals[origin]["count"] += 1
# Calculate averages
averages = {
origin: totals["sum"] / totals["count"]
for origin, totals in origin_totals.items()
if totals["count"] > 0
}
return averages
# Example usage
if __name__ == "__main__":
csv_file = "squad_results.csv" # Update this to your CSV file path
try:
averages = compute_average_eval_from_csv(csv_file)
# Print results
print("Average eval/squad_exact_match per origin:")
for origin, avg in averages.items():
print(f"{origin}: {avg:.2f}")
except FileNotFoundError:
print("Error: CSV file not found.")
except Exception as e:
print(f"An error occurred: {e}")