diff --git a/GPy/core/parameterization/param.py b/GPy/core/parameterization/param.py index 96df98f4..89d3a4e4 100644 --- a/GPy/core/parameterization/param.py +++ b/GPy/core/parameterization/param.py @@ -60,6 +60,19 @@ class Param(Constrainable, ObservableArray, Gradcheckable): def __init__(self, name, input_array, default_constraint=None, *a, **kw): super(Param, self).__init__(name=name, default_constraint=default_constraint, *a, **kw) + def build_pydot(self,G): + import pydot + node = pydot.Node(id(self), shape='record', label=self.name) + G.add_node(node) + for o in self._observer_callables_.keys(): + label = o.name if hasattr(o, 'name') else str(o) + observed_node = pydot.Node(id(o), label=label) + G.add_node(observed_node) + edge = pydot.Edge(str(id(self)), str(id(o)), color='darkorange2', arrowhead='vee') + G.add_edge(edge) + + return node + def __array_finalize__(self, obj): # see InfoArray.__array_finalize__ for comments if obj is None: return diff --git a/GPy/core/parameterization/parameterized.py b/GPy/core/parameterization/parameterized.py index 18cea61a..f5fcc6ad 100644 --- a/GPy/core/parameterization/parameterized.py +++ b/GPy/core/parameterization/parameterized.py @@ -64,6 +64,30 @@ class Parameterized(Parameterizable, Pickleable, Gradcheckable): self._connect_parameters() del self._in_init_ + def build_pydot(self, G=None): + import pydot + iamroot = False + if G is None: + G = pydot.Dot(graph_type='digraph') + iamroot=True + node = pydot.Node(id(self), shape='record', label=self.name) + G.add_node(node) + for child in self._parameters_: + child_node = child.build_pydot(G) + G.add_edge(pydot.Edge(node, child_node)) + + for o in self._observer_callables_.keys(): + label = o.name if hasattr(o, 'name') else str(o) + observed_node = pydot.Node(id(o), label=label) + G.add_node(observed_node) + edge = pydot.Edge(str(id(self)), str(id(o)), color='darkorange2', arrowhead='vee') + G.add_edge(edge) + + if iamroot: + return G + return node + + def add_parameter(self, param, index=None): """ :param parameters: the parameters to add diff --git a/GPy/kern/_src/add.py b/GPy/kern/_src/add.py index 1466800c..77fe057d 100644 --- a/GPy/kern/_src/add.py +++ b/GPy/kern/_src/add.py @@ -184,20 +184,12 @@ class Add(Kern): target_S += b return target_mu, target_S - def plot(self, *args, **kwargs): - """ - See GPy.plotting.matplot_dep.plot - """ - assert "matplotlib" in sys.modules, "matplotlib package has not been imported." - from ..plotting.matplot_dep import kernel_plots - kernel_plots.plot(self,*args) - def input_sensitivity(self): in_sen = np.zeros((self.num_params, self.input_dim)) for i, [p, i_s] in enumerate(zip(self._parameters_, self.input_slices)): in_sen[i, i_s] = p.input_sensitivity() return in_sen - + def _getstate(self): """ Get the current state of the class, diff --git a/GPy/kern/_src/coregionalize.py b/GPy/kern/_src/coregionalize.py index cafdd5ee..6679eba4 100644 --- a/GPy/kern/_src/coregionalize.py +++ b/GPy/kern/_src/coregionalize.py @@ -60,17 +60,6 @@ class Coregionalize(Kern): def K(self, X, X2=None): index = np.asarray(X, dtype=np.int) - #here's the old code (numpy) - #if index2 is None: - #index2 = index - #else: - #index2 = np.asarray(index2, dtype=np.int) - #false_target = target.copy() - #ii, jj = np.meshgrid(index, index2) - #ii, jj = ii.T, jj.T - #false_target += self.B[ii, jj] - - if X2 is None: target = np.empty((X.shape[0], X.shape[0]), dtype=np.float64) code=""" diff --git a/GPy/kern/_src/kern.py b/GPy/kern/_src/kern.py index d7d8f9ca..eb3291e0 100644 --- a/GPy/kern/_src/kern.py +++ b/GPy/kern/_src/kern.py @@ -68,6 +68,14 @@ class Kern(Parameterized): """ raise NotImplementedError + def plot(self, *args, **kwargs): + """ + See GPy.plotting.matplot_dep.plot + """ + assert "matplotlib" in sys.modules, "matplotlib package has not been imported." + from ..plotting.matplot_dep import kernel_plots + kernel_plots.plot(self,*args) + def plot_ARD(self, *args, **kw): """ See :class:`~GPy.plotting.matplot_dep.kernel_plots` diff --git a/GPy/kern/_src/rbf.py b/GPy/kern/_src/rbf.py index 5e973aee..342b065e 100644 --- a/GPy/kern/_src/rbf.py +++ b/GPy/kern/_src/rbf.py @@ -179,6 +179,11 @@ class RBF(Stationary): return denom, dist, dist_sq, psi1 + #@cache_this(ignore_args=(1,)) + def _Z_distances(self, Z): + Zhat = 0.5 * (Z[:, None, :] + Z[None, :, :]) # M,M,Q + Zdist = 0.5 * (Z[:, None, :] - Z[None, :, :]) # M,M,Q + return Zhat, Zdist @Cache_this(limit=1) def _psi2computations(self, Z, vp): @@ -188,8 +193,7 @@ class RBF(Stationary): M = Z.shape[0] #compute required distances - Zhat = 0.5 * (Z[:, None, :] + Z[None, :, :]) # M,M,Q - Zdist = 0.5 * (Z[:, None, :] - Z[None, :, :]) # M,M,Q + Zhat, Zdist = self._Z_distances(Z) Zdist_sq = np.square(Zdist / self.lengthscale) # M,M,Q #allocate memory for the things we want to compute @@ -202,7 +206,7 @@ class RBF(Stationary): denom = (2.*S[:,None,None,:] / l2) + 1. # N,Q half_log_denom = 0.5 * np.log(denom[:,0,0,:]) denom_l2 = denom[:,0,0,:]*l2 - + variance_sq = float(np.square(self.variance)) code = """ double tmp, exponent_tmp; @@ -238,7 +242,7 @@ class RBF(Stationary): } } """ - + support_code = """ #include #include diff --git a/GPy/plotting/matplot_dep/base_plots.py b/GPy/plotting/matplot_dep/base_plots.py index a9d25223..e86ef6ca 100644 --- a/GPy/plotting/matplot_dep/base_plots.py +++ b/GPy/plotting/matplot_dep/base_plots.py @@ -18,17 +18,17 @@ def meanplot(x, mu, color=Tango.colorsHex['darkBlue'], ax=None, fignum=None, lin _, axes = ax_default(fignum, ax) #here's the mean return axes.plot(x,mu,color=color,linewidth=linewidth,**kw) - + def gpplot(x,mu,lower,upper,edgecol=Tango.colorsHex['darkBlue'],fillcol=Tango.colorsHex['lightBlue'],ax=None,fignum=None,xlabel='x',ylabel='y',**kwargs): - _, axes = ax_default(ax, fignum) - + _, axes = ax_default(fignum, ax) + mu = mu.flatten() x = x.flatten() lower = lower.flatten() upper = upper.flatten() plots = [] - + #here's the mean plots.append(meanplot(x, mu, edgecol, axes)) @@ -39,15 +39,15 @@ def gpplot(x,mu,lower,upper,edgecol=Tango.colorsHex['darkBlue'],fillcol=Tango.co plots.append(axes.fill(np.hstack((x,x[::-1])),np.hstack((upper,lower[::-1])),color=fillcol,**kwargs)) #this is the edge: - plots.append(meanplot(x, upper,color=edgecol,linewidth=0.2,axes=axes)) - plots.append(meanplot(x, lower,color=edgecol,linewidth=0.2,axes=axes)) - + plots.append(meanplot(x, upper,color=edgecol,linewidth=0.2,ax=axes)) + plots.append(meanplot(x, lower,color=edgecol,linewidth=0.2,ax=axes)) + axes.set_xlabel(xlabel) axes.set_ylabel(ylabel) - + return plots - - + + def removeRightTicks(ax=None): ax = ax or pb.gca() for i, line in enumerate(ax.get_yticklines()): diff --git a/GPy/plotting/matplot_dep/models_plots.py b/GPy/plotting/matplot_dep/models_plots.py index 5123f514..d72d2a3e 100644 --- a/GPy/plotting/matplot_dep/models_plots.py +++ b/GPy/plotting/matplot_dep/models_plots.py @@ -86,7 +86,7 @@ def plot_fit(model, plot_limits=None, which_data_rows='all', m, v, lower, upper = model.predict(Xgrid) Y = Y for d in which_data_ycols: - gpplot(Xnew, m[:, d], lower[:, d], upper[:, d], axes=ax, edgecol=linecol, fillcol=fillcol) + gpplot(Xnew, m[:, d], lower[:, d], upper[:, d], ax=ax, edgecol=linecol, fillcol=fillcol) ax.plot(X[which_data_rows,free_dims], Y[which_data_rows, d], 'kx', mew=1.5) #optionally plot some samples