test_feature_space.py 4.55 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.
Thomas Purcell's avatar
Thomas Purcell committed
14
15
import shutil
import numpy as np
Thomas Purcell's avatar
Thomas Purcell committed
16

17
from sissopp import (
Thomas Purcell's avatar
Thomas Purcell committed
18
    FeatureNode,
19
20
    FeatureSpace,
    Inputs,
Thomas Purcell's avatar
Thomas Purcell committed
21
22
23
24
    Unit,
    initialize_values_arr,
)

Thomas Purcell's avatar
Thomas Purcell committed
25
26
27
28
import pathlib

parent = pathlib.Path(__file__).parent.absolute()

Thomas Purcell's avatar
Thomas Purcell committed
29
30

def test_feature_space():
31
32
33
34
    task_sizes_train = [90]
    task_sizes_test = [10]

    initialize_values_arr(task_sizes_train, task_sizes_test, 10, 2)
35
36
37

    inputs = Inputs()
    inputs.phi_0 = [
Thomas Purcell's avatar
Thomas Purcell committed
38
39
40
        FeatureNode(
            ff,
            f"feat_{ff}",
41
42
            np.random.random(task_sizes_train[0]) * 1e2 - 50,
            np.random.random(task_sizes_test[0]) * 1e2 - 50,
Thomas Purcell's avatar
Thomas Purcell committed
43
            Unit("s"),
Thomas Purcell's avatar
Thomas Purcell committed
44
45
46
47
        )
        for ff in range(10)
    ]

48
    inputs.prop_train = np.power(inputs.phi_0[0].value + inputs.phi_0[1].value, 2.0)
Thomas Purcell's avatar
Thomas Purcell committed
49

50
    inputs.allowed_ops = ["add", "sub", "mult", "sq", "cb", "sqrt", "cbrt"]
51
52
    inputs.task_sizes_train = task_sizes_train
    inputs.task_sizes_test = task_sizes_test
53
54
55
    inputs.calc_type = "regression"
    inputs.max_rung = 2
    inputs.n_sis_select = 10
Thomas Purcell's avatar
Thomas Purcell committed
56
    inputs.phi_out_file = "feature_space/phi.txt"
Thomas Purcell's avatar
Thomas Purcell committed
57

Thomas Purcell's avatar
Thomas Purcell committed
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
    try:
        inputs.n_rung_generate = 2
        feat_space = FeatureSpace(inputs)
        raise ValueError("FeatureSpace created with invalid parameters")
    except RuntimeError:
        inputs.n_rung_generate = 0
        pass

    try:
        inputs.n_rung_generate = 1
        inputs.n_rung_store = 2
        feat_space = FeatureSpace(inputs)
        raise ValueError("FeatureSpace created with invalid parameters")
    except RuntimeError:
        inputs.n_rung_generate = 0
        inputs.n_rung_store = 1
        pass

    try:
        inputs.allowed_ops = ["exp"]
        feat_space = FeatureSpace(inputs)
        raise ValueError(
            "FeatureSpace created when there is a rung with no features created"
        )
    except RuntimeError:
        inputs.allowed_ops = ["add", "sub", "mult", "sq", "cb", "sqrt", "cbrt"]
        pass

86
    feat_space = FeatureSpace(inputs)
87
88
    assert feat_space.phi0[0].expr == inputs.phi_0[0].expr

89
    feat_space.sis(inputs.prop_train)
Thomas Purcell's avatar
Thomas Purcell committed
90

Thomas Purcell's avatar
Thomas Purcell committed
91
    assert feat_space.phi_selected[0].postfix_expr == "1|0|add|sq"
Thomas Purcell's avatar
Thomas Purcell committed
92

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    feat_space.sis(list(inputs.prop_train))
    shutil.rmtree("feature_space/")
    assert feat_space.phi_selected[0].postfix_expr == "1|0|add|sq"

    assert (
        np.abs(
            np.corrcoef(inputs.prop_train, inputs.phi_0[0].value)[0, 1] ** 2.0
            + feat_space.scores[0]
        )
        < 1e-10
    )
    assert feat_space.task_sizes_train[0] == 90
    assert feat_space.allowed_param_ops == []
    assert feat_space.allowed_ops == ["add", "sub", "mult", "sq", "cb", "sqrt", "cbrt"]
    assert feat_space.start_rung[0] == 0
    assert feat_space.start_rung[1] == 10
    assert feat_space.get_feature(0).expr == "feat_0"
    assert feat_space.phi_selected[1].d_mat_ind == 1

Thomas Purcell's avatar
Thomas Purcell committed
112
113
114
115
116
117
    try:
        feat_space.remove_feature(feat_space.phi_selected[0].feat_ind)
        raise ValueError("Removed selected feature.")
    except RuntimeError:
        pass

118
119
120
121
    test_expr = feat_space.get_feature(len(feat_space.phi) - 2).expr
    feat_space.remove_feature(len(feat_space.phi) - 2)
    assert feat_space.get_feature(len(feat_space.phi) - 2).expr != test_expr

122
123
124
125
126
127
128
129
130
    if feat_space.parameterized_feats_allowed:
        try:
            inputs.max_param_depth = 10
            feat_space = FeatureSpace(inputs)
            raise ValueError("FeatureSpace created with invalid parameters")
        except RuntimeError:
            inputs.max_param_depth = 0
            pass

Thomas Purcell's avatar
Thomas Purcell committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    feat_space.output_phi()
    feat_space_2 = FeatureSpace(
        f"{parent}/feature_space/phi.txt",
        inputs.phi_0,
        inputs.prop_train,
        inputs.task_sizes_train,
        inputs.calc_type,
        inputs.n_sis_select,
        inputs.cross_cor_max,
    )

    # shutil.rmtree("feature_space/")
    assert feat_space.n_feat == feat_space_2.n_feat

    feat_space_2.sis(inputs.prop_train)
    assert (
        feat_space.phi_selected[0].postfix_expr
        == feat_space_2.phi_selected[0].postfix_expr
    )

Thomas Purcell's avatar
Thomas Purcell committed
151
152
153

if __name__ == "__main__":
    test_feature_space()