Skip to content

Commit

Permalink
adding to_dict() method to Ornstein-Uhlenbeck (OU) kernel
Browse files Browse the repository at this point in the history
This method was missing, causing serialization functionality that uses it to raise an AttributeError. This commit implements the missing method.
  • Loading branch information
ekalosak authored May 11, 2021
2 parents 434e1b0 + b906719 commit 562266f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
18 changes: 17 additions & 1 deletion GPy/kern/src/stationary.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@ def _build_from_input_dict(kernel_class, input_dict):
# return (F, L, Qc, H, Pinf)



class OU(Stationary):
"""
OU kernel:
Expand All @@ -426,6 +425,23 @@ class OU(Stationary):
def __init__(self, input_dim, variance=1., lengthscale=None, ARD=False, active_dims=None, name='OU'):
super(OU, self).__init__(input_dim, variance, lengthscale, ARD, active_dims, name)

def to_dict(self):
"""
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(OU, self)._save_to_input_dict()
input_dict["class"] = "GPy.kern.OU"
return input_dict

@staticmethod
def _build_from_input_dict(kernel_class, input_dict):
useGPU = input_dict.pop('useGPU', None)
return OU(**input_dict)

def K_of_r(self, r):
return self.variance * np.exp(-r)

Expand Down
17 changes: 9 additions & 8 deletions GPy/testing/serialization_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ def test_serialize_deserialize_kernels(self):
k7 = GPy.kern.Matern32(2, variance=1.0, lengthscale=[1.0,3.0], ARD=True, active_dims = [1,1])
k8 = GPy.kern.Matern52(2, variance=2.0, lengthscale=[2.0,1.0], ARD=True, active_dims = [1,0])
k9 = GPy.kern.ExpQuad(2, variance=3.0, lengthscale=[1.0,2.0], ARD=True, active_dims = [0,1])
k10 = k1 + k1.copy() + k2 + k3 + k4 + k5 + k6
k11 = k1 * k2 * k2.copy() * k3 * k4 * k5
k12 = (k1 + k2) * (k3 + k4 + k5)
k13 = ((k1 + k2) * k3) + k4 + k5 * k7
k14 = ((k1 + k2) * k3) + k4 * k5 + k8
k15 = ((k1 * k2) * k3) + k4 * k5 + k8 + k9

k_list = [k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15]
k10 = GPy.kern.OU(2, variance=2.0, lengthscale=[2.0, 1.0], ARD=True, active_dims=[1, 0])
k11 = k1 + k1.copy() + k2 + k3 + k4 + k5 + k6
k12 = k1 * k2 * k2.copy() * k3 * k4 * k5
k13 = (k1 + k2) * (k3 + k4 + k5)
k14 = ((k1 + k2) * k3) + k4 + k5 * k7
k15 = ((k1 + k2) * k3) + k4 * k5 + k8 * k10
k16 = ((k1 * k2) * k3) + k4 * k5 + k8 + k9

k_list = [k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16]

for kk in k_list:
kk_dict = kk.to_dict()
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ We welcome any contributions to GPy, after all it is an open source project. We

For an in depth description of pull requests, please visit https://help.github.com/articles/using-pull-requests/ .

### Steps to a successfull contribution:
### Steps to a successful contribution:

1. Fork GPy: https://help.github.com/articles/fork-a-repo/
2. Make your changes to the source in your fork.
3. Make sure the [guidelines](#gl) are met.
4. Set up tests to test your code. We are using unttests in the testing subfolder of GPy. There is a good chance that there is already a framework set up to test your new model in model_tests.py or kernel in kernel_tests.py. have a look at the source and you might be able to just add your model (or kernel or others) as an additional test in the appropriate file. There is more frameworks for testing the other bits and pieces, just head over to the testing folder and have a look.
4. Set up tests to test your code. We are using unittests in the testing subfolder of GPy. There is a good chance
that there is already a framework set up to test your new model in model_tests.py or kernel in kernel_tests.py. have a look at the source and you might be able to just add your model (or kernel or others) as an additional test in the appropriate file. There is more frameworks for testing the other bits and pieces, just head over to the testing folder and have a look.
5. Create a pull request to the devel branch in GPy, see above.
6. The tests will be running on your pull request. In the comments section we will be able to discuss the changes and help you with any problems. Let us know if there are any in the comments, so we can help.
7. The pull request gets accepted and your awsome new feature will be in the next GPy release :)
7. The pull request gets accepted and your awesome new feature will be in the next GPy release :)

For any further questions/suggestions head over to the issues section in GPy.

Expand Down

0 comments on commit 562266f

Please sign in to comment.