<s> import unittest <EOL> import numpy as np <EOL> import GPy <EOL> import inspect <EOL> import pkgutil <EOL> import os <EOL> import random <EOL> from nose . tools import nottest <EOL> import sys <EOL> import itertools <EOL> class ExamplesTests ( unittest . TestCase ) : <EOL> def _checkgrad ( self , Model ) : <EOL> self . assertTrue ( Model . checkgrad ( ) ) <EOL> def _model_instance ( self , Model ) : <EOL> self . assertTrue ( isinstance ( Model , GPy . models ) ) <EOL> def model_checkgrads ( model ) : <EOL> model . randomize ( ) <EOL> return model . checkgrad ( step = <NUM_LIT> ) <EOL> def model_instance ( model ) : <EOL> return isinstance ( model , GPy . core . model . Model ) <EOL> def flatten_nested ( lst ) : <EOL> result = [ ] <EOL> for element in lst : <EOL> if hasattr ( element , '<STR_LIT>' ) : <EOL> result . extend ( flatten_nested ( element ) ) <EOL> else : <EOL> result . append ( element ) <EOL> return result <EOL> @ nottest <EOL> def test_models ( ) : <EOL> optimize = False <EOL> plot = True <EOL> examples_path = os . path . dirname ( GPy . examples . __file__ ) <EOL> failing_models = { } <EOL> for loader , module_name , is_pkg in pkgutil . iter_modules ( [ examples_path ] ) : <EOL> module_examples = loader . find_module ( module_name ) . load_module ( module_name ) <EOL> print ( "<STR_LIT>" , module_examples ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( inspect . getmembers ( module_examples , predicate = inspect . isfunction ) ) <EOL> functions = [ func for func in inspect . getmembers ( module_examples , predicate = inspect . isfunction ) if func [ <NUM_LIT:0> ] . startswith ( '<STR_LIT:_>' ) is False ] [ : : - <NUM_LIT:1> ] <EOL> print ( "<STR_LIT>" ) <EOL> print ( functions ) <EOL> for example in functions : <EOL> if example [ <NUM_LIT:0> ] in [ '<STR_LIT>' ] : <EOL> if example [ <NUM_LIT:0> ] == '<STR_LIT>' and not GPy . util . datasets . gpxpy_available : <EOL> print ( "<STR_LIT>" ) <EOL> continue <EOL> print ( "<STR_LIT>" , example [ <NUM_LIT:0> ] ) <EOL> try : <EOL> models = [ example [ <NUM_LIT:1> ] ( optimize = optimize , plot = plot ) ] <EOL> models = flatten_nested ( models ) <EOL> except Exception as e : <EOL> failing_models [ example [ <NUM_LIT:0> ] ] = "<STR_LIT>" . format ( e = e ) <EOL> else : <EOL> print ( models ) <EOL> model_checkgrads . description = '<STR_LIT>' % example [ <NUM_LIT:0> ] <EOL> try : <EOL> for model in models : <EOL> if not model_checkgrads ( model ) : <EOL> failing_models [ model_checkgrads . description ] = False <EOL> except Exception as e : <EOL> failing_models [ model_checkgrads . description ] = e <EOL> model_instance . description = '<STR_LIT>' % example [ <NUM_LIT:0> ] <EOL> try : <EOL> for model in models : <EOL> if not model_instance ( model ) : <EOL> failing_models [ model_instance . description ] = False <EOL> except Exception as e : <EOL> failing_models [ model_instance . description ] = e <EOL> print ( "<STR_LIT>" . format ( m = module_name ) ) <EOL> if len ( failing_models . keys ( ) ) > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" ) <EOL> print ( failing_models ) <EOL> if len ( failing_models . keys ( ) ) > <NUM_LIT:0> : <EOL> print ( failing_models ) <EOL> raise Exception ( failing_models ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> print ( "<STR_LIT>" ) <EOL> test_models ( ) </s>
