Cotton code
83 lines · 3.5 KB · PythonRaw
#!/usr/bin/env python3"""The core's Sets on a synthetic library, against the shots the library knows are one moment.    python3 scripts/albums-sets-score.py --core <albums-core.json> --export <albums-export.jsonl> --truth <truth.jsonl>`albums-core.json` is the Dev build's `-albums-core` export: `albums.sets`' every row, each with its `members` and`keepers` (PhotoKit ids). The phone's own export maps ids to file names, and the truth names each file's moment.Pairs: two camera shots of one moment are a true pair. Precision is the share of pairs inside one Set that are onemoment; recall is the share of true pairs (moments of two or more shots) that land in one Set. Picks: how many a Setkeeps against its size, and how often a kept shot is one the truth drew blurred or dark."""import argparseimport jsonfrom collections import Counter, defaultdictfrom itertools import combinationsdef main():    ap = argparse.ArgumentParser()    ap.add_argument("--core", required=True)    ap.add_argument("--export", required=True)    ap.add_argument("--truth", required=True)    ap.add_argument("--out")    a = ap.parse_args()    core = json.load(open(a.core))    files = {}    for line in open(a.export):        r = json.loads(line)        files[r["id"]] = r.get("file")    truth = {}    for line in open(a.truth):        t = json.loads(line)        truth[t["file"]] = t    sets = core.get("sets") or []    in_pairs = same = 0    pair_set = set()    sizes, keeps, bad_keeps, kept = [], [], 0, 0    purity = []    for s in sets:        members = [files.get(i) for i in s.get("members", [])]        moments = [truth[f]["moment"] for f in members if f in truth]        if moments:            purity.append(Counter(moments).most_common(1)[0][1] / len(moments))        for x, y in combinations([f for f in members if f in truth], 2):            in_pairs += 1            if truth[x]["moment"] == truth[y]["moment"]:                same += 1                pair_set.add(frozenset((x, y)))        sizes.append(len(members))        keepers = [files.get(i) for i in s.get("keepers", [])]        keeps.append(len(keepers))        for f in keepers:            if f in truth:                kept += 1                bad_keeps += bool(truth[f].get("blurred") or truth[f].get("dark"))    by_moment = defaultdict(list)    for f, t in truth.items():        if t["kind"] == "camera":            by_moment[t["moment"]].append(f)    true_pairs = [frozenset(p) for m in by_moment.values() if len(m) > 1 for p in combinations(m, 2)]    found = sum(1 for p in true_pairs if p in pair_set)    tidy = core.get("tidy") or {}    out = dict(        sets=len(sets), photos_in_sets=sum(sizes),        moments_of_two_or_more=sum(1 for m in by_moment.values() if len(m) > 1),        pair_precision=round(same / in_pairs, 4) if in_pairs else None,        pair_recall=round(found / len(true_pairs), 4) if true_pairs else None,        set_purity_mean=round(sum(purity) / len(purity), 4) if purity else None,        keeps_mean=round(sum(keeps) / len(keeps), 2) if keeps else None,        keeps_mean_sets_of_10_or_more=round(sum(k for k, n in zip(keeps, sizes) if n >= 10) / max(1, sum(1 for n in sizes if n >= 10)), 2),        kept_blurred_or_dark=round(bad_keeps / kept, 4) if kept else None,        tidy_lists=[(l.get("name"), l.get("count"), l.get("size")) for l in tidy.get("lists", [])],        lines=core.get("lines"))    text = json.dumps(out, indent=2, ensure_ascii=False)    print(text)    if a.out:        open(a.out, "w").write(text)if __name__ == "__main__":    main()