-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplots.py
261 lines (248 loc) · 10.4 KB
/
plots.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
256
257
258
259
260
261
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import arrow
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as colors
from numpy.ma import masked_array
from sklearn.manifold import TSNE
color_set = ["blue", "red", "green", "grey"]
cmap_set = ["Blues", "Reds", "Greens", "Greys"]
def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100):
"""truncate colormap by proportion"""
new_cmap = colors.LinearSegmentedColormap.from_list(
'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval),
cmap(np.linspace(minval, maxval, n)))
return new_cmap
def visualize_2Dspace_LFD(
n_grid, max_H, min_H, p_hat_test, dim,
H_train, Y_train, H_test, Y_test, prefix="test"):
"""
visualize 2D embedding space and corresponding training data points.
"""
n_class = p_hat_test.shape[0]
assert n_grid * n_grid == p_hat_test.shape[1]
n_train_sample = H_train.shape[0]
n_test_sample = H_test.shape[0]
# organize the p_hat as a matrix
p_hat_test = p_hat_test.numpy()
p_hat_show = p_hat_test[dim]
p_hat_mat = p_hat_show.reshape(n_grid, n_grid) # [n_grid, n_grid]
# scale the training data to (0, n_grid)
H_train = H_train.numpy()
H_train = (H_train - min_H) /\
np.repeat(np.expand_dims(max_H - min_H, 0), n_train_sample, axis=0)
H_train = np.nan_to_num(H_train) * n_grid
# scale the testing data to (0, n_grid)
H_test = H_test.numpy()
H_test = (H_test - min_H) /\
np.repeat(np.expand_dims(max_H - min_H, 0), n_test_sample, axis=0)
H_test = np.nan_to_num(H_test) * n_grid
# prepare label set
color_set = ["b", "r", "green"]
Y_train = Y_train.numpy()[0]
Y_set = list(set(Y_train))
Y_set.sort()
# plot the region
fig, ax = plt.subplots(1, 1)
if dim == 0:
cmap = truncate_colormap(cm.get_cmap('Blues'), 0., 0.7)
elif dim == 1:
cmap = truncate_colormap(cm.get_cmap('Reds'), 0., 0.7)
elif dim == 2:
cmap = truncate_colormap(cm.get_cmap('Greens'), 0., 0.7)
implot = ax.imshow(p_hat_mat, vmin=p_hat_mat.min(), vmax=p_hat_mat.max(), cmap=cmap)
for c, y in zip(color_set, Y_set):
Y_train_inds = np.where(Y_train == y)[0]
Y_test_inds = np.where(Y_test == y)[0]
plt.scatter(H_train[Y_train_inds, 1], H_train[Y_train_inds, 0], s=40, c=c, linewidths="1", edgecolors="black")
# plt.scatter(H_test[Y_test_inds, 1], H_test[Y_test_inds, 0], s=2, c=c, alpha=0.3)
plt.axis('off')
plt.savefig("results/%s_lfd_%s.pdf" % (prefix, arrow.now()), bbox_inches='tight')
plt.clf()
def visualize_2Dspace_2class(
n_grid, max_H, min_H, p_hat_test,
H_train, Y_train, H_test, Y_test, prefix="test"):
"""
visualize 2D embedding space and corresponding training data points.
"""
n_class = p_hat_test.shape[0]
assert n_grid * n_grid == p_hat_test.shape[1]
n_train_sample = H_train.shape[0]
n_test_sample = H_test.shape[0]
# organize the p_hat as a matrix
p_hat_test = p_hat_test.numpy()
p_hat_show = p_hat_test[0] / (p_hat_test[0] + p_hat_test[1]) # [n_grid * n_grid]
p_hat_mat = p_hat_show.reshape(n_grid, n_grid) # [n_grid, n_grid]
# scale the training data to (0, n_grid)
H_train = H_train.numpy()
H_train = (H_train - min_H) /\
np.repeat(np.expand_dims(max_H - min_H, 0), n_train_sample, axis=0)
H_train = np.nan_to_num(H_train) * n_grid
# scale the testing data to (0, n_grid)
H_test = H_test.numpy()
H_test = (H_test - min_H) /\
np.repeat(np.expand_dims(max_H - min_H, 0), n_test_sample, axis=0)
H_test = np.nan_to_num(H_test) * n_grid
# prepare label set
color_set = ["b", "r"]
Y_train = Y_train.numpy().flatten()
Y_set = list(set(Y_train))
Y_set.sort()
# plot the region
fig, ax = plt.subplots(1, 1)
cmap = truncate_colormap(cm.get_cmap('RdBu'), 0.3, 0.7)
implot = ax.imshow(p_hat_mat, vmin=p_hat_mat.min(), vmax=p_hat_mat.max(), cmap=cmap)
for c, y in zip(color_set, Y_set):
Y_train_inds = np.where(Y_train == y)[0]
Y_test_inds = np.where(Y_test == y)[0]
plt.scatter(H_train[Y_train_inds, 1], H_train[Y_train_inds, 0], s=30, c=c, linewidths="1", edgecolors="black")
plt.scatter(H_test[Y_test_inds, 1], H_test[Y_test_inds, 0], s=5, c=c, alpha=0.3)
plt.axis('off')
plt.savefig("results/%s_map_%s.pdf" % (prefix, arrow.now()), bbox_inches='tight')
plt.clf()
def visualize_2Dspace_2class_boundary(
n_grid, max_H, min_H, p_hat_test,
H_train, Y_train, H_test, Y_test, prefix="test"):
"""
visualize 2D embedding space and corresponding training data points.
"""
n_class = p_hat_test.shape[0]
assert n_grid * n_grid == p_hat_test.shape[1]
n_train_sample = H_train.shape[0]
n_test_sample = H_test.shape[0]
# organize the p_hat as a matrix
p_hat_test = p_hat_test.numpy()
p_hat_show = p_hat_test[0] / (p_hat_test[0] + p_hat_test[1]) # [n_grid * n_grid]
p_hat_mat = p_hat_show.reshape(n_grid, n_grid) # [n_grid, n_grid]
# select marginal points
p_hat_mat = (1 - abs( p_hat_mat - 0.5 )) # * (p_hat_mat > 0.1) * (p_hat_mat < 0.9)
# scale the training data to (0, n_grid)
H_train = H_train.numpy()
H_train = (H_train - min_H) /\
np.repeat(np.expand_dims(max_H - min_H, 0), n_train_sample, axis=0)
H_train = np.nan_to_num(H_train) * n_grid
# scale the testing data to (0, n_grid)
H_test = H_test.numpy()
H_test = (H_test - min_H) /\
np.repeat(np.expand_dims(max_H - min_H, 0), n_test_sample, axis=0)
H_test = np.nan_to_num(H_test) * n_grid
# prepare label set
color_set = ["b", "r"]
Y_train = Y_train.numpy()[0]
Y_set = list(set(Y_train))
Y_set.sort()
# plot the region
fig, ax = plt.subplots(1, 1)
cmap = truncate_colormap(cm.get_cmap('Greys'), 0., 0.6)
implot = ax.imshow(p_hat_mat, vmin=p_hat_mat.min(), vmax=p_hat_mat.max(), cmap=cmap)
# plot the points
for c, y in zip(color_set, Y_set):
Y_train_inds = np.where(Y_train == y)[0]
Y_test_inds = np.where(Y_test == y)[0]
plt.scatter(H_train[Y_train_inds, 1], H_train[Y_train_inds, 0], s=30, c=c, linewidths="1", edgecolors="black")
plt.scatter(H_test[Y_test_inds, 1], H_test[Y_test_inds, 0], s=5, c=c, alpha=0.3)
# plot the contour
cp = ax.contour(list(range(n_grid)), list(range(n_grid)), p_hat_mat,
levels=[0.6, 0.8, 0.9, 0.95])
ax.clabel(cp, inline=True, fontsize=10)
plt.axis('off')
plt.savefig("results/%s_map_%s.pdf" % (prefix, arrow.now()), bbox_inches='tight')
plt.clf()
def visualize_2Dspace_Nclass(
n_grid, max_H, min_H, p_hat_test,
H_train, Y_train, H_test, Y_test, prefix="test"):
"""
visualize 2D embedding space and corresponding training data points.
"""
n_class = p_hat_test.shape[0]
assert n_grid * n_grid == p_hat_test.shape[1]
n_train_sample = H_train.shape[0]
n_test_sample = H_test.shape[0]
# organize the p_hat as multiple matrices for each class
p_hat_test = p_hat_test.numpy().reshape(n_class, n_grid, n_grid)
p_hat_max = p_hat_test.argmax(0) # [n_grid, n_grid]
p_hat_mats = [] # (n_class [n_grid, n_grid])
for i in range(n_class):
p_hat_show = p_hat_test[i] / p_hat_test.sum(0)
p_hat_mat = masked_array(p_hat_show, p_hat_max != i)
p_hat_mats.append(p_hat_mat)
# scale the training data to (0, n_grid)
H_train = H_train.numpy()
H_train = (H_train - min_H) /\
np.repeat(np.expand_dims(max_H - min_H, 0), n_train_sample, axis=0)
H_train = np.nan_to_num(H_train) * n_grid
# scale the testing data to (0, n_grid)
H_test = H_test.numpy()
H_test = (H_test - min_H) /\
np.repeat(np.expand_dims(max_H - min_H, 0), n_test_sample, axis=0)
H_test = np.nan_to_num(H_test) * n_grid
# prepare label set
Y_train = Y_train.numpy()[0]
Y_set = list(set(Y_train))
Y_set.sort()
# plot the region
fig, ax = plt.subplots(1, 1)
cmaps = [
truncate_colormap(cm.get_cmap(cmap), 0., 0.7)
for cmap in cmap_set[:n_class] ]
implots = [
ax.imshow(p_hat_mats[i], vmin=p_hat_mats[i].min(), vmax=p_hat_mats[i].max(), cmap=cmaps[i])
for i in range(n_class) ]
# plot the points
for c, y in zip(color_set[:n_class], Y_set):
Y_train_inds = np.where(Y_train == y)[0]
Y_test_inds = np.where(Y_test == y)[0]
plt.scatter(H_train[Y_train_inds, 1], H_train[Y_train_inds, 0],
s=30, c=c, linewidths="1", edgecolors="black")
plt.scatter(H_test[Y_test_inds, 1], H_test[Y_test_inds, 0],
s=5, c=c, alpha=0.3)
plt.axis('off')
plt.savefig("results/%s_map_%s.pdf" % (prefix, arrow.now()), bbox_inches='tight')
plt.clf()
def visualize_embedding(H, p_hat, useTSNE=True, perplexity=20):
"""
visualize data embedding on a 2D space using TSNE.
input
- H: [n_sample, n_feature]
- p_hat: [n_class, n_sample]
"""
# configuration
n_class = p_hat.shape[0]
n = H.shape[0]
H = H.numpy()
p_hat = p_hat.numpy()
# check data dimension
assert useTSNE is True or H.shape[1] == 2
# fit TSNE
if useTSNE:
tsne = TSNE(n_components=2, init='random', random_state=0, perplexity=perplexity)
E2D = tsne.fit_transform(H)
else:
E2D = H
# plot
fig, axs = plt.subplots(1, n_class)
# ax
# ax1 = axs[0]
# ax2 = axs[1]
# plot embedding colored by their labels
# cm1 = plt.cm.get_cmap('Reds')
# cm2 = plt.cm.get_cmap('Blues')
cms = [ plt.cm.get_cmap(c) for c in ['Reds', 'Blues', 'Greens'] ]
for i in range(n_class):
axs[i].scatter(E2D[:, 0], E2D[:, 1], c=p_hat[i, :], vmin=p_hat[i, :].min(), vmax=p_hat[i, :].max(), cmap=cms[i])
plt.savefig("results/scatter_%s.pdf" % arrow.now())
plt.clf()
# # plot
# fig, axs = plt.subplots(1, 2)
# cm = plt.cm.get_cmap('RdYlBu')
# ax1 = axs[0]
# ax2 = axs[1]
# # plot embedding colored by their labels
# ax1.scatter(E2D[:int(n/2), 0], E2D[:int(n/2), 1], c="b")
# ax1.scatter(E2D[int(n/2):, 0], E2D[int(n/2):, 1], c="r")
# # plot embedding colored by p_hat
# p_hat = p_hat[0] / (p_hat[0] + p_hat[1])
# ax2.scatter(E2D[:, 0], E2D[:, 1], c=p_hat, vmin=0, vmax=1, cmap=cm)
# plt.savefig("results/scatter_%s.pdf" % arrow.now())