format on save

This commit is contained in:
Martin Bubel 2023-10-10 20:03:23 +02:00
parent 323d29bc7d
commit 975fb7e383

View file

@ -1,4 +1,4 @@
#=============================================================================== # ===============================================================================
# Copyright (c) 2016, Max Zwiessele, Alan Saul # Copyright (c) 2016, Max Zwiessele, Alan Saul
# All rights reserved. # All rights reserved.
# #
@ -26,150 +26,169 @@
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#=============================================================================== # ===============================================================================
import unittest
import numpy as np import numpy as np
import GPy import GPy
class TestDebug(unittest.TestCase):
class UtilTest:
def test_checkFinite(self): def test_checkFinite(self):
from GPy.util.debug import checkFinite from GPy.util.debug import checkFinite
array = np.random.normal(0, 1, 100).reshape(25,4)
self.assertTrue(checkFinite(array, name='test'))
array[np.random.binomial(1, .3, array.shape).astype(bool)] = np.nan array = np.random.normal(0, 1, 100).reshape(25, 4)
self.assertTrue(checkFinite(array, name="test"))
array[np.random.binomial(1, 0.3, array.shape).astype(bool)] = np.nan
self.assertFalse(checkFinite(array)) self.assertFalse(checkFinite(array))
def test_checkFullRank(self): def test_checkFullRank(self):
from GPy.util.debug import checkFullRank from GPy.util.debug import checkFullRank
from GPy.util.linalg import tdot from GPy.util.linalg import tdot
array = np.random.normal(0, 1, 100).reshape(25,4)
self.assertFalse(checkFullRank(tdot(array), name='test'))
array = np.random.normal(0, 1, (25,25)) array = np.random.normal(0, 1, 100).reshape(25, 4)
self.assertFalse(checkFullRank(tdot(array), name="test"))
array = np.random.normal(0, 1, (25, 25))
self.assertTrue(checkFullRank(tdot(array))) self.assertTrue(checkFullRank(tdot(array)))
def test_fixed_inputs_median(self): def test_fixed_inputs_median(self):
""" test fixed_inputs convenience function """ """test fixed_inputs convenience function"""
from GPy.plotting.matplot_dep.util import fixed_inputs from GPy.plotting.matplot_dep.util import fixed_inputs
import GPy import GPy
X = np.random.randn(10, 3) X = np.random.randn(10, 3)
Y = np.sin(X) + np.random.randn(10, 3)*1e-3 Y = np.sin(X) + np.random.randn(10, 3) * 1e-3
m = GPy.models.GPRegression(X, Y) m = GPy.models.GPRegression(X, Y)
fixed = fixed_inputs(m, [1], fix_routine='median', as_list=True, X_all=False) fixed = fixed_inputs(m, [1], fix_routine="median", as_list=True, X_all=False)
self.assertTrue((0, np.median(X[:,0])) in fixed) self.assertTrue((0, np.median(X[:, 0])) in fixed)
self.assertTrue((2, np.median(X[:,2])) in fixed) self.assertTrue((2, np.median(X[:, 2])) in fixed)
self.assertTrue(len([t for t in fixed if t[0] == 1]) == 0) # Unfixed input should not be in fixed self.assertTrue(
len([t for t in fixed if t[0] == 1]) == 0
) # Unfixed input should not be in fixed
def test_fixed_inputs_mean(self): def test_fixed_inputs_mean(self):
from GPy.plotting.matplot_dep.util import fixed_inputs from GPy.plotting.matplot_dep.util import fixed_inputs
import GPy import GPy
X = np.random.randn(10, 3) X = np.random.randn(10, 3)
Y = np.sin(X) + np.random.randn(10, 3)*1e-3 Y = np.sin(X) + np.random.randn(10, 3) * 1e-3
m = GPy.models.GPRegression(X, Y) m = GPy.models.GPRegression(X, Y)
fixed = fixed_inputs(m, [1], fix_routine='mean', as_list=True, X_all=False) fixed = fixed_inputs(m, [1], fix_routine="mean", as_list=True, X_all=False)
self.assertTrue((0, np.mean(X[:,0])) in fixed) self.assertTrue((0, np.mean(X[:, 0])) in fixed)
self.assertTrue((2, np.mean(X[:,2])) in fixed) self.assertTrue((2, np.mean(X[:, 2])) in fixed)
self.assertTrue(len([t for t in fixed if t[0] == 1]) == 0) # Unfixed input should not be in fixed self.assertTrue(
len([t for t in fixed if t[0] == 1]) == 0
) # Unfixed input should not be in fixed
def test_fixed_inputs_zero(self): def test_fixed_inputs_zero(self):
from GPy.plotting.matplot_dep.util import fixed_inputs from GPy.plotting.matplot_dep.util import fixed_inputs
import GPy import GPy
X = np.random.randn(10, 3) X = np.random.randn(10, 3)
Y = np.sin(X) + np.random.randn(10, 3)*1e-3 Y = np.sin(X) + np.random.randn(10, 3) * 1e-3
m = GPy.models.GPRegression(X, Y) m = GPy.models.GPRegression(X, Y)
fixed = fixed_inputs(m, [1], fix_routine='zero', as_list=True, X_all=False) fixed = fixed_inputs(m, [1], fix_routine="zero", as_list=True, X_all=False)
self.assertTrue((0, 0.0) in fixed) self.assertTrue((0, 0.0) in fixed)
self.assertTrue((2, 0.0) in fixed) self.assertTrue((2, 0.0) in fixed)
self.assertTrue(len([t for t in fixed if t[0] == 1]) == 0) # Unfixed input should not be in fixed self.assertTrue(
len([t for t in fixed if t[0] == 1]) == 0
) # Unfixed input should not be in fixed
def test_fixed_inputs_uncertain(self): def test_fixed_inputs_uncertain(self):
from GPy.plotting.matplot_dep.util import fixed_inputs from GPy.plotting.matplot_dep.util import fixed_inputs
import GPy import GPy
from GPy.core.parameterization.variational import NormalPosterior from GPy.core.parameterization.variational import NormalPosterior
X_mu = np.random.randn(10, 3) X_mu = np.random.randn(10, 3)
X_var = np.random.randn(10, 3) X_var = np.random.randn(10, 3)
X = NormalPosterior(X_mu, X_var) X = NormalPosterior(X_mu, X_var)
Y = np.sin(X_mu) + np.random.randn(10, 3)*1e-3 Y = np.sin(X_mu) + np.random.randn(10, 3) * 1e-3
m = GPy.models.BayesianGPLVM(Y, X=X_mu, X_variance=X_var, input_dim=3) m = GPy.models.BayesianGPLVM(Y, X=X_mu, X_variance=X_var, input_dim=3)
fixed = fixed_inputs(m, [1], fix_routine='median', as_list=True, X_all=False) fixed = fixed_inputs(m, [1], fix_routine="median", as_list=True, X_all=False)
self.assertTrue((0, np.median(X.mean.values[:,0])) in fixed) self.assertTrue((0, np.median(X.mean.values[:, 0])) in fixed)
self.assertTrue((2, np.median(X.mean.values[:,2])) in fixed) self.assertTrue((2, np.median(X.mean.values[:, 2])) in fixed)
self.assertTrue(len([t for t in fixed if t[0] == 1]) == 0) # Unfixed input should not be in fixed self.assertTrue(
len([t for t in fixed if t[0] == 1]) == 0
) # Unfixed input should not be in fixed
def test_DSYR(self): def test_DSYR(self):
from GPy.util.linalg import DSYR, DSYR_numpy from GPy.util.linalg import DSYR, DSYR_numpy
A = np.arange(9.0).reshape(3,3)
A = np.arange(9.0).reshape(3, 3)
A = np.dot(A.T, A) A = np.dot(A.T, A)
b = np.ones(3, dtype=float) b = np.ones(3, dtype=float)
alpha = 1.0 alpha = 1.0
DSYR(A, b, alpha) DSYR(A, b, alpha)
R = np.array([ R = np.array([[46, 55, 64], [55, 67, 79], [64, 79, 94]])
[46, 55, 64],
[55, 67, 79],
[64, 79, 94]]
)
self.assertTrue(abs(np.sum(A - R)) < 1e-12) self.assertTrue(abs(np.sum(A - R)) < 1e-12)
def test_subarray(self): def test_subarray(self):
import GPy import GPy
X = np.zeros((3,6), dtype=bool)
X[[1,1,1],[0,4,5]] = 1 X = np.zeros((3, 6), dtype=bool)
X[1:,[2,3]] = 1 X[[1, 1, 1], [0, 4, 5]] = 1
d = GPy.util.subarray_and_sorting.common_subarrays(X,axis=1) X[1:, [2, 3]] = 1
d = GPy.util.subarray_and_sorting.common_subarrays(X, axis=1)
self.assertTrue(len(d) == 3) self.assertTrue(len(d) == 3)
X[:, d[tuple(X[:,0])]] X[:, d[tuple(X[:, 0])]]
self.assertTrue(d[tuple(X[:,4])] == d[tuple(X[:,0])] == [0, 4, 5]) self.assertTrue(d[tuple(X[:, 4])] == d[tuple(X[:, 0])] == [0, 4, 5])
self.assertTrue(d[tuple(X[:,1])] == [1]) self.assertTrue(d[tuple(X[:, 1])] == [1])
def test_offset_cluster(self): def test_offset_cluster(self):
#Tests the GPy.util.cluster_with_offset.cluster utility with a small # Tests the GPy.util.cluster_with_offset.cluster utility with a small
#test data set. Not using random noise just in case it occasionally # test data set. Not using random noise just in case it occasionally
#causes it not to cluster correctly. # causes it not to cluster correctly.
#groundtruth cluster identifiers are: [0,1,1,0] # groundtruth cluster identifiers are: [0,1,1,0]
#data contains a list of the four sets of time series (3 per data point) # data contains a list of the four sets of time series (3 per data point)
data = [np.array([[ 2.18094245, 1.96529789, 2.00265523, 2.18218742, 2.06795428], data = [
[ 1.62254829, 1.75748448, 1.83879347, 1.87531326, 1.52503496], np.array(
[ 1.54589609, 1.61607914, 2.00463192, 1.48771394, 1.63339218]]), [
np.array([[ 2.86766106, 2.97953437, 2.91958876, 2.92510506, 3.03239241], [2.18094245, 1.96529789, 2.00265523, 2.18218742, 2.06795428],
[ 2.57368423, 2.59954886, 3.10000395, 2.75806125, 2.89865704], [1.62254829, 1.75748448, 1.83879347, 1.87531326, 1.52503496],
[ 2.58916318, 2.53698259, 2.63858411, 2.63102504, 2.51853901]]), [1.54589609, 1.61607914, 2.00463192, 1.48771394, 1.63339218],
np.array([[ 2.77834168, 2.9618564 , 2.88482141, 3.24259745, 2.9716821 ], ]
[ 2.60675576, 2.67095624, 2.94824436, 2.80520631, 2.87247516], ),
[ 2.49543562, 2.5492281 , 2.6505866 , 2.65015308, 2.59738616]]), np.array(
np.array([[ 1.76783086, 2.21666738, 2.07939706, 1.9268263 , 2.23360121], [
[ 1.94305547, 1.94648592, 2.1278921 , 2.09481457, 2.08575238], [2.86766106, 2.97953437, 2.91958876, 2.92510506, 3.03239241],
[ 1.69336013, 1.72285186, 1.6339506 , 1.61212022, 1.39198698]])] [2.57368423, 2.59954886, 3.10000395, 2.75806125, 2.89865704],
[2.58916318, 2.53698259, 2.63858411, 2.63102504, 2.51853901],
]
),
np.array(
[
[2.77834168, 2.9618564, 2.88482141, 3.24259745, 2.9716821],
[2.60675576, 2.67095624, 2.94824436, 2.80520631, 2.87247516],
[2.49543562, 2.5492281, 2.6505866, 2.65015308, 2.59738616],
]
),
np.array(
[
[1.76783086, 2.21666738, 2.07939706, 1.9268263, 2.23360121],
[1.94305547, 1.94648592, 2.1278921, 2.09481457, 2.08575238],
[1.69336013, 1.72285186, 1.6339506, 1.61212022, 1.39198698],
]
),
]
#inputs contains their associated X values # inputs contains their associated X values
inputs = [np.array([[ 0. ], inputs = [
[ 0.68040097], np.array([[0.0], [0.68040097], [1.20316795], [1.798749], [2.14891733]]),
[ 1.20316795], np.array([[0.0], [0.51910637], [0.98259352], [1.57442965], [1.82515098]]),
[ 1.798749 ], np.array([[0.0], [0.66645478], [1.59464591], [1.69769551], [1.80932752]]),
[ 2.14891733]]), np.array([[ 0. ], np.array([[0.0], [0.87512108], [1.71881079], [2.67162871], [3.23761907]]),
[ 0.51910637], ]
[ 0.98259352],
[ 1.57442965],
[ 1.82515098]]), np.array([[ 0. ],
[ 0.66645478],
[ 1.59464591],
[ 1.69769551],
[ 1.80932752]]), np.array([[ 0. ],
[ 0.87512108],
[ 1.71881079],
[ 2.67162871],
[ 3.23761907]])]
#try doing the clustering # try doing the clustering
active = GPy.util.cluster_with_offset.cluster(data,inputs) active = GPy.util.cluster_with_offset.cluster(data, inputs)
#check to see that the clustering has correctly clustered the time series. # check to see that the clustering has correctly clustered the time series.
clusters = set([frozenset(cluster) for cluster in active]) clusters = set([frozenset(cluster) for cluster in active])
assert set([1,2]) in clusters, "Offset Clustering algorithm failed" assert set([1, 2]) in clusters, "Offset Clustering algorithm failed"
assert set([0,3]) in clusters, "Offset Clustering algoirthm failed" assert set([0, 3]) in clusters, "Offset Clustering algoirthm failed"
class TestUnivariateGaussian(unittest.TestCase): class TestUnivariateGaussian(unittest.TestCase):
@ -178,12 +197,15 @@ class TestUnivariateGaussian(unittest.TestCase):
def test_logPdfNormal(self): def test_logPdfNormal(self):
from GPy.util.univariate_Gaussian import logPdfNormal from GPy.util.univariate_Gaussian import logPdfNormal
pySols = [-13.4189385332,
pySols = [
-13.4189385332,
-1.2389385332, -1.2389385332,
-0.918938533205, -0.918938533205,
-1.0439385332, -1.0439385332,
-2.9189385332, -2.9189385332,
-50.9189385332] -50.9189385332,
]
diff = 0.0 diff = 0.0
for i in range(len(pySols)): for i in range(len(pySols)):
diff += abs(logPdfNormal(self.zz[i]) - pySols[i]) diff += abs(logPdfNormal(self.zz[i]) - pySols[i])
@ -191,12 +213,15 @@ class TestUnivariateGaussian(unittest.TestCase):
def test_cdfNormal(self): def test_cdfNormal(self):
from GPy.util.univariate_Gaussian import cdfNormal from GPy.util.univariate_Gaussian import cdfNormal
pySols = [2.86651571879e-07,
pySols = [
2.86651571879e-07,
0.211855398583, 0.211855398583,
0.5, 0.5,
0.691462461274, 0.691462461274,
0.977249868052, 0.977249868052,
1.0] 1.0,
]
diff = 0.0 diff = 0.0
for i in range(len(pySols)): for i in range(len(pySols)):
diff += abs(cdfNormal(self.zz[i]) - pySols[i]) diff += abs(cdfNormal(self.zz[i]) - pySols[i])
@ -204,33 +229,41 @@ class TestUnivariateGaussian(unittest.TestCase):
def test_logCdfNormal(self): def test_logCdfNormal(self):
from GPy.util.univariate_Gaussian import logCdfNormal from GPy.util.univariate_Gaussian import logCdfNormal
pySols = [-15.064998394,
pySols = [
-15.064998394,
-1.55185131919, -1.55185131919,
-0.69314718056, -0.69314718056,
-0.368946415289, -0.368946415289,
-0.023012909329, -0.023012909329,
0.0] 0.0,
]
diff = 0.0 diff = 0.0
for i in range(len(pySols)): for i in range(len(pySols)):
diff += abs(logCdfNormal(self.zz[i]) - pySols[i]) diff += abs(logCdfNormal(self.zz[i]) - pySols[i])
self.assertTrue(diff < 1e-10) self.assertTrue(diff < 1e-10)
def test_derivLogCdfNormal(self): def test_derivLogCdfNormal(self):
from GPy.util.univariate_Gaussian import derivLogCdfNormal from GPy.util.univariate_Gaussian import derivLogCdfNormal
pySols = [5.18650396941,
pySols = [
5.18650396941,
1.3674022693, 1.3674022693,
0.79788456081, 0.79788456081,
0.50916043387, 0.50916043387,
0.0552478626962, 0.0552478626962,
0.0] 0.0,
]
diff = 0.0 diff = 0.0
for i in range(len(pySols)): for i in range(len(pySols)):
diff += abs(derivLogCdfNormal(self.zz[i]) - pySols[i]) diff += abs(derivLogCdfNormal(self.zz[i]) - pySols[i])
self.assertTrue(diff < 1e-8) self.assertTrue(diff < 1e-8)
class TestStandardize(unittest.TestCase): class TestStandardize(unittest.TestCase):
def setUp(self): def setUp(self):
self.normalizer = GPy.util.normalizer.Standardize() self.normalizer = GPy.util.normalizer.Standardize()
y = np.stack([np.random.randn(10), 2*np.random.randn(10)], axis=1) y = np.stack([np.random.randn(10), 2 * np.random.randn(10)], axis=1)
self.normalizer.scale_by(y) self.normalizer.scale_by(y)
def test_inverse_covariance(self): def test_inverse_covariance(self):