diff --git a/GPy/inference/optimization/optimization.py b/GPy/inference/optimization/optimization.py index 9aab1bec..93a4b83f 100644 --- a/GPy/inference/optimization/optimization.py +++ b/GPy/inference/optimization/optimization.py @@ -12,7 +12,7 @@ except ImportError: rasm_available = False from .scg import SCG -class Optimizer(): +class Optimizer(object): """ Superclass for all the optimizers. @@ -56,16 +56,6 @@ class Optimizer(): def opt(self, f_fp=None, f=None, fp=None): raise NotImplementedError("this needs to be implemented to use the optimizer class") - def plot(self): - """ - See GPy.plotting.matplot_dep.inference_plots - """ - import sys - assert "matplotlib" in sys.modules, "matplotlib package has not been imported." - from ...plotting.matplot_dep import inference_plots - inference_plots.plot_optimizer(self) - - def __str__(self): diagnostics = "Optimizer: \t\t\t\t %s\n" % self.opt_name diagnostics += "f(x_opt): \t\t\t\t %.3f\n" % self.f_opt diff --git a/GPy/plotting/__init__.py b/GPy/plotting/__init__.py index d8de7b03..3fb6bcd2 100644 --- a/GPy/plotting/__init__.py +++ b/GPy/plotting/__init__.py @@ -13,7 +13,10 @@ try: import matplotlib from .matplot_dep import plot_definitions plotting_library = plot_definitions.MatplotlibPlots() - + if lib == 'plotly': + import plotly + from .plotly_dep import plot_definitions + plotting_library = plot_definitions.PlotlyPlots() #=========================================================================== except (ImportError, NameError): raise @@ -63,4 +66,6 @@ if config.get('plotting', 'library') is not 'none': Kern.plot_covariance = gpy_plot.kernel_plots.plot_covariance Kern.plot_covariance = gpy_plot.kernel_plots.plot_ARD + from ..inference.optimization import Optimizer + Optimizer.plot = gpy_plot.inference_plots.plot_optimizer # Variational plot! diff --git a/GPy/plotting/abstract_plotting_library.py b/GPy/plotting/abstract_plotting_library.py index 35353f36..4743f1bc 100644 --- a/GPy/plotting/abstract_plotting_library.py +++ b/GPy/plotting/abstract_plotting_library.py @@ -64,7 +64,7 @@ class AbstractPlottingLibrary(object): """ raise NotImplementedError("Implement all plot functions in AbstractPlottingLibrary in order to use your own plotting library") - def get_new_canvas(self, projection='2d', figure=None, col=1, row=1, **kwargs): + def new_canvas(self, figure=None, col=1, row=1, projection='2d', xlabel=None, ylabel=None, zlabel=None, title=None, xlim=None, ylim=None, zlim=None, **kwargs): """ Return a canvas, kwargupdate for your plotting library. @@ -84,7 +84,7 @@ class AbstractPlottingLibrary(object): """ raise NotImplementedError("Implement all plot functions in AbstractPlottingLibrary in order to use your own plotting library") - def show_canvas(self, canvas, plots, xlabel=None, ylabel=None, zlabel=None, title=None, xlim=None, ylim=None, zlim=None, legend=True, **kwargs): + def show_canvas(self, canvas, plots, legend=True, title=None, **kwargs): """ Show the canvas given. plots is a dictionary with the plots diff --git a/GPy/plotting/gpy_plot/__init__.py b/GPy/plotting/gpy_plot/__init__.py index 6a4b7391..48ab256b 100644 --- a/GPy/plotting/gpy_plot/__init__.py +++ b/GPy/plotting/gpy_plot/__init__.py @@ -1,3 +1,3 @@ from .. import plotting_library as pl -from . import data_plots, gp_plots, latent_plots, kernel_plots, plot_util +from . import data_plots, gp_plots, latent_plots, kernel_plots, plot_util, inference_plots diff --git a/GPy/plotting/gpy_plot/data_plots.py b/GPy/plotting/gpy_plot/data_plots.py index 0cfb4006..f5743aaa 100644 --- a/GPy/plotting/gpy_plot/data_plots.py +++ b/GPy/plotting/gpy_plot/data_plots.py @@ -54,7 +54,7 @@ def plot_data(self, which_data_rows='all', :returns list: of plots created. """ - canvas, plot_kwargs = pl.get_new_canvas(projection=projection, **plot_kwargs) + canvas, plot_kwargs = pl.new_canvas(projection=projection, **plot_kwargs) plots = _plot_data(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection, label, **plot_kwargs) return pl.show_canvas(canvas, plots) @@ -117,7 +117,7 @@ def plot_data_error(self, which_data_rows='all', :returns list: of plots created. """ - canvas, error_kwargs = pl.get_new_canvas(projection=='3d', **error_kwargs) + canvas, error_kwargs = pl.new_canvas(projection=='3d', **error_kwargs) plots = _plot_data_error(self, canvas, which_data_rows, which_data_ycols, visible_dims, projection, label, **error_kwargs) return pl.show_canvas(canvas, plots) @@ -165,7 +165,7 @@ def plot_inducing(self, visible_dims=None, projection='2d', label=None, **plot_k :param array-like visible_dims: an array specifying the input dimensions to plot (maximum two) :param kwargs plot_kwargs: keyword arguments for the plotting library """ - canvas, kwargs = pl.get_new_canvas(projection=projection, **plot_kwargs) + canvas, kwargs = pl.new_canvas(projection=projection, **plot_kwargs) plots = _plot_inducing(self, canvas, visible_dims, projection, label, **kwargs) return pl.show_canvas(canvas, plots) @@ -217,7 +217,7 @@ def plot_errorbars_trainset(self, which_data_rows='all', :param dict predict_kwargs: kwargs for the prediction used to predict the right quantiles. :param kwargs plot_kwargs: kwargs for the data plot for the plotting library you are using """ - canvas, kwargs = pl.get_new_canvas(projection=projection, **plot_kwargs) + canvas, kwargs = pl.new_canvas(projection=projection, **plot_kwargs) plots = _plot_errorbars_trainset(self, canvas, which_data_rows, which_data_ycols, fixed_inputs, plot_raw, apply_link, label, projection, predict_kw, **kwargs) return pl.show_canvas(canvas, plots) diff --git a/GPy/plotting/gpy_plot/gp_plots.py b/GPy/plotting/gpy_plot/gp_plots.py index 4a5ba7ea..222a4318 100644 --- a/GPy/plotting/gpy_plot/gp_plots.py +++ b/GPy/plotting/gpy_plot/gp_plots.py @@ -62,7 +62,7 @@ def plot_mean(self, plot_limits=None, fixed_inputs=None, :param str label: the label for the plot. :param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=) in here """ - canvas, kwargs = pl.get_new_canvas(projection=projection, **kwargs) + canvas, kwargs = pl.new_canvas(projection=projection, **kwargs) plots = _plot_mean(self, canvas, plot_limits, fixed_inputs, resolution, plot_raw, apply_link, visible_dims, which_data_ycols, @@ -131,7 +131,7 @@ def plot_confidence(self, lower=2.5, upper=97.5, plot_limits=None, fixed_inputs= :param array-like which_data_ycols: which columns of the output y (!) to plot (array-like or list of ints) :param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=) in here """ - canvas, kwargs = pl.get_new_canvas(**kwargs) + canvas, kwargs = pl.new_canvas(**kwargs) plots = _plot_confidence(self, canvas, lower, upper, plot_limits, fixed_inputs, resolution, plot_raw, apply_link, visible_dims, which_data_ycols, @@ -190,7 +190,7 @@ def plot_samples(self, plot_limits=None, fixed_inputs=None, :param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=) in here :param int levels: for 2D plotting, the number of contour levels to use is """ - canvas, kwargs = pl.get_new_canvas(projection=projection, **kwargs) + canvas, kwargs = pl.new_canvas(projection=projection, **kwargs) plots = _plot_samples(self, canvas, plot_limits, fixed_inputs, resolution, plot_raw, apply_link, visible_dims, which_data_ycols, samples, projection, @@ -253,7 +253,7 @@ def plot_density(self, plot_limits=None, fixed_inputs=None, :param int levels: the number of levels in the density (number bigger then 1, where 35 is smooth and 1 is the same as plot_confidence). You can go higher then 50 if the result is not smooth enough for you. :param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=) in here """ - canvas, kwargs = pl.get_new_canvas(**kwargs) + canvas, kwargs = pl.new_canvas(**kwargs) plots = _plot_density(self, canvas, plot_limits, fixed_inputs, resolution, plot_raw, apply_link, visible_dims, which_data_ycols, @@ -326,7 +326,7 @@ def plot(self, plot_limits=None, fixed_inputs=None, :param bool plot_density: plot density instead of the confidence interval? :param dict predict_kw: the keyword arguments for the prediction. If you want to plot a specific kernel give dict(kern=) in here """ - canvas, _ = pl.get_new_canvas(projection=projection, **kwargs) + canvas, _ = pl.new_canvas(projection=projection, **kwargs) plots = _plot(self, canvas, plot_limits, fixed_inputs, resolution, plot_raw, apply_link, which_data_ycols, which_data_rows, visible_dims, levels, samples, samples_likelihood, lower, upper, plot_data, @@ -375,7 +375,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 """ - canvas, _ = pl.get_new_canvas(projection=='3d', **kwargs) + canvas, _ = pl.new_canvas(projection=='3d', **kwargs) plots = _plot(self, canvas, plot_limits, fixed_inputs, resolution, True, apply_link, which_data_ycols, which_data_rows, visible_dims, levels, samples, 0, lower, upper, diff --git a/GPy/plotting/matplot_dep/inference_plots.py b/GPy/plotting/gpy_plot/inference_plots.py similarity index 53% rename from GPy/plotting/matplot_dep/inference_plots.py rename to GPy/plotting/gpy_plot/inference_plots.py index 430a3003..bbb75c3b 100644 --- a/GPy/plotting/matplot_dep/inference_plots.py +++ b/GPy/plotting/gpy_plot/inference_plots.py @@ -5,23 +5,24 @@ #import Tango #from base_plots import gpplot, x_frame1D, x_frame2D -from . import plotting_library as pl +from . import pl def plot_optimizer(optimizer, **kwargs): if optimizer.trace == None: print("No trace present so I can't plot it. Please check that the optimizer actually supplies a trace.") else: - canvas, kwargs = pl.get_new_canvas(**kwargs) + canvas, kwargs = pl.new_canvas(**kwargs) plots = dict(trace=pl.plot(range(len(optimizer.trace)), optimizer.trace)) return pl.show_canvas(canvas, plots, xlabel='Iteration', ylabel='f(x)') def plot_sgd_traces(optimizer): - pb.figure() - pb.subplot(211) - pb.title('Parameters') + figure = pl.figure(2,1) + canvas, _ = pl.new_canvas(figure, 1, 1, title="Parameters") + plots = dict(lines=[]) for k in optimizer.param_traces.keys(): - pb.plot(optimizer.param_traces[k], label=k) - pb.legend(loc=0) - pb.subplot(212) - pb.title('Objective function') - pb.plot(optimizer.fopt_trace) + plots['lines'].append(pl.plot(canvas, range(len(optimizer.param_traces[k])), optimizer.param_traces[k], label=k)) + pl.show_canvas(canvas, legend=True) + canvas, _ = pl.new_canvas(figure, 1, 2, title="Objective function") + pl.plot(canvas, range(len(optimizer.fopt_trace)), optimizer.fopt_trace) + return pl.show_canvas(canvas, plots, legend=True) + \ No newline at end of file diff --git a/GPy/plotting/gpy_plot/kernel_plots.py b/GPy/plotting/gpy_plot/kernel_plots.py index 26eb4b8c..e9b20196 100644 --- a/GPy/plotting/gpy_plot/kernel_plots.py +++ b/GPy/plotting/gpy_plot/kernel_plots.py @@ -41,7 +41,7 @@ def plot_ARD(kernel, filtering=None, **kwargs): will be used for plotting. :type filtering: list of names to use for ARD plot """ - canvas, kwargs = pl.get_new_canvas(kwargs) + canvas, kwargs = pl.new_canvas(kwargs) Tango.reset() diff --git a/GPy/plotting/gpy_plot/latent_plots.py b/GPy/plotting/gpy_plot/latent_plots.py index 576e747c..43e3f650 100644 --- a/GPy/plotting/gpy_plot/latent_plots.py +++ b/GPy/plotting/gpy_plot/latent_plots.py @@ -29,8 +29,8 @@ #=============================================================================== import numpy as np from . import pl -from .plot_util import get_x_y_var, get_which_data_ycols,\ - get_which_data_rows, update_not_existing_kwargs, helper_predict_with_model,\ +from .plot_util import get_x_y_var,\ + update_not_existing_kwargs, \ helper_for_plot_data, scatter_label_generator, subsample_X,\ find_best_layout_for_subplots @@ -83,7 +83,10 @@ def plot_latent_scatter(self, labels=None, """ input_1, input_2, input_3 = sig_dims = self.get_most_significant_input_dimensions(which_indices) - canvas, kwargs = pl.get_new_canvas(projection=projection, **kwargs) + 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) @@ -91,17 +94,7 @@ def plot_latent_scatter(self, labels=None, else: legend = find_best_layout_for_subplots(len(np.unique(labels)))[1] scatters = _plot_latent_scatter(canvas, X, sig_dims, labels, marker, num_samples, projection=projection, **kwargs) - if projection == '3d': - return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend, - xlabel='latent dimension %i' % input_1, - ylabel='latent dimension %i' % input_2, - zlabel='latent dimension %i' % input_3) - else: - return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend, - xlabel='latent dimension %i' % input_1, - ylabel='latent dimension %i' % input_2, - #zlabel='latent dimension %i' % input_3 - ) + return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend) def plot_latent_inducing(self, which_indices=None, legend=False, @@ -125,24 +118,17 @@ def plot_latent_inducing(self, if 'color' not in kwargs: kwargs['color'] = 'white' - canvas, kwargs = pl.get_new_canvas(projection=projection, **kwargs) + 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) 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) - if projection == '3d': - return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend, - xlabel='latent dimension %i' % input_1, - ylabel='latent dimension %i' % input_2, - zlabel='latent dimension %i' % input_3) - else: - return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend, - xlabel='latent dimension %i' % input_1, - ylabel='latent dimension %i' % input_2, - #zlabel='latent dimension %i' % input_3 - ) + return pl.show_canvas(canvas, dict(scatter=scatters), legend=legend) def _plot_magnification(self, canvas, which_indices, Xgrid, - xmin, xmax, resolution, + xmin, xmax, resolution, updates, mean=True, covariance=True, kern=None, **imshow_kwargs): @@ -153,9 +139,11 @@ def _plot_magnification(self, canvas, which_indices, Xgrid, return mf.reshape(resolution, resolution).T imshow_kwargs = update_not_existing_kwargs(imshow_kwargs, pl.defaults.magnification) try: - return pl.imshow_interact(canvas, plot_function, (xmin[0], xmax[0], xmin[1], xmax[1]), resolution=resolution, **imshow_kwargs) + if updates: + return pl.imshow_interact(canvas, plot_function, (xmin[0], xmax[0], xmin[1], xmax[1]), resolution=resolution, **imshow_kwargs) + else: raise NotImplementedError except NotImplementedError: - return pl.imshow(canvas, plot_function(Xgrid), (xmin[0], xmax[0], xmin[1], xmax[1]), **imshow_kwargs) + return pl.imshow(canvas, plot_function(Xgrid[:, which_indices]), (xmin[0], xmax[0], xmin[1], xmax[1]), **imshow_kwargs) def plot_magnification(self, labels=None, which_indices=None, resolution=60, marker='<>^vsd', legend=True, @@ -184,19 +172,19 @@ def plot_magnification(self, labels=None, which_indices=None, :param kwargs: the kwargs for the scatter plots """ input_1, input_2 = which_indices = self.get_most_significant_input_dimensions(which_indices)[:2] - canvas, imshow_kwargs = pl.get_new_canvas(**imshow_kwargs) X, _, _, _, _, Xgrid, _, _, xmin, xmax, resolution = helper_for_plot_data(self, 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[:2], Xgrid, xmin, xmax, resolution, mean, covariance, kern, **imshow_kwargs) + view = _plot_magnification(self, canvas, which_indices[:2], Xgrid, xmin, xmax, resolution, updates, mean, covariance, kern, **imshow_kwargs) plots = pl.show_canvas(canvas, dict(scatter=scatters, imshow=view), legend=legend, - xlim=(xmin[0], xmax[0]), ylim=(xmin[1], xmax[1]), - xlabel='latent dimension %i' % input_1, ylabel='latent dimension %i' % input_2) + ) _wait_for_updates(view, updates) return plots @@ -204,7 +192,7 @@ def plot_magnification(self, labels=None, which_indices=None, def _plot_latent(self, canvas, which_indices, Xgrid, - xmin, xmax, resolution, + xmin, xmax, resolution, updates, kern=None, **imshow_kwargs): def plot_function(x): @@ -215,9 +203,11 @@ def _plot_latent(self, canvas, which_indices, Xgrid, imshow_kwargs = update_not_existing_kwargs(imshow_kwargs, pl.defaults.latent) try: - return pl.imshow_interact(canvas, plot_function, (xmin[0], xmax[0], xmin[1], xmax[1]), resolution=resolution, **imshow_kwargs) + if updates: + return pl.imshow_interact(canvas, plot_function, (xmin[0], xmax[0], xmin[1], xmax[1]), resolution=resolution, **imshow_kwargs) + else: raise NotImplementedError except NotImplementedError: - return pl.imshow(canvas, plot_function(Xgrid), (xmin[0], xmax[0], xmin[1], xmax[1]), **imshow_kwargs) + return pl.imshow(canvas, plot_function(Xgrid[:, which_indices]), (xmin[0], xmax[0], xmin[1], xmax[1]), **imshow_kwargs) def plot_latent(self, labels=None, which_indices=None, resolution=60, legend=True, @@ -245,24 +235,22 @@ def plot_latent(self, labels=None, which_indices=None, :param scatter_kwargs: the kwargs for the scatter plots """ input_1, input_2 = which_indices = self.get_most_significant_input_dimensions(which_indices)[:2] - canvas, imshow_kwargs = pl.get_new_canvas(**imshow_kwargs) X, _, _, _, _, Xgrid, _, _, xmin, xmax, resolution = helper_for_plot_data(self, 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_latent(self, canvas, which_indices, Xgrid, xmin, xmax, resolution, kern, **imshow_kwargs) - plots = pl.show_canvas(canvas, dict(scatter=scatters, imshow=view), - legend=legend, - xlim=(xmin[0], xmax[0]), ylim=(xmin[1], xmax[1]), - xlabel='latent dimension %i' % input_1, ylabel='latent dimension %i' % input_2) + view = _plot_latent(self, canvas, which_indices, Xgrid, xmin, xmax, resolution, updates, kern, **imshow_kwargs) + plots = pl.show_canvas(canvas, dict(scatter=scatters, imshow=view), legend=legend) _wait_for_updates(view, updates) return plots def _plot_steepest_gradient_map(self, canvas, which_indices, Xgrid, - xmin, xmax, resolution, output_labels, + xmin, xmax, resolution, output_labels, updates, kern=None, annotation_kwargs=None, **imshow_kwargs): if output_labels is None: @@ -276,9 +264,12 @@ def _plot_steepest_gradient_map(self, canvas, which_indices, Xgrid, annotation_kwargs = update_not_existing_kwargs(annotation_kwargs or {}, pl.defaults.annotation) imshow_kwargs = update_not_existing_kwargs(imshow_kwargs or {}, pl.defaults.gradient) try: - return dict(annotation=pl.annotation_heatmap_interact(canvas, plot_function, (xmin[0], xmax[0], xmin[1], xmax[1]), resolution=resolution, imshow_kwargs=imshow_kwargs, **annotation_kwargs)) + if updates: + return dict(annotation=pl.annotation_heatmap_interact(canvas, plot_function, (xmin[0], xmax[0], xmin[1], xmax[1]), resolution=resolution, imshow_kwargs=imshow_kwargs, **annotation_kwargs)) + else: + raise NotImplementedError except NotImplementedError: - imshow, annotation = pl.annotation_heatmap(canvas, *plot_function(Xgrid), extent=(xmin[0], xmax[0], xmin[1], xmax[1]), imshow_kwargs=imshow_kwargs, **annotation_kwargs) + imshow, annotation = pl.annotation_heatmap(canvas, *plot_function(Xgrid[:, which_indices]), extent=(xmin[0], xmax[0], xmin[1], xmax[1]), imshow_kwargs=imshow_kwargs, **annotation_kwargs) return dict(heatmap=imshow, annotation=annotation) def plot_steepest_gradient_map(self, output_labels=None, data_labels=None, which_indices=None, @@ -309,20 +300,19 @@ def plot_steepest_gradient_map(self, output_labels=None, data_labels=None, which :param scatter_kwargs: the kwargs for the scatter plots """ input_1, input_2 = which_indices = self.get_most_significant_input_dimensions(which_indices)[:2] - canvas, imshow_kwargs = pl.get_new_canvas(**imshow_kwargs) X, _, _, _, _, Xgrid, _, _, xmin, xmax, resolution = helper_for_plot_data(self, 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 (data_labels is not None): legend = find_best_layout_for_subplots(len(np.unique(data_labels)))[1] else: data_labels = np.ones(self.num_data) legend = False plots = dict(scatter=_plot_latent_scatter(canvas, X, which_indices, data_labels, marker, num_samples, **scatter_kwargs or {})) - plots.update(_plot_steepest_gradient_map(self, canvas, which_indices, Xgrid, xmin, xmax, resolution, output_labels, kern, annotation_kwargs=annotation_kwargs, **imshow_kwargs)) - pl.show_canvas(canvas, plots, legend=legend, - xlim=(xmin[0], xmax[0]), ylim=(xmin[1], xmax[1]), - xlabel='latent dimension %i' % input_1, ylabel='latent dimension %i' % input_2) + plots.update(_plot_steepest_gradient_map(self, canvas, which_indices, Xgrid, xmin, xmax, resolution, output_labels, updates, kern, annotation_kwargs=annotation_kwargs, **imshow_kwargs)) + show = pl.show_canvas(canvas, plots, legend=legend) _wait_for_updates(plots['annotation'], updates) - return plots + return show diff --git a/GPy/plotting/matplot_dep/controllers/imshow_controller.py b/GPy/plotting/matplot_dep/controllers/imshow_controller.py index d4403f94..a0eb3bef 100644 --- a/GPy/plotting/matplot_dep/controllers/imshow_controller.py +++ b/GPy/plotting/matplot_dep/controllers/imshow_controller.py @@ -9,7 +9,7 @@ import numpy class ImshowController(BufferedAxisChangedController): - def __init__(self, ax, plot_function, plot_limits, resolution=50, update_lim=.8, **kwargs): + def __init__(self, ax, plot_function, plot_limits, resolution=50, update_lim=.9, **kwargs): """ :param plot_function: function to use for creating image for plotting (return ndarray-like) @@ -23,7 +23,7 @@ class ImshowController(BufferedAxisChangedController): super(ImshowController, self).__init__(ax, plot_function, plot_limits, resolution, update_lim, **kwargs) def _init_view(self, canvas, X, xmin, xmax, ymin, ymax, vmin=None, vmax=None, **kwargs): - xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax) + xoffset, yoffset = 0, 0#self._offsets(xmin, xmax, ymin, ymax) return canvas.imshow(X, extent=(xmin-xoffset, xmax+xoffset, ymin-yoffset, ymax+yoffset), vmin=vmin, vmax=vmax, @@ -31,7 +31,7 @@ class ImshowController(BufferedAxisChangedController): def update_view(self, view, X, xmin, xmax, ymin, ymax): view.set_data(X) - xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax) + xoffset, yoffset = 0, 0#self._offsets(xmin, xmax, ymin, ymax) view.set_extent((xmin-xoffset, xmax+xoffset, ymin-yoffset, ymax+yoffset)) @@ -57,19 +57,19 @@ class ImAnnotateController(ImshowController): def _init_view(self, ax, X, xmin, xmax, ymin, ymax, **kwargs): view = [super(ImAnnotateController, self)._init_view(ax, X[0], xmin, xmax, ymin, ymax, **self.imshow_kwargs)] xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax) - xlin = numpy.linspace(xmin-xoffset, xmax+xoffset, self.resolution, endpoint=False) - ylin = numpy.linspace(ymin-yoffset, ymax+yoffset, self.resolution, endpoint=False) + xlin = numpy.linspace(xmin, xmax, self.resolution, endpoint=False) + ylin = numpy.linspace(ymin, ymax, self.resolution, endpoint=False) for [i, x], [j, y] in itertools.product(enumerate(xlin), enumerate(ylin)): - view.append(ax.text(x, y, "{}".format(X[1][j, i]), ha='center', va='center', **kwargs)) + view.append(ax.text(x+xoffset, y+yoffset, "{}".format(X[1][j, i]), ha='center', va='center', **kwargs)) return view def update_view(self, view, X, xmin, xmax, ymin, ymax): super(ImAnnotateController, self).update_view(view[0], X[0], xmin, xmax, ymin, ymax) xoffset, yoffset = self._offsets(xmin, xmax, ymin, ymax) - xlin = numpy.linspace(xmin-xoffset, xmax+xoffset, self.resolution, endpoint=False) - ylin = numpy.linspace(ymin-yoffset, ymax+yoffset, self.resolution, endpoint=False) + xlin = numpy.linspace(xmin, xmax, self.resolution, endpoint=False) + ylin = numpy.linspace(ymin, ymax, self.resolution, endpoint=False) for [[i, x], [j, y]], text in zip(itertools.product(enumerate(xlin), enumerate(ylin)), view[1:]): - text.set_x(x + xoffset) - text.set_y(y + yoffset) + text.set_x(x+xoffset) + text.set_y(y+yoffset) text.set_text("{}".format(X[1][j, i])) return view \ No newline at end of file diff --git a/GPy/plotting/matplot_dep/plot_definitions.py b/GPy/plotting/matplot_dep/plot_definitions.py index 1f32f346..4678f1a8 100644 --- a/GPy/plotting/matplot_dep/plot_definitions.py +++ b/GPy/plotting/matplot_dep/plot_definitions.py @@ -35,7 +35,7 @@ from . import defaults from matplotlib.colors import LinearSegmentedColormap from .controllers import ImshowController, ImAnnotateController import itertools -from GPy.plotting.matplot_dep.util import legend_ontop +from .util import legend_ontop class MatplotlibPlots(AbstractPlottingLibrary): def __init__(self): @@ -43,48 +43,52 @@ class MatplotlibPlots(AbstractPlottingLibrary): self._defaults = defaults.__dict__ def figure(self, rows=1, cols=1, **kwargs): - self.rows = rows - self.cols = cols - return plt.figure(**kwargs) + fig = plt.figure(**kwargs) + fig.rows = rows + fig.cols = cols + return fig - def get_new_canvas(self, projection='2d', figure=None, col=1, row=1, **kwargs): + def new_canvas(self, figure=None, col=1, row=1, projection='2d', xlabel=None, ylabel=None, zlabel=None, title=None, xlim=None, ylim=None, zlim=None, **kwargs): if projection == '3d': from mpl_toolkits.mplot3d import Axes3D elif projection == '2d': projection = None if 'ax' in kwargs: ax = kwargs.pop('ax') - elif 'num' in kwargs and 'figsize' in kwargs: - fig = self.figure(num=kwargs.pop('num'), figsize=kwargs.pop('figsize')) - elif 'num' in kwargs: - fig = self.figure(num=kwargs.pop('num')) - elif 'figsize' in kwargs: - fig = self.figure(figsize=kwargs.pop('figsize')) else: - fig = self.figure() + if 'num' in kwargs and 'figsize' in kwargs: + fig = self.figure(num=kwargs.pop('num'), figsize=kwargs.pop('figsize')) + elif 'num' in kwargs: + fig = self.figure(num=kwargs.pop('num')) + elif 'figsize' in kwargs: + fig = self.figure(figsize=kwargs.pop('figsize')) + else: + fig = self.figure() - ax = fig.add_subplot(self.rows, self.cols, ) + if hasattr(fig, 'rows') and hasattr(fig, 'cols'): + ax = fig.add_subplot(fig.rows, fig.cols, (col,row), projection=projection) + else: + ax = fig.add_subplot(1, 1, (1, 1), projection=projection) - return ax, kwargs - - def show_canvas(self, ax, plots, xlabel=None, ylabel=None, zlabel=None, title=None, xlim=None, ylim=None, zlim=None, legend=False, **kwargs): - ax.autoscale_view() - ax.set_xlim(xlim) - ax.set_ylim(ylim) + if xlim is not None: ax.set_xlim(xlim) + if ylim is not None: ax.set_ylim(ylim) if xlabel is not None: ax.set_xlabel(xlabel) if ylabel is not None: ax.set_ylabel(ylabel) - if zlabel is not None: ax.set_zlabel(zlabel) if title is not None: ax.set_title(title) + if projection == '3d': + if zlim is not None: ax.set_zlim(zlim) + if zlabel is not None: ax.set_zlabel(zlabel) + return ax, kwargs + + def show_canvas(self, ax, plots, legend=False, title=None, **kwargs): + ax.autoscale_view() fontdict=dict(family='sans-serif', weight='light', size=9) if legend is True: ax.legend(*ax.get_legend_handles_labels()) elif legend >= 1: #ax.legend(prop=fontdict) legend_ontop(ax, ncol=legend, fontdict=fontdict) - if zlim is not None: - ax.set_zlim(zlim) - ax.figure.canvas.draw() - ax.figure.show() + if title is not None: ax.figure.suptitle(title) ax.figure.canvas.draw() return plots @@ -158,12 +162,11 @@ class MatplotlibPlots(AbstractPlottingLibrary): extent = (0, X.shape[0], 0, X.shape[1]) xmin, xmax, ymin, ymax = extent xoffset, yoffset = (xmax - xmin) / (2. * X.shape[0]), (ymax - ymin) / (2. * X.shape[1]) - xmin, xmax, ymin, ymax = extent = xmin+xoffset, xmax-xoffset, ymin+yoffset, ymax-yoffset xlin = np.linspace(xmin, xmax, X.shape[0], endpoint=False) ylin = np.linspace(ymin, ymax, X.shape[1], endpoint=False) annotations = [] for [i, x], [j, y] in itertools.product(enumerate(xlin), enumerate(ylin)): - annotations.append(ax.text(x, y, "{}".format(annotation[j, i]), **annotation_kwargs)) + annotations.append(ax.text(x+xoffset, y+yoffset, "{}".format(annotation[j, i]), **annotation_kwargs)) return imshow, annotations def annotation_heatmap_interact(self, ax, plot_function, extent, label=None, resolution=15, imshow_kwargs=None, **annotation_kwargs): diff --git a/GPy/testing/plotting_tests.py b/GPy/testing/plotting_tests.py index e467c1f0..88aa32c6 100644 --- a/GPy/testing/plotting_tests.py +++ b/GPy/testing/plotting_tests.py @@ -30,14 +30,17 @@ import numpy as np import GPy, os from nose import SkipTest -from matplotlib.testing.compare import compare_images -from matplotlib.testing.noseclasses import ImageComparisonFailure + +from ..util.config import config + +if config.get('plotting', 'library') != 'matplotlib': + raise SkipTest("Matplotlib not installed, not testing plots") try: from matplotlib import cbook, pyplot as plt - import matplotlib - matplotlib.rcParams['text.usetex'] = False -except: + from matplotlib.testing.compare import compare_images + from matplotlib.testing.noseclasses import ImageComparisonFailure +except ImportError: raise SkipTest("Matplotlib not installed, not testing plots") extensions = ['png'] diff --git a/GPy/testing/plotting_tests/baseline/bayesian_gplvm_gradient.png b/GPy/testing/plotting_tests/baseline/bayesian_gplvm_gradient.png index 697862ad..6bcdb21e 100644 Binary files a/GPy/testing/plotting_tests/baseline/bayesian_gplvm_gradient.png and b/GPy/testing/plotting_tests/baseline/bayesian_gplvm_gradient.png differ diff --git a/GPy/testing/plotting_tests/baseline/bayesian_gplvm_inducing_3d.png b/GPy/testing/plotting_tests/baseline/bayesian_gplvm_inducing_3d.png index 1177a685..6f46d423 100644 Binary files a/GPy/testing/plotting_tests/baseline/bayesian_gplvm_inducing_3d.png and b/GPy/testing/plotting_tests/baseline/bayesian_gplvm_inducing_3d.png differ diff --git a/GPy/testing/plotting_tests/baseline/bayesian_gplvm_magnification.png b/GPy/testing/plotting_tests/baseline/bayesian_gplvm_magnification.png index f20865ab..f9614714 100644 Binary files a/GPy/testing/plotting_tests/baseline/bayesian_gplvm_magnification.png and b/GPy/testing/plotting_tests/baseline/bayesian_gplvm_magnification.png differ diff --git a/GPy/testing/plotting_tests/baseline/gplvm_gradient.png b/GPy/testing/plotting_tests/baseline/gplvm_gradient.png index 9ac2cc3a..6e513fc3 100644 Binary files a/GPy/testing/plotting_tests/baseline/gplvm_gradient.png and b/GPy/testing/plotting_tests/baseline/gplvm_gradient.png differ diff --git a/GPy/testing/plotting_tests/baseline/gplvm_latent.png b/GPy/testing/plotting_tests/baseline/gplvm_latent.png index 0d5d2704..4a336856 100644 Binary files a/GPy/testing/plotting_tests/baseline/gplvm_latent.png and b/GPy/testing/plotting_tests/baseline/gplvm_latent.png differ diff --git a/GPy/testing/plotting_tests/baseline/gplvm_magnification.png b/GPy/testing/plotting_tests/baseline/gplvm_magnification.png index 3962d9c0..0baac435 100644 Binary files a/GPy/testing/plotting_tests/baseline/gplvm_magnification.png and b/GPy/testing/plotting_tests/baseline/gplvm_magnification.png differ