File size: 2,963 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
#!/usr/bin/env python3
"""Draw a batch of plans for real-chain runs: a few of each kind of flat from one screened library, and (optionally) plans that a second, wider
library has and the first has not, each written as a one-plan library p01.json, p02.json ... (run_trials.sh takes those names as "kinds").
  pick_batch.py --library balanced.json [--wide-library wide.json --wide-count 6] --out-dir batch [--seed 1]"""
import argparse
import json
import random
from pathlib import Path

import pick_trials as kinds        # the kind tests live there; importing it must not run anything


def main():
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument('--library', required=True, type=Path)
    ap.add_argument('--wide-library', type=Path)
    ap.add_argument('--wide-count', type=int, default=6)
    ap.add_argument('--out-dir', required=True, type=Path)
    ap.add_argument('--seed', type=int, default=1)
    ap.add_argument('--plan', action='append', default=[], metavar='KIND=N', help='how many plans of a kind (default: the list below)')
    args = ap.parse_args()
    wanted = {'two_bedrooms_balcony': 3, 'two_bedrooms_no_balcony': 2, 'three_bedrooms': 3, 'study_as_bedroom': 2, 'contact_repaired': 2,
              'hallway_two_bedrooms': 2, 'hallway_three_bedrooms': 2, 'indirect_access': 2}
    wanted.update({k: int(v) for k, v in (item.split('=') for item in args.plan)})
    rng = random.Random(args.seed)
    library = json.loads(args.library.read_text())
    meta = {k: v for k, v in library.items() if k not in ('plans', 'plan_count', 'rejected_by_reason')}
    chosen, used = [], set()
    for kind, count in wanted.items():
        pool = [p for p in library['plans'] if kinds.KINDS[kind](p, kinds.traits(p)) and p['plan_id'] not in used]
        for plan in rng.sample(pool, min(count, len(pool))):
            used.add(plan['plan_id'])
            chosen.append((kind, plan, str(args.library)))
    if args.wide_library:
        have = {p['plan_id'] for p in library['plans']}
        wide = [p for p in json.loads(args.wide_library.read_text())['plans'] if p['plan_id'] not in have]
        for plan in rng.sample(wide, min(args.wide_count, len(wide))):
            chosen.append(('wide_only', plan, str(args.wide_library)))
    rng.shuffle(chosen)        # so that every parallel stream gets a mix
    args.out_dir.mkdir(parents=True, exist_ok=True)
    manifest = {}
    for index, (kind, plan, source) in enumerate(chosen, 1):
        name = 'p%02d' % index
        one = dict(meta, plan_count=1, plans=[plan])
        (args.out_dir / (name + '.json')).write_text(json.dumps(one, ensure_ascii=False))
        manifest[name] = dict(kind=kind, plan_id=plan['plan_id'], library=source)
        print('%s  %-24s %s' % (name, kind, plan['plan_id']))
    (args.out_dir / 'manifest.json').write_text(json.dumps(manifest, indent=1))


if __name__ == '__main__':
    main()