-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizations.py
248 lines (190 loc) · 6.32 KB
/
visualizations.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
# -*- coding: utf-8 -*-
"""
@author: Amin
"""
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import numpy as np
import scipy as sp
from scipy import stats
import matplotlib
from matplotlib.patches import Ellipse
import matplotlib.transforms as transforms
from matplotlib.axes._axes import _log as matplotlib_axes_logger
matplotlib_axes_logger.setLevel('ERROR')
# %%
def visualize_pc(
means,covs,pc=None,title_str='',fontsize=9,dotsize=30,K=None,linewidth=2,std=3,lim=None,save=False,file=None
):
'''Visualize point clouds and atlases learned from them
'''
if means.shape[2] > 2:
pca = PCA(n_components=2)
pca.fit_transform(np.vstack(means))
if pc is not None: pc = pca.transform(pc)
means = [pca.transform(means[i]) for i in range(len(means))]
covs = [pca.components_@covs[i]@pca.components_.T for i in range(len(means))]
fig = plt.figure(figsize=(15,8))
plt.title(title_str,fontsize=fontsize)
colors = np.vstack((
np.zeros((2,len(means))),
np.linspace(0,1,len(means))
)).T
if K is not None:
for i in range(len(K)):
for j in range(i+1,len(K)):
if K[i,j] > 0:
plt.plot(
[np.mean(means,1)[i][0],np.mean(means,1)[j][0]],
[np.mean(means,1)[i][1],np.mean(means,1)[j][1]],
lw=linewidth,linestyle='dashed',color='k'
)
else:
plt.plot(
np.mean(means,1)[:,0],np.mean(means,1)[:,1],'k--',
linewidth=linewidth
)
for j in range(len(means)):
plt.scatter(
means[j][:,0],means[j][:,1],
s=dotsize, c=colors[j], marker='.'
)
draw_ellipse(
means[j].mean(0)[:2],covs[j][:2,:2],colors[j],ax=plt.gca(),
std_devs=std,linewidth=linewidth
)
if pc is not None:
plt.scatter(pc[:,0],pc[:,1],s=.5)
plt.axis('equal')
plt.axis('off')
plt.xticks(fontsize=fontsize)
plt.yticks(fontsize=fontsize)
if lim is not None:
plt.xlim(lim)
plt.ylim(lim)
if save:
plt.savefig(file+'.png',format='png')
plt.savefig(file+'.pdf',format='pdf')
plt.close('all')
else:
plt.show()
# %%
def plot_loss(
loss,error=None,xlabel='',ylabel='',titlestr='',colors=None,legends=None,fontsize=15,linewidth=2,save=False,file=None
):
if colors is None: colors = plt.cm.hsv(np.linspace(0,1,len(legends)+1)[0:-1])[:,0:3]
plt.figure(figsize=(10,3))
for i in range(len(loss)):
plt.plot(loss[i],color=colors[i],linewidth=linewidth)
plt.yscale('log')
if error is not None:
plt.fill_between(np.arange(len(loss[i])), loss[i]-error[i], loss[i]+error[i], color=colors[i], alpha=.1)
plt.grid('on')
plt.xlabel(xlabel,fontsize=fontsize)
plt.ylabel(ylabel,fontsize=fontsize)
plt.xticks(fontsize=fontsize)
plt.yticks(fontsize=fontsize)
if legends is not None:
plt.legend(legends,fontsize=fontsize)
plt.title(titlestr,fontsize=fontsize)
if save:
plt.savefig(file+'.png',format='png')
plt.savefig(file+'.pdf',format='pdf')
plt.close('all')
else:
plt.show()
# %%
def plot_box(
performance,titlestr='',fontsize=10,save=False,file=None
):
plt.boxplot(performance.values())
plt.xticks(np.arange(1,1+len(performance)),list(performance.keys()),fontsize=fontsize)
plt.yticks(fontsize=fontsize)
plt.title(titlestr,fontsize=fontsize)
if save:
plt.savefig(file+'.png',format='png')
plt.savefig(file+'.pdf',format='pdf')
plt.close('all')
else:
plt.show()
# %%
def plot_tuning(
x,y,lw=2,titlestr='',fontsize=10,save=False,file=None
):
plt.plot(x,y,lw=lw)
plt.xticks(fontsize=fontsize)
plt.yticks(fontsize=fontsize)
plt.title(titlestr,fontsize=fontsize)
if save:
plt.savefig(file+'.png',format='png')
plt.savefig(file+'.pdf',format='pdf')
plt.close('all')
else:
plt.show()
# %%
def draw_ellipse(
mu, cov, colors, ax, std_devs=3.0,
facecolor='none', linewidth=1, **kwargs
):
pearson = cov[0, 1]/np.sqrt(cov[0, 0] * cov[1, 1])
ell_radius_x = np.sqrt(1 + pearson)
ell_radius_y = np.sqrt(1 - pearson)
ellipse = Ellipse(
(0, 0), width=ell_radius_x * 2, height=ell_radius_y * 2,
facecolor=facecolor, edgecolor=colors, linewidth=linewidth
)
scale_x = np.sqrt(cov[0, 0]) * std_devs
scale_y = np.sqrt(cov[1, 1]) * std_devs
transf = transforms.Affine2D() \
.rotate_deg(45) \
.scale(scale_x, scale_y) \
.translate(mu[0], mu[1])
ellipse.set_transform(transf + ax.transData)
return ax.add_patch(ellipse)
# %%
def visualize_covariances(
cov,titlestr='',fontsize=10,save=False,file=None
):
fig, axes = plt.subplots(
nrows=cov.shape[0], ncols=cov.shape[1],
figsize=(3*cov.shape[1], 3*cov.shape[0])
)
if cov.shape[0] == 1: axes = axes[None]
if cov.shape[1] == 1: axes = axes[:,None]
vmin = cov.min()
vmax = cov.max()
for i in range(cov.shape[0]):
for j in range(cov.shape[1]):
im = axes[i,j].imshow(cov[i,j], vmin=vmin, vmax=vmax)
plt.xticks(fontsize=fontsize)
plt.yticks(fontsize=fontsize)
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.9, 0.1, 0.05, 0.8])
fig.colorbar(im, cax=cbar_ax)
plt.suptitle(titlestr,fontsize=fontsize)
if save:
plt.savefig(file+'.png',format='png')
plt.savefig(file+'.pdf',format='pdf')
plt.close('all')
else:
plt.show()
# %%
def plot_torus(phi,theta,R0=2,a=.3,save=False,file=None):
R0, a = 2., .3
zlim = max(R0,a)+1
# torus parametrization
x_ = (R0 + a*np.cos(theta)) * np.cos(phi)
y_ = (R0 + a*np.cos(theta)) * np.sin(phi)
z_ = a * np.sin(theta)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x_,y_,z_,s=10)
ax.set_zlim(-zlim,zlim)
ax.axis('off')
ax.view_init(elev=30, azim=20)
if save:
plt.savefig(file+'.png',format='png')
plt.savefig(file+'.pdf',format='pdf')
plt.close('all')
else:
plt.show()