File size: 4,586 Bytes
9befad5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import io
from pathlib import Path
import sys
import msgpack
import numpy as np

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from gpt6_b1k.recording import sensor_only
from gpt6_b1k.server import pack, unpack
from gpt6_b1k.policy import TrashPolicy
from gpt6_b1k.perception import held_can_evidence, refine_can
from gpt6_b1k.motion import SkillFailure
from run import summarize


def test_privileged_fields_do_not_enter_policy_observations():
    raw = {'robot::proprio': np.zeros(61), 'robot::head::rgb': np.zeros((4, 4, 3)),
           'robot::head::depth_linear': np.ones((4, 4)),
           'robot::head::seg_instance': np.ones((4, 4)), 'object_poses': {'can': [1, 2, 3]},
           'robot_global_pose': [0, 0, 0], 'q_score': 1.0}
    assert set(sensor_only(raw)) == {'robot::proprio', 'robot::head::rgb', 'robot::head::depth_linear'}


def test_official_wire_arrays_roundtrip():
    arrays = {'action': np.arange(23, dtype=np.float32), 'rgb': np.ones((3, 4, 3), dtype=np.uint8)}
    result = msgpack.unpackb(msgpack.packb(arrays, default=pack), object_hook=unpack, strict_map_key=False)
    for name, value in arrays.items():
        np.testing.assert_array_equal(result[name], value)
        assert result[name].dtype == value.dtype


def test_reset_clears_episode_memory():
    policy = TrashPolicy(io.StringIO())
    policy.objects = [dict(done=True)]
    policy.odom.pose[:] = 3
    policy.gripper = -1
    policy.reset()
    assert policy.objects == [] and policy.gripper == 1
    np.testing.assert_array_equal(policy.odom.pose, np.zeros(3))


def test_open_gripper_is_not_mistaken_for_carried_can():
    from types import SimpleNamespace
    p = np.zeros(61)
    p[49:51] = .05
    assert held_can_evidence(SimpleNamespace(p=p)) == (False, 0)


def test_physical_skill_failure_keeps_evaluation_actions_valid():
    p = np.zeros(61)
    p[23] = p[48] = 1  # Valid end-effector quaternions.
    raw = {'robot::proprio': p}
    for camera in ('zed', 'left_realsense', 'right_realsense'):
        prefix = f'robot:{camera}_link:Camera:0::'
        raw[prefix+'rgb'] = np.zeros((16, 16, 3), dtype=np.uint8)
        raw[prefix+'depth_linear'] = np.ones((16, 16), dtype=np.float32)
    policy = TrashPolicy(io.StringIO())
    policy.gripper = -1
    def blocked_motion():
        raise SkillFailure('Physical execution blocked')
        yield
    policy.generator = blocked_motion()
    for _ in range(2):
        action = policy.predict(raw)
        assert action.shape == (23,) and np.isfinite(action).all()
        assert action[22] == -1
    assert policy.phase == 'unresolved_motion'


def test_wrist_refinement_accepts_a_silver_can_lid():
    from types import SimpleNamespace
    x, y = np.meshgrid(np.linspace(.70, .77, 20), np.linspace(-.34, -.27, 20))
    cloud = np.stack((x, y, np.full_like(x, .12)), -1)
    # No RGB supplied: semantic identity was established in the preceding head
    # view, and the silver lid is refined from the local observed depth surface.
    d = refine_can(SimpleNamespace(cloud=lambda camera: cloud), [.735, -.305, .07])
    assert d is not None
    np.testing.assert_allclose(d.center[:2], [.735, -.305], atol=.001)


def test_short_or_partial_runs_cannot_claim_goal(tmp_path):
    (tmp_path/'manifest.json').write_text(json.dumps(dict(indices=list(range(20)), instance_ids=list(range(301,321)), diagnostic=True)))
    out = tmp_path/'instance_301/evaluation/json'
    out.mkdir(parents=True)
    (out/'picking_up_trash_301_0.json').write_text(json.dumps(dict(instance_id=301, q_score={'final':1.}, success=True, steps=1)))
    summarize(tmp_path)
    result = json.loads((tmp_path/'summary.json').read_text())
    assert result['completed'] == 1
    assert not result['complete'] and not result['target_achieved']


def test_complete_score_must_be_strictly_above_threshold(tmp_path):
    (tmp_path/'manifest.json').write_text(json.dumps(dict(indices=list(range(20)), instance_ids=list(range(301,321)), diagnostic=False)))
    out = tmp_path/'batch_000/evaluation/json'
    out.mkdir(parents=True)
    for i in range(301,321):
        (out/f'{i}.json').write_text(json.dumps(dict(instance_id=i, q_score={'final': .7}, success=False, steps=1)))
    summarize(tmp_path)
    result = json.loads((tmp_path/'summary.json').read_text())
    assert result['complete'] and not result['target_achieved']
    (out/'301.json').write_text(json.dumps(dict(instance_id=301, q_score={'final':1.}, success=True, steps=1)))
    summarize(tmp_path)
    assert json.loads((tmp_path/'summary.json').read_text())['target_achieved']