Repository navigation
Expand file tree
/
Copy pathcodereview.py
More file actions
507 lines (443 loc) · 25.9 KB
/
Copy pathcodereview.py
File metadata and controls
507 lines (443 loc) · 25.9 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
from collections.abc import Sequence
from typing import NamedTuple, Self, override
import numpy as np
from bcbench_core.agent.harness import AgentResult
from bcbench_core.scoring import f1_score, f_beta_score, precision_recall
from pydantic import Field
from rich.console import Group, RenderableType
from rich.panel import Panel
from rich.table import Table
from scipy.optimize import linear_sum_assignment
from bcbench.dataset import BaseDatasetEntry, ReviewComment
from bcbench.results.base import BaseEvaluationResult, JudgeScoredEvaluationResult
from bcbench.results.summary import JudgeBasedEvaluationResultSummary
from bcbench.types import EvaluationContext
_METRIC_EXPLANATIONS = """\
<details>
<summary>📖 How to read these metrics</summary>
- **Micro** — sums matched, scorable generated (generated minus ignored), and expected across all tasks and computes one score; tasks with many comments dominate.
- **Macro** — computes P/R/F1 per task and averages the scores; every task counts equally regardless of comment volume. Macro precision averages over tasks where the agent commented plus negative tasks (no expected findings), where correct silence scores as perfect precision; silence on a positive task is still not rewarded. Macro recall averages only over positive tasks, since negative tasks have nothing to recall.
- **Matched comment** — a generated comment assigned one-to-one to an expected finding after an LLM judge confirms the same underlying issue. All same-file pairs are candidates; line distance only breaks assignment ties.
- **Ignored comment** — a generated comment that matched the entry's `ignored_comments` set (acceptable-but-not-required findings). Neutral: removed from the scored generated set, so it neither earns recall nor counts against precision. Precision's denominator is scorable generated (generated minus ignored).
- **F1** — harmonic mean of precision and recall; balances both equally. (Special case of Fβ at β=1.)
- **Fβ** — generalized F-score with a tunable precision/recall trade-off:
```
F_β = (1 + β²) · (P · R) / (β² · P + R)
```
where *P* = precision, *R* = recall. β < 1 favors precision; β > 1 favors recall.
- **Fβ (β=0.5)** — precision-leaning; use when false positives are costly (noisy reviews waste reviewer time).
- **Fβ (β=2)** — recall-leaning; use when missing issues is costly.
- **Severity MAE** — mean absolute error between generated and expected severity levels (matched comments only). Lower is better; `0` = exact match.
- **Valid review output rate** — fraction of runs whose output parsed into a structured review. Failures score 0 on every other metric.
</details>
"""
_CONSOLE_METRIC_EXPLANATIONS = (
"[bold]Micro[/bold] — volume-weighted across all comments; sums matched, scorable generated (generated minus ignored), and expected, so tasks with many comments dominate.\n"
"[bold]Macro[/bold] — per-task P/R/F1 averaged equally; every task counts the same. Macro precision rewards correct silence on negative tasks but not on positive ones; macro recall averages only over positive tasks.\n"
"[bold]Matched comment[/bold] — assigned one-to-one after an LLM judge confirms the same underlying issue; all same-file pairs are candidates, with line distance only breaking assignment ties.\n"
"[bold]Ignored comment[/bold] — matched the entry's ignored_comments set; neutral, so it is removed from the scored generated set (no recall, no precision hit). Precision's denominator is scorable generated (generated minus ignored).\n"
"[bold]F1[/bold] — harmonic mean of precision and recall (special case of Fβ at β=1).\n"
"[bold]Fβ[/bold] — F_β = (1 + β²) · (P · R) / (β² · P + R); β<1 favors precision, β>1 favors recall.\n"
"[bold]Fβ (β=0.5)[/bold] — precision-leaning; use when false positives are costly.\n"
"[bold]Fβ (β=2)[/bold] — recall-leaning; use when missing issues is costly.\n"
"[bold]Severity MAE[/bold] — mean absolute error of severity levels for matched comments; lower is better, 0 = exact match.\n"
"[bold]Valid review output rate[/bold] — fraction of runs whose output parsed into a structured review."
)
def _build_console_table(title: str, columns: list[str], row: list[str]) -> Table:
table = Table(title=title, title_justify="left", title_style="bold cyan", show_header=True, header_style="bold")
for column in columns:
table.add_column(column, justify="right")
table.add_row(*row)
return table
def _normalize_path(path: str) -> str:
return path.replace("\\", "/").lstrip("./").lstrip("/")
def _line_distance(line: int, start: int, end: int | None) -> int:
effective_end = end if end is not None else start
if start <= line <= effective_end:
return 0
if line < start:
return start - line
return line - effective_end
def candidate_comment_pairs(
expected_comments: list[ReviewComment],
generated_comments: list[ReviewComment],
) -> list[tuple[ReviewComment, ReviewComment]]:
"""All same-file pairs, preserving comment identity and input order without line pruning."""
return [(expected, generated) for expected in expected_comments for generated in generated_comments if _normalize_path(expected.file) == _normalize_path(generated.file)]
def assign_comment_matches(
expected_pairs: list[tuple[ReviewComment, ReviewComment]],
ignored_pairs: list[tuple[ReviewComment, ReviewComment]],
) -> tuple[list[tuple[ReviewComment, ReviewComment]], list[tuple[ReviewComment, ReviewComment]]]:
"""Assign eligible edges by expected count, then ignored count, then total line distance."""
pairs = expected_pairs + ignored_pairs
if not pairs:
return [], []
# Identity, not value equality: identical-looking findings are still separate comments.
gold_ids = dict.fromkeys(id(gold) for gold, _ in pairs)
generated_ids = dict.fromkeys(id(generated) for _, generated in pairs)
rows = {key: index for index, key in enumerate(gold_ids)}
columns = {key: index for index, key in enumerate(generated_ids)}
edges = {(rows[id(gold)], columns[id(generated)]): (gold, generated) for gold, generated in pairs}
expected_edges = {(rows[id(gold)], columns[id(generated)]) for gold, generated in expected_pairs}
distances = {key: _line_distance(generated.line_start, gold.line_start, gold.line_end) for key, (gold, generated) in edges.items()}
num_gold, num_generated = len(rows), len(columns)
# One ignored match outweighs ALL possible distance savings; one expected match
# outweighs ALL ignored matches plus distance savings. Ineligible edges stay forbidden.
ignored_reward = max(distances.values()) * min(num_gold, num_generated) + 1
expected_reward = (num_gold + 1) * ignored_reward
cost = np.full((num_gold, num_generated + num_gold), np.inf)
cost[:, num_generated:] = 0 # Dummy columns let every gold comment remain unmatched.
for key, distance in distances.items():
cost[key] = distance - (expected_reward if key in expected_edges else ignored_reward)
row_indices, column_indices = linear_sum_assignment(cost)
selected = [(row, column) for row, column in zip(row_indices, column_indices, strict=True) if column < num_generated]
return (
[edges[key] for key in selected if key in expected_edges],
[edges[key] for key in selected if key not in expected_edges],
)
def _severity_mean_absolute_error(matched_pairs: list[tuple[ReviewComment, ReviewComment]]) -> float:
"""Mean absolute difference between expected and generated severity levels over matched pairs.
Pairs where either side has no severity are skipped, since prod keeps unknown-severity findings
rather than defaulting them. Returns 0.0 when there are no scorable pairs.
"""
errors = [abs(expected.severity.level - generated.severity.level) for expected, generated in matched_pairs if expected.severity is not None and generated.severity is not None]
if not errors:
return 0.0
return sum(errors) / len(errors)
class _ScoreCounts(NamedTuple):
matched: int
incorrect: int
missed: int
ignored: int
precision: float
recall: float
def _score_counts(matched_count: int, generated_count: int, expected_count: int, ignored_count: int) -> _ScoreCounts:
"""Derive the code-review scoring counts and precision/recall from raw match counts.
Ignored comments are neutral: they leave the scored generated set (``generated - ignored``) so
they neither earn recall (they are not expected) nor cost precision (they are not false
positives). Extracted as a pure function so this critical metric math can be unit-tested
independently of comment matching, the LLM judge, and I/O.
"""
scored_generated_count = generated_count - ignored_count
precision, recall = precision_recall(matched_count, scored_generated_count, expected_count)
return _ScoreCounts(
matched=matched_count,
incorrect=scored_generated_count - matched_count,
missed=expected_count - matched_count,
ignored=ignored_count,
precision=precision,
recall=recall,
)
class CodeReviewResult(JudgeScoredEvaluationResult):
"""Result for the code-review category."""
generated_comments: list[ReviewComment] = Field(default_factory=list)
expected_comments: list[ReviewComment] = Field(default_factory=list)
ignored_comments: list[ReviewComment] = Field(default_factory=list)
valid_review_output: bool = False
matched_comment_count: int = Field(default=0, ge=0)
missed_comment_count: int = Field(default=0, ge=0)
incorrect_comment_count: int = Field(default=0, ge=0)
ignored_matched_comment_count: int = Field(default=0, ge=0)
precision: float = Field(default=0.0, ge=0.0, le=1.0)
recall: float = Field(default=0.0, ge=0.0, le=1.0)
f1: float = Field(default=0.0, ge=0.0, le=1.0)
f_beta_05: float = Field(default=0.0, ge=0.0, le=1.0)
f_beta_2: float = Field(default=0.0, ge=0.0, le=1.0)
severity_mae: float = 0.0
@classmethod
def create[E: BaseDatasetEntry](
cls,
context: EvaluationContext[E],
agent_result: AgentResult,
output: str,
expected_comments: list[ReviewComment],
generated_comments: list[ReviewComment],
*,
matched_pairs: list[tuple[ReviewComment, ReviewComment]],
ignored_comments: list[ReviewComment],
ignored_matched_pairs: list[tuple[ReviewComment, ReviewComment]],
) -> Self:
scores = _score_counts(
matched_count=len(matched_pairs),
generated_count=len(generated_comments),
expected_count=len(expected_comments),
ignored_count=len(ignored_matched_pairs),
)
return cls(
**cls._base_fields(context, agent_result),
output=output,
expected_comments=expected_comments,
generated_comments=generated_comments,
ignored_comments=ignored_comments,
valid_review_output=True,
matched_comment_count=scores.matched,
incorrect_comment_count=scores.incorrect,
missed_comment_count=scores.missed,
ignored_matched_comment_count=scores.ignored,
precision=scores.precision,
recall=scores.recall,
f1=f1_score(scores.precision, scores.recall),
f_beta_05=f_beta_score(scores.precision, scores.recall, beta=0.5),
f_beta_2=f_beta_score(scores.precision, scores.recall, beta=2.0),
severity_mae=_severity_mean_absolute_error(matched_pairs),
)
@classmethod
def create_invalid[E: BaseDatasetEntry](
cls,
context: EvaluationContext[E],
agent_result: AgentResult,
output: str,
expected_comments: list[ReviewComment],
) -> Self:
"""Result for output that could not be parsed into a review — scored zero."""
return cls(
**cls._base_fields(context, agent_result),
output=output,
expected_comments=expected_comments,
valid_review_output=False,
)
@property
@override
def category_metrics(self) -> dict[str, int | float | bool]:
return {
"generated_comment_count": len(self.generated_comments),
"expected_comment_count": len(self.expected_comments),
"matched_comment_count": self.matched_comment_count,
"incorrect_comment_count": self.incorrect_comment_count,
"missed_comment_count": self.missed_comment_count,
"ignored_matched_comment_count": self.ignored_matched_comment_count,
"precision": round(self.precision, 3),
"recall": round(self.recall, 3),
"f1": round(self.f1, 3),
"f_beta_05": round(self.f_beta_05, 3),
"f_beta_2": round(self.f_beta_2, 3),
"severity_mae": round(self.severity_mae, 3),
"valid_review_output": self.valid_review_output,
}
@property
@override
def display_row(self) -> dict[str, str]:
return {
"Generated": str(len(self.generated_comments)),
"Matched": str(self.matched_comment_count),
"Expected": str(len(self.expected_comments)),
"Precision": f"{self.precision:.2f}",
"Recall": f"{self.recall:.2f}",
"F1": f"{self.f1:.2f}",
}
class CodeReviewResultSummary(JudgeBasedEvaluationResultSummary):
"""
Summary for the code-review category.
Micro metrics aggregate matched/expected/generated comment counts across all results (volume-weighted).
Macro metrics average per-task scores (each task weighted equally).
"""
average_prompt_tokens: float | None = None
average_completion_tokens: float | None = None
generated_comment_count: int = Field(default=0, ge=0)
expected_comment_count: int = Field(default=0, ge=0)
matched_comment_count: int = Field(default=0, ge=0)
incorrect_comment_count: int = Field(default=0, ge=0)
missed_comment_count: int = Field(default=0, ge=0)
ignored_matched_comment_count: int = Field(default=0, ge=0)
precision: float = Field(default=0.0, ge=0.0, le=1.0)
recall: float = Field(default=0.0, ge=0.0, le=1.0)
f1: float = Field(default=0.0, ge=0.0, le=1.0)
f_beta_05: float = Field(default=0.0, ge=0.0, le=1.0)
f_beta_2: float = Field(default=0.0, ge=0.0, le=1.0)
macro_precision: float = Field(default=0.0, ge=0.0, le=1.0)
macro_recall: float = Field(default=0.0, ge=0.0, le=1.0)
macro_f1: float = Field(default=0.0, ge=0.0, le=1.0)
macro_f_beta_05: float = Field(default=0.0, ge=0.0, le=1.0)
macro_f_beta_2: float = Field(default=0.0, ge=0.0, le=1.0)
severity_mae: float = 0.0
valid_review_output_rate: float = Field(default=0.0, ge=0.0, le=1.0)
average_total_tokens: float | None = None
# Per-task F1 keyed by instance_id, retained so the leaderboard can bootstrap a confidence
# interval over tasks (meaningful even for a single run) instead of only over runs.
instance_results: dict[str, float] = Field(default_factory=dict)
def _performance_markdown(self) -> str:
def metric(value: float | None, digits: int = 1) -> str:
return f"{value:.{digits}f}" if value is not None else "n/a"
return (
"## Performance\n"
"\n"
"| Avg duration (s) | Avg prompt tokens | Avg completion tokens | Avg total tokens | Avg AI credits |\n"
"|-----------------:|------------------:|----------------------:|-----------------:|---------------:|\n"
f"| {self.average_duration:.1f} | {metric(self.average_prompt_tokens)} | {metric(self.average_completion_tokens)} | "
f"{metric(self.average_total_tokens)} | {metric(self.average_ai_credits, 4)} |\n"
"\n"
)
@override
def render_github_metrics_markdown(self) -> str:
micro_p = self.precision * 100
micro_r = self.recall * 100
micro_f1 = self.f1 * 100
micro_f05 = self.f_beta_05 * 100
micro_f2 = self.f_beta_2 * 100
macro_p = self.macro_precision * 100
macro_r = self.macro_recall * 100
macro_f1 = self.macro_f1 * 100
macro_f05 = self.macro_f_beta_05 * 100
macro_f2 = self.macro_f_beta_2 * 100
valid_rate = self.valid_review_output_rate * 100
return (
"## Comment counts\n"
"\n"
"| Generated | Expected | Matched | Incorrect | Missed | Ignored |\n"
"|----------:|---------:|--------:|----------:|-------:|--------:|\n"
f"| {self.generated_comment_count} | {self.expected_comment_count} | {self.matched_comment_count} | {self.incorrect_comment_count} | {self.missed_comment_count} | {self.ignored_matched_comment_count} |\n"
"\n"
"## Micro metrics (volume-weighted across all comments)\n"
"\n"
"| Precision | Recall | F1 | Fβ (β=0.5) | Fβ (β=2) |\n"
"|----------:|-------:|---:|-----------:|---------:|\n"
f"| {micro_p:.1f}% | {micro_r:.1f}% | {micro_f1:.1f}% | {micro_f05:.1f}% | {micro_f2:.1f}% |\n"
"\n"
"## Macro metrics (averaged per task)\n"
"\n"
"| Precision | Recall | F1 | Fβ (β=0.5) | Fβ (β=2) |\n"
"|----------:|-------:|---:|-----------:|---------:|\n"
f"| {macro_p:.1f}% | {macro_r:.1f}% | {macro_f1:.1f}% | {macro_f05:.1f}% | {macro_f2:.1f}% |\n"
"\n"
"## Quality\n"
"\n"
"| Severity MAE | Valid review output rate |\n"
"|-------------:|-------------------------:|\n"
f"| {self.severity_mae:.3f} | {valid_rate:.1f}% |\n"
"\n"
f"{self._performance_markdown()}"
f"{_METRIC_EXPLANATIONS}"
)
@override
def render_console_metrics(self) -> RenderableType:
metric_columns = ["Precision", "Recall", "F1", "Fβ (β=0.5)", "Fβ (β=2)"]
return Group(
_build_console_table(
"Comment counts",
["Generated", "Expected", "Matched", "Incorrect", "Missed", "Ignored"],
[
str(self.generated_comment_count),
str(self.expected_comment_count),
str(self.matched_comment_count),
str(self.incorrect_comment_count),
str(self.missed_comment_count),
str(self.ignored_matched_comment_count),
],
),
_build_console_table(
"Micro metrics (volume-weighted across all comments)",
metric_columns,
[
f"{self.precision * 100:.1f}%",
f"{self.recall * 100:.1f}%",
f"{self.f1 * 100:.1f}%",
f"{self.f_beta_05 * 100:.1f}%",
f"{self.f_beta_2 * 100:.1f}%",
],
),
_build_console_table(
"Macro metrics (averaged per task)",
metric_columns,
[
f"{self.macro_precision * 100:.1f}%",
f"{self.macro_recall * 100:.1f}%",
f"{self.macro_f1 * 100:.1f}%",
f"{self.macro_f_beta_05 * 100:.1f}%",
f"{self.macro_f_beta_2 * 100:.1f}%",
],
),
_build_console_table(
"Quality",
["Severity MAE", "Valid review output rate"],
[f"{self.severity_mae:.3f}", f"{self.valid_review_output_rate * 100:.1f}%"],
),
_build_console_table(
"Performance",
["Avg duration (s)", "Prompt", "Completion", "Total", "AI credits"],
[
f"{self.average_duration:.1f}",
f"{self.average_prompt_tokens:.1f}" if self.average_prompt_tokens is not None else "n/a",
f"{self.average_completion_tokens:.1f}" if self.average_completion_tokens is not None else "n/a",
f"{self.average_total_tokens:.1f}" if self.average_total_tokens is not None else "n/a",
f"{self.average_ai_credits:.4f}" if self.average_ai_credits is not None else "n/a",
],
),
Panel(
_CONSOLE_METRIC_EXPLANATIONS,
title="📖 How to read these metrics",
title_align="left",
border_style="dim",
padding=(1, 2),
),
)
@classmethod
@override
def from_results(cls, results: Sequence[BaseEvaluationResult], run_id: str) -> "CodeReviewResultSummary":
summary = super().from_results(results, run_id)
assert isinstance(summary, CodeReviewResultSummary)
code_review_results: list[CodeReviewResult] = [r for r in results if isinstance(r, CodeReviewResult)]
total_results: int = len(code_review_results)
generated_total: int = sum(len(r.generated_comments) for r in code_review_results)
expected_total: int = sum(len(r.expected_comments) for r in code_review_results)
matched_total: int = sum(r.matched_comment_count for r in code_review_results)
incorrect_total: int = sum(r.incorrect_comment_count for r in code_review_results)
missed_total: int = sum(r.missed_comment_count for r in code_review_results)
ignored_total: int = sum(r.ignored_matched_comment_count for r in code_review_results)
# Ignored comments are neutral, so they leave the scored generated set exactly as each
# per-task precision already does -- keeping micro precision consistent with macro.
scored_generated_total: int = generated_total - ignored_total
precision, recall = precision_recall(matched_total, scored_generated_total, expected_total)
f1: float = f1_score(precision, recall)
f_beta_05: float = f_beta_score(precision, recall, beta=0.5)
f_beta_2: float = f_beta_score(precision, recall, beta=2.0)
# Precision is only defined where the agent actually commented, so silent tasks are normally
# excluded from macro precision (rewarding silence would inflate precision on positive tasks).
# "Commented" means scorable comments (matched + incorrect); a positive task whose only output
# was neutralized as ignored is treated like silence, not rewarded. Exception: negative tasks
# (no expected findings) ARE scored when silent -- staying quiet earns precision=1.0, while any
# scorable comment is a false positive (precision=0).
results_for_macro_precision = [r for r in code_review_results if (r.matched_comment_count + r.incorrect_comment_count) > 0 or not r.expected_comments]
macro_precision: float = sum(r.precision for r in results_for_macro_precision) / len(results_for_macro_precision) if results_for_macro_precision else 0.0
# Recall measures the ability to find planted issues, so it only averages over positive tasks.
# Negative tasks (no expected findings) have a vacuous recall of 1.0 that would otherwise inflate
# the metric; when a run is all-negative there is nothing to recall, so recall defaults to 1.0.
positive_results = [r for r in code_review_results if r.expected_comments]
macro_recall: float = sum(r.recall for r in positive_results) / len(positive_results) if positive_results else 1.0
macro_f1: float = sum(r.f1 for r in code_review_results) / total_results
macro_f_beta_05: float = sum(r.f_beta_05 for r in code_review_results) / total_results
macro_f_beta_2: float = sum(r.f_beta_2 for r in code_review_results) / total_results
weighted_mae_numerator: float = sum(r.severity_mae * r.matched_comment_count for r in code_review_results)
weighted_mae_denominator: int = sum(r.matched_comment_count for r in code_review_results)
severity_mae: float = weighted_mae_numerator / weighted_mae_denominator if weighted_mae_denominator > 0 else 0.0
valid_output_count: int = sum(1 for r in code_review_results if r.valid_review_output)
valid_output_rate: float = valid_output_count / total_results
def average_metric(values: Sequence[int | float | None]) -> float | None:
available = [value for value in values if value is not None]
return sum(available) / len(available) if available else None
return summary.model_copy(
update={
"generated_comment_count": generated_total,
"expected_comment_count": expected_total,
"matched_comment_count": matched_total,
"incorrect_comment_count": incorrect_total,
"missed_comment_count": missed_total,
"ignored_matched_comment_count": ignored_total,
"precision": round(precision, 3),
"recall": round(recall, 3),
"f1": round(f1, 3),
"f_beta_05": round(f_beta_05, 3),
"f_beta_2": round(f_beta_2, 3),
"macro_precision": round(macro_precision, 3),
"macro_recall": round(macro_recall, 3),
"macro_f1": round(macro_f1, 3),
"macro_f_beta_05": round(macro_f_beta_05, 3),
"macro_f_beta_2": round(macro_f_beta_2, 3),
"severity_mae": round(severity_mae, 3),
"valid_review_output_rate": round(valid_output_rate, 3),
"instance_results": {r.instance_id: round(r.f1, 6) for r in code_review_results},
"average_prompt_tokens": average_metric([result.metrics.prompt_tokens if result.metrics else None for result in code_review_results]),
"average_completion_tokens": average_metric([result.metrics.completion_tokens if result.metrics else None for result in code_review_results]),
"average_total_tokens": average_metric([result.metrics.total_tokens if result.metrics else None for result in code_review_results]),
"average_ai_credits": average_metric([result.metrics.ai_credits if result.metrics else None for result in code_review_results]),
}
)