#!/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 --albums [--out trips.json] `albums.json` is what the core's own rule answered (cotton_core::rules::media::albums: infer_home, then derive_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 is the trip (Jaccard of the two member sets at least 0.5 counts as a clean match; the share of the trip's photos inside proposals of kind trip is said too). A proposed trip is right when it matches a known trip. Places (DECISIONS: 30 or more photos at one place on 3 or more days, away from home) are counted from the truth, since the rule has no place kind. """ import argparse import json from collections import defaultdict def 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.0 for k, (p, m) in enumerate(proposed): inter = len(members & m) if not 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 else 0 if best is not None and bj >= 0.5: clean += 1 right.add(best) if best is not None and bcover >= 0.5: found += 1 title = proposed[best][0]["title"] if best is not None else None 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 else None, trip_recall=round(clean / len(trips), 4) if trips else None, places=[dict(place=p, photos=len(f), days=len(days[p]), qualifies=len(f) >= 30 and 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()