#!/usr/bin/env python3"""Albums that make themselves, on a synthetic library: the core's trip rule against the library's known trips. python3 scripts/albums-trips-score.py --truth <truth.jsonl> --albums <albums.json> [--out trips.json]`albums.json` is what the core's own rule answered (cotton_core::rules::media::albums: infer_home, thenderive_albums), written by the lane's harness from the library's candidates. The truth names each trip(scripts/albums-library.py's timeline: home, weekends at one other place, about three trips a year).A known trip is found when one proposed trip holds at least half of its photos and at least half of that proposal isthe trip (Jaccard of the two member sets at least 0.5 counts as a clean match; the share of the trip's photos insideproposals of kind trip is said too). A proposed trip is right when it matches a known trip. Places (DECISIONS: 30 ormore photos at one place on 3 or more days, away from home) are counted from the truth, since the rule has no placekind."""import argparseimport jsonfrom collections import defaultdictdef main(): ap = argparse.ArgumentParser() ap.add_argument("--truth", required=True) ap.add_argument("--albums", required=True) ap.add_argument("--out") a = ap.parse_args() truth = [json.loads(l) for l in open(a.truth)] albums = json.load(open(a.albums)) trips = defaultdict(set) days = defaultdict(set) at_place = defaultdict(set)for t in truth:if t.get("trip"): trips[t["trip"]].add(t["file"])if t.get("place") and t["kind"] in ("camera", "document"): at_place[t["place"]].add(t["file"]) days[t["place"]].add(t["when"][:10]) proposals = albums["result"]["proposals"] proposed = [(p, {m["id"] for m in p["members"]}) for p in proposals if p["kind"] == "trip"] events = [p for p in proposals if p["kind"] == "event"] found, clean, right = 0, 0, set() rows = []for name, members in sorted(trips.items(), key=lambda kv: int(kv[0].split("-")[1])): best, bj, bcover = None, 0.0, 0.0for k, (p, m) in enumerate(proposed): inter = len(members & m)ifnot inter:continue j = inter / len(members | m)if j > bj: best, bj, bcover = k, j, inter / len(members) covered = len(members & set().union(*[m for _, m in proposed])) / len(members) if proposed else0if best isnotNoneand bj >= 0.5: clean += 1 right.add(best)if best isnotNoneand bcover >= 0.5: found += 1 title = proposed[best][0]["title"] if best isnotNoneelseNone rows.append(dict(trip=name, photos=len(members), jaccard=round(bj, 3), share_in_best=round(bcover, 3), share_in_any_trip=round(covered, 3), title=title)) home = albums.get("home") out = dict( home=home and home.get("label"), known_trips=len(trips), proposed_trips=len(proposed), events=len(events), trips_found=found, trips_clean=clean, proposed_trips_right=len(right), trip_precision=round(len(right) / len(proposed), 4) if proposed elseNone, trip_recall=round(clean / len(trips), 4) if trips elseNone, places=[dict(place=p, photos=len(f), days=len(days[p]), qualifies=len(f) >= 30and len(days[p]) >= 3)for p, f in sorted(at_place.items(), key=lambda kv: -len(kv[1]))], trips=rows) text = json.dumps(out, indent=2) print(text)if a.out: open(a.out, "w").write(text)if __name__ == "__main__": main()