-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_sql.py
179 lines (160 loc) · 5.02 KB
/
plot_sql.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
import glob
import matplotlib.pyplot as plt
import brewer2mpl
import math
import numpy as np
from time import sleep
from event_buffer import EventBufferSQLProxy
from scipy.signal import savgol_filter
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.preprocessing import StandardScaler
import imageio
import os
import matplotlib.lines as lines
fontsize = 14
skip_main_plot = False
skip_event_plot = False
single = False
single_title = "deathmatch"
plt.style.use('ggplot')
# brewer2mpl.get_map args: set name set type number of colors
bmap = brewer2mpl.get_map('Set2', 'qualitative', 7)
colors = bmap.mpl_colors
num_events = 26
num_agents = 1
exp_id = 111
pca = False
names = [
'movement',
'health',
'armour',
'shots',
'ammo',
'weapon 0 pickup',
'weapon 1 pickup',
'weapon 2 pickup',
'weapon 3 pickup',
'weapon 4 pickup',
'weapon 5 pickup',
'weapon 6 pickup',
'weapon 7 pickup',
'weapon 8 pickup',
'weapon 9 pickup',
'kill',
'weapon 0 kill',
'weapon 1 kill',
'weapon 2 kill',
'weapon 3 kill',
'weapon 4 kill',
'weapon 5 kill',
'weapon 6 kill',
'weapon 7 kill',
'weapon 8 kill',
'weapon 9 kill'
]
data = []
for i in range(num_agents):
buffer = EventBufferSQLProxy(num_events, 100000000, exp_id, i+1)
events = buffer.get_own_events()
data.append(events)
print(data)
for i in range(num_events):
if i >= len(names):
break
name = names[i].title()
plt.title(name)
cols = math.floor(math.sqrt(num_events))
rows = math.ceil(math.sqrt(num_events))
for a in range(num_agents):
x = []
y = []
for e in data[a]:
x.append(e[0])
y.append(e[i+1])
yhat = savgol_filter(y, 5001, 3) # window size 5001, polynomial order 3
plt.plot(x, yhat, color=colors[a])
plt.savefig(f'plots/events_{exp_id}_{name}')
plt.clf()
smooth_over = 1000
smoothed_agent_episodic_events = []
for a in range(num_agents):
episodic_events = []
for e in data[a]:
episodic_events.append(e)
smoothed_episodic_events = []
smoothed = []
for i in range(len(episodic_events)):
smoothed.append(episodic_events[i])
if len(smoothed) >= smooth_over:
smoothed_episodic_events.append(np.mean(smoothed, axis=0))
smoothed.clear()
smoothed_agent_episodic_events.append(smoothed_episodic_events)
def plot_pca(data, points, step, max_step, pca=True):
print(step, " ", max_step)
fig, plot = plt.subplots()
fig.set_size_inches(4, 4)
plt.prism()
#x_len = x_max - x_min
#y_len = y_max - y_min
#plt.plot([x_min + x_len*0.1, y_min + y_len*0.1], [x_min + x_len*0.8*(step / max_step), y_min + y_len*0.1])
plt.plot(data[:, 0], data[:, 1], 'o', markerfacecolor='grey', markersize=1, fillstyle='full', markeredgewidth=0.0)
#colors = ['red', 'blue', 'green', 'purple', 'orange', 'teal', 'black', 'grey']
for i in range(len(points)):
plt.plot(points[i][0], points[i][1], 'o', markerfacecolor=colors[i], markersize=6, fillstyle='full', markeredgewidth=0.0)
plot.set_xticks(())
plot.set_yticks(())
plt.title(str(int(step)))
plt.tight_layout(pad=-0.5, w_pad=-0.5, h_pad=-0.5)
#fig.savefig("plots/{}.pdf".format("pca" if pca else "t-sne"), bbox_inches='tight', pad_inches=0)
fig.savefig("plots/pca/{}_step_{}.png".format("pca" if pca else "t-sne", step), bbox_inches='tight', pad_inches=0)
return fig
y_all = []
xx = []
for a in range(num_agents):
x = []
for i in range(len(smoothed_agent_episodic_events[a])):
events = smoothed_agent_episodic_events[a][i]
x.append(events[0])
y_all.append(events[1:])
xx.append(x)
# Standardize
print(y_all)
y_all = StandardScaler().fit_transform(y_all)
yy = []
idx=0
for a in range(num_agents):
y = []
for i in range(len(xx[a])):
y.append(y_all[idx])
idx += 1
yy.append(y)
if pca:
transformed = PCA(n_components=2).fit_transform(y_all)
else:
transformed = TSNE(n_components=2).fit_transform(y_all)
for i in range(len(xx[0])):
step = xx[0][i]
points = []
for a in range(num_agents):
point = yy[a][i]
for i in range(len(y_all)):
y = y_all[i]
if np.array_equal(y, point):
points.append(transformed[i])
break
assert len(points) == num_agents
x_min, x_max, y_min, y_max = np.min(y_all[:, 0]), np.max(y_all[:, 0]), np.max(y_all[:, 1]), np.max(y_all[:, 1])
plot_pca(transformed, points, step=step, max_step=xx[0][-1], pca=pca)
images = []
filenames = glob.glob(f'plots/pca/{"pca" if pca else "t-sne"}_*.png')
d = {}
for filename in filenames:
step = float(filename.split("_")[-1].split(".png")[0])
d[step] = filename
for step in sorted(d.keys()):
filename = d[step]
images.append(imageio.imread(filename))
os.remove(filename)
imageio.mimsave(f'plots/pca/{"pca" if pca else "t-sne"}_{smooth_over}_{exp_id}.gif', images)
# transformed_tsne = TSNE(n_components=2).fit_transform(y_all)