-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculate_average.py
More file actions
138 lines (109 loc) · 4.07 KB
/
Copy pathcalculate_average.py
File metadata and controls
138 lines (109 loc) · 4.07 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
import json
import statistics
import sys
import os
# Configuration
FILE_PATH = '/home/xwang378/scratch/2025/AudioBench/lite.json'
TASK_FAMILIES = {
'perception': 'Perc.',
'spatial': 'Spat.',
'temporal': 'Temp.',
'speech': 'Ling.',
'external': 'Knwl.'
}
MODALITIES = {
'audio_text': 'A->T',
'audio_vision': 'A->V',
'text_audio': 'T->A',
'text_vision': 'T->V',
'vision_audio': 'V->A',
'vision_text': 'V->T'
}
# Enforce order for columns
TASK_ORDER = ['perception', 'spatial', 'temporal', 'speech', 'external']
MODALITY_ORDER = ['audio_text', 'audio_vision', 'text_audio', 'text_vision', 'vision_audio', 'vision_text']
# Load data
def load_data(filepath):
if not os.path.exists(filepath):
print(f"Error: File not found at {filepath}")
sys.exit(1)
with open(filepath, 'r') as f:
lines = f.readlines()
# Join lines and find the start of the JSON object (first '{')
content = ''.join(lines)
start_idx = content.find('{')
if start_idx == -1:
print("Error: No JSON object found in file")
sys.exit(1)
json_content = content[start_idx:]
try:
return json.loads(json_content)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
sys.exit(1)
def main():
data = load_data(FILE_PATH)
# Define column headers
task_headers = [TASK_FAMILIES[k] for k in TASK_ORDER]
modality_headers = [MODALITIES[k] for k in MODALITY_ORDER]
# Print Table Header
# Adjust widths: Model (25), Tasks (7 each), Modalities (7 each), Std (8), Avg (8)
header = f"{'Model':<25} |"
for h in task_headers:
header += f" {h:>6}"
header += " |"
for h in modality_headers:
header += f" {h:>6}"
header += " | Std. | Avg."
print("-" * len(header))
print(header)
print("-" * len(header))
# Calculate metrics for each model
model_metrics = []
for model_name, model_data in data.items():
# Aggregators
task_family_scores = {k: [] for k in TASK_ORDER}
modality_scores = {k: [] for k in MODALITY_ORDER}
all_scores = []
for task_family, tasks in model_data.items():
if task_family not in TASK_ORDER:
continue
for task_name, modalities in tasks.items():
if isinstance(modalities, dict):
for modality, score in modalities.items():
if modality in MODALITY_ORDER and isinstance(score, (int, float)):
task_family_scores[task_family].append(score)
modality_scores[modality].append(score)
all_scores.append(score)
# Calculate Averages
row_data = {'name': model_name}
# Task Families
for tf_key in TASK_ORDER:
scores = task_family_scores[tf_key]
row_data[TASK_FAMILIES[tf_key]] = statistics.mean(scores) if scores else 0.0
# Modalities
for mod_key in MODALITY_ORDER:
scores = modality_scores[mod_key]
row_data[MODALITIES[mod_key]] = statistics.mean(scores) if scores else 0.0
# Overall
if all_scores:
row_data['Avg'] = statistics.mean(all_scores)
row_data['Std'] = statistics.stdev(all_scores) if len(all_scores) > 1 else 0.0
else:
row_data['Avg'] = 0.0
row_data['Std'] = 0.0
model_metrics.append(row_data)
# Sort by Average score descending
model_metrics.sort(key=lambda x: x['Avg'], reverse=True)
# Print Rows
for m in model_metrics:
row_str = f"{m['name']:<25} |"
for h in task_headers:
row_str += f" {m[h]:6.1f}"
row_str += " |"
for h in modality_headers:
row_str += f" {m[h]:6.1f}"
row_str += f" | {m['Std']:6.4f} | {m['Avg']:6.1f}"
print(row_str)
if __name__ == '__main__':
main()