2013-03-11 13:54:47 +00:00
|
|
|
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
|
|
|
|
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
import numpy as np
|
|
|
|
|
import GPy
|
2013-03-11 17:05:18 +00:00
|
|
|
import inspect
|
|
|
|
|
import pkgutil
|
|
|
|
|
import os
|
2013-03-11 18:09:47 +00:00
|
|
|
import random
|
2013-03-11 19:37:56 +00:00
|
|
|
from nose.tools import nottest
|
2013-06-05 13:02:03 +01:00
|
|
|
import sys
|
2013-03-11 13:54:47 +00:00
|
|
|
|
|
|
|
|
class ExamplesTests(unittest.TestCase):
|
2013-06-05 16:14:30 +01:00
|
|
|
def _checkgrad(self, Model):
|
|
|
|
|
self.assertTrue(Model.checkgrad())
|
2013-03-11 17:05:18 +00:00
|
|
|
|
2013-06-05 16:14:30 +01:00
|
|
|
def _model_instance(self, Model):
|
|
|
|
|
self.assertTrue(isinstance(Model, GPy.models))
|
2013-03-11 17:05:18 +00:00
|
|
|
|
|
|
|
|
"""
|
2013-06-05 16:14:30 +01:00
|
|
|
def model_instance_generator(Model):
|
2013-03-11 17:05:18 +00:00
|
|
|
def check_model_returned(self):
|
2013-06-05 16:14:30 +01:00
|
|
|
self._model_instance(Model)
|
2013-03-11 17:05:18 +00:00
|
|
|
return check_model_returned
|
|
|
|
|
|
2013-06-05 16:14:30 +01:00
|
|
|
def checkgrads_generator(Model):
|
2013-03-11 17:05:18 +00:00
|
|
|
def model_checkgrads(self):
|
2013-06-05 16:14:30 +01:00
|
|
|
self._checkgrad(Model)
|
2013-03-11 17:05:18 +00:00
|
|
|
return model_checkgrads
|
|
|
|
|
"""
|
2013-03-11 17:35:01 +00:00
|
|
|
|
2013-03-11 17:05:18 +00:00
|
|
|
def model_checkgrads(model):
|
2013-03-11 18:25:11 +00:00
|
|
|
model.randomize()
|
2013-06-05 17:15:26 +01:00
|
|
|
#assert model.checkgrad()
|
|
|
|
|
return model.checkgrad()
|
2013-03-11 13:54:47 +00:00
|
|
|
|
2013-03-11 17:05:18 +00:00
|
|
|
def model_instance(model):
|
2013-06-05 17:15:26 +01:00
|
|
|
#assert isinstance(model, GPy.core.model)
|
2013-06-05 17:19:35 +01:00
|
|
|
return isinstance(model, GPy.core.Model)
|
2013-03-11 13:54:47 +00:00
|
|
|
|
2013-03-11 19:37:56 +00:00
|
|
|
@nottest
|
2013-03-11 17:05:18 +00:00
|
|
|
def test_models():
|
|
|
|
|
examples_path = os.path.dirname(GPy.examples.__file__)
|
2013-06-05 13:02:03 +01:00
|
|
|
# Load modules
|
2013-06-05 17:15:26 +01:00
|
|
|
failing_models = {}
|
2013-03-11 17:05:18 +00:00
|
|
|
for loader, module_name, is_pkg in pkgutil.iter_modules([examples_path]):
|
2013-06-05 13:02:03 +01:00
|
|
|
# Load examples
|
2013-03-11 17:05:18 +00:00
|
|
|
module_examples = loader.find_module(module_name).load_module(module_name)
|
2013-03-11 17:35:01 +00:00
|
|
|
print "MODULE", module_examples
|
|
|
|
|
print "Before"
|
|
|
|
|
print inspect.getmembers(module_examples, predicate=inspect.isfunction)
|
2013-03-11 18:14:23 +00:00
|
|
|
functions = [ func for func in inspect.getmembers(module_examples, predicate=inspect.isfunction) if func[0].startswith('_') is False ][::-1]
|
2013-03-11 17:35:01 +00:00
|
|
|
print "After"
|
|
|
|
|
print functions
|
2013-03-11 17:05:18 +00:00
|
|
|
for example in functions:
|
2013-03-11 19:37:56 +00:00
|
|
|
if example[0] in ['oil', 'silhouette', 'GPLVM_oil_100']:
|
|
|
|
|
print "SKIPPING"
|
|
|
|
|
continue
|
|
|
|
|
|
2013-03-11 17:05:18 +00:00
|
|
|
print "Testing example: ", example[0]
|
2013-06-05 13:02:03 +01:00
|
|
|
# Generate model
|
2013-06-05 17:15:26 +01:00
|
|
|
try:
|
|
|
|
|
model = example[1]()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
failing_models[example[0]] = "Cannot make model: \n{e}".format(e=e)
|
2013-03-11 17:05:18 +00:00
|
|
|
print model
|
2013-03-11 13:54:47 +00:00
|
|
|
|
2013-06-05 13:02:03 +01:00
|
|
|
# Create tests for instance check
|
2013-03-11 17:05:18 +00:00
|
|
|
"""
|
2013-06-05 16:14:30 +01:00
|
|
|
test = model_instance_generator(Model)
|
2013-03-11 17:05:18 +00:00
|
|
|
test.__name__ = 'test_instance_%s' % example[0]
|
|
|
|
|
setattr(ExamplesTests, test.__name__, test)
|
2013-03-11 13:54:47 +00:00
|
|
|
|
2013-03-11 17:05:18 +00:00
|
|
|
#Create tests for checkgrads check
|
2013-06-05 16:14:30 +01:00
|
|
|
test = checkgrads_generator(Model)
|
2013-03-11 17:05:18 +00:00
|
|
|
test.__name__ = 'test_checkgrads_%s' % example[0]
|
|
|
|
|
setattr(ExamplesTests, test.__name__, test)
|
|
|
|
|
"""
|
2013-06-05 17:15:26 +01:00
|
|
|
|
2013-03-11 17:05:18 +00:00
|
|
|
model_checkgrads.description = 'test_checkgrads_%s' % example[0]
|
2013-06-05 17:15:26 +01:00
|
|
|
try:
|
|
|
|
|
if not model_checkgrads(model):
|
|
|
|
|
failing_models[model_checkgrads.description] = False
|
|
|
|
|
except Exception as e:
|
|
|
|
|
failing_models[model_checkgrads.description] = e
|
|
|
|
|
|
2013-03-11 17:35:01 +00:00
|
|
|
model_instance.description = 'test_instance_%s' % example[0]
|
2013-06-05 17:15:26 +01:00
|
|
|
try:
|
|
|
|
|
if not model_instance(model):
|
|
|
|
|
failing_models[model_instance.description] = False
|
|
|
|
|
except Exception as e:
|
|
|
|
|
failing_models[model_instance.description] = e
|
|
|
|
|
|
|
|
|
|
#model_checkgrads.description = 'test_checkgrads_%s' % example[0]
|
|
|
|
|
#yield model_checkgrads, model
|
|
|
|
|
#model_instance.description = 'test_instance_%s' % example[0]
|
|
|
|
|
#yield model_instance, model
|
|
|
|
|
print "Finished checking module {m}".format(m=module_name)
|
|
|
|
|
if len(failing_models.keys()) > 0:
|
|
|
|
|
print "Failing models: "
|
|
|
|
|
print failing_models
|
|
|
|
|
|
|
|
|
|
if len(failing_models.keys()) > 0:
|
|
|
|
|
print failing_models
|
|
|
|
|
raise Exception(failing_models)
|
|
|
|
|
|
2013-03-11 13:54:47 +00:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
print "Running unit tests, please be (very) patient..."
|
2013-06-05 13:02:03 +01:00
|
|
|
# unittest.main()
|
|
|
|
|
test_models()
|