diff --git a/GPy/plotting/gpy_plot/gp_plots.py b/GPy/plotting/gpy_plot/gp_plots.py index da92748d..4d467e62 100644 --- a/GPy/plotting/gpy_plot/gp_plots.py +++ b/GPy/plotting/gpy_plot/gp_plots.py @@ -393,7 +393,7 @@ def plot_f(self, plot_limits=None, fixed_inputs=None, 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) + plot_density, predict_kw, projection, legend, **kwargs) diff --git a/GPy/plotting/gpy_plot/latent_plots.py b/GPy/plotting/gpy_plot/latent_plots.py index 2e5c7148..240f35ae 100644 --- a/GPy/plotting/gpy_plot/latent_plots.py +++ b/GPy/plotting/gpy_plot/latent_plots.py @@ -123,13 +123,16 @@ def plot_latent_inducing(self, :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 + 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='latent dimension %i' % input_3, **kwargs) + 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) @@ -195,7 +198,7 @@ def plot_magnification(self, labels=None, which_indices=None, 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[:2], Xgrid, xmin, xmax, resolution, updates, mean, covariance, kern, **imshow_kwargs) + 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), legend=legend, ) diff --git a/GPy/plotting/gpy_plot/plot_util.py b/GPy/plotting/gpy_plot/plot_util.py index 715ca759..254886a2 100644 --- a/GPy/plotting/gpy_plot/plot_util.py +++ b/GPy/plotting/gpy_plot/plot_util.py @@ -131,6 +131,8 @@ def helper_for_plot_data(self, X, plot_limits, visible_dims, fixed_inputs, resol Xnew, x, y, xmin, xmax = x_frame2D(X[:,free_dims], plot_limits, resolution) Xgrid = np.zeros((Xnew.shape[0], self.input_dim)) Xgrid[:,free_dims] = Xnew + #xmin = Xgrid.min(0)[free_dims] + #xmax = Xgrid.max(0)[free_dims] for i,v in fixed_inputs: Xgrid[:,i] = v else: @@ -305,7 +307,7 @@ def get_free_dims(model, visible_dims, fixed_dims): visible_dims = np.arange(model.input_dim) dims = np.asanyarray(visible_dims) if fixed_dims is not None: - dims = np.setdiff1d(dims, fixed_dims) + dims = [dim for dim in dims if dim not in fixed_dims] return np.asanyarray([dim for dim in dims if dim is not None]) @@ -357,7 +359,7 @@ def x_frame2D(X,plot_limits=None,resolution=None): """ assert X.shape[1]==2, "x_frame2D is defined for two-dimensional inputs" if plot_limits is None: - xmin, xmax = X.min(0),X.max(0) + xmin, xmax = X.min(0), X.max(0) xmin, xmax = xmin-0.075*(xmax-xmin), xmax+0.075*(xmax-xmin) elif len(plot_limits) == 2: xmin, xmax = plot_limits diff --git a/GPy/testing/baseline/bayesian_gplvm_gradient.png b/GPy/testing/baseline/bayesian_gplvm_gradient.png index ccc72002..365052ef 100644 Binary files a/GPy/testing/baseline/bayesian_gplvm_gradient.png and b/GPy/testing/baseline/bayesian_gplvm_gradient.png differ diff --git a/GPy/testing/baseline/bayesian_gplvm_inducing.png b/GPy/testing/baseline/bayesian_gplvm_inducing.png index 9ef57a9a..c6f928bc 100644 Binary files a/GPy/testing/baseline/bayesian_gplvm_inducing.png and b/GPy/testing/baseline/bayesian_gplvm_inducing.png differ diff --git a/GPy/testing/baseline/bayesian_gplvm_inducing_3d.png b/GPy/testing/baseline/bayesian_gplvm_inducing_3d.png index 6f46d423..5675149f 100644 Binary files a/GPy/testing/baseline/bayesian_gplvm_inducing_3d.png and b/GPy/testing/baseline/bayesian_gplvm_inducing_3d.png differ diff --git a/GPy/testing/baseline/bayesian_gplvm_latent.png b/GPy/testing/baseline/bayesian_gplvm_latent.png new file mode 100644 index 00000000..6de7bc38 Binary files /dev/null and b/GPy/testing/baseline/bayesian_gplvm_latent.png differ diff --git a/GPy/testing/baseline/bayesian_gplvm_latent_3d.png b/GPy/testing/baseline/bayesian_gplvm_latent_3d.png index db81f483..9eb87cc3 100644 Binary files a/GPy/testing/baseline/bayesian_gplvm_latent_3d.png and b/GPy/testing/baseline/bayesian_gplvm_latent_3d.png differ diff --git a/GPy/testing/baseline/bayesian_gplvm_magnification.png b/GPy/testing/baseline/bayesian_gplvm_magnification.png index ebb624a6..2ddab8ce 100644 Binary files a/GPy/testing/baseline/bayesian_gplvm_magnification.png and b/GPy/testing/baseline/bayesian_gplvm_magnification.png differ diff --git a/GPy/testing/baseline/gplvm_gradient.png b/GPy/testing/baseline/gplvm_gradient.png index 2dd49320..5b6616ca 100644 Binary files a/GPy/testing/baseline/gplvm_gradient.png and b/GPy/testing/baseline/gplvm_gradient.png differ diff --git a/GPy/testing/baseline/gplvm_latent.png b/GPy/testing/baseline/gplvm_latent.png index eb58a709..81ddd70e 100644 Binary files a/GPy/testing/baseline/gplvm_latent.png and b/GPy/testing/baseline/gplvm_latent.png differ diff --git a/GPy/testing/baseline/gplvm_latent_3d.png b/GPy/testing/baseline/gplvm_latent_3d.png index da040371..9eb87cc3 100644 Binary files a/GPy/testing/baseline/gplvm_latent_3d.png and b/GPy/testing/baseline/gplvm_latent_3d.png differ diff --git a/GPy/testing/baseline/gplvm_magnification.png b/GPy/testing/baseline/gplvm_magnification.png index 2f09cf3c..55d0702d 100644 Binary files a/GPy/testing/baseline/gplvm_magnification.png and b/GPy/testing/baseline/gplvm_magnification.png differ diff --git a/GPy/testing/plotting_tests.py b/GPy/testing/plotting_tests.py index ecbb4d7e..d7250cec 100644 --- a/GPy/testing/plotting_tests.py +++ b/GPy/testing/plotting_tests.py @@ -311,26 +311,35 @@ def test_gplvm(): from ..examples.dimensionality_reduction import _simulate_matern from ..kern import RBF from ..models import GPLVM - np.random.seed(11111) - import matplotlib + np.random.seed(12345) matplotlib.rcParams.update(matplotlib.rcParamsDefault) matplotlib.rcParams[u'figure.figsize'] = (4,3) matplotlib.rcParams[u'text.usetex'] = False Q = 3 - _, _, Ylist = _simulate_matern(5, 1, 1, 100, num_inducing=5, plot_sim=False) - Y = Ylist[0] - k = RBF(Q, ARD=True) # + kern.white(Q, _np.exp(-2)) # + kern.bias(Q) + # Define dataset + N = 10 + k1 = GPy.kern.RBF(5, variance=1, lengthscale=1./np.random.dirichlet(np.r_[10,10,10,0.1,0.1]), ARD=True) + k2 = GPy.kern.RBF(5, variance=1, lengthscale=1./np.random.dirichlet(np.r_[10,0.1,10,0.1,10]), ARD=True) + k3 = GPy.kern.RBF(5, variance=1, lengthscale=1./np.random.dirichlet(np.r_[0.1,0.1,10,10,10]), ARD=True) + X = np.random.normal(0, 1, (N, 5)) + A = np.random.multivariate_normal(np.zeros(N), k1.K(X), Q).T + B = np.random.multivariate_normal(np.zeros(N), k2.K(X), Q).T + C = np.random.multivariate_normal(np.zeros(N), k3.K(X), Q).T + + Y = np.vstack((A,B,C)) + labels = np.hstack((np.zeros(A.shape[0]), np.ones(B.shape[0]), np.ones(C.shape[0])*2)) + + k = RBF(Q, ARD=True, lengthscale=2) # + kern.white(Q, _np.exp(-2)) # + kern.bias(Q) m = GPLVM(Y, Q, init="PCA", kernel=k) - m.likelihood.variance = .1 + m.likelihood.variance = .001 #m.optimize(messages=0) - labels = np.random.multinomial(1, np.random.dirichlet([.3333333, .3333333, .3333333]), size=(m.Y.shape[0])).nonzero()[1] np.random.seed(111) - m.plot_latent() + m.plot_latent(labels=labels) np.random.seed(111) m.plot_scatter(projection='3d', labels=labels) np.random.seed(111) m.plot_magnification(labels=labels) - m.plot_steepest_gradient_map(resolution=7) + m.plot_steepest_gradient_map(resolution=7, data_labels=labels) for do_test in _image_comparison(baseline_images=['gplvm_{}'.format(sub) for sub in ["latent", "latent_3d", "magnification", 'gradient']], extensions=extensions): yield (do_test, ) @@ -338,31 +347,41 @@ def test_bayesian_gplvm(): from ..examples.dimensionality_reduction import _simulate_matern from ..kern import RBF from ..models import BayesianGPLVM - import matplotlib + np.random.seed(12345) matplotlib.rcParams.update(matplotlib.rcParamsDefault) matplotlib.rcParams[u'figure.figsize'] = (4,3) matplotlib.rcParams[u'text.usetex'] = False - np.random.seed(111) Q = 3 - _, _, Ylist = _simulate_matern(5, 1, 1, 100, num_inducing=5, plot_sim=False) - Y = Ylist[0] - k = RBF(Q, ARD=True) # + kern.white(Q, _np.exp(-2)) # + kern.bias(Q) - # k = kern.RBF(Q, ARD=True, lengthscale=10.) + # Define dataset + N = 10 + k1 = GPy.kern.RBF(5, variance=1, lengthscale=1./np.random.dirichlet(np.r_[10,10,10,0.1,0.1]), ARD=True) + k2 = GPy.kern.RBF(5, variance=1, lengthscale=1./np.random.dirichlet(np.r_[10,0.1,10,0.1,10]), ARD=True) + k3 = GPy.kern.RBF(5, variance=1, lengthscale=1./np.random.dirichlet(np.r_[0.1,0.1,10,10,10]), ARD=True) + X = np.random.normal(0, 1, (N, 5)) + A = np.random.multivariate_normal(np.zeros(N), k1.K(X), Q).T + B = np.random.multivariate_normal(np.zeros(N), k2.K(X), Q).T + C = np.random.multivariate_normal(np.zeros(N), k3.K(X), Q).T + + Y = np.vstack((A,B,C)) + labels = np.hstack((np.zeros(A.shape[0]), np.ones(B.shape[0]), np.ones(C.shape[0])*2)) + + k = RBF(Q, ARD=True, lengthscale=2) # + kern.white(Q, _np.exp(-2)) # + kern.bias(Q) m = BayesianGPLVM(Y, Q, init="PCA", kernel=k) - m.likelihood.variance = .1 + m.likelihood.variance = .001 #m.optimize(messages=0) - labels = np.random.multinomial(1, np.random.dirichlet([.3333333, .3333333, .3333333]), size=(m.Y.shape[0])).nonzero()[1] np.random.seed(111) m.plot_inducing(projection='2d') np.random.seed(111) m.plot_inducing(projection='3d') np.random.seed(111) - m.plot_scatter(projection='3d') + m.plot_latent(projection='2d', labels=labels) + np.random.seed(111) + m.plot_scatter(projection='3d', labels=labels) np.random.seed(111) m.plot_magnification(labels=labels) np.random.seed(111) - m.plot_steepest_gradient_map(resolution=7) - for do_test in _image_comparison(baseline_images=['bayesian_gplvm_{}'.format(sub) for sub in ["inducing", "inducing_3d", "latent_3d", "magnification", 'gradient']], extensions=extensions): + m.plot_steepest_gradient_map(resolution=7, data_labels=labels) + for do_test in _image_comparison(baseline_images=['bayesian_gplvm_{}'.format(sub) for sub in ["inducing", "inducing_3d", "latent", "latent_3d", "magnification", 'gradient']], extensions=extensions): yield (do_test, ) if __name__ == '__main__':