Datasets:
Download examples/run_decision_workflow.py from dengzhe-hou/HumanDec: direct link, hf CLI and curl.
- Browser
- Download file 6.65 kB
-
https://huggingface.co/datasets/dengzhe-hou/HumanDec/resolve/main/examples/run_decision_workflow.py
- Command line
-
hf download hf://datasets/dengzhe-hou/HumanDec/examples/run_decision_workflow.py
-
curl -L -o run_decision_workflow.py https://huggingface.co/datasets/dengzhe-hou/HumanDec/resolve/main/examples/run_decision_workflow.py
6.65 kB
| """Evaluate checkpoint starting gaps, adapted gaps and transfer with fixed protocols.""" | |
| import argparse | |
| import json | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| METRICS = ('nll', 'kappa', 'wrong_label', 'accuracy') | |
| ARMS = ('before_base', 'before_decision', 'after_base', 'after_decision') | |
| def sign(row, lower_better=True): | |
| ci = row['ci95'] | |
| if row['point'] is None or ci is None: | |
| return '未定义' | |
| if ci[0] <= 0 <= ci[1]: | |
| return '不确定' | |
| favorable = ci[1] < 0 if lower_better else ci[0] > 0 | |
| return '有利于决策版' if favorable else '有利于普通版' | |
| def cell(row): | |
| if row['point'] is None: | |
| return '未定义' | |
| interval = row['ci95'] | |
| ci = '区间未定义' if interval is None else f"[{interval[0]:+.6f}, {interval[1]:+.6f}]" | |
| return f"{row['point']:+.6f} {ci}" | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--bundle', type=Path, required=True) | |
| mode = parser.add_mutually_exclusive_group(required=True) | |
| mode.add_argument('--reference-pair', choices=('qwen/intern', 'lfm/d1')) | |
| mode.add_argument('--prediction-config', type=Path) | |
| parser.add_argument('--fold', choices=('low-pass', 'uniform-noise', 'sketch'), default='low-pass') | |
| parser.add_argument('--seed', type=int, choices=(0, 1, 2), default=0) | |
| parser.add_argument('--output-dir', type=Path, required=True) | |
| args = parser.parse_args() | |
| from hudec.bundle import Bundle | |
| bundle = Bundle(args.bundle) | |
| protocols = [f'internal/{args.fold}', f'external/{args.fold}/full', | |
| f'external/{args.fold}/source_nonoverlap'] | |
| config = None | |
| if args.prediction_config: | |
| config = json.loads(args.prediction_config.read_text()) | |
| if set(config['protocols']) != set(protocols): | |
| raise ValueError('Prediction config must cover the selected source fold and both external views') | |
| for protocol in protocols: | |
| if set(config['protocols'][protocol]) != set(ARMS): | |
| raise ValueError('Each protocol needs the same four predictor roles') | |
| for path in config['protocols'][protocol].values(): | |
| if not Path(path).is_file(): | |
| raise FileNotFoundError(path) | |
| if args.output_dir.exists(): | |
| raise FileExistsError(f'Use a new output directory: {args.output_dir}') | |
| args.output_dir.mkdir(parents=True) | |
| reports = {} | |
| for protocol in protocols: | |
| output = args.output_dir / (protocol.replace('/', '__') + '.json') | |
| if args.reference_pair: | |
| command = [sys.executable, '-B', str(Path(__file__).with_name('run_reference_example.py')), | |
| '--bundle', str(bundle.root), '--protocol', protocol, '--pair', args.reference_pair, | |
| '--seed', str(args.seed), '--output', str(output)] | |
| else: | |
| command = [sys.executable, '-B', str(Path(__file__).with_name('paired_change_example.py')), | |
| '--bundle', str(bundle.root), '--protocol', protocol, '--output', str(output)] | |
| for arm in ARMS: | |
| command.extend(['--'+arm.replace('_', '-'), str(Path(config['protocols'][protocol][arm]).resolve())]) | |
| subprocess.run(command, check=True) | |
| reports[protocol] = json.loads(output.read_text()) | |
| report = {'name': args.reference_pair if args.reference_pair else config['name'], | |
| 'fold': args.fold, 'training_seed': args.seed if args.reference_pair else None, | |
| 'input_kind': 'bundled reference predictions' if args.reference_pair else 'user supplied predictions', | |
| 'new_model_inference': False, 'metrics': list(METRICS), 'protocols': reports, | |
| 'calibration_policy': 'Source validation only; no checkpoint or temperature fitting on test responses', | |
| 'seed_uncertainty': 'Intervals condition on the supplied fitted checkpoints', | |
| 'scope': 'Reports the supplied checkpoints; does not select checkpoints using external test responses'} | |
| (args.output_dir/'REPORT.json').write_text(json.dumps(report, indent=2, allow_nan=False)+'\n') | |
| lines = ['# 决策模型的起点、微调变化和迁移', '', | |
| f"模型组合为 {report['name']},来源留出条件为 {args.fold}。本次只评分已有概率,没有模型推理或训练。", '', | |
| '| 测试视图 | 微调前差距 | 微调后差距 | 直接配对变化 |', | |
| '| --- | --- | --- | --- |'] | |
| for protocol, result in reports.items(): | |
| nll = [result[k]['nll'] for k in ('pre_gap', 'post_gap', 'adaptation_change')] | |
| lines.append('| '+protocol+' | '+' | '.join(sign(x) for x in nll)+' |') | |
| lines += ['', '上表以人类选择NLL为主指标,差距均为决策版减普通版。直接变化是微调后差距减微调前差距,方向不等于两个模型各自的进退。', '', | |
| '每个视图的下表保留原四指标与95%配对区间。普通版自身变化和决策版自身变化都是微调后减微调前,NLL为负表示改善,其他三指标为正表示改善。'] | |
| labels = {'nll':'人类选择NLL', 'kappa':'正确错误κ', 'wrong_label':'双方均错时答案一致率', 'accuracy':'每图等权正确率'} | |
| for protocol, result in reports.items(): | |
| lines += ['', '## '+protocol, '', | |
| '| 指标 | 微调前差距 | 微调后差距 | 直接变化 | 普通版自身变化 | 决策版自身变化 |', | |
| '| --- | --- | --- | --- | --- | --- |'] | |
| for metric in METRICS: | |
| lines.append('| '+labels[metric]+' | '+' | '.join(cell(result[k][metric]) for k in | |
| ('pre_gap','post_gap','adaptation_change','base_adaptation','decision_adaptation'))+' |') | |
| lines += ['', '全部协议使用固定16类顺序、来源校准和原2000次刺激与观察者配对抽样。外部噪声版本按原图聚类。反向、含零和未定义项都保留;格子不是独立实验,也不代表跨训练种子的总体区间。', | |
| '', '这份报告比较已经给定的模型。它不使用外部测试反应事先选模型,不定位内部机制,也不声称在新模型上保证得到相同结论。自己的模型先按NEW_MODEL.md生成四组概率,再用同一入口评分。'] | |
| (args.output_dir/'REPORT.md').write_text('\n'.join(lines)+'\n', encoding='utf-8') | |
| (args.output_dir/'RUN.json').write_text(json.dumps({'complete':True, 'protocols':protocols, 'metrics':list(METRICS), 'bootstrap_draws':2000},indent=2)+'\n') | |
| print(args.output_dir/'REPORT.md') | |
| if __name__ == '__main__': | |
| main() | |