Datasets:
Download code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_evidence.py from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 8.09 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_evidence.py
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_evidence.py
-
curl -L -o test_evidence.py https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_evidence.py
8.09 kB
| """Evidence samples preserve exact cells and cannot read private answer/proof fields.""" | |
| import json | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| from unittest import mock | |
| import numpy as np | |
| from tools.evaluation import blind_codex, evidence_export | |
| class EvidenceTest(unittest.TestCase): | |
| def test_raw_world_and_neighbor_ticks_without_private_fields(self): | |
| with tempfile.TemporaryDirectory() as tmp: | |
| p = Path(tmp) | |
| script = {'actors': [{'id': 'a', 'body_present': True}], 'frame': {'units': 'm'}, | |
| 'scene': {'rooms': [{'id': 'r', 'label': 'kitchen', 'polygon_uv': []}], 'objects': []}} | |
| (p / 'script.json').write_text(json.dumps(script)) | |
| np.savez(p / 'curves.npz', t=[0., 1., 2.], a__u=[0., 1., 3.], a__v=[0., 0., 0.], a__yaw=[0., 0., 0.]) | |
| np.savez(p / 'facts.npz', room_init=[[True]], room_event_t=[], room_event_actor=[], room_event_room=[], room_event_kind=[]) | |
| e = evidence_export.evidence({'slots': {'1': {'value': {'t': 1.}}}}, | |
| {'world_span_s': [1., 1.]}, {'videos': {}}, p) | |
| self.assertEqual([r[:3] for r in e['trajectories'][0]['rows']], [[0., 0., 0.], [1., 1., 0.], [2., 3., 0.]]) | |
| self.assertEqual(e['trajectories'][0]['rows'][1][-1], ['r']) | |
| self.assertFalse({'truth', 'basis', 'proof', 'replay', 'answer_margin'} & {k for n in evidence_export.nodes(e) for k in n}) | |
| def test_exact_version_scaffold_cells_seed_and_answer_separation(self): | |
| with tempfile.TemporaryDirectory() as tmp: | |
| p = Path(tmp); s = p / 'segment'; s.mkdir(); q = s / 'groups.jsonl' | |
| rows = [{'public': {'version_id': v, 'scaffold': sc, 'slots': {}}, 'candidate_id': str(n), | |
| 'binding': {}, 'presentation': {}, 'decision': {'result': 'PASS'}, | |
| 'replay': {'value': 'SECRET'}, 'proof': {'basis': 'SECRET'}} | |
| for n, (v, sc) in enumerate([('x/single', []), ('x/multi', []), ('x/single', ['grid']), ('x/single', ['pose'])])] | |
| q.write_text(''.join(json.dumps(r) + '\n' for r in rows)) | |
| def item(row, sid, out, **kw): | |
| return {'item_id': row['candidate_id'], 'tags': {}}, {'truth': row['replay']['value']} | |
| with mock.patch.object(evidence_export.np_export, 'item_of', side_effect=item), \ | |
| mock.patch.object(evidence_export, 'evidence', return_value={'world': 3}) as build: | |
| reports = [evidence_export.export([q], [s], p / str(n), 0) for n in range(2)] | |
| self.assertEqual(reports[0], reports[1]); self.assertEqual(reports[0]['cells'], 4) | |
| self.assertNotIn('SECRET', (p / '0/items.jsonl').read_text()) | |
| self.assertTrue(all(len(c.args) == 5 for c in build.call_args_list)) | |
| with self.assertRaises(ValueError): | |
| evidence_export.export([q], [s], p / '0', 0) | |
| def test_evidence_prompt_and_missing_review_are_explicit(self): | |
| item = {'item_id': 'x', 'version_id': 'x/single', 'question': {'zh': '题'}, 'tags': {'scaffold': 'none'}, 'evidence': {'world': 3}} | |
| with mock.patch.object(blind_codex.prompts, 'instructions', return_value='答'), \ | |
| mock.patch.object(blind_codex.subprocess, 'run', side_effect=FileNotFoundError) as run: | |
| with self.assertRaises(FileNotFoundError): | |
| blind_codex.ask([item], 30) | |
| prompt = run.call_args.kwargs['input'] | |
| self.assertIn('根据题面和世界状态证据作答', prompt); self.assertIn('"world": 3', prompt) | |
| self.assertEqual(run.call_args.args[0][-1], '-') | |
| with tempfile.TemporaryDirectory() as tmp: | |
| out = Path(tmp) | |
| blind_codex.write(out / 'items.jsonl', [item]) | |
| blind_codex.write(out / 'scores.jsonl', [{'item_id': 'x', 'passed': False}]) | |
| blind_codex.write(out / 'responses.jsonl', [{'item_id': 'x', 'status': 'OK'}]) | |
| blind_codex.write(out / 'reviews.jsonl', [{'item_id': 'x', 'category': '证据不够', 'reason': '缺对象轮廓'}]) | |
| with mock.patch.object(blind_codex.runner, 'score'): | |
| self.assertTrue(blind_codex.evidence_score(out, None)) | |
| self.assertEqual(json.loads((out / 'evidence-scores.json').read_text())['pending_review'], ['x']) | |
| self.assertFalse(blind_codex.evidence_score(out, out / 'reviews.jsonl')) | |
| self.assertEqual(json.loads((out / 'evidence-scores.json').read_text())['categories']['证据不够'], ['x']) | |
| if __name__ == '__main__': | |
| unittest.main() | |
| class EvidenceReviewRegression(unittest.TestCase): | |
| def test_timeout_is_recorded_and_next_pack_runs(self): | |
| item = {'item_id': 'a', 'version_id': 'v/x', 'question': {'zh': '题'}, 'evidence': {}} | |
| with mock.patch.object(blind_codex.prompts, 'instructions', return_value='答'), \ | |
| mock.patch.object(blind_codex.subprocess, 'run', side_effect=[ | |
| blind_codex.subprocess.TimeoutExpired('codex', 1), | |
| blind_codex.subprocess.CompletedProcess([], 0, '', '')]): | |
| row = blind_codex.ask([item], 1)[0] | |
| self.assertEqual((row['status'], row['rc']), ('ERROR', 124)) | |
| self.assertIn('timeout after 1 s', row['stderr_tail']) | |
| self.assertEqual(blind_codex.ask([dict(item, item_id='b')], 1)[0]['rc'], 0) | |
| def test_crop_clock_markers_and_doorways_are_raw_inputs(self): | |
| with tempfile.TemporaryDirectory() as tmp: | |
| p = Path(tmp) | |
| script = {'actors': [{'id': 'o', 'kind': 'observer_virtual', 'recording': { | |
| 'frame_count': 3, 'first_frame_s': 0., 'last_frame_s': 2.}}], 'frame': {'units': 'm'}, | |
| 'scene': {'rooms': [], 'objects': [], 'doorways': [{'id': 'd', 'rooms': ['a', 'b'], 'segment_uv': [[0, 0], [1, 0]]}]}} | |
| (p / 'script.json').write_text(json.dumps(script)) | |
| np.savez(p / 'curves.npz', t=[0., 1., 2.], o__u=[0., 1., 2.], o__v=[0., 0., 0.], o__yaw=[0., 0., 0.], o__cam_yaw=[0., 0., 0.]) | |
| np.savez(p / 'facts.npz', frame_time_s=[[0., 1., 2.]], room_init=np.zeros((1, 0), bool), room_event_t=[], room_event_actor=[], room_event_room=[], room_event_kind=[], obs_projected_height=np.zeros((1, 0, 3)), obs_occlusion=np.zeros((1, 0, 3))) | |
| e = evidence_export.evidence({'slots': {'p': {'value': {'points': {'A': [1, 2]}}}}}, {'world_span_s': [1., 1.]}, | |
| {'videos': {'A': 'o'}}, p, {'A': {'observer': 'o', 'start_frame': 1, 'end_frame_exclusive': 3}}) | |
| self.assertEqual(e['actors'][0]['recording']['first_frame_s'], 1.) | |
| self.assertEqual(e['map_markers'], {'A': [1, 2]}) | |
| self.assertEqual(e['doorways'][0]['segment_uv'], [[0, 0], [1, 0]]) | |
| self.assertEqual(e['schema'], 'world-state-evidence/2') | |
| def test_unparsed_review_can_finish_without_claiming_pass(self): | |
| with tempfile.TemporaryDirectory() as tmp: | |
| out = Path(tmp); item = {'item_id': 'x', 'version_id': 'v/x', 'tags': {'scaffold': 'none'}, 'evidence': {}} | |
| blind_codex.write(out / 'items.jsonl', [item]) | |
| blind_codex.write(out / 'scores.jsonl', [{'item_id': 'x', 'passed': False}]) | |
| blind_codex.write(out / 'responses.jsonl', [{'item_id': 'x', 'status': 'ERROR', 'rc': 0, 'text': 'null'}]) | |
| blind_codex.write(out / 'reviews.jsonl', [{'item_id': 'x', 'category': '不确定', 'reason': '原始模型文本未保存,无法判断'}]) | |
| with mock.patch.object(blind_codex.runner, 'score'): | |
| self.assertFalse(blind_codex.evidence_score(out, out / 'reviews.jsonl')) | |
| r = json.loads((out / 'evidence-scores.json').read_text()) | |
| self.assertEqual((r['passed'], r['not_run'], r['uncertain']), (0, ['x'], ['x'])) | |
| blind_codex.write(out / 'responses.jsonl', [{'item_id': 'x', 'status': 'ERROR', 'rc': 124}]) | |
| self.assertTrue(blind_codex.evidence_score(out, out / 'reviews.jsonl')) | |