Node.cpp 3.07 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
void Node::set_standardized_value(const bool for_comp) const
52
53
54
55
56
57
58
59
{
    double* stand_val_ptr;
    if(_selected)
    {
        stand_val_ptr = node_value_arrs::get_stand_d_matrix_ptr(_d_mat_ind);
    }
    else
    {
60
        stand_val_ptr = node_value_arrs::access_temp_stand_storage(_arr_ind, for_comp);
61
62
    }

63
    util_funcs::standardize(value_ptr(-1, for_comp), _n_samp, stand_val_ptr);
64
65
}

66
void Node::set_standardized_test_value(const bool for_comp) const
67
{
68
69
70
    double* val_ptr = value_ptr(-1, for_comp);
    double* test_val_ptr = test_value_ptr(-1, for_comp);
    double* stand_val_ptr = node_value_arrs::access_temp_stand_storage_test(_arr_ind, for_comp);
71
72
73
74
75
76
77
78
79
80
81

    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;}
    );
}

82
double* Node::stand_value_ptr(const bool for_comp) const
83
84
85
86
87
{
    if(_selected)
    {
        return node_value_arrs::get_stand_d_matrix_ptr(_d_mat_ind);
    }
88
89
    set_standardized_value(for_comp);
    return node_value_arrs::access_temp_stand_storage(_arr_ind, for_comp);
90
91
}

92
double* Node::stand_test_value_ptr(const bool for_comp) const
93
{
94
95
    set_standardized_test_value(for_comp);
    return node_value_arrs::access_temp_stand_storage_test(_arr_ind, for_comp);
96
}