File size: 6,643 Bytes
fb0074a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import unittest
import numpy as np
from common import qpos_from_absolute, ISAAC_TO_MUJOCO, PEER, WORK, OMNI, OMG

class AdapterTests(unittest.TestCase):
    def motion(self,n=51):
        x=np.zeros((n,36),np.float32);x[:,32]=1;x[:,31]=.8
        return x

    def test_joint_order(self):
        x=self.motion();x[:,:29]=np.arange(29)
        q=qpos_from_absolute(x)
        np.testing.assert_array_equal(q[0,7:],np.array(ISAAC_TO_MUJOCO))
        np.testing.assert_allclose(q[0,:7],[0,0,.8,1,0,0,0])

    def test_timing(self):
        x=self.motion();x[:,29]=np.arange(51)/50
        q=qpos_from_absolute(x)
        self.assertEqual(q.shape,(31,36))
        np.testing.assert_allclose(q[:,0],np.arange(31)/30,atol=1e-7)

    def test_quaternion_sign(self):
        x=self.motion();x[1::2,32]=-1
        q=qpos_from_absolute(x)
        np.testing.assert_allclose(abs(q[:,3]),1)

    def test_reject_bad_input(self):
        x=self.motion();x[0,32]=0
        with self.assertRaises(ValueError):qpos_from_absolute(x)
        with self.assertRaises(ValueError):qpos_from_absolute(np.zeros((10,40)))

    def test_matches_formal_renderer(self):
        import ast
        path=PEER/'GR00T-WholeBodyControl/gear_sonic/scripts/render_reference_motion_mujoco.py'
        if not path.exists():self.skipTest('Optional upstream renderer unavailable after migration')
        for node in ast.parse(path.read_text()).body:
            if isinstance(node,ast.Assign) and any(isinstance(t,ast.Name) and t.id=='ISAACLAB_TO_MUJOCO_DOF' for t in node.targets):
                self.assertEqual(ast.literal_eval(node.value.args[0]),ISAAC_TO_MUJOCO)
                break
        else:self.fail('Renderer mapping missing')

    def test_original_evaluator_schema(self):
        import sys
        import tempfile
        from pathlib import Path
        sys.path[:0]=[str(OMNI),str(OMG/'src')]
        from metrics import load_benchmark_inputs
        # Our packing adapter, not the reference evaluator CLI.
        import importlib.util
        spec=importlib.util.spec_from_file_location('hfd_eval_adapter',WORK/'evaluate.py')
        module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module)
        (WORK/'outputs').mkdir(exist_ok=True)
        with tempfile.TemporaryDirectory(dir=WORK/'outputs') as tmp:
            folder=Path(tmp);rows=[]
            for i in range(32):
                sid=f'test_{i}'
                np.savez(folder/f'{sid}.npz',absolute=self.motion())
                rows.append(dict(sample_id=sid,score_start=0,score_end=51,caption=f'action {i}',
                    dataset_index=i,reference_file=f'{sid}.npz'))
            target=folder/'packed';frames=module.pack(rows,folder,folder,target)
            kwargs=dict(task='text',generated_path=str(target/'generated.npz'),
                reference_path=str(target/'reference.npz'),condition_path=str(target/'conditions.npz'),
                expected_frames=frames,expected_fps=30)
            result=load_benchmark_inputs(**kwargs)
            self.assertEqual(result['generated_qpos'].shape,(32,31,36))
            with np.load(target/'conditions.npz') as z:bad={k:z[k] for k in z.files}
            bad['sample_id']=bad['sample_id'][::-1]
            np.savez(target/'conditions.npz',**bad)
            with self.assertRaises(ValueError):load_benchmark_inputs(**kwargs)

    def test_stratified_selection(self):
        import copy
        from collections import Counter
        from select_cohort import balanced_select
        candidates=[dict(caption_key=f'action {i}',features=dict(categories=[str(i%4)],
            duration_bin=i%5,travel_bin=i%3),record=dict(filename=str(i))) for i in range(512)]
        a=balanced_select(copy.deepcopy(candidates),128,42)
        b=balanced_select(copy.deepcopy(candidates),128,42)
        self.assertEqual(a,b)
        self.assertEqual(len({r['record']['filename'] for r in a}),128)
        counts=Counter(r['features']['primary_category'] for r in a)
        self.assertLessEqual(max(counts.values())-min(counts.values()),4)
        with self.assertRaises(ValueError):balanced_select([],32,42)

    def test_vendored_fingerprints(self):
        import json
        from common import sha256
        manifest=json.loads((WORK/'vendor/manifest.json').read_text())
        for row in manifest['files']:
            if row['path'].endswith('.py'):
                self.assertEqual(sha256(WORK/row['path']),row['sha256'])
        self.assertEqual(sha256(WORK/'vendor/evaluator_v13.pt'),
            'e6aedfdce0346296c57f9491b3e282f818721e3880698aa0de90d45d60613d2b')

    def test_session_quotas(self):
        from session_data import quotas
        q=quotas(2048)
        self.assertEqual(sum(q.values()),2048)
        self.assertEqual(q['interruption']+q['stop'],q['turn-taking'])
        self.assertEqual(set(q),{'turn-taking','interruption','stop','backchannel','hesitation','pause','third-party'})

    def test_session_final_command_and_interruptions(self):
        from session_data import motion_segments, flattened_rows
        records=[dict(instruction='walk forward',instruction_zh='向前走',filename='walk',row_id=1),
                 dict(instruction='wave hands',instruction_zh='挥手',filename='wave',row_id=2)]
        steps=[dict(turn_index=i//100,latest_instruction_text=('向前走' if i<100 else '挥手') if i%100==0 else '') for i in range(200)]
        source=dict(steps=steps,interaction_mode='interruption',session_voice_name='female_01_zh',
                    disfluency_events=[dict(text='go backward, no, wave hands')])
        labels=np.ones(210,dtype=int)
        segments,turns=motion_segments(source,records,labels)
        self.assertFalse(segments[0]['eligible'])
        self.assertTrue(segments[1]['eligible'])
        self.assertEqual(segments[1]['caption'],'wave hands')
        self.assertEqual(segments[1]['final_instruction'],'挥手')
        self.assertEqual((segments[1]['score_start'],segments[1]['score_end']),(99,209))
        flat=flattened_rows([dict(sample_id='session',segments=segments)])
        self.assertEqual(len(flat),1)
        self.assertEqual(flat[0]['parent_sample_id'],'session')
        source['interaction_mode']='emergency-stop'
        source['control_command_events']=[dict(turn_index=1,text='stop')]
        segments,_=motion_segments(source,records,labels)
        self.assertTrue(segments[0]['eligible'])
        self.assertFalse(segments[1]['eligible'])
        steps[100]['latest_instruction_text']='废弃指令'
        with self.assertRaises(ValueError):motion_segments(source,records,labels)

if __name__=='__main__':unittest.main()