File size: 10,348 Bytes
4949db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Generate Anonymous-Hard subset from Qwen per-law eval results.

For each prompt, computes a physics micro-avg from per-law scores
(all 13 laws), averaged across all models.
Keeps prompts with cross-model avg < threshold.

Physical laws are sourced from the canonical prompt JSONs (not from eval
JSONs, which may contain stale vocabulary).

Usage:
    # Default: threshold 1.75, overwrite existing hard subset
    python -m dataprocessing.refine.gen_hard_subset

    # Custom threshold, write to a different file
    python -m dataprocessing.refine.gen_hard_subset --threshold 1.50 \
        --output data/prompts/anonymous_hard_subset_150.json

    # Dry run: print stats without writing
    python -m dataprocessing.refine.gen_hard_subset --dry-run

    # Strict mode: fail on data quality issues
    python -m dataprocessing.refine.gen_hard_subset --strict
"""

import argparse
import json
import logging
import sys
from collections import Counter, defaultdict
from pathlib import Path

from dataprocessing.common.pipeline import PipelineCheck
from dataprocessing.common.video_id import PROMPT_SOURCES, load_source_laws

logger = logging.getLogger(__name__)

ROOT = Path(__file__).resolve().parents[2]
VIDEOS_DIR = ROOT / "data/videos"
OUTPUT_PATH = ROOT / "data/prompts/anonymous_hard_subset.json"

# Dataset suffixes that appear in video dir names.
DATASET_SUFFIXES = ["video_phy_2", "physics_iq", "openvid", "wmb"]


def parse_model_dataset(dirname: str) -> tuple[str, str] | None:
    """Extract (model, dataset) from a directory name like 'ltx-2-video_phy_2'."""
    for ds in DATASET_SUFFIXES:
        if dirname.endswith(f"-{ds}"):
            model = dirname[:-(len(ds) + 1)]
            return model, ds
    return None


def find_latest_eval(dirpath: Path, evaluator: str = "qwen") -> Path | None:
    """Find the latest batched eval JSON for the given evaluator, fallback to gemini."""
    evals = sorted(dirpath.glob(f"eval_{evaluator}_2*.json"))
    if evals:
        return evals[-1]
    if evaluator != "gemini":
        gemini = sorted(dirpath.glob("eval_gemini*_2*.json"))
        if gemini:
            return gemini[-1]
    return None


def load_eval_scores(eval_path: Path) -> list[dict]:
    """Load eval JSON and extract per-video physics micro-avg.

    Computes micro-avg from per-law scores (all laws).
    Follows the same scoring approach as score_histogram / rank.md.

    Returns list of dicts with keys: video, prompt, phys_micro_avg, n_laws_scored.
    """
    with open(eval_path) as f:
        data = json.load(f)

    entries = []
    for r in data.get("results", []):
        video = r.get("video", "")
        prompt = r.get("prompt", "")
        if not video:
            continue

        # Per-law physical scores (null-aware, supports v1 and v2 formats)
        phys = r.get("physical", {})
        if not isinstance(phys, dict):
            continue
        laws = phys.get("laws", {})

        scored_vals = []
        for law_name, law_data in laws.items():
            if not isinstance(law_data, dict):
                continue
            score = law_data.get("score")
            is_scored = (law_data.get("status") == "scored"
                         or law_data.get("valid", False))
            if is_scored and score is not None:
                scored_vals.append(score)

        if not scored_vals:
            continue

        entries.append({
            "video": video,
            "prompt": prompt,
            "phys_micro_avg": sum(scored_vals) / len(scored_vals),
            "n_laws_scored": len(scored_vals),
        })
    return entries


def main(argv: list[str] | None = None):
    parser = argparse.ArgumentParser(
        description="Generate Anonymous-Hard subset from Gemini eval scores")
    parser.add_argument("--threshold", type=float, default=3.00,
                        help="Physics micro-avg threshold (default: 3.00)")
    parser.add_argument("--output", type=str, default=None,
                        help="Output path (default: data/prompts/anonymous_hard_subset.json)")
    parser.add_argument("--dry-run", action="store_true",
                        help="Print stats without writing")
    parser.add_argument("--strict", action="store_true",
                        help="Fail on data quality issues (for CI)")
    args = parser.parse_args(argv)

    logging.basicConfig(
        level=logging.INFO,
        format="[%(asctime)s] %(message)s",
        handlers=[logging.StreamHandler(sys.stdout)],
    )

    checker = PipelineCheck(strict=args.strict)

    # ---- Step 1: Collect per-model physics micro-avg for every video ----
    # {eval_video: {model: phys_micro_avg}}
    video_scores: dict[str, dict] = defaultdict(dict)
    video_prompts: dict[str, str] = {}
    video_eval_ds: dict[str, str] = {}   # eval-side dataset suffix
    eval_paths_by_ds: dict[str, Path] = {}

    for d in sorted(VIDEOS_DIR.iterdir()):
        if not d.is_dir():
            continue
        parsed = parse_model_dataset(d.name)
        if parsed is None:
            continue
        model, dataset = parsed
        if "real_world" in model:
            continue

        eval_path = find_latest_eval(d)
        if eval_path is None:
            continue

        eval_paths_by_ds[dataset] = eval_path
        entries = load_eval_scores(eval_path)
        logger.info("Loaded %d entries from %s", len(entries), eval_path.name)

        for e in entries:
            vid = e["video"]
            video_scores[vid][model] = e["phys_micro_avg"]
            video_prompts[vid] = e["prompt"]
            video_eval_ds[vid] = dataset

    logger.info("Total unique videos with scores: %d", len(video_scores))

    # ---- Step 2: Compute cross-model average ----
    video_difficulty = {}
    for vid, model_scores in video_scores.items():
        vals = list(model_scores.values())
        avg = sum(vals) / len(vals)
        video_difficulty[vid] = {
            "phys_micro_avg": round(avg, 3),
            "n_models": len(model_scores),
        }

    # ---- Step 3: Filter by threshold ----
    hard_vids = [
        vid for vid, diff in video_difficulty.items()
        if diff["phys_micro_avg"] < args.threshold
    ]
    hard_vids.sort(key=lambda v: video_difficulty[v]["phys_micro_avg"])

    logger.info("Threshold < %.2f: %d / %d videos",
                args.threshold, len(hard_vids), len(video_difficulty))

    # ---- Step 4: Look up physical_laws via canonical vid matching ----
    source = load_source_laws()

    # Check staleness: only compare each source JSON against its matching eval
    DS_TO_EVAL_SUFFIX = {
        "wmb": "wmb", "video_phy_2": "video_phy_2", "physics_iq": "physics_iq",
        "openvid": "openvid",
    }
    for ds_name, src_path in PROMPT_SOURCES:
        eval_suffix = DS_TO_EVAL_SUFFIX.get(ds_name)
        if eval_suffix and eval_suffix in eval_paths_by_ds:
            checker.check_staleness(src_path, eval_paths_by_ds[eval_suffix])

    missing = 0
    prompts_out = []
    seen_disk_vids: dict[str, int] = {}  # disk_vid -> index in prompts_out
    for eval_vid in hard_vids:
        eval_ds = video_eval_ds.get(eval_vid, "")
        matched = source.resolve_eval(eval_vid, eval_ds)

        if matched:
            cvid, entry = matched
            laws = entry["laws"]
            dataset = entry["dataset"]
            prompt = entry["prompt"] or video_prompts.get(eval_vid, "")
            legacy_ids = source.cvid_to_legacies.get(cvid, set())
            disk_vid = max(legacy_ids, key=len) if legacy_ids else eval_vid
        else:
            # Not in kept source — skip (removed, safety-blocked, etc.)
            missing += 1
            continue

        # Deduplicate: different eval_vid values can resolve to the same
        # disk_vid via legacy aliases.  Keep the entry with more models.
        if disk_vid in seen_disk_vids:
            idx = seen_disk_vids[disk_vid]
            existing = prompts_out[idx]
            if video_difficulty[eval_vid]["n_models"] > existing["difficulty"]["n_models"]:
                prompts_out[idx] = {
                    "video": disk_vid,
                    "dataset": dataset,
                    "prompt": prompt,
                    "physical_laws": laws,
                    "difficulty": video_difficulty[eval_vid],
                    "per_model_scores": dict(video_scores[eval_vid]),
                }
            continue

        checker.check_empty_laws(disk_vid, laws, dataset,
                                 resolved=matched is not None)

        seen_disk_vids[disk_vid] = len(prompts_out)
        prompts_out.append({
            "video": disk_vid,
            "dataset": dataset,
            "prompt": prompt,
            "physical_laws": laws,
            "difficulty": video_difficulty[eval_vid],
            "per_model_scores": dict(video_scores[eval_vid]),
        })

    checker.check_missing_ratio(missing, len(hard_vids))

    # ---- Step 5: Compute stats and report ----
    by_dataset = Counter(p["dataset"] for p in prompts_out)
    law_counts = Counter()
    for p in prompts_out:
        for law in p["physical_laws"]:
            law_counts[law] += 1

    output = {
        "description": (
            f"Anonymous-Hard: prompts where cross-model physics micro-avg < {args.threshold} "
            f"(Qwen, per-law scores, all 13 laws)"
        ),
        "threshold": args.threshold,
        "scoring_mode": "phys_micro_avg",
        "judge": "qwen",
        "num_prompts": len(prompts_out),
        "by_dataset": dict(by_dataset.most_common()),
        "prompts": prompts_out,
    }

    logger.info("=" * 60)
    logger.info("Hard subset: %d prompts", len(prompts_out))
    logger.info("By dataset:")
    for ds, cnt in by_dataset.most_common():
        logger.info("  %s: %d", ds, cnt)
    logger.info("Physical law counts:")
    for law, cnt in law_counts.most_common():
        logger.info("  %s: %d", law, cnt)

    score = checker.report()

    if args.dry_run:
        logger.info("(dry-run — no file written)")
        checker.finalize()
        return

    out_path = Path(args.output) if args.output else OUTPUT_PATH
    with open(out_path, "w") as f:
        json.dump(output, f, indent=2, ensure_ascii=False)
    logger.info("Saved → %s", out_path)

    checker.finalize()


if __name__ == "__main__":
    main()