"""Independent Fraction model; all inputs are synthetic classroom data.""" import json import sys import platform from fractions import Fraction as F from pathlib import Path ROOT = Path(__file__).resolve().parent CASES = json.loads((ROOT / 'cases.json').read_text(encoding='utf-8')) def model(x): fc, wp, depth = F(x['fc'], 100), F(x['wp'], 100), F(x['depth'], 100) theta = None if x['current'] is None else F(x['current'], 100) p = None if x['p'] is None else F(x['p'], 100) storage_fc, storage_wp = 1000 * fc * depth, 1000 * wp * depth taw = storage_fc - storage_wp raw = None if p is None else p * taw stored = None if theta is None else 1000 * theta * depth depletion = None if stored is None else storage_fc - stored inside = None if theta is None else wp <= theta <= fc remaining = taw - depletion if inside else None if inside is False: outcome = 'outside' elif inside is None: outcome = 'pending' elif raw is not None: outcome = 'within' if depletion <= raw else 'exceeds' else: outcome = 'within' if depletion == 0 else 'pending' def milli(q): if q is None: return None n = q * 1000 assert n.denominator == 1 return n.numerator return dict(input={k:x[k] for k in ['fc','wp','depth','current','p']}, fc_storage_milli=milli(storage_fc), wp_storage_milli=milli(storage_wp), taw_milli=milli(taw), raw_milli=milli(raw), raw_range_milli=[0, milli(taw)] if raw is None else [milli(raw)]*2, current_storage_milli=milli(stored), depletion_milli=milli(depletion), in_interval=inside, remaining_milli=milli(remaining), outcome=outcome) main=[] for profile in CASES['profiles']: for depth in CASES['depths']: for current in CASES['currents']: for p in CASES['fractions']: x = dict(fc=profile['fc'], wp=profile['wp'], depth=depth, current=current, p=p) main.append(dict(profile=profile['id'], **model(x))) transfer=[dict(id=x['id'], **model(x)) for x in CASES['transfers']] result=dict(python_version=platform.python_version(), method='Fraction volumetric fractions and depth in metres; storage differences, exact threshold and explicit missing/outside domain branches; output integer thousandths of mm, not measured precision.',main=main,transfer=transfer) out=json.dumps(result, ensure_ascii=False, indent=2)+'\n' if len(sys.argv)>1: assert sys.argv[1]=='--check' and len(sys.argv)==3 assert Path(sys.argv[2]).read_bytes()==out.encode('utf-8') print('Identical bytes') else: sys.stdout.reconfigure(encoding='utf-8', newline='\n') print(out, end='')