1515from GPflow .model import Model
1616
1717
18+ class ParentHook (object ):
19+ """
20+ Temporary solution for fixing the recompilation issues (#37, GPflow issue #442).
21+
22+ An object of this class is returned when highest_parent is called on a model, which holds references to the highest
23+ parentable, as well as the highest model class. When setting the needs recompile flag, this is intercepted and
24+ performed on the model. At the same time, kill autoflow is called on the highest parent.
25+ """
26+ def __init__ (self , highest_parent , highest_model ):
27+ self ._hp = highest_parent
28+ self ._hm = highest_model
29+
30+ def __getattr__ (self , item ):
31+ if item is '_needs_recompile' :
32+ return getattr (self ._hm , item )
33+ return getattr (self ._hp , item )
34+
35+ def __setattr__ (self , key , value ):
36+ if key in ['_hp' , '_hm' ]:
37+ object .__setattr__ (self , key , value )
38+ return
39+ if key is '_needs_recompile' :
40+ setattr (self ._hm , key , value )
41+ if value :
42+ self ._hp ._kill_autoflow ()
43+ else :
44+ setattr (self ._hp , key , value )
45+
46+
1847class ModelWrapper (Parameterized ):
1948 """
2049 Class for fast implementation of a wrapper for models defined in GPflow.
@@ -25,6 +54,7 @@ class ModelWrapper(Parameterized):
2554 AutoFlow methods are influenced following this pattern, the original AF storage (if existing) is unaffected and a
2655 new storage is added to the subclass.
2756 """
57+
2858 def __init__ (self , model ):
2959 """
3060 :param model: model to be wrapped
@@ -45,6 +75,7 @@ def __getattr__(self, item):
4575 method = item [1 :].rstrip ('_AF_storage' )
4676 if method in dir (self ):
4777 raise AttributeError ("{0} has no attribute {1}" .format (self .__class__ .__name__ , item ))
78+
4879 return getattr (self .wrapped , item )
4980
5081 def __setattr__ (self , key , value ):
@@ -90,3 +121,11 @@ def __eq__(self, other):
90121 def name (self ):
91122 name = super (ModelWrapper , self ).name
92123 return "." .join ([name , str .lower (self .__class__ .__name__ )])
124+
125+ @Parameterized .highest_parent .getter
126+ def highest_parent (self ):
127+ """
128+ Returns an instance of the ParentHook instead of the usual reference to a Parentable.
129+ """
130+ original_hp = super (ModelWrapper , self ).highest_parent
131+ return original_hp if isinstance (original_hp , ParentHook ) else ParentHook (original_hp , self )
0 commit comments