File size: 7,100 Bytes
d606d10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Render current failure-taxonomy summary tables and SVG figures.

The legacy renderer uses the 35-row preliminary evidence table. This renderer
uses the post-PR193 paper-grade taxonomy surface so paper/deck figures can cite
the same CSV as the current failure-analysis text.
"""

from __future__ import annotations

import csv
import html
from collections import Counter
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
METRICS_DIR = ROOT / "results" / "metrics"
FIGURES_DIR = ROOT / "results" / "figures"
SOURCE_CSV = METRICS_DIR / "failure_taxonomy_current.csv"

LABELS = {
    "low_task_completion": "Task completion",
    "low_data_retrieval_accuracy": "Data retrieval accuracy",
    "low_agent_sequence_correct": "Agent sequence correctness",
    "low_generalized_result_verification": "Result verification",
}

PALETTE = ["#1f6f78", "#c85a3a", "#6f7d2a", "#5b6aa8"]


def read_rows() -> list[dict[str, str]]:
    with SOURCE_CSV.open(newline="") as f:
        return list(csv.DictReader(f))


def write_csv(path: Path, fieldnames: list[str], rows: list[dict[str, object]]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames, lineterminator="\n")
        writer.writeheader()
        writer.writerows(rows)


def pct(numerator: int, denominator: int) -> str:
    return f"{(100.0 * numerator / denominator):.1f}" if denominator else "0.0"


def xml(value: object) -> str:
    return html.escape(str(value), quote=True)


def write_auto_label_counts(rows: list[dict[str, str]]) -> list[dict[str, object]]:
    paper_failed = [r for r in rows if r["paper_eligible"] == "true"]
    counts = Counter(r["auto_taxonomy_label"] for r in paper_failed)
    total = len(paper_failed)
    out = []
    for label, count in counts.most_common():
        out.append(
            {
                "auto_taxonomy_label": label,
                "display_label": LABELS.get(label, label.replace("_", " ")),
                "rows": count,
                "percent_of_paper_failures": pct(count, total),
                "source_rows": total,
                "source_csv": "results/metrics/failure_taxonomy_current.csv",
            }
        )
    write_csv(
        METRICS_DIR / "failure_taxonomy_current_auto_label_counts.csv",
        [
            "auto_taxonomy_label",
            "display_label",
            "rows",
            "percent_of_paper_failures",
            "source_rows",
            "source_csv",
        ],
        out,
    )
    return out


def write_failed_dim_counts(rows: list[dict[str, str]]) -> list[dict[str, object]]:
    paper_failed = [r for r in rows if r["paper_eligible"] == "true"]
    counts = Counter(r["failed_dim_count"] for r in paper_failed)
    total = len(paper_failed)
    out = []
    for failed_dim_count in sorted(counts, key=lambda v: int(v)):
        count = counts[failed_dim_count]
        out.append(
            {
                "failed_dim_count": failed_dim_count,
                "rows": count,
                "percent_of_paper_failures": pct(count, total),
                "source_rows": total,
                "source_csv": "results/metrics/failure_taxonomy_current.csv",
            }
        )
    write_csv(
        METRICS_DIR / "failure_taxonomy_current_failed_dim_counts.csv",
        [
            "failed_dim_count",
            "rows",
            "percent_of_paper_failures",
            "source_rows",
            "source_csv",
        ],
        out,
    )
    return out


def write_manual_audit_counts(rows: list[dict[str, str]]) -> list[dict[str, object]]:
    audited = [r for r in rows if r["audit_status"] == "manual_confirmed"]
    categories = [
        ("audit_decision", "audit_decision"),
        ("berkeley_label", "berkeley_label"),
        ("failure_stage", "failure_stage"),
    ]
    out = []
    for section, column in categories:
        counts = Counter(r[column] or "blank" for r in audited)
        for value, count in counts.most_common():
            out.append(
                {
                    "section": section,
                    "value": value,
                    "rows": count,
                    "percent_of_manual_sample": pct(count, len(audited)),
                    "source_rows": len(audited),
                    "source_csv": "results/metrics/failure_taxonomy_current.csv",
                }
            )
    write_csv(
        METRICS_DIR / "failure_taxonomy_current_manual_audit_counts.csv",
        [
            "section",
            "value",
            "rows",
            "percent_of_manual_sample",
            "source_rows",
            "source_csv",
        ],
        out,
    )
    return out


def svg_auto_label_bar_chart(rows: list[dict[str, object]], path: Path) -> None:
    width, height = 1080, 520
    left, top = 390, 118
    bar_h, gap = 56, 24
    max_count = max(int(r["rows"]) for r in rows) or 1
    parts = [
        f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}">',
        '<rect width="100%" height="100%" fill="#fbf7ef"/>',
        '<text x="40" y="46" font-family="Georgia, serif" font-size="30" fill="#1b1b1b">Current paper-grade failure taxonomy</text>',
        '<text x="40" y="76" font-family="Verdana, sans-serif" font-size="14" fill="#5b5147">1,276 paper-eligible failed judge rows from failure_taxonomy_current.csv</text>',
        '<text x="40" y="98" font-family="Verdana, sans-serif" font-size="13" fill="#5b5147">Auto label is the highest-priority failed judge dimension; manual audit sample is tracked separately.</text>',
    ]
    for i, row in enumerate(rows):
        y = top + i * (bar_h + gap)
        count = int(row["rows"])
        bar_w = int((width - left - 150) * count / max_count)
        color = PALETTE[i % len(PALETTE)]
        percent = row["percent_of_paper_failures"]
        parts.extend(
            [
                f'<text x="40" y="{y + 35}" font-family="Verdana, sans-serif" font-size="16" fill="#26231f">{xml(row["display_label"])}</text>',
                f'<rect x="{left}" y="{y}" width="{bar_w}" height="{bar_h}" rx="6" fill="{color}"/>',
                f'<text x="{left + bar_w + 14}" y="{y + 34}" font-family="Verdana, sans-serif" font-size="18" font-weight="700" fill="#26231f">{count} ({percent}%)</text>',
            ]
        )
    parts.append("</svg>")
    path.write_text("\n".join(parts) + "\n")


def main() -> None:
    rows = read_rows()
    FIGURES_DIR.mkdir(parents=True, exist_ok=True)
    auto_label_counts = write_auto_label_counts(rows)
    failed_dim_counts = write_failed_dim_counts(rows)
    manual_counts = write_manual_audit_counts(rows)
    svg_auto_label_bar_chart(
        auto_label_counts,
        FIGURES_DIR / "failure_taxonomy_current_auto_label_counts.svg",
    )
    print(
        "Rendered "
        f"{len(auto_label_counts)} auto-label rows, "
        f"{len(failed_dim_counts)} failed-dim rows, "
        f"{len(manual_counts)} manual-audit rows."
    )


if __name__ == "__main__":
    main()