File size: 2,693 Bytes
870b85a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
#!/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 <tree> --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))