-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtest_geometry.py
More file actions
76 lines (62 loc) · 2.61 KB
/
test_geometry.py
File metadata and controls
76 lines (62 loc) · 2.61 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
##
# test_geometry.py: Checks correctness of computational geometry functionality.
##
# © 2014 Chris Ferrie (csferrie@gmail.com) and
# Christopher E. Granade (cgranade@gmail.com)
#
# This file is a part of the Qinfer project.
# Licensed under the AGPL version 3.
##
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##
## FEATURES ###################################################################
from __future__ import absolute_import
from __future__ import division # Ensures that a/b is always a float.
## IMPORTS ####################################################################
import numpy as np
from numpy.testing import assert_almost_equal
from qinfer.tests.base_test import DerandomizedTestCase
from qinfer.geometry import convex_distance
from qinfer.utils import (
assert_sigfigs_equal, requires_optional_module,
)
from qinfer.perf_testing import numpy_err_policy
## TESTS #####################################################################
class TestConvexGeometry(DerandomizedTestCase):
@requires_optional_module("cvxopt", if_missing="skip")
def test_convex_distance_known_cases(self, dimension=3):
"""
Convex geometry: Checks convex_distance for known cases.
"""
assert_almost_equal(
convex_distance(np.ones((dimension,)), np.eye(dimension)),
np.linalg.norm(((dimension - 1) / dimension) * np.ones(dimension,), ord=2)
)
# TODO: add cases.
@requires_optional_module("cvxopt", if_missing="skip")
def test_convex_distance_interior_point(self, n_points=500, dimension=5):
"""
Convex geometry: Checks that convex_distance is zero for interior pts.
"""
S = np.random.random((n_points, dimension))
alpha = np.random.random((n_points))
alpha /= alpha.sum()
z = np.dot(alpha, S)
with numpy_err_policy(invalid='raise'):
assert_almost_equal(
convex_distance(z, S),
0
)