Merge branch 'devel' of https://github.com/SheffieldML/GPy into wgps_improvements

Merging new devel
This commit is contained in:
beckdaniel 2016-03-02 17:26:04 +00:00
commit 76f3ff65a1
45 changed files with 729 additions and 378 deletions

View file

@ -46,7 +46,7 @@ def plot_mean(self, plot_limits=None, fixed_inputs=None,
"""
Plot the mean of the GP.
You can deactivate the legend for this one plot by supplying None to label.
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.
@ -91,7 +91,7 @@ def _plot_mean(self, canvas, helper_data, helper_prediction,
if projection == '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),
mu.reshape(resolution, resolution).T,
levels=levels, label=label, **kwargs)])
elif projection == '3d':
update_not_existing_kwargs(kwargs, pl().defaults.meanplot_3d) # @UndefinedVariable
@ -116,7 +116,7 @@ def plot_confidence(self, lower=2.5, upper=97.5, plot_limits=None, fixed_inputs=
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.
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.
@ -170,7 +170,7 @@ def plot_samples(self, plot_limits=None, fixed_inputs=None,
"""
Plot the mean of the GP.
You can deactivate the legend for this one plot by supplying None to label.
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.
@ -231,7 +231,7 @@ def plot_density(self, plot_limits=None, fixed_inputs=None,
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.
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.
@ -288,7 +288,7 @@ def plot(self, plot_limits=None, fixed_inputs=None,
"""
Convenience function for plotting the fit of a GP.
You can deactivate the legend for this one plot by supplying None to label.
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.
@ -319,9 +319,17 @@ def plot(self, plot_limits=None, fixed_inputs=None,
:param {2d|3d} projection: plot in 2d or 3d?
:param bool legend: convenience, whether to put a legend on the plot or not.
"""
canvas, _ = 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)
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])
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),
@ -330,9 +338,11 @@ def plot(self, plot_limits=None, fixed_inputs=None,
# 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, visible_dims, projection, 'Inducing'))
if plot_data:
plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection, "Data"))
plots.update(_plot_data_error(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection, "Data Error"))
plots.update(_plot_data(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data"))
plots.update(_plot_data_error(self, canvas, which_data_rows, which_data_ycols, free_dims, projection, "Data Error"))
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[2], False,
@ -340,8 +350,6 @@ def plot(self, plot_limits=None, fixed_inputs=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"))
if hasattr(self, 'Z') and plot_inducing:
plots.update(_plot_inducing(self, canvas, visible_dims, projection, 'Inducing'))
return pl().add_to_canvas(canvas, plots, legend=legend)
@ -362,7 +370,7 @@ def plot_f(self, plot_limits=None, fixed_inputs=None,
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.
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.
@ -389,7 +397,7 @@ 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
"""
plot(self, plot_limits, fixed_inputs, resolution, True,
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,

View file

@ -33,7 +33,7 @@ from .. import Tango
from .plot_util import update_not_existing_kwargs, helper_for_plot_data
from ...kern.src.kern import Kern, CombinationKernel
def plot_ARD(kernel, filtering=None, legend=False, **kwargs):
def plot_ARD(kernel, filtering=None, legend=False, canvas=None, **kwargs):
"""
If an ARD kernel is present, plot a bar representation using matplotlib
@ -62,7 +62,11 @@ def plot_ARD(kernel, filtering=None, legend=False, **kwargs):
bars = []
kwargs = update_not_existing_kwargs(kwargs, pl().defaults.ard)
canvas, kwargs = pl().new_canvas(xlim=(-.5, kernel._effective_input_dim-.5), xlabel='input dimension', ylabel='sensitivity', **kwargs)
if canvas is None:
canvas, kwargs = pl().new_canvas(xlim=(-.5, kernel._effective_input_dim-.5), xlabel='input dimension', ylabel='sensitivity', **kwargs)
for i in range(ard_params.shape[0]):
if parts[i].name in filtering:
c = Tango.nextMedium()
@ -96,7 +100,7 @@ def plot_covariance(kernel, x=None, label=None,
"""
X = np.ones((2, kernel._effective_input_dim)) * [[-3], [3]]
_, free_dims, Xgrid, xx, yy, _, _, resolution = helper_for_plot_data(kernel, X, plot_limits, visible_dims, None, resolution)
from numbers import Number
if x is None:
from ...kern.src.stationary import Stationary
@ -104,7 +108,7 @@ def plot_covariance(kernel, x=None, label=None,
elif isinstance(x, Number):
x = np.ones((1, kernel._effective_input_dim))*x
K = kernel.K(Xgrid, x)
if projection == '3d':
xlabel = 'X[:,0]'
ylabel = 'X[:,1]'

View file

@ -50,6 +50,17 @@ def _wait_for_updates(view, updates):
# No updateable view:
pass
def _new_canvas(self, projection, kwargs, which_indices):
input_1, input_2, input_3 = sig_dims = self.get_most_significant_input_dimensions(which_indices)
if input_3 is None:
zlabel = None
else:
zlabel = 'latent dimension %i' % input_3
canvas, kwargs = pl().new_canvas(projection=projection, xlabel='latent dimension %i' % input_1,
ylabel='latent dimension %i' % input_2,
zlabel=zlabel, **kwargs)
return canvas, projection, kwargs, sig_dims
def _plot_latent_scatter(canvas, X, visible_dims, labels, marker, num_samples, projection='2d', **kwargs):
from .. import Tango
@ -85,12 +96,8 @@ def plot_latent_scatter(self, labels=None,
:param str marker: markers to use - cycle if more labels then markers are given
:param kwargs: the kwargs for the scatter plots
"""
input_1, input_2, input_3 = sig_dims = self.get_most_significant_input_dimensions(which_indices)
canvas, projection, kwargs, sig_dims = _new_canvas(self, projection, kwargs, which_indices)
canvas, kwargs = pl().new_canvas(projection=projection,
xlabel='latent dimension %i' % input_1,
ylabel='latent dimension %i' % input_2,
zlabel='latent dimension %i' % input_3, **kwargs)
X, _, _ = get_x_y_var(self)
if labels is None:
labels = np.ones(self.num_data)
@ -101,8 +108,6 @@ def plot_latent_scatter(self, labels=None,
return pl().add_to_canvas(canvas, dict(scatter=scatters), legend=legend)
def plot_latent_inducing(self,
which_indices=None,
legend=False,
@ -122,17 +127,8 @@ def plot_latent_inducing(self,
:param str marker: markers to use - cycle if more labels then markers are given
:param kwargs: the kwargs for the scatter plots
"""
input_1, input_2, input_3 = sig_dims = self.get_most_significant_input_dimensions(which_indices)
if input_3 is None: zlabel=None
else: zlabel = 'latent dimension %i' % input_3
canvas, projection, kwargs, sig_dims = _new_canvas(self, projection, kwargs, which_indices)
if 'color' not in kwargs:
kwargs['color'] = 'white'
canvas, kwargs = pl().new_canvas(projection=projection,
xlabel='latent dimension %i' % input_1,
ylabel='latent dimension %i' % input_2,
zlabel=zlabel, **kwargs)
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)
@ -167,7 +163,8 @@ def plot_magnification(self, labels=None, which_indices=None,
updates=False,
mean=True, covariance=True,
kern=None, num_samples=1000,
scatter_kwargs=None, **imshow_kwargs):
scatter_kwargs=None, plot_scatter=True,
**imshow_kwargs):
"""
Plot the magnification factor of the GP on the inputs. This is the
density of the GP as a gray scale.
@ -192,17 +189,20 @@ def plot_magnification(self, labels=None, which_indices=None,
_, _, Xgrid, _, _, xmin, xmax, resolution = helper_for_plot_data(self, X, plot_limits, which_indices, None, resolution)
canvas, imshow_kwargs = pl().new_canvas(xlim=(xmin[0], xmax[0]), ylim=(xmin[1], xmax[1]),
xlabel='latent dimension %i' % input_1, ylabel='latent dimension %i' % input_2, **imshow_kwargs)
if (labels is not None):
legend = find_best_layout_for_subplots(len(np.unique(labels)))[1]
else:
labels = np.ones(self.num_data)
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, Xgrid, xmin, xmax, resolution, updates, mean, covariance, kern, **imshow_kwargs)
retval = pl().add_to_canvas(canvas, dict(scatter=scatters, imshow=view),
plots = {}
if legend and plot_scatter:
if (labels is not None):
legend = find_best_layout_for_subplots(len(np.unique(labels)))[1]
else:
labels = np.ones(self.num_data)
legend = False
if plot_scatter:
plots['scatters'] = _plot_latent_scatter(canvas, X, which_indices, labels, marker, num_samples, projection='2d', **scatter_kwargs or {})
plots['view'] = _plot_magnification(self, canvas, which_indices, Xgrid, xmin, xmax, resolution, updates, mean, covariance, kern, **imshow_kwargs)
retval = pl().add_to_canvas(canvas, plots,
legend=legend,
)
_wait_for_updates(view, updates)
_wait_for_updates(plots['view'], updates)
return retval
@ -231,7 +231,7 @@ def plot_latent(self, labels=None, which_indices=None,
plot_limits=None,
updates=False,
kern=None, marker='<>^vsd',
num_samples=1000,
num_samples=1000, projection='2d',
scatter_kwargs=None, **imshow_kwargs):
"""
Plot the latent space of the GP on the inputs. This is the
@ -251,16 +251,19 @@ def plot_latent(self, labels=None, which_indices=None,
:param imshow_kwargs: the kwargs for the imshow (magnification factor)
:param scatter_kwargs: the kwargs for the scatter plots
"""
if projection != '2d':
raise ValueError('Cannot plot latent in other then 2 dimensions, consider plot_scatter')
input_1, input_2 = which_indices = self.get_most_significant_input_dimensions(which_indices)[:2]
X = get_x_y_var(self)[0]
_, _, Xgrid, _, _, xmin, xmax, resolution = helper_for_plot_data(self, X, plot_limits, which_indices, None, resolution)
canvas, imshow_kwargs = pl().new_canvas(xlim=(xmin[0], xmax[0]), ylim=(xmin[1], xmax[1]),
xlabel='latent dimension %i' % input_1, ylabel='latent dimension %i' % input_2, **imshow_kwargs)
if (labels is not None):
legend = find_best_layout_for_subplots(len(np.unique(labels)))[1]
else:
labels = np.ones(self.num_data)
legend = False
if legend:
if (labels is not None):
legend = find_best_layout_for_subplots(len(np.unique(labels)))[1]
else:
labels = np.ones(self.num_data)
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)
retval = pl().add_to_canvas(canvas, dict(scatter=scatters, imshow=view), legend=legend)

View file

@ -203,7 +203,7 @@ def subsample_X(X, labels, num_samples=1000):
num_samples and the returned subsampled X.
"""
if X.shape[0] > num_samples:
print("Warning: subsampling X, as it has more samples then 1000. X.shape={!s}".format(X.shape))
print("Warning: subsampling X, as it has more samples then {}. X.shape={!s}".format(int(num_samples), X.shape))
if labels is not None:
subsample = []
for _, _, _, _, index, _ in scatter_label_generator(labels, X, (0, None, None)):
@ -289,7 +289,10 @@ def get_x_y_var(model):
X = model.X.mean.values
X_variance = model.X.variance.values
else:
X = model.X.values
try:
X = model.X.values
except AttributeError:
X = model.X
X_variance = None
try:
Y = model.Y.values
@ -352,7 +355,7 @@ def x_frame1D(X,plot_limits=None,resolution=None):
xmin,xmax = X.min(0),X.max(0)
xmin, xmax = xmin-0.25*(xmax-xmin), xmax+0.25*(xmax-xmin)
elif len(plot_limits) == 2:
xmin, xmax = plot_limits
xmin, xmax = map(np.atleast_1d, plot_limits)
else:
raise ValueError("Bad limits for plotting")