test_param_add.py 1.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# Copyright 2021 Thomas A. R. Purcell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
14
from sissopp import (
15
16
17
18
    FeatureNode,
    AddParamNode,
    Unit,
    initialize_values_arr,
19
    initialize_param_storage,
Thomas Purcell's avatar
Thomas Purcell committed
20
    get_reg_optimizer,
21
)
22
23

import numpy as np
24
from scipy.stats import linregress
25
26
27
28
29
30


class InvalidFeatureMade(Exception):
    pass


31
32
33
34
def check_feat_parmeters(feat, prop):
    return linregress(feat.value, prop)[2] ** 2.0 > 0.99


35
def test_param_add_node():
36
37
38
    task_sizes_train = [90]
    task_sizes_test = [10]

Thomas Purcell's avatar
Thomas Purcell committed
39
    initialize_values_arr(np.array(task_sizes_train), np.array(task_sizes_test), 2, 1)
40
    initialize_param_storage()
41

42
43
    data_1 = np.linspace(-20, 20, task_sizes_train[0])
    test_data_1 = np.linspace(-19.99, 19.99, task_sizes_test[0])
44
    feat_1 = FeatureNode(0, "t_a", data_1, test_data_1, Unit())
45

46
47
    data_2 = np.linspace(-14.256, 18.6523, task_sizes_train[0])
    test_data_2 = np.linspace(-16.256, 17.6523, task_sizes_test[0])
48
    feat_2 = FeatureNode(1, "x_a", data_2, test_data_2, Unit())
49

50
    prop = -2.3 * (data_1 + 1.5 * data_2) - 1.2
51
    optimizer = get_reg_optimizer([task_sizes_train[0]], prop, 1, 1, 0.5, False)
52

53
    feat_node = AddParamNode(feat_1, feat_2, 2, 1e-50, 1e50)
Thomas Purcell's avatar
Thomas Purcell committed
54
    feat_node.get_parameters(optimizer)
55

56
    assert check_feat_parmeters(feat_node, prop)
57

Thomas Purcell's avatar
Thomas Purcell committed
58
59
60
    p_0, p_1 = feat_node.parameters
    assert feat_node.matlab_fxn_expr == f"(t_a + ({p_0:.6e}.*x_a))"

61
62
63

if __name__ == "__main__":
    test_param_add_node()