forked from espdev/csaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ndgrid.py
147 lines (116 loc) · 3.92 KB
/
test_ndgrid.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
# -*- coding: utf-8 -*-
import pytest
import numpy as np
import csaps
@pytest.mark.parametrize('x,y,w,p', [
([1, 2, 3], np.ones((10, 10)), None, None),
([[1], [1]], np.ones((1, 1)), None, None),
([[1, 2, 3], [1, 2, 3]], np.ones((4, 3)), None, None),
([[1, 2, 3], [1, 2, 3]], np.ones((3, 3, 3)), None, None),
([[1, 2, 3], [1, 2, 3]], np.ones((3, 3)), [1, 2, 3], None),
([[1, 2, 3], [1, 2, 3]], np.ones((3, 3)), [[1, 2, 3]], None),
([[1, 2, 3], [1, 2, 3]], np.ones((3, 3)), [[1, 2], [1, 2]], None),
([[1, 2, 3], [1, 2, 3]], np.ones((3, 3)), None, [0.5, 0.4, 0.2]),
(np.array([[1, 2, 3], [4, 5, 6]]), np.ones((3, 3)), None, None),
([np.arange(6).reshape(2, 3), np.arange(6).reshape(2, 3)], np.ones((6, 6)), None, None),
])
def test_invalid_data(x, y, w, p):
with pytest.raises((ValueError, TypeError)):
csaps.NdGridCubicSmoothingSpline(x, y, w, p)
def test_surface():
xdata = [np.linspace(-3, 3, 61), np.linspace(-3.5, 3.5, 51)]
i, j = np.meshgrid(*xdata, indexing='ij')
ydata = (3 * (1 - j)**2. * np.exp(-(j**2) - (i + 1)**2)
- 10 * (j / 5 - j**3 - i**5) * np.exp(-j**2 - i**2)
- 1 / 3 * np.exp(-(j + 1)**2 - i**2))
np.random.seed(12345)
noisy = ydata + (np.random.randn(*ydata.shape) * 0.75)
sp = csaps.NdGridCubicSmoothingSpline(xdata, noisy)
noisy_s = sp(xdata)
assert isinstance(sp.smooth, tuple)
assert len(sp.smooth) == 2
assert isinstance(sp.spline, csaps.NdGridSplinePPForm)
assert noisy_s.shape == noisy.shape
@pytest.mark.parametrize('shape, smooth', [
((4,), 0.0),
((4,), 0.5),
((4,), 1.0),
((4,), (0.0,)),
((4,), (0.5,)),
((4,), (1.0,)),
((4, 5), 0.0),
((4, 5), 0.5),
((4, 5), 1.0),
((4, 5), (0.0, 0.0)),
((4, 5), (0.0, 0.5)),
((4, 5), (0.5, 0.0)),
((4, 5), (0.5, 0.7)),
((4, 5), (1.0, 0.0)),
((4, 5), (0.0, 1.0)),
((4, 5), (1.0, 1.0)),
((4, 5, 6), 0.0),
((4, 5, 6), 0.5),
((4, 5, 6), 1.0),
((4, 5, 6), (0.0, 0.0, 0.0)),
((4, 5, 6), (0.5, 0.0, 0.0)),
((4, 5, 6), (0.5, 0.6, 0.0)),
((4, 5, 6), (0.0, 0.5, 0.6)),
((4, 5, 6), (0.4, 0.5, 0.6)),
((4, 5, 6, 7), 0.0),
((4, 5, 6, 7), 0.5),
((4, 5, 6, 7), 1.0),
((4, 5, 6, 7), (0.0, 0.0, 0.0, 0.0)),
((4, 5, 6, 7), (0.5, 0.0, 0.0, 0.0)),
((4, 5, 6, 7), (0.0, 0.5, 0.0, 0.0)),
((4, 5, 6, 7), (0.5, 0.6, 0.0, 0.0)),
((4, 5, 6, 7), (0.0, 0.5, 0.6, 0.0)),
((4, 5, 6, 7), (0.0, 0.5, 0.6, 0.7)),
((4, 5, 6, 7), (0.4, 0.5, 0.6, 0.7)),
])
def test_smooth_factor(shape, smooth):
x = [np.arange(s) for s in shape]
y = np.arange(0, np.prod(shape)).reshape(shape)
sp = csaps.NdGridCubicSmoothingSpline(x, y, smooth=smooth)
if isinstance(smooth, tuple):
expected_smooth = smooth
else:
expected_smooth = tuple([smooth] * len(shape))
assert sp.smooth == pytest.approx(expected_smooth)
@pytest.mark.parametrize('shape', [
(2,),
(2, 3),
(2, 2),
(2, 3, 4),
(2, 2, 3),
(2, 2, 2),
(2, 3, 4, 5),
(2, 2, 3, 4),
(2, 2, 2, 3),
(2, 2, 2, 2),
(2, 3, 4, 5, 6),
(2, 2, 3, 4, 5),
(2, 2, 2, 3, 4),
(2, 2, 2, 2, 3),
(2, 2, 2, 2, 2),
])
def test_nd_2pt_array(shape: tuple):
xdata = [np.arange(s) for s in shape]
ydata = np.arange(0, np.prod(shape)).reshape(shape)
sp = csaps.NdGridCubicSmoothingSpline(xdata, ydata, smooth=1.0)
ydata_s = sp(xdata)
assert ydata_s.shape == ydata.shape
assert ydata_s == pytest.approx(ydata)
@pytest.mark.parametrize('shape', [
(3,),
(3, 4),
(3, 4, 5),
(3, 4, 5, 6),
(3, 4, 5, 6, 7),
], ids=['1d', '2d', '3d', '4d', '5d'])
def test_nd_array(shape: tuple):
xdata = [np.arange(s) for s in shape]
ydata = np.arange(0, np.prod(shape)).reshape(shape)
sp = csaps.NdGridCubicSmoothingSpline(xdata, ydata, smooth=1.0)
ydata_s = sp(xdata)
assert sp.spline.shape == ydata.shape
assert ydata_s == pytest.approx(ydata)