Debian Codegraph @

skmisc/loess/tests/test_loess.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 import pickle
2 import tempfile
3 from pathlib import Path
4
5 import numpy as np
6 import numpy.testing as npt
7 import pytest
8
9 from skmisc.loess import loess, loess_anova
10
11 data_path = Path(__file__).parent
12
13
14 def madeup_data():
15 dfile = data_path / 'madeup_data'
16 rfile = data_path / 'madeup_result'
17
18 with open(dfile, 'r') as f:
19 f.readline()
20 x = np.fromiter(
21 (float(v) for v in f.readline().rstrip().split()),
•22 np.float64).reshape(-1, 2)
23 f.readline()
24 y = np.fromiter(
25 (float(v) for v in f.readline().rstrip().split()),
•26 np.float64)
27
28 results = []
29 with open(rfile, 'r') as f:
30 for i in range(8):
31 f.readline()
32 z = np.fromiter(
33 (float(v) for v in f.readline().rstrip().split()),
•34 np.float64)
35 results.append(z)
36
37 newdata1 = np.array([[-2.5, 0.], [2.5, 0.], [0., 0.]])
38 newdata2 = np.array([[-0.5, 0.5], [0., 0.]])
39 return (x, y, results, newdata1, newdata2)
40
41
42 def gas_data():
43 NOx = np.array([4.818, 2.849, 3.275, 4.691, 4.255, 5.064, 2.118, 4.602,
44 2.286, 0.970, 3.965, 5.344, 3.834, 1.990, 5.199, 5.283,
45 3.752, 0.537, 1.640, 5.055, 4.937, 1.561])
46 E = np.array([0.831, 1.045, 1.021, 0.970, 0.825, 0.891, 0.71, 0.801,
47 1.074, 1.148, 1.000, 0.928, 0.767, 0.701, 0.807, 0.902,
48 0.997, 1.224, 1.089, 0.973, 0.980, 0.665])
49 gas_fit_E = np.array([0.665, 0.949, 1.224])
50 newdata = np.array([0.6650000, 0.7581667, 0.8513333, 0.9445000,
51 1.0376667, 1.1308333, 1.2240000])
52 alpha = 0.01
53
54 rfile = data_path / 'gas_result'
55 results = []
56 with open(rfile, 'r') as f:
57 for i in range(8):
58 f.readline()
59 z = np.fromiter(
60 (float(v) for v in f.readline().rstrip().split()),
•61 np.float64)
62 results.append(z)
63 return (E, NOx, gas_fit_E, newdata, alpha, results)
64
65
66 class TestLoess2d(object):
67 "Test class for lowess."
68 d = madeup_data()
69
70 def test_2dbasic(self):
71 "2D standard"
72 (x, y, results, _, _) = self.d
73 madeup = loess(x, y)
74 madeup.model.span = 0.5
75 madeup.model.normalize = True
76 madeup.fit()
77 npt.assert_almost_equal(madeup.outputs.fitted_values, results[0], 5)
78 npt.assert_almost_equal(madeup.outputs.enp, 14.9, 1)
79 npt.assert_almost_equal(madeup.outputs.residual_scale, 0.9693, 4)
80
81 def test_2d_modflags(self):
82 "2D - modification of model flags"
83 (x, y, results, _, _) = self.d
84 madeup = loess(x, y)
85 madeup.model.span = 0.8
86 madeup.model.drop_square = [True, False]
87 madeup.model.parametric = [True, False]
88 npt.assert_equal(madeup.model.parametric[:2], [1, 0])
89 madeup.fit()
90 npt.assert_almost_equal(madeup.outputs.fitted_values, results[1], 5)
91 npt.assert_almost_equal(madeup.outputs.enp, 6.9, 1)
92 npt.assert_almost_equal(madeup.outputs.residual_scale, 1.4804, 4)
93
94 def test_2d_modfamily(self):
95 "2D - family modification"
96 (x, y, results, _, _) = self.d
97 madeup = loess(x, y)
98 madeup.model.span = 0.8
99 madeup.model.drop_square = [True, False]
100 madeup.model.parametric = [True, False]
101 madeup.model.family = "symmetric"
102 madeup.fit()
103 npt.assert_almost_equal(madeup.outputs.fitted_values, results[2], 5)
104 npt.assert_almost_equal(madeup.outputs.enp, 6.9, 1)
105 npt.assert_almost_equal(madeup.outputs.residual_scale, 1.0868, 4)
106
107 def test_2d_modnormalize(self):
108 "2D - normalization modification"
109 (x, y, results, _, _) = self.d
110 madeup = loess(x, y)
111 madeup.model.span = 0.8
112 madeup.model.drop_square = [True, False]
113 madeup.model.parametric = [True, False]
114 madeup.model.family = "symmetric"
115 madeup.model.normalize = False
116 madeup.fit()
117 npt.assert_almost_equal(madeup.outputs.fitted_values, results[3], 5)
118 npt.assert_almost_equal(madeup.outputs.enp, 6.9, 1)
119 npt.assert_almost_equal(madeup.outputs.residual_scale, 1.0868, 4)
120
121 def test_2d_pred_nostderr(self):
122 "2D prediction - no stderr"
123 (x, y, results, newdata1, _) = self.d
124 madeup = loess(x, y)
125 madeup.model.span = 0.5
126 madeup.model.normalize = True
127 prediction = madeup.predict(newdata1, stderror=False)
128 npt.assert_almost_equal(prediction.values, results[4], 5)
129 #
130 prediction = madeup.predict(newdata1, stderror=False)
131 npt.assert_almost_equal(prediction.values, results[4], 5)
132
133 def test_2d_pred_nodata(self):
134 "2D prediction - nodata"
135 (x, y, _, _, _) = self.d
136 madeup = loess(x, y)
137 try:
138 madeup.predict(None)
139 except ValueError:
140 pass
141 else:
142 raise AssertionError("The test should have failed")
143
144 def test_2d_pred_stderr(self):
145 "2D prediction - w/ stderr"
146 (x, y, results, _, newdata2) = self.d
147 madeup = loess(x, y)
148 madeup.model.span = 0.5
149 madeup.model.normalize = True
150 prediction = madeup.predict(newdata2, stderror=True)
151 npt.assert_almost_equal(prediction.values, results[5], 5)
152 npt.assert_almost_equal(prediction.stderr, [0.276746, 0.278009], 5)
153 npt.assert_almost_equal(prediction.residual_scale, 0.969302, 6)
154 npt.assert_almost_equal(prediction.df, 81.2319, 4)
155
156 # Direct access
157 prediction = madeup.predict(newdata2, stderror=True)
158 npt.assert_almost_equal(prediction.values, results[5], 5)
159 npt.assert_almost_equal(prediction.stderr, [0.276746, 0.278009], 5)
160 npt.assert_almost_equal(prediction.residual_scale, 0.969302, 6)
161 npt.assert_almost_equal(prediction.df, 81.2319, 4)
162
163 def test_2d_pred_confinv(self):
164 "2D prediction - confidence"
165 (x, y, results, _, newdata2) = self.d
166 madeup = loess(x, y)
167 madeup.model.span = 0.5
168 madeup.model.normalize = True
169 prediction = madeup.predict(newdata2, stderror=True)
170 ci = prediction.confidence(alpha=0.01)
171 npt.assert_almost_equal(ci.lower, results[6][::3], 5)
172 npt.assert_almost_equal(ci.fit, results[6][1::3], 5)
173 npt.assert_almost_equal(ci.upper, results[6][2::3], 5)
174
175
176 class TestLoessGas(object):
177 "Test class for lowess."
178
179 d = gas_data()
180
181 def test_1dbasic(self):
182 "Basic test 1d"
183 (E, NOx, _, _, _, results) = self.d
184 gas = loess(E, NOx)
185 gas.model.span = 2./3.
186 gas.fit()
187 npt.assert_almost_equal(gas.outputs.fitted_values, results[0], 6)
188 npt.assert_almost_equal(gas.outputs.enp, 5.5, 1)
189 npt.assert_almost_equal(gas.outputs.residual_scale, 0.3404, 4)
190
191 def test_1dbasic_alt(self):
192 "Basic test 1d - part #2"
193 (E, NOx, _, _, _, results) = self.d
194 gas_null = loess(E, NOx)
195 gas_null.model.span = 1.0
196 gas_null.fit()
197 npt.assert_almost_equal(gas_null.outputs.fitted_values, results[1], 6)
198 npt.assert_almost_equal(gas_null.outputs.enp, 3.5, 1)
199 npt.assert_almost_equal(gas_null.outputs.residual_scale, 0.5197, 4)
200
201 def test_1dpredict(self):
202 "Basic test 1d - prediction"
203 (E, NOx, gas_fit_E, _, _, results) = self.d
204 gas = loess(E, NOx, span=2./3.)
205 gas.fit()
206 predicted = gas.predict(gas_fit_E, stderror=False).values
207 npt.assert_almost_equal(predicted, results[2], 6)
208
209 def test_1dpredict_2(self):
210 "Basic test 1d - new predictions"
211 (E, NOx, _, newdata, _, results) = self.d
212 # gas = loess(E, NOx, span=2./3.)
213 gas = loess(E, NOx)
214 gas.model.span = 2./3.
215 prediction = gas.predict(newdata, stderror=True)
216 ci = prediction.confidence(alpha=0.01)
217 npt.assert_almost_equal(ci.lower, results[3][0::3], 6)
218 npt.assert_almost_equal(ci.fit, results[3][1::3], 6)
219 npt.assert_almost_equal(ci.upper, results[3][2::3], 6)
220
221 def test_anova(self):
222 "Tests anova"
223 (E, NOx, _, _, _, results) = self.d
224 gas = loess(E, NOx, span=2./3.)
225 gas.fit()
226 gas_null = loess(E, NOx, span=1.0)
227 gas_null.fit()
228 gas_anova = loess_anova(gas, gas_null)
229 gas_anova_theo = results[4]
230 npt.assert_almost_equal(gas_anova.dfn, gas_anova_theo[0], 5)
231 npt.assert_almost_equal(gas_anova.dfd, gas_anova_theo[1], 5)
232 npt.assert_almost_equal(gas_anova.F_value, gas_anova_theo[2], 5)
233 npt.assert_almost_equal(gas_anova.Pr_F, gas_anova_theo[3], 5)
234
235 def test_failures(self):
236 "Tests failures"
237 (E, NOx, gas_fit_E, _, _, _) = self.d
238 gas = loess(E, NOx, span=2./3.)
239 # This one should fail (all parametric)
240 gas.model.parametric = True
241 with pytest.raises(ValueError):
242 gas.fit()
243
244 # This one also (all drop_square)
245 gas.model.drop_square = True
246 with pytest.raises(ValueError):
247 gas.fit()
248
249 gas.model.degree = 1
250 with pytest.raises(ValueError):
251 gas.fit()
252
253 # This one should not (revert to std)
254 gas.model.parametric = False
255 gas.model.drop_square = False
256 gas.model.degree = 2
257 gas.fit()
258
259 # Now, for predict .................
260 prediction = gas.predict(gas_fit_E, stderror=False)
261 # This one should fail (extrapolation & blending)
262 with pytest.raises(ValueError):
263 gas.predict(prediction.values, stderror=False)
264
265 # But this one should not ..........
266 gas.predict(gas_fit_E, stderror=False)
267
268 def test_pickling(self):
269 rs = np.random.RandomState(seed=123)
270 n = 500
271 np.arange(n)
272 x = np.linspace(0, 500, n)
273 y = rs.randint(1, 1000, n)
274 lo = loess(y, x, span=0.3)
275 lo.fit()
276
277 # pickle lo, unpickle it as lo2 and compare
278 # predictions from both
279 with tempfile.TemporaryFile() as f:
280 pickle.dump(lo, f)
281 f.seek(0)
282 lo2 = pickle.load(f)
283
284 npt.assert_array_equal(lo.predict(x).values, lo2.predict(x).values)