File size: 7,545 Bytes
a2ffd07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Aggregate validation results across all relations into a consolidated table.

Searches for validation_results.json files in the output directory structure:
    step4_outputs/{relation}/run_*/validation_results.json

Usage:
    python -m experiment.scripts.aggregate_results
    python -m experiment.scripts.aggregate_results --output_base ./step4_outputs
    python -m experiment.scripts.aggregate_results --format csv --out results.csv
"""

import argparse
import json
import math
import os
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[2]))

from experiment.config.relation_config import get_relation_config, list_relation_keys


def find_latest_results(output_base: str, relation: str) -> dict | None:
    """Find the most recent validation_results.json for a relation."""
    rel_dir = os.path.join(output_base, relation)
    if not os.path.isdir(rel_dir):
        return None

    # Find all run dirs, sort by name (timestamp-based)
    run_dirs = sorted(
        [d for d in os.listdir(rel_dir) if d.startswith("run_")],
        reverse=True,
    )

    for run_dir in run_dirs:
        results_path = os.path.join(rel_dir, run_dir, "validation_results.json")
        if os.path.exists(results_path):
            with open(results_path) as f:
                data = json.load(f)
            data["_run_dir"] = run_dir
            data["_path"] = results_path
            return data

    # Also check directly in rel_dir (flat structure)
    results_path = os.path.join(rel_dir, "validation_results.json")
    if os.path.exists(results_path):
        with open(results_path) as f:
            data = json.load(f)
        data["_run_dir"] = "flat"
        data["_path"] = results_path
        return data

    return None


def fmt(v, pct=False):
    """Format a metric value."""
    if v is None or (isinstance(v, float) and math.isnan(v)):
        return "N/A"
    if pct:
        return f"{v:.1%}"
    return f"{v:.3f}"


def print_markdown_table(all_results: dict):
    """Print a consolidated markdown table."""
    relations = sorted(all_results.keys())

    print("\n## Efficacy & Locality (Keyword Mention Rate)")
    print("")
    print("| Relation | Efficacy↑ | Loc_pos↑ | Loc_unrel↑ | Gen_seen↑ | Gen_unseen↑ |")
    print("|----------|-----------|----------|------------|-----------|-------------|")

    for rel in relations:
        data = all_results[rel]
        kme = data.get("kme_metrics", {})
        rc = get_relation_config(rel)

        eff = kme.get("efficacy_keyword")
        gen_seen = kme.get("generality_seen")
        gen_unseen = kme.get("generality_unseen")

        # Loc_pos: mention rate on scene_with_object (should stay high)
        summary = data.get("summary", {})
        scene_with = summary.get(rc.scene_with_object, {})
        loc_pos_ft = scene_with.get("finetuned", {}).get("mention_rate_keyword")

        # Loc_unrel: exact_match on unrelated
        loc_unrel = kme.get(f"locality/unrelated/exact_match")

        print(f"| {rel:<20} | {fmt(eff, True):>9} | {fmt(loc_pos_ft, True):>8} | "
              f"{fmt(loc_unrel):>10} | {fmt(gen_seen, True):>9} | {fmt(gen_unseen, True):>11} |")

    print("")
    print("## Caption Quality (Finetuned)")
    print("")
    print("| Relation | Efficacy Cat | With-Object Cat | Unrelated |")
    print("|----------|-------------|-----------------|-----------|")

    for rel in relations:
        data = all_results[rel]
        rc = get_relation_config(rel)
        summary = data.get("summary", {})

        eff_q = summary.get(rc.scene_no_object, {}).get("finetuned", {}).get("avg_caption_quality")
        pos_q = summary.get(rc.scene_with_object, {}).get("finetuned", {}).get("avg_caption_quality")
        unr_q = summary.get("unrelated", {}).get("finetuned", {}).get("avg_caption_quality")

        print(f"| {rel:<20} | {fmt(eff_q):>11} | {fmt(pos_q):>15} | {fmt(unr_q):>9} |")


def print_csv(all_results: dict, out_path: str = None):
    """Write results as CSV."""
    import csv
    import io

    relations = sorted(all_results.keys())
    fieldnames = [
        "relation", "efficacy_keyword", "generality_seen", "generality_unseen",
        "loc_pos_mention_rate", "loc_unrel_exact_match",
        "consistency_rouge_l", "consistency_bert_score_f1",
        "caption_quality_efficacy", "caption_quality_with_object", "caption_quality_unrelated",
        "run_dir",
    ]

    rows = []
    for rel in relations:
        data = all_results[rel]
        kme = data.get("kme_metrics", {})
        rc = get_relation_config(rel)
        summary = data.get("summary", {})

        scene_with = summary.get(rc.scene_with_object, {})
        loc_pos_ft = scene_with.get("finetuned", {}).get("mention_rate_keyword")

        rows.append({
            "relation": rel,
            "efficacy_keyword": kme.get("efficacy_keyword"),
            "generality_seen": kme.get("generality_seen"),
            "generality_unseen": kme.get("generality_unseen"),
            "loc_pos_mention_rate": loc_pos_ft,
            "loc_unrel_exact_match": kme.get("locality/unrelated/exact_match"),
            "consistency_rouge_l": kme.get("consistency/rouge_l"),
            "consistency_bert_score_f1": kme.get("consistency/bert_score_f1"),
            "caption_quality_efficacy": summary.get(rc.scene_no_object, {}).get("finetuned", {}).get("avg_caption_quality"),
            "caption_quality_with_object": summary.get(rc.scene_with_object, {}).get("finetuned", {}).get("avg_caption_quality"),
            "caption_quality_unrelated": summary.get("unrelated", {}).get("finetuned", {}).get("avg_caption_quality"),
            "run_dir": data.get("_run_dir", ""),
        })

    if out_path:
        with open(out_path, "w", newline="") as f:
            writer = csv.DictWriter(f, fieldnames=fieldnames)
            writer.writeheader()
            writer.writerows(rows)
        print(f"CSV saved to {out_path}")
    else:
        buf = io.StringIO()
        writer = csv.DictWriter(buf, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)
        print(buf.getvalue())


def main():
    parser = argparse.ArgumentParser(description="Aggregate validation results across relations")
    parser.add_argument("--output_base", type=str, default="./step4_outputs",
                        help="Base directory containing per-relation result dirs")
    parser.add_argument("--format", type=str, choices=["markdown", "csv"], default="markdown")
    parser.add_argument("--out", type=str, default=None,
                        help="Output file path (for CSV format)")
    parser.add_argument("--relations", type=str, nargs="*", default=None,
                        help="Specific relations to include (default: all found)")
    args = parser.parse_args()

    relations = args.relations or list_relation_keys()

    all_results = {}
    for rel in relations:
        data = find_latest_results(args.output_base, rel)
        if data is not None:
            all_results[rel] = data
            print(f"  Found: {rel} ({data['_run_dir']})")
        else:
            print(f"  Missing: {rel}")

    if not all_results:
        print("\nNo results found. Run training + evaluation first.")
        return

    print(f"\nFound results for {len(all_results)}/{len(relations)} relations\n")

    if args.format == "csv":
        print_csv(all_results, args.out)
    else:
        print_markdown_table(all_results)


if __name__ == "__main__":
    main()