-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_func.py
More file actions
611 lines (537 loc) · 33.6 KB
/
plot_func.py
File metadata and controls
611 lines (537 loc) · 33.6 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#===============================================================================
# Copyright (c) 2012-2015, GPy authors (see AUTHORS.txt).
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of GPy nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#===============================================================================
import numpy as np
import sys
from GPy.plotting import plotting_library as pl
from GPy.plotting.gpy_plot.plot_util import helper_for_plot_data, update_not_existing_kwargs, \
helper_predict_with_model, get_which_data_ycols, get_x_y_var
from data_plots import _plot_data, _plot_inducing, _plot_data_error
import os
import sys
# sys.path.append(os.path.relpath('/home/mok/module/abcd/ASMD'))
from GPy.plotting.gpy_plot.plot_util import helper_for_plot_data as my_helper_for_plot_data
def plot_mean(self, plot_limits=None, fixed_inputs=None,
resolution=None, plot_raw=False,
apply_link=False, visible_dims=None,
which_data_ycols='all',
levels=20, projection='2d',
label='gp mean',
predict_kw=None,
**kwargs):
"""
Plot the mean of the GP.
You can deactivate the legend for this one plot by supplying None to label.
Give the Y_metadata in the predict_kw if you need it.
:param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits
:type plot_limits: np.array
:param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v.
:type fixed_inputs: a list of tuples
:param int resolution: The resolution of the prediction [defaults are 1D:200, 2D:50]
:param bool plot_raw: plot the latent function (usually denoted f) only?
:param bool apply_link: whether to apply the link function of the GP to the raw prediction.
:param array-like which_data_ycols: which columns of y to plot (array-like or list of ints)
:param int levels: for 2D plotting, the number of contour levels to use is
:param {'2d','3d'} projection: whether to plot in 2d or 3d. This only applies when plotting two dimensional inputs!
:param str label: the label for the plot.
:param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=<specific kernel>) in here
"""
canvas, kwargs = pl().new_canvas(projection=projection, **kwargs)
X = get_x_y_var(self)[0]
helper_data = helper_for_plot_data(self, X, plot_limits, visible_dims, fixed_inputs, resolution)
helper_prediction = helper_predict_with_model(self, helper_data[2], plot_raw,
apply_link, None,
get_which_data_ycols(self, which_data_ycols),
predict_kw)
plots = _plot_mean(self, canvas, helper_data, helper_prediction,
levels, projection, label, **kwargs)
return pl().add_to_canvas(canvas, plots)
def _plot_mean(self, canvas, helper_data, helper_prediction,
levels=20, projection='2d', label=None,
**kwargs):
_, free_dims, Xgrid, x, y, _, _, resolution = helper_data
if len(free_dims)<=2:
mu, _, _ = helper_prediction
if len(free_dims)==1:
# 1D plotting:
update_not_existing_kwargs(kwargs, pl().defaults.meanplot_1d) # @UndefinedVariable
##### 이거하니까 오히려 더 못하는거같은데??
if not 'mean_color' in kwargs:
# print('no mean color!')
kwargs['color'] = '#FF2400'
if 'data_color' in kwargs: kwargs.pop('data_color')
if 'mean_color' in kwargs: kwargs.pop('mean_color')
if 'shade_color' in kwargs: kwargs.pop('shade_color')
else:
kwargs['color']=kwargs['mean_color']
if 'data_color' in kwargs: kwargs.pop('data_color')
if 'mean_color' in kwargs: kwargs.pop('mean_color')
if 'shade_color' in kwargs: kwargs.pop('shade_color')
# kwargs['color'] = kwargs['mean_color'] #### like this
# kwargs['color'] = '#000000'
# kwargs['linewidth'] = 1
plots = dict(gpmean=[pl().plot(canvas, Xgrid[:, free_dims], mu, label=label, **kwargs)])
else:
if projection.lower() in '2d':
update_not_existing_kwargs(kwargs, pl().defaults.meanplot_2d) # @UndefinedVariable
plots = dict(gpmean=[pl().contour(canvas, x[:,0], y[0,:],
mu.reshape(resolution, resolution).T,
levels=levels, label=label, **kwargs)])
elif projection.lower() in '3d':
update_not_existing_kwargs(kwargs, pl().defaults.meanplot_3d) # @UndefinedVariable
plots = dict(gpmean=[pl().surface(canvas, x, y,
mu.reshape(resolution, resolution),
label=label,
**kwargs)])
elif len(free_dims)==0:
pass # Nothing to plot!
else:
raise RuntimeError('Cannot plot mean in more then 2 input dimensions')
return plots
def plot_confidence(self, lower=2.5, upper=97.5, plot_limits=None, fixed_inputs=None,
resolution=None, plot_raw=False,
apply_link=False, visible_dims=None,
which_data_ycols='all', label='gp confidence',
predict_kw=None,
**kwargs):
"""
Plot the confidence interval between the percentiles lower and upper.
E.g. the 95% confidence interval is $2.5, 97.5$.
Note: Only implemented for one dimension!
You can deactivate the legend for this one plot by supplying None to label.
Give the Y_metadata in the predict_kw if you need it.
:param float lower: the lower percentile to plot
:param float upper: the upper percentile to plot
:param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits
:type plot_limits: np.array
:param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v.
:type fixed_inputs: a list of tuples
:param int resolution: The resolution of the prediction [default:200]
:param bool plot_raw: plot the latent function (usually denoted f) only?
:param bool apply_link: whether to apply the link function of the GP to the raw prediction.
:param array-like visible_dims: which columns of the input X (!) to plot (array-like or list of ints)
:param array-like which_data_ycols: which columns of the output y (!) to plot (array-like or list of ints)
:param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=<specific kernel>) in here
"""
canvas, kwargs = pl().new_canvas(**kwargs)
ycols = get_which_data_ycols(self, which_data_ycols)
X = get_x_y_var(self)[0]
helper_data = helper_for_plot_data(self, X, plot_limits, visible_dims, fixed_inputs, resolution)
helper_prediction = helper_predict_with_model(self, helper_data[2], plot_raw, apply_link,
(lower, upper),
ycols, predict_kw)
plots = _plot_confidence(self, canvas, helper_data, helper_prediction, label, **kwargs)
return pl().add_to_canvas(canvas, plots, legend=label is not None)
def _plot_confidence(self, canvas, helper_data, helper_prediction, label, **kwargs):
_, free_dims, Xgrid, _, _, _, _, _ = helper_data
# import pdb; pdb.set_trace()
update_not_existing_kwargs(kwargs, pl().defaults.confidence_interval) # @UndefinedVariable
#### ADDED
# kwargs['color'] = kwargs['variance_color']
if len(free_dims)<=1:
if len(free_dims)==1:
percs = helper_prediction[1]
fills = []
if 'shade_color' in kwargs:
kwargs['color']=kwargs['shade_color']
if 'data_color' in kwargs: kwargs.pop('data_color')
if 'mean_color' in kwargs: kwargs.pop('mean_color')
if 'shade_color' in kwargs: kwargs.pop('shade_color')
else:
if 'data_color' in kwargs: kwargs.pop('data_color')
if 'mean_color' in kwargs: kwargs.pop('mean_color')
if 'shade_color' in kwargs: kwargs.pop('shade_color')
if 'markersize' in kwargs:
kwargs.pop('markersize')
if 'linewidth' in kwargs:
kwargs.pop('linewidth')
kwargs['alpha'] = 0.3
for d in range(helper_prediction[0].shape[1]):
fills.append(pl().fill_between(canvas, Xgrid[:,free_dims[0]], percs[0][:,d], percs[1][:,d], label=label, **kwargs))
return dict(gpconfidence=fills)
else:
pass #Nothing to plot!
else:
raise RuntimeError('Can only plot confidence interval in one input dimension')
def plot_samples(self, plot_limits=None, fixed_inputs=None,
resolution=None, plot_raw=True,
apply_link=False, visible_dims=None,
which_data_ycols='all',
samples=3, projection='2d', label='gp_samples',
predict_kw=None,
**kwargs):
"""
Plot the mean of the GP.
You can deactivate the legend for this one plot by supplying None to label.
Give the Y_metadata in the predict_kw if you need it.
:param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits
:type plot_limits: np.array
:param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v.
:type fixed_inputs: a list of tuples
:param int resolution: The resolution of the prediction [defaults are 1D:200, 2D:50]
:param bool plot_raw: plot the latent function (usually denoted f) only? This is usually what you want!
:param bool apply_link: whether to apply the link function of the GP to the raw prediction.
:param array-like visible_dims: which columns of the input X (!) to plot (array-like or list of ints)
:param array-like which_data_ycols: which columns of y to plot (array-like or list of ints)
:param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=<specific kernel>) in here
:param int levels: for 2D plotting, the number of contour levels to use is
"""
canvas, kwargs = pl().new_canvas(projection=projection, **kwargs)
ycols = get_which_data_ycols(self, which_data_ycols)
X = get_x_y_var(self)[0]
helper_data = helper_for_plot_data(self, X, plot_limits, visible_dims, fixed_inputs, resolution)
helper_prediction = helper_predict_with_model(self, helper_data[2], plot_raw, apply_link,
None,
ycols, predict_kw, samples)
plots = _plot_samples(self, canvas, helper_data, helper_prediction,
projection, label, **kwargs)
return pl().add_to_canvas(canvas, plots)
def _plot_samples(self, canvas, helper_data, helper_prediction, projection,
label, **kwargs):
_, free_dims, Xgrid, x, y, _, _, resolution = helper_data
samples = helper_prediction[2]
if len(free_dims)<=2:
if len(free_dims)==1:
# 1D plotting:
# import pdb; pdb.set_trace()
if 'data_color' in kwargs:
print('data_color given!')
kwargs['color'] = kwargs['data_color']
kwargs.pop('data_color')
kwargs.pop('mean_color')
kwargs.pop('shade_color')
update_not_existing_kwargs(kwargs, pl().defaults.samples_1d) # @UndefinedVariable
plots = [pl().plot(canvas, Xgrid[:, free_dims], samples[:, :, s], label=label if s==0 else None, **kwargs) for s in range(samples.shape[-1])]
elif len(free_dims)==2 and projection=='3d':
update_not_existing_kwargs(kwargs, pl().defaults.samples_3d) # @UndefinedVariable
plots = [pl().surface(canvas, x, y, samples[:, :, s].reshape(resolution, resolution), **kwargs) for s in range(samples.shape[-1])]
else:
pass # Nothing to plot!
return dict(gpmean=plots)
else:
raise RuntimeError('Cannot plot mean in more then 1 input dimensions')
def plot_density(self, plot_limits=None, fixed_inputs=None,
resolution=None, plot_raw=False,
apply_link=False, visible_dims=None,
which_data_ycols='all',
levels=35, label='gp density',
predict_kw=None,
**kwargs):
"""
Plot the confidence interval between the percentiles lower and upper.
E.g. the 95% confidence interval is $2.5, 97.5$.
Note: Only implemented for one dimension!
You can deactivate the legend for this one plot by supplying None to label.
Give the Y_metadata in the predict_kw if you need it.
:param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits
:type plot_limits: np.array
:param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v.
:type fixed_inputs: a list of tuples
:param int resolution: The resolution of the prediction [default:200]
:param bool plot_raw: plot the latent function (usually denoted f) only?
:param bool apply_link: whether to apply the link function of the GP to the raw prediction.
:param array-like visible_dims: which columns of the input X (!) to plot (array-like or list of ints)
:param array-like which_data_ycols: which columns of y to plot (array-like or list of ints)
:param int levels: the number of levels in the density (number bigger then 1, where 35 is smooth and 1 is the same as plot_confidence). You can go higher then 50 if the result is not smooth enough for you.
:param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=<specific kernel>) in here
"""
canvas, kwargs = pl().new_canvas(**kwargs)
X = get_x_y_var(self)[0]
helper_data = helper_for_plot_data(self, X, plot_limits, visible_dims, fixed_inputs, resolution)
helper_prediction = helper_predict_with_model(self, helper_data[2], plot_raw,
apply_link, np.linspace(2.5, 97.5, levels*2),
get_which_data_ycols(self, which_data_ycols),
predict_kw)
plots = _plot_density(self, canvas, helper_data, helper_prediction, label, **kwargs)
return pl().add_to_canvas(canvas, plots)
def _plot_density(self, canvas, helper_data, helper_prediction, label, **kwargs):
_, free_dims, Xgrid, _, _, _, _, _ = helper_data
mu, percs, _ = helper_prediction
update_not_existing_kwargs(kwargs, pl().defaults.density) # @UndefinedVariable
if len(free_dims)<=1:
if len(free_dims)==1:
# 1D plotting:
fills = []
for d in range(mu.shape[1]):
fills.append(pl().fill_gradient(
canvas, Xgrid[:, free_dims[0]], [p[:,d] for p in percs],
label=label, **kwargs)
)
return dict(gpdensity=fills)
else:
pass # Nothing to plot!
else:
raise RuntimeError('Can only plot density in one input dimension')
def plot(self, ymin, ymax, xlim=None, plot_limits=None, fixed_inputs=None,
resolution=None,
plot_raw=False, apply_link=False,
which_data_ycols='all', which_data_rows='all',
visible_dims=None,
levels=20, samples=0, samples_likelihood=0, lower=2.5, upper=97.5,
plot_data=True, plot_samples=True, plot_inducing=True, plot_density=False, plot_confidence=True,
predict_kw=None, projection='2d', legend=True, limit_x=False,
**kwargs):
"""
Convenience function for plotting the fit of a GP.
You can deactivate the legend for this one plot by supplying None to label.
Give the Y_metadata in the predict_kw if you need it.
If you want fine graned control use the specific plotting functions supplied in the model.
:param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits
:type plot_limits: np.array
:param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v.
:type fixed_inputs: a list of tuples
:param int resolution: The resolution of the prediction [default:200]
:param bool plot_raw: plot the latent function (usually denoted f) only?
:param bool apply_link: whether to apply the link function of the GP to the raw prediction.
:param which_data_ycols: when the data has several columns (independant outputs), only plot these
:type which_data_ycols: 'all' or a list of integers
:param which_data_rows: which of the training data to plot (default all)
:type which_data_rows: 'all' or a slice object to slice self.X, self.Y
:param array-like visible_dims: which columns of the input X (!) to plot (array-like or list of ints)
:param int levels: the number of levels in the density (number bigger then 1, where 35 is smooth and 1 is the same as plot_confidence). You can go higher then 50 if the result is not smooth enough for you.
:param int samples: the number of samples to draw from the GP and plot into the plot. This will allways be samples from the latent function.
:param int samples_likelihood: the number of samples to draw from the GP and apply the likelihood noise. This is usually not what you want!
:param float lower: the lower percentile to plot
:param float upper: the upper percentile to plot
:param bool plot_data: plot the data into the plot?
:param bool plot_inducing: plot inducing inputs?
:param bool plot_density: plot density instead of the confidence interval?
:param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=<specific kernel>) in here
:param {2d|3d} projection: plot in 2d or 3d?
:param bool legend: convenience, whether to put a legend on the plot or not.
"""
X = get_x_y_var(self)[0]
# import pdb; pdb.set_trace()
if not limit_x:
helper_data = helper_for_plot_data(self, X, plot_limits, visible_dims, fixed_inputs, resolution)
else:
helper_data = my_helper_for_plot_data(self, X, plot_limits, visible_dims, fixed_inputs, resolution) #### CURRENTLY NOT USED
xmin, xmax = helper_data[5:7]
free_dims = helper_data[1]
# if not 'xlim' in kwargs:
# kwargs['xlim'] = (xlim[0], xlim[1]) #### 위에 arg에서 xlim 없애놓기
if not 'ylim' in kwargs and len(free_dims) == 2:
kwargs['ylim'] = (xmin[1], xmax[1])
# kwargs['ylim'] = (ymin-(ymax-ymin)*0.5, ymax+(ymax-ymin)*0.5)
if xlim is not None:
kwargs['xlim'] = (xlim[0], xlim[1]) #### 위에 arg에서 xlim 없애놓기
kwargs['ylim'] = (ymin, ymax)
# print('ymin, ymax setting done!')
canvas, _ = pl().new_canvas(projection=projection, **kwargs)
helper_prediction = helper_predict_with_model(self, helper_data[2], plot_raw,
apply_link, np.linspace(2.5, 97.5, levels*2) if plot_density else (lower,upper),
get_which_data_ycols(self, which_data_ycols),
predict_kw, samples)
if plot_raw and not apply_link:
# It does not make sense to plot the data (which lives not in the latent function space) into latent function space.
plot_data = False
plots = {}
if hasattr(self, 'Z') and plot_inducing:
plots.update(_plot_inducing(self, canvas, free_dims, projection, 'Inducing'))
if plot_data:
# import pdb; pdb.set_trace()
# plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data", **kwargs))
if 'data_color' in kwargs:
plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data", data_color=kwargs['data_color'], dataX=kwargs['dataX'], dataY=kwargs['dataY']))
else:
if 'dataX' in kwargs and 'dataY' in kwargs:
plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data", dataX=kwargs['dataX'], dataY=kwargs['dataY']))
else:
plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data"))
# import pdb; pdb.set_trace()
# plots.update(_plot_data_error(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data Error", **kwargs))
if 'dataX' in kwargs and 'dataY' in kwargs:
plots.update(_plot_data_error(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data Error", dataX=kwargs['dataX'], dataY=kwargs['dataY']))
else:
plots.update(_plot_data_error(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data Error"))
# import pdb; pdb.set_trace()
if plot_samples:
# plots.update(_plot(self, canvas, plots, helper_data, helper_prediction, levels, plot_inducing, plot_density, plot_confidence, projection, **kwargs))
plots.update(_plot(self, canvas, plots, helper_data, helper_prediction, levels, plot_inducing, plot_density, plot_confidence, projection))
# import pdb; pdb.set_trace()
if plot_raw and (samples_likelihood > 0):
helper_prediction = helper_predict_with_model(self, helper_data[2], False,
apply_link, None,
get_which_data_ycols(self, which_data_ycols),
predict_kw, samples_likelihood)
# plots.update(_plot_samples(canvas, helper_data, helper_prediction, projection, "Lik Samples", **kwargs))
plots.update(_plot_samples(canvas, helper_data, helper_prediction, projection, "Lik Samples"))
# import pdb; pdb.set_trace()
return pl().add_to_canvas(canvas, plots, legend=legend)
def plot_ablation(self, ymin, ymax, plot_limits=None, fixed_inputs=None,
resolution=None,
plot_raw=False, apply_link=False,
which_data_ycols='all', which_data_rows='all',
visible_dims=None,
levels=20, samples=0, samples_likelihood=0, lower=2.5, upper=97.5,
plot_data=True, plot_samples=True, plot_inducing=True, plot_density=False, plot_confidence=True,
predict_kw=None, projection='2d', legend=True, limit_x=False,
**kwargs):
"""
Convenience function for plotting the fit of a GP.
You can deactivate the legend for this one plot by supplying None to label.
Give the Y_metadata in the predict_kw if you need it.
If you want fine graned control use the specific plotting functions supplied in the model.
:param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits
:type plot_limits: np.array
:param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v.
:type fixed_inputs: a list of tuples
:param int resolution: The resolution of the prediction [default:200]
:param bool plot_raw: plot the latent function (usually denoted f) only?
:param bool apply_link: whether to apply the link function of the GP to the raw prediction.
:param which_data_ycols: when the data has several columns (independant outputs), only plot these
:type which_data_ycols: 'all' or a list of integers
:param which_data_rows: which of the training data to plot (default all)
:type which_data_rows: 'all' or a slice object to slice self.X, self.Y
:param array-like visible_dims: which columns of the input X (!) to plot (array-like or list of ints)
:param int levels: the number of levels in the density (number bigger then 1, where 35 is smooth and 1 is the same as plot_confidence). You can go higher then 50 if the result is not smooth enough for you.
:param int samples: the number of samples to draw from the GP and plot into the plot. This will allways be samples from the latent function.
:param int samples_likelihood: the number of samples to draw from the GP and apply the likelihood noise. This is usually not what you want!
:param float lower: the lower percentile to plot
:param float upper: the upper percentile to plot
:param bool plot_data: plot the data into the plot?
:param bool plot_inducing: plot inducing inputs?
:param bool plot_density: plot density instead of the confidence interval?
:param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=<specific kernel>) in here
:param {2d|3d} projection: plot in 2d or 3d?
:param bool legend: convenience, whether to put a legend on the plot or not.
"""
X = get_x_y_var(self)[0]
if not limit_x:
helper_data = helper_for_plot_data(self, X, plot_limits, visible_dims, fixed_inputs, resolution)
else:
helper_data = my_helper_for_plot_data(self, X, plot_limits, visible_dims, fixed_inputs, resolution) #### CURRENTLY NOT USED
xmin, xmax = helper_data[5:7]
free_dims = helper_data[1]
if not 'xlim' in kwargs:
kwargs['xlim'] = (xmin[0], xmax[0])
if not 'ylim' in kwargs and len(free_dims) == 2:
kwargs['ylim'] = (xmin[1], xmax[1])
# kwargs['ylim'] = (ymin-(ymax-ymin)*0.5, ymax+(ymax-ymin)*0.5)
kwargs['ylim'] = (ymin, ymax)
# print('ymin, ymax setting done!')
canvas, _ = pl().new_canvas(projection=projection, **kwargs)
# import pdb; pdb.set_trace()
helper_prediction = helper_predict_with_model(self, helper_data[2], plot_raw,
apply_link, np.linspace(2.5, 97.5, levels*2) if plot_density else (lower,upper),
get_which_data_ycols(self, which_data_ycols),
predict_kw, samples)
if plot_raw and not apply_link:
# It does not make sense to plot the data (which lives not in the latent function space) into latent function space.
plot_data = False
kwargs.pop('xlim')
kwargs.pop('ylim')
if 'figsize' in kwargs: kwargs.pop('figsize')
plots = {}
if hasattr(self, 'Z') and plot_inducing:
plots.update(_plot_inducing(self, canvas, free_dims, projection, 'Inducing'))
if plot_data:
# import pdb; pdb.set_trace()
plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data", **kwargs))
# plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data"))
# import pdb; pdb.set_trace()
plots.update(_plot_data_error(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data Error", **kwargs))
# plots.update(_plot_data_error(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data Error"))
# import pdb; pdb.set_trace()
if plot_samples:
plots.update(_plot(self, canvas, plots, helper_data, helper_prediction, levels, plot_inducing, plot_density, plot_confidence, projection, **kwargs))
# plots.update(_plot(self, canvas, plots, helper_data, helper_prediction, levels, plot_inducing, plot_density, plot_confidence, projection))
# import pdb; pdb.set_trace()
if plot_raw and (samples_likelihood > 0):
helper_prediction = helper_predict_with_model(self, helper_data[2], False,
apply_link, None,
get_which_data_ycols(self, which_data_ycols),
predict_kw, samples_likelihood)
# plots.update(_plot_samples(canvas, helper_data, helper_prediction, projection, "Lik Samples", **kwargs))
plots.update(_plot_samples(canvas, helper_data, helper_prediction, projection, "Lik Samples"))
# import pdb; pdb.set_trace()
return pl().add_to_canvas(canvas, plots, legend=legend)
def plot_f(self, plot_limits=None, fixed_inputs=None,
resolution=None,
apply_link=False,
which_data_ycols='all', which_data_rows='all',
visible_dims=None,
levels=20, samples=0, lower=2.5, upper=97.5,
plot_density=False,
plot_data=True, plot_inducing=True,
projection='2d', legend=True,
predict_kw=None,
**kwargs):
"""
Convinience function for plotting the fit of a GP.
This is the same as plot, except it plots the latent function fit of the GP!
If you want fine graned control use the specific plotting functions supplied in the model.
You can deactivate the legend for this one plot by supplying None to label.
Give the Y_metadata in the predict_kw if you need it.
:param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits
:type plot_limits: np.array
:param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v.
:type fixed_inputs: a list of tuples
:param int resolution: The resolution of the prediction [default:200]
:param bool apply_link: whether to apply the link function of the GP to the raw prediction.
:param which_data_ycols: when the data has several columns (independant outputs), only plot these
:type which_data_ycols: 'all' or a list of integers
:param which_data_rows: which of the training data to plot (default all)
:type which_data_rows: 'all' or a slice object to slice self.X, self.Y
:param array-like visible_dims: an array specifying the input dimensions to plot (maximum two)
:param int levels: the number of levels in the density (number bigger then 1, where 35 is smooth and 1 is the same as plot_confidence). You can go higher then 50 if the result is not smooth enough for you.
:param int samples: the number of samples to draw from the GP and plot into the plot. This will allways be samples from the latent function.
:param float lower: the lower percentile to plot
:param float upper: the upper percentile to plot
:param bool plot_data: plot the data into the plot?
:param bool plot_inducing: plot inducing inputs?
:param bool plot_density: plot density instead of the confidence interval?
:param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=<specific kernel>) in here
:param dict error_kwargs: kwargs for the error plot for the plotting library you are using
:param kwargs plot_kwargs: kwargs for the data plot for the plotting library you are using
"""
return plot(self, plot_limits, fixed_inputs, resolution, True,
apply_link, which_data_ycols, which_data_rows,
visible_dims, levels, samples, 0,
lower, upper, plot_data, plot_inducing,
plot_density, predict_kw, projection, legend, **kwargs)
def _plot(self, canvas, plots, helper_data, helper_prediction, levels, plot_inducing=True, plot_density=False, plot_confidence=True, projection='2d', **kwargs):##kwargs 추가
plots.update(_plot_mean(self, canvas, helper_data, helper_prediction, levels, projection, 'Mean', **kwargs))
try:
if projection=='2d':
if not plot_density:
if plot_confidence:
plots.update(_plot_confidence(self, canvas, helper_data, helper_prediction, "Confidence", **kwargs))
else:
pass
else:
plots.update(_plot_density(self, canvas, helper_data, helper_prediction, "Density", **kwargs))
except RuntimeError:
#plotting in 2d
pass
if helper_prediction[2] is not None:
plots.update(_plot_samples(self, canvas, helper_data, helper_prediction, projection, "Samples", **kwargs))
return plots
if __name__=='__main__':
import pdb; pdb.set_trace()