summaryrefslogtreecommitdiff
path: root/test/test_parameters.py
blob: 63562a4cd671df815e2b656a290da79a19ae6561 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3

from dfatool import dfatool as dt
from dfatool import parameters
from dfatool.utils import by_name_to_by_param
from dfatool.functions import analytic
import unittest

import numpy as np


class TestModels(unittest.TestCase):
    def test_distinct_param_values(self):
        X = np.arange(35)
        by_name = {
            "TX": {
                "param": [(x % 5, x % 7) for x in X],
                "power": X,
                "attributes": ["power"],
            }
        }
        self.assertEqual(
            parameters.distinct_param_values(by_name, "TX"),
            [list(range(5)), list(range(7))],
        )

    def test_parameter_detection_linear(self):
        rng = np.random.default_rng(seed=1312)
        X = np.arange(200) % 50
        Y = X + rng.normal(size=X.size)
        parameter_names = ["p_mod5", "p_linear"]

        # Test input data:
        # * param[0] ("p_mod5") == X % 5 (bogus data to test detection of non-influence)
        # * param[1] ("p_linear") == X
        # * TX power == X ± gaussian noise
        # -> TX power depends linearly on "p_linear"
        by_name = {
            "TX": {
                "param": [(x % 5, x) for x in X],
                "power": Y,
                "attributes": ["power"],
            }
        }
        by_param = by_name_to_by_param(by_name)
        stats = parameters.ParamStats(by_name, by_param, parameter_names, dict())

        self.assertEqual(stats.depends_on_param("TX", "power", "p_mod5"), False)
        self.assertEqual(stats.depends_on_param("TX", "power", "p_linear"), True)

        # Fit individual functions for each parameter (only "p_linear" in this case)

        paramfit = dt.ParallelParamFit(by_param)
        paramfit.enqueue("TX", "power", 1, "p_linear")
        paramfit.fit()

        fit_result = paramfit.get_result("TX", "power")
        self.assertEqual(fit_result["p_linear"]["best"], "linear")
        self.assertEqual("p_mod5" not in fit_result, True)

        # Fit a single function for all parameters (still only "p_linear" in this case)

        combined_fit = analytic.function_powerset(fit_result, parameter_names, 0)

        self.assertEqual(
            combined_fit.model_function,
            "0 + regression_arg(0) + regression_arg(1) * parameter(p_linear)",
        )
        self.assertEqual(
            combined_fit._function_str,
            "0 + reg_param[0] + reg_param[1] * model_param[1]",
        )

        combined_fit.fit(by_param, "TX", "power")

        self.assertEqual(combined_fit.fit_success, True)

        self.assertEqual(combined_fit.is_predictable([None, None]), False)
        self.assertEqual(combined_fit.is_predictable([None, 0]), True)
        self.assertEqual(combined_fit.is_predictable([None, 50]), True)
        self.assertEqual(combined_fit.is_predictable([0, None]), False)
        self.assertEqual(combined_fit.is_predictable([50, None]), False)
        self.assertEqual(combined_fit.is_predictable([0, 0]), True)
        self.assertEqual(combined_fit.is_predictable([0, 50]), True)
        self.assertEqual(combined_fit.is_predictable([50, 0]), True)
        self.assertEqual(combined_fit.is_predictable([50, 50]), True)

        # The function should be linear without offset or skew
        for i in range(100):
            self.assertAlmostEqual(combined_fit.eval([None, i]), i, places=0)

    def test_parameter_detection_multi_dimensional(self):
        rng = np.random.default_rng(seed=1312)
        # vary each parameter from 1 to 10
        Xi = (np.arange(50) % 10) + 1
        # Three parameters -> Build input array [[1, 1, 1], [1, 1, 2], ..., [10, 10, 10]]
        X = np.array(np.meshgrid(Xi, Xi, Xi)).T.reshape(-1, 3)

        f_lls = np.vectorize(
            lambda x: 42 + 7 * x[0] + 10 * np.log(x[1]) - 0.5 * x[2] * x[2],
            signature="(n)->()",
        )
        f_ll = np.vectorize(
            lambda x: 23 + 5 * x[0] - 3 * x[0] / x[1], signature="(n)->()"
        )

        Y_lls = f_lls(X) + rng.normal(size=X.shape[0])
        Y_ll = f_ll(X) + rng.normal(size=X.shape[0])

        parameter_names = ["lin_lin", "log_inv", "square_none"]

        by_name = {
            "someKey": {
                "param": X,
                "lls": Y_lls,
                "ll": Y_ll,
                "attributes": ["lls", "ll"],
            }
        }
        by_param = by_name_to_by_param(by_name)
        stats = parameters.ParamStats(by_name, by_param, parameter_names, dict())

        self.assertEqual(stats.depends_on_param("someKey", "lls", "lin_lin"), True)
        self.assertEqual(stats.depends_on_param("someKey", "lls", "log_inv"), True)
        self.assertEqual(stats.depends_on_param("someKey", "lls", "square_none"), True)

        self.assertEqual(stats.depends_on_param("someKey", "ll", "lin_lin"), True)
        self.assertEqual(stats.depends_on_param("someKey", "ll", "log_inv"), True)
        self.assertEqual(stats.depends_on_param("someKey", "ll", "square_none"), False)

        paramfit = dt.ParallelParamFit(by_param)
        paramfit.enqueue("someKey", "lls", 0, "lin_lin")
        paramfit.enqueue("someKey", "lls", 1, "log_inv")
        paramfit.enqueue("someKey", "lls", 2, "square_none")
        paramfit.enqueue("someKey", "ll", 0, "lin_lin")
        paramfit.enqueue("someKey", "ll", 1, "log_inv")
        paramfit.fit()

        fit_lls = paramfit.get_result("someKey", "lls")
        self.assertEqual(fit_lls["lin_lin"]["best"], "linear")
        self.assertEqual(fit_lls["log_inv"]["best"], "logarithmic")
        self.assertEqual(fit_lls["square_none"]["best"], "square")

        combined_fit_lls = analytic.function_powerset(fit_lls, parameter_names, 0)

        self.assertEqual(
            combined_fit_lls.model_function,
            "0 + regression_arg(0) + regression_arg(1) * parameter(lin_lin)"
            " + regression_arg(2) * np.log(parameter(log_inv))"
            " + regression_arg(3) * (parameter(square_none))**2"
            " + regression_arg(4) * parameter(lin_lin) * np.log(parameter(log_inv))"
            " + regression_arg(5) * parameter(lin_lin) * (parameter(square_none))**2"
            " + regression_arg(6) * np.log(parameter(log_inv)) * (parameter(square_none))**2"
            " + regression_arg(7) * parameter(lin_lin) * np.log(parameter(log_inv)) * (parameter(square_none))**2",
        )

        combined_fit_lls.fit(by_param, "someKey", "lls")

        self.assertEqual(combined_fit_lls.fit_success, True)

        # Verify that f_lls parameters have been found
        self.assertAlmostEqual(combined_fit_lls.model_args[0], 42, places=0)
        self.assertAlmostEqual(combined_fit_lls.model_args[1], 7, places=0)
        self.assertAlmostEqual(combined_fit_lls.model_args[2], 10, places=0)
        self.assertAlmostEqual(combined_fit_lls.model_args[3], -0.5, places=1)
        self.assertAlmostEqual(combined_fit_lls.model_args[4], 0, places=2)
        self.assertAlmostEqual(combined_fit_lls.model_args[5], 0, places=2)
        self.assertAlmostEqual(combined_fit_lls.model_args[6], 0, places=2)
        self.assertAlmostEqual(combined_fit_lls.model_args[7], 0, places=2)

        self.assertEqual(combined_fit_lls.is_predictable([None, None, None]), False)
        self.assertEqual(combined_fit_lls.is_predictable([None, None, 11]), False)
        self.assertEqual(combined_fit_lls.is_predictable([None, 11, None]), False)
        self.assertEqual(combined_fit_lls.is_predictable([None, 11, 11]), False)
        self.assertEqual(combined_fit_lls.is_predictable([11, None, None]), False)
        self.assertEqual(combined_fit_lls.is_predictable([11, None, 11]), False)
        self.assertEqual(combined_fit_lls.is_predictable([11, 11, None]), False)
        self.assertEqual(combined_fit_lls.is_predictable([11, 11, 11]), True)

        # Verify that fitted function behaves like input function
        for i, x in enumerate(X):
            self.assertAlmostEqual(combined_fit_lls.eval(x), f_lls(x), places=0)

        fit_ll = paramfit.get_result("someKey", "ll")
        self.assertEqual(fit_ll["lin_lin"]["best"], "linear")
        self.assertEqual(fit_ll["log_inv"]["best"], "inverse")
        self.assertEqual("quare_none" not in fit_ll, True)

        combined_fit_ll = analytic.function_powerset(fit_ll, parameter_names, 0)

        self.assertEqual(
            combined_fit_ll.model_function,
            "0 + regression_arg(0) + regression_arg(1) * parameter(lin_lin)"
            " + regression_arg(2) * 1/(parameter(log_inv))"
            " + regression_arg(3) * parameter(lin_lin) * 1/(parameter(log_inv))",
        )

        combined_fit_ll.fit(by_param, "someKey", "ll")

        self.assertEqual(combined_fit_ll.fit_success, True)

        # Verify that f_ll parameters have been found
        self.assertAlmostEqual(combined_fit_ll.model_args[0], 23, places=0)
        self.assertAlmostEqual(combined_fit_ll.model_args[1], 5, places=0)
        self.assertAlmostEqual(combined_fit_ll.model_args[2], 0, places=1)
        self.assertAlmostEqual(combined_fit_ll.model_args[3], -3, places=0)

        self.assertEqual(combined_fit_ll.is_predictable([None, None, None]), False)
        self.assertEqual(combined_fit_ll.is_predictable([None, None, 11]), False)
        self.assertEqual(combined_fit_ll.is_predictable([None, 11, None]), False)
        self.assertEqual(combined_fit_ll.is_predictable([None, 11, 11]), False)
        self.assertEqual(combined_fit_ll.is_predictable([11, None, None]), False)
        self.assertEqual(combined_fit_ll.is_predictable([11, None, 11]), False)
        self.assertEqual(combined_fit_ll.is_predictable([11, 11, None]), True)
        self.assertEqual(combined_fit_ll.is_predictable([11, 11, 11]), True)

        # Verify that fitted function behaves like input function
        for i, x in enumerate(X):
            self.assertAlmostEqual(combined_fit_ll.eval(x), f_ll(x), places=0)


if __name__ == "__main__":
    unittest.main()