FeatureSpace.hpp 16.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
/** @file feature_creation/feature_space/FeatureSpace.hpp
 *  @brief Create a feature space from an initial set of features and algebraic operators
 *
 *  Use an initial set of features and combine them to generate more complicated algebraical features. SIS is also performed here
 *
 *  @author Thomas A. R. Purcell (tpurcell)
 *  @bug No known bugs.
 */

Thomas Purcell's avatar
Thomas Purcell committed
10
11
12
#ifndef FEATURE_SPACE
#define FEATURE_SPACE

Thomas Purcell's avatar
Thomas Purcell committed
13
#include <mpi_interface/MPI_Interface.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
14
#include <feature_creation/node/FeatureNode.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
15
#include <feature_creation/node/ModelNode.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
16
#include <feature_creation/node/operator_nodes/allowed_ops.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
17
#include <feature_creation/node/value_storage/nodes_value_containers.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
18
#include <utils/project.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
19

Thomas Purcell's avatar
Thomas Purcell committed
20
#include <boost/serialization/shared_ptr.hpp>
21
#include <boost/filesystem.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
22

Thomas Purcell's avatar
Thomas Purcell committed
23
#include <iostream>
Thomas Purcell's avatar
Thomas Purcell committed
24
#include <iomanip>
Thomas Purcell's avatar
Thomas Purcell committed
25

26
27
28
29
#ifdef PY_BINDINGS
    namespace np = boost::python::numpy;
    namespace py = boost::python;
#endif
30

31
// DocString: cls_feat_space
32
33
34
35
36
/**
 * @brief Feature Space for SISSO calculations
 * @details Stores and performs all feature calculations for SIS
 *
 */
Thomas Purcell's avatar
Thomas Purcell committed
37
38
class FeatureSpace
{
39
    std::vector<node_ptr> _phi_selected; //!< selected features
40
41
42
43
44
45
46
47
48
49
    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

50
51
52
    std::vector<int> _task_sizes; //!< The number of elements in each task (training data)
    std::vector<int> _start_gen; //!< list of the indexes where each generation starts in _phi
    std::string _feature_space_file; //!< File to store information about the selected features
Thomas Purcell's avatar
Thomas Purcell committed
53
    std::string _feature_space_summary_file; //!< File to store information about the selected features
54

55
56
    std::function<void(double*, double*, std::vector<node_ptr>&, std::vector<int>&, int)> _project; //!< Function used to calculate the scores for SIS
    std::shared_ptr<MPI_Interface> _mpi_comm; //!< MPI communicator
57

Thomas Purcell's avatar
Thomas Purcell committed
58
    double _cross_cor_max; //!< Maximum cross-correlation used for selecting features
59
60
61
    double _l_bound; //!< lower bound for absolute value of the features
    double _u_bound; //!< upper bound for absolute value of the features

62
63
    int _max_phi; //!< Maximum rung for the feature creation
    int _n_sis_select; //!< Number of features to select for each dimensions
64
    int _n_samp; //!< Number of samples (training data)
65
    int _n_feat; //!< Total number of features
Thomas Purcell's avatar
Thomas Purcell committed
66
    int _n_rung_store; //!< Total rungs stored
67
    int _n_rung_generate; //!< Total number of rungs to generate on the fly
Thomas Purcell's avatar
Thomas Purcell committed
68
public:
Thomas Purcell's avatar
Thomas Purcell committed
69

70
71
    /**
     * @brief Constructor for the feature space
72
     * @details constructs the feature space from an initial set of features and a list of allowed operators
73
74
     *
     * @param mpi_comm MPI communicator for the calculations
75
     * @param phi_0 The initial set of features to combine
76
     * @param allowed_ops list of allowed operators
77
     * @param prop The property to be learned (training data)
78
79
     * @param max_phi highest rung value for the calculation
     * @param n_sis_select number of features to select during each SIS step
80
81
     * @param max_store_rung number of rungs to calculate and store the value of the features for all samples
     * @param n_rung_generate number of rungs to generate on the fly during SIS (this must be 1 or 0 right now, possible to be higher with recursive algorithm)
Thomas Purcell's avatar
Thomas Purcell committed
82
     * @param cross_corr_max Maximum cross-correlation used for selecting features
83
     * @param min_abs_feat_val minimum absolute feature value
84
85
     * @param max_abs_feat_val maximum absolute feature value
     */
Thomas Purcell's avatar
Thomas Purcell committed
86
    FeatureSpace(
Thomas Purcell's avatar
Thomas Purcell committed
87
        std::shared_ptr<MPI_Interface> mpi_comm,
Thomas Purcell's avatar
Thomas Purcell committed
88
89
        std::vector<node_ptr> phi_0,
        std::vector<std::string> allowed_ops,
90
        std::vector<double> prop,
Thomas Purcell's avatar
Thomas Purcell committed
91
        std::vector<int> task_sizes,
Thomas Purcell's avatar
Thomas Purcell committed
92
93
        int max_phi=1,
        int n_sis_select=1,
94
95
        int max_store_rung=-1,
        int n_rung_generate=0,
Thomas Purcell's avatar
Thomas Purcell committed
96
        double cross_corr_max=1.0,
97
98
99
100
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50
    );

101
102
103
104
105
    /**
     * @brief Initialize the feature set given a property vector
     *
     * @param prop The property trying to be learned
     */
106
107
    void initialize_fs(std::vector<double> prop);

108
109
110
111
    /**
     * @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
     */
112
    void generate_feature_space(std::vector<double>& prop);
Thomas Purcell's avatar
Thomas Purcell committed
113

114
    /**
115
     * @brief The selected feature space
116
     */
117
    inline std::vector<node_ptr> phi_selected(){return _phi_selected;};
118
119

    /**
120
     * @brief The full feature space
121
     */
Thomas Purcell's avatar
Thomas Purcell committed
122
    inline std::vector<node_ptr> phi(){return _phi;};
123
124

    /**
125
     * @brief The initial feature space
126
     */
Thomas Purcell's avatar
Thomas Purcell committed
127
    inline std::vector<node_ptr> phi0(){return _phi_0;};
128
129

    /**
130
     * @brief The vector of projection scores for SIS
131
     */
132
133
    inline std::vector<double> scores(){return _scores;}

134
    /**
135
     * @brief The MPI Communicator
136
     */
Thomas Purcell's avatar
Thomas Purcell committed
137
    inline std::shared_ptr<MPI_Interface> mpi_comm(){return _mpi_comm;}
138

139
    /**
140
     * @brief The vector storing the number of samples in each task
141
     */
Thomas Purcell's avatar
Thomas Purcell committed
142
    inline std::vector<int> task_sizes(){return _task_sizes;}
143

144
    // DocString: feat_space_feature_space_file
145
    /**
146
     * @brief The feature space filename
147
     */
148
    inline std::string feature_space_file(){return _feature_space_file;}
149

150
    // DocString: feat_space_l_bound
151
    /**
152
     * @brief The minimum absolute value of the feature
153
     */
154
    inline double l_bound(){return _l_bound;}
155

156
    // DocString: feat_space_u_bound
157
    /**
158
     * @brief The maximum absolute value of the feature
159
     */
160
    inline double u_bound(){return _u_bound;}
161

162
    // DocString: feat_space_max_phi
163
    /**
164
     * @brief The maximum rung of the feature space
165
     */
166
    inline int max_phi(){return _max_phi;}
167

168
    // DocString: feat_space_n_sis_select
169
    /**
170
     * @brief The number of features selected in each SIS step
171
     */
172
    inline int n_sis_select(){return _n_sis_select;}
173

174
    // DocString: feat_space_n_samp
175
    /**
176
     * @brief The number of samples per feature
177
     */
178
    inline int n_samp(){return _n_samp;}
179

180
    // DocString: feat_space_n_feat
181
    /**
182
     * @brief The number of features in the feature space
183
     */
184
    inline int n_feat(){return _n_feat;}
185

186
    // DocString: feat_space_n_rung_store
187
    /**
188
     * @brief The number of rungs whose feature training data is stored in memory
189
     */
190
    inline int n_rung_store(){return _n_rung_store;}
191

192
    // DocString: feat_space_n_rung_generate
193
    /**
194
     * @brief The number of rungs to be generated on the fly during SIS
195
     */
196
    inline int n_rung_generate(){return _n_rung_generate;}
197

198
199
200
201
202
203
204
205
206
207
    /**
     * @brief Generate a new set of features from a single feature
     * @details Take in the feature and perform all valid algebraic operations on it.
     *
     * @param feat The feature to spawn new features from
     * @param feat_set The feature set to pull features from for combinations
     * @param feat_ind starting index for the next feature generated
     * @param l_bound lower bound for the absolute value of the feature
     * @param u_bound upper bound for the abosulte value of the feature
     */
208
209
    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);

210
211
212
213
214
215
216
217
218
219
    /**
     * @brief Calculate the SIS Scores for feature generated on the fly
     * @details Create the next rung of features and calculate their projection scores. Only keep those that can be selected by SIS.
     *
     * @param prop Pointer to the start of the vector storing the data to project the features onto
     * @param size The size of the data to project over
     * @param phi_selected The features that would be selected from the previous rungs
     * @param scores_selected The projection scores of the features that would be selected from the previous rungs
     * @param scores_comp vector to store temporary score comparisons
     */
220
    void project_generated(double* prop, int size, std::vector<node_ptr>& phi_selected, std::vector<double>& scores_selected, std::vector<double>& scores_comp);
221

Thomas Purcell's avatar
Thomas Purcell committed
222
223
224
225
226
227
228
229
230
231
    /**
     * @brief Checks the feature to see if it is still valid against previously selected features
     *
     * @param val_ptr pointer to value array of the current feature
     * @param end_sel index of the feature to stop checking
     *
     * @return True if the feature is still valid
     */
    bool valid_feature_against_selected(double* val_ptr, int end_sel, int start_sel = 0);

232
233
234
235
236
237
238
239
240
241
242
    /**
     * @brief Check if a feature overlaps with a feature previously selected in earlier SIS iterations
     * @details Compares the projection score of the current candidate feature with all those of previously selected features (using the current prop) and
     *          if they are within 1e-10, then check the correlation between the features themselves
     *
     * @param val_ptr pointer to the candidate feature's data
     * @param cur_score the projection score of the candidate feature
     * @param scores_past The projection scores of the previous features
     * @param scores_comp vector to temporarily store the comparison of projection scores
     * @return True if the feature does not overlap with any previously selected
     */
243
    bool valid_score_against_past(double* val_ptr, double cur_score, std::vector<double> scores_past, std::vector<double>& scores_comp);
244

245
246
247
248
249
250
251
252
253
254
255
256
    /**
     * @brief Check if a feature overlaps with a feature previously selected in this SIS iterations
     * @details CCompares the projection score of the current candidate feature with all those of previously selected features in this iteration and
     *          if they are within 1e-10, then check the correlation between the features themselves
     *
     * @param end_check the end point to stop the comparison (the same as the current number of selected features)
     * @param val_ptr pointer to the candidate feature's data
     * @param cur_score the projection score of the candidate feature
     * @param scores_selected The projection scores of the previous features
     * @param scores_comp vector to temporarily store the comparison of projection scores
     * @return True if the feature does not overlap with any previously selected
     */
257
    bool valid_score_against_current(int end_check, double* val_ptr, double cur_score, std::vector<double>& scores_selected, std::vector<double>& scores_comp);
258
259
    /**
     * @brief Perform SIS on a feature set with a specified property
260
     * @details Perform sure-independence screening with either the correct property or the error
261
     *
262
     * @param prop The property to perform SIS over
263
     */
Thomas Purcell's avatar
Thomas Purcell committed
264
    void sis(std::vector<double>& prop);
265

266
    // DocString: feat_space_feat_in_phi
267
268
269
    /**
     * @brief Is a feature in this process' _phi?
     *
270
271
     * @param ind The index of the feature
     * @return True if feature is in this rank's _phi
272
273
274
     */
    inline bool feat_in_phi(int ind){return (ind >= _phi[0]->feat_ind()) && (ind <= _phi.back()->feat_ind());}

275
276
277
    // Python Interface Functions
    #ifdef PY_BINDINGS
        /**
278
279
         * @brief Constructor for the feature space that takes in python objects
         * @details constructs the feature space from an initial set of features and a list of allowed operators (cpp definition in <python/feature_creation/FeatureSpace.cpp>)
280
281
         *
         * @param mpi_comm MPI communicator for the calculations
282
         * @param phi_0 The initial set of features to combine
283
         * @param allowed_ops list of allowed operators
284
         * @param prop The property to be learned (training data)
285
286
         * @param max_phi highest rung value for the calculation
         * @param n_sis_select number of features to select during each SIS step
287
288
         * @param max_store_rung number of rungs to calculate and store the value of the features for all samples
         * @param n_rung_generate number of rungs to generate on the fly during SIS (this must be 1 or 0 right now, possible to be higher with recursive algorithm)
Thomas Purcell's avatar
Thomas Purcell committed
289
         * @param cross_corr_max Maximum cross-correlation used for selecting features
290
         * @param min_abs_feat_val minimum absolute feature value
291
292
293
294
295
296
297
298
299
300
301
         * @param max_abs_feat_val maximum absolute feature value
         */
        FeatureSpace(
            py::list phi_0,
            py::list allowed_ops,
            py::list prop,
            py::list task_sizes,
            int max_phi=1,
            int n_sis_select=1,
            int max_store_rung=-1,
            int n_rung_generate=0,
Thomas Purcell's avatar
Thomas Purcell committed
302
            double cross_corr_max=1.0,
303
304
305
306
307
            double min_abs_feat_val=1e-50,
            double max_abs_feat_val=1e50
        );

        /**
308
309
         * @brief Constructor for the feature space that takes in python and numpy objects
         * @details constructs the feature space from an initial set of features and a list of allowed operators (cpp definition in <python/feature_creation/FeatureSpace.cpp>)
310
311
         *
         * @param mpi_comm MPI communicator for the calculations
312
         * @param phi_0 The initial set of features to combine
313
         * @param allowed_ops list of allowed operators
314
         * @param prop The property to be learned (training data)
315
316
         * @param max_phi highest rung value for the calculation
         * @param n_sis_select number of features to select during each SIS step
317
318
         * @param max_store_rung number of rungs to calculate and store the value of the features for all samples
         * @param n_rung_generate number of rungs to generate on the fly during SIS (this must be 1 or 0 right now, possible to be higher with recursive algorithm)
Thomas Purcell's avatar
Thomas Purcell committed
319
         * @param cross_corr_max Maximum cross-correlation used for selecting features
320
         * @param min_abs_feat_val minimum absolute feature value
321
322
323
324
325
326
327
328
329
330
331
         * @param max_abs_feat_val maximum absolute feature value
         */
        FeatureSpace(
            py::list phi_0,
            py::list allowed_ops,
            np::ndarray prop,
            py::list task_sizes,
            int max_phi=1,
            int n_sis_select=1,
            int max_store_rung=-1,
            int n_rung_generate=0,
Thomas Purcell's avatar
Thomas Purcell committed
332
            double cross_corr_max=1.0,
333
334
335
336
            double min_abs_feat_val=1e-50,
            double max_abs_feat_val=1e50
        );

337
        // DocString: feat_space_sis_arr
338
339
340
        /**
         * @brief Wrapper function for SIS using a numpy array
         *
341
         * @param prop(np.ndarray) The property to perform SIS over as a numpy array
342
         */
343
344
345
346
347
        inline void sis(np::ndarray prop)
        {
            std::vector<double> prop_vec = python_conv_utils::from_ndarray<double>(prop);
            sis(prop_vec);
        }
348
349

        // DocString: feat_space_sis_list
350
351
352
        /**
         * @brief Wrapper function for SIS using a python list
         *
353
         * @param prop(list) The property to perform SIS over as a python list
354
         */
355
356
357
358
359
360
        inline void sis(py::list prop)
        {
            std::vector<double> prop_vec = python_conv_utils::from_list<double>(prop);
            sis(prop_vec);
        }

361
        // DocString: feat_space_phi_selected_py
362
        /**
363
         * @brief The selected feature space (cpp definition in <python/feature_creation/FeatureSpace.cpp>)
364
365
         * @return _phi_selected as a python list
         */
366
        py::list phi_selected_py();
367

368
        // DocString: feat_space_phi0_py
369
        /**
370
         * @brief The initial feature space (cpp definition in <python/feature_creation/FeatureSpace.cpp>)
371
372
         * @return _phi0 as a python list
         */
373
        py::list phi0_py();
374

375
        // DocString: feat_space_scores_py
376
        /**
377
         * @brief The vector of projection scores for SIS
378
379
         * @return _scores as a numpy array
         */
380
        inline np::ndarray scores_py(){return python_conv_utils::to_ndarray<double>(_scores);};
381

382
        // DocString: feat_space_task_sizes_py
383
        /**
384
         * @brief The vector storing the number of samples in each task
385
386
         * @return _task_sizes as a python list
         */
387
        inline py::list task_sizes_py(){return python_conv_utils::to_list<int>(_task_sizes);};
388

389
        // DocString: feat_space_allowed_ops_py
390
        /**
391
         * @brief The list of allowed operator nodes
392
393
         * @return _allowed_ops as a python list
         */
394
        inline py::list allowed_ops_py(){return python_conv_utils::to_list<std::string>(_allowed_ops);}
395

396
        // DocString: feat_space_start_gen_py
397
        /**
398
         * @brief The index in _phi where each generation starts
399
400
         * @return _start_gen as a python list
         */
401
        inline py::list start_gen_py(){return python_conv_utils::to_list<int>(_start_gen);}
402

403
    #endif
Thomas Purcell's avatar
Thomas Purcell committed
404
405
406
};

#endif