File size: 2,197 Bytes
891c713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Grade /app/prediction.json for a v3 task. Writes reward.json (numeric values only).

- Predictions are keyed by target name ("best_+15m", ..., "best_deadline", "final"), clipped to [0, 100].
- Targets whose truth is missing (status != "ok") are not scored and do not count.
- A missing, non-numeric or non-finite prediction for a scored target counts as an error of 100.
- reward = mean over scored targets of max(0, 1 - |error| / 10); 0 with n_scored = 0 if none is scored.
- The parsed prediction is copied to <verifier dir>/prediction.json for offline scoring.
"""
import json, math, sys

pred_path, gt_path, out_path = sys.argv[1:4]
targets = json.load(open(gt_path))['targets']
scored = [t for t in targets if t.get('status') == 'ok']
res = {'reward': 0.0, 'valid': 0, 'n_targets': len(targets), 'n_scored': len(scored)}
try:
    p = json.load(open(pred_path))
    if not isinstance(p, dict) or not isinstance(p.get('predictions'), list):
        raise ValueError('predictions must be a list')
    preds = {}
    for x in p['predictions']:
        if not isinstance(x, dict) or not isinstance(x.get('target'), str) or x['target'] in preds:
            continue
        v = x.get('score')
        try:
            v = None if v is None or isinstance(v, bool) else float(v)
        except (TypeError, ValueError):
            v = None
        preds[x['target']] = v if v is not None and math.isfinite(v) else None
    try:
        json.dump({'predictions': [{'target': k, 'score': v} for k, v in preds.items() if v is not None],
                   'rationale': str(p.get('rationale', ''))[:4000]},
                  open(out_path.rsplit('/', 1)[0] + '/prediction.json', 'w'))
    except Exception:
        pass
    errs = []
    for t in scored:
        v = preds.get(t['target'])
        e = 100.0 if v is None else abs(max(0.0, min(100.0, v)) - t['truth'])
        errs.append(e)
        res['abs_err_' + t['target']] = e
    if errs:
        res['mae'] = sum(errs) / len(errs)
        res['reward'] = sum(max(0.0, 1 - e / 10) for e in errs) / len(errs)
    res['valid'] = 1
except Exception as ex:
    print('invalid prediction:', ex, file=sys.stderr)
json.dump(res, open(out_path, 'w'))