File size: 23,601 Bytes
e933f67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Audit upstream dependency accuracy and downstream error propagation.

The IJCV revision asks for accuracy, failure modes, and downstream propagation
for optional joint-type classifiers (GPT/VLM or KPP head) and segmentation
front ends such as P3-SAM. This script intentionally works from locked eval
artifacts: it does not rerun inference or alter metric definitions.
"""

from __future__ import annotations

import argparse
import csv
import json
import math
from collections import Counter, defaultdict
from pathlib import Path
from statistics import mean, median
from typing import Dict, Iterable, List, Optional


BOOL_TRUE = {"true", "1", "yes", "y", "t"}


def parse_bool(value: object) -> bool:
    return str(value).strip().lower() in BOOL_TRUE


def parse_float(value: object) -> Optional[float]:
    if value is None:
        return None
    text = str(value).strip()
    if not text or text.upper() == "N/A":
        return None
    return float(text)


def read_csv(path: Path) -> List[Dict[str, str]]:
    with path.open(newline="") as f:
        return list(csv.DictReader(f))


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


def fmt_ratio(numer: int, denom: int) -> str:
    pct = 100.0 * numer / denom if denom else 0.0
    return f"{numer}/{denom} ({pct:.1f}%)"


def summarize(values: Iterable[Optional[float]]) -> Dict[str, object]:
    vals = [float(v) for v in values if v is not None and math.isfinite(float(v))]
    if not vals:
        return {"n": 0, "mean": "N/A", "median": "N/A", "max": "N/A"}
    vals_sorted = sorted(vals)
    return {
        "n": len(vals_sorted),
        "mean": mean(vals_sorted),
        "median": median(vals_sorted),
        "max": max(vals_sorted),
    }


def fmt_stat(value: object, digits: int = 4) -> str:
    if isinstance(value, (int, float)):
        return f"{value:.{digits}f}"
    return str(value)


def first_present(row: Dict[str, object], keys: Iterable[str]) -> Optional[str]:
    for key in keys:
        if key in row and str(row[key]).strip() != "":
            return key
    return None


def parse_segmentation_rows(segmentation_rows: List[Dict[str, str]]) -> List[Dict[str, object]]:
    parsed = []
    for row in segmentation_rows:
        iou_key = first_present(row, ["iou", "IoU", "mask_iou", "p3sam_iou"])
        precision_key = first_present(row, ["precision", "Precision", "mask_precision"])
        recall_key = first_present(row, ["recall", "Recall", "mask_recall"])
        parsed.append(
            {
                "case_id": str(row.get("case_id", "unknown")),
                "iou": parse_float(row.get(iou_key)) if iou_key else None,
                "precision": parse_float(row.get(precision_key)) if precision_key else None,
                "recall": parse_float(row.get(recall_key)) if recall_key else None,
                "selection_mode": row.get("selection_mode", ""),
                "prompt_mode": row.get("prompt_mode", ""),
                "choose_head": row.get("choose_head", ""),
            }
        )
    return parsed


def load_optional_segmentation(path: Optional[Path]) -> Optional[List[Dict[str, str]]]:
    if path is None:
        return None
    if not path.exists():
        raise FileNotFoundError(f"Segmentation metrics file not found: {path}")
    return read_csv(path)


def load_optional_jsonl(path: Optional[Path]) -> Optional[List[Dict[str, object]]]:
    if path is None:
        return None
    if not path.exists():
        raise FileNotFoundError(f"JSONL audit file not found: {path}")
    rows = []
    with path.open() as f:
        for line in f:
            text = line.strip()
            if text:
                rows.append(json.loads(text))
    return rows


def make_markdown_report(
    rows: List[Dict[str, object]],
    segmentation_rows: Optional[List[Dict[str, str]]],
    llm_audit_rows: Optional[List[Dict[str, object]]],
    output_csv: Path,
) -> str:
    n = len(rows)
    type_correct = sum(bool(row["type_correct"]) for row in rows)
    strict = sum(bool(row["success_strict"]) for row in rows)
    relaxed = sum(bool(row["success_relaxed"]) for row in rows)
    axis_only = sum(bool(row["success_axis_only"]) for row in rows)

    type_ok_rows = [row for row in rows if row["type_correct"]]
    type_bad_rows = [row for row in rows if not row["type_correct"]]
    axis_outliers = [row for row in rows if (row["axis_error_deg"] or 0.0) >= 45.0]
    origin_failures = [row for row in rows if (row["origin_error_normalized"] or 0.0) >= 0.05]

    confusion = Counter((row["gt_joint_type"], row["pred_joint_type"]) for row in rows)
    by_category = defaultdict(list)
    for row in rows:
        by_category[row["category"]].append(row)

    scope_lines = [
        "# Dependency Error Propagation Audit",
        "",
        "## Scope",
        "",
        "- Source metrics: locked GAP-25 DragMesh/KPP evaluation CSV.",
        "- This audits the automatic KPP joint-type head used in the reported KPP-only setting.",
        "- GPT/VLM joint classification is optional in `inference_pipeline.py`; no GPT response log was found in the supplied DragMesh artifacts, so GPT-4o accuracy is not inferred here.",
    ]
    if segmentation_rows is None:
        scope_lines.append(
            "- P3-SAM is optional. No segmentation metrics CSV was supplied, so this report documents mask dependency and propagation risk instead of fabricating P3-SAM accuracy."
        )
    else:
        scope_lines.append(
            "- P3-SAM/mask metrics were supplied separately and are treated as an upstream-front-end audit; the locked kinematic CSV itself was measured with provided movable masks."
        )
    scope_lines += [
        "",
        "## Overall Accuracy",
        "",
        f"- Cases: `{n}`",
        f"- Joint type accuracy: `{fmt_ratio(type_correct, n)}`",
        f"- Strict / relaxed / axis-only success: `{fmt_ratio(strict, n)}` / `{fmt_ratio(relaxed, n)}` / `{fmt_ratio(axis_only, n)}`",
        f"- Axis error deg mean / median / max: `{fmt_stat(summarize(row['axis_error_deg'] for row in rows)['mean'])}` / `{fmt_stat(summarize(row['axis_error_deg'] for row in rows)['median'])}` / `{fmt_stat(summarize(row['axis_error_deg'] for row in rows)['max'])}`",
        f"- Origin error mean / median / max: `{fmt_stat(summarize(row['origin_error_normalized'] for row in rows)['mean'])}` / `{fmt_stat(summarize(row['origin_error_normalized'] for row in rows)['median'])}` / `{fmt_stat(summarize(row['origin_error_normalized'] for row in rows)['max'])}`",
        "",
        "## Joint-Type Confusion",
        "",
        "| GT | Pred | Count |",
        "| --- | --- | --- |",
    ]
    lines = scope_lines
    for (gt, pred), count in sorted(confusion.items()):
        lines.append(f"| {gt} | {pred} | {count} |")

    lines += [
        "",
        "## Downstream Propagation",
        "",
        "| Subset | Count | Strict | Relaxed | Axis-only | Mean final CD | Median final CD | Mean endpoint L2 |",
        "| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |",
    ]

    for name, subset in [("type correct", type_ok_rows), ("type wrong", type_bad_rows)]:
        denom = len(subset)
        final_cd = summarize(row["normalized_traj_cd_final"] for row in subset)
        endpoint = summarize(row["endpoint_l2_mean"] for row in subset)
        lines.append(
            "| "
            + " | ".join(
                [
                    name,
                    str(denom),
                    fmt_ratio(sum(bool(row["success_strict"]) for row in subset), denom),
                    fmt_ratio(sum(bool(row["success_relaxed"]) for row in subset), denom),
                    fmt_ratio(sum(bool(row["success_axis_only"]) for row in subset), denom),
                    fmt_stat(final_cd["mean"]),
                    fmt_stat(final_cd["median"]),
                    fmt_stat(endpoint["mean"]),
                ]
            )
            + " |"
        )

    lines += [
        "",
        "## Category Breakdown",
        "",
        "| Category | Type Acc | Strict | Relaxed | Axis-only | Mean Axis Deg | Mean Origin |",
        "| --- | ---: | ---: | ---: | ---: | ---: | ---: |",
    ]
    for category in sorted(by_category):
        subset = by_category[category]
        denom = len(subset)
        lines.append(
            "| "
            + " | ".join(
                [
                    category,
                    fmt_ratio(sum(bool(row["type_correct"]) for row in subset), denom),
                    fmt_ratio(sum(bool(row["success_strict"]) for row in subset), denom),
                    fmt_ratio(sum(bool(row["success_relaxed"]) for row in subset), denom),
                    fmt_ratio(sum(bool(row["success_axis_only"]) for row in subset), denom),
                    fmt_stat(summarize(row["axis_error_deg"] for row in subset)["mean"]),
                    fmt_stat(summarize(row["origin_error_normalized"] for row in subset)["mean"]),
                ]
            )
            + " |"
        )

    lines += [
        "",
        "## Failure Modes",
        "",
        f"- Type errors: `{len(type_bad_rows)}/{n}`; cases: `{', '.join(row['case_id'] for row in type_bad_rows)}`.",
        f"- Axis outliers >=45 deg: `{len(axis_outliers)}/{n}`; cases: `{', '.join(row['case_id'] for row in axis_outliers)}`.",
        f"- Origin errors >=0.05 bbox diag: `{len(origin_failures)}/{n}`; cases: `{', '.join(row['case_id'] for row in origin_failures)}`.",
        "- Type mistakes switch the revolute/prismatic branch, so they directly zero out strict, relaxed, and axis-only success under the reported definitions and often raise trajectory CD.",
        "- Origin errors mostly explain the gap between axis-only success and strict success: many cases have a plausible axis but fail the tight origin threshold.",
        "",
        "## GPT/VLM Audit Log Status",
        "",
    ]

    if llm_audit_rows is None:
        lines += [
            "- No `inference_pipeline.py --audit_log` JSONL was supplied.",
            "- GPT/VLM accuracy is therefore not estimated from missing logs; the report uses the local KPP joint-type head as the measured automatic classifier.",
        ]
    else:
        source_counts = Counter(str(row.get("joint_type_source", "unknown")) for row in llm_audit_rows)
        llm_enabled = sum(bool(row.get("llm_enabled")) for row in llm_audit_rows)
        llm_used = sum(str(row.get("joint_type_source")) == "llm" for row in llm_audit_rows)
        source_summary = ", ".join(f"`{key}`={value}" for key, value in sorted(source_counts.items()))
        lines += [
            f"- Audit rows: `{len(llm_audit_rows)}`",
            f"- LLM enabled / used as final source: `{fmt_ratio(llm_enabled, len(llm_audit_rows))}` / `{fmt_ratio(llm_used, len(llm_audit_rows))}`",
            f"- Joint-type source counts: {source_summary}",
        ]

    lines += [
        "",
        "## Segmentation / P3-SAM Status",
        "",
    ]

    if segmentation_rows is None:
        lines += [
            "- No P3-SAM prediction metrics were supplied or found for this DragMesh run.",
            "- Therefore the current quantitative GAP-25 result should be described as using provided/ground-truth part labels for the movable mask.",
            "- The correct reviewer response is to state that P3-SAM is an optional annotation-free front end and to report mask-error propagation qualitatively unless a P3-SAM-vs-GT mask CSV is generated.",
        ]
    else:
        parsed_seg = parse_segmentation_rows(segmentation_rows)
        seg_by_case = {row["case_id"]: row for row in parsed_seg}
        ious = [row["iou"] for row in parsed_seg]
        precisions = [row["precision"] for row in parsed_seg]
        recalls = [row["recall"] for row in parsed_seg]
        low_iou_cases = [str(row["case_id"]) for row in parsed_seg if row["iou"] is not None and row["iou"] < 0.25]
        high_iou_cases = [str(row["case_id"]) for row in parsed_seg if row["iou"] is not None and row["iou"] >= 0.50]
        iou_summary = summarize(ious)
        precision_summary = summarize(precisions)
        recall_summary = summarize(recalls)
        protocol_bits = []
        selection_modes = sorted({str(row["selection_mode"]) for row in parsed_seg if row["selection_mode"]})
        prompt_modes = sorted({str(row["prompt_mode"]) for row in parsed_seg if row["prompt_mode"]})
        choose_heads = sorted({str(row["choose_head"]) for row in parsed_seg if row["choose_head"]})
        if selection_modes:
            protocol_bits.append("selection=" + "/".join(selection_modes))
        if prompt_modes:
            protocol_bits.append("prompt=" + "/".join(prompt_modes))
        if choose_heads:
            protocol_bits.append("head=" + "/".join(choose_heads))
        protocol_text = "; ".join(protocol_bits) if protocol_bits else "mask CSV"

        lines += [
            f"- Segmentation rows: `{len(segmentation_rows)}`",
            f"- Protocol tag: `{protocol_text}`",
            f"- P3-SAM/mask IoU mean / median / max: `{fmt_stat(iou_summary['mean'])}` / `{fmt_stat(iou_summary['median'])}` / `{fmt_stat(iou_summary['max'])}`",
            f"- Precision mean / median: `{fmt_stat(precision_summary['mean'])}` / `{fmt_stat(precision_summary['median'])}`; recall mean / median: `{fmt_stat(recall_summary['mean'])}` / `{fmt_stat(recall_summary['median'])}`",
            f"- Low-IoU cases (<0.25): `{len(low_iou_cases)}/{len(segmentation_rows)}`; cases: `{', '.join(low_iou_cases)}`.",
            f"- Higher-IoU cases (>=0.50): `{len(high_iou_cases)}/{len(segmentation_rows)}`; cases: `{', '.join(high_iou_cases)}`.",
            "- These mask metrics are not merged into the locked KPP-only kinematic score, because that score was measured with provided masks. If the P3-SAM mask were substituted, low-IoU cases should be counted as upstream segmentation failures before KPP/DQ-VAE execution.",
            "",
            "### Mask-Conditioned Risk View",
            "",
            "| Mask subset | Count | Type acc under provided-mask KPP | Strict under provided-mask KPP | Mean mask IoU | Mean final CD |",
            "| --- | ---: | ---: | ---: | ---: | ---: |",
        ]

        joined_rows = [row for row in rows if row["case_id"] in seg_by_case]
        subsets = [
            ("mask IoU < 0.25", [row for row in joined_rows if (seg_by_case[row["case_id"]]["iou"] or 0.0) < 0.25]),
            ("mask IoU >= 0.25", [row for row in joined_rows if (seg_by_case[row["case_id"]]["iou"] or 0.0) >= 0.25]),
            ("mask IoU >= 0.50", [row for row in joined_rows if (seg_by_case[row["case_id"]]["iou"] or 0.0) >= 0.50]),
        ]
        for name, subset in subsets:
            denom = len(subset)
            mask_stats = summarize(seg_by_case[row["case_id"]]["iou"] for row in subset)
            final_cd = summarize(row["normalized_traj_cd_final"] for row in subset)
            lines.append(
                "| "
                + " | ".join(
                    [
                        name,
                        str(denom),
                        fmt_ratio(sum(bool(row["type_correct"]) for row in subset), denom),
                        fmt_ratio(sum(bool(row["success_strict"]) for row in subset), denom),
                        fmt_stat(mask_stats["mean"]),
                        fmt_stat(final_cd["mean"]),
                    ]
                )
                + " |"
            )

    if segmentation_rows is None:
        p3sam_sentence = (
            "The current locked evaluation does not contain P3-SAM predictions; it uses provided part labels, "
            "so P3-SAM should be described as an optional front end whose mask errors would propagate into KPP point features and Gaussian binding rather than as an evaluated oracle."
        )
    else:
        parsed_seg = parse_segmentation_rows(segmentation_rows)
        iou_summary = summarize(row["iou"] for row in parsed_seg)
        low_iou = sum((row["iou"] or 0.0) < 0.25 for row in parsed_seg)
        p3sam_sentence = (
            "A separate P3-SAM/mask audit on the same GAP-25 cases gives mean/median IoU "
            f"{fmt_stat(iou_summary['mean'])}/{fmt_stat(iou_summary['median'])}, with {low_iou}/{len(parsed_seg)} cases below 0.25 IoU. "
            "Because the locked kinematic score uses provided masks, these P3-SAM numbers are reported as upstream-front-end accuracy rather than folded into the KPP-only trajectory score; substituting such masks would corrupt KPP point features and Gaussian binding before motion decoding."
        )

    lines += [
        "",
        "## Manuscript-Ready Paragraph",
        "",
        "For the automatic GAP-25 setting, DragMesh uses the local KPP joint-type head rather than a required GPT/VLM call. The head is correct on "
        f"{type_correct}/{n} cases. Type errors are not hidden by oracle labels: they switch the revolute/prismatic motion branch and reduce downstream success to "
        f"{strict}/{n} strict, {relaxed}/{n} relaxed, and {axis_only}/{n} axis-only successes. The failure tail is concentrated in ambiguous sliding-vs-hinge geometries and in origin localization: "
        f"{len(axis_outliers)}/{n} cases have axis error above 45 degrees, while {len(origin_failures)}/{n} exceed the 0.05 bbox-diagonal origin threshold. "
        + p3sam_sentence,
        "",
        f"Per-case CSV: `{output_csv}`",
        "",
    ]
    return "\n".join(lines)


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--trajectory_csv",
        type=Path,
        required=True,
        help="Locked DragMesh trajectory/kinematic metrics CSV.",
    )
    parser.add_argument(
        "--segmentation_csv",
        type=Path,
        default=None,
        help="Optional P3-SAM or mask-vs-GT metrics CSV with a case_id column.",
    )
    parser.add_argument(
        "--llm_audit_jsonl",
        type=Path,
        default=None,
        help="Optional JSONL produced by inference_pipeline.py --audit_log.",
    )
    parser.add_argument(
        "--output_dir",
        type=Path,
        default=Path("results/dependency_error_propagation"),
        help="Directory for audit outputs.",
    )
    args = parser.parse_args()

    raw_rows = read_csv(args.trajectory_csv)
    rows: List[Dict[str, object]] = []
    for row in raw_rows:
        rows.append(
            {
                "case_id": row["case_id"],
                "category": row["category"],
                "gt_joint_type": row["gt_joint_type"],
                "pred_joint_type": row["pred_joint_type"],
                "type_correct": parse_bool(row["joint_type_correct"]),
                "axis_error_deg": parse_float(row["axis_error_deg"]),
                "origin_error_normalized": parse_float(row["origin_error_normalized"]),
                "success_strict": parse_bool(row["success_strict"]),
                "success_relaxed": parse_bool(row["success_relaxed"]),
                "success_axis_only": parse_bool(row["success_axis_only"]),
                "normalized_traj_cd_mean": parse_float(row["normalized_traj_cd_mean"]),
                "normalized_traj_cd_final": parse_float(row["normalized_traj_cd_final"]),
                "endpoint_l2_mean": parse_float(row["endpoint_l2_mean"]),
                "endpoint_l2_median": parse_float(row["endpoint_l2_median"]),
                "failure_mode": "",
            }
        )

    for row in rows:
        modes = []
        if not row["type_correct"]:
            modes.append("joint_type_wrong")
        if (row["axis_error_deg"] or 0.0) >= 45.0:
            modes.append("axis_outlier_ge45")
        elif (row["axis_error_deg"] or 0.0) >= 10.0:
            modes.append("axis_ge10")
        if (row["origin_error_normalized"] or 0.0) >= 0.10:
            modes.append("origin_ge010")
        elif (row["origin_error_normalized"] or 0.0) >= 0.05:
            modes.append("origin_ge005")
        row["failure_mode"] = "+".join(modes) if modes else "pass_or_minor"

    segmentation_rows = load_optional_segmentation(args.segmentation_csv)
    llm_audit_rows = load_optional_jsonl(args.llm_audit_jsonl)
    if segmentation_rows is not None:
        seg_by_case = {row["case_id"]: row for row in parse_segmentation_rows(segmentation_rows)}
        for row in rows:
            seg = seg_by_case.get(row["case_id"])
            row["mask_iou"] = None if seg is None else seg["iou"]
            row["mask_precision"] = None if seg is None else seg["precision"]
            row["mask_recall"] = None if seg is None else seg["recall"]
            row["mask_failure_lt_0p25"] = False if seg is None else ((seg["iou"] or 0.0) < 0.25)

    args.output_dir.mkdir(parents=True, exist_ok=True)
    output_csv = args.output_dir / "dependency_error_propagation_cases.csv"
    fieldnames = [
        "case_id",
        "category",
        "gt_joint_type",
        "pred_joint_type",
        "type_correct",
        "axis_error_deg",
        "origin_error_normalized",
        "success_strict",
        "success_relaxed",
        "success_axis_only",
        "normalized_traj_cd_mean",
        "normalized_traj_cd_final",
        "endpoint_l2_mean",
        "endpoint_l2_median",
        "failure_mode",
    ]
    if segmentation_rows is not None:
        fieldnames += ["mask_iou", "mask_precision", "mask_recall", "mask_failure_lt_0p25"]
    write_csv(output_csv, rows, fieldnames)

    report = make_markdown_report(rows, segmentation_rows, llm_audit_rows, output_csv)
    report_path = args.output_dir / "dependency_error_propagation_report.md"
    report_path.write_text(report)

    segmentation_summary = None
    if segmentation_rows is not None:
        parsed_seg = parse_segmentation_rows(segmentation_rows)
        segmentation_summary = {
            "cases": len(parsed_seg),
            "iou": summarize(row["iou"] for row in parsed_seg),
            "precision": summarize(row["precision"] for row in parsed_seg),
            "recall": summarize(row["recall"] for row in parsed_seg),
            "low_iou_lt_0p25": sum((row["iou"] or 0.0) < 0.25 for row in parsed_seg),
            "high_iou_ge_0p50": sum((row["iou"] or 0.0) >= 0.50 for row in parsed_seg),
        }

    summary = {
        "cases": len(rows),
        "type_correct": sum(bool(row["type_correct"]) for row in rows),
        "strict_success": sum(bool(row["success_strict"]) for row in rows),
        "relaxed_success": sum(bool(row["success_relaxed"]) for row in rows),
        "axis_only_success": sum(bool(row["success_axis_only"]) for row in rows),
        "axis_error_deg": summarize(row["axis_error_deg"] for row in rows),
        "origin_error_normalized": summarize(row["origin_error_normalized"] for row in rows),
        "normalized_traj_cd_final": summarize(row["normalized_traj_cd_final"] for row in rows),
        "segmentation_metrics_supplied": segmentation_rows is not None,
        "segmentation": segmentation_summary,
        "llm_audit_rows_supplied": llm_audit_rows is not None,
    }
    (args.output_dir / "dependency_error_propagation_summary.json").write_text(
        json.dumps(summary, indent=2, sort_keys=True)
    )
    print(f"Wrote {report_path}")
    print(f"Wrote {output_csv}")


if __name__ == "__main__":
    main()