From 327ed46c6e54e17dcbec74e135438b115bdca261 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Fri, 16 Oct 2015 14:53:40 +0100 Subject: [PATCH 1/6] [pickling] wrote warning for using pickling --- GPy/core/parameterization/parameter_core.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/GPy/core/parameterization/parameter_core.py b/GPy/core/parameterization/parameter_core.py index 308bf20d..b89f6f4e 100644 --- a/GPy/core/parameterization/parameter_core.py +++ b/GPy/core/parameterization/parameter_core.py @@ -105,6 +105,20 @@ class Pickleable(object): #=========================================================================== def pickle(self, f, protocol=-1): """ + Pickle a model to file to reload from disk. + + .. warning:: + + Pickling is a local method, to pickle a model to disk and reload + it on the same machine in this instance. + Using pickling as saving states of models could be not supported + across versions and need more work to reload a model after a version + change. If you want to save a model consistently, save the script to + create the model and the `param_array` (e.g. using numpy.save) of + the model you want to save. Then load the model using the script and + push the parameters from the saved `param_array` into the newly + created model. + :param f: either filename or open file object to write to. if it is an open buffer, you have to make sure to close it properly. From b236896fbdfbdf494ce47035a09a11725aa08c68 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Fri, 16 Oct 2015 14:55:04 +0100 Subject: [PATCH 2/6] [plotting] got the old way in again --- GPy/plotting/__init__.py | 4 +++- GPy/plotting/gpy_plot/plot_util.py | 7 +++++-- GPy/plotting/matplot_dep/mapping_plots.py | 2 +- GPy/plotting/matplot_dep/variational_plots.py | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/GPy/plotting/__init__.py b/GPy/plotting/__init__.py index 28c05cef..e4fe7080 100644 --- a/GPy/plotting/__init__.py +++ b/GPy/plotting/__init__.py @@ -13,6 +13,7 @@ def change_plotting_library(lib): if lib == 'matplotlib': import matplotlib from .matplot_dep.plot_definitions import MatplotlibPlots + from .matplot_dep import visualize, mapping_plots, priors_plots, ssgplvm, svig_plots, variational_plots, img_plots current_lib[0] = MatplotlibPlots() if lib == 'plotly': import plotly @@ -22,10 +23,11 @@ def change_plotting_library(lib): current_lib[0] = None #=========================================================================== except (ImportError, NameError): + raise config.set('plotting', 'library', 'none') 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/gpy_plot/plot_util.py b/GPy/plotting/gpy_plot/plot_util.py index 22d6627c..e89aae0f 100644 --- a/GPy/plotting/gpy_plot/plot_util.py +++ b/GPy/plotting/gpy_plot/plot_util.py @@ -275,7 +275,7 @@ def get_x_y_var(model): and Y the outputs If (X, X_variance, Y) is given, this just returns. - + :returns: (X, X_variance, Y) """ # model given @@ -285,7 +285,10 @@ def get_x_y_var(model): else: X = model.X.values X_variance = None - Y = model.Y.values + try: + Y = model.Y.values + except AttributeError: + Y = model.Y if sparse.issparse(Y): Y = Y.todense().view(np.ndarray) return X, X_variance, Y diff --git a/GPy/plotting/matplot_dep/mapping_plots.py b/GPy/plotting/matplot_dep/mapping_plots.py index c563d392..8ec09758 100644 --- a/GPy/plotting/matplot_dep/mapping_plots.py +++ b/GPy/plotting/matplot_dep/mapping_plots.py @@ -7,7 +7,6 @@ try: from matplotlib import pyplot as pb except: pass -from .base_plots import x_frame1D, x_frame2D def plot_mapping(self, plot_limits=None, which_data='all', which_parts='all', resolution=None, levels=20, samples=0, fignum=None, ax=None, fixed_inputs=[], linecol=Tango.colorsHex['darkBlue']): @@ -52,6 +51,7 @@ def plot_mapping(self, plot_limits=None, which_data='all', which_parts='all', re ax = fig.add_subplot(111) plotdims = self.input_dim - len(fixed_inputs) + from ..gpy_plot.plot_util import x_frame1D, x_frame2D if plotdims == 1: diff --git a/GPy/plotting/matplot_dep/variational_plots.py b/GPy/plotting/matplot_dep/variational_plots.py index 24e613aa..34681552 100644 --- a/GPy/plotting/matplot_dep/variational_plots.py +++ b/GPy/plotting/matplot_dep/variational_plots.py @@ -94,7 +94,7 @@ def plot_SpikeSlab(parameterized, fignum=None, ax=None, colors=None, side_by_sid a.set_xticklabels('') # binary prob plot a = fig.add_subplot(*sub2) - a.bar(x,gamma[:,i],bottom=0.,linewidth=0,width=1.0,align='center') + a.bar(x,gamma[:,i],bottom=0.,linewidth=1.,width=1.0,align='center') a.set_xlim(x.min(), x.max()) a.set_ylim([0.,1.]) pb.draw() From 4cd16a86b48b03d4a6edd56a969242296ab66f4d Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Fri, 16 Oct 2015 14:56:32 +0100 Subject: [PATCH 3/6] [pickling] _src -> src --- GPy/__init__.py | 36 ++++++++++++++-------- GPy/core/gp.py | 6 +++- GPy/core/model.py | 4 +++ GPy/core/parameterization/param.py | 2 +- GPy/core/parameterization/updateable.py | 9 ++++++ GPy/inference/optimization/optimization.py | 12 ++++---- GPy/kern/__init__.py | 4 +-- GPy/kern/src/kern.py | 25 ++++++++++----- GPy/kern/src/rbf.py | 5 +-- 9 files changed, 69 insertions(+), 34 deletions(-) diff --git a/GPy/__init__.py b/GPy/__init__.py index 32f0c1c4..d044b2c0 100644 --- a/GPy/__init__.py +++ b/GPy/__init__.py @@ -40,18 +40,28 @@ def load(file_or_path): :param file_name: path/to/file.pickle """ + # This is the pickling pain when changing _src -> src try: - import cPickle as pickle - if isinstance(file_or_path, basestring): - with open(file_or_path, 'rb') as f: - m = pickle.load(f) - else: - m = pickle.load(file_or_path) - except: - import pickle - if isinstance(file_or_path, str): - with open(file_or_path, 'rb') as f: - m = pickle.load(f) - else: - m = pickle.load(file_or_path) + try: + import cPickle as pickle + if isinstance(file_or_path, basestring): + with open(file_or_path, 'rb') as f: + m = pickle.load(f) + else: + m = pickle.load(file_or_path) + except: + import pickle + if isinstance(file_or_path, str): + with open(file_or_path, 'rb') as f: + m = pickle.load(f) + else: + m = pickle.load(file_or_path) + except ImportError: + import sys + import inspect + sys.modules['GPy.kern._src'] = kern.src + for name, module in inspect.getmembers(kern.src): + if not name.startswith('_'): + sys.modules['GPy.kern._src.{}'.format(name)] = module + m = load(file_or_path) return m diff --git a/GPy/core/gp.py b/GPy/core/gp.py index c08e7906..6d66cd68 100644 --- a/GPy/core/gp.py +++ b/GPy/core/gp.py @@ -121,6 +121,10 @@ class GP(Model): # W_{pp} := \texttt{Woodbury inv} # p := _predictive_variable + def __setstate__(self, state): + self.mean_function = None + super(GP, self).__setstate__(state) + @property def _predictive_variable(self): return self.X @@ -459,7 +463,7 @@ class GP(Model): m, v = self._raw_predict(X, full_cov=full_cov, **predict_kwargs) if self.normalizer is not None: m, v = self.normalizer.inverse_mean(m), self.normalizer.inverse_variance(v) - + def sim_one_dim(m, v): if not full_cov: return np.random.multivariate_normal(m.flatten(), np.diag(v.flatten()), size).T diff --git a/GPy/core/model.py b/GPy/core/model.py index c79c5465..ada0ef5f 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -30,6 +30,10 @@ class Model(Parameterized): self.obj_grads = None self.add_observer(self.tie, self.tie._parameters_changed_notification, priority=-500) + def __setstate__(self, state): + self.obj_grads = None + super(Model, self).__setstate__(state) + def log_likelihood(self): raise NotImplementedError("this needs to be implemented to use the model class") def _log_likelihood_gradients(self): diff --git a/GPy/core/parameterization/param.py b/GPy/core/parameterization/param.py index 8fdd744e..52bb28d1 100644 --- a/GPy/core/parameterization/param.py +++ b/GPy/core/parameterization/param.py @@ -90,7 +90,7 @@ class Param(Parameterizable, ObsAr): self._original_ = getattr(obj, '_original_', None) self._name = getattr(obj, '_name', None) self._gradient_array_ = getattr(obj, '_gradient_array_', None) - self._update_on = getattr(obj, '_update_on', None) + self.__update_on = getattr(obj, '__update_on', None) self.constraints = getattr(obj, 'constraints', None) self.priors = getattr(obj, 'priors', None) diff --git a/GPy/core/parameterization/updateable.py b/GPy/core/parameterization/updateable.py index 07083ce0..22a57d41 100644 --- a/GPy/core/parameterization/updateable.py +++ b/GPy/core/parameterization/updateable.py @@ -34,6 +34,15 @@ class Updateable(Observable): p.traverse(turn_updates) self.trigger_update() + @property + def _update_on(self): + if not hasattr(self, '__update_on'): + self.__update_on = True + return self.__update_on + @_update_on.setter + def _update_on(self, update): + self.__update_on = update + def toggle_update(self): print("deprecated: toggle_update was renamed to update_toggle for easier access") self.update_toggle() diff --git a/GPy/inference/optimization/optimization.py b/GPy/inference/optimization/optimization.py index 1052e909..7f80971c 100644 --- a/GPy/inference/optimization/optimization.py +++ b/GPy/inference/optimization/optimization.py @@ -27,7 +27,7 @@ class Optimizer(object): :rtype: optimizer object. """ - def __init__(self, x_init, messages=False, model=None, max_f_eval=1e4, max_iters=1e3, + def __init__(self, x_init=None, messages=False, model=None, max_f_eval=1e4, max_iters=1e3, ftol=None, gtol=None, xtol=None, bfgs_factor=None): self.opt_name = None self.x_init = x_init @@ -133,7 +133,7 @@ class opt_lbfgsb(Optimizer): #a more helpful error message is available in opt_result in the Error case if opt_result[2]['warnflag']==2: self.status = 'Error' + str(opt_result[2]['task']) - + class opt_bfgs(Optimizer): def __init__(self, *args, **kwargs): Optimizer.__init__(self, *args, **kwargs) @@ -245,7 +245,7 @@ class opt_SCG(Optimizer): self.f_opt = self.trace[-1] self.funct_eval = opt_result[2] self.status = opt_result[3] - + class Opt_Adadelta(Optimizer): def __init__(self, step_rate=0.1, decay=0.9, momentum=0, *args, **kwargs): Optimizer.__init__(self, *args, **kwargs) @@ -256,11 +256,11 @@ class Opt_Adadelta(Optimizer): def opt(self, f_fp=None, f=None, fp=None): assert not fp is None - + import climin - + opt = climin.adadelta.Adadelta(self.x_init, fp, step_rate=self.step_rate, decay=self.decay, momentum=self.momentum) - + for info in opt: if info['n_iter']>=self.max_iters: self.x_opt = opt.wrt diff --git a/GPy/kern/__init__.py b/GPy/kern/__init__.py index f8f7d016..05540a8c 100644 --- a/GPy/kern/__init__.py +++ b/GPy/kern/__init__.py @@ -1,10 +1,8 @@ """ Kernel module the kernels to sit in. -.. automodule:: .src - :members: - :private-members: """ +from . import src from .src.kern import Kern from .src.add import Add from .src.prod import Prod diff --git a/GPy/kern/src/kern.py b/GPy/kern/src/kern.py index 4d535b60..da4c17d5 100644 --- a/GPy/kern/src/kern.py +++ b/GPy/kern/src/kern.py @@ -54,13 +54,22 @@ class Kern(Parameterized): self.active_dims = active_dims self._all_dims_active = np.atleast_1d(active_dims).astype(int) - assert self._all_dims_active.size == self.input_dim, "input_dim={} does not match len(active_dim)={}, _all_dims_active={}".format(self.input_dim, self._all_dims_active.size, self._all_dims_active) + assert self._all_dims_active.size == self.input_dim, "input_dim={} does not match len(active_dim)={}, active_dim={}".format(self.input_dim, self._all_dims_active.size, self._all_dims_active) self._sliced_X = 0 self.useGPU = self._support_GPU and useGPU from .psi_comp import PSICOMP_GH - self.psicomp = PSICOMP_GH() + self.psicomp = PSICOMP_GH() + + @property + def _all_dims_active(self): + if not hasattr(self, '__all_dims_active'): + self.__all_dims_active = np.asanyarray(self.active_dims) + return self.__all_dims_active + @_all_dims_active.setter + def _all_dims_active(self, active_dims): + self.__all_dims_active = np.asanyarray(active_dims) @property def _effective_input_dim(self): @@ -211,15 +220,15 @@ class Kern(Parameterized): def get_most_significant_input_dimensions(self, which_indices=None): """ Determine which dimensions should be plotted - + Returns the top three most signification input dimensions - + if less then three dimensions, the non existing dimensions are labeled as None, so for a 1 dimensional input this returns (0, None, None). - - :param which_indices: force the indices to be the given indices. - :type which_indices: int or tuple(int,int) or tuple(int,int,int) + + :param which_indices: force the indices to be the given indices. + :type which_indices: int or tuple(int,int) or tuple(int,int,int) """ if which_indices is None: which_indices = np.argsort(self.input_sensitivity())[::-1][:3] @@ -235,7 +244,7 @@ class Kern(Parameterized): input_1, input_2 = which_indices, None except ValueError: # which_indices was a list or array like with only one int - input_1, input_2 = which_indices[0], None + input_1, input_2 = which_indices[0], None return input_1, input_2, input_3 diff --git a/GPy/kern/src/rbf.py b/GPy/kern/src/rbf.py index 3607bea9..ce422816 100644 --- a/GPy/kern/src/rbf.py +++ b/GPy/kern/src/rbf.py @@ -47,12 +47,13 @@ class RBF(Stationary): return dc def __setstate__(self, state): + self.use_invLengthscale = False return super(RBF, self).__setstate__(state) def spectrum(self, omega): assert self.input_dim == 1 #TODO: higher dim spectra? return self.variance*np.sqrt(2*np.pi)*self.lengthscale*np.exp(-self.lengthscale*2*omega**2/2) - + def parameters_changed(self): if self.use_invLengthscale: self.lengthscale[:] = 1./np.sqrt(self.inv_l+1e-200) super(RBF,self).parameters_changed() @@ -85,7 +86,7 @@ class RBF(Stationary): def gradients_qX_expectations(self, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior): return self.psicomp.psiDerivativecomputations(self, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior)[3:] - + def update_gradients_diag(self, dL_dKdiag, X): super(RBF,self).update_gradients_diag(dL_dKdiag, X) if self.use_invLengthscale: self.inv_l.gradient =self.lengthscale.gradient*(self.lengthscale**3/-2.) From 29921e1c69afc9b3b195f5984196cb11ab286be3 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Fri, 16 Oct 2015 15:06:10 +0100 Subject: [PATCH 4/6] Revert "[pickling] _src -> src" This reverts commit 4cd16a86b48b03d4a6edd56a969242296ab66f4d. --- GPy/__init__.py | 36 ++++++++-------------- GPy/core/gp.py | 6 +--- GPy/core/model.py | 4 --- GPy/core/parameterization/param.py | 2 +- GPy/core/parameterization/updateable.py | 9 ------ GPy/inference/optimization/optimization.py | 12 ++++---- GPy/kern/__init__.py | 4 ++- GPy/kern/src/kern.py | 25 +++++---------- GPy/kern/src/rbf.py | 5 ++- 9 files changed, 34 insertions(+), 69 deletions(-) diff --git a/GPy/__init__.py b/GPy/__init__.py index d044b2c0..32f0c1c4 100644 --- a/GPy/__init__.py +++ b/GPy/__init__.py @@ -40,28 +40,18 @@ def load(file_or_path): :param file_name: path/to/file.pickle """ - # This is the pickling pain when changing _src -> src try: - try: - import cPickle as pickle - if isinstance(file_or_path, basestring): - with open(file_or_path, 'rb') as f: - m = pickle.load(f) - else: - m = pickle.load(file_or_path) - except: - import pickle - if isinstance(file_or_path, str): - with open(file_or_path, 'rb') as f: - m = pickle.load(f) - else: - m = pickle.load(file_or_path) - except ImportError: - import sys - import inspect - sys.modules['GPy.kern._src'] = kern.src - for name, module in inspect.getmembers(kern.src): - if not name.startswith('_'): - sys.modules['GPy.kern._src.{}'.format(name)] = module - m = load(file_or_path) + import cPickle as pickle + if isinstance(file_or_path, basestring): + with open(file_or_path, 'rb') as f: + m = pickle.load(f) + else: + m = pickle.load(file_or_path) + except: + import pickle + if isinstance(file_or_path, str): + with open(file_or_path, 'rb') as f: + m = pickle.load(f) + else: + m = pickle.load(file_or_path) return m diff --git a/GPy/core/gp.py b/GPy/core/gp.py index 6d66cd68..c08e7906 100644 --- a/GPy/core/gp.py +++ b/GPy/core/gp.py @@ -121,10 +121,6 @@ class GP(Model): # W_{pp} := \texttt{Woodbury inv} # p := _predictive_variable - def __setstate__(self, state): - self.mean_function = None - super(GP, self).__setstate__(state) - @property def _predictive_variable(self): return self.X @@ -463,7 +459,7 @@ class GP(Model): m, v = self._raw_predict(X, full_cov=full_cov, **predict_kwargs) if self.normalizer is not None: m, v = self.normalizer.inverse_mean(m), self.normalizer.inverse_variance(v) - + def sim_one_dim(m, v): if not full_cov: return np.random.multivariate_normal(m.flatten(), np.diag(v.flatten()), size).T diff --git a/GPy/core/model.py b/GPy/core/model.py index ada0ef5f..c79c5465 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -30,10 +30,6 @@ class Model(Parameterized): self.obj_grads = None self.add_observer(self.tie, self.tie._parameters_changed_notification, priority=-500) - def __setstate__(self, state): - self.obj_grads = None - super(Model, self).__setstate__(state) - def log_likelihood(self): raise NotImplementedError("this needs to be implemented to use the model class") def _log_likelihood_gradients(self): diff --git a/GPy/core/parameterization/param.py b/GPy/core/parameterization/param.py index 52bb28d1..8fdd744e 100644 --- a/GPy/core/parameterization/param.py +++ b/GPy/core/parameterization/param.py @@ -90,7 +90,7 @@ class Param(Parameterizable, ObsAr): self._original_ = getattr(obj, '_original_', None) self._name = getattr(obj, '_name', None) self._gradient_array_ = getattr(obj, '_gradient_array_', None) - self.__update_on = getattr(obj, '__update_on', None) + self._update_on = getattr(obj, '_update_on', None) self.constraints = getattr(obj, 'constraints', None) self.priors = getattr(obj, 'priors', None) diff --git a/GPy/core/parameterization/updateable.py b/GPy/core/parameterization/updateable.py index 22a57d41..07083ce0 100644 --- a/GPy/core/parameterization/updateable.py +++ b/GPy/core/parameterization/updateable.py @@ -34,15 +34,6 @@ class Updateable(Observable): p.traverse(turn_updates) self.trigger_update() - @property - def _update_on(self): - if not hasattr(self, '__update_on'): - self.__update_on = True - return self.__update_on - @_update_on.setter - def _update_on(self, update): - self.__update_on = update - def toggle_update(self): print("deprecated: toggle_update was renamed to update_toggle for easier access") self.update_toggle() diff --git a/GPy/inference/optimization/optimization.py b/GPy/inference/optimization/optimization.py index 7f80971c..1052e909 100644 --- a/GPy/inference/optimization/optimization.py +++ b/GPy/inference/optimization/optimization.py @@ -27,7 +27,7 @@ class Optimizer(object): :rtype: optimizer object. """ - def __init__(self, x_init=None, messages=False, model=None, max_f_eval=1e4, max_iters=1e3, + def __init__(self, x_init, messages=False, model=None, max_f_eval=1e4, max_iters=1e3, ftol=None, gtol=None, xtol=None, bfgs_factor=None): self.opt_name = None self.x_init = x_init @@ -133,7 +133,7 @@ class opt_lbfgsb(Optimizer): #a more helpful error message is available in opt_result in the Error case if opt_result[2]['warnflag']==2: self.status = 'Error' + str(opt_result[2]['task']) - + class opt_bfgs(Optimizer): def __init__(self, *args, **kwargs): Optimizer.__init__(self, *args, **kwargs) @@ -245,7 +245,7 @@ class opt_SCG(Optimizer): self.f_opt = self.trace[-1] self.funct_eval = opt_result[2] self.status = opt_result[3] - + class Opt_Adadelta(Optimizer): def __init__(self, step_rate=0.1, decay=0.9, momentum=0, *args, **kwargs): Optimizer.__init__(self, *args, **kwargs) @@ -256,11 +256,11 @@ class Opt_Adadelta(Optimizer): def opt(self, f_fp=None, f=None, fp=None): assert not fp is None - + import climin - + opt = climin.adadelta.Adadelta(self.x_init, fp, step_rate=self.step_rate, decay=self.decay, momentum=self.momentum) - + for info in opt: if info['n_iter']>=self.max_iters: self.x_opt = opt.wrt diff --git a/GPy/kern/__init__.py b/GPy/kern/__init__.py index 05540a8c..f8f7d016 100644 --- a/GPy/kern/__init__.py +++ b/GPy/kern/__init__.py @@ -1,8 +1,10 @@ """ Kernel module the kernels to sit in. +.. automodule:: .src + :members: + :private-members: """ -from . import src from .src.kern import Kern from .src.add import Add from .src.prod import Prod diff --git a/GPy/kern/src/kern.py b/GPy/kern/src/kern.py index da4c17d5..4d535b60 100644 --- a/GPy/kern/src/kern.py +++ b/GPy/kern/src/kern.py @@ -54,22 +54,13 @@ class Kern(Parameterized): self.active_dims = active_dims self._all_dims_active = np.atleast_1d(active_dims).astype(int) - assert self._all_dims_active.size == self.input_dim, "input_dim={} does not match len(active_dim)={}, active_dim={}".format(self.input_dim, self._all_dims_active.size, self._all_dims_active) + assert self._all_dims_active.size == self.input_dim, "input_dim={} does not match len(active_dim)={}, _all_dims_active={}".format(self.input_dim, self._all_dims_active.size, self._all_dims_active) self._sliced_X = 0 self.useGPU = self._support_GPU and useGPU from .psi_comp import PSICOMP_GH - self.psicomp = PSICOMP_GH() - - @property - def _all_dims_active(self): - if not hasattr(self, '__all_dims_active'): - self.__all_dims_active = np.asanyarray(self.active_dims) - return self.__all_dims_active - @_all_dims_active.setter - def _all_dims_active(self, active_dims): - self.__all_dims_active = np.asanyarray(active_dims) + self.psicomp = PSICOMP_GH() @property def _effective_input_dim(self): @@ -220,15 +211,15 @@ class Kern(Parameterized): def get_most_significant_input_dimensions(self, which_indices=None): """ Determine which dimensions should be plotted - + Returns the top three most signification input dimensions - + if less then three dimensions, the non existing dimensions are labeled as None, so for a 1 dimensional input this returns (0, None, None). - - :param which_indices: force the indices to be the given indices. - :type which_indices: int or tuple(int,int) or tuple(int,int,int) + + :param which_indices: force the indices to be the given indices. + :type which_indices: int or tuple(int,int) or tuple(int,int,int) """ if which_indices is None: which_indices = np.argsort(self.input_sensitivity())[::-1][:3] @@ -244,7 +235,7 @@ class Kern(Parameterized): input_1, input_2 = which_indices, None except ValueError: # which_indices was a list or array like with only one int - input_1, input_2 = which_indices[0], None + input_1, input_2 = which_indices[0], None return input_1, input_2, input_3 diff --git a/GPy/kern/src/rbf.py b/GPy/kern/src/rbf.py index ce422816..3607bea9 100644 --- a/GPy/kern/src/rbf.py +++ b/GPy/kern/src/rbf.py @@ -47,13 +47,12 @@ class RBF(Stationary): return dc def __setstate__(self, state): - self.use_invLengthscale = False return super(RBF, self).__setstate__(state) def spectrum(self, omega): assert self.input_dim == 1 #TODO: higher dim spectra? return self.variance*np.sqrt(2*np.pi)*self.lengthscale*np.exp(-self.lengthscale*2*omega**2/2) - + def parameters_changed(self): if self.use_invLengthscale: self.lengthscale[:] = 1./np.sqrt(self.inv_l+1e-200) super(RBF,self).parameters_changed() @@ -86,7 +85,7 @@ class RBF(Stationary): def gradients_qX_expectations(self, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior): return self.psicomp.psiDerivativecomputations(self, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior)[3:] - + def update_gradients_diag(self, dL_dKdiag, X): super(RBF,self).update_gradients_diag(dL_dKdiag, X) if self.use_invLengthscale: self.inv_l.gradient =self.lengthscale.gradient*(self.lengthscale**3/-2.) From b2b88ae8b841e14bbb1210c1f8074d1c53a4f352 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Fri, 16 Oct 2015 15:22:34 +0100 Subject: [PATCH 5/6] [pickling] have the pickling test against a model, which is now being shipped with the distro --- GPy/__init__.py | 36 +++++++++++++++++++++++------------- GPy/testing/pickle_tests.py | 6 ++++++ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/GPy/__init__.py b/GPy/__init__.py index 32f0c1c4..d044b2c0 100644 --- a/GPy/__init__.py +++ b/GPy/__init__.py @@ -40,18 +40,28 @@ def load(file_or_path): :param file_name: path/to/file.pickle """ + # This is the pickling pain when changing _src -> src try: - import cPickle as pickle - if isinstance(file_or_path, basestring): - with open(file_or_path, 'rb') as f: - m = pickle.load(f) - else: - m = pickle.load(file_or_path) - except: - import pickle - if isinstance(file_or_path, str): - with open(file_or_path, 'rb') as f: - m = pickle.load(f) - else: - m = pickle.load(file_or_path) + try: + import cPickle as pickle + if isinstance(file_or_path, basestring): + with open(file_or_path, 'rb') as f: + m = pickle.load(f) + else: + m = pickle.load(file_or_path) + except: + import pickle + if isinstance(file_or_path, str): + with open(file_or_path, 'rb') as f: + m = pickle.load(f) + else: + m = pickle.load(file_or_path) + except ImportError: + import sys + import inspect + sys.modules['GPy.kern._src'] = kern.src + for name, module in inspect.getmembers(kern.src): + if not name.startswith('_'): + sys.modules['GPy.kern._src.{}'.format(name)] = module + m = load(file_or_path) return m diff --git a/GPy/testing/pickle_tests.py b/GPy/testing/pickle_tests.py index 59818de7..575496e1 100644 --- a/GPy/testing/pickle_tests.py +++ b/GPy/testing/pickle_tests.py @@ -42,6 +42,12 @@ class ListDictTestCase(unittest.TestCase): np.testing.assert_array_equal(a1, a2) class Test(ListDictTestCase): + def test_load_pickle(self): + import os + m = GPy.load(os.path.join(os.path.abspath(os.path.split(__file__)[0]), 'pickle_test.pickle')) + self.assertTrue(m.checkgrad()) + self.assertEqual(m.log_likelihood(), -4.7351019830022087) + def test_parameter_index_operations(self): pio = ParameterIndexOperations(dict(test1=np.array([4,3,1,6,4]), test2=np.r_[2:130])) piov = ParameterIndexOperationsView(pio, 20, 250) From 6b6938bd1132a93e3bc7e21587b5702b3711a736 Mon Sep 17 00:00:00 2001 From: Max Zwiessele Date: Fri, 16 Oct 2015 15:46:13 +0100 Subject: [PATCH 6/6] [pickling] have the pickling test against a model, which is now being shipped with the distro --- GPy/testing/pickle_test.pickle | Bin 0 -> 8639 bytes MANIFEST.in | 8 ++------ setup.py | 31 +++++++++++++++++-------------- 3 files changed, 19 insertions(+), 20 deletions(-) create mode 100644 GPy/testing/pickle_test.pickle diff --git a/GPy/testing/pickle_test.pickle b/GPy/testing/pickle_test.pickle new file mode 100644 index 0000000000000000000000000000000000000000..568e7a604b770be8789e427a43991329562da205 GIT binary patch literal 8639 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 literal 0 HcmV?d00001 diff --git a/MANIFEST.in b/MANIFEST.in index 1800fa52..91f053cd 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,11 +1,7 @@ -include *.txt -recursive-include doc *.txt -include *.md -recursive-include doc *.md -include *.cfg recursive-include doc *.cfg include *.json -recursive-include doc *.json recursive-include GPy *.c recursive-include GPy *.so recursive-include GPy *.pyx +include GPy/testing/plotting_tests/baseline/*.png +include GPy/testing/pickle_test.pickle diff --git a/setup.py b/setup.py index 1d4e7321..82190c12 100644 --- a/setup.py +++ b/setup.py @@ -7,21 +7,21 @@ # Copyright (c) 2015, Max Zwiessele # # All rights reserved. -# +# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: -# +# # * Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. -# +# # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. -# +# # * Neither the name of GPy nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. -# +# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -44,7 +44,7 @@ import codecs def read(fname): with codecs.open(fname, 'r', 'latin') as f: return f.read() - + def read_to_rst(fname): try: import pypandoc @@ -102,32 +102,35 @@ setup(name = 'GPy', ext_modules = ext_mods, packages = ["GPy", "GPy.core", - "GPy.core.parameterization", + "GPy.core.parameterization", "GPy.kern", "GPy.kern.src", - "GPy.kern.src.psi_comp", + "GPy.kern.src.psi_comp", "GPy.models", "GPy.inference", "GPy.inference.optimization", "GPy.inference.mcmc", "GPy.inference.latent_function_inference", - "GPy.likelihoods", + "GPy.likelihoods", "GPy.mappings", "GPy.examples", "GPy.testing", - "GPy.util", + "GPy.util", "GPy.plotting", "GPy.plotting.gpy_plot", - "GPy.plotting.matplot_dep", + "GPy.plotting.matplot_dep", "GPy.plotting.matplot_dep.controllers", - "GPy.plotting.plotly_dep", + "GPy.plotting.plotly_dep", ], package_dir={'GPy': 'GPy'}, package_data = {'GPy': ['defaults.cfg', 'installation.cfg', 'util/data_resources.json', 'util/football_teams.json', - 'plotting/plotting_tests/baseline/*.png' + 'testing/plotting_tests/baseline/*.png' ]}, + data_files=[('GPy/testing/plotting_tests/baseline', 'testing/plotting_tests/baseline/*.png'), + ('GPy/testing/', 'GPy/testing/pickle_test.pickle'), + ], include_package_data = True, py_modules = ['GPy.__init__'], test_suite = 'GPy.testing', @@ -159,7 +162,7 @@ if not os.path.exists(user_file): if os.path.exists(old_user_file): # Move it to new location: print("GPy: Found old config file, moving to new location {}".format(user_file)) - os.rename(old_user_file, user_file) + os.rename(old_user_file, user_file) else: # No config file exists, save informative stub to user config folder: print("GPy: Saving user configuration file to {}".format(user_file))