mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-03 00:32:39 +02:00
ObservableArray -> ObsAr, because of pickling and ndarray printing
This commit is contained in:
parent
3b42bd4def
commit
64f44cf179
7 changed files with 24 additions and 21 deletions
|
|
@ -7,7 +7,7 @@ import warnings
|
||||||
from .. import kern
|
from .. import kern
|
||||||
from ..util.linalg import dtrtrs
|
from ..util.linalg import dtrtrs
|
||||||
from model import Model
|
from model import Model
|
||||||
from parameterization import ObservableArray
|
from parameterization import ObsAr
|
||||||
from .. import likelihoods
|
from .. import likelihoods
|
||||||
from ..likelihoods.gaussian import Gaussian
|
from ..likelihoods.gaussian import Gaussian
|
||||||
from ..inference.latent_function_inference import exact_gaussian_inference, expectation_propagation
|
from ..inference.latent_function_inference import exact_gaussian_inference, expectation_propagation
|
||||||
|
|
@ -31,19 +31,19 @@ class GP(Model):
|
||||||
super(GP, self).__init__(name)
|
super(GP, self).__init__(name)
|
||||||
|
|
||||||
assert X.ndim == 2
|
assert X.ndim == 2
|
||||||
if isinstance(X, (ObservableArray, VariationalPosterior)):
|
if isinstance(X, (ObsAr, VariationalPosterior)):
|
||||||
self.X = X
|
self.X = X
|
||||||
else: self.X = ObservableArray(X)
|
else: self.X = ObsAr(X)
|
||||||
|
|
||||||
self.num_data, self.input_dim = self.X.shape
|
self.num_data, self.input_dim = self.X.shape
|
||||||
|
|
||||||
assert Y.ndim == 2
|
assert Y.ndim == 2
|
||||||
self.Y = ObservableArray(Y)
|
self.Y = ObsAr(Y)
|
||||||
assert Y.shape[0] == self.num_data
|
assert Y.shape[0] == self.num_data
|
||||||
_, self.output_dim = self.Y.shape
|
_, self.output_dim = self.Y.shape
|
||||||
|
|
||||||
if Y_metadata is None:
|
if Y_metadata is None:
|
||||||
Y_metadata = {}
|
self.Y_metadata = {}
|
||||||
else:
|
else:
|
||||||
self.Y_metadata = Y_metadata
|
self.Y_metadata = Y_metadata
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
||||||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||||
|
|
||||||
from param import Param, ObservableArray
|
from param import Param, ObsAr
|
||||||
from parameterized import Parameterized
|
from parameterized import Parameterized
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,20 @@ __updated__ = '2014-03-17'
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from parameter_core import Observable
|
from parameter_core import Observable
|
||||||
|
|
||||||
class ObservableArray(np.ndarray, Observable):
|
class ObsAr(np.ndarray, Observable):
|
||||||
"""
|
"""
|
||||||
An ndarray which reports changes to its observers.
|
An ndarray which reports changes to its observers.
|
||||||
The observers can add themselves with a callable, which
|
The observers can add themselves with a callable, which
|
||||||
will be called every time this array changes. The callable
|
will be called every time this array changes. The callable
|
||||||
takes exactly one argument, which is this array itself.
|
takes exactly one argument, which is this array itself.
|
||||||
"""
|
"""
|
||||||
__array_priority__ = -1 # Never give back ObservableArray
|
__array_priority__ = -1 # Never give back ObsAr
|
||||||
def __new__(cls, input_array, *a, **kw):
|
def __new__(cls, input_array, *a, **kw):
|
||||||
if not isinstance(input_array, ObservableArray):
|
if not isinstance(input_array, ObsAr):
|
||||||
obj = np.atleast_1d(np.require(input_array, dtype=np.float64, requirements=['W', 'C'])).view(cls)
|
obj = np.atleast_1d(np.require(input_array, dtype=np.float64, requirements=['W', 'C'])).view(cls)
|
||||||
else: obj = input_array
|
else: obj = input_array
|
||||||
cls.__name__ = "ObsAr" # because of fixed printing of `array` in np printing
|
#cls.__name__ = "ObsAr" # because of fixed printing of `array` in np printing
|
||||||
super(ObservableArray, obj).__init__(*a, **kw)
|
super(ObsAr, obj).__init__(*a, **kw)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def __array_finalize__(self, obj):
|
def __array_finalize__(self, obj):
|
||||||
|
|
@ -54,7 +54,7 @@ class ObservableArray(np.ndarray, Observable):
|
||||||
|
|
||||||
def __setitem__(self, s, val):
|
def __setitem__(self, s, val):
|
||||||
if self._s_not_empty(s):
|
if self._s_not_empty(s):
|
||||||
super(ObservableArray, self).__setitem__(s, val)
|
super(ObsAr, self).__setitem__(s, val)
|
||||||
self.notify_observers(self[s])
|
self.notify_observers(self[s])
|
||||||
|
|
||||||
def __getslice__(self, start, stop):
|
def __getslice__(self, start, stop):
|
||||||
|
|
@ -64,7 +64,7 @@ class ObservableArray(np.ndarray, Observable):
|
||||||
return self.__setitem__(slice(start, stop), val)
|
return self.__setitem__(slice(start, stop), val)
|
||||||
|
|
||||||
def __copy__(self, *args):
|
def __copy__(self, *args):
|
||||||
return ObservableArray(self.view(np.ndarray).copy())
|
return ObsAr(self.view(np.ndarray).copy())
|
||||||
|
|
||||||
def copy(self, *args):
|
def copy(self, *args):
|
||||||
return self.__copy__(*args)
|
return self.__copy__(*args)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
import itertools
|
import itertools
|
||||||
import numpy
|
import numpy
|
||||||
from parameter_core import OptimizationHandlable, adjust_name_for_printing
|
from parameter_core import OptimizationHandlable, adjust_name_for_printing
|
||||||
from array_core import ObservableArray
|
from array_core import ObsAr
|
||||||
|
|
||||||
###### printing
|
###### printing
|
||||||
__constraints_name__ = "Constraint"
|
__constraints_name__ = "Constraint"
|
||||||
|
|
@ -15,7 +15,7 @@ __precision__ = numpy.get_printoptions()['precision'] # numpy printing precision
|
||||||
__print_threshold__ = 5
|
__print_threshold__ = 5
|
||||||
######
|
######
|
||||||
|
|
||||||
class Param(OptimizationHandlable, ObservableArray):
|
class Param(OptimizationHandlable, ObsAr):
|
||||||
"""
|
"""
|
||||||
Parameter object for GPy models.
|
Parameter object for GPy models.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,9 @@ class Gaussian(Likelihood):
|
||||||
if isinstance(gp_link, link_functions.Identity):
|
if isinstance(gp_link, link_functions.Identity):
|
||||||
self.log_concave = True
|
self.log_concave = True
|
||||||
|
|
||||||
|
def gaussian_variance(self):
|
||||||
|
return self.variance
|
||||||
|
|
||||||
def covariance_matrix(self, Y, Y_metadata=None):
|
def covariance_matrix(self, Y, Y_metadata=None):
|
||||||
return np.eye(Y.shape[0]) * self.variance
|
return np.eye(Y.shape[0]) * self.variance
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,14 @@ class GPRegression(GP):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, X, Y, kernel=None):
|
def __init__(self, X, Y, kernel=None, Y_metadata=None):
|
||||||
|
|
||||||
if kernel is None:
|
if kernel is None:
|
||||||
kernel = kern.RBF(X.shape[1])
|
kernel = kern.RBF(X.shape[1])
|
||||||
|
|
||||||
likelihood = likelihoods.Gaussian()
|
likelihood = likelihoods.Gaussian()
|
||||||
|
|
||||||
super(GPRegression, self).__init__(X, Y, kernel, likelihood, name='GP regression')
|
super(GPRegression, self).__init__(X, Y, kernel, likelihood, name='GP regression', Y_metadata=Y_metadata)
|
||||||
|
|
||||||
def _getstate(self):
|
def _getstate(self):
|
||||||
return GP._getstate(self)
|
return GP._getstate(self)
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,16 @@ import unittest
|
||||||
import GPy
|
import GPy
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from GPy.core.parameterization.parameter_core import HierarchyError
|
from GPy.core.parameterization.parameter_core import HierarchyError
|
||||||
from GPy.core.parameterization.array_core import ObservableArray
|
from GPy.core.parameterization.array_core import ObsAr
|
||||||
|
|
||||||
class ArrayCoreTest(unittest.TestCase):
|
class ArrayCoreTest(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.X = np.random.normal(1,1, size=(100,10))
|
self.X = np.random.normal(1,1, size=(100,10))
|
||||||
self.obsX = ObservableArray(self.X)
|
self.obsX = ObsAr(self.X)
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
X = ObservableArray(self.X)
|
X = ObsAr(self.X)
|
||||||
X2 = ObservableArray(X)
|
X2 = ObsAr(X)
|
||||||
self.assertIs(X, X2, "no new Observable array, when Observable is given")
|
self.assertIs(X, X2, "no new Observable array, when Observable is given")
|
||||||
|
|
||||||
def test_slice(self):
|
def test_slice(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue