[testing] updates again and plotly is going forward

This commit is contained in:
mzwiessele 2015-10-07 19:03:03 +01:00
parent 78128ea218
commit ccb6ebcadd
15 changed files with 409 additions and 309 deletions

View file

@ -56,7 +56,7 @@ def plot_data(self, which_data_rows='all',
"""
canvas, plot_kwargs = pl.new_canvas(projection=projection, **plot_kwargs)
plots = _plot_data(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection, label, **plot_kwargs)
return pl.show_canvas(canvas, plots)
return pl.add_to_canvas(canvas, plots)
def _plot_data(self, canvas, which_data_rows='all',
which_data_ycols='all', visible_dims=None,
@ -81,12 +81,12 @@ def _plot_data(self, canvas, which_data_rows='all',
for d in ycols:
update_not_existing_kwargs(plot_kwargs, pl.defaults.data_2d) # @UndefinedVariable
plots['dataplot'].append(pl.scatter(canvas, X[rows, free_dims[0]], X[rows, free_dims[1]],
color=Y[rows, d], vmin=Y.min(), vmax=Y.max(), label=label, **plot_kwargs))
color=Y[rows, d], label=label, **plot_kwargs))
else:
for d in ycols:
update_not_existing_kwargs(plot_kwargs, pl.defaults.data_2d) # @UndefinedVariable
plots['dataplot'].append(pl.scatter(canvas, X[rows, free_dims[0]], X[rows, free_dims[1]],
Z=Y[rows, d], vmin=Y.min(), color=Y[rows, d], vmax=Y.max(), label=label, **plot_kwargs))
Z=Y[rows, d], color=Y[rows, d], label=label, **plot_kwargs))
elif len(free_dims) == 0:
pass #Nothing to plot!
else:
@ -117,9 +117,9 @@ def plot_data_error(self, which_data_rows='all',
:returns list: of plots created.
"""
canvas, error_kwargs = pl.new_canvas(projection=='3d', **error_kwargs)
canvas, error_kwargs = pl.new_canvas(projection=projection, **error_kwargs)
plots = _plot_data_error(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection, label, **error_kwargs)
return pl.show_canvas(canvas, plots)
return pl.add_to_canvas(canvas, plots)
def _plot_data_error(self, canvas, which_data_rows='all',
which_data_ycols='all', visible_dims=None,
@ -167,7 +167,7 @@ def plot_inducing(self, visible_dims=None, projection='2d', label=None, **plot_k
"""
canvas, kwargs = pl.new_canvas(projection=projection, **plot_kwargs)
plots = _plot_inducing(self, canvas, visible_dims, projection, label, **kwargs)
return pl.show_canvas(canvas, plots)
return pl.add_to_canvas(canvas, plots)
def _plot_inducing(self, canvas, visible_dims, projection, label, **plot_kwargs):
if visible_dims is None:
@ -220,7 +220,7 @@ def plot_errorbars_trainset(self, which_data_rows='all',
canvas, kwargs = pl.new_canvas(projection=projection, **plot_kwargs)
plots = _plot_errorbars_trainset(self, canvas, which_data_rows, which_data_ycols,
fixed_inputs, plot_raw, apply_link, label, projection, predict_kw, **kwargs)
return pl.show_canvas(canvas, plots)
return pl.add_to_canvas(canvas, plots)
def _plot_errorbars_trainset(self, canvas,
which_data_rows='all', which_data_ycols='all',
@ -245,25 +245,31 @@ def _plot_errorbars_trainset(self, canvas,
if len(free_dims)<=2:
update_not_existing_kwargs(plot_kwargs, pl.defaults.yerrorbar)
if len(free_dims)==1:
if predict_kw is None:
if predict_kw is None:
predict_kw = {}
if 'Y_metadata' not in predict_kw:
predict_kw['Y_metadata'] = self.Y_metadata or {}
_, percs, _ = helper_predict_with_model(self, Xgrid, plot_raw,
apply_link, (2.5, 97.5),
ycols, predict_kw)
if 'Y_metadata' not in predict_kw:
predict_kw['Y_metadata'] = self.Y_metadata or {}
mu, percs, _ = helper_predict_with_model(self, Xgrid, plot_raw,
apply_link, (2.5, 97.5),
ycols, predict_kw)
if len(free_dims)==1:
for d in ycols:
plots.append(pl.yerrorbar(canvas, X[rows,free_dims[0]], Y[rows,d],
np.vstack([Y[rows,d]-percs[0][rows,d], percs[1][rows,d]-Y[rows,d]]),
plots.append(pl.yerrorbar(canvas, X[rows,free_dims[0]], mu[rows,d],
np.vstack([mu[rows, d] - percs[0][rows, d], percs[1][rows, d] - mu[rows,d]]),
label=label,
**plot_kwargs))
elif len(free_dims) == 2:
plots.append(pl.yerrorbar(canvas, X[rows,free_dims[0]], X[rows,free_dims[1]],
np.vstack([Y[rows,d]-percs[0][rows,d], percs[1][rows,d]-Y[rows,d]]),
Y[rows,d],
label=label,
**plot_kwargs))
for d in ycols:
plots.append(pl.yerrorbar(canvas, X[rows,free_dims[0]], X[rows,free_dims[1]],
np.vstack([mu[rows, d] - percs[0][rows, d], percs[1][rows, d] - mu[rows,d]]),
color=Y[rows,d],
label=label,
**plot_kwargs))
plots.append(pl.xerrorbar(canvas, X[rows,free_dims[0]], X[rows,free_dims[1]],
np.vstack([mu[rows, d] - percs[0][rows, d], percs[1][rows, d] - mu[rows,d]]),
color=Y[rows,d],
label=label,
**plot_kwargs))
pass #Nothing to plot!
else:
raise NotImplementedError("Cannot plot in more then one dimension.")

View file

@ -63,26 +63,23 @@ def plot_mean(self, plot_limits=None, fixed_inputs=None,
: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)
plots = _plot_mean(self, canvas, plot_limits, fixed_inputs,
resolution, plot_raw,
apply_link, visible_dims, which_data_ycols,
levels, projection, label, predict_kw, **kwargs)
return pl.show_canvas(canvas, plots)
def _plot_mean(self, canvas, 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=None,
predict_kw=None,
**kwargs):
_, _, _, _, free_dims, Xgrid, x, y, _, _, resolution = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution)
if len(free_dims)<=2:
mu, _, _ = helper_predict_with_model(self, Xgrid, plot_raw,
helper_data = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution)
helper_prediction = helper_predict_with_model(self, helper_data[5], 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)
pl.add_to_canvas(canvas, plots)
return pl.show_canvas(canvas)
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
@ -108,7 +105,7 @@ def _plot_mean(self, canvas, plot_limits=None, fixed_inputs=None,
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',
which_data_ycols='all', label=None,
predict_kw=None,
**kwargs):
"""
@ -132,33 +129,23 @@ def plot_confidence(self, lower=2.5, upper=97.5, plot_limits=None, fixed_inputs=
: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)
plots = _plot_confidence(self, canvas, lower, upper, plot_limits,
fixed_inputs, resolution, plot_raw,
apply_link, visible_dims, which_data_ycols,
predict_kw, **kwargs)
return pl.show_canvas(canvas, plots)
def _plot_confidence(self, canvas, lower, upper, plot_limits=None, fixed_inputs=None,
resolution=None, plot_raw=False,
apply_link=False, visible_dims=None,
which_data_ycols=None,
predict_kw=None,
**kwargs):
_, _, _, _, free_dims, Xgrid, _, _, _, _, _ = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution)
ycols = get_which_data_ycols(self, which_data_ycols)
update_not_existing_kwargs(kwargs, pl.defaults.confidence_interval) # @UndefinedVariable
if len(free_dims)<=1:
if len(free_dims)==1:
_, percs, _ = helper_predict_with_model(self, Xgrid, plot_raw, apply_link,
helper_data = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution)
helper_prediction = helper_predict_with_model(self, helper_data[5], 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)
def _plot_confidence(self, canvas, helper_data, helper_prediction, label, **kwargs):
_, _, _, _, free_dims, Xgrid, _, _, _, _, _ = helper_data
update_not_existing_kwargs(kwargs, pl.defaults.confidence_interval) # @UndefinedVariable
if len(free_dims)<=1:
if len(free_dims)==1:
percs = helper_prediction[1]
fills = []
for d in ycols:
fills.append(pl.fill_between(canvas, Xgrid[:,free_dims[0]], percs[0][:,d], percs[1][:,d], **kwargs))
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!
@ -170,7 +157,8 @@ 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', predict_kw=None,
samples=3, projection='2d', label=None,
predict_kw=None,
**kwargs):
"""
Plot the mean of the GP.
@ -191,36 +179,31 @@ def plot_samples(self, plot_limits=None, fixed_inputs=None,
:param int levels: for 2D plotting, the number of contour levels to use is
"""
canvas, kwargs = pl.new_canvas(projection=projection, **kwargs)
plots = _plot_samples(self, canvas, plot_limits, fixed_inputs,
resolution, plot_raw,
apply_link, visible_dims, which_data_ycols, samples, projection,
predict_kw, **kwargs)
return pl.show_canvas(canvas, plots)
ycols = get_which_data_ycols(self, which_data_ycols)
helper_data = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution)
helper_prediction = helper_predict_with_model(self, helper_data[5], 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, plot_limits=None, fixed_inputs=None,
resolution=None, plot_raw=True,
apply_link=False, visible_dims=None,
which_data_ycols=None,
samples=3, projection='2d',
label=None,
predict_kw=None, **kwargs):
_, _, _, _, free_dims, Xgrid, x, y, _, _, resolution = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution)
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:
_, _, samples = helper_predict_with_model(self, Xgrid, plot_raw, apply_link,
None, get_which_data_ycols(self, which_data_ycols), predict_kw, samples)
update_not_existing_kwargs(kwargs, pl.defaults.samples_1d) # @UndefinedVariable
return dict(gpmean=[pl.plot(canvas, Xgrid[:, free_dims], samples, **kwargs)])
return dict(gpmean=[pl.plot(canvas, Xgrid[:, free_dims], samples, label=label, **kwargs)])
elif len(free_dims)==2 and projection=='3d':
_, _, samples = helper_predict_with_model(self, Xgrid, plot_raw, apply_link,
None, get_which_data_ycols(self, which_data_ycols), predict_kw, samples)
update_not_existing_kwargs(kwargs, pl.defaults.samples_3d) # @UndefinedVariable
for s in range(samples.shape[-1]):
return dict(gpmean=[pl.surface(canvas, x,
y, samples[:, s].reshape(resolution, resolution),
**kwargs)])
y, samples[:, s].reshape(resolution, resolution),
**kwargs)])
else:
pass # Nothing to plot!
else:
@ -231,7 +214,7 @@ 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,
levels=35, label=None,
predict_kw=None,
**kwargs):
"""
@ -254,35 +237,26 @@ def plot_density(self, plot_limits=None, fixed_inputs=None,
: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)
plots = _plot_density(self, canvas, plot_limits,
fixed_inputs, resolution, plot_raw,
apply_link, visible_dims, which_data_ycols,
levels,
predict_kw, **kwargs)
return pl.show_canvas(canvas, plots)
helper_data = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution)
helper_prediction = helper_predict_with_model(self, helper_data[5], 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, plot_limits=None, fixed_inputs=None,
resolution=None, plot_raw=False,
apply_link=False, visible_dims=None,
which_data_ycols=None,
levels=35,
predict_kw=None, **kwargs):
_, _, _, _, free_dims, Xgrid, x, y, _, _, resolution = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution)
ycols = get_which_data_ycols(self, which_data_ycols)
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:
_, percs, _ = helper_predict_with_model(self, Xgrid, plot_raw,
apply_link, np.linspace(2.5, 97.5, levels*2),
get_which_data_ycols(self, which_data_ycols),
predict_kw)
# 1D plotting:
fills = []
for d in ycols:
fills.append(pl.fill_gradient(canvas, Xgrid[:, free_dims[0]], [p[:,d] for p in percs], **kwargs))
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!
@ -327,11 +301,28 @@ def plot(self, plot_limits=None, fixed_inputs=None,
: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, _ = pl.new_canvas(projection=projection, **kwargs)
plots = _plot(self, canvas, plot_limits, fixed_inputs, resolution, plot_raw,
apply_link, which_data_ycols, which_data_rows, visible_dims,
levels, samples, samples_likelihood, lower, upper, plot_data,
plot_inducing, plot_density, projection, predict_kw)
return pl.show_canvas(canvas, plots)
helper_data = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution)
helper_prediction = helper_predict_with_model(self, helper_data[5], 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 plot_data:
plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection))
plots.update(_plot_data_error(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection))
plots.update(_plot(self, canvas, plots, helper_data, helper_prediction, levels, plot_inducing, plot_density, projection))
if plot_raw and (samples_likelihood > 0):
helper_prediction = helper_predict_with_model(self, helper_data[5], 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))
if hasattr(self, 'Z') and plot_inducing:
plots.update(_plot_inducing(self, canvas, visible_dims, projection, None))
return pl.add_to_canvas(canvas, plots)
def plot_f(self, plot_limits=None, fixed_inputs=None,
@ -375,50 +366,35 @@ def plot_f(self, plot_limits=None, fixed_inputs=None,
: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
"""
canvas, _ = pl.new_canvas(projection=='3d', **kwargs)
plots = _plot(self, canvas, 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, projection,
predict_kw)
return pl.show_canvas(canvas, plots)
canvas, _ = pl.new_canvas(projection=projection, **kwargs)
helper_data = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution)
helper_prediction = helper_predict_with_model(self, helper_data[5], True,
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 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 plot_data:
plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection))
plots.update(_plot_data_error(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection))
plots.update(_plot(self, canvas, plots, helper_data, helper_prediction, levels, plot_inducing, plot_density, projection))
if hasattr(self, 'Z') and plot_inducing:
plots.update(_plot_inducing(self, canvas, visible_dims, projection, None))
return pl.add_to_canvas(canvas, plots)
def _plot(self, canvas, 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_inducing=True, plot_density=False, projection='2d',
predict_kw=None):
plots = {}
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
if plot_data:
plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, visible_dims,
projection, label=None))
plots.update(_plot_data_error(self, canvas, which_data_rows, which_data_ycols, visible_dims,
projection, label=None))
plots.update(_plot_mean(self, canvas, plot_limits, fixed_inputs, resolution, plot_raw, apply_link, visible_dims, which_data_ycols, levels, projection, label=None,
predict_kw=None))
def _plot(self, canvas, plots, helper_data, helper_prediction, levels, plot_inducing=True, plot_density=False, projection='2d'):
plots.update(_plot_mean(self, canvas, helper_data, helper_prediction, levels, projection, None))
if projection=='2d':
if not plot_density:
plots.update(_plot_confidence(self, canvas, lower, upper, plot_limits, fixed_inputs, resolution, plot_raw, apply_link, visible_dims, which_data_ycols, predict_kw))
plots.update(_plot_confidence(self, canvas, helper_data, helper_prediction, None))
else:
plots.update(_plot_density(self, canvas, plot_limits, fixed_inputs, resolution, plot_raw, apply_link, visible_dims, which_data_ycols, levels, predict_kw))
if samples > 0:
plots.update(_plot_samples(self, canvas, plot_limits, fixed_inputs, resolution, True, apply_link, visible_dims, which_data_ycols, samples, predict_kw))
if samples_likelihood > 0:
plots.update(_plot_samples(self, canvas, plot_limits, fixed_inputs, resolution, False, apply_link, visible_dims, which_data_ycols, samples, predict_kw))
if hasattr(self, 'Z') and plot_inducing:
plots.update(_plot_inducing(self, canvas, visible_dims, projection, None))
plots.update(_plot_density(self, canvas, helper_data, helper_prediction, None))
if helper_prediction[2] is not None:
plots.update(_plot_samples(self, canvas, helper_data, helper_prediction, projection, None))
return plots

View file

@ -13,7 +13,7 @@ def plot_optimizer(optimizer, **kwargs):
else:
canvas, kwargs = pl.new_canvas(**kwargs)
plots = dict(trace=pl.plot(range(len(optimizer.trace)), optimizer.trace))
return pl.show_canvas(canvas, plots, xlabel='Iteration', ylabel='f(x)')
return pl.add_to_canvas(canvas, plots, xlabel='Iteration', ylabel='f(x)')
def plot_sgd_traces(optimizer):
figure = pl.figure(2,1)
@ -21,8 +21,8 @@ def plot_sgd_traces(optimizer):
plots = dict(lines=[])
for k in optimizer.param_traces.keys():
plots['lines'].append(pl.plot(canvas, range(len(optimizer.param_traces[k])), optimizer.param_traces[k], label=k))
pl.show_canvas(canvas, legend=True)
pl.add_to_canvas(canvas, legend=True)
canvas, _ = pl.new_canvas(figure, 1, 2, title="Objective function")
pl.plot(canvas, range(len(optimizer.fopt_trace)), optimizer.fopt_trace)
return pl.show_canvas(canvas, plots, legend=True)
return pl.add_to_canvas(canvas, plots, legend=True)

View file

@ -30,6 +30,10 @@
import numpy as np
from . import pl
from .. import Tango
from .plot_util import get_x_y_var,\
update_not_existing_kwargs, \
helper_for_plot_data, scatter_label_generator, subsample_X,\
find_best_layout_for_subplots
def plot_ARD(kernel, filtering=None, **kwargs):
"""
@ -64,11 +68,95 @@ def plot_ARD(kernel, filtering=None, **kwargs):
else:
print("filtering out {}".format(kernel.parameters[i].name))
plt.show_canvas()
plt.add_to_canvas()
ax.set_xlim(-.5, kernel.input_dim - .5)
add_bar_labels(fig, ax, [bars[-1]], bottom=bottom-last_bottom)
return dict(barplots=bars)
def plot_covariance():
def plot_covariance(kernel, x=None, label=None, plot_limits=None, visible_dims=None, resolution=None, projection=None, levels=20, **mpl_kwargs):
"""
plot a kernel.
:param x: the value to use for the other kernel argument (kernels are a function of two variables!)
:param fignum: figure number of the plot
:param ax: matplotlib axis to plot on
:param title: the matplotlib title
:param plot_limits: the range over which to plot the kernel
:resolution: the resolution of the lines used in plotting
:mpl_kwargs avalid keyword arguments to pass through to matplotlib (e.g. lw=7)
"""
canvas, error_kwargs = pl.new_canvas(projection=projection, **error_kwargs)
_, _, _, _, free_dims, Xgrid, x, y, _, _, resolution = helper_for_plot_data(kernel, plot_limits, visible_dims, None, resolution)
if len(free_dims)<=2:
if len(free_dims)==1:
if x is None: x = np.zeros((1, 1))
else:
x = np.asarray(x)
assert x.size == 1, "The size of the fixed variable x is not 1"
x = x.reshape((1, 1))
# 1D plotting:
update_not_existing_kwargs(kwargs, pl.defaults.meanplot_1d) # @UndefinedVariable
plots = dict(covariance=[pl.plot(canvas, Xgrid[:, free_dims], mu, label=label, **kwargs)])
else:
if projection == '2d':
update_not_existing_kwargs(kwargs, pl.defaults.meanplot_2d) # @UndefinedVariable
plots = dict(covariance=[pl.contour(canvas, x, y,
mu.reshape(resolution, resolution).T,
levels=levels, label=label, **kwargs)])
elif projection == '3d':
update_not_existing_kwargs(kwargs, pl.defaults.meanplot_3d) # @UndefinedVariable
plots = dict(covariance=[pl.surface(canvas, x, y,
mu.reshape(resolution, resolution).T,
label=label,
**kwargs)])
return pl.add_to_canvas(canvas, plots)
if kernel.input_dim == 1:
if plot_limits == None:
xmin, xmax = (x - 5).flatten(), (x + 5).flatten()
elif len(plot_limits) == 2:
xmin, xmax = plot_limits
else:
raise ValueError("Bad limits for plotting")
Xnew = np.linspace(xmin, xmax, resolution or 201)[:, None]
Kx = kernel.K(Xnew, x)
ax.plot(Xnew, Kx, **mpl_kwargs)
ax.set_xlim(xmin, xmax)
ax.set_xlabel("x")
ax.set_ylabel("k(x,%0.1f)" % x)
elif kernel.input_dim == 2:
if x is None:
x = np.zeros((1, 2))
else:
x = np.asarray(x)
assert x.size == 2, "The size of the fixed variable x is not 2"
x = x.reshape((1, 2))
if plot_limits is None:
xmin, xmax = (x - 5).flatten(), (x + 5).flatten()
elif len(plot_limits) == 2:
xmin, xmax = plot_limits
else:
raise ValueError("Bad limits for plotting")
resolution = resolution or 51
xx, yy = np.mgrid[xmin[0]:xmax[0]:1j * resolution, xmin[1]:xmax[1]:1j * resolution]
Xnew = np.vstack((xx.flatten(), yy.flatten())).T
Kx = kernel.K(Xnew, x)
Kx = Kx.reshape(resolution, resolution).T
ax.contour(xx, yy, Kx, vmin=Kx.min(), vmax=Kx.max(), cmap=pb.cm.jet, **mpl_kwargs) # @UndefinedVariable
ax.set_xlim(xmin[0], xmax[0])
ax.set_ylim(xmin[1], xmax[1])
ax.set_xlabel("x1")
ax.set_ylabel("x2")
ax.set_title("k(x1,x2 ; %0.1f,%0.1f)" % (x[0, 0], x[0, 1]))
else:
raise NotImplementedError("Cannot plot a kernel with more than two input dimensions")
pass

View file

@ -94,7 +94,11 @@ def plot_latent_scatter(self, labels=None,
else:
legend = find_best_layout_for_subplots(len(np.unique(labels)))[1]
scatters = _plot_latent_scatter(canvas, X, sig_dims, labels, marker, num_samples, projection=projection, **kwargs)
return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend)
return pl.add_to_canvas(canvas, dict(scatter=scatters), legend=legend)
def plot_latent_inducing(self,
which_indices=None,
legend=False,
@ -125,7 +129,12 @@ def plot_latent_inducing(self,
Z = self.Z.values
labels = np.array(['inducing'] * Z.shape[0])
scatters = _plot_latent_scatter(canvas, Z, sig_dims, labels, marker, num_samples, projection=projection, **kwargs)
return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend)
return pl.add_to_canvas(canvas, dict(scatter=scatters), legend=legend)
def _plot_magnification(self, canvas, which_indices, Xgrid,
xmin, xmax, resolution, updates,
@ -182,12 +191,12 @@ def plot_magnification(self, labels=None, which_indices=None,
legend = False
scatters = _plot_latent_scatter(canvas, X, which_indices, labels, marker, num_samples, projection='2d', **scatter_kwargs or {})
view = _plot_magnification(self, canvas, which_indices[:2], Xgrid, xmin, xmax, resolution, updates, mean, covariance, kern, **imshow_kwargs)
plots = pl.show_canvas(canvas, dict(scatter=scatters, imshow=view),
retval = pl.add_to_canvas(canvas, dict(scatter=scatters, imshow=view),
legend=legend,
)
_wait_for_updates(view, updates)
return plots
return retval
@ -245,10 +254,10 @@ def plot_latent(self, labels=None, which_indices=None,
legend = False
scatters = _plot_latent_scatter(canvas, X, which_indices, labels, marker, num_samples, projection='2d', **scatter_kwargs or {})
view = _plot_latent(self, canvas, which_indices, Xgrid, xmin, xmax, resolution, updates, kern, **imshow_kwargs)
plots = pl.show_canvas(canvas, dict(scatter=scatters, imshow=view), legend=legend)
retval = pl.add_to_canvas(canvas, dict(scatter=scatters, imshow=view), legend=legend)
_wait_for_updates(view, updates)
return plots
return retval
def _plot_steepest_gradient_map(self, canvas, which_indices, Xgrid,
xmin, xmax, resolution, output_labels, updates,
kern=None, annotation_kwargs=None,
@ -310,10 +319,9 @@ def plot_steepest_gradient_map(self, output_labels=None, data_labels=None, which
legend = False
plots = dict(scatter=_plot_latent_scatter(canvas, X, which_indices, data_labels, marker, num_samples, **scatter_kwargs or {}))
plots.update(_plot_steepest_gradient_map(self, canvas, which_indices, Xgrid, xmin, xmax, resolution, output_labels, updates, kern, annotation_kwargs=annotation_kwargs, **imshow_kwargs))
show = pl.show_canvas(canvas, plots, legend=legend)
retval = pl.add_to_canvas(canvas, plots, legend=legend)
_wait_for_updates(plots['annotation'], updates)
return show
return retval

View file

@ -32,6 +32,15 @@ import numpy as np
from scipy import sparse
import itertools
def in_ipynb():
try:
cfg = get_ipython().config
if cfg['IPKernelApp']['parent_appname'] == 'ipython-notebook':
return True
else:
return False
except NameError:
return False
def find_best_layout_for_subplots(num_subplots):
r, c = 1, 1
@ -109,7 +118,7 @@ def helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resoluti
if fixed_inputs is None:
fixed_inputs = []
fixed_dims = get_fixed_dims(self, fixed_inputs)
free_dims = get_free_dims(self, visible_dims, fixed_dims)[:2]
free_dims = get_free_dims(self, visible_dims, fixed_dims)
if len(free_dims) == 1:
#define the frame on which to plot