-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPlot.py
268 lines (212 loc) · 9.65 KB
/
Plot.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
262
263
264
265
import numpy as np
import matplotlib.pyplot as plt
import csv
import time
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
# Function for converting time to formatted string
def convert_time(t):
minutes = np.floor((t/3600.0) * 60)
seconds = np.ceil(((t/3600.0) * 60 - minutes) * 60)
if (minutes >= 1):
minutes = np.floor(t/60.0)
seconds = np.ceil((t/60.0 - minutes) * 60)
t_str = str(int(minutes)).rjust(2) + 'm ' + \
str(int(seconds)).rjust(2) + 's'
else:
seconds = (t/60.0 - minutes) * 60
t_str = str(seconds) + 's'
return t_str
# Define function for removing axes from MatPlotLib plots
def remove_axes(ax):
# make the panes transparent
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# make the grid lines transparent
ax.xaxis._axinfo["grid"]['color'] = (1,1,1,0)
ax.yaxis._axinfo["grid"]['color'] = (1,1,1,0)
ax.zaxis._axinfo["grid"]['color'] = (1,1,1,0)
# remove axes
ax._axis3don = False
# Evaluate SciKit Learn Gaussian Process Regressor and Plot Results
def main():
# First determine the dimension of the input values
filename = "predictions.csv"
with open(filename, "r") as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='|')
row = next(csvreader)
nonInputLength = 3
inputDim = len(row) - nonInputLength
# Get prediction data
filename = "predictions.csv"
inVals = []; trueVals = []; predMean = []; predStd = []
with open(filename, "r") as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in csvreader:
if inputDim == 2:
i1, i2, t, m, v = row
inVals.append([i1,i2])
else:
i, t, m, v = row
inVals.append(i)
trueVals.append(t)
predMean.append(m)
predStd.append(v)
inVals = np.array(inVals).astype(np.float32)
trueVals = np.array(trueVals).astype(np.float32)
predMean = np.array(predMean).astype(np.float32)
predStd = np.array(predStd).astype(np.float32)
## Get observation data
filename = "observations.csv"
obsX = []; obsY = []
with open(filename, "r") as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in csvreader:
if inputDim == 2:
x1, x2, y = row
obsX.append([x1,x2])
else:
x, y = row
obsX.append(x)
obsY.append(y)
obsX = np.array(obsX).astype(np.float32)
obsY = np.array(obsY).astype(np.float32)
if inputDim == 1:
# Get posterior samples
filename = "samples.csv"
samples = []
with open(filename, "r") as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in csvreader:
vals = np.array(row)
samples.append(vals)
samples = np.array(samples).astype(np.float32)
X = np.reshape(obsX, [-1, inputDim])
Y = np.reshape(obsY, [-1])
Xtest = np.reshape(inVals, [-1, inputDim])
### ###
### PLOT RESULTS ###
### ###
if inputDim == 1:
""" ONE-DIMENSIONAL PLOTS """
### C++ IMPLEMENTATION
plt.figure()
plt.plot(inVals, predMean, 'C0', linewidth=2.0)
alpha = 0.075
for k in [1,2,3]:
plt.fill_between(inVals, predMean-k*predStd, predMean+k*predStd, where=1>=0, facecolor="C0", alpha=alpha, interpolate=True)
plt.plot(inVals, trueVals, 'C1', linewidth=1.0, linestyle="dashed")
alpha_scatter = 0.5
plt.scatter(obsX, obsY, alpha=alpha_scatter)
for i in range(0,samples.shape[0]):
plt.plot(inVals, samples[i,:], 'C0', alpha=0.2, linewidth=1.0, linestyle="dashed")
plt.suptitle("C++ Implementation")
plt.show()
elif inputDim == 2:
""" TWO-DIMENSIONAL PLOTS """
# Flatten input values for compatibility with MatPlotLib's tri_surf
plot_X_flat = []; plot_Y_flat = []
R = inVals.shape[0]
for n in range(0,R):
plot_X_flat.append(inVals[n,0])
plot_Y_flat.append(inVals[n,1])
tri_fig = plt.figure()
tri_ax1 = tri_fig.add_subplot(111, projection='3d')
linewidth = 0.1
cmap = "Blues"
# Plot CppGPs results
tri_ax1.plot_trisurf(plot_X_flat,plot_Y_flat, predMean, cmap=cmap, linewidth=linewidth, antialiased=True)
pred_title = "CppGPs"
tri_ax1.set_title(pred_title, fontsize=24)
# Remove axes from plots
remove_axes(tri_ax1)
# Bind axes for comparison
def tri_on_move(event):
if event.inaxes == tri_ax1:
if tri_ax1.button_pressed in tri_ax1._rotate_btn:
tri_ax2.view_init(elev=tri_ax1.elev, azim=tri_ax1.azim)
elif tri_ax1.button_pressed in tri_ax1._zoom_btn:
tri_ax2.set_xlim3d(tri_ax1.get_xlim3d())
tri_ax2.set_ylim3d(tri_ax1.get_ylim3d())
tri_ax2.set_zlim3d(tri_ax1.get_zlim3d())
elif event.inaxes == tri_ax2:
if tri_ax2.button_pressed in tri_ax2._rotate_btn:
tri_ax1.view_init(elev=tri_ax2.elev, azim=tri_ax2.azim)
elif tri_ax2.button_pressed in tri_ax2._zoom_btn:
tri_ax1.set_xlim3d(tri_ax2.get_xlim3d())
tri_ax1.set_ylim3d(tri_ax2.get_ylim3d())
tri_ax1.set_zlim3d(tri_ax2.get_zlim3d())
else:
return
tri_fig.canvas.draw_idle()
tri_c1 = tri_fig.canvas.mpl_connect('motion_notify_event', tri_on_move)
""" Zoom in to view predictive uncertainty """
plot_radius = 0.5
plot_x_min = -0.125
plot_y_min = -0.125
plot_x_max = 0.25
# Define conditions for including values associated with an input point (x,y)
def include_conditions(x,y,delta=0.0):
rad = np.sqrt(np.power(x,2) + np.power(y,2))
return (x-delta<=plot_x_max) and (x+delta>=plot_x_min) and (y+delta>=plot_y_min) and (rad-delta<=plot_radius)
# Restrict plots to values corresponding to valid input points
R = inVals.shape[0]
plot_X_zoom = []; plot_Y_zoom = []; predMean_zoom = []
predMean_plus_std = []; predMean_minus_std = []
predMean_plus_std2 = []; predMean_minus_std2 = []
predMean_plus_std3 = []; predMean_minus_std3 = []
trueVals_zoom = []
for n in range(0,R):
x = plot_X_flat[n]
y = plot_Y_flat[n]
if include_conditions(x,y,delta=0.025):
plot_X_zoom.append(x)
plot_Y_zoom.append(y)
predMean_zoom.append(predMean[n])
predMean_plus_std.append( predMean[n] + 1 * predStd[n] )
predMean_minus_std.append( predMean[n] - 1 * predStd[n] )
predMean_plus_std2.append( predMean[n] + 2 * predStd[n] )
predMean_minus_std2.append( predMean[n] - 2 * predStd[n] )
predMean_plus_std3.append( predMean[n] + 3 * predStd[n] )
predMean_minus_std3.append( predMean[n] - 3 * predStd[n] )
trueVals_zoom.append(trueVals[n])
# Restrict observations to valid input points
obsX_x_zoom = []; obsX_y_zoom = []; obsY_zoom = []
for n in range(0, obsX.shape[0]):
x = obsX[n,0]
y = obsX[n,1]
if include_conditions(x,y):
obsX_x_zoom.append(x)
obsX_y_zoom.append(y)
obsY_zoom.append(obsY[n])
# Initialize plot for assessing predictive uncertainty
tri_fig2 = plt.figure()
tri2_ax1 = tri_fig2.add_subplot(111, projection='3d')
# Plot Predictive Mean
linewidth = 0.1; alpha = 0.85
tri2_ax1.plot_trisurf(plot_X_zoom,plot_Y_zoom, predMean_zoom, cmap=cmap, linewidth=linewidth, antialiased=True, alpha=alpha)
# One Standard Deviation
linewidth = 0.075; alpha = 0.2
tri2_ax1.plot_trisurf(plot_X_zoom,plot_Y_zoom, predMean_plus_std, cmap=cmap, linewidth=linewidth, antialiased=True,alpha=alpha)
tri2_ax1.plot_trisurf(plot_X_zoom,plot_Y_zoom, predMean_minus_std, cmap=cmap, linewidth=linewidth,antialiased=True,alpha=alpha)
# Two Standard Deviations
linewidth = 0.05; alpha = 0.1
tri2_ax1.plot_trisurf(plot_X_zoom,plot_Y_zoom, predMean_plus_std2, cmap=cmap, linewidth=linewidth,antialiased=True,alpha=alpha)
tri2_ax1.plot_trisurf(plot_X_zoom,plot_Y_zoom, predMean_minus_std2, cmap=cmap, linewidth=linewidth,antialiased=True,alpha=alpha)
# Three Standard Deviations
linewidth = 0.01; alpha = 0.01
tri2_ax1.plot_trisurf(plot_X_zoom,plot_Y_zoom, predMean_plus_std3, cmap=cmap, linewidth=linewidth,antialiased=True,alpha=alpha)
tri2_ax1.plot_trisurf(plot_X_zoom,plot_Y_zoom, predMean_minus_std3, cmap=cmap, linewidth=linewidth,antialiased=True,alpha=alpha)
# Scatter plot of training observations
alpha = 0.4
tri2_ax1.scatter(obsX_x_zoom, obsX_y_zoom, obsY_zoom, c='k', marker='o', s=15.0, alpha=alpha)
# Add title to plot
plt.suptitle("CppGPs Predictive Uncertainty", fontsize=24)
# Remove axes from plot
remove_axes(tri2_ax1)
# Display plots
plt.show()
# Run main() function when called directly
if __name__ == '__main__':
main()