migrate minibatch_tests to pytest

This commit is contained in:
Martin Bubel 2023-10-08 23:41:44 +02:00
parent 4070fd68ff
commit 4685d10463

View file

@ -3,13 +3,18 @@ Created on 4 Sep 2015
@author: maxz
"""
import unittest
import pytest
import numpy as np
import GPy
try:
import climin
except ImportError:
climin = None
class BGPLVMTest(unittest.TestCase):
def setUp(self):
class TestBGPLVM:
def setup(self):
np.random.seed(12345)
X, W = np.random.normal(0, 1, (100, 6)), np.random.normal(0, 1, (6, 13))
Y = X.dot(W) + np.random.normal(0, 0.1, (X.shape[0], W.shape[1]))
@ -19,6 +24,7 @@ class BGPLVMTest(unittest.TestCase):
self.m_full = GPy.models.BayesianGPLVM(Y, self.Q)
def test_lik_comparisons_m1_s0(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
@ -32,6 +38,7 @@ class BGPLVMTest(unittest.TestCase):
assert m.checkgrad()
def test_predict_missing_data(self):
self.setup()
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
self.Y,
self.Q,
@ -63,6 +70,7 @@ class BGPLVMTest(unittest.TestCase):
np.testing.assert_allclose(var1[:, [0]], var2)
def test_lik_comparisons_m0_s0(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
@ -80,6 +88,7 @@ class BGPLVMTest(unittest.TestCase):
assert m.checkgrad()
def test_lik_comparisons_m1_s1(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
@ -97,6 +106,7 @@ class BGPLVMTest(unittest.TestCase):
assert m.checkgrad()
def test_lik_comparisons_m0_s1(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
@ -114,6 +124,7 @@ class BGPLVMTest(unittest.TestCase):
assert m.checkgrad()
def test_gradients_missingdata(self):
self.seutp()
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
self.Y,
self.Q,
@ -124,6 +135,7 @@ class BGPLVMTest(unittest.TestCase):
assert m.checkgrad()
def test_gradients_missingdata_stochastics(self):
self.setup()
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
self.Y, self.Q, missing_data=True, stochastic=True, batchsize=1
)
@ -134,6 +146,7 @@ class BGPLVMTest(unittest.TestCase):
assert m.checkgrad()
def test_gradients_stochastics(self):
self.setup()
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
self.Y, self.Q, missing_data=False, stochastic=True, batchsize=1
)
@ -144,6 +157,7 @@ class BGPLVMTest(unittest.TestCase):
assert m.checkgrad()
def test_predict(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
@ -161,8 +175,8 @@ class BGPLVMTest(unittest.TestCase):
assert m.checkgrad()
class SparseGPMinibatchTest(unittest.TestCase):
def setUp(self):
class TestSparseGPMinibatch:
def setup(self):
np.random.seed(12345)
X, W = np.random.normal(0, 1, (100, 6)), np.random.normal(0, 1, (6, 13))
Y = X.dot(W) + np.random.normal(0, 0.1, (X.shape[0], W.shape[1]))
@ -174,6 +188,7 @@ class SparseGPMinibatchTest(unittest.TestCase):
)
def test_lik_comparisons_m1_s0(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
@ -186,70 +201,68 @@ class SparseGPMinibatchTest(unittest.TestCase):
np.testing.assert_allclose(m.gradient, self.m_full.gradient)
assert m.checkgrad()
@pytest.mark.skipif(climin is None, reason="climin not installed")
def test_sparsegp_init(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
try:
np.random.seed(1234)
Z = self.X[np.random.choice(self.X.shape[0], replace=False, size=10)].copy()
Q = Z.shape[1]
m = GPy.models.sparse_gp_minibatch.SparseGPMiniBatch(
self.X,
self.Y,
Z,
GPy.kern.RBF(Q) + GPy.kern.Matern32(Q) + GPy.kern.Bias(Q),
GPy.likelihoods.Gaussian(),
missing_data=True,
stochastic=False,
)
assert m.checkgrad()
m.optimize("adadelta", max_iters=10)
assert m.checkgrad()
np.random.seed(1234)
Z = self.X[np.random.choice(self.X.shape[0], replace=False, size=10)].copy()
Q = Z.shape[1]
m = GPy.models.sparse_gp_minibatch.SparseGPMiniBatch(
self.X,
self.Y,
Z,
GPy.kern.RBF(Q) + GPy.kern.Matern32(Q) + GPy.kern.Bias(Q),
GPy.likelihoods.Gaussian(),
missing_data=True,
stochastic=False,
)
assert m.checkgrad()
m.optimize("adadelta", max_iters=10)
assert m.checkgrad()
m = GPy.models.sparse_gp_minibatch.SparseGPMiniBatch(
self.X,
self.Y,
Z,
GPy.kern.RBF(Q) + GPy.kern.Matern32(Q) + GPy.kern.Bias(Q),
GPy.likelihoods.Gaussian(),
missing_data=True,
stochastic=True,
)
assert m.checkgrad()
m.optimize("rprop", max_iters=10)
assert m.checkgrad()
m = GPy.models.sparse_gp_minibatch.SparseGPMiniBatch(
self.X,
self.Y,
Z,
GPy.kern.RBF(Q) + GPy.kern.Matern32(Q) + GPy.kern.Bias(Q),
GPy.likelihoods.Gaussian(),
missing_data=True,
stochastic=True,
)
assert m.checkgrad()
m.optimize("rprop", max_iters=10)
assert m.checkgrad()
m = GPy.models.sparse_gp_minibatch.SparseGPMiniBatch(
self.X,
self.Y,
Z,
GPy.kern.RBF(Q) + GPy.kern.Matern32(Q) + GPy.kern.Bias(Q),
GPy.likelihoods.Gaussian(),
missing_data=False,
stochastic=False,
)
assert m.checkgrad()
m.optimize("rprop", max_iters=10)
assert m.checkgrad()
m = GPy.models.sparse_gp_minibatch.SparseGPMiniBatch(
self.X,
self.Y,
Z,
GPy.kern.RBF(Q) + GPy.kern.Matern32(Q) + GPy.kern.Bias(Q),
GPy.likelihoods.Gaussian(),
missing_data=False,
stochastic=False,
)
assert m.checkgrad()
m.optimize("rprop", max_iters=10)
assert m.checkgrad()
m = GPy.models.sparse_gp_minibatch.SparseGPMiniBatch(
self.X,
self.Y,
Z,
GPy.kern.RBF(Q) + GPy.kern.Matern32(Q) + GPy.kern.Bias(Q),
GPy.likelihoods.Gaussian(),
missing_data=False,
stochastic=True,
)
assert m.checkgrad()
m.optimize("adadelta", max_iters=10)
assert m.checkgrad()
except ImportError:
from nose import SkipTest
raise SkipTest("climin not installed, skipping stochastic gradients")
m = GPy.models.sparse_gp_minibatch.SparseGPMiniBatch(
self.X,
self.Y,
Z,
GPy.kern.RBF(Q) + GPy.kern.Matern32(Q) + GPy.kern.Bias(Q),
GPy.likelihoods.Gaussian(),
missing_data=False,
stochastic=True,
)
assert m.checkgrad()
m.optimize("adadelta", max_iters=10)
assert m.checkgrad()
def test_predict_missing_data(self):
self.setup()
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
self.Y,
self.Q,
@ -277,6 +290,7 @@ class SparseGPMinibatchTest(unittest.TestCase):
np.testing.assert_allclose(var1[:, :, i], var2)
def test_lik_comparisons_m0_s0(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
@ -290,6 +304,7 @@ class SparseGPMinibatchTest(unittest.TestCase):
assert m.checkgrad()
def test_lik_comparisons_m1_s1(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
@ -308,6 +323,7 @@ class SparseGPMinibatchTest(unittest.TestCase):
assert m.checkgrad()
def test_lik_comparisons_m0_s1(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
@ -326,6 +342,7 @@ class SparseGPMinibatchTest(unittest.TestCase):
assert m.checkgrad()
def test_gradients_missingdata(self):
self.setup()
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
self.Y,
self.Q,
@ -337,6 +354,7 @@ class SparseGPMinibatchTest(unittest.TestCase):
assert m.checkgrad()
def test_gradients_missingdata_stochastics(self):
self.setup()
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
self.Y,
self.Q,
@ -357,6 +375,7 @@ class SparseGPMinibatchTest(unittest.TestCase):
assert m.checkgrad()
def test_gradients_stochastics(self):
self.setup()
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
self.Y,
self.Q,
@ -377,6 +396,7 @@ class SparseGPMinibatchTest(unittest.TestCase):
assert m.checkgrad()
def test_predict(self):
self.setup()
# Test if the different implementations give the exact same likelihood as the full model.
# All of the following settings should give the same likelihood and gradients as the full model:
m = GPy.models.bayesian_gplvm_minibatch.BayesianGPLVMMiniBatch(
@ -393,8 +413,3 @@ class SparseGPMinibatchTest(unittest.TestCase):
)
np.testing.assert_allclose(m.gradient, self.m_full.gradient)
assert m.checkgrad()
if __name__ == "__main__":
# import sys;sys.argv = ['', 'Test.testName']
unittest.main()