| """ |
| 回填质检标签 (backfill) |
| ======================== |
| 把 output 根目录 a1/a2/a3 原始文件里的条目, 按对应 *_strict.json 打上质检标签: |
| - 出现在 strict 文件里的 -> human_valid_label_strict = True |
| - 未出现的 (当初被淘汰) -> human_valid_label_strict = False |
| |
| 目的: 让增量续跑时 step_a6 的 _already_checked (规则2: 已有标签就跳过) 命中, |
| 从而旧批用户跳过三教师质检、且严格保留原有 strict 结果 (通过的保通过, 淘汰的仍淘汰), |
| 新用户 (无标签) 正常质检。 |
| |
| 匹配键: A1 按 user_id, A2 按 session_id, A3 按 QA id。 |
| 幂等: 已有标签的条目不覆盖 (二次运行安全)。原子写 (.tmp 再 rename)。 |
| |
| 用法: |
| python backfill_labels.py # 回填 a1/a2/a3 |
| python backfill_labels.py --dry-run # 只统计, 不写文件 |
| """ |
| import argparse |
| import json |
| import re |
| import sys |
| from pathlib import Path |
|
|
| sys.path.append(str(Path(__file__).resolve().parent)) |
| from config import ( |
| OUTPUT_A1, OUTPUT_A1_STRICT, |
| OUTPUT_A2, OUTPUT_A2_STRICT, |
| OUTPUT_A3, OUTPUT_A3_STRICT, |
| ) |
|
|
|
|
| def _user_num(item: dict): |
| """解析 user 编号: profile/QA 用 user_id, session 用 session_id 前缀 (u0036_s001 -> 36).""" |
| uid = item.get("user_id") |
| if not uid: |
| sid = item.get("session_id", "") |
| uid = sid.split("_", 1)[0] if sid else "" |
| m = re.match(r"[uU]?(\d+)", str(uid)) |
| return int(m.group(1)) if m else None |
|
|
|
|
| def _load(path: Path): |
| if not path.exists(): |
| return None |
| with open(path, "r", encoding="utf-8") as f: |
| return json.load(f) |
|
|
|
|
| def _atomic_dump(obj, path: Path): |
| tmp = path.with_suffix(path.suffix + ".tmp") |
| with open(tmp, "w", encoding="utf-8") as f: |
| json.dump(obj, f, ensure_ascii=False, indent=2) |
| tmp.replace(path) |
|
|
|
|
| def _set_label(item: dict, passed: bool): |
| """给条目打上质检标签 (仅当尚无稳定标签时)。返回是否新打了标签。""" |
| if "human_valid_label_strict" in item and isinstance(item.get("quality_check"), dict) \ |
| and "error" not in item["quality_check"]: |
| return False |
| item["quality_check"] = { |
| "final_valid_loose": passed, |
| "final_valid_strict": passed, |
| "final_valid": passed, |
| "backfilled": True, |
| } |
| item["human_valid_label_loose"] = passed |
| item["human_valid_label_strict"] = passed |
| item["human_valid_label"] = passed |
| return True |
|
|
|
|
| def backfill_a1(dry: bool, max_id: int): |
| raw = _load(OUTPUT_A1) |
| strict = _load(OUTPUT_A1_STRICT) |
| if raw is None or strict is None: |
| print(f"[A1] 跳过: 缺 {OUTPUT_A1.name} 或 {OUTPUT_A1_STRICT.name}") |
| return |
| strict_ids = {p["user_id"] for p in strict} |
| n_pass = n_fail = n_skip = n_new = 0 |
| for p in raw: |
| num = _user_num(p) |
| if num is None or num > max_id: |
| n_new += 1 |
| continue |
| passed = p["user_id"] in strict_ids |
| if _set_label(p, passed): |
| n_pass += passed |
| n_fail += (not passed) |
| else: |
| n_skip += 1 |
| print(f"[A1] {len(raw)} profiles | 新打通过={n_pass} 新打淘汰={n_fail} 已有标签跳过={n_skip} 新用户不回填={n_new}") |
| if not dry: |
| _atomic_dump(raw, OUTPUT_A1) |
| print(f"[A1] 写回 {OUTPUT_A1.name}") |
|
|
|
|
| def backfill_a2(dry: bool, max_id: int): |
| raw = _load(OUTPUT_A2) |
| strict = _load(OUTPUT_A2_STRICT) |
| if raw is None or strict is None: |
| print(f"[A2] 跳过: 缺 {OUTPUT_A2.name} 或 {OUTPUT_A2_STRICT.name}") |
| return |
| strict_sids = set() |
| for u in strict: |
| for s in u.get("evidence_sessions", []): |
| strict_sids.add(s["session_id"]) |
| n_pass = n_fail = n_skip = n_new = 0 |
| for u in raw: |
| for s in u.get("evidence_sessions", []): |
| num = _user_num(s) |
| if num is None or num > max_id: |
| n_new += 1 |
| continue |
| passed = s["session_id"] in strict_sids |
| if _set_label(s, passed): |
| n_pass += passed |
| n_fail += (not passed) |
| else: |
| n_skip += 1 |
| print(f"[A2] evidence sessions | 新打通过={n_pass} 新打淘汰={n_fail} 已有标签跳过={n_skip} 新用户不回填={n_new}") |
| if not dry: |
| _atomic_dump(raw, OUTPUT_A2) |
| print(f"[A2] 写回 {OUTPUT_A2.name}") |
|
|
|
|
| def backfill_a3(dry: bool, max_id: int): |
| raw = _load(OUTPUT_A3) |
| strict = _load(OUTPUT_A3_STRICT) |
| if raw is None or strict is None: |
| print(f"[A3] 跳过: 缺 {OUTPUT_A3.name} 或 {OUTPUT_A3_STRICT.name}") |
| return |
| strict_ids = {q["id"] for q in strict} |
| n_pass = n_fail = n_skip = n_new = 0 |
| for q in raw: |
| num = _user_num(q) |
| if num is None or num > max_id: |
| n_new += 1 |
| continue |
| passed = q["id"] in strict_ids |
| if _set_label(q, passed): |
| n_pass += passed |
| n_fail += (not passed) |
| else: |
| n_skip += 1 |
| print(f"[A3] {len(raw)} QA | 新打通过={n_pass} 新打淘汰={n_fail} 已有标签跳过={n_skip} 新用户不回填={n_new}") |
| if not dry: |
| _atomic_dump(raw, OUTPUT_A3) |
| print(f"[A3] 写回 {OUTPUT_A3.name}") |
|
|
|
|
| def main(): |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--dry-run", action="store_true", help="只统计不写文件") |
| ap.add_argument("--max-id", type=int, default=200, |
| help="只回填 user 编号 <= 此值的旧批条目; 更大编号的新用户保持无标签待质检 (默认 200)") |
| args = ap.parse_args() |
| print(f"{'[DRY-RUN] ' if args.dry_run else ''}回填质检标签 (旧批 user_id<={args.max_id}; 通过=strict里有, 淘汰=strict里没有)") |
| backfill_a1(args.dry_run, args.max_id) |
| backfill_a2(args.dry_run, args.max_id) |
| backfill_a3(args.dry_run, args.max_id) |
| print("完成。" + ("" if args.dry_run else " 现在增量跑时旧批跳过质检并保留原 strict 结果, 新用户正常质检。")) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|