#!/usr/bin/env python3 """Windows the generator of one code tree gives each plan of a library, so two trees can be compared (before / after a generator change). compare_windows.py record --src --library lib.json --out windows_a.json [--limit N] compare_windows.py diff windows_a.json windows_b.json `diff` lists, per plan, the windows that appear, vanish or move, and says whether they all belong to further bedrooms.""" import argparse import copy import json import sys def record(args): import screen_library as sl sl.init_worker(args.src) plans = json.load(open(args.library))['plans'][:args.limit] out = {} for plan in plans: topo = sl.NATIVE.add_plan_topology(copy.deepcopy(plan), include_windows=True) out[plan['plan_id']] = sorted([w['room_id'], w['window_id'], w['window_interval_m'], w['wall_line_m']] for w in topo['windows']) json.dump(out, open(args.out, 'w')) print('recorded %d plans' % len(out)) def diff(args): a, b = json.load(open(args.a)), json.load(open(args.b)) same = changed_elsewhere = only_extra = 0 examples = [] for pid in a: if a[pid] == b.get(pid): same += 1 continue old = {tuple(map(json.dumps, w)) for w in a[pid]} new = {tuple(map(json.dumps, w)) for w in b[pid]} extra_rooms = {json.loads(w[0]) for w in new - old} removed_rooms = {json.loads(w[0]) for w in old - new} if not removed_rooms and all(r.startswith('bedroom_') for r in extra_rooms): only_extra += 1 else: changed_elsewhere += 1 if len(examples) < 5: examples.append((pid, sorted(removed_rooms), sorted(extra_rooms))) print('plans compared: %d' % len(a)) print(' identical windows: %d' % same) print(' only new windows of further bedrooms: %d' % only_extra) print(' any other change: %d' % changed_elsewhere) for example in examples: print(' e.g. %s removed rooms %s, new rooms %s' % example) if __name__ == '__main__': ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) sub = ap.add_subparsers(dest='mode', required=True) r = sub.add_parser('record') r.add_argument('--src', required=True) r.add_argument('--library', required=True) r.add_argument('--out', required=True) r.add_argument('--limit', type=int, default=4000) r.set_defaults(run=record) d = sub.add_parser('diff') d.add_argument('a') d.add_argument('b') d.set_defaults(run=diff) parsed = ap.parse_args() sys.exit(parsed.run(parsed))