From f469ce8f417d5b30f0d080597132498152445794 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Mon, 29 Jul 2013 15:18:22 +0100 Subject: [PATCH] ard and latent plotting improved --- GPy/kern/kern.py | 19 +++++++------ GPy/util/plot_latent.py | 60 ++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/GPy/kern/kern.py b/GPy/kern/kern.py index b2b99094..be715335 100644 --- a/GPy/kern/kern.py +++ b/GPy/kern/kern.py @@ -79,6 +79,8 @@ class kern(Parameterized): if ax is None: fig = pb.figure(fignum) ax = fig.add_subplot(111) + else: + fig = ax.figure from GPy.util import Tango from matplotlib.textpath import TextPath Tango.reset() @@ -124,15 +126,16 @@ class kern(Parameterized): # ax.set_xticklabels(xticklabels, rotation=17) ax.set_xticks([]) ax.set_xlim(-.5, x0 - .5) - if title is '': - mode = 'expand' - if len(bars) > 1: + if legend: + if title is '': mode = 'expand' - ax.legend(bbox_to_anchor=(0., 1.02, 1., 1.02), loc=3, - ncol=len(bars), mode=mode, borderaxespad=0.) - fig.tight_layout(rect=(0, 0, 1, .9)) - else: - ax.legend() + if len(bars) > 1: + mode = 'expand' + ax.legend(bbox_to_anchor=(0., 1.02, 1., 1.02), loc=3, + ncol=len(bars), mode=mode, borderaxespad=0.) + fig.tight_layout(rect=(0, 0, 1, .9)) + else: + ax.legend() return ax def _transform_gradients(self, g): diff --git a/GPy/util/plot_latent.py b/GPy/util/plot_latent.py index d89d90d8..4a19ea5b 100644 --- a/GPy/util/plot_latent.py +++ b/GPy/util/plot_latent.py @@ -1,19 +1,10 @@ import pylab as pb import numpy as np from .. import util +from GPy.util.latent_space_visualizations.controllers.imshow_controller import ImshowController +import itertools -def plot_latent(model, labels=None, which_indices=None, resolution=50, ax=None, marker='o', s=40, fignum=None, plot_inducing=False, legend=True): - """ - :param labels: a np.array of size model.num_data containing labels for the points (can be number, strings, etc) - :param resolution: the resolution of the grid on which to evaluate the predictive variance - """ - if ax is None: - fig = pb.figure(num=fignum) - ax = fig.add_subplot(111) - util.plot.Tango.reset() - - if labels is None: - labels = np.ones(model.num_data) +def most_significant_input_dimensions(model, which_indices): if which_indices is None: if model.input_dim == 1: input_1 = 0 @@ -27,15 +18,40 @@ def plot_latent(model, labels=None, which_indices=None, resolution=50, ax=None, raise ValueError, "cannot Atomatically determine which dimensions to plot, please pass 'which_indices'" else: input_1, input_2 = which_indices + return input_1, input_2 + +def plot_latent(model, labels=None, which_indices=None, + resolution=50, ax=None, marker='o', s=40, + fignum=None, plot_inducing=False, legend=True, + aspect='auto', updates=False): + """ + :param labels: a np.array of size model.num_data containing labels for the points (can be number, strings, etc) + :param resolution: the resolution of the grid on which to evaluate the predictive variance + """ + if ax is None: + fig = pb.figure(num=fignum) + ax = fig.add_subplot(111) + util.plot.Tango.reset() + + if labels is None: + labels = np.ones(model.num_data) + + input_1, input_2 = most_significant_input_dimensions(model, which_indices) # first, plot the output variance as a function of the latent space Xtest, xx, yy, xmin, xmax = util.plot.x_frame2D(model.X[:, [input_1, input_2]], resolution=resolution) Xtest_full = np.zeros((Xtest.shape[0], model.X.shape[1])) - Xtest_full[:, :2] = Xtest - mu, var, low, up = model.predict(Xtest_full) - var = var[:, :1] - ax.imshow(var.reshape(resolution, resolution).T, - extent=[xmin[0], xmax[0], xmin[1], xmax[1]], cmap=pb.cm.binary, interpolation='bilinear', origin='lower') + def plot_function(x): + Xtest_full[:, [input_1, input_2]] = x + mu, var, low, up = model.predict(Xtest_full) + var = var[:, :1] + return var + view = ImshowController(ax, plot_function, tuple(xmin) + tuple(xmax), + resolution, aspect=aspect, interpolation='bilinear', + cmap=pb.cm.binary) + +# ax.imshow(var.reshape(resolution, resolution).T, +# extent=[xmin[0], xmax[0], xmin[1], xmax[1]], cmap=pb.cm.binary, interpolation='bilinear', origin='lower') # make sure labels are in order of input: ulabels = [] @@ -43,6 +59,8 @@ def plot_latent(model, labels=None, which_indices=None, resolution=50, ax=None, if not lab in ulabels: ulabels.append(lab) + marker = itertools.cycle(list(marker)) + for i, ul in enumerate(ulabels): if type(ul) is np.string_: this_label = ul @@ -50,10 +68,7 @@ def plot_latent(model, labels=None, which_indices=None, resolution=50, ax=None, this_label = 'class %i' % ul else: this_label = 'class %i' % i - if len(marker) == len(ulabels): - m = marker[i] - else: - m = marker + m = marker.next() index = np.nonzero(labels == ul)[0] if model.input_dim == 1: @@ -78,4 +93,7 @@ def plot_latent(model, labels=None, which_indices=None, resolution=50, ax=None, if plot_inducing: ax.plot(model.Z[:, input_1], model.Z[:, input_2], '^w') + if updates: + ax.figure.canvas.show() + raw_input('Enter to continue') return ax