Datasets:
Download source/src/simready/generation/runtime/self_check_mujoco.py from AndyZ2001/mingru-mojoco-runs: direct link, hf CLI and curl.
- Browser
- Download file 3.51 kB
-
https://huggingface.co/datasets/AndyZ2001/mingru-mojoco-runs/resolve/main/source/src/simready/generation/runtime/self_check_mujoco.py
- Command line
-
hf download hf://datasets/AndyZ2001/mingru-mojoco-runs/source/src/simready/generation/runtime/self_check_mujoco.py
-
curl -L -o self_check_mujoco.py https://huggingface.co/datasets/AndyZ2001/mingru-mojoco-runs/resolve/main/source/src/simready/generation/runtime/self_check_mujoco.py
3.51 kB
| """Public self-check for a MuJoCo submission. Uses only the submission. | |
| Usage: python self_check.py /work/output | |
| Reports manifest errors (missing labels, references), contacts deeper than 5 mm at the authored | |
| state, and the time above 5 mm, peak body speed and finite state over a 3 s passive rollout. | |
| The limits are the prompt's construction rules. | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import mujoco | |
| import numpy as np | |
| LIMIT_M, LIMIT_S, SPEED = .005, .02, 5. | |
| def body(model, geom): | |
| return mujoco.mj_id2name(model, mujoco.mjtObj.mjOBJ_BODY, int(model.geom_bodyid[geom])) or f'body{int(model.geom_bodyid[geom])}' | |
| def main(output): | |
| output = Path(output) | |
| report = {'manifest_errors': []} | |
| model = mujoco.MjModel.from_xml_path(str(output / 'scene.xml')) | |
| data = mujoco.MjData(model) | |
| manifest = json.loads((output / 'objects.json').read_text())['objects'] | |
| kinds = {'body_names': mujoco.mjtObj.mjOBJ_BODY, 'geom_names': mujoco.mjtObj.mjOBJ_GEOM, | |
| 'joint_names': mujoco.mjtObj.mjOBJ_JOINT, 'flex_names': mujoco.mjtObj.mjOBJ_FLEX} | |
| ids = [o.get('id') for o in manifest] | |
| if len(ids) != len(set(ids)): | |
| report['manifest_errors'].append('duplicate object ids') | |
| for obj in manifest: | |
| if not isinstance(obj.get('label'), str) or not obj['label'].strip(): | |
| report['manifest_errors'].append(f"{obj.get('id')}: no label (say plainly what the object is)") | |
| for key, kind in kinds.items(): | |
| for name in obj.get(key, []): | |
| if mujoco.mj_name2id(model, kind, name) < 0: | |
| report['manifest_errors'].append(f"{obj.get('id')}: {key} '{name}' does not resolve") | |
| sites = obj.get('sites', {}) | |
| for name in (sites.values() if isinstance(sites, dict) else sites): | |
| if mujoco.mj_name2id(model, mujoco.mjtObj.mjOBJ_SITE, name) < 0: | |
| report['manifest_errors'].append(f"{obj.get('id')}: site '{name}' does not resolve") | |
| mujoco.mj_forward(model, data) | |
| initial = sorted(((-float(c.dist), body(model, c.geom1), body(model, c.geom2)) for c in data.contact[:data.ncon] | |
| if -c.dist > LIMIT_M), reverse=True) | |
| report['initial_contacts_deeper_than_5mm'] = [{'depth_m': d, 'bodies': [a, b]} for d, a, b in initial[:20]] | |
| report['initial_contact_count_deeper_than_5mm'] = len(initial) | |
| above, peak, finite = 0., 0., True | |
| for _ in range(int(np.ceil(3/model.opt.timestep))): | |
| mujoco.mj_step(model, data) | |
| deepest = max([-float(c.dist) for c in data.contact[:data.ncon]] or [0.]) | |
| above += model.opt.timestep if deepest > LIMIT_M else 0. | |
| peak = max(peak, float(np.linalg.norm(data.cvel[:, 3:], axis=1).max()) if data.cvel.size else 0.) | |
| finite = bool(np.isfinite(data.qpos).all() and np.isfinite(data.qvel).all()) | |
| if not finite: | |
| break | |
| report.update(time_above_5mm_s=above, peak_body_speed_mps=peak, finite=finite, | |
| timestep_s=float(model.opt.timestep), | |
| passive_ok=bool(finite and above <= LIMIT_S and peak <= SPEED and not report['manifest_errors'])) | |
| print(json.dumps(report, indent=2)) | |
| print('\nPASSIVE CHECK', 'PASSED' if report['passive_ok'] else 'FAILED', | |
| f"- {len(initial)} initial contacts > 5 mm, {above:.3f} s above 5 mm, peak speed {peak:.2f} m/s," | |
| f" {len(report['manifest_errors'])} manifest errors") | |
| if __name__ == '__main__': | |
| main(sys.argv[1] if len(sys.argv) > 1 else '/work/output') | |