-
Notifications
You must be signed in to change notification settings - Fork 31
/
test_utils.py
209 lines (170 loc) · 6.61 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/python
# -*- coding: utf-8 -*-
##
# test_utils.py: Tests helper functions in utils.py
##
# © 2017, Chris Ferrie (csferrie@gmail.com) and
# Christopher Granade (cgranade@cgranade.com).
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER 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 OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
##
## FEATURES ###################################################################
from __future__ import absolute_import
from __future__ import division # Ensures that a/b is always a float.
## IMPORTS ####################################################################
import warnings
import unittest
from scipy.linalg import sqrtm
import numpy as np
from numpy.testing import assert_equal, assert_almost_equal
from qinfer.tests.base_test import DerandomizedTestCase, MockModel, assert_warns
from qinfer.utils import in_ellipsoid, assert_sigfigs_equal, sqrtm_psd, to_simplex, from_simplex
## TESTS #####################################################################
class TestNumericTests(DerandomizedTestCase):
def test_assert_sigfigs_equal(self):
"""
Tests to make sure assert_sigfigs_equal
only passes if the correct number of
significant figures match
"""
# these are the same to 6 sigfigs
assert_sigfigs_equal(
np.array([3.141592]),
np.array([3.141593]),
6
)
# these are only the same to 5 sigfigs
self.assertRaises(
AssertionError,
assert_sigfigs_equal,
np.array([3.14159]),
np.array([3.14158]),
6
)
# these are the same to 3 sigfigs
assert_sigfigs_equal(
np.array([1729]),
np.array([1728]),
3
)
# these are only the same to 3 sigfigs
self.assertRaises(
AssertionError,
assert_sigfigs_equal,
np.array([1729]),
np.array([1728]),
4
)
class TestEllipsoids(DerandomizedTestCase):
def test_in_ellipsoid(self):
# the semi-major axes are the square roots of the
# singular values, so 2 and 1 in this case.
A = np.array([[4,0], [0,1]])
c = np.array([0,1])
# test with multiple inputs. account for numerical error at boundary.
x = np.array([[10,5],[0,1],[0,2],[0,3],[2,1],[3,1],[0.5,1.5]])
assert_equal(
in_ellipsoid(x, A, c),
np.array([0, 1, 1, 0, 1, 0, 1],dtype=bool)
)
# test with single input
assert(in_ellipsoid(c,A,c))
# Random positive matrix and origin
A = np.random.randn(5, 5)
A = np.dot(A, A.T)
c = np.random.randn(5)
# Look along a couple of the semi-major axes
U, s, _ = np.linalg.svd(A)
x = np.vstack([
c + 0.99 * np.sqrt(s[2]) * U[:,2],
c + 1.01 * np.sqrt(s[2]) * U[:,2],
c - 0.99 * np.sqrt(s[0]) * U[:,0],
c - 1.01 * np.sqrt(s[0]) * U[:,0],
])
assert_equal(
in_ellipsoid(x, A, c),
np.array([1,0,1,0], dtype=bool)
)
class TestLinearAlgebra(DerandomizedTestCase):
def test_sqrtm_psd(self):
# Construct Y = XX^T as a PSD matrix.
X = np.random.random((5, 5))
Y = np.dot(X, X.T)
sqrt_Y = sqrtm_psd(Y, est_error=False)
np.testing.assert_allclose(
np.dot(sqrt_Y, sqrt_Y),
Y
)
# Try again, but with a singular matrix.
Y_singular = np.zeros((6, 6))
Y_singular[:5, :5] = Y
sqrt_Y_singular = sqrtm_psd(Y_singular, est_error=False)
np.testing.assert_allclose(
np.dot(sqrt_Y_singular, sqrt_Y_singular),
Y_singular
)
class TestSimplexTransforms(DerandomizedTestCase):
"""
Tests to_simplex and from_simplex.
"""
def test_to_simplex(self):
y = np.random.random(size=(20,10,15))
y[..., -1] = 0
x = to_simplex(y)
assert(x.shape == y.shape)
assert(np.all(np.isfinite(x)))
assert_almost_equal(np.sum(x, axis=-1), 1)
y = np.random.random(size=(15,))
y[..., -1] = 0
x = to_simplex(y)
assert(x.shape == y.shape)
assert(np.all(np.isfinite(x)))
assert_almost_equal(np.sum(x, axis=-1), 1)
def test_from_simplex(self):
x = np.abs(np.random.random(size=(20,10,15)))
x = x / np.sum(x, axis=-1)[...,np.newaxis]
y = from_simplex(x)
assert(x.shape == y.shape)
assert(np.all(np.isfinite(y)))
assert(np.all(np.isreal(y)))
assert_almost_equal(y[..., -1], 0)
x = np.abs(np.random.random(size=(15,)))
x = x / np.sum(x, axis=-1)[...,np.newaxis]
y = from_simplex(x)
assert(x.shape == y.shape)
assert(np.all(np.isfinite(y)))
assert(np.all(np.isreal(y)))
assert_almost_equal(y[..., -1], 0)
def test_inverses(self):
y = np.random.random(size=(20,10,15))
y[..., -1] = 0
x = to_simplex(y)
assert_almost_equal(from_simplex(x), y)
x = np.abs(np.random.random(size=(20,10,15)))
x = x / np.sum(x, axis=-1)[...,np.newaxis]
y = from_simplex(x)
assert_almost_equal(to_simplex(y), x)