migrate ep_likelihood_test to pytest

This commit is contained in:
Martin Bubel 2023-10-04 20:42:54 +02:00
parent 1e5dd36bc7
commit 4d41594e18

View file

@ -3,14 +3,18 @@ import unittest
import GPy
from GPy.models import GradientChecker
fixed_seed = 10
from nose.tools import with_setup, nottest
def rmse(Y, Ystar):
return np.sqrt(np.mean((Y - Ystar) ** 2))
# this file will contain some high level tests, this is not unit testing, but will give us a higher level estimate
# if things are going well under the hood.
class TestObservationModels(unittest.TestCase):
def setUp(self):
class TestObservationModels:
def setup(self):
np.random.seed(fixed_seed)
self.N = 100
self.D = 2
@ -38,15 +42,17 @@ class TestObservationModels(unittest.TestCase):
self.Y_metadata["censored"] = censored
self.kernel1 = GPy.kern.RBF(self.X.shape[1]) + GPy.kern.White(self.X.shape[1])
def tearDown(self):
def tear_down(self):
self.Y = None
self.X = None
self.binary_Y = None
self.positive_Y = None
self.kernel1 = None
@with_setup(setUp, tearDown)
def testEPClassification(self):
def test_epccassification(self):
self.setup()
self.tear_down()
bernoulli = GPy.likelihoods.Bernoulli()
laplace_inf = GPy.inference.latent_function_inference.Laplace()
@ -111,23 +117,22 @@ class TestObservationModels(unittest.TestCase):
probs_mean_ep_nested, probs_var_ep_nested = m3.predict(self.X)
# for simple single dimension data , marginal likelihood for laplace and EP approximations should not be so far apart.
self.assertAlmostEqual(m1.log_likelihood(), m2.log_likelihood(), delta=1)
self.assertAlmostEqual(m1.log_likelihood(), m3.log_likelihood(), delta=1)
self.assertAlmostEqual(m1.log_likelihood(), m4.log_likelihood(), delta=5)
# TODO: the below were assertAlmostEqual, not sure if allclose will do the job here
assert np.allclose(m1.log_likelihood(), m2.log_likelihood()) # , delta=1
assert np.allcose(m1.log_likelihood(), m3.log_likelihood()) # , delta=1
assert np.allclose(m1.log_likelihood(), m4.log_likelihood()) # , delta=5
GPy.util.classification.conf_matrix(probs_mean_lap, self.binary_Y)
GPy.util.classification.conf_matrix(probs_mean_ep_alt, self.binary_Y)
GPy.util.classification.conf_matrix(probs_mean_ep_nested, self.binary_Y)
@nottest
def rmse(self, Y, Ystar):
return np.sqrt(np.mean((Y - Ystar) ** 2))
@with_setup(setUp, tearDown)
@unittest.skip(
@pytest.skip(
"Fails as a consequence of fixing the DSYR function. Needs to be reviewed!"
)
def test_EP_with_StudentT(self):
def test_ep_with_studentt(self):
self.setup()
self.tear_down()
studentT = GPy.likelihoods.StudentT(
deg_free=self.deg_free, sigma2=self.init_var
)
@ -171,7 +176,8 @@ class TestObservationModels(unittest.TestCase):
m2.optimize(optimizer=optimizer, max_iters=400)
# m3.optimize(optimizer=optimizer, max_iters=500)
self.assertAlmostEqual(m1.log_likelihood(), m2.log_likelihood(), delta=200)
# TODO: this was assertAlmostEqual, not sure if allclose will do the job here
assert np.allclose(m1.log_likelihood(), m2.log_likelihood()) # , delta=200
# self.assertAlmostEqual(m1.log_likelihood(), m3.log_likelihood(), 3)
@ -183,9 +189,6 @@ class TestObservationModels(unittest.TestCase):
# rmse_nested = self.rmse(preds_mean_nested, self.Y_noisy)
if rmse_alt > rmse_lap:
self.assertAlmostEqual(rmse_lap, rmse_alt, delta=1.5)
# TODO: this was assertAlmostEqual, not sure if allclose will do the job here
assert np.allclose(rmse_lap, rmse_alt) # , delta=1.5
# m3.optimize(optimizer=optimizer, max_iters=500)
if __name__ == "__main__":
unittest.main()