Node.cpp 4.75 KB
Newer Older
1
// Copyright 2021 Thomas A. R. Purcell
2
//
3
4
5
// 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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
10
11
12
13
// 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
15
16
17
18
19
20
21
22
23
24
25

/** @file feature_creation/node/Node.cpp
 *  @brief Implements the base class for the objects that represent features
 *
 *  @author Thomas A. R. Purcell (tpurcell90)
 *  @bug No known bugs.
 *
 *  This package represents the features in SISSO as a binary expression tree and are accessible from the root node of that tree.
 *  The node class are the vertices of the tree and represent initial features (FeatureNode) and all algebraic operators (OperatorNodes).
 *  The edges are the represented via the _feats member in OperatorNode that store the features the operation acts on (its children).
 */

Thomas Purcell's avatar
Thomas Purcell committed
26
#include "feature_creation/node/Node.hpp"
Thomas Purcell's avatar
Thomas Purcell committed
27

Thomas Purcell's avatar
Thomas Purcell committed
28
29
30
Node::Node()
{}

Thomas Purcell's avatar
Thomas Purcell committed
31
32
Node::Node(const unsigned long int feat_ind, const int n_samp, const int n_samp_test) :
    _n_samp_test(n_samp_test),
Thomas Purcell's avatar
Thomas Purcell committed
33
    _n_samp(n_samp),
34
    _feat_ind(feat_ind),
35
36
37
    _arr_ind(feat_ind),
    _d_mat_ind(-1),
    _selected(false)
Thomas Purcell's avatar
Thomas Purcell committed
38
39
{}

Thomas Purcell's avatar
Thomas Purcell committed
40
Node::~Node()
41
42
{}

Thomas Purcell's avatar
Thomas Purcell committed
43
44
45
46
47
48
std::map<std::string, int> Node::primary_feature_decomp() const
{
    std::map<std::string, int> pf_decomp;
    update_primary_feature_decomp(pf_decomp);
    return pf_decomp;
}
Thomas Purcell's avatar
Thomas Purcell committed
49
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Node)
Thomas Purcell's avatar
Thomas Purcell committed
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
void Node::set_standardized_value(int offset, const bool for_comp) const
{
    double* stand_val_ptr;
    if(_selected)
    {
        stand_val_ptr = node_value_arrs::get_stand_d_matrix_ptr(_d_mat_ind);
    }
    else
    {
        stand_val_ptr = node_value_arrs::access_temp_stand_storage(_arr_ind);
    }

    util_funcs::standardize(value_ptr(offset, for_comp), _n_samp, stand_val_ptr);
}

void Node::set_standardized_test_value(int offset, const bool for_comp) const
{
    double* val_ptr = value_ptr(offset, for_comp);
    double* test_val_ptr = test_value_ptr(offset, for_comp);
    double* stand_val_ptr = node_value_arrs::access_temp_stand_storage_test(_arr_ind);

    double mean = util_funcs::mean(val_ptr, _n_samp);
    double stand_dev = util_funcs::stand_dev(val_ptr, _n_samp, mean);
    std::transform(
        test_val_ptr,
        test_val_ptr + _n_samp_test,
        stand_val_ptr,
        [&](double val){return (val - mean) / stand_dev;}
    );
}

void Node::set_standardized_value(const double* params, int offset, const bool for_comp, const int depth) const
{
    double* stand_val_ptr;
    if(_selected)
    {
        stand_val_ptr = node_value_arrs::get_stand_d_matrix_ptr(_d_mat_ind);
    }
    else
    {
        stand_val_ptr = node_value_arrs::access_temp_stand_storage(_arr_ind);
    }

    util_funcs::standardize(value_ptr(params, offset, for_comp, depth), _n_samp, stand_val_ptr);
}

void Node::set_standardized_test_value(const double* params, int offset, const bool for_comp, const int depth) const
{
    double* val_ptr = value_ptr(params, offset, for_comp, depth);
    double* test_val_ptr = test_value_ptr(params, offset, for_comp, depth);
    double* stand_val_ptr = node_value_arrs::access_temp_stand_storage_test(_arr_ind);

    double mean = util_funcs::mean(val_ptr, _n_samp);
    double stand_dev = util_funcs::stand_dev(val_ptr, _n_samp, mean);
    std::transform(
        test_val_ptr,
        test_val_ptr + _n_samp_test,
        stand_val_ptr,
        [&](double val){return (val - mean) / stand_dev;}
    );
}

double* Node::stand_value_ptr(int offset, const bool for_comp) const
{
    if(_selected)
    {
        return node_value_arrs::get_stand_d_matrix_ptr(_d_mat_ind);
    }
    set_standardized_value(offset, for_comp);
    return node_value_arrs::access_temp_stand_storage(_arr_ind);
}

double* Node::stand_test_value_ptr(int offset, const bool for_comp) const
{
    set_standardized_test_value(offset, for_comp);
    return node_value_arrs::access_temp_stand_storage_test(_arr_ind);
}

double* Node::stand_value_ptr(const double* params, int offset, const bool for_comp, const int depth) const
{
    if(_selected)
    {
        return node_value_arrs::get_stand_d_matrix_ptr(_d_mat_ind);
    }
    set_standardized_value(params, offset, for_comp, depth);
    return node_value_arrs::access_temp_stand_storage(_arr_ind);
}

double* Node::stand_test_value_ptr(const double* params, int offset, const bool for_comp, const int depth) const
{
    set_standardized_test_value(params, offset, for_comp, depth);
    return node_value_arrs::access_temp_stand_storage_test(_arr_ind);
}