Merge pull request #1070 from SheffieldML/1069-import-gpy-fails-with-matplotlib-390

1069 import gpy fails with matplotlib 390
This commit is contained in:
Martin Bubel 2024-05-20 15:02:35 +02:00 committed by GitHub
commit 08d182bd88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 394 additions and 258 deletions

View file

@ -2,6 +2,8 @@
## Unreleased ## Unreleased
+ update import in `.plotting.matplot_dep.defaults` due to change in matplotlib
## v1.13.1 (2024-01-14) ## v1.13.1 (2024-01-14)
* limit `scipy<1.12` as macos and linux jobs install some pre-release version of `scipy==1.12` which breaks tests * limit `scipy<1.12` as macos and linux jobs install some pre-release version of `scipy==1.12` which breaks tests

View file

@ -13,6 +13,7 @@ import warnings
from scipy import special as special from scipy import special as special
class sde_StdPeriodic(StdPeriodic): class sde_StdPeriodic(StdPeriodic):
""" """
@ -27,6 +28,7 @@ class sde_StdPeriodic(StdPeriodic):
\left( \frac{\sin(\frac{\pi}{\lambda_i} (x_i - y_i) )}{l_i} \right)^2 \right] } \left( \frac{\sin(\frac{\pi}{\lambda_i} (x_i - y_i) )}{l_i} \right)^2 \right] }
""" """
# TODO: write comment to the constructor arguments # TODO: write comment to the constructor arguments
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
""" """
@ -44,16 +46,15 @@ class sde_StdPeriodic(StdPeriodic):
# import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
if 'approx_order' in kwargs: if "approx_order" in kwargs:
self.approx_order = kwargs.get('approx_order') self.approx_order = kwargs.get("approx_order")
del kwargs['approx_order'] del kwargs["approx_order"]
else: else:
self.approx_order = 7 self.approx_order = 7
if "balance" in kwargs:
if 'balance' in kwargs: self.balance = bool(kwargs.get("balance"))
self.balance = bool( kwargs.get('balance') ) del kwargs["balance"]
del kwargs['balance']
else: else:
self.balance = False self.balance = False
@ -106,12 +107,29 @@ class sde_StdPeriodic(StdPeriodic):
dq2l = 2 * dq2l # This is because the lengthscale if multiplied by 2. dq2l = 2 * dq2l # This is because the lengthscale if multiplied by 2.
eps = 1e-12 eps = 1e-12
if np.any( np.isfinite(q2) == False) or np.any( np.abs(q2) > 1.0/eps) or np.any( np.abs(q2) < eps): if (
warnings.warn("sde_Periodic: Infinite, too small, or too large (eps={0:e}) values in q2 :".format(eps) + q2.__format__("") ) np.any(np.isfinite(q2) == False)
or np.any(np.abs(q2) > 1.0 / eps)
if np.any( np.isfinite(dq2l) == False) or np.any( np.abs(dq2l) > 1.0/eps) or np.any( np.abs(dq2l) < eps): or np.any(np.abs(q2) < eps)
warnings.warn("sde_Periodic: Infinite, too small, or too large (eps={0:e}) values in dq2l :".format(eps) + q2.__format__("") ) ):
warnings.warn(
"sde_Periodic: Infinite, too small, or too large (eps={0:e}) values in q2 :".format(
eps
)
+ q2.__format__("")
)
if (
np.any(np.isfinite(dq2l) == False)
or np.any(np.abs(dq2l) > 1.0 / eps)
or np.any(np.abs(dq2l) < eps)
):
warnings.warn(
"sde_Periodic: Infinite, too small, or too large (eps={0:e}) values in dq2l :".format(
eps
)
+ q2.__format__("")
)
F = np.kron(np.diag(range(0, N + 1)), np.array(((0, -w0), (w0, 0)))) F = np.kron(np.diag(range(0, N + 1)), np.array(((0, -w0), (w0, 0))))
L = np.eye(2 * (N + 1)) L = np.eye(2 * (N + 1))
@ -131,7 +149,9 @@ class sde_StdPeriodic(StdPeriodic):
dP_inf[:, :, 0] = P_inf / p_variance dP_inf[:, :, 0] = P_inf / p_variance
# Derivatives self.period # Derivatives self.period
dF[:,:,1] = np.kron(np.diag(range(0,N+1)),np.array( ((0, w0), (-w0, 0)) ) / p_period ); dF[:, :, 1] = np.kron(
np.diag(range(0, N + 1)), np.array(((0, w0), (-w0, 0))) / p_period
)
dQc[:, :, 1] = np.zeros(Qc.shape) dQc[:, :, 1] = np.zeros(Qc.shape)
dP_inf[:, :, 1] = np.zeros(P_inf.shape) dP_inf[:, :, 1] = np.zeros(P_inf.shape)
@ -144,13 +164,14 @@ class sde_StdPeriodic(StdPeriodic):
if self.balance: if self.balance:
# Benefits of this are not very sound. # Benefits of this are not very sound.
import GPy.models.state_space_main as ssm import GPy.models.state_space_main as ssm
(F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf,dP0) = ssm.balance_ss_model(F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf, dP0 )
(F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf, dP0) = ssm.balance_ss_model(
F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf, dP0
)
return (F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf, dP0) return (F, L, Qc, H, P_inf, P0, dF, dQc, dP_inf, dP0)
def seriescoeff(m=6, lengthScale=1.0, magnSigma2=1.0, true_covariance=False): def seriescoeff(m=6, lengthScale=1.0, magnSigma2=1.0, true_covariance=False):
""" """
Calculate the coefficients q_j^2 for the covariance function Calculate the coefficients q_j^2 for the covariance function
@ -192,21 +213,39 @@ def seriescoeff(m=6,lengthScale=1.0,magnSigma2=1.0, true_covariance=False):
if true_covariance: if true_covariance:
bb = lambda j,m: (1.0 + np.array((j != 0), dtype=np.float64) ) / (2**(j)) *\ bb = (
sp.special.binom(j, sp.floor( (j-m)/2.0 * np.array(m<=j, dtype=np.float64) ))*\ lambda j, m: (1.0 + np.array((j != 0), dtype=np.float64))
np.array(m<=j, dtype=np.float64) *np.array(sp.mod(j-m,2)==0, dtype=np.float64) / (2 ** (j))
* sp.special.binom(
j, sp.floor((j - m) / 2.0 * np.array(m <= j, dtype=np.float64))
)
* np.array(m <= j, dtype=np.float64)
* np.array(sp.mod(j - m, 2) == 0, dtype=np.float64)
)
M, J = np.meshgrid(range(0, m + 1), range(0, m + 1)) M, J = np.meshgrid(range(0, m + 1), range(0, m + 1))
coeffs = bb(J,M) / sp.misc.factorial(J) * sp.exp( -lengthScale**(-2) ) *\ coeffs = (
(lengthScale**(-2))**J *magnSigma2 bb(J, M)
/ sp.misc.factorial(J)
* np.exp(-(lengthScale ** (-2)))
* (lengthScale ** (-2)) ** J
* magnSigma2
)
coeffs_dl = np.sum( coeffs*lengthScale**(-3)*(2.0-2.0*J*lengthScale**2),0) coeffs_dl = np.sum(
coeffs * lengthScale ** (-3) * (2.0 - 2.0 * J * lengthScale**2), 0
)
coeffs = np.sum(coeffs, 0) coeffs = np.sum(coeffs, 0)
else: else:
coeffs = 2*magnSigma2*sp.exp( -lengthScale**(-2) ) * special.iv(range(0,m+1),1.0/lengthScale**(2)) coeffs = (
2
* magnSigma2
* np.exp(-(lengthScale ** (-2)))
* special.iv(range(0, m + 1), 1.0 / lengthScale ** (2))
)
if np.any(np.isfinite(coeffs) == False): if np.any(np.isfinite(coeffs) == False):
raise ValueError("sde_standard_periodic: Coefficients are not finite!") raise ValueError("sde_standard_periodic: Coefficients are not finite!")
# import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
@ -214,12 +253,27 @@ def seriescoeff(m=6,lengthScale=1.0,magnSigma2=1.0, true_covariance=False):
# print(coeffs) # print(coeffs)
# Derivatives wrt (lengthScale) # Derivatives wrt (lengthScale)
coeffs_dl = np.zeros(m + 1) coeffs_dl = np.zeros(m + 1)
coeffs_dl[1:] = magnSigma2*lengthScale**(-3) * sp.exp(-lengthScale**(-2))*\ coeffs_dl[1:] = (
(-4*special.iv(range(0,m),lengthScale**(-2)) + 4*(1+np.arange(1,m+1)*lengthScale**(2))*special.iv(range(1,m+1),lengthScale**(-2)) ) magnSigma2
* lengthScale ** (-3)
* np.exp(-(lengthScale ** (-2)))
* (
-4 * special.iv(range(0, m), lengthScale ** (-2))
+ 4
* (1 + np.arange(1, m + 1) * lengthScale ** (2))
* special.iv(range(1, m + 1), lengthScale ** (-2))
)
)
# The first element # The first element
coeffs_dl[0] = magnSigma2*lengthScale**(-3) * np.exp(-lengthScale**(-2))*\ coeffs_dl[0] = (
(2*special.iv(0,lengthScale**(-2)) - 2*special.iv(1,lengthScale**(-2)) ) magnSigma2
* lengthScale ** (-3)
* np.exp(-(lengthScale ** (-2)))
* (
2 * special.iv(0, lengthScale ** (-2))
- 2 * special.iv(1, lengthScale ** (-2))
)
)
return coeffs.squeeze(), coeffs_dl.squeeze() return coeffs.squeeze(), coeffs_dl.squeeze()

View file

@ -11,12 +11,14 @@ from .stationary import RatQuad
import numpy as np import numpy as np
import scipy as sp import scipy as sp
try: try:
from scipy.linalg import solve_continuous_lyapunov as lyap from scipy.linalg import solve_continuous_lyapunov as lyap
except ImportError: except ImportError:
from scipy.linalg import solve_lyapunov as lyap from scipy.linalg import solve_lyapunov as lyap
import warnings import warnings
class sde_RBF(RBF): class sde_RBF(RBF):
""" """
@ -30,6 +32,7 @@ class sde_RBF(RBF):
k(r) = \sigma^2 \exp \\bigg(- \\frac{1}{2} r^2 \\bigg) \\ \\ \\ \\ \text{ where } r = \sqrt{\sum_{i=1}^{input dim} \frac{(x_i-y_i)^2}{\ell_i^2} } k(r) = \sigma^2 \exp \\bigg(- \\frac{1}{2} r^2 \\bigg) \\ \\ \\ \\ \text{ where } r = \sqrt{\sum_{i=1}^{input dim} \frac{(x_i-y_i)^2}{\ell_i^2} }
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
""" """
Init constructior. Init constructior.
@ -44,21 +47,18 @@ class sde_RBF(RBF):
:type balance: bool :type balance: bool
""" """
if 'balance' in kwargs: if "balance" in kwargs:
self.balance = bool( kwargs.get('balance') ) self.balance = bool(kwargs.get("balance"))
del kwargs['balance'] del kwargs["balance"]
else: else:
self.balance = True self.balance = True
if "approx_order" in kwargs:
if 'approx_order' in kwargs: self.approx_order = kwargs.get("approx_order")
self.approx_order = kwargs.get('approx_order') del kwargs["approx_order"]
del kwargs['approx_order']
else: else:
self.approx_order = 6 self.approx_order = 6
super(sde_RBF, self).__init__(*args, **kwargs) super(sde_RBF, self).__init__(*args, **kwargs)
def sde_update_gradient_full(self, gradients): def sde_update_gradient_full(self, gradients):
@ -101,19 +101,29 @@ class sde_RBF(RBF):
eps = 1e-12 eps = 1e-12
if (float(Qc) > 1.0 / eps) or (float(Qc) < eps): if (float(Qc) > 1.0 / eps) or (float(Qc) < eps):
warnings.warn("""sde_RBF kernel: the noise variance Qc is either very large or very small. warnings.warn(
It influece conditioning of P_inf: {0:e}""".format(float(Qc)) ) """sde_RBF kernel: the noise variance Qc is either very large or very small.
It influece conditioning of P_inf: {0:e}""".format(
float(Qc)
)
)
pp1 = np.zeros((2*N+1,)) # array of polynomial coefficients from higher power to lower pp1 = np.zeros(
(2 * N + 1,)
) # array of polynomial coefficients from higher power to lower
for n in range(0, N + 1): # (2N+1) - number of polynomial coefficients for n in range(0, N + 1): # (2N+1) - number of polynomial coefficients
pp1[2*(N-n)] = fn*(4.0*kappa)**(N-n)/np.math.factorial(n)*(-1)**n pp1[2 * (N - n)] = (
fn * (4.0 * kappa) ** (N - n) / np.math.factorial(n) * (-1) ** n
)
pp = sp.poly1d(pp1) pp = np.poly1d(pp1)
roots = sp.roots(pp) roots = np.roots(pp)
neg_real_part_roots = roots[np.round(np.real(roots) ,roots_rounding_decimals) < 0] neg_real_part_roots = roots[
aa = sp.poly1d(neg_real_part_roots, r=True).coeffs np.round(np.real(roots), roots_rounding_decimals) < 0
]
aa = np.poly1d(neg_real_part_roots, r=True).coeffs
F = np.diag(np.ones((N - 1,)), 1) F = np.diag(np.ones((N - 1,)), 1)
F[-1, :] = -aa[-1:0:-1] F[-1, :] = -aa[-1:0:-1]
@ -138,12 +148,27 @@ class sde_RBF(RBF):
dFlengthscale[-1, :] = -aa[-1:0:-1] / p_lengthscale * np.arange(-N, 0, 1) dFlengthscale[-1, :] = -aa[-1:0:-1] / p_lengthscale * np.arange(-N, 0, 1)
dQcvariance = Qc / p_variance dQcvariance = Qc / p_variance
dQclengthscale = np.array(( (p_variance*np.sqrt(2*np.pi)*fn*2**N*p_lengthscale**(-2*N)*(1-2*N),),)) dQclengthscale = np.array(
(
(
p_variance
* np.sqrt(2 * np.pi)
* fn
* 2**N
* p_lengthscale ** (-2 * N)
* (1 - 2 * N),
),
)
)
dPinf_variance = Pinf / p_variance dPinf_variance = Pinf / p_variance
lp = Pinf.shape[0] lp = Pinf.shape[0]
coeff = np.arange(1,lp+1).reshape(lp,1) + np.arange(1,lp+1).reshape(1,lp) - 2 coeff = (
np.arange(1, lp + 1).reshape(lp, 1)
+ np.arange(1, lp + 1).reshape(1, lp)
- 2
)
coeff[np.mod(coeff, 2) != 0] = 0 coeff[np.mod(coeff, 2) != 0] = 0
dPinf_lengthscale = -1 / p_lengthscale * Pinf * coeff dPinf_lengthscale = -1 / p_lengthscale * Pinf * coeff
@ -161,10 +186,14 @@ class sde_RBF(RBF):
# Benefits of this are not very sound. Helps only in one case: # Benefits of this are not very sound. Helps only in one case:
# SVD Kalman + RBF kernel # SVD Kalman + RBF kernel
import GPy.models.state_space_main as ssm import GPy.models.state_space_main as ssm
(F, L, Qc, H, Pinf, P0, dF, dQc, dPinf,dP0) = ssm.balance_ss_model(F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0 )
(F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0) = ssm.balance_ss_model(
F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0
)
return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0) return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0)
class sde_Exponential(Exponential): class sde_Exponential(Exponential):
""" """
@ -202,9 +231,9 @@ class sde_Exponential(Exponential):
Pinf = np.array(((variance,),)) Pinf = np.array(((variance,),))
P0 = Pinf.copy() P0 = Pinf.copy()
dF = np.zeros((1,1,2)); dF = np.zeros((1, 1, 2))
dQc = np.zeros((1,1,2)); dQc = np.zeros((1, 1, 2))
dPinf = np.zeros((1,1,2)); dPinf = np.zeros((1, 1, 2))
dF[:, :, 0] = 0.0 dF[:, :, 0] = 0.0
dF[:, :, 1] = 1.0 / lengthscale**2 dF[:, :, 1] = 1.0 / lengthscale**2
@ -219,6 +248,7 @@ class sde_Exponential(Exponential):
return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0) return (F, L, Qc, H, Pinf, P0, dF, dQc, dPinf, dP0)
class sde_RatQuad(RatQuad): class sde_RatQuad(RatQuad):
""" """
@ -238,7 +268,7 @@ class sde_RatQuad(RatQuad):
Return the state space representation of the covariance. Return the state space representation of the covariance.
""" """
assert False, 'Not Implemented' assert False, "Not Implemented"
# Params to use: # Params to use:

View file

@ -28,10 +28,10 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# =============================================================================== # ===============================================================================
from matplotlib import cm from matplotlib import pyplot
from .. import Tango from .. import Tango
''' """
This file is for defaults for the gpy plot, specific to the plotting library. This file is for defaults for the gpy plot, specific to the plotting library.
Create a kwargs dictionary with the right name for the plotting function Create a kwargs dictionary with the right name for the plotting function
@ -40,36 +40,55 @@ the plotting library will be used.
In the code, always ise plotting.gpy_plots.defaults to get the defaults, as In the code, always ise plotting.gpy_plots.defaults to get the defaults, as
it gives back an empty default, when defaults are not defined. it gives back an empty default, when defaults are not defined.
''' """
# Data plots: # Data plots:
data_1d = dict(lw=1.5, marker='x', color='k') data_1d = dict(lw=1.5, marker="x", color="k")
data_2d = dict(s=35, edgecolors='none', linewidth=0., cmap=cm.get_cmap('hot'), alpha=.5) data_2d = dict(
inducing_1d = dict(lw=0, s=500, color=Tango.colorsHex['darkRed']) s=35, edgecolors="none", linewidth=0.0, cmap=pyplot.get_cmap("hot"), alpha=0.5
inducing_2d = dict(s=17, edgecolor='k', linewidth=.4, color='white', alpha=.5, marker='^') )
inducing_3d = dict(lw=.3, s=500, color=Tango.colorsHex['darkRed'], edgecolor='k') inducing_1d = dict(lw=0, s=500, color=Tango.colorsHex["darkRed"])
xerrorbar = dict(color='k', fmt='none', elinewidth=.5, alpha=.5) inducing_2d = dict(
yerrorbar = dict(color=Tango.colorsHex['darkRed'], fmt='none', elinewidth=.5, alpha=.5) s=17, edgecolor="k", linewidth=0.4, color="white", alpha=0.5, marker="^"
)
inducing_3d = dict(lw=0.3, s=500, color=Tango.colorsHex["darkRed"], edgecolor="k")
xerrorbar = dict(color="k", fmt="none", elinewidth=0.5, alpha=0.5)
yerrorbar = dict(
color=Tango.colorsHex["darkRed"], fmt="none", elinewidth=0.5, alpha=0.5
)
# GP plots: # GP plots:
meanplot_1d = dict(color=Tango.colorsHex['mediumBlue'], linewidth=2) meanplot_1d = dict(color=Tango.colorsHex["mediumBlue"], linewidth=2)
meanplot_2d = dict(cmap='hot', linewidth=.5) meanplot_2d = dict(cmap="hot", linewidth=0.5)
meanplot_3d = dict(linewidth=0, antialiased=True, cstride=1, rstride=1, cmap='hot', alpha=.3) meanplot_3d = dict(
samples_1d = dict(color=Tango.colorsHex['mediumBlue'], linewidth=.3) linewidth=0, antialiased=True, cstride=1, rstride=1, cmap="hot", alpha=0.3
samples_3d = dict(cmap='hot', alpha=.1, antialiased=True, cstride=1, rstride=1, linewidth=0) )
confidence_interval = dict(edgecolor=Tango.colorsHex['darkBlue'], linewidth=.5, color=Tango.colorsHex['lightBlue'],alpha=.2) samples_1d = dict(color=Tango.colorsHex["mediumBlue"], linewidth=0.3)
density = dict(alpha=.5, color=Tango.colorsHex['lightBlue']) samples_3d = dict(
cmap="hot", alpha=0.1, antialiased=True, cstride=1, rstride=1, linewidth=0
)
confidence_interval = dict(
edgecolor=Tango.colorsHex["darkBlue"],
linewidth=0.5,
color=Tango.colorsHex["lightBlue"],
alpha=0.2,
)
density = dict(alpha=0.5, color=Tango.colorsHex["lightBlue"])
# GPLVM plots: # GPLVM plots:
data_y_1d = dict(linewidth=0, cmap='RdBu', s=40) data_y_1d = dict(linewidth=0, cmap="RdBu", s=40)
data_y_1d_plot = dict(color='k', linewidth=1.5) data_y_1d_plot = dict(color="k", linewidth=1.5)
# Kernel plots: # Kernel plots:
ard = dict(edgecolor='k', linewidth=1.2) ard = dict(edgecolor="k", linewidth=1.2)
# Input plots: # Input plots:
latent = dict(aspect='auto', cmap='Greys', interpolation='bicubic') latent = dict(aspect="auto", cmap="Greys", interpolation="bicubic")
gradient = dict(aspect='auto', cmap='RdBu', interpolation='nearest', alpha=.7) gradient = dict(aspect="auto", cmap="RdBu", interpolation="nearest", alpha=0.7)
magnification = dict(aspect='auto', cmap='Greys', interpolation='bicubic') magnification = dict(aspect="auto", cmap="Greys", interpolation="bicubic")
latent_scatter = dict(s=20, linewidth=.2, edgecolor='k', alpha=.9) latent_scatter = dict(s=20, linewidth=0.2, edgecolor="k", alpha=0.9)
annotation = dict(fontdict=dict(family='sans-serif', weight='light', fontsize=9), zorder=.3, alpha=.7) annotation = dict(
fontdict=dict(family="sans-serif", weight="light", fontsize=9),
zorder=0.3,
alpha=0.7,
)

View file

@ -1,4 +1,6 @@
from matplotlib import pyplot as pb, numpy as np from matplotlib import pyplot as pb
import numpy as np
def plot(parameterized, fignum=None, ax=None, colors=None, figsize=(12, 6)): def plot(parameterized, fignum=None, ax=None, colors=None, figsize=(12, 6)):
""" """
@ -17,6 +19,7 @@ def plot(parameterized, fignum=None, ax=None, colors=None, figsize=(12, 6)):
if colors is None: if colors is None:
from ..Tango import mediumList from ..Tango import mediumList
from itertools import cycle from itertools import cycle
colors = cycle(mediumList) colors = cycle(mediumList)
pb.clf() pb.clf()
else: else:
@ -33,21 +36,30 @@ def plot(parameterized, fignum=None, ax=None, colors=None, figsize=(12, 6)):
a = ax[i] a = ax[i]
else: else:
raise ValueError("Need one ax per latent dimension input_dim") raise ValueError("Need one ax per latent dimension input_dim")
bg_lines.append(a.plot(means, c='k', alpha=.3)) bg_lines.append(a.plot(means, c="k", alpha=0.3))
lines.extend(a.plot(x, means.T[i], c=next(colors), label=r"$\mathbf{{X_{{{}}}}}$".format(i))) lines.extend(
fills.append(a.fill_between(x, a.plot(
x, means.T[i], c=next(colors), label=r"$\mathbf{{X_{{{}}}}}$".format(i)
)
)
fills.append(
a.fill_between(
x,
means.T[i] - 2 * np.sqrt(variances.T[i]), means.T[i] - 2 * np.sqrt(variances.T[i]),
means.T[i] + 2 * np.sqrt(variances.T[i]), means.T[i] + 2 * np.sqrt(variances.T[i]),
facecolor=lines[-1].get_color(), facecolor=lines[-1].get_color(),
alpha=.3)) alpha=0.3,
a.legend(borderaxespad=0.) )
)
a.legend(borderaxespad=0.0)
a.set_xlim(x.min(), x.max()) a.set_xlim(x.min(), x.max())
if i < means.shape[1] - 1: if i < means.shape[1] - 1:
a.set_xticklabels('') a.set_xticklabels("")
pb.draw() pb.draw()
a.figure.tight_layout(h_pad=.01) # , rect=(0, 0, 1, .95)) a.figure.tight_layout(h_pad=0.01) # , rect=(0, 0, 1, .95))
return dict(lines=lines, fills=fills, bg_lines=bg_lines) return dict(lines=lines, fills=fills, bg_lines=bg_lines)
def plot_SpikeSlab(parameterized, fignum=None, ax=None, colors=None, side_by_side=True): def plot_SpikeSlab(parameterized, fignum=None, ax=None, colors=None, side_by_side=True):
""" """
Plot latent space X in 1D: Plot latent space X in 1D:
@ -62,18 +74,27 @@ def plot_SpikeSlab(parameterized, fignum=None, ax=None, colors=None, side_by_sid
""" """
if ax is None: if ax is None:
if side_by_side: if side_by_side:
fig = pb.figure(num=fignum, figsize=(16, min(12, (2 * parameterized.mean.shape[1])))) fig = pb.figure(
num=fignum, figsize=(16, min(12, (2 * parameterized.mean.shape[1])))
)
else: else:
fig = pb.figure(num=fignum, figsize=(8, min(12, (2 * parameterized.mean.shape[1])))) fig = pb.figure(
num=fignum, figsize=(8, min(12, (2 * parameterized.mean.shape[1])))
)
if colors is None: if colors is None:
from ..Tango import mediumList from ..Tango import mediumList
from itertools import cycle from itertools import cycle
colors = cycle(mediumList) colors = cycle(mediumList)
pb.clf() pb.clf()
else: else:
colors = iter(colors) colors = iter(colors)
plots = [] plots = []
means, variances, gamma = parameterized.mean, parameterized.variance, parameterized.binary_prob means, variances, gamma = (
parameterized.mean,
parameterized.variance,
parameterized.binary_prob,
)
x = np.arange(means.shape[0]) x = np.arange(means.shape[0])
for i in range(means.shape[1]): for i in range(means.shape[1]):
if side_by_side: if side_by_side:
@ -85,22 +106,28 @@ def plot_SpikeSlab(parameterized, fignum=None, ax=None, colors=None, side_by_sid
# mean and variance plot # mean and variance plot
a = fig.add_subplot(*sub1) a = fig.add_subplot(*sub1)
a.plot(means, c='k', alpha=.3) a.plot(means, c="k", alpha=0.3)
plots.extend(a.plot(x, means.T[i], c=next(colors), label=r"$\mathbf{{X_{{{}}}}}$".format(i))) plots.extend(
a.fill_between(x, a.plot(
x, means.T[i], c=next(colors), label=r"$\mathbf{{X_{{{}}}}}$".format(i)
)
)
a.fill_between(
x,
means.T[i] - 2 * np.sqrt(variances.T[i]), means.T[i] - 2 * np.sqrt(variances.T[i]),
means.T[i] + 2 * np.sqrt(variances.T[i]), means.T[i] + 2 * np.sqrt(variances.T[i]),
facecolor=plots[-1].get_color(), facecolor=plots[-1].get_color(),
alpha=.3) alpha=0.3,
a.legend(borderaxespad=0.) )
a.legend(borderaxespad=0.0)
a.set_xlim(x.min(), x.max()) a.set_xlim(x.min(), x.max())
if i < means.shape[1] - 1: if i < means.shape[1] - 1:
a.set_xticklabels('') a.set_xticklabels("")
# binary prob plot # binary prob plot
a = fig.add_subplot(*sub2) a = fig.add_subplot(*sub2)
a.bar(x,gamma[:,i],bottom=0.,linewidth=1.,width=1.0,align='center') a.bar(x, gamma[:, i], bottom=0.0, linewidth=1.0, width=1.0, align="center")
a.set_xlim(x.min(), x.max()) a.set_xlim(x.min(), x.max())
a.set_ylim([0.,1.]) a.set_ylim([0.0, 1.0])
pb.draw() pb.draw()
fig.tight_layout(h_pad=.01) # , rect=(0, 0, 1, .95)) fig.tight_layout(h_pad=0.01) # , rect=(0, 0, 1, .95))
return fig return fig

View file

@ -29,6 +29,7 @@ class TestGridModel:
self.dim = self.X.shape[1] self.dim = self.X.shape[1]
def test_alpha_match(self): def test_alpha_match(self):
self.setup()
kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True) kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True)
m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel) m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel)
@ -38,6 +39,7 @@ class TestGridModel:
np.testing.assert_almost_equal(m.posterior.alpha, m2.posterior.woodbury_vector) np.testing.assert_almost_equal(m.posterior.alpha, m2.posterior.woodbury_vector)
def test_gradient_match(self): def test_gradient_match(self):
self.setup()
kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True) kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True)
m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel) m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel)
@ -55,6 +57,7 @@ class TestGridModel:
) )
def test_prediction_match(self): def test_prediction_match(self):
self.setup()
kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True) kernel = GPy.kern.RBF(input_dim=self.dim, variance=1, ARD=True)
m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel) m = GPy.models.GPRegressionGrid(self.X, self.Y, kernel)

View file

@ -544,6 +544,7 @@ class TestKernelGradientContinuous:
assert check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose) assert check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose)
def test_OU(self): def test_OU(self):
self.setup()
k = GPy.kern.OU(self.D - 1, ARD=True) k = GPy.kern.OU(self.D - 1, ARD=True)
k.randomize() k.randomize()
assert check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose) assert check_kernel_gradient_functions(k, X=self.X, X2=self.X2, verbose=verbose)