diff --git a/GPy/models/bayesian_gplvm.py b/GPy/models/bayesian_gplvm.py index b806ea31..7a22b5ea 100644 --- a/GPy/models/bayesian_gplvm.py +++ b/GPy/models/bayesian_gplvm.py @@ -3,14 +3,12 @@ import numpy as np import itertools -from matplotlib import pyplot from gplvm import GPLVM from .. import kern from ..core import SparseGP from ..likelihoods import Gaussian from ..inference.optimization import SCG -from ..util import plot_latent, linalg -from ..util.plot_latent import most_significant_input_dimensions +from ..util import linalg from ..core.parameterization.variational import Normal class BayesianGPLVM(SparseGP, GPLVM): @@ -75,11 +73,11 @@ class BayesianGPLVM(SparseGP, GPLVM): # """ # Horizontally stacks the parameters in order to present them to the optimizer. # The resulting 1-input_dim array has this structure: -# +# # =============================================================== # | mu | S | Z | theta | beta | # =============================================================== -# +# # """ # x = np.hstack((self.X.flatten(), self.X_variance.flatten(), SparseGP._get_params(self))) # return x @@ -131,7 +129,13 @@ class BayesianGPLVM(SparseGP, GPLVM): # return np.hstack((self.dbound_dmuS.flatten(), self.dbound_dZtheta)) def plot_latent(self, plot_inducing=True, *args, **kwargs): - return plot_latent.plot_latent(self, plot_inducing=plot_inducing, *args, **kwargs) + """ + See GPy.plotting.matplot_dep.dim_reduction_plots.plot_latent + """ + assert "matplotlib" in sys.modules, "matplotlib package has not been imported." + from ..plotting.matplot_dep import dim_reduction_plots + + return dim_reduction_plots.plot_latent(self, plot_inducing=plot_inducing, *args, **kwargs) def do_test_latents(self, Y): """ @@ -190,65 +194,14 @@ class BayesianGPLVM(SparseGP, GPLVM): dK_dX[:, i] = self.kern.dK_dX(ones, Xnew, self.Z[i:i + 1, :]).sum(-1) return np.dot(dK_dX, self.Cpsi1Vf) - def plot_steepest_gradient_map(self, fignum=None, ax=None, which_indices=None, labels=None, data_labels=None, data_marker='o', data_s=40, resolution=20, aspect='auto', updates=False, ** kwargs): - input_1, input_2 = significant_dims = most_significant_input_dimensions(self, which_indices) + def plot_steepest_gradient_map(self, *args, ** kwargs): + """ + See GPy.plotting.matplot_dep.dim_reduction_plots.plot_steepest_gradient_map + """ + assert "matplotlib" in sys.modules, "matplotlib package has not been imported." + from ..plotting.matplot_dep import dim_reduction_plots - X = np.zeros((resolution ** 2, self.input_dim)) - indices = np.r_[:X.shape[0]] - if labels is None: - labels = range(self.output_dim) - - def plot_function(x): - X[:, significant_dims] = x - dmu_dX = self.dmu_dXnew(X) - argmax = np.argmax(dmu_dX, 1) - return dmu_dX[indices, argmax], np.array(labels)[argmax] - - if ax is None: - fig = pyplot.figure(num=fignum) - ax = fig.add_subplot(111) - - if data_labels is None: - data_labels = np.ones(self.num_data) - ulabels = [] - for lab in data_labels: - if not lab in ulabels: - ulabels.append(lab) - marker = itertools.cycle(list(data_marker)) - from GPy.util import Tango - for i, ul in enumerate(ulabels): - if type(ul) is np.string_: - this_label = ul - elif type(ul) is np.int64: - this_label = 'class %i' % ul - else: - this_label = 'class %i' % i - m = marker.next() - index = np.nonzero(data_labels == ul)[0] - x = self.X[index, input_1] - y = self.X[index, input_2] - ax.scatter(x, y, marker=m, s=data_s, color=Tango.nextMedium(), label=this_label) - - ax.set_xlabel('latent dimension %i' % input_1) - ax.set_ylabel('latent dimension %i' % input_2) - - from matplotlib.cm import get_cmap - from GPy.util.latent_space_visualizations.controllers.imshow_controller import ImAnnotateController - controller = ImAnnotateController(ax, - plot_function, - tuple(self.X.min(0)[:, significant_dims]) + tuple(self.X.max(0)[:, significant_dims]), - resolution=resolution, - aspect=aspect, - cmap=get_cmap('jet'), - **kwargs) - ax.legend() - ax.figure.tight_layout() - if updates: - pyplot.show() - clear = raw_input('Enter to continue') - if clear.lower() in 'yes' or clear == '': - controller.deactivate() - return controller.view + return dim_reduction_plots.plot_steepest_gradient_map(model,*args,**kwargs) def latent_cost_and_grad(mu_S, kern, Z, dL_dpsi0, dL_dpsi1, dL_dpsi2): """ @@ -304,5 +257,3 @@ def latent_grad(mu_S, kern, Z, dL_dpsi0, dL_dpsi1, dL_dpsi2): dlnS = S * (S0 + S1 + S2 - 0.5) + .5 return -np.hstack((dmu.flatten(), dlnS.flatten())) - -