Model.hpp 3.38 KB
Newer Older
Thomas Purcell's avatar
Thomas Purcell committed
1
2
3
#ifndef MODEL
#define MODEL

4
5
6
7
8
9
#include <boost/filesystem.hpp>

#include<iomanip>
#include<fstream>
#include<iostream>

10
#include <feature_creation/node/FeatureNode.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
11

Thomas Purcell's avatar
Thomas Purcell committed
12
13
14
15
/**
 * @brief Class to store the models found from SISSO
 *
 */
Thomas Purcell's avatar
Thomas Purcell committed
16
17
class Model
{
18
19
    int _n_samp_train; //!< The number of samples per feature
    int _n_samp_test; //!< The number of test samples per feature
Thomas Purcell's avatar
Thomas Purcell committed
20
    int _n_dim; //!< Dimension of the model
Thomas Purcell's avatar
Thomas Purcell committed
21

22
    std::vector<node_ptr> _feats; //!< List of features in the model
Thomas Purcell's avatar
Thomas Purcell committed
23

Thomas Purcell's avatar
Thomas Purcell committed
24
    std::vector<std::vector<double>> _coefs; //!< Coefficients for teh features
25
26
27
28
29
30
    std::vector<double> _prop_train; //!< The property to be modeled
    std::vector<double> _prop_test; //!< The property to be modeled
    std::vector<double> _train_error; //!< The error of the model
    std::vector<double> _test_error; //!< The error of the model
    std::vector<double> _D_train; //!< The Descriptor matrix
    std::vector<double> _D_test; //!< The Descriptor matrix
Thomas Purcell's avatar
Thomas Purcell committed
31

32
33
    std::vector<double> _prop_train_est; //!< The estimated Property
    std::vector<double> _prop_test_est; //!< The estimated Property
Thomas Purcell's avatar
Thomas Purcell committed
34

Thomas Purcell's avatar
Thomas Purcell committed
35
36
    std::vector<int> _task_sizes_train; //!< Number of samples in each task
    std::vector<int> _task_sizes_test; //!< Number of samples in each task
Thomas Purcell's avatar
Thomas Purcell committed
37
public:
Thomas Purcell's avatar
Thomas Purcell committed
38
39
40
41
42
43
    /**
     * @brief Constructor for the model
     *
     * @param prop The property
     * @param feats The features for the model
     */
44
    Model(std::vector<double> prop_train, std::vector<double> prop_test, std::vector<node_ptr> feats, std::vector<int> task_sizes_train, std::vector<int> task_sizes_test);
Thomas Purcell's avatar
Thomas Purcell committed
45

Thomas Purcell's avatar
Thomas Purcell committed
46

Thomas Purcell's avatar
Thomas Purcell committed
47
48
49
50
51
    /**
     * @brief Convert the model to a string

     * @return The string of the model
     */
Thomas Purcell's avatar
Thomas Purcell committed
52
    std::string toString() const;
Thomas Purcell's avatar
Thomas Purcell committed
53
54
55
56

    /**
     * @brief Accessor function to _prop_est
     */
57
58
59
60
61
62
    inline std::vector<double>& predict(){return _prop_test_est;}

    /**
     * @brief Accessor function to _prop_est
     */
    inline std::vector<double>& predict_train(){return _prop_train_est;}
Thomas Purcell's avatar
Thomas Purcell committed
63
64
65
66
67
68

    /**
     * @brief Copy the error into a new array
     *
     * @param res pointer to the beginning of the array
     */
69
    inline void copy_error(double* res){std::copy_n(_train_error.data(), _n_samp_train, res);}
Thomas Purcell's avatar
Thomas Purcell committed
70
71
72
73

    /**
     * @brief The rmes of the model
     */
74
75
    inline double rmse(){return util_funcs::norm(_train_error.data(), _n_samp_train) / std::sqrt(static_cast<double>(_n_samp_train));}
    inline double test_rmse(){return util_funcs::norm(_test_error.data(), _n_samp_test) / std::sqrt(static_cast<double>(_n_samp_test));}
Thomas Purcell's avatar
Thomas Purcell committed
76
77
78
79

    /**
     * @brief The max Absolute error of the array
     */
Thomas Purcell's avatar
Thomas Purcell committed
80
81
    inline double max_ae()
    {
Thomas Purcell's avatar
Thomas Purcell committed
82
        return std::abs(*std::max_element(_train_error.data(), _train_error.data() + _n_samp_train, [](double d1, double d2){return std::abs(d1) < std::abs(d2);}));
83
84
85
86
    }

    inline double test_max_ae()
    {
Thomas Purcell's avatar
Thomas Purcell committed
87
        return std::abs(*std::max_element(_test_error.data(), _test_error.data() + _n_samp_test, [](double d1, double d2){return std::abs(d1) < std::abs(d2);}));
Thomas Purcell's avatar
Thomas Purcell committed
88
    }
89
90
91
92
93


    /**
     * @brief Print model to a file
     */
Thomas Purcell's avatar
Thomas Purcell committed
94
    void test_to_file(std::string filename, std::vector<int> test_inds);
95
96
97
98
99

    /**
     * @brief Print model to a file
     */
    void train_to_file(std::string filename);
Thomas Purcell's avatar
Thomas Purcell committed
100
101
};

Thomas Purcell's avatar
Thomas Purcell committed
102
103
104
105
106
107
/**
 * @brief Print a model to an string stream
 *
 * @param outStream The output stream the model is to be printed
 * @param model The model to be printed
 */
Thomas Purcell's avatar
Thomas Purcell committed
108
109
110
std::ostream& operator<< (std::ostream& outStream, const Model& model);

#endif