-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetrics.py
More file actions
1149 lines (919 loc) · 49 KB
/
metrics.py
File metadata and controls
1149 lines (919 loc) · 49 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import argparse
from typing import Dict, List, Any, Tuple
from collections import defaultdict
from dataclasses import dataclass, field
import pandas as pd
import os
import numpy as np
@dataclass
class ScoreResults:
"""存储各项评分结果"""
# 基础指标 - ISR在前,CSR在后
isr: float = 0.0 # 指令满足率
csr: float = 0.0 # 约束满足率
# Rule-based 指标 - ISR在前,CSR在后
rule_based_isr: float = 0.0 # 基于规则的指令满足率
rule_based_csr: float = 0.0 # 基于规则的约束满足率
# Open-ended 指标 - ISR在前,CSR在后
open_ended_isr: float = 0.0 # 开放式指令满足率
open_ended_csr: float = 0.0 # 开放式约束满足率
# Fact-Free 指标 (忽略correctness类型检查) - ISR在前,CSR在后
ff_isr: float = 0.0 # Fact-Free指令满足率
ff_csr: float = 0.0 # Fact-Free约束满足率
# Fact-Only 指标 (只关注correctness类型检查) - ISR在前,CSR在后
fo_isr: float = 0.0 # Fact-Only指令满足率
fo_csr: float = 0.0 # Fact-Only约束满足率
# 专项能力指标
constraint_dimension_scores: Dict[str, float] = field(default_factory=dict)
# 详细统计数据
stats: Dict[str, Any] = field(default_factory=dict)
# 视频级别的详细得分
video_scores: Dict[str, Dict[str, float]] = field(default_factory=dict)
class ScoreCalculator:
"""分数计算器"""
def __init__(self, data: Dict[str, Any]):
self.data = data
self.results = ScoreResults()
self.stats = defaultdict(lambda: defaultdict(int))
self.video_scores = defaultdict(lambda: {
'ISR': 0.0,
'CSR': 0.0,
'Rule-based ISR': 0.0,
'Rule-based CSR': 0.0,
'Open-ended ISR': 0.0,
'Open-ended CSR': 0.0,
'FF-ISR': 0.0,
'FF-CSR': 0.0,
'FO-ISR': 0.0,
'FO-CSR': 0.0
})
def calculate_all_scores(self) -> ScoreResults:
"""计算所有分数"""
self._calculate_isr()
self._calculate_csr()
self._calculate_rule_based_isr_csr()
self._calculate_open_ended_isr_csr()
self._calculate_ff_isr_csr()
self._calculate_fo_isr_csr()
self._calculate_constraint_dimension_scores()
self.results.stats = dict(self.stats)
self.results.video_scores = dict(self.video_scores)
return self.results
def _calculate_csr(self):
"""计算约束满足率 (CSR)"""
video_csr_scores = defaultdict(float)
# 全局统计
global_constraints_passed = 0
global_constraints_total = 0
for video_id, case in self.data.items():
for check_item in case:
check_result = check_item.get('check_result', {})
# 约束数量统计
ruled_based_constraints = len(check_result.get('ruled_based_check', []))
open_ended_constraints = len(check_result.get('open_ended_check', []))
total_constraints = ruled_based_constraints + open_ended_constraints
if total_constraints == 0:
continue
global_constraints_total += total_constraints
# 统计通过的约束数量
passed_constraints = 0
# 规则检查 - 每个rule_check都是一个约束
for rule_check in check_result.get('ruled_based_check', []):
if rule_check.get('result', False):
passed_constraints += 1
# 开放式检查 - 每个open_check元素是一个约束
for open_check in check_result.get('open_ended_check', []):
check_items = open_check.get('check_items', [])
if not check_items:
continue
# 判断该open_check约束是否满足(所有check_items都通过)
constraint_satisfied = True
for item in check_items:
if not item.get('result', False):
constraint_satisfied = False
break
if constraint_satisfied:
passed_constraints += 1
# 计算当前prompt的CSR
csr = passed_constraints / total_constraints if total_constraints > 0 else 0
# 累积到视频级别统计(用于计算视频平均值)
video_csr_scores[video_id] += csr
# 累积到全局统计
global_constraints_passed += passed_constraints
# 计算全局CSR
self.results.csr = global_constraints_passed / global_constraints_total if global_constraints_total > 0 else 0
# 保存统计信息
self.stats['csr']['global_constraints_total'] = global_constraints_total
self.stats['csr']['global_constraints_passed'] = global_constraints_passed
# 计算每个视频的平均CSR(基于该视频的所有prompt)
for video_id, total_score in video_csr_scores.items():
# 计算该视频有多少个prompt
prompt_count = len([item for item in self.data.get(video_id, []) if item.get('check_result')])
if prompt_count > 0:
self.video_scores[video_id]['CSR'] = total_score / prompt_count
def _calculate_isr(self):
"""计算指令满足率 (ISR)"""
# 存储每个prompt的约束满足情况
prompt_constraint_status = defaultdict(lambda: {
'total_constraints': 0,
'passed_constraints': 0
})
for video_id, case in self.data.items():
for check_item in case:
prompt_id = check_item.get('prompt_id', '')
prompt_key = (video_id, prompt_id)
check_result = check_item.get('check_result', {})
# 约束数量统计
ruled_based_constraints = len(check_result.get('ruled_based_check', []))
open_ended_constraints = len(check_result.get('open_ended_check', []))
total_constraints = ruled_based_constraints + open_ended_constraints
if total_constraints == 0:
continue
prompt_constraint_status[prompt_key]['total_constraints'] = total_constraints
# 统计通过的约束数量
passed_constraints = 0
# 规则检查 - 每个rule_check都是一个约束
for rule_check in check_result.get('ruled_based_check', []):
if rule_check.get('result', False):
passed_constraints += 1
# 开放式检查 - 每个open_check元素是一个约束
for open_check in check_result.get('open_ended_check', []):
check_items = open_check.get('check_items', [])
if not check_items:
continue
# 判断该open_check约束是否满足(所有check_items都通过)
constraint_satisfied = True
for item in check_items:
if not item.get('result', False):
constraint_satisfied = False
break
if constraint_satisfied:
passed_constraints += 1
prompt_constraint_status[prompt_key]['passed_constraints'] = passed_constraints
# 获取所有唯一的prompt
all_prompt_keys = list(prompt_constraint_status.keys())
total_prompts = len(all_prompt_keys)
# 计算ISR (prompt级别的完全满足率)
# ISR: 该prompt所有约束都满足
prompts_fully_satisfied = 0
for prompt_key in all_prompt_keys:
status = prompt_constraint_status[prompt_key]
if status['passed_constraints'] == status['total_constraints']:
prompts_fully_satisfied += 1
self.results.isr = prompts_fully_satisfied / total_prompts if total_prompts > 0 else 0
# 计算每个视频的ISR
video_prompt_constraint_status = defaultdict(lambda: {})
# 按video_id重新组织数据
for prompt_key, status in prompt_constraint_status.items():
video_id, prompt_id = prompt_key
video_prompt_constraint_status[video_id][prompt_id] = status
# 计算每个视频的ISR
for video_id, prompts in video_prompt_constraint_status.items():
total_prompts_in_video = len(prompts)
if total_prompts_in_video == 0:
continue
# ISR
prompts_fully_satisfied_in_video = 0
for prompt_id, status in prompts.items():
if status['passed_constraints'] == status['total_constraints']:
prompts_fully_satisfied_in_video += 1
self.video_scores[video_id]['ISR'] = prompts_fully_satisfied_in_video / total_prompts_in_video
def _calculate_rule_based_isr_csr(self):
"""计算Rule-based ISR和CSR"""
# Rule-based约束统计
rule_based_total = 0
rule_based_passed = 0
rule_based_video_constraints = defaultdict(lambda: {'total': 0, 'passed': 0})
# Rule-based ISR统计(按prompt统计)
prompt_constraint_status = defaultdict(lambda: {
'total_constraints': 0,
'passed_constraints': 0
})
for video_id, case in self.data.items():
for check_item in case:
prompt_id = check_item.get('prompt_id', '')
prompt_key = (video_id, prompt_id)
check_result = check_item.get('check_result', {})
# Rule-based约束 - 每个rule_check就是一个约束项
ruled_based_checks = check_result.get('ruled_based_check', [])
rule_based_total += len(ruled_based_checks)
rule_based_video_constraints[video_id]['total'] += len(ruled_based_checks)
prompt_constraint_status[prompt_key]['total_constraints'] += len(ruled_based_checks)
for rule_check in ruled_based_checks:
if rule_check.get('result', False):
rule_based_passed += 1
rule_based_video_constraints[video_id]['passed'] += 1
prompt_constraint_status[prompt_key]['passed_constraints'] += 1
# 计算Rule-based CSR
self.results.rule_based_csr = rule_based_passed / rule_based_total if rule_based_total > 0 else 0
# 计算Rule-based ISR
all_prompt_keys = [k for k in prompt_constraint_status.keys() if prompt_constraint_status[k]['total_constraints'] > 0]
total_prompts = len(all_prompt_keys)
prompts_fully_satisfied = sum(1 for prompt_key in all_prompt_keys
if prompt_constraint_status[prompt_key]['passed_constraints'] ==
prompt_constraint_status[prompt_key]['total_constraints'])
self.results.rule_based_isr = prompts_fully_satisfied / total_prompts if total_prompts > 0 else 0
# 保存统计信息
self.stats['rule_based_csr'] = {
'total_constraints': rule_based_total,
'passed_constraints': rule_based_passed
}
self.stats['rule_based_isr'] = {
'total_prompts': total_prompts,
'fully_satisfied_prompts': prompts_fully_satisfied
}
# 计算每个视频的Rule-based CSR
for video_id, constraints in rule_based_video_constraints.items():
if constraints['total'] > 0:
self.video_scores[video_id]['Rule-based CSR'] = constraints['passed'] / constraints['total']
# 计算每个视频的Rule-based ISR
video_prompt_constraint_status = defaultdict(lambda: {})
for prompt_key, status in prompt_constraint_status.items():
video_id, prompt_id = prompt_key
if status['total_constraints'] > 0: # 只考虑有rule-based约束的prompt
video_prompt_constraint_status[video_id][prompt_id] = status
for video_id, prompts in video_prompt_constraint_status.items():
total_prompts_in_video = len(prompts)
if total_prompts_in_video == 0:
continue
prompts_fully_satisfied_in_video = sum(1 for prompt_id, status in prompts.items()
if status['passed_constraints'] == status['total_constraints'])
self.video_scores[video_id]['Rule-based ISR'] = prompts_fully_satisfied_in_video / total_prompts_in_video
def _calculate_open_ended_isr_csr(self):
"""计算Open-ended ISR和CSR"""
# Open-ended约束统计
open_ended_total = 0
open_ended_passed = 0
open_ended_video_constraints = defaultdict(lambda: {'total': 0, 'passed': 0})
# Open-ended ISR统计(按prompt统计)
prompt_constraint_status = defaultdict(lambda: {
'total_constraints': 0,
'passed_constraints': 0
})
for video_id, case in self.data.items():
for check_item in case:
prompt_id = check_item.get('prompt_id', '')
prompt_key = (video_id, prompt_id)
check_result = check_item.get('check_result', {})
# Open-ended约束 - 每个open_check元素是一个约束
open_ended_checks = check_result.get('open_ended_check', [])
open_ended_total += len(open_ended_checks)
open_ended_video_constraints[video_id]['total'] += len(open_ended_checks)
prompt_constraint_status[prompt_key]['total_constraints'] += len(open_ended_checks)
for open_check in open_ended_checks:
check_items = open_check.get('check_items', [])
if not check_items:
continue
# 判断该open_check约束是否满足(所有check_items都通过)
constraint_satisfied = True
for item in check_items:
if not item.get('result', False):
constraint_satisfied = False
break
if constraint_satisfied:
open_ended_passed += 1
open_ended_video_constraints[video_id]['passed'] += 1
prompt_constraint_status[prompt_key]['passed_constraints'] += 1
# 计算Open-ended CSR
self.results.open_ended_csr = open_ended_passed / open_ended_total if open_ended_total > 0 else 0
# 计算Open-ended ISR
all_prompt_keys = [k for k in prompt_constraint_status.keys() if prompt_constraint_status[k]['total_constraints'] > 0]
total_prompts = len(all_prompt_keys)
prompts_fully_satisfied = sum(1 for prompt_key in all_prompt_keys
if prompt_constraint_status[prompt_key]['passed_constraints'] ==
prompt_constraint_status[prompt_key]['total_constraints'])
self.results.open_ended_isr = prompts_fully_satisfied / total_prompts if total_prompts > 0 else 0
# 保存统计信息
self.stats['open_ended_csr'] = {
'total_constraints': open_ended_total,
'passed_constraints': open_ended_passed
}
self.stats['open_ended_isr'] = {
'total_prompts': total_prompts,
'fully_satisfied_prompts': prompts_fully_satisfied
}
# 计算每个视频的Open-ended CSR
for video_id, constraints in open_ended_video_constraints.items():
if constraints['total'] > 0:
self.video_scores[video_id]['Open-ended CSR'] = constraints['passed'] / constraints['total']
# 计算每个视频的Open-ended ISR
video_prompt_constraint_status = defaultdict(lambda: {})
for prompt_key, status in prompt_constraint_status.items():
video_id, prompt_id = prompt_key
if status['total_constraints'] > 0: # 只考虑有open-ended约束的prompt
video_prompt_constraint_status[video_id][prompt_id] = status
for video_id, prompts in video_prompt_constraint_status.items():
total_prompts_in_video = len(prompts)
if total_prompts_in_video == 0:
continue
prompts_fully_satisfied_in_video = sum(1 for prompt_id, status in prompts.items()
if status['passed_constraints'] == status['total_constraints'])
self.video_scores[video_id]['Open-ended ISR'] = prompts_fully_satisfied_in_video / total_prompts_in_video
def _calculate_ff_isr_csr(self):
"""计算Fact-Free ISR和CSR(忽略correctness类型的检查)"""
ff_total = 0
ff_passed = 0
ff_video_constraints = defaultdict(lambda: {'total': 0, 'passed': 0})
# FF ISR统计(按prompt统计)
prompt_constraint_status = defaultdict(lambda: {
'total_constraints': 0,
'passed_constraints': 0
})
for video_id, case in self.data.items():
for check_item in case:
prompt_id = check_item.get('prompt_id', '')
prompt_key = (video_id, prompt_id)
check_result = check_item.get('check_result', {})
# Rule-based约束 - 每个rule_check就是一个约束项
ruled_based_checks = check_result.get('ruled_based_check', [])
ff_total += len(ruled_based_checks)
ff_video_constraints[video_id]['total'] += len(ruled_based_checks)
prompt_constraint_status[prompt_key]['total_constraints'] += len(ruled_based_checks)
for rule_check in ruled_based_checks:
if rule_check.get('result', False):
ff_passed += 1
ff_video_constraints[video_id]['passed'] += 1
prompt_constraint_status[prompt_key]['passed_constraints'] += 1
# 开放式检查约束 - 只考虑attempt类型的检查项
open_ended_checks = check_result.get('open_ended_check', [])
for open_check in open_ended_checks:
check_items = open_check.get('check_items', [])
if not check_items:
continue
# 过滤掉correctness类型的检查项
attempt_items = [item for item in check_items if item.get('check_type') != 'correctness']
if not attempt_items:
continue
ff_total += 1
ff_video_constraints[video_id]['total'] += 1
prompt_constraint_status[prompt_key]['total_constraints'] += 1
# 该约束项满足需要所有attempt检查项都通过
all_attempt_items_passed = all(item.get('result', False) for item in attempt_items)
if all_attempt_items_passed:
ff_passed += 1
ff_video_constraints[video_id]['passed'] += 1
prompt_constraint_status[prompt_key]['passed_constraints'] += 1
# 计算FF CSR
self.results.ff_csr = ff_passed / ff_total if ff_total > 0 else 0
# 计算FF ISR
all_prompt_keys = list(prompt_constraint_status.keys())
total_prompts = len(all_prompt_keys)
prompts_fully_satisfied = sum(1 for prompt_key in all_prompt_keys
if prompt_constraint_status[prompt_key]['passed_constraints'] ==
prompt_constraint_status[prompt_key]['total_constraints'])
self.results.ff_isr = prompts_fully_satisfied / total_prompts if total_prompts > 0 else 0
# 保存统计信息
self.stats['ff_csr'] = {
'total_constraints': ff_total,
'passed_constraints': ff_passed
}
self.stats['ff_isr'] = {
'total_prompts': total_prompts,
'fully_satisfied_prompts': prompts_fully_satisfied
}
# 计算每个视频的FF CSR
for video_id, constraints in ff_video_constraints.items():
if constraints['total'] > 0:
self.video_scores[video_id]['FF-CSR'] = constraints['passed'] / constraints['total']
# 计算每个视频的FF ISR
video_prompt_constraint_status = defaultdict(lambda: {})
for prompt_key, status in prompt_constraint_status.items():
video_id, prompt_id = prompt_key
video_prompt_constraint_status[video_id][prompt_id] = status
for video_id, prompts in video_prompt_constraint_status.items():
total_prompts_in_video = len(prompts)
if total_prompts_in_video == 0:
continue
prompts_fully_satisfied_in_video = sum(1 for prompt_id, status in prompts.items()
if status['passed_constraints'] == status['total_constraints'])
self.video_scores[video_id]['FF-ISR'] = prompts_fully_satisfied_in_video / total_prompts_in_video
def _calculate_fo_isr_csr(self):
"""计算Fact-Only ISR和CSR(只关注correctness类型的检查)"""
fo_total = 0
fo_passed = 0
fo_video_constraints = defaultdict(lambda: {'total': 0, 'passed': 0})
# FO ISR统计(按prompt统计)
prompt_constraint_status = defaultdict(lambda: {
'total_constraints': 0,
'passed_constraints': 0
})
for video_id, case in self.data.items():
for check_item in case:
prompt_id = check_item.get('prompt_id', '')
prompt_key = (video_id, prompt_id)
check_result = check_item.get('check_result', {})
# 开放式检查约束 - 只考虑correctness类型的检查项
open_ended_checks = check_result.get('open_ended_check', [])
for open_check in open_ended_checks:
check_items = open_check.get('check_items', [])
if not check_items:
continue
# 只考虑correctness类型的检查项
correctness_items = [item for item in check_items if item.get('check_type') == 'correctness']
if not correctness_items:
continue
fo_total += 1
fo_video_constraints[video_id]['total'] += 1
prompt_constraint_status[prompt_key]['total_constraints'] += 1
# 该约束项满足需要所有correctness检查项都通过
all_correctness_items_passed = all(item.get('result', False) for item in correctness_items)
if all_correctness_items_passed:
fo_passed += 1
fo_video_constraints[video_id]['passed'] += 1
prompt_constraint_status[prompt_key]['passed_constraints'] += 1
# 计算FO CSR
self.results.fo_csr = fo_passed / fo_total if fo_total > 0 else 0
# 计算FO ISR
all_prompt_keys = [k for k in prompt_constraint_status.keys() if prompt_constraint_status[k]['total_constraints'] > 0]
total_prompts = len(all_prompt_keys)
prompts_fully_satisfied = sum(1 for prompt_key in all_prompt_keys
if prompt_constraint_status[prompt_key]['passed_constraints'] ==
prompt_constraint_status[prompt_key]['total_constraints'])
self.results.fo_isr = prompts_fully_satisfied / total_prompts if total_prompts > 0 else 0
# 保存统计信息
self.stats['fo_csr'] = {
'total_constraints': fo_total,
'passed_constraints': fo_passed
}
self.stats['fo_isr'] = {
'total_prompts': total_prompts,
'fully_satisfied_prompts': prompts_fully_satisfied
}
# 计算每个视频的FO CSR
for video_id, constraints in fo_video_constraints.items():
if constraints['total'] > 0:
self.video_scores[video_id]['FO-CSR'] = constraints['passed'] / constraints['total']
# 计算每个视频的FO ISR
video_prompt_constraint_status = defaultdict(lambda: {})
for prompt_key, status in prompt_constraint_status.items():
video_id, prompt_id = prompt_key
if status['total_constraints'] > 0: # 只考虑有FO约束的prompt
video_prompt_constraint_status[video_id][prompt_id] = status
for video_id, prompts in video_prompt_constraint_status.items():
total_prompts_in_video = len(prompts)
if total_prompts_in_video == 0:
continue
prompts_fully_satisfied_in_video = sum(1 for prompt_id, status in prompts.items()
if status['passed_constraints'] == status['total_constraints'])
self.video_scores[video_id]['FO-ISR'] = prompts_fully_satisfied_in_video / total_prompts_in_video
def _calculate_constraint_dimension_scores(self):
"""计算约束维度得分"""
constraint_categories = {
'format': ['format'],
'content': ['content'],
'relation': ['logical', 'conditional']
}
category_scores = defaultdict(lambda: {'total': 0, 'passed': 0})
for video_id, case in self.data.items():
for check_item in case:
check_result = check_item.get('check_result', {})
for rule_check in check_result.get('ruled_based_check', []):
constraint_id = rule_check.get('constraint_id', '')
result = rule_check.get('result', False)
# 根据constraint_id判断类别
for category, keywords in constraint_categories.items():
if any(keyword in constraint_id.lower() for keyword in keywords):
category_scores[category]['total'] += 1
if result:
category_scores[category]['passed'] += 1
break
# 计算各维度得分
for category, scores in category_scores.items():
if scores['total'] > 0:
score = scores['passed'] / scores['total']
self.results.constraint_dimension_scores[f'{category}_score'] = score
self.stats['constraint_dimensions'][category] = scores
def calculate_prompt_scores(check_item: Dict[str, Any]) -> Dict[str, float]:
"""计算单个prompt的各项指标"""
check_result = check_item.get('check_result', {})
# 约束数量统计
ruled_based_constraints = len(check_result.get('ruled_based_check', []))
open_ended_constraints = len(check_result.get('open_ended_check', []))
total_constraints = ruled_based_constraints + open_ended_constraints
if total_constraints == 0:
return {
'isr': 0.0,
'csr': 0.0,
'rule_based_isr': 0.0,
'rule_based_csr': 0.0,
'open_ended_isr': 0.0,
'open_ended_csr': 0.0,
'ff_isr': 0.0,
'ff_csr': 0.0,
'fo_isr': 0.0,
'fo_csr': 0.0
}
# 统计通过的约束数量
passed_constraints = 0
# 规则检查统计
ruled_based_passed = 0
for rule_check in check_result.get('ruled_based_check', []):
if rule_check.get('result', False):
passed_constraints += 1
ruled_based_passed += 1
# 开放式检查统计
open_ended_passed = 0
# FF (Fact-Free) 统计
ff_total_constraints = ruled_based_constraints # Rule-based约束
ff_passed_constraints = ruled_based_passed # Rule-based通过数
# FO (Fact-Only) 统计
fo_total_constraints = 0
fo_passed_constraints = 0
for open_check in check_result.get('open_ended_check', []):
check_items = open_check.get('check_items', [])
if not check_items:
continue
# 判断该open_check约束是否满足(所有check_items都通过)
constraint_satisfied = True
for item in check_items:
if not item.get('result', False):
constraint_satisfied = False
break
if constraint_satisfied:
passed_constraints += 1
open_ended_passed += 1
# 统计attempt和correctness检查项
attempt_items = []
correctness_items = []
for item in check_items:
check_type = item.get('check_type')
if check_type == 'attempt':
attempt_items.append(item)
elif check_type == 'correctness':
correctness_items.append(item)
# FF统计:只考虑attempt类型的检查项
if attempt_items:
ff_total_constraints += 1
all_attempt_items_passed = all(item.get('result', False) for item in attempt_items)
if all_attempt_items_passed:
ff_passed_constraints += 1
# FO统计:只考虑correctness类型的检查项
if correctness_items:
fo_total_constraints += 1
all_correctness_items_passed = all(item.get('result', False) for item in correctness_items)
if all_correctness_items_passed:
fo_passed_constraints += 1
# 计算各项指标
csr = passed_constraints / total_constraints if total_constraints > 0 else 0
# ISR: 该prompt所有约束都满足
isr = 1.0 if passed_constraints == total_constraints else 0.0
# Rule-based指标
rule_based_csr = ruled_based_passed / ruled_based_constraints if ruled_based_constraints > 0 else 0
rule_based_isr = 1.0 if ruled_based_passed == ruled_based_constraints else 0.0
# Open-ended指标
open_ended_csr = open_ended_passed / open_ended_constraints if open_ended_constraints > 0 else 0
open_ended_isr = 1.0 if open_ended_passed == open_ended_constraints else 0.0
# FF指标
ff_csr = ff_passed_constraints / ff_total_constraints if ff_total_constraints > 0 else 0
ff_isr = 1.0 if ff_passed_constraints == ff_total_constraints else 0.0
# FO指标
fo_csr = fo_passed_constraints / fo_total_constraints if fo_total_constraints > 0 else 0
fo_isr = 1.0 if fo_passed_constraints == fo_total_constraints else 0.0
return {
'isr': isr,
'csr': csr,
'rule_based_isr': rule_based_isr,
'rule_based_csr': rule_based_csr,
'open_ended_isr': open_ended_isr,
'open_ended_csr': open_ended_csr,
'ff_isr': ff_isr,
'ff_csr': ff_csr,
'fo_isr': fo_isr,
'fo_csr': fo_csr
}
def process_multiple_models(model_names: List[str], input_folder: str, output_folder: str):
"""处理多个模型并生成Excel文件"""
# 确保输出文件夹存在
os.makedirs(output_folder, exist_ok=True)
# 存储所有模型的结果
all_model_results = []
# 存储prompt级别的结果(按prompt为粒度)
prompt_scores_by_model = defaultdict(list)
for model_name in model_names:
print(f"Processing model: {model_name}")
if model_name == 'baseline':
input_file = os.path.join(input_folder, "check_result.json")
else:
input_file = os.path.join(input_folder, f"{model_name}_check_result.json")
# 检查文件是否存在
if not os.path.exists(input_file):
print(f"Warning: File not found - {input_file}")
continue
# 读取数据
with open(input_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# 计算分数
calculator = ScoreCalculator(data)
results = calculator.calculate_all_scores()
# 转换为百分比并保留两位小数 - ISR在前,CSR在后
results.isr = round(results.isr * 100, 2)
results.csr = round(results.csr * 100, 2)
results.rule_based_isr = round(results.rule_based_isr * 100, 2)
results.rule_based_csr = round(results.rule_based_csr * 100, 2)
results.open_ended_isr = round(results.open_ended_isr * 100, 2)
results.open_ended_csr = round(results.open_ended_csr * 100, 2)
results.ff_isr = round(results.ff_isr * 100, 2)
results.ff_csr = round(results.ff_csr * 100, 2)
results.fo_isr = round(results.fo_isr * 100, 2)
results.fo_csr = round(results.fo_csr * 100, 2)
for k in results.constraint_dimension_scores:
results.constraint_dimension_scores[k] = round(results.constraint_dimension_scores[k] * 100, 2)
# 视频级别的分数也转换为百分比
for video_id, scores in results.video_scores.items():
scores['ISR'] = round(scores['ISR'] * 100, 2)
scores['CSR'] = round(scores['CSR'] * 100, 2)
scores['Rule-based ISR'] = round(scores['Rule-based ISR'] * 100, 2)
scores['Rule-based CSR'] = round(scores['Rule-based CSR'] * 100, 2)
scores['Open-ended ISR'] = round(scores['Open-ended ISR'] * 100, 2)
scores['Open-ended CSR'] = round(scores['Open-ended CSR'] * 100, 2)
scores['FF-ISR'] = round(scores['FF-ISR'] * 100, 2)
scores['FF-CSR'] = round(scores['FF-CSR'] * 100, 2)
scores['FO-ISR'] = round(scores['FO-ISR'] * 100, 2)
scores['FO-CSR'] = round(scores['FO-CSR'] * 100, 2)
# 收集模型级别的结果 - ISR在前,CSR在后
model_result = {
'Model': model_name,
'ISR': results.isr,
'CSR': results.csr,
'Rule-based ISR': results.rule_based_isr,
'Rule-based CSR': results.rule_based_csr,
'Open-ended ISR': results.open_ended_isr,
'Open-ended CSR': results.open_ended_csr,
'FF-ISR': results.ff_isr,
'FF-CSR': results.ff_csr,
'FO-ISR': results.fo_isr,
'FO-CSR': results.fo_csr
}
# 添加约束维度得分
model_result.update(results.constraint_dimension_scores)
all_model_results.append(model_result)
# 收集prompt级别的结果
prompt_scores = []
for video_id, case in data.items():
for check_item in case:
prompt_id = check_item.get('prompt_id', '')
if prompt_id:
# 计算单个prompt的各项指标
prompt_score = calculate_prompt_scores(check_item)
prompt_score_entry = {
'Model': model_name,
'video_id': video_id,
'prompt_id': prompt_id,
'ISR': round(prompt_score['isr'] * 100, 2),
'CSR': round(prompt_score['csr'] * 100, 2),
'Rule-based ISR': round(prompt_score['rule_based_isr'] * 100, 2),
'Rule-based CSR': round(prompt_score['rule_based_csr'] * 100, 2),
'Open-ended ISR': round(prompt_score['open_ended_isr'] * 100, 2),
'Open-ended CSR': round(prompt_score['open_ended_csr'] * 100, 2),
'FF-ISR': round(prompt_score['ff_isr'] * 100, 2),
'FF-CSR': round(prompt_score['ff_csr'] * 100, 2),
'FO-ISR': round(prompt_score['fo_isr'] * 100, 2),
'FO-CSR': round(prompt_score['fo_csr'] * 100, 2)
}
prompt_scores.append(prompt_score_entry)
prompt_scores_by_model[model_name].extend(prompt_scores)
# 生成汇总的Excel文件
if all_model_results:
# 模型级别的指标汇总
df_models = pd.DataFrame(all_model_results)
# 按ISR降序排序
df_models = df_models.sort_values('ISR', ascending=False)
# 合并所有模型的prompt级别数据
all_prompt_scores = []
for model_name, scores in prompt_scores_by_model.items():
all_prompt_scores.extend(scores)
# 保存为Excel - 分成两个sheet
metrics_excel_path = os.path.join(output_folder, "metrics.xlsx")
with pd.ExcelWriter(metrics_excel_path, engine='openpyxl') as writer:
# Sheet 1: 主要指标
main_columns = ['Model', 'ISR', 'CSR', 'Rule-based ISR', 'Rule-based CSR',
'Open-ended ISR', 'Open-ended CSR']
available_main = [col for col in main_columns if col in df_models.columns]
df_main = df_models[available_main]
df_main.to_excel(writer, sheet_name='Main Metrics', index=False)
# Sheet 2: Fact-Free和Fact-Only指标
detailed_columns = ['Model', 'FF-ISR', 'FF-CSR', 'FO-ISR', 'FO-CSR']
detailed_columns.extend([col for col in df_models.columns
if col not in main_columns and col not in detailed_columns])
available_detailed = [col for col in detailed_columns if col in df_models.columns]
df_detailed = df_models[available_detailed]
df_detailed.to_excel(writer, sheet_name='Detailed Metrics', index=False)
# Sheet 3: 所有指标(原来的完整表格)
df_models.to_excel(writer, sheet_name='All Metrics', index=False)
# Sheet 4: prompt级别的详细表格
if all_prompt_scores:
df_prompt_scores = pd.DataFrame(all_prompt_scores)
df_prompt_scores = df_prompt_scores.sort_values(['Model', 'video_id', 'prompt_id'])
df_prompt_scores.to_excel(writer, sheet_name='Prompt Detailed Scores', index=False)
# 自动调整列宽
for sheet_name in writer.sheets:
worksheet = writer.sheets[sheet_name]
for column in worksheet.columns:
max_length = 0
column_letter = column[0].column_letter
for cell in column:
try:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except:
pass
adjusted_width = min(max_length + 2, 50)
worksheet.column_dimensions[column_letter].width = adjusted_width
print(f"Model metrics Excel saved to: {metrics_excel_path}")
# 生成LaTeX表格并保存到txt文件
latex_table_path = os.path.join(output_folder, "metrics_latex_table.txt")
generate_latex_table(df_models, latex_table_path)
print(f"LaTeX tables saved to:")
print(f" - {latex_table_path}")
print(f" - {latex_table_path.replace('.txt', '_main.txt')}")
print(f" - {latex_table_path.replace('.txt', '_detailed.txt')}")
else:
df_models = None
# 合并所有模型的prompt级别数据用于返回
all_prompt_scores = []
for model_name, scores in prompt_scores_by_model.items():
all_prompt_scores.extend(scores)
df_prompt_detailed = None
if all_prompt_scores:
df_prompt_detailed = pd.DataFrame(all_prompt_scores)
df_prompt_detailed = df_prompt_detailed.sort_values(['Model', 'video_id', 'prompt_id'])
return df_models, df_prompt_detailed
def generate_latex_table(df_models, output_file):
"""生成LaTeX表格格式并保存到txt文件(只包含&和\\)"""
if df_models is None or df_models.empty:
return
latex_lines_main = [] # 主要指标表格
latex_lines_detailed = [] # 详细指标表格
# 表格1:主要指标 - ISR, CSR, Rule-based ISR, Rule-based CSR, Open-ended ISR, Open-ended CSR
main_columns = [
'Model', 'ISR', 'CSR',
'Rule-based ISR', 'Rule-based CSR',
'Open-ended ISR', 'Open-ended CSR'
]
# 表格2:Fact-Free和Fact-Only指标
detailed_columns = [
'Model',
'FF-ISR', 'FF-CSR',
'FO-ISR', 'FO-CSR'
]
# 生成主要指标表格
available_main_columns = [col for col in main_columns if col in df_models.columns]
df_main = df_models[available_main_columns]
for _, row in df_main.iterrows():
row_values = []
for col in available_main_columns:
value = row[col]
if col == 'Model':
row_values.append(str(value))
else:
row_values.append(f"{value:.2f}\\%")
latex_line = " & ".join(row_values) + " \\\\"
latex_lines_main.append(latex_line)
# 生成详细指标表格
available_detailed_columns = [col for col in detailed_columns if col in df_models.columns]
df_detailed = df_models[available_detailed_columns]
for _, row in df_detailed.iterrows():
row_values = []
for col in available_detailed_columns:
value = row[col]
if col == 'Model':
row_values.append(str(value))
else:
row_values.append(f"{value:.2f}\\%")
latex_line = " & ".join(row_values) + " \\\\"
latex_lines_detailed.append(latex_line)
# 保存到文件,两个表格用分隔符分开
with open(output_file, 'w', encoding='utf-8') as f:
f.write("% Table 1: Main Metrics\n")
f.write('\n'.join(latex_lines_main))
f.write("\n\n% Table 2: Fact-Free and Fact-Only Metrics\n")
f.write('\n'.join(latex_lines_detailed))
# 同时保存为两个单独的文件
main_table_file = output_file.replace('.txt', '_main.txt')
detailed_table_file = output_file.replace('.txt', '_detailed.txt')
with open(main_table_file, 'w', encoding='utf-8') as f:
f.write('\n'.join(latex_lines_main))
with open(detailed_table_file, 'w', encoding='utf-8') as f:
f.write('\n'.join(latex_lines_detailed))
def generate_report(results: ScoreResults, output_file: str = None):
"""生成评分报告"""
report = []
report.append("=" * 50)
report.append("分数统计报告")
report.append("=" * 50)
# 宏观表现指标 - ISR在前,CSR在后
report.append("\n## 宏观表现指标")