From 57d2f9857079c16c2ddc80399df9afb2dbdfb864 Mon Sep 17 00:00:00 2001 From: mzwiessele Date: Sat, 10 Oct 2015 09:55:00 +0100 Subject: [PATCH] [inducing] 3d added --- GPy/installation.cfg | 4 +- GPy/plotting/__init__.py | 7 ++-- GPy/plotting/abstract_plotting_library.py | 2 +- GPy/plotting/gpy_plot/data_plots.py | 10 ++--- GPy/plotting/gpy_plot/gp_plots.py | 25 +++++++++++- GPy/plotting/plotly_dep/defaults.py | 8 ++-- GPy/plotting/plotly_dep/plot_definitions.py | 42 ++++++++++++++------- 7 files changed, 68 insertions(+), 30 deletions(-) diff --git a/GPy/installation.cfg b/GPy/installation.cfg index a9289933..d3b20c32 100644 --- a/GPy/installation.cfg +++ b/GPy/installation.cfg @@ -1,5 +1,5 @@ # This is the local installation configuration file for GPy [plotting] -#library = plotly -library = matplotlib +library = plotly +#library = matplotlib diff --git a/GPy/plotting/__init__.py b/GPy/plotting/__init__.py index e5af7a8c..5edbb9e0 100644 --- a/GPy/plotting/__init__.py +++ b/GPy/plotting/__init__.py @@ -22,10 +22,11 @@ def change_plotting_library(lib): current_lib[0] = None #=========================================================================== except (ImportError, NameError): - import warnings - warnings.warn(ImportWarning("{} not available, install newest version of {} for plotting".format(lib, lib))) config.set('plotting', 'library', 'none') - + raise + import warnings + #warnings.warn(ImportWarning("{} not available, install newest version of {} for plotting".format(lib, lib))) + from ..util.config import config lib = config.get('plotting', 'library') change_plotting_library(lib) diff --git a/GPy/plotting/abstract_plotting_library.py b/GPy/plotting/abstract_plotting_library.py index f46843cc..64061b99 100644 --- a/GPy/plotting/abstract_plotting_library.py +++ b/GPy/plotting/abstract_plotting_library.py @@ -277,4 +277,4 @@ class AbstractPlottingLibrary(object): the kwargs are plotting library specific kwargs! """ - raise NotImplementedError("Implement all plot functions in AbstractPlottingLibrary in order to use your own plotting library") + print("fill_gradient not implemented in this backend.") diff --git a/GPy/plotting/gpy_plot/data_plots.py b/GPy/plotting/gpy_plot/data_plots.py index d167d4da..a071a427 100644 --- a/GPy/plotting/gpy_plot/data_plots.py +++ b/GPy/plotting/gpy_plot/data_plots.py @@ -159,7 +159,7 @@ def _plot_data_error(self, canvas, which_data_rows='all', return plots -def plot_inducing(self, visible_dims=None, projection='2d', label=None, **plot_kwargs): +def plot_inducing(self, visible_dims=None, projection='2d', label='inducing', **plot_kwargs): """ Plot the inducing inputs of a sparse gp model @@ -168,7 +168,7 @@ def plot_inducing(self, visible_dims=None, projection='2d', label=None, **plot_k """ canvas, kwargs = pl().new_canvas(projection=projection, **plot_kwargs) plots = _plot_inducing(self, canvas, visible_dims, projection, label, **kwargs) - return pl().add_to_canvas(canvas, plots) + return pl().add_to_canvas(canvas, plots, legend=label is not None) def _plot_inducing(self, canvas, visible_dims, projection, label, **plot_kwargs): if visible_dims is None: @@ -182,15 +182,15 @@ def _plot_inducing(self, canvas, visible_dims, projection, label, **plot_kwargs) #one dimensional plotting if len(free_dims) == 1: update_not_existing_kwargs(plot_kwargs, pl().defaults.inducing_1d) # @UndefinedVariable - plots['inducing'] = pl().plot_axis_lines(canvas, Z[:, free_dims], **plot_kwargs) + plots['inducing'] = pl().plot_axis_lines(canvas, Z[:, free_dims], label=label, **plot_kwargs) #2D plotting elif len(free_dims) == 2 and projection == '3d': update_not_existing_kwargs(plot_kwargs, pl().defaults.inducing_3d) # @UndefinedVariable - plots['inducing'] = pl().plot_axis_lines(canvas, Z[:, free_dims], **plot_kwargs) + plots['inducing'] = pl().plot_axis_lines(canvas, Z[:, free_dims], label=label, **plot_kwargs) elif len(free_dims) == 2: update_not_existing_kwargs(plot_kwargs, pl().defaults.inducing_2d) # @UndefinedVariable plots['inducing'] = pl().scatter(canvas, Z[:, free_dims[0]], Z[:, free_dims[1]], - **plot_kwargs) + label=label, **plot_kwargs) elif len(free_dims) == 0: pass #Nothing to plot! else: diff --git a/GPy/plotting/gpy_plot/gp_plots.py b/GPy/plotting/gpy_plot/gp_plots.py index 89510561..a1197af6 100644 --- a/GPy/plotting/gpy_plot/gp_plots.py +++ b/GPy/plotting/gpy_plot/gp_plots.py @@ -46,9 +46,12 @@ def plot_mean(self, plot_limits=None, fixed_inputs=None, """ Plot the mean of the GP. + You can deactivate the legend for this one plot by supplying None to label. + Give the Y_metadata in the predict_kw if you need it. + :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits :type plot_limits: np.array :param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v. @@ -112,8 +115,11 @@ def plot_confidence(self, lower=2.5, upper=97.5, plot_limits=None, fixed_inputs= E.g. the 95% confidence interval is $2.5, 97.5$. Note: Only implemented for one dimension! + You can deactivate the legend for this one plot by supplying None to label. + Give the Y_metadata in the predict_kw if you need it. + :param float lower: the lower percentile to plot :param float upper: the upper percentile to plot :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits @@ -134,7 +140,7 @@ def plot_confidence(self, lower=2.5, upper=97.5, plot_limits=None, fixed_inputs= (lower, upper), ycols, predict_kw) plots = _plot_confidence(self, canvas, helper_data, helper_prediction, label, **kwargs) - return pl().add_to_canvas(canvas, plots) + return pl().add_to_canvas(canvas, plots, legend=label is not None) def _plot_confidence(self, canvas, helper_data, helper_prediction, label, **kwargs): _, _, _, _, free_dims, Xgrid, _, _, _, _, _ = helper_data @@ -162,9 +168,12 @@ def plot_samples(self, plot_limits=None, fixed_inputs=None, """ Plot the mean of the GP. + You can deactivate the legend for this one plot by supplying None to label. + Give the Y_metadata in the predict_kw if you need it. + :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits :type plot_limits: np.array :param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v. @@ -221,8 +230,12 @@ def plot_density(self, plot_limits=None, fixed_inputs=None, E.g. the 95% confidence interval is $2.5, 97.5$. Note: Only implemented for one dimension! + You can deactivate the legend for this one plot by supplying None to label. + Give the Y_metadata in the predict_kw if you need it. + + :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits :type plot_limits: np.array :param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v. @@ -271,10 +284,13 @@ def plot(self, plot_limits=None, fixed_inputs=None, plot_data=True, plot_inducing=True, plot_density=False, predict_kw=None, projection='2d', legend=False, **kwargs): """ - Convinience function for plotting the fit of a GP. + Convenience function for plotting the fit of a GP. + + You can deactivate the legend for this one plot by supplying None to label. Give the Y_metadata in the predict_kw if you need it. + If you want fine graned control use the specific plotting functions supplied in the model. :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits @@ -298,6 +314,8 @@ def plot(self, plot_limits=None, fixed_inputs=None, :param bool plot_inducing: plot inducing inputs? :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 + :param {2d|3d} projection: plot in 2d or 3d? + :param bool legend: convenience, whether to put a legend on the plot or not. """ canvas, _ = pl().new_canvas(projection=projection, **kwargs) helper_data = helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution) @@ -341,8 +359,11 @@ def plot_f(self, plot_limits=None, fixed_inputs=None, If you want fine graned control use the specific plotting functions supplied in the model. + You can deactivate the legend for this one plot by supplying None to label. + Give the Y_metadata in the predict_kw if you need it. + :param plot_limits: The limits of the plot. If 1D [xmin,xmax], if 2D [[xmin,ymin],[xmax,ymax]]. Defaluts to data limits :type plot_limits: np.array :param fixed_inputs: a list of tuple [(i,v), (i,v)...], specifying that input dimension i should be set to value v. diff --git a/GPy/plotting/plotly_dep/defaults.py b/GPy/plotting/plotly_dep/defaults.py index 901455ad..faf343b0 100644 --- a/GPy/plotting/plotly_dep/defaults.py +++ b/GPy/plotting/plotly_dep/defaults.py @@ -44,17 +44,17 @@ it gives back an empty default, when defaults are not defined. # Data plots: data_1d = dict(marker_kwargs=dict(), marker='x', color='black') -data_2d = dict(marker='o', cmap='Hot', marker_kwargs=dict(opacity=1., size='10', line=Line(width=.5, color='black'))) +data_2d = dict(marker='o', cmap='Hot', marker_kwargs=dict(opacity=1., size='5', line=Line(width=.5, color='black'))) inducing_1d = dict(color=Tango.colorsHex['darkRed']) -inducing_2d = dict(marker_kwargs=dict(size='8', opacity=.7, line=Line(width=.5, color='black')), opacity=.7, color='white', marker='star-triangle-up') -inducing_3d = dict(marker_kwargs=dict(size='8', opacity=.7, line=Line(width=.5, color='black')), opacity=.7, color='white', marker='star-triangle-up') +inducing_2d = dict(marker_kwargs=dict(size='5', opacity=.7, line=Line(width=.5, color='black')), opacity=.7, color='white', marker='star-triangle-up') +inducing_3d = dict(marker_kwargs=dict(symbol='diamond', size='5', opacity=.7, line=Line(width=.1, color='black')), color='#F5F5F5') xerrorbar = dict(color='black', error_kwargs=dict(thickness=.5), opacity=.5) yerrorbar = dict(color=Tango.colorsHex['darkRed'], error_kwargs=dict(thickness=.5), opacity=.5) # # # GP plots: meanplot_1d = dict(color=Tango.colorsHex['mediumBlue'], line_kwargs=dict(width=2)) meanplot_2d = dict(colorscale='Hot') -meanplot_3d = dict(colorscale='Hot', opacity=.8) +meanplot_3d = dict(colorscale='Hot', opacity=.9) samples_1d = dict(color=Tango.colorsHex['mediumBlue'], line_kwargs=dict(width=.3)) samples_3d = dict(cmap='Hot', opacity=.5) confidence_interval = dict(mode='lines', line_kwargs=dict(color=Tango.colorsHex['darkBlue'], width=.4), diff --git a/GPy/plotting/plotly_dep/plot_definitions.py b/GPy/plotting/plotly_dep/plot_definitions.py index 80f737ac..9c2a8de1 100644 --- a/GPy/plotting/plotly_dep/plot_definitions.py +++ b/GPy/plotting/plotly_dep/plot_definitions.py @@ -35,7 +35,7 @@ from plotly import tools from plotly import plotly as py from plotly.graph_objs import Scatter, Scatter3d, Line,\ Marker, ErrorX, ErrorY, Bar, Heatmap, Trace,\ - Annotations, Annotation, Contour, Contours, Font, Surface + Annotations, Annotation, Contour, Font, Surface from plotly.exceptions import PlotlyDictKeyError SYMBOL_MAP = { @@ -78,6 +78,9 @@ class PlotlyPlots(AbstractPlottingLibrary): figure.layout.font = Font(family="Raleway, sans-serif") else: return canvas, kwargs + if projection == '3d': + figure.layout.legend.x=.5 + figure.layout.legend.bgcolor='#DCDCDC' return (figure, row, col), kwargs def add_to_canvas(self, canvas, traces, legend=False, **kwargs): @@ -129,9 +132,15 @@ class PlotlyPlots(AbstractPlottingLibrary): except: #not matplotlib marker pass + marker_kwargs.setdefault('symbol', marker) if Z is not None: - return Scatter3d(x=X, y=Y, z=Z, mode='markers', showlegend=label is not None, marker=Marker(color=color, symbol=marker, colorscale=cmap, **marker_kwargs or {}), name=label, **kwargs) - return Scatter(x=X, y=Y, mode='markers', showlegend=label is not None, marker=Marker(color=color, symbol=marker, colorscale=cmap, **marker_kwargs or {}), name=label, **kwargs) + return Scatter3d(x=X, y=Y, z=Z, mode='markers', + showlegend=label is not None, + marker=Marker(color=color, colorscale=cmap, **marker_kwargs or {}), + name=label, **kwargs) + return Scatter(x=X, y=Y, mode='markers', showlegend=label is not None, + marker=Marker(color=color, colorscale=cmap, **marker_kwargs or {}), + name=label, **kwargs) def plot(self, ax, X, Y, Z=None, color=None, label=None, line_kwargs=None, **kwargs): if 'mode' not in kwargs: @@ -140,14 +149,14 @@ class PlotlyPlots(AbstractPlottingLibrary): return Scatter3d(x=X, y=Y, z=Z, showlegend=label is not None, line=Line(color=color, **line_kwargs or {}), name=label, **kwargs) return Scatter(x=X, y=Y, showlegend=label is not None, line=Line(color=color, **line_kwargs or {}), name=label, **kwargs) - def plot_axis_lines(self, ax, X, Z=None, color=Tango.colorsHex['mediumBlue'], label=None, marker_kwargs=None, **kwargs): + def plot_axis_lines(self, ax, X, color=Tango.colorsHex['mediumBlue'], label=None, marker_kwargs=None, **kwargs): if X.shape[1] == 1: annotations = Annotations() for n, row in enumerate(X): annotations.append( Annotation( text='', - x=row[0], y=0, + x=row[n], y=0, yref='paper', ax=0, ay=20, arrowhead=2, @@ -155,10 +164,17 @@ class PlotlyPlots(AbstractPlottingLibrary): arrowwidth=2, arrowcolor=color, showarrow=True)) - return annotations - #if Z is not None: - # return Scatter3d(x=X[:,0], y=X[:,1], z=0, zref='paper', showlegend=label is not None, mode='markers', marker=Marker(color=color, symbol='diamond-tall', **marker_kwargs or {}), name=label, **kwargs) - #return Scatter(x=X, y=0, mode='markers', showlegend=label is not None, marker=Marker(yref='paper', color=color, symbol='diamond-tall', **marker_kwargs or {}), name=label, **kwargs) + return annotations + elif X.shape[1] == 2: + marker_kwargs.setdefault('symbol', 'diamond') + opacity = kwargs.pop('opacity', .8) + return Scatter3d(x=X[:, 0], y=X[:, 1], z=np.zeros(X.shape[0]), + mode='markers', + projection=dict(z=dict(show=True, opacity=opacity)), + marker=Marker(color=color, **marker_kwargs or {}), + opacity=0, + name=label, + showlegend=label is not None, **kwargs) def barplot(self, canvas, x, height, width=0.8, bottom=0, color=Tango.colorsHex['mediumBlue'], label=None, **kwargs): figure, _, _ = canvas @@ -247,7 +263,7 @@ class PlotlyPlots(AbstractPlottingLibrary): name=label, **kwargs) def surface(self, ax, X, Y, Z, color=None, label=None, **kwargs): - return Surface(x=X, y=Y, z=Z, name=label, **kwargs) + return Surface(x=X, y=Y, z=Z, name=label, showlegend=label is not None, **kwargs) def fill_between(self, ax, X, lower, upper, color=Tango.colorsHex['mediumBlue'], label=None, line_kwargs=None, **kwargs): if not 'line' in kwargs: @@ -257,9 +273,9 @@ class PlotlyPlots(AbstractPlottingLibrary): if color.startswith('#'): fcolor = 'rgba ({c[0]}, {c[1]}, {c[2]}, {alpha})'.format(c=Tango.hex2rgb(color), alpha=kwargs.get('opacity', 1.0)) else: fcolor = color - u = Scatter(x=X, y=upper, fillcolor=fcolor, showlegend=label is not None, name=label, fill='tonexty', **kwargs) - fcolor = '{}, {alpha})'.format(','.join(fcolor.split(',')[:-1]), alpha=0.0) - l = Scatter(x=X, y=lower, fillcolor=fcolor, showlegend=False, fill='tonexty', name=label, **kwargs) + u = Scatter(x=X, y=upper, fillcolor=fcolor, showlegend=label is not None, name=label, fill='tonextx', legendgroup='density', **kwargs) + #fcolor = '{}, {alpha})'.format(','.join(fcolor.split(',')[:-1]), alpha=0.0) + l = Scatter(x=X, y=lower, fillcolor=fcolor, showlegend=False, name=label, legendgroup='density', **kwargs) return l, u def fill_gradient(self, canvas, X, percentiles, color=Tango.colorsHex['mediumBlue'], label=None, **kwargs):