File size: 3,383 Bytes
38dcc02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Materialize selected trajectories, with optional references and assets."""
import json
from pathlib import Path
from urllib.parse import quote

EMBODIMENT = 'franka_sharpa_nonl'

def trial_path(row):
    task = quote(row['reference_id'], safe='')
    trial = f"{row['checkpoint_label']}_seed{row['seed']}_env{row['env']}"
    return Path('processed') / row['source'] / EMBODIMENT / 'right' / task / trial

def supporting_paths(dataset, rows, with_references=True, with_assets=True):
    paths = set()
    for row in rows:
        if with_references:
            prefix = str(Path(row['reference_metadata']).parent) + '/'
            paths.update(p for p in dataset.context if p.startswith(prefix))
            if row.get('original_grasp_path'):
                paths.add(row['original_grasp_path'])
        if with_assets:
            prefix = str(Path(row['visual_mesh']).parent) + '/'
            paths.update(p for p in dataset.context if p.startswith(prefix))
            for key in ('environment_config', 'robot_asset'):
                if row.get(key) in dataset.context:
                    paths.add(row[key])
    return paths

def materialize(dataset, rows, output, with_references=True, with_assets=True):
    root = Path(output); root.mkdir(parents=True, exist_ok=True)
    for path in sorted(supporting_paths(dataset, rows, with_references, with_assets)):
        target = root / path; target.parent.mkdir(parents=True, exist_ok=True)
        target.write_bytes(dataset.read_bytes(path))
    results = []
    for row in rows:
        directory = trial_path(row); dest = root / directory; dest.mkdir(parents=True, exist_ok=True)
        for key, name in [('rollout_path','trajectory.npz'), ('metadata_path','metadata.json')]:
            (dest/name).write_bytes(dataset.read_bytes(row[key], row['episode_archive']))
        if with_references:
            meta = json.loads(dataset.read_bytes(row['reference_metadata']))
            (dest/'reference.npz').write_bytes(dataset.read_bytes(row['icl_context']))
            (dest/'source.json').write_text(json.dumps(meta, ensure_ascii=False, indent=2))
        if with_assets:
            for key in ('visual_mesh', 'object_asset'):
                src = dataset.root / row[key]
                target = root / row[key]
                if src.is_file() and src.resolve() != target.resolve():
                    target.parent.mkdir(parents=True, exist_ok=True); target.write_bytes(src.read_bytes())
        scene = dict(paths_relative_to='release root',
                     robot_urdf='assets/robot/generated/fr3_with_right_sharpa_wave.urdf',
                     object_mesh=row['visual_mesh'], object_usd=row['object_asset'],
                     environment_config=row['environment_config'], state_frame='env_local_world_axes',
                     quaternion_order='wxyz', robot_base_body='base', object_mesh_scale=1.0)
        (dest/'scene.json').write_text(json.dumps(scene, indent=2))
        metrics = {k:row.get(k) for k in ['strict_grasp','success','final_pos_error_m','final_rot_error_rad','duration_s','steps','training_reference']}
        (dest/'metrics.json').write_text(json.dumps(metrics, indent=2))
        results.append(dict(row, processed_path=directory.as_posix()))
    (root/'selected_manifest.jsonl').write_text(''.join(json.dumps(r, ensure_ascii=False)+'\n' for r in results))
    return results