[plotly] last minute change for ipython notebook

This commit is contained in:
Max Zwiessele 2015-10-09 09:58:35 +01:00
parent f4f6eb32c2
commit e3617942d4

View file

@ -1,21 +1,21 @@
#=============================================================================== #===============================================================================
# Copyright (c) 2012-2015, GPy authors (see AUTHORS.txt). # Copyright (c) 2012-2015, GPy authors (see AUTHORS.txt).
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met: # modification, are permitted provided that the following conditions are met:
# #
# * Redistributions of source code must retain the above copyright notice, this # * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer. # list of conditions and the following disclaimer.
# #
# * Redistributions in binary form must reproduce the above copyright notice, # * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation # this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution. # and/or other materials provided with the distribution.
# #
# * Neither the name of GPy nor the names of its # * Neither the name of GPy nor the names of its
# contributors may be used to endorse or promote products derived from # contributors may be used to endorse or promote products derived from
# this software without specific prior written permission. # this software without specific prior written permission.
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@ -34,11 +34,8 @@ import itertools
def in_ipynb(): def in_ipynb():
try: try:
cfg = get_ipython().config get_ipython()
if cfg['IPKernelApp']['parent_appname'] == 'ipython-notebook': return True
return True
else:
return False
except NameError: except NameError:
return False return False
@ -54,11 +51,11 @@ def find_best_layout_for_subplots(num_subplots):
def helper_predict_with_model(self, Xgrid, plot_raw, apply_link, percentiles, which_data_ycols, predict_kw, samples=0): def helper_predict_with_model(self, Xgrid, plot_raw, apply_link, percentiles, which_data_ycols, predict_kw, samples=0):
""" """
Make the right decisions for prediction with a model Make the right decisions for prediction with a model
based on the standard arguments of plotting. based on the standard arguments of plotting.
This is quite complex and will take a while to understand, This is quite complex and will take a while to understand,
so do not change anything in here lightly!!! so do not change anything in here lightly!!!
""" """
# Put some standards into the predict_kw so that prediction is done automatically: # Put some standards into the predict_kw so that prediction is done automatically:
if predict_kw is None: if predict_kw is None:
@ -77,7 +74,7 @@ def helper_predict_with_model(self, Xgrid, plot_raw, apply_link, percentiles, wh
predict_kw['Y_metadata']['output_index'] = Xgrid[:,-1:].astype(np.int) predict_kw['Y_metadata']['output_index'] = Xgrid[:,-1:].astype(np.int)
mu, _ = self.predict(Xgrid, **predict_kw) mu, _ = self.predict(Xgrid, **predict_kw)
if percentiles is not None: if percentiles is not None:
percentiles = self.predict_quantiles(Xgrid, quantiles=percentiles, **predict_kw) percentiles = self.predict_quantiles(Xgrid, quantiles=percentiles, **predict_kw)
else: percentiles = [] else: percentiles = []
@ -87,11 +84,11 @@ def helper_predict_with_model(self, Xgrid, plot_raw, apply_link, percentiles, wh
fsamples = fsamples[which_data_ycols] if fsamples.ndim == 3 else fsamples fsamples = fsamples[which_data_ycols] if fsamples.ndim == 3 else fsamples
else: else:
fsamples = None fsamples = None
# Filter out the ycolums which we want to plot: # Filter out the ycolums which we want to plot:
retmu = mu[:, which_data_ycols] retmu = mu[:, which_data_ycols]
percs = [p[:, which_data_ycols] for p in percentiles] percs = [p[:, which_data_ycols] for p in percentiles]
if plot_raw and apply_link: if plot_raw and apply_link:
for i in range(len(which_data_ycols)): for i in range(len(which_data_ycols)):
retmu[:, [i]] = self.likelihood.gp_link.transf(mu[:, [i]]) retmu[:, [i]] = self.likelihood.gp_link.transf(mu[:, [i]])
@ -108,8 +105,8 @@ def helper_predict_with_model(self, Xgrid, plot_raw, apply_link, percentiles, wh
def helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution): def helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resolution):
""" """
Figure out the data, free_dims and create an Xgrid for Figure out the data, free_dims and create an Xgrid for
the prediction. the prediction.
This is only implemented for two dimensions for now! This is only implemented for two dimensions for now!
""" """
X, Xvar, Y = get_x_y_var(self) X, Xvar, Y = get_x_y_var(self)
@ -119,7 +116,7 @@ def helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resoluti
fixed_inputs = [] fixed_inputs = []
fixed_dims = get_fixed_dims(self, fixed_inputs) fixed_dims = get_fixed_dims(self, fixed_inputs)
free_dims = get_free_dims(self, visible_dims, fixed_dims) free_dims = get_free_dims(self, visible_dims, fixed_dims)
if len(free_dims) == 1: if len(free_dims) == 1:
#define the frame on which to plot #define the frame on which to plot
resolution = resolution or 200 resolution = resolution or 200
@ -139,7 +136,7 @@ def helper_for_plot_data(self, plot_limits, visible_dims, fixed_inputs, resoluti
for i,v in fixed_inputs: for i,v in fixed_inputs:
Xgrid[:,i] = v Xgrid[:,i] = v
else: else:
raise TypeError("calculated free_dims {} from visible_dims {} and fixed_dims {} is neither 1D nor 2D".format(free_dims, visible_dims, fixed_dims)) raise TypeError("calculated free_dims {} from visible_dims {} and fixed_dims {} is neither 1D nor 2D".format(free_dims, visible_dims, fixed_dims))
return X, Xvar, Y, fixed_dims, free_dims, Xgrid, x, y, xmin, xmax, resolution return X, Xvar, Y, fixed_dims, free_dims, Xgrid, x, y, xmin, xmax, resolution
def scatter_label_generator(labels, X, visible_dims, marker=None): def scatter_label_generator(labels, X, visible_dims, marker=None):
@ -148,10 +145,10 @@ def scatter_label_generator(labels, X, visible_dims, marker=None):
if not lab in ulabels: if not lab in ulabels:
ulabels.append(lab) ulabels.append(lab)
if marker is not None: if marker is not None:
marker = itertools.cycle(list(marker)) marker = itertools.cycle(list(marker))
else: else:
m = None m = None
try: try:
input_1, input_2, input_3 = visible_dims input_1, input_2, input_3 = visible_dims
except: except:
@ -175,12 +172,12 @@ def scatter_label_generator(labels, X, visible_dims, marker=None):
this_label = 'class {!s}'.format(ul) this_label = 'class {!s}'.format(ul)
else: else:
this_label = ul this_label = ul
if marker is not None: if marker is not None:
m = next(marker) m = next(marker)
index = np.nonzero(labels == ul)[0] index = np.nonzero(labels == ul)[0]
if input_2 is None: if input_2 is None:
x = X[index, input_1] x = X[index, input_1]
y = np.zeros(index.size) y = np.zeros(index.size)
@ -191,14 +188,14 @@ def scatter_label_generator(labels, X, visible_dims, marker=None):
z = None z = None
else: else:
x = X[index, input_1] x = X[index, input_1]
y = X[index, input_2] y = X[index, input_2]
z = X[index, input_3] z = X[index, input_3]
yield x, y, z, this_label, index, m yield x, y, z, this_label, index, m
def subsample_X(X, labels, num_samples=1000): def subsample_X(X, labels, num_samples=1000):
""" """
Stratified subsampling if labels are given. Stratified subsampling if labels are given.
This means due to rounding errors you might get a little differences between the This means due to rounding errors you might get a little differences between the
num_samples and the returned subsampled X. num_samples and the returned subsampled X.
""" """
if X.shape[0] > num_samples: if X.shape[0] > num_samples:
@ -258,13 +255,13 @@ def subsample_X(X, labels, num_samples=1000):
# plt.show() # plt.show()
#======================================================================= #=======================================================================
return X, labels return X, labels
def update_not_existing_kwargs(to_update, update_from): def update_not_existing_kwargs(to_update, update_from):
""" """
This function updates the keyword aguments from update_from in This function updates the keyword aguments from update_from in
to_update, only if the keys are not set in to_update. to_update, only if the keys are not set in to_update.
This is used for updated kwargs from the default dicts. This is used for updated kwargs from the default dicts.
""" """
if to_update is None: if to_update is None:
@ -274,12 +271,12 @@ def update_not_existing_kwargs(to_update, update_from):
def get_x_y_var(model): def get_x_y_var(model):
""" """
The the data from a model as The the data from a model as
X the inputs, X the inputs,
X_variance the variance of the inputs ([default: None]) X_variance the variance of the inputs ([default: None])
and Y the outputs and Y the outputs
:returns: (X, X_variance, Y) :returns: (X, X_variance, Y)
""" """
if hasattr(model, 'has_uncertain_inputs') and model.has_uncertain_inputs(): if hasattr(model, 'has_uncertain_inputs') and model.has_uncertain_inputs():
X = model.X.mean.values X = model.X.mean.values
@ -294,10 +291,10 @@ def get_x_y_var(model):
def get_free_dims(model, visible_dims, fixed_dims): def get_free_dims(model, visible_dims, fixed_dims):
""" """
work out what the inputs are for plotting (1D or 2D) work out what the inputs are for plotting (1D or 2D)
The visible dimensions are the dimensions, which are visible. The visible dimensions are the dimensions, which are visible.
the fixed_dims are the fixed dimensions for this. the fixed_dims are the fixed dimensions for this.
The free_dims are then the visible dims without the fixed dims. The free_dims are then the visible dims without the fixed dims.
""" """
if visible_dims is None: if visible_dims is None:
@ -306,7 +303,7 @@ def get_free_dims(model, visible_dims, fixed_dims):
if fixed_dims is not None: if fixed_dims is not None:
dims = np.setdiff1d(dims, fixed_dims) dims = np.setdiff1d(dims, fixed_dims)
return np.asanyarray([dim for dim in dims if dim is not None]) return np.asanyarray([dim for dim in dims if dim is not None])
def get_fixed_dims(model, fixed_inputs): def get_fixed_dims(model, fixed_inputs):
""" """
@ -321,7 +318,7 @@ def get_which_data_ycols(model, which_data_ycols):
if which_data_ycols == 'all' or which_data_ycols is None: if which_data_ycols == 'all' or which_data_ycols is None:
return np.arange(model.output_dim) return np.arange(model.output_dim)
return which_data_ycols return which_data_ycols
def get_which_data_rows(model, which_data_rows): def get_which_data_rows(model, which_data_rows):
""" """
Helper to get the data rows to plot. Helper to get the data rows to plot.