-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrender_utils.py
255 lines (215 loc) · 8.67 KB
/
render_utils.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import sys
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
class StatsVisualizer:
def __init__(self, window, max_length):
plt.rcParams["toolbar"] = "None"
fig = plt.figure(figsize=(5, 5))
if plt.get_backend() == "Qt5Agg":
# Set figure window location to left side
manager = plt.get_current_fig_manager()
_, _, dx, dy = manager.window.geometry().getRect()
manager.window.setGeometry(1100, 100, dx, dy)
fig.set_facecolor("black")
fig.canvas.mpl_connect("close_event", lambda x: sys.exit())
nrow = 11
ncol = 6
gs = gridspec.GridSpec(
nrow,
ncol,
wspace=0.0,
hspace=0.0,
top=1.0 - 0.5 / (nrow + 1),
bottom=0.5 / (nrow + 1),
left=0.5 / (ncol + 1),
right=1 - 0.5 / (ncol + 1),
)
vf_axis = fig.add_subplot(gs[0, :])
vf_axis.set_facecolor("black")
vf_axis.grid(False)
vf_axis.set_xlim(0, window)
vf_axis.set_ylim(1e-2, 500)
vf_axis.spines["bottom"].set_color("#dddddd")
vf_axis.spines["top"].set_color("#dddddd")
vf_axis.spines["right"].set_color("red")
vf_axis.spines["left"].set_color("red")
vf_axis.tick_params(axis="y", colors="#dddddd")
action_axes = [
fig.add_subplot(gs[1, 0:2], yticks=[], yticklabels=[]), # abdomen z
fig.add_subplot(gs[1, 2:4], yticks=[], yticklabels=[]), # abdomen y
fig.add_subplot(gs[1, 4:6], yticks=[], yticklabels=[]), # abdomen x
fig.add_subplot(gs[2, 3:6], yticks=[], yticklabels=[]), # right_hip_x
fig.add_subplot(gs[3, 3:6], yticks=[], yticklabels=[]), # right_hip_z
fig.add_subplot(gs[4, 3:6], yticks=[], yticklabels=[]), # right_hip_y
fig.add_subplot(gs[5, 3:6], yticks=[], yticklabels=[]), # right_knee
fig.add_subplot(gs[6, 3:6], yticks=[], yticklabels=[]), # right_ankle
fig.add_subplot(gs[2, 0:3]), # left_hip_x
fig.add_subplot(gs[3, 0:3], yticks=[], yticklabels=[]), # left_hip_z
fig.add_subplot(gs[4, 0:3]), # left_hip_y
fig.add_subplot(gs[5, 0:3], yticks=[], yticklabels=[]), # left_knee
fig.add_subplot(gs[6, 0:3]), # left_ankle
fig.add_subplot(gs[7, 3:6], yticks=[], yticklabels=[]), # right_shoulder_x
fig.add_subplot(gs[8, 3:6], yticks=[], yticklabels=[]), # right_shoulder_z
fig.add_subplot(gs[9, 3:6], yticks=[], yticklabels=[]), # right_shoulder_y
fig.add_subplot(gs[10, 3:6], yticks=[], yticklabels=[]), # right_elbow
fig.add_subplot(gs[7, 0:3], yticks=[], yticklabels=[]), # left_shoulder_x
fig.add_subplot(gs[8, 0:3]), # left_shoulder_z
fig.add_subplot(gs[9, 0:3], yticks=[], yticklabels=[]), # left_shoulder_y
fig.add_subplot(gs[10, 0:3]), # left_elbow
]
for i, ax in enumerate(action_axes):
ax.set_facecolor("black")
ax.grid(False)
ax.set_xlim(0, window)
ax.set_ylim(-1.2, 1.2)
ax.spines["bottom"].set_color("#dddddd")
ax.spines["top"].set_color("#dddddd")
ax.spines["right"].set_color("red")
ax.spines["left"].set_color("red")
ax.set_xticklabels([])
ax.set_xticks([])
ax.yaxis.label.set_color("red")
ax.xaxis.label.set_color("red")
ax.tick_params(axis="y", colors="#dddddd")
ax.title.set_color("red")
names = [
"hip_x",
"hip_z",
"hip_y",
"knee",
"ankle",
"shoulder_x",
"shoulder_z",
"shoulder_y",
"elbow",
]
right_joints = [action_axes[i] for i in [3, 4, 5, 6, 7, 13, 14, 15, 16]]
for ax, name in zip(right_joints, names):
ax.text(
0.95,
0.1,
name,
color="#dddddd",
horizontalalignment="right",
transform=ax.transAxes,
)
plt.ion()
plt.show()
fig.canvas.draw()
self.background = fig.canvas.copy_from_bbox(fig.bbox)
fake_data = np.zeros(window)
line_style = {
"linestyle": "none",
"marker": "o",
"markersize": "1",
"animated": True,
}
vf_line = vf_axis.plot(fake_data, **line_style)[0]
fps_label = vf_axis.text(
0.1,
0.6,
"00.0",
color="#dddddd",
horizontalalignment="right",
transform=vf_axis.transAxes,
animated=True,
)
reward_label = vf_axis.text(
0.97,
0.6,
"0000.0",
color="#dddddd",
horizontalalignment="right",
transform=vf_axis.transAxes,
animated=True,
)
action_lines = []
for ax in action_axes:
line = ax.plot(fake_data, **line_style)[0]
action_lines.append(line)
cline_style = {
"x": 0,
"ymin": 0,
"ymax": 1,
"color": "orange",
"animated": True,
}
ax = right_joints[3]
num_lines = 5
self.clines = [ax.axvline(**cline_style) for _ in range(num_lines)]
self.clines_x = (window + 1) * np.ones(num_lines)
self.clines_active = np.zeros(num_lines)
for cl in self.clines:
cl.set_xdata(window + 1)
self.fig = fig
self.lines = [vf_line] + action_lines
self.fps_label = fps_label
self.reward_label = reward_label
self.step = 0
self.window = window
self.batch = 1
self.data = -2 * np.ones((len(self.lines), max_length))
def update_plot(self, value, action, tot_reward, done, contact, fps):
self.data[0, self.step] = value
self.data[1:, self.step] = action
self.fps_label.set_text("{:02.1f}".format(fps))
self.reward_label.set_text("{:04.1f}".format(tot_reward))
self.fig.canvas.restore_region(self.background)
self.fps_label.axes.draw_artist(self.fps_label)
self.reward_label.axes.draw_artist(self.reward_label)
# Draw new data
start = self.step % self.batch
begin = 0 if self.step < self.window else self.step - 100
end = begin + 100
# This whole section is pretty ugly...
if self.step >= self.window:
if contact:
# Find a new line to move if contact
active_x = self.clines_x[self.clines_active == 1]
max_active_x = 0 if active_x.size == 0 else active_x.max()
found = False
for i, x in enumerate(self.clines_x):
if x <= 0:
self.clines_active[i] = 0
self.clines_x[i] = self.window + 1
self.clines[i].set_xdata(self.clines_x[i])
if x - max_active_x > 5 and not found:
found = True
self.clines_active[i] = 1
# Move every active line left 1
for i, active in enumerate(self.clines_active):
if not active:
continue
self.clines_x[i] -= 1
self.clines[i].set_xdata(self.clines_x[i])
self.clines[i].axes.draw_artist(self.clines[i])
else:
if contact:
# Find a new line to move if contact
found = False
for i, x in enumerate(self.clines_x):
if not self.clines_active[i] and not found:
found = True
self.clines_active[i] = 1
self.clines_x[i] = self.step
self.clines[i].set_xdata(self.clines_x[i])
# Still need to redraw because blit
for i, active in enumerate(self.clines_active):
if not active:
continue
self.clines[i].axes.draw_artist(self.clines[i])
for (line, data) in zip(
self.lines[start :: self.batch], self.data[start :: self.batch]
):
line.set_ydata(data[begin:end])
line.axes.draw_artist(line)
self.fig.canvas.blit(line.axes.bbox)
self.fig.canvas.flush_events()
self.step += 1
if done:
self.step = 0
self.reward = 0
self.data.fill(-2)
self.clines_active.fill(0)
self.clines_x.fill(self.window + 1)