summaryrefslogtreecommitdiff
path: root/test/test_parameters.py
blob: 5d7ec84654a11854f8bbd679b6932f904bff305c (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
#!/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_str,
            "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)


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