FeatureSpace.hpp 17.4 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>
17
#include <feature_creation/node/utils.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
18
#include <feature_creation/node/value_storage/nodes_value_containers.hpp>
19
#include <utils/compare_features.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
20
#include <utils/project.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
21

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

Thomas Purcell's avatar
Thomas Purcell committed
25
#include <iostream>
Thomas Purcell's avatar
Thomas Purcell committed
26
#include <iomanip>
Thomas Purcell's avatar
Thomas Purcell committed
27

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

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

52
53
54
    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
55
    std::string _feature_space_summary_file; //!< File to store information about the selected features
56

57
    std::function<void(double*, double*, std::vector<node_ptr>&, std::vector<int>&, int)> _project; //!< Function used to calculate the scores for SIS
58
    std::function<void(double*, double*, std::vector<node_ptr>&, std::vector<int>&, int)> _project_no_omp; //!< Function used to calculate the scores for SIS without changing omp environment
59
    std::function<bool(double*, int, double, std::vector<double>&, double, int, int)> _is_valid; //!< Function used to calculate the scores for SIS
Thomas Purcell's avatar
Bug fix    
Thomas Purcell committed
60
    std::function<bool(double*, int, double, std::vector<node_ptr>&, std::vector<double>&, double)> _is_valid_feat_list; //!< Function used to calculate the scores for SIS without changing omp environment
61
62


63
    std::shared_ptr<MPI_Interface> _mpi_comm; //!< MPI communicator
64

Thomas Purcell's avatar
Thomas Purcell committed
65
    double _cross_cor_max; //!< Maximum cross-correlation used for selecting features
66
67
68
    double _l_bound; //!< lower bound for absolute value of the features
    double _u_bound; //!< upper bound for absolute value of the features

69
70
    int _max_phi; //!< Maximum rung for the feature creation
    int _n_sis_select; //!< Number of features to select for each dimensions
71
    int _n_samp; //!< Number of samples (training data)
72
    int _n_feat; //!< Total number of features
Thomas Purcell's avatar
Thomas Purcell committed
73
    int _n_rung_store; //!< Total rungs stored
74
    int _n_rung_generate; //!< Total number of rungs to generate on the fly
Thomas Purcell's avatar
Thomas Purcell committed
75
public:
Thomas Purcell's avatar
Thomas Purcell committed
76

77
78
    /**
     * @brief Constructor for the feature space
79
     * @details constructs the feature space from an initial set of features and a list of allowed operators
80
81
     *
     * @param mpi_comm MPI communicator for the calculations
82
     * @param phi_0 The initial set of features to combine
83
     * @param allowed_ops list of allowed operators
84
     * @param prop The property to be learned (training data)
85
86
     * @param task_sizes The number of samples per task
     * @param project_type The projection operator to use
87
88
     * @param max_phi highest rung value for the calculation
     * @param n_sis_select number of features to select during each SIS step
89
90
     * @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
91
     * @param cross_corr_max Maximum cross-correlation used for selecting features
92
     * @param min_abs_feat_val minimum absolute feature value
93
94
     * @param max_abs_feat_val maximum absolute feature value
     */
Thomas Purcell's avatar
Thomas Purcell committed
95
    FeatureSpace(
Thomas Purcell's avatar
Thomas Purcell committed
96
        std::shared_ptr<MPI_Interface> mpi_comm,
Thomas Purcell's avatar
Thomas Purcell committed
97
98
        std::vector<node_ptr> phi_0,
        std::vector<std::string> allowed_ops,
99
        std::vector<double> prop,
Thomas Purcell's avatar
Thomas Purcell committed
100
        std::vector<int> task_sizes,
101
        std::string project_type="pearson",
Thomas Purcell's avatar
Thomas Purcell committed
102
103
        int max_phi=1,
        int n_sis_select=1,
104
105
        int max_store_rung=-1,
        int n_rung_generate=0,
Thomas Purcell's avatar
Thomas Purcell committed
106
        double cross_corr_max=1.0,
107
108
109
110
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50
    );

111
112
113
114
115
    /**
     * @brief Initialize the feature set given a property vector
     *
     * @param prop The property trying to be learned
     */
116
    void initialize_fs(std::vector<double> prop, std::string project_type);
117

118
119
120
121
    /**
     * @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
     */
122
    void generate_feature_space(std::vector<double>& prop);
Thomas Purcell's avatar
Thomas Purcell committed
123

124
    /**
125
     * @brief The selected feature space
126
     */
127
    inline std::vector<node_ptr> phi_selected(){return _phi_selected;};
128
129

    /**
130
     * @brief The full feature space
131
     */
Thomas Purcell's avatar
Thomas Purcell committed
132
    inline std::vector<node_ptr> phi(){return _phi;};
133
134

    /**
135
     * @brief The initial feature space
136
     */
Thomas Purcell's avatar
Thomas Purcell committed
137
    inline std::vector<node_ptr> phi0(){return _phi_0;};
138
139

    /**
140
     * @brief The vector of projection scores for SIS
141
     */
142
143
    inline std::vector<double> scores(){return _scores;}

144
    /**
145
     * @brief The MPI Communicator
146
     */
Thomas Purcell's avatar
Thomas Purcell committed
147
    inline std::shared_ptr<MPI_Interface> mpi_comm(){return _mpi_comm;}
148

149
    /**
150
     * @brief The vector storing the number of samples in each task
151
     */
Thomas Purcell's avatar
Thomas Purcell committed
152
    inline std::vector<int> task_sizes(){return _task_sizes;}
153

154
    // DocString: feat_space_feature_space_file
155
    /**
156
     * @brief The feature space filename
157
     */
158
    inline std::string feature_space_file(){return _feature_space_file;}
159

160
    // DocString: feat_space_l_bound
161
    /**
162
     * @brief The minimum absolute value of the feature
163
     */
164
    inline double l_bound(){return _l_bound;}
165

166
    // DocString: feat_space_u_bound
167
    /**
168
     * @brief The maximum absolute value of the feature
169
     */
170
    inline double u_bound(){return _u_bound;}
171

172
    // DocString: feat_space_max_phi
173
    /**
174
     * @brief The maximum rung of the feature space
175
     */
176
    inline int max_phi(){return _max_phi;}
177

178
    // DocString: feat_space_n_sis_select
179
    /**
180
     * @brief The number of features selected in each SIS step
181
     */
182
    inline int n_sis_select(){return _n_sis_select;}
183

184
    // DocString: feat_space_n_samp
185
    /**
186
     * @brief The number of samples per feature
187
     */
188
    inline int n_samp(){return _n_samp;}
189

190
    // DocString: feat_space_n_feat
191
    /**
192
     * @brief The number of features in the feature space
193
     */
194
    inline int n_feat(){return _n_feat;}
195

196
    // DocString: feat_space_n_rung_store
197
    /**
198
     * @brief The number of rungs whose feature training data is stored in memory
199
     */
200
    inline int n_rung_store(){return _n_rung_store;}
201

202
    // DocString: feat_space_n_rung_generate
203
    /**
204
     * @brief The number of rungs to be generated on the fly during SIS
205
     */
206
    inline int n_rung_generate(){return _n_rung_generate;}
207

208
209
210
211
212
213
214
215
216
217
    /**
     * @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
     */
218
219
    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);

220
221
222
223
224
225
226
227
228
229
    /**
     * @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
     */
230
    void project_generated(double* prop, int size, std::vector<node_ptr>& phi_selected, std::vector<double>& scores_selected);
231

232
233
    /**
     * @brief Perform SIS on a feature set with a specified property
234
     * @details Perform sure-independence screening with either the correct property or the error
235
     *
236
     * @param prop The property to perform SIS over
237
     */
Thomas Purcell's avatar
Thomas Purcell committed
238
    void sis(std::vector<double>& prop);
239

240
    // DocString: feat_space_feat_in_phi
241
242
243
    /**
     * @brief Is a feature in this process' _phi?
     *
244
245
     * @param ind The index of the feature
     * @return True if feature is in this rank's _phi
246
247
248
     */
    inline bool feat_in_phi(int ind){return (ind >= _phi[0]->feat_ind()) && (ind <= _phi.back()->feat_ind());}

249
250
251
252
253
254
255
256
    // DocString: feat_space_remove_feature
    /**
     * @brief Remove a feature from phi
     *
     * @param ind index of feature to remove
     */
    void remove_feature(int ind);

257
258
259
    // Python Interface Functions
    #ifdef PY_BINDINGS
        /**
260
261
         * @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>)
262
         *
263
         * @param phi_0 The initial set of features to combine
264
         * @param allowed_ops list of allowed operators
265
         * @param prop The property to be learned (training data)
266
267
         * @param task_sizes The number of samples per task
         * @param project_type The projection operator to use
268
269
         * @param max_phi highest rung value for the calculation
         * @param n_sis_select number of features to select during each SIS step
270
271
         * @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
272
         * @param cross_corr_max Maximum cross-correlation used for selecting features
273
         * @param min_abs_feat_val minimum absolute feature value
274
275
276
277
278
279
280
         * @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,
Thomas Purcell's avatar
Thomas Purcell committed
281
            std::string project_type="regression",
282
283
284
285
            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
286
            double cross_corr_max=1.0,
287
288
289
290
291
            double min_abs_feat_val=1e-50,
            double max_abs_feat_val=1e50
        );

        /**
292
293
         * @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>)
294
         *
295
         * @param phi_0 The initial set of features to combine
296
         * @param allowed_ops list of allowed operators
297
         * @param prop The property to be learned (training data)
298
299
         * @param task_sizes The number of samples per task
         * @param project_type The projection operator to use
300
301
         * @param max_phi highest rung value for the calculation
         * @param n_sis_select number of features to select during each SIS step
302
303
         * @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
304
         * @param cross_corr_max Maximum cross-correlation used for selecting features
305
         * @param min_abs_feat_val minimum absolute feature value
306
307
308
309
310
311
312
         * @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,
Thomas Purcell's avatar
Thomas Purcell committed
313
            std::string project_type="regression",
314
315
316
317
            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
318
            double cross_corr_max=1.0,
319
320
321
322
            double min_abs_feat_val=1e-50,
            double max_abs_feat_val=1e50
        );

323
324
325
326
327
328
        /**
         * @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 file containing postfix expressions for the features (cpp definition in <python/feature_creation/FeatureSpace.cpp>)
         *
         * @param feature_file The file with the postfix expressions for the feature space
         * @param phi_0 The initial set of features to combine
329
330
         * @param task_sizes The number of samples per task
         * @param project_type The projection operator to use
331
332
333
334
335
336
337
         * @param n_sis_select number of features to select during each SIS step
         * @param cross_corr_max Maximum cross-correlation used for selecting features
         */
        FeatureSpace(
            std::string feature_file,
            py::list phi_0,
            py::list task_sizes,
338
            std::string project_type="pearson",
339
340
341
342
            int n_sis_select=1,
            double cross_corr_max=1.0
        );

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

        // DocString: feat_space_sis_list
356
357
358
        /**
         * @brief Wrapper function for SIS using a python list
         *
359
         * @param prop(list) The property to perform SIS over as a python list
360
         */
361
362
363
364
365
366
        inline void sis(py::list prop)
        {
            std::vector<double> prop_vec = python_conv_utils::from_list<double>(prop);
            sis(prop_vec);
        }

367
        // DocString: feat_space_phi_selected_py
368
        /**
369
         * @brief The selected feature space (cpp definition in <python/feature_creation/FeatureSpace.cpp>)
370
371
         * @return _phi_selected as a python list
         */
372
        py::list phi_selected_py();
373

374
        // DocString: feat_space_phi0_py
375
        /**
376
         * @brief The initial feature space (cpp definition in <python/feature_creation/FeatureSpace.cpp>)
377
378
         * @return _phi0 as a python list
         */
379
        py::list phi0_py();
380

381
382
383
384
385
386
387
        // DocString: feat_space_phi_py
        /**
         * @brief The feature space (cpp definition in <python/feature_creation/FeatureSpace.cpp>)
         * @return _phi as a python list
         */
        py::list phi_py();

388
        // DocString: feat_space_scores_py
389
        /**
390
         * @brief The vector of projection scores for SIS
391
392
         * @return _scores as a numpy array
         */
393
        inline np::ndarray scores_py(){return python_conv_utils::to_ndarray<double>(_scores);};
394

395
        // DocString: feat_space_task_sizes_py
396
        /**
397
         * @brief The vector storing the number of samples in each task
398
399
         * @return _task_sizes as a python list
         */
400
        inline py::list task_sizes_py(){return python_conv_utils::to_list<int>(_task_sizes);};
401

402
        // DocString: feat_space_allowed_ops_py
403
        /**
404
         * @brief The list of allowed operator nodes
405
406
         * @return _allowed_ops as a python list
         */
407
        inline py::list allowed_ops_py(){return python_conv_utils::to_list<std::string>(_allowed_ops);}
408

409
        // DocString: feat_space_start_gen_py
410
        /**
411
         * @brief The index in _phi where each generation starts
412
413
         * @return _start_gen as a python list
         */
414
        inline py::list start_gen_py(){return python_conv_utils::to_list<int>(_start_gen);}
415

416
417
418
419
420
421
422
423
        // DocString: feat_space_get_feature
        /**
         * @brief Return a feature at a specified index
         *
         * @param ind index of the feature to get
         * @return A ModelNode of the feature at index ind
         */
        inline ModelNode get_feature(int ind){return ModelNode(_phi[ind]->d_mat_ind(), _phi[ind]->rung(), _phi[ind]->expr(), _phi[ind]->postfix_expr(), _phi[ind]->value(), _phi[ind]->test_value(), _phi[ind]->unit());}
424
    #endif
Thomas Purcell's avatar
Thomas Purcell committed
425
426
427
};

#endif