FeatureSpace.hpp 5.17 KB
Newer Older
Thomas Purcell's avatar
Thomas Purcell committed
1
2
3
#ifndef FEATURE_SPACE
#define FEATURE_SPACE

Thomas Purcell's avatar
Thomas Purcell committed
4
#include <mpi_interface/MPI_Interface.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
5
6
#include <feature_creation/node/FeatureNode.hpp>
#include <feature_creation/node/operator_nodes/allowed_ops.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
7
#include <feature_creation/node/value_storage/nodes_value_containers.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
8
#include <utils/project.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
9

Thomas Purcell's avatar
Thomas Purcell committed
10
#include <boost/serialization/shared_ptr.hpp>
11
#include <boost/filesystem.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
12
#include <boost/python.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
13

Thomas Purcell's avatar
Thomas Purcell committed
14
#include <iostream>
Thomas Purcell's avatar
Thomas Purcell committed
15
#include <iomanip>
Thomas Purcell's avatar
Thomas Purcell committed
16

Thomas Purcell's avatar
Thomas Purcell committed
17
// namespace mpi = boost::mpi;
18
19
20
21
22
/**
 * @brief Feature Space for SISSO calculations
 * @details Stores and performs all feature calculations for SIS
 *
 */
Thomas Purcell's avatar
Thomas Purcell committed
23
24
class FeatureSpace
{
25
    std::vector<node_ptr> _phi_selected; //!< selected features
26
27
28
29
30
31
32
33
34
35
    std::vector<node_ptr> _phi; //!< all features
    std::vector<node_ptr> _phi_0; //!< initial feature space

    std::vector<std::string> _allowed_ops; //!< list of all allowed operators strings
    std::vector<un_op_node_gen> _un_operators; //!< list of all unary operators
    std::vector<bin_op_node_gen> _com_bin_operators; //!< list of all commutable binary operators
    std::vector<bin_op_node_gen> _bin_operators; //!< list of all binary operators

    std::vector<double> _scores; //!< projection scores for each feature

Thomas Purcell's avatar
Thomas Purcell committed
36
    std::vector<int> _task_sizes; //!< The number of elements in each task
37
    std::vector<int> _start_gen; //!< list of starting index for each generation
38
    std::string _feature_space_file; //!< File to store infromation on the feature space
39

Thomas Purcell's avatar
Thomas Purcell committed
40
    std::function<void(double*, double*, std::vector<node_ptr>&, std::vector<int>&, int)> _project; //!< Function used for projection onto SIS
41
    std::shared_ptr<MPI_Interface> _mpi_comm; //!< MPi communicator
42
43
44
45

    double _l_bound; //!< lower bound for absolute value of the features
    double _u_bound; //!< upper bound for absolute value of the features

46
47
48
49
    int _max_phi; //!< Maximum rung for the feature creation
    int _n_sis_select; //!< Number of features to select for each dimensions
    int _n_samp; //!< Number of samples
    int _n_feat; //!< Total number of features
Thomas Purcell's avatar
Thomas Purcell committed
50
    int _n_rung_store; //!< Total rungs stored
51
    int _n_rung_generate; //!< Total number of rungs to generate on the fly
Thomas Purcell's avatar
Thomas Purcell committed
52
    int _max_temp_store;
Thomas Purcell's avatar
Thomas Purcell committed
53
public:
Thomas Purcell's avatar
Thomas Purcell committed
54

55
56
57
58
59
60
61
62
63
64
    /**
     * @brief Constructor for the feature space
     * @details constructs the feature space from an initial set of features and a list of allowed operatiors
     *
     * @param mpi_comm MPI communicator for the calculations
     * @param allowed_ops list of allowed operators
     * @param max_phi highest rung value for the calculation
     * @param n_sis_select number of features to select during each SIS step
     * @param max_abs_feat_val maximum absolute feature value
     */
Thomas Purcell's avatar
Thomas Purcell committed
65
    FeatureSpace(
Thomas Purcell's avatar
Thomas Purcell committed
66
        std::shared_ptr<MPI_Interface> mpi_comm,
Thomas Purcell's avatar
Thomas Purcell committed
67
68
        std::vector<node_ptr> phi_0,
        std::vector<std::string> allowed_ops,
69
        std::vector<double> prop,
Thomas Purcell's avatar
Thomas Purcell committed
70
        std::vector<int> task_sizes,
Thomas Purcell's avatar
Thomas Purcell committed
71
72
        int max_phi=1,
        int n_sis_select=1,
Thomas Purcell's avatar
Thomas Purcell committed
73
        int max_store_rung=2,
74
        int n_rung_generate=0,
Thomas Purcell's avatar
Thomas Purcell committed
75
        int max_temp_store=-1,
Thomas Purcell's avatar
Thomas Purcell committed
76
77
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50
Thomas Purcell's avatar
Thomas Purcell committed
78
79
    );

80
81
82
83
    /**
     * @brief Generate the full feature set from the allowed operators and initial feature set
     * @details populates phi with all features from an initial set and the allowed operators
     */
84
    void generate_feature_space(std::vector<double>& prop);
Thomas Purcell's avatar
Thomas Purcell committed
85

86
87
88
    /**
     * @brief Accessor function for _phi_selected
     */
89
    inline std::vector<node_ptr> phi_selected(){return _phi_selected;};
90
91
92
93

    /**
     * @brief Accessor function for _phi
     */
Thomas Purcell's avatar
Thomas Purcell committed
94
    inline std::vector<node_ptr> phi(){return _phi;};
95
96
97
98

    /**
     * @brief Accessor function for _phi_0
     */
Thomas Purcell's avatar
Thomas Purcell committed
99
    inline std::vector<node_ptr> phi0(){return _phi_0;};
100
101
102
103

    /**
     * @brief Accessor function for _scores
     */
Thomas Purcell's avatar
Thomas Purcell committed
104
    inline std::vector<double> scores(){return _scores;};
105
106
107
108

    /**
     * @brief Accessor function for _mpi_comm
     */
Thomas Purcell's avatar
Thomas Purcell committed
109
    inline std::shared_ptr<MPI_Interface> mpi_comm(){return _mpi_comm;}
110

Thomas Purcell's avatar
Thomas Purcell committed
111
    inline std::vector<int> task_sizes(){return _task_sizes;}
112
113
114

    void generate_new_feats(std::vector<node_ptr>::iterator& feat, std::vector<node_ptr>& feat_set, int& feat_ind, double l_bound=1e-50, double u_bound=1e50);

115
    void project_generated(double* prop, int size, std::vector<node_ptr>& phi_selected, std::vector<double>& scores_selected, std::vector<double>& scores_comp);
116

117
    bool valid_score_against_past(double* val_ptr, double cur_score, std::vector<double> scores_past, std::vector<double>& scores_comp);
118
119

    bool valid_score_against_current(int end_check, double* val_ptr, double cur_score, std::vector<double>& scores_selected, std::vector<double>& scores_comp);
120
121
122
123
124
125
    /**
     * @brief Perform SIS on a feature set with a specified property
     * @details Perform sure-independence screening with either the correct property
     *
     * @param prop The property to calculate SIS from
     */
Thomas Purcell's avatar
Thomas Purcell committed
126
    void sis(std::vector<double>& prop);
127
128
129
130
131
132
133
134
135

    /**
     * @brief Is a feature in this process' _phi?
     *
     * @param ind index
     * @return True if feature is in this _phi
     */
    inline bool feat_in_phi(int ind){return (ind >= _phi[0]->feat_ind()) && (ind <= _phi.back()->feat_ind());}

Thomas Purcell's avatar
Thomas Purcell committed
136
    static void register_python();
Thomas Purcell's avatar
Thomas Purcell committed
137
138
139
};

#endif