#!/usr/bin/env python3 """Do the rooms of a converted plan have the sizes and shapes of an ordinary flat of that total area? The converter scales a whole plan uniformly to a target area, so a plan that is 60 m2 in the data set and is scaled to 150 m2 gets a 16 m2 bathroom and a 55 m2 living room. `problems(plan, tier)` lists what is out of the ordinary; `conventional(plan, tier)` is True when nothing is. The numbers follow common residential practice (minimum and comfortable sizes of Chinese flats) and are plain data in TIERS and RANGES, so they can be changed in one place. proportions.py --library lib.json --tier 150 [--show 5] counts the plans that pass and why the others do not """ import argparse import json from collections import Counter # An area band: a flat belongs to the tier when its total floor area (sum of the room rectangles, m2) is inside the band, so 'near the area' # counts. Also: bedrooms the flat should have, and the share of the area the living + dining room may take TIERS = { '100': dict(area=(90, 112), bedrooms=(2, 3), living_dining_share=(0.22, 0.42)), '120': dict(area=(112, 135), bedrooms=(2, 3), living_dining_share=(0.22, 0.42)), '150': dict(area=(135, 170), bedrooms=(3, 4), living_dining_share=(0.22, 0.42)), } # per room: area range (m2), shortest side at least (m), longest over shortest side at most RANGES = { 'living': dict(area=(18, 42), side=3.3, aspect=2.3), 'dining': dict(area=(8, 24), side=2.4, aspect=2.5), 'kitchen': dict(area=(5, 14), side=1.9, aspect=2.8), 'bath': dict(area=(3.5, 9), side=1.6, aspect=2.3), 'master': dict(area=(12, 26), side=3.0, aspect=1.9), 'guest': dict(area=(9, 18), side=2.7, aspect=1.9), 'bedroom': dict(area=(8, 17), side=2.5, aspect=1.9), # bedroom_3, bedroom_4 ... 'balcony': dict(area=(3, 14), side=1.2, aspect=4.5), 'hallway': dict(area=(3.5, 9), side=1.1, aspect=99), # a corridor, not a stub and not a room } def kind(room_id): if room_id.startswith('bedroom_'): return 'bedroom' if room_id.startswith('hallway'): return 'hallway' return room_id def problems(plan, tier): out = [] spec = TIERS[tier] rooms = {r['room_id']: r['bounds_m'] for r in plan['rooms']} sizes = {rid: (b[2] - b[0], b[3] - b[1]) for rid, b in rooms.items()} areas = {rid: w * h for rid, (w, h) in sizes.items()} total = sum(areas.values()) if not spec['area'][0] <= total <= spec['area'][1]: out.append('total_area_%.0f' % total) bedrooms = sum(1 for r in plan['rooms'] if 'bedroom' in r['room_type']) if not spec['bedrooms'][0] <= bedrooms <= spec['bedrooms'][1]: out.append('bedrooms_%d' % bedrooms) for rid, (w, h) in sizes.items(): rule = RANGES.get(kind(rid)) if rule is None: continue if not rule['area'][0] <= areas[rid] <= rule['area'][1]: out.append('%s_area_%s' % (kind(rid), 'small' if areas[rid] < rule['area'][0] else 'large')) if min(w, h) < rule['side']: out.append('%s_narrow' % kind(rid)) if max(w, h) > rule['aspect'] * min(w, h): out.append('%s_elongated' % kind(rid)) if sum(1 for rid in rooms if kind(rid) == 'hallway') > 1: out.append('more_than_one_hallway') share = (areas.get('living', 0) + areas.get('dining', 0)) / total if total else 0 if not spec['living_dining_share'][0] <= share <= spec['living_dining_share'][1]: out.append('living_dining_share_%.2f' % share) if areas.get('living', 0) < areas.get('master', 0): out.append('living_smaller_than_master') if areas.get('dining', 0) > areas.get('living', 0): out.append('dining_larger_than_living') if areas.get('master', 0) < max((a for rid, a in areas.items() if kind(rid) in ('guest', 'bedroom')), default=0): out.append('master_smaller_than_another_bedroom') if areas.get('bath', 0) > 0.5 * areas.get('master', 1e9): out.append('bath_over_half_the_master') return out def conventional(plan, tier): return not problems(plan, tier) def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument('--library', required=True) ap.add_argument('--tier', required=True, choices=sorted(TIERS)) args = ap.parse_args() plans = json.load(open(args.library))['plans'] reasons, ok = Counter(), 0 for plan in plans: found = problems(plan, args.tier) ok += not found for item in found: reasons[item.rsplit('_', 1)[0] if item.split('_')[-1].replace('.', '').isdigit() else item] += 1 print('%d of %d plans pass for tier %s' % (ok, len(plans), args.tier)) for item, count in reasons.most_common(15): print(' %6d %s' % (count, item)) if __name__ == '__main__': main()