File size: 2,934 Bytes
c00c855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Write the Dress-ED ids that match_dress_ed.py found into the package index (2026-11-11).

For every record of index.jsonl.gz: dress_ed_item_id and dress_ed_filename (they read "pending" before), the
Dress-ED file (path in the repository at --revision), row group and row in it, and how the record was matched. The
direction comes from Dress-ED's own text (the record with instruction_forward is forward); where it differs from the
direction guessed when the index was built, `direction` is corrected and `direction_basis` says so. A record the
matcher left unmatched keeps "pending" and is counted. Prints a summary.

    python apply_mapping.py --index index.jsonl.gz --mapping mapping.jsonl.gz --revision <sha> --out index_v2.jsonl.gz
"""
from __future__ import annotations

import argparse
import collections
import gzip
import json
import re
import sys


def main(argv=None) -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--index", required=True)
    ap.add_argument("--mapping", required=True)
    ap.add_argument("--revision", required=True)
    ap.add_argument("--out", required=True)
    args = ap.parse_args(argv)
    m = {}
    with gzip.open(args.mapping, "rt", encoding="utf-8") as fh:
        for line in fh:
            r = json.loads(line)
            m[r["record_id"]] = r
    st = collections.Counter()
    with gzip.open(args.index, "rt", encoding="utf-8") as fi, gzip.open(args.out, "wt", encoding="utf-8") as fo:
        for line in fi:
            r = json.loads(line)
            x = m.get(r["record_id"])
            st["records"] += 1
            if not x or not x.get("dress_ed_item_id"):
                st["unmatched"] += 1
                fo.write(json.dumps(r) + "\n")
                continue
            path = re.sub(r"^datasets/[^@]+@[0-9a-f]+/", "", x["parquet_file"])
            r.update({"dress_ed_item_id": x["dress_ed_item_id"], "dress_ed_filename": x["dress_ed_filename"],
                      "dress_ed_revision": args.revision, "dress_ed_file": path,
                      "dress_ed_row_group": x["row_group"], "dress_ed_row_in_group": x["row_in_group"],
                      "dress_ed_match": x["match"].split(":")[0],
                      "dress_ed_edited_image_check": x.get("edited_image_check")})   # same / differs / None (not checked)
            if r.get("direction") != x["direction"]:
                st["direction_corrected"] += 1
                r["direction"] = x["direction"]
                r["direction_basis"] = "Dress-ED's instruction_forward / instruction_reverse (corrected 2026-11-11)"
            st["mapped"] += 1
            st["match:" + r["dress_ed_match"]] += 1
            st[f"edited_image_check:{r['dress_ed_edited_image_check']}"] += 1
            fo.write(json.dumps(r) + "\n")
    print(json.dumps(dict(st), indent=1))
    return 0 if st["unmatched"] == 0 else 1


if __name__ == "__main__":
    sys.exit(main())