-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_fit.py
146 lines (126 loc) · 5.35 KB
/
test_fit.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
"""unit tests for fit module"""
import unittest
import pickle
import sys
from io import StringIO
from numpy import logspace, log10, log, vstack
from gpfit.fit import fit, MaxAffine, SoftmaxAffine, ImplicitSoftmaxAffine
SEED = 33404
class TestFit(unittest.TestCase):
"""Test fit class"""
u = logspace(0, log10(3), 101)
w = (u**2 + 3)/(u + 1)**2
x = log(u)
y = log(w)
K = 3
def test_max_affine(self):
f = MaxAffine(self.x, self.y, self.K, seed=SEED)
self.assertTrue(f.errors["rms_rel"] < 1e-2)
self.assertEqual(f.__repr__(), (
"w = 0.807159*(u_1)^-0.0703921\n"
"w = 0.995106*(u_1)^-0.431386\n"
"w = 0.92288*(u_1)^-0.247099"
))
def test_softmax_affine(self):
f = SoftmaxAffine(self.x, self.y, self.K, seed=SEED)
self.assertTrue(f.errors["rms_rel"] < 1e-4)
self.assertEqual(f.__repr__(), (
"w^3.44109 = 0.15339*(u_1)^0.584655\n"
" + 0.431128*(u_1)^-2.14831\n"
" + 0.415776*(u_1)^-2.14794"
))
def test_implicit_softmax_affine(self):
f = ImplicitSoftmaxAffine(self.x, self.y, self.K, seed=SEED)
self.assertTrue(f.errors["rms_rel"] < 1e-5)
self.assertEqual(f.__repr__(), (
"1 = (0.947385/w^0.0920329)*(u_1)^0.0176859\n"
" + (0.992721/w^0.349639)*(u_1)^-0.201861\n"
" + (0.961596/w^0.116677)*(u_1)^-0.0112199"
))
def test_fit_ma(self):
f = fit(self.x, self.y, self.K, fit_type="ma", seed=SEED)
self.assertTrue(f.errors["rms_rel"] < 1e-2)
self.assertEqual(f.__repr__(), (
"w = 0.807159*(u_1)^-0.0703921\n"
"w = 0.995106*(u_1)^-0.431386\n"
"w = 0.92288*(u_1)^-0.247099"
))
def test_fit_sma(self):
f = fit(self.x, self.y, self.K, fit_type="sma", seed=SEED)
self.assertTrue(f.errors["rms_rel"] < 1e-4)
self.assertEqual(f.__repr__(), (
"w^3.44109 = 0.15339*(u_1)^0.584655\n"
" + 0.431128*(u_1)^-2.14831\n"
" + 0.415776*(u_1)^-2.14794"
))
def test_fit_isma(self):
f = fit(self.x, self.y, self.K, seed=SEED)
self.assertTrue(f.errors["rms_rel"] < 1e-5)
self.assertEqual(f.__repr__(), (
"1 = (0.947385/w^0.0920329)*(u_1)^0.0176859\n"
" + (0.992721/w^0.349639)*(u_1)^-0.201861\n"
" + (0.961596/w^0.116677)*(u_1)^-0.0112199"
))
def test_incorrect_inputs(self):
with self.assertRaises(ValueError):
MaxAffine(self.x, vstack((self.y, self.y)), self.K)
def test_save_and_load(self):
f1 = ImplicitSoftmaxAffine(self.x, self.y, self.K, seed=SEED)
f1.save("fit.pkl")
strings1 = f1.__repr__()
with open("fit.pkl", "rb") as picklefile:
f2 = pickle.load(picklefile)
self.assertTrue(f2.errors["rms_rel"] < 1e-5)
strings2 = f2.__repr__()
self.assertEqual(strings1, strings2)
def test_savetxt(self):
f = ImplicitSoftmaxAffine(self.x, self.y, self.K, seed=SEED)
f.savetxt("fit.txt")
with open("fit.txt", "r", encoding="utf-8") as textfile:
fitstring = textfile.read()
self.assertEqual(fitstring, (
"1 = (0.947385/w**0.0920329)*(u_1)**0.0176859\n"
" + (0.992721/w**0.349639)*(u_1)**-0.201861\n"
" + (0.961596/w**0.116677)*(u_1)**-0.0112199"
))
def test_verbosity_1(self):
captured_output = StringIO()
sys.stdout = captured_output
ImplicitSoftmaxAffine(self.x, self.y, self.K, verbosity=1, seed=SEED)
sys.stdout = sys.__stdout__
expected_output = (
"Generated ImplicitSoftmaxAffine fit with 3 terms.\n"
"\n"
"Fit\n"
"---\n"
"1 = (0.947385/w^0.0920329)*(u_1)^0.0176859\n"
" + (0.992721/w^0.349639)*(u_1)^-0.201861\n"
" + (0.961596/w^0.116677)*(u_1)^-0.0112199\n"
"\n"
"Error\n"
"-----\n"
"RMS: 8.1e-05%\n"
"Max: 0.00035%\n\n"
)
self.assertEqual(expected_output, captured_output.getvalue())
def test_error(self):
f1 = ImplicitSoftmaxAffine(self.x, self.y, self.K, seed=SEED)
f2 = ImplicitSoftmaxAffine(self.x, 2*self.y, self.K, seed=SEED)
self.assertAlmostEqual(f1.errors["rms_rel"], 8.12727e-07, places=11)
self.assertAlmostEqual(f1.errors["rms_abs"], 7.43308e-07, places=11)
self.assertAlmostEqual(f1.errors["rms_log"], 8.12726e-07, places=11)
self.assertAlmostEqual(f1.errors["max_rel"], 3.52899e-06, places=10)
self.assertAlmostEqual(f1.errors["max_abs"], 3.52899e-06, places=10)
self.assertAlmostEqual(f2.errors["rms_rel"], 1.61329e-06, places=10)
self.assertAlmostEqual(f2.errors["rms_abs"], 1.36908e-06, places=10)
self.assertAlmostEqual(f2.errors["rms_log"], 1.61329e-06, places=10)
self.assertAlmostEqual(f2.errors["max_rel"], 7.00807e-06, places=10)
self.assertAlmostEqual(f2.errors["max_abs"], 7.00807e-06, places=10)
self.assertEqual(f1.errors["rms_rel"], f1.error)
TESTS = [TestFit]
if __name__ == '__main__':
SUITE = unittest.TestSuite()
LOADER = unittest.TestLoader()
for t in TESTS:
SUITE.addTests(LOADER.loadTestsFromTestCase(t))
unittest.TextTestRunner(verbosity=2).run(SUITE)