[inducing] 3d added

This commit is contained in:
mzwiessele 2015-10-10 09:55:00 +01:00
parent cfae854f88
commit 57d2f98570
7 changed files with 68 additions and 30 deletions

View file

@ -1,5 +1,5 @@
# This is the local installation configuration file for GPy
[plotting]
#library = plotly
library = matplotlib
library = plotly
#library = matplotlib

View file

@ -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)

View file

@ -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.")

View file

@ -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:

View file

@ -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=<specific kernel>) 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.

View file

@ -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),

View file

@ -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):