desimplex-pusht-eval / plot_episode.py
zengxy0624's picture
plot_episode.py: the usage example points at anchor200_20261004_dpobs
1ffd1e9 verified
Raw History Blame Contribute Delete
2.36 kB
"""Plot one sim episode from its trace.json alone (numpy + matplotlib, no project code).
python plot_episode.py anchor200_20261004_dpobs/switch_traces/seed_10131/trace.json [out.png]
"""
import json
import sys
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle, Polygon
S = 30.0 # T scale (px); local outline, origin = mid-point of the crossbar's outer edge
T_LOCAL = np.array([(-2 * S, 0), (2 * S, 0), (2 * S, S), (S / 2, S),
(S / 2, 4 * S), (-S / 2, 4 * S), (-S / 2, S), (-2 * S, S)])
AGENT_RADIUS = 15.0
COLOR = {'HPA': 'tab:blue', 'HAA': 'tab:orange'}
def t_outline(x, y, angle):
c, s = np.cos(angle), np.sin(angle)
return T_LOCAL @ np.array([[c, s], [-s, c]]) + (x, y)
def controller(step):
# switch logs the active controller in `planner`; single-controller runs leave it empty
return step['planner'] or ('HPA' if step['phase'] == 'HPA-only' else 'HAA')
ep = json.load(open(sys.argv[1]))
scene, trace, summ = ep['scene'], ep['trace'], ep['summary']
fig, ax = plt.subplots(figsize=(6, 6))
for o in scene['obstacles']:
ax.add_patch(Polygon(o['polygon'], color='0.55'))
ax.add_patch(Polygon(t_outline(*scene['goal']), fill=False, ls='--', color='tab:green', label='goal'))
first, last = trace[0]['pre_true']['block'], trace[-1]['post_true']['block']
ax.add_patch(Polygon(t_outline(first['x'], first['y'], first['angle']), color='0.85', label='T start'))
ax.add_patch(Polygon(t_outline(last['x'], last['y'], last['angle']), color='0.3', alpha=0.6, label='T end'))
xy = np.array([[s['pre_true']['agent']['x'], s['pre_true']['agent']['y']] for s in trace]
+ [[trace[-1]['post_true']['agent']['x'], trace[-1]['post_true']['agent']['y']]])
for i, s in enumerate(trace):
ax.plot(xy[i:i + 2, 0], xy[i:i + 2, 1], color=COLOR[controller(s)], lw=1.5)
ax.add_patch(Circle(xy[-1], AGENT_RADIUS, fill=False, color=COLOR[controller(trace[-1])]))
for name, c in COLOR.items():
ax.plot([], [], color=c, label=f'EE ({name})')
ax.set(xlim=(0, 512), ylim=(512, 0), aspect='equal', # image frame: y points down
title=f"seed {summ['seed']} success={summ['success']} hit={summ['obstacle_hit']} "
f"steps={summ['length']}")
ax.legend(loc='lower right', fontsize=8)
fig.tight_layout()
fig.savefig(sys.argv[2]) if len(sys.argv) > 2 else plt.show()