-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathtest.py
159 lines (123 loc) · 4.03 KB
/
test.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
from unittest import mock
import numpy as np
import numpy.testing as nptest
import pytest
from ellipse import LsqEllipse
def make_dataset(center, width, height, phi, n_points):
"""Generate Elliptical data with noise"""
t = np.linspace(0, 1.8 * np.pi, n_points)
x = (center[0] + width * np.cos(t) * np.cos(phi) - height * np.sin(t) * np.sin(phi))
y = (center[1] + width * np.cos(t) * np.sin(phi) + height * np.sin(t) * np.cos(phi))
return np.c_[x, y]
def _normalize_result(major, minor, phi):
"""Map parameters back to original orientation
If phi > np.pi/2 then we are actually measuring a tall ellipse
chaneg this to measure angle of the waist
"""
if phi >= np.pi/2:
width, height = minor, major
phi -= np.pi/2
else:
width, height = major, minor
return width, height, phi
# phi needs to be < (1/4 * pi) and width != height or test is degenerate
@pytest.mark.parametrize('center', [[1, 1], [0, 1]])
@pytest.mark.parametrize('width', [.4, 10])
@pytest.mark.parametrize('height', [.2, 3])
@pytest.mark.parametrize('phi', [np.pi/5, np.pi/13])
def test_ellipse_fit(center, width, height, phi):
X = make_dataset(
center=center,
width=width,
height=height,
phi=phi,
n_points=10
)
elp = LsqEllipse()
elp.fit(X)
_center, _major, _minor, _phi = elp.as_parameters()
_width, _height, _phi = _normalize_result(_major, _minor, _phi)
nptest.assert_array_almost_equal(_center, center)
nptest.assert_almost_equal(_width, width)
nptest.assert_almost_equal(_height, height)
nptest.assert_almost_equal(_phi, phi)
def test_minimum_data_points():
X = make_dataset(
center=[0, 0],
width=1,
height=.5,
phi=np.pi/20,
n_points=5
)
elp = LsqEllipse()
elp.fit(X)
_center, _major, _minor, _phi = elp.as_parameters()
_width, _height, _phi = _normalize_result(_major, _minor, _phi)
nptest.assert_array_almost_equal(_center, [0, 0])
nptest.assert_almost_equal(_width, 1)
nptest.assert_almost_equal(_height, .5)
nptest.assert_almost_equal(_phi, np.pi/20)
def test_less_than_minimum_data_points_raises_err():
X = make_dataset(
center=[0, 0],
width=1,
height=.5,
phi=0,
n_points=4
)
elp = LsqEllipse()
with pytest.raises(ValueError):
elp.fit(X)
def test_cannot_get_coef_without_fitting():
elp = LsqEllipse()
with pytest.raises(ValueError):
elp.coefficients
@pytest.mark.parametrize('n_points', [6, 100])
def test_return_fit_returns_correct_ellipse(n_points):
X = make_dataset(
center=[0, 0],
width=1,
height=.5,
phi=0,
n_points=n_points
)
elp = LsqEllipse().fit(X)
t = np.linspace(0, 1.8 * np.pi, n_points)
center, major, minor, phi = elp.as_parameters()
with mock.patch.object(LsqEllipse, 'as_parameters') as as_parameters:
as_parameters.return_value = (center, *_normalize_result(major, minor, phi))
x = elp.return_fit(n_points, t=t)
nptest.assert_array_almost_equal(x, X)
def test_if_perfect_circle():
X = make_dataset(
center=[0, 0],
width=1,
height=1,
phi=0,
n_points=50
)
elp = LsqEllipse().fit(X)
_center, _width, _height, _phi = elp.as_parameters()
nptest.assert_array_almost_equal(_center, [0, 0])
nptest.assert_almost_equal(_width, 1)
nptest.assert_almost_equal(_height, 1)
# nptest.assert_almost_equal(_phi, 0)
@pytest.mark.xfail()
def test_if_no_ellipse_found():
"""
This data causes a divide by zero error
TODO: add in check of this
"""
X = make_dataset(
center=[0, 0],
width=1,
height=1,
phi=0,
n_points=5
)
elp = LsqEllipse().fit(X)
_center, _width, _height, _phi = elp.as_parameters()
nptest.assert_array_almost_equal(_center, [0, 0])
nptest.assert_almost_equal(_width, 1)
nptest.assert_almost_equal(_height, 1)
# nptest.assert_almost_equal(_phi, 0)