diff --git a/GPy/plotting/gpy_plot/latent_plots.py b/GPy/plotting/gpy_plot/latent_plots.py index bd19fb29..a8bb2be6 100644 --- a/GPy/plotting/gpy_plot/latent_plots.py +++ b/GPy/plotting/gpy_plot/latent_plots.py @@ -238,3 +238,67 @@ def plot_latent(self, labels=None, which_indices=None, plots = pl.show_canvas(canvas, dict(scatter=scatters, imshow=view), legend=legend, xlim=(xmin[0], xmax[0]), ylim=(xmin[1], xmax[1])) _wait_for_updates(view, updates) return plots + + +def plot_steepest_gradient_map(self, labels=None, which_indices=None, + resolution=60, legend=True, + plot_limits=None, + updates=False, + kern=None, marker='<>^vsd', + num_samples=1000, + imshow_kwargs=None, **kwargs): + + """ + Plot the latent space of the GP on the inputs. This is the + density of the GP posterior as a grey scale and the + scatter plot of the input dimemsions selected by which_indices. + + :param array-like labels: a label for each data point (row) of the inputs + :param (int, int) which_indices: which input dimensions to plot against each other + :param int resolution: the resolution at which we predict the magnification factor + :param bool legend: whether to plot the legend on the figure + :param plot_limits: the plot limits for the plot + :type plot_limits: (xmin, xmax, ymin, ymax) or ((xmin, xmax), (ymin, ymax)) + :param bool updates: if possible, make interactive updates using the specific library you are using + :param :py:class:`~GPy.kern.Kern` kern: the kernel to use for prediction + :param str marker: markers to use - cycle if more labels then markers are given + :param int num_samples: the number of samples to plot maximally. We do a stratified subsample from the labels, if the number of samples (in X) is higher then num_samples. + :param imshow_kwargs: the kwargs for the imshow (magnification factor) + :param kwargs: the kwargs for the scatter plots + """ + input_1, input_2 = self.get_most_significant_input_dimensions(which_indices) + + from .. import Tango + Tango.reset() + + if labels is None: + labels = np.ones(self.num_data) + legend = False # No legend if there is no labels given + + canvas, kwargs = pl.get_new_canvas(xlabel='latent dimension %i' % input_1, ylabel='latent dimension %i' % input_2, **kwargs) + + X, _, _, _, _, Xgrid, _, _, xmin, xmax, resolution = helper_for_plot_data(self, plot_limits, (input_1, input_2), None, resolution) + X, labels = subsample_X(X, labels) + + def plot_function(x): + X[:, [input_1, input_2]] = x + dmu_dX = self.predictive_gradients(X)[0] + argmax = np.argmax(dmu_dX, 1) + return dmu_dX[:, argmax], np.array(labels)[argmax] + + imshow_kwargs = update_not_existing_kwargs(imshow_kwargs, pl.defaults.latent) + Y = plot_function(Xgrid[:, [input_1, input_2]]).reshape(resolution, resolution).T[::-1, :] + view = pl.imshow(canvas, Y, + (xmin[0], xmin[1], xmax[1], xmax[1]), + None, plot_function, resolution, + vmin=Y.min(), vmax=Y.max(), + **imshow_kwargs) + + scatters = [] + for x, y, this_label, _, m in scatter_label_generator(labels, X, input_1, input_2, marker): + update_not_existing_kwargs(kwargs, pl.defaults.latent_scatter) + scatters.append(pl.scatter(canvas, x, y, marker=m, color=Tango.nextMedium(), label=this_label, **kwargs)) + + plots = pl.show_canvas(canvas, dict(scatter=scatters, imshow=view), legend=legend, xlim=(xmin[0], xmax[0]), ylim=(xmin[1], xmax[1])) + _wait_for_updates(view, updates) + return plots \ No newline at end of file diff --git a/GPy/plotting/matplot_dep/dim_reduction_plots.py b/GPy/plotting/matplot_dep/dim_reduction_plots.py index 25b5d45b..1b12f337 100644 --- a/GPy/plotting/matplot_dep/dim_reduction_plots.py +++ b/GPy/plotting/matplot_dep/dim_reduction_plots.py @@ -357,7 +357,7 @@ def plot_steepest_gradient_map(model, fignum=None, ax=None, which_indices=None, def plot_function(x): X[:, significant_dims] = x - dmu_dX = model.dmu_dXnew(X) + dmu_dX = model.predictive_gradients(X)[0] argmax = np.argmax(dmu_dX, 1) return dmu_dX[indices, argmax], np.array(labels)[argmax] diff --git a/GPy/testing/plotting_tests.py b/GPy/testing/plotting_tests.py index 4d2cb39d..24e64ccd 100644 --- a/GPy/testing/plotting_tests.py +++ b/GPy/testing/plotting_tests.py @@ -88,8 +88,7 @@ def _image_comparison(baseline_images, extensions=['pdf','svg','ong'], tol=1e-3) 'image does not exist: %s' % expected) if err: raise ImageComparisonFailure( - 'images not close: %(actual)s vs. %(expected)s ' - '(RMS %(rms).3f)'%err) + 'images not close: {err[actual]!s} vs. {err[expected]!s} (RMS {err[rms]:.3f})'.format(err=err)) except ImageComparisonFailure: pass yield do_test diff --git a/travis_tests.py b/travis_tests.py index 2b9d074f..36dc1568 100644 --- a/travis_tests.py +++ b/travis_tests.py @@ -32,7 +32,7 @@ #!/usr/bin/env python import matplotlib -matplotlib.use('pdf') +matplotlib.use('png') matplotlib.rcParams[u'figure.figsize'] = (4,3) matplotlib.rcParams[u'text.usetex'] = False