Serialization: Add docstrings

This commit is contained in:
Moreno 2018-06-07 09:52:13 +01:00
parent 7b2af57aee
commit 11aa6ea27b
24 changed files with 393 additions and 69 deletions

View file

@ -30,7 +30,15 @@ class Bernoulli(Likelihood):
self.log_concave = True
def to_dict(self):
input_dict = super(Bernoulli, self)._to_dict()
"""
Convert the object into a json serializable dictionary.
Note: It uses the private method _save_to_input_dict of the parent.
:return dict: json serializable dictionary containing the needed information to instantiate the object
"""
input_dict = super(Bernoulli, self)._save_to_input_dict()
input_dict["class"] = "GPy.likelihoods.Bernoulli"
return input_dict

View file

@ -47,7 +47,15 @@ class Gaussian(Likelihood):
self.log_concave = True
def to_dict(self):
input_dict = super(Gaussian, self)._to_dict()
"""
Convert the object into a json serializable dictionary.
Note: It uses the private method _save_to_input_dict of the parent.
:return dict: json serializable dictionary containing the needed information to instantiate the object
"""
input_dict = super(Gaussian, self)._save_to_input_dict()
input_dict["class"] = "GPy.likelihoods.Gaussian"
input_dict["variance"] = self.variance.values.tolist()
return input_dict

View file

@ -49,7 +49,7 @@ class Likelihood(Parameterized):
def to_dict(self):
raise NotImplementedError
def _to_dict(self):
def _save_to_input_dict(self):
input_dict = {}
input_dict["name"] = self.name
input_dict["gp_link_dict"] = self.gp_link.to_dict()
@ -57,6 +57,18 @@ class Likelihood(Parameterized):
@staticmethod
def from_dict(input_dict):
"""
Instantiate an object of a derived class using the information
in input_dict (built by the to_dict method of the derived class).
More specifically, after reading the derived class from input_dict,
it calls the method _build_from_input_dict of the derived class.
Note: This method should not be overrided in the derived class. In case
it is needed, please override _build_from_input_dict instate.
:param dict input_dict: Dictionary with all the information needed to
instantiate the object.
"""
import copy
input_dict = copy.deepcopy(input_dict)
likelihood_class = input_dict.pop('class')
@ -64,10 +76,10 @@ class Likelihood(Parameterized):
name = input_dict.pop('name')
import GPy
likelihood_class = eval(likelihood_class)
return likelihood_class._from_dict(likelihood_class, input_dict)
return likelihood_class._build_from_input_dict(likelihood_class, input_dict)
@staticmethod
def _from_dict(likelihood_class, input_dict):
def _build_from_input_dict(likelihood_class, input_dict):
import copy
input_dict = copy.deepcopy(input_dict)
gp_link_dict = input_dict.pop('gp_link_dict')

View file

@ -46,20 +46,32 @@ class GPTransformation(object):
def to_dict(self):
raise NotImplementedError
def _to_dict(self):
def _save_to_input_dict(self):
return {}
@staticmethod
def from_dict(input_dict):
"""
Instantiate an object of a derived class using the information
in input_dict (built by the to_dict method of the derived class).
More specifically, after reading the derived class from input_dict,
it calls the method _build_from_input_dict of the derived class.
Note: This method should not be overrided in the derived class. In case
it is needed, please override _build_from_input_dict instate.
:param dict input_dict: Dictionary with all the information needed to
instantiate the object.
"""
import copy
input_dict = copy.deepcopy(input_dict)
link_class = input_dict.pop('class')
import GPy
link_class = eval(link_class)
return link_class._from_dict(link_class, input_dict)
return link_class._build_from_input_dict(link_class, input_dict)
@staticmethod
def _from_dict(link_class, input_dict):
def _build_from_input_dict(link_class, input_dict):
return link_class(**input_dict)
class Identity(GPTransformation):
@ -82,7 +94,15 @@ class Identity(GPTransformation):
return np.zeros_like(f)
def to_dict(self):
input_dict = super(Identity, self)._to_dict()
"""
Convert the object into a json serializable dictionary.
Note: It uses the private method _save_to_input_dict of the parent.
:return dict: json serializable dictionary containing the needed information to instantiate the object
"""
input_dict = super(Identity, self)._save_to_input_dict()
input_dict["class"] = "GPy.likelihoods.link_functions.Identity"
return input_dict
@ -106,7 +126,15 @@ class Probit(GPTransformation):
return (safe_square(f)-1.)*std_norm_pdf(f)
def to_dict(self):
input_dict = super(Probit, self)._to_dict()
"""
Convert the object into a json serializable dictionary.
Note: It uses the private method _save_to_input_dict of the parent.
:return dict: json serializable dictionary containing the needed information to instantiate the object
"""
input_dict = super(Probit, self)._save_to_input_dict()
input_dict["class"] = "GPy.likelihoods.link_functions.Probit"
return input_dict