-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
255 lines (197 loc) · 7.23 KB
/
draw.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 matplotlib.pyplot as plt
import numpy as np
from matplotlib import gridspec
from matplotlib.patches import Ellipse
from matplotlib.patches import Polygon
from mpl_toolkits.mplot3d import axes3d, Axes3D
import pandas as pd
def draw2d(df, selected=[0, 1], constraints=None, title=None):
fig = plt.figure(figsize=(8, 12))
ax = fig.add_subplot(111)
draw2dset(ax=ax, df=df, selected=selected, constraints=constraints, title=title)
plt.show()
def draw2dset(df, ax=None, selected=[0, 1], constraints=None, title=None):
if ax is None:
fig = plt.figure(figsize=(8, 12))
ax = fig.add_subplot(111)
ax.set_aspect('equal', 'box')
red = (1.0, 0.0, 0.0, 0.1)
green = (0.0, 1.0, 0.0, 0.5)
blue = (0.0, 0.0, 1.0, 0.5)
if 'valid' in df:
colors = [green if v == True else red for v in df['valid'].values]
sizes = [1 if v == True else 0.5 for v in df['valid'].values]
else:
colors = blue
sizes = 0.5
x_1 = df.columns[selected[0]]
x_2 = df.columns[selected[1]]
ax.scatter(x=df[x_1].values, y=df[x_2].values, c=colors, s=sizes)
ax.set_xlabel(x_1)
ax.set_ylabel(x_2)
if title is not None:
ax.set_title(title)
# plt.show()
def draw3dset(df, ax=None, selected=[0, 1, 2], constraints=None, title=None):
if ax is None:
fig = plt.figure(figsize=(8, 12))
ax = fig.add_subplot(111)
ax.set_aspect('equal', 'box')
red = (1.0, 0.0, 0.0, 0.1)
green = (0.0, 1.0, 0.0, 0.5)
blue = (0.0, 0.0, 1.0, 0.5)
if 'valid' in df:
colors = [green if v == True else red for v in df['valid'].values]
sizes = [1 if v == True else 0.5 for v in df['valid'].values]
else:
colors = blue
sizes = 0.5
x_1 = df.columns[selected[0]]
x_2 = df.columns[selected[1]]
x_3 = df.columns[selected[2]]
ax.scatter(xs=df[x_1].values, ys=df[x_2].values, zs=df[x_3].values, c=colors, s=sizes)
ax.set_xlabel(x_1)
ax.set_ylabel(x_2)
ax.set_zlabel(x_3)
if title is not None:
ax.set_title(title)
def draw2dmodel(train, df, model, constraints=None, title=None):
fig = plt.figure(figsize=(8, 12))
gs1 = gridspec.GridSpec(nrows=3, ncols=2)
valid_ax = fig.add_subplot(gs1[:-1, :])
train_ax = fig.add_subplot(gs1[-1, :-1])
w_ax = fig.add_subplot(gs1[-1, -1])
# validation set
draw2dset(ax=valid_ax, df=df, title=title)
# train set
train_title = 'Train set ({}/{})'.format(train['valid'].sum(),
train['valid'].count()) if 'valid' in train else 'Train set'
draw2dset(ax=train_ax, df=train, title=train_title)
if model == 'cube':
draw_cube(ax=train_ax)
elif model == 'ball':
draw_ellipse(train_ax, width=5.4, height=5.4)
elif model == 'simplex':
draw_simplex(train_ax)
train_ax.autoscale()
# constraints
draw_constraints(w_ax, constraints=constraints, title='w (n={})'.format(len(constraints)))
plt.show()
def draw3dmodel(train, df, model, constraints=None, title=None):
fig = plt.figure(figsize=(8, 12))
gs1 = gridspec.GridSpec(nrows=3, ncols=2)
valid_ax = fig.add_subplot(gs1[:-1, :], projection='3d')
train_ax = fig.add_subplot(gs1[-1, :-1], projection='3d')
w_ax = fig.add_subplot(gs1[-1, -1], projection='3d')
# validation set
draw3dset(ax=valid_ax, df=df, title=title)
# train set
train_title = 'Train set ({}/{})'.format(train['valid'].sum(), train['valid'].count()) if 'valid' in train else 'Train set'
draw3dset(ax=train_ax, df=train, title=train_title)
# constraints
draw_constraints(w_ax, constraints=constraints, title='w (n={})'.format(len(constraints)))
plt.show()
def draw3d(df, selected=[0, 1, 2], title=None):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
draw3dset(ax=ax, df=df, title='Model')
x_0 = df.columns[selected[0]]
x_1 = df.columns[selected[1]]
x_2 = df.columns[selected[2]]
ax.set_xlabel(x_0)
ax.set_ylabel(x_1)
ax.set_zlabel(x_2)
if title is not None:
ax.set_title(title)
plt.show()
def draw_cube(ax):
bounds = [[1, 3.7], [2, 7.4]]
x1 = bounds[0]
x2 = bounds[1]
points = [(x1[0], x2[0]), (x1[1], x2[0]), (x1[1], x2[1]), (x1[0], x2[1])]
rectangle = Polygon(points, closed=True, fill=None, edgecolor='blue')
ax.add_patch(rectangle)
def draw_constraints(ax, constraints, title=None):
draw_ellipse3d(ax)
if constraints is None:
return
for constraint in constraints:
normalized = np.linalg.norm(constraint)
x0 = constraint[0] / normalized
x1 = constraint[1] / normalized
if len(constraint) == 3:
x2 = constraint[2] / normalized
ax.plot([0, x0], [0, x1], [0, x2], 'k-')
ax.scatter(xs=x0, ys=x1, zs=x2, color=(1.0, 0.0, 0.0, 0.7))
else:
ax.scatter(x=x0, y=x1, color=(1.0, 0.0, 0.0, 0.7))
ax.plot([0, x0], [0, x1], 'k-')
ax.set_xlabel('x_0')
ax.set_ylabel('x_1')
if hasattr(ax, 'set_zlabel'):
ax.set_zlabel('x_2')
if title is not None:
ax.set_title(title)
def draw_ellipse(ax, xy=(1, 2), width: float=1, height: float=1):
ellipse = Ellipse(xy=xy, width=width, height=height, fill=False, edgecolor='blue')
ax.add_patch(ellipse)
def draw_simplex(ax):
x1 = 2.55
x2 = 0.18
points = [[0, 0], [x1, x2], [x2, x1]]
triangle = Polygon(points, closed=True, fill=None, edgecolor='blue')
ax.add_patch(triangle)
def draw_ellipse3d(ax):
# Make data
count = 60
u = np.linspace(0, 2 * np.pi, count)
v = np.linspace(0, np.pi, count)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))
# Plot the surface
if hasattr(ax, 'plot_wireframe'):
ax.plot_wireframe(x, y, z, color=(0.0, 0.0, 1.0, 0.1))
else:
ellipse = Ellipse(xy=(0, 0), width=2, height=2, fill=False, edgecolor=(0.0, 0.0, 1.0, 0.1))
ax.add_patch(ellipse)
def draw_df(filename: str):
df = pd.read_csv(filename)
n = df.shape[1]
if n > 3 or n < 2:
print("Cannot draw.")
return
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d') if n == 3 else fig.add_subplot(111)
red = (1.0, 0.0, 0.0, 0.1)
green = (0.0, 1.0, 0.0, 0.5)
blue = (0.0, 0.0, 1.0, 0.5)
colors = green
sizes = 1
if n == 2:
draw2d_df(df, ax, colors, sizes)
elif n == 3:
draw3d_df(df, ax, colors, sizes)
title = filename.split('/')[-1].split('_')
title = "{name} [k={k}, n={n}]".format(name=title[1], k=title[2], n=title[3])
ax.set_title(title)
plt.show()
def draw2d_df(df, ax, colors, sizes):
x_1 = df.columns[0]
x_2 = df.columns[1]
ax.scatter(x=df[x_1].values, y=df[x_2].values, c=colors, s=sizes)
ax.set_xlabel(x_1)
ax.set_ylabel(x_2)
def draw3d_df(df, ax, colors, sizes):
x_1 = df.columns[0]
x_2 = df.columns[1]
x_3 = df.columns[2]
ax.scatter(xs=df[x_1].values, ys=df[x_2].values, zs=df[x_3].values, c=colors, s=sizes)
ax.set_xlabel(x_1)
ax.set_ylabel(x_2)
ax.set_zlabel(x_3)
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
# draw_ellipse3d(ax)
# plt.ion()
# plt.show()