Datasets:
Download code/evaluation-code/v0.2-0c2690ae/tests/pipeline/test_batch_stats.py from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/tests/pipeline/test_batch_stats.py
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/v0.2-0c2690ae/tests/pipeline/test_batch_stats.py
-
curl -L -o test_batch_stats.py https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/tests/pipeline/test_batch_stats.py
10.8 kB
| """批次统计 np.batch_stats:按来源分开的输入分布、每版多样性四项,和两批对照的两向计数与噪声底判据。""" | |
| import json | |
| import contextlib | |
| import io | |
| from pathlib import Path | |
| import tempfile | |
| import unittest | |
| import numpy as np | |
| from np import batch_stats as B | |
| def q(v, text, value, start): | |
| return dict(public=dict(version_id=v, text=text), replay=dict(value=value), binding=dict(local_span_s=[start, start+5])) | |
| def write(root, seg, qs, pool=None, kinds=('humanoid', 'humanoid'), arr=()): | |
| d = Path(root) / 'out' / seg | |
| d.mkdir(parents=True) | |
| cfg = dict(cast=[dict(kind=k) for k in kinds], observers=[dict(kind='embodied', body='quadruped_cam', motion_mode='seek')], | |
| arrangements=[dict(type=a) for a in arr], **({'pool': pool} if pool else {})) | |
| (d / 'script.json').write_text(json.dumps(dict(world_id='w', generator=dict(house='/x/house-1', config=cfg)))) | |
| np.savez(d / 'facts.npz', room_event_kind=np.array([1, -1, 1], np.int8), door_event_t=np.zeros(2)) | |
| v = Path(root) / 'vout' / seg | |
| v.mkdir(parents=True) | |
| (v / 'questions.jsonl').write_text(''.join(json.dumps(x) + '\n' for x in qs)) | |
| class BatchStatsTest(unittest.TestCase): | |
| def test_segdist_grid_and_cli(self): | |
| with tempfile.TemporaryDirectory() as t: | |
| root = Path(t) | |
| for folder in ('cfg', 'state/seg', 'logs'): | |
| (root / folder).mkdir(parents=True) | |
| areas = json.loads((Path(B.__file__).parent / 'batches/release40-areas.json').read_text())['pass_m2'] | |
| scenes = sorted(areas, key=areas.get)[::max(1, len(areas)//4)][:4] | |
| inputs = [('p2_a_cr-s0', 60, 2, 0, 10), ('p4_b_ev-s0', 125, 4, 124, 300), | |
| ('p6_c_ob-s0', 126, 6, 2, 20), ('p2_d_cr-s0', 40, 2, 0, 60)] | |
| for scene, (name, dur, people, rc, wall) in zip(scenes, inputs): | |
| (root / 'cfg' / (name+'.json')).write_text(json.dumps(dict( | |
| id=name, scene=scene, duration_s=dur, cast=[{}]*people, observers=[{}]))) | |
| (root / 'state/seg' / (name+'.json')).write_text(json.dumps(dict(rc=rc, wall_s=wall))) | |
| (root / 'logs' / (name+'.log')).write_text( | |
| 'gave way too long\nno speed at 1 s\nno speed at 2 s\nrefused 123\n' if rc else 'ok\n') | |
| with contextlib.redirect_stdout(io.StringIO()) as output: | |
| self.assertEqual(B.main(['--segdist', '--run', t]), 0) | |
| r = json.loads(output.getvalue()) | |
| self.assertEqual(r, B.segdist(root)) | |
| self.assertEqual(r['total'], dict(segments=4, failed=2, failure_rate=.5)) | |
| self.assertEqual(r['timing_total'], dict(succeeded=2, wall_s_median=35, | |
| wall_s_p90=60, wall_s_p90_linear=55, le10_s=1, le60_s=2)) | |
| self.assertEqual(len(r['timing']), 9) | |
| self.assertEqual(sum(c['succeeded'] for c in r['timing']), 2) | |
| for axis, count in (('duration', 9), ('quartile', 12), ('kind', 3)): | |
| self.assertEqual(len(r['failures'][axis]), count) | |
| self.assertEqual(sum(c['segments'] for c in r['failures'][axis]), 4) | |
| self.assertEqual(sum(c['failed'] for c in r['failures'][axis]), 2) | |
| for band, people in (('mid', 4), ('long', 6)): | |
| c = next(c for c in r['failures']['duration'] if (c['bucket'], c['people']) == (band, people)) | |
| self.assertEqual((c['segments'], c['failed']), (1, 1)) | |
| self.assertEqual(sum(c['segments'] for c in r['failure_tails']), 2) | |
| self.assertEqual([c['segments'] for c in r['hard_reasons']], [2, 2]) | |
| self.assertEqual(r['missing_logs'], []) | |
| (root / 'logs/p2_a_cr-s0.log').unlink() | |
| self.assertEqual(B.segdist(root)['missing_logs'], ['p2_a_cr-s0']) | |
| (root / 'state/seg/p2_a_cr-s0.json').write_text('{"rc": null, "wall_s": 0}') | |
| with self.assertRaises(ValueError): | |
| B.segdist(root) | |
| with self.assertRaises(SystemExit), contextlib.redirect_stderr(io.StringIO()): | |
| B.main(['--segdist']) | |
| def test_shortfall_grid_and_cli(self): | |
| with tempfile.TemporaryDirectory() as t: | |
| root = Path(t) | |
| cfg = root / 'cfg'; cfg.mkdir() | |
| for name, house, duration in (('s1', 'h1', 40), ('s2', 'h1', 120), ('s3', 'h2', 300)): | |
| (cfg / f'{name}.json').write_text(json.dumps(dict(id=name, scene=f'/x/{house}', duration_s=duration))) | |
| a = q('distance.value.at_time/single_ego', 'a', 1, 0) | |
| b = q('speed.value.at_time/single_ego', 'b', 2, 0) | |
| # Explicit unit wins: a single-view control can belong to ability five. | |
| b['scope'] = dict(group=dict(unit='5.1-1.2', id='g1')) | |
| write(t, 's1', [a]) | |
| write(t, 's3', []) | |
| grouped = root / 'vout/s1/groups.jsonl' | |
| grouped.write_text(json.dumps(b) + '\n') | |
| report = B.shortfall(root / 'out', root / 'vout', cfg) | |
| cells = report['格子'] | |
| self.assertEqual(len(cells), 3 * 2 * 2) | |
| self.assertEqual(sum(c['目标题数'] for c in cells), 4000) | |
| self.assertEqual([c['目标题数'] for c in cells], [333, 333, 334] * 4) | |
| self.assertEqual(report['去重总数'], dict(目标段数=18, 成段数=2, 出题数=2, 缺口=16)) | |
| self.assertEqual(report['合计'], dict(目标段数=36, 成段数=4, 出题段数=2, 出题数=2, 题包数=1, 缺口=32, 出题缺口=34, 目标题数=4000)) | |
| for key, value in report['合计'].items(): | |
| self.assertEqual(sum(c[key] for c in cells), value, key) | |
| self.assertEqual(len(report['缺口前十']), 10) | |
| self.assertEqual([c['缺口'] for c in report['缺口前十']], sorted((c['缺口'] for c in cells), reverse=True)[:10]) | |
| for group in ('1-4', '5'): | |
| c = next(c for c in cells if (c['时段档'], c['能力组'], c['房子']) == ('short', group, 'h1')) | |
| self.assertEqual((c['目标段数'], c['成段数'], c['出题段数'], c['出题数'], c['缺口']), (3, 1, 1, 1, 2)) | |
| with contextlib.redirect_stdout(io.StringIO()) as output: | |
| self.assertEqual(B.main([str(root/'out'), str(root/'vout'), '--shortfall', '--cfg', str(cfg)]), 0) | |
| result = json.loads(output.getvalue()) | |
| self.assertEqual(result.pop('三维缺口'), report) | |
| self.assertEqual(result, json.loads(json.dumps(B.summarize(root/'out', root/'vout')))) | |
| for bad in (dict(unit='6.1', id='g1'), dict(unit='unknown', id='g1')): | |
| b['scope']['group'] = bad | |
| grouped.write_text(json.dumps(b) + '\n') | |
| with self.assertRaises(ValueError): | |
| B.shortfall(root/'out', root/'vout', cfg) | |
| grouped.write_text('') | |
| (cfg/'s2.json').write_text(json.dumps(dict(id='s2', scene='/x/h1', duration_s=75))) | |
| with self.assertRaises(ValueError): | |
| B.shortfall(root/'out', root/'vout', cfg) | |
| def test_summary_splits_pools_and_counts_diversity(self): | |
| with tempfile.TemporaryDirectory() as t: | |
| write(t, 's1', [q('v1', 'a', 1, 3), q('v1', 'b', 1, 14), q('v1', 'b', 2, 15)], pool='anti_shortcut', arr=('meet',)) | |
| write(t, 's2', [q('v1', 'a', 'x', 3)], kinds=('humanoid', 'quadruped')) | |
| r = B.summarize(Path(t) / 'out', Path(t) / 'vout') | |
| self.assertEqual(set(r), {'anti_shortcut', 'natural', '未标'}) | |
| self.assertEqual(r['natural']['分母'], 0) | |
| a = r['anti_shortcut'] | |
| self.assertEqual(a['每版']['v1']['写出中位'], 3) | |
| self.assertEqual([a['每版']['v1'][f'{n}中位'] for n in B.ITEMS], [3, 2, 2, 2]) | |
| self.assertEqual(a['输入分布']['事件'], {'进房': 2, '出房': 1, '穿门': 2}) | |
| self.assertEqual(a['输入分布']['安排类型'], {'meet': 1}) | |
| self.assertEqual(a['每版']['v1']['作答分布']['中位'], 1) | |
| self.assertEqual(r['未标']['输入分布']['主体种类'], {'humanoid': 1, 'quadruped': 1}) | |
| def test_final_denominators_and_full_answer_counts(self): | |
| with tempfile.TemporaryDirectory() as t: | |
| for i, pool in enumerate(('anti_shortcut', 'natural', None)): | |
| write(t, str(i), [q('v', 'x', n, 0) for n in range(10)], pool=pool) | |
| r = B.summarize(Path(t) / 'out', Path(t) / 'vout') | |
| self.assertEqual({p: b['分母'] for p, b in r.items()}, dict(anti_shortcut=10, natural=10, 未标=10)) | |
| for b in r.values(): | |
| self.assertEqual(b['每版']['v']['分母'], 10) | |
| self.assertEqual(sum(b['每版']['v']['答案计数'].values()), 10) | |
| self.assertEqual((b['各版载体配方'], b['一般稀有性判据']), ('NOT_CHECKED', 'NOT_CHECKED')) | |
| def test_compare_counts_both_directions_and_uses_noise_floor(self): | |
| with tempfile.TemporaryDirectory() as t: | |
| T = Path(t) | |
| for name, vals in (('a', (1, 2, 3)), ('b', (1, 1, 3)), ('c', (1, 2, 2))): | |
| for seg in ('s1', 's2'): | |
| d = T / name / seg | |
| d.mkdir(parents=True) | |
| rows = [q('v1', f't{i}', x if seg == 's1' or name != 'b' else (1, 2, 3)[i], 10*i) for i, x in enumerate(vals)] | |
| (d / 'questions.jsonl').write_text(''.join(json.dumps(x) + '\n' for x in rows)) | |
| r = B.compare(T / 'a', T / 'b', noise=T / 'c') | |
| ans = r['B']['各项']['作答取值'] | |
| self.assertEqual(ans['合计'], [6, 5]) # s1 降一(1,1,3 两种),s2 与对照相同 | |
| self.assertEqual((ans['降'], ans['升'], ans['z']), (1, 0, 1.0)) | |
| self.assertEqual(r['噪声底']['各项']['作答取值']['合计'], [6, 4]) | |
| self.assertEqual(r['退步'], []) # 只一版降,z 1.0 不显著 | |
| self.assertEqual(r['噪声底显著'], []) | |
| def test_compare_flags_consistent_drop(self): | |
| with tempfile.TemporaryDirectory() as t: | |
| T = Path(t) | |
| for name, vals in (('a', (1, 2, 3)), ('b', (1, 1, 3))): | |
| for s in range(6): # 六段都降:z = 6/√6 ≈ 2.45 > 2 | |
| d = T / name / f's{s}' | |
| d.mkdir(parents=True) | |
| (d / 'questions.jsonl').write_text(''.join(json.dumps(q('v1', f't{i}', x, 10*i)) + '\n' for i, x in enumerate(vals))) | |
| r = B.compare(T / 'a', T / 'b') | |
| self.assertEqual(r['B']['各项']['作答取值']['z'], 2.45) | |
| self.assertEqual(r['退步'], ['作答取值']) | |
| if __name__ == '__main__': | |
| unittest.main() | |