FeatureSpace.hpp 27.5 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

13
#include <boost/filesystem.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
14

15
#include <utility>
Thomas Purcell's avatar
Thomas Purcell committed
16

Thomas Purcell's avatar
Thomas Purcell committed
17
#include "feature_creation/node/utils.hpp"
Thomas Purcell's avatar
Thomas Purcell committed
18
19
20
21
22

#include "mpi_interface/MPI_Interface.hpp"
#include "mpi_interface/MPI_ops.hpp"
#include "mpi_interface/serialize_tuple.h"

Thomas Purcell's avatar
Thomas Purcell committed
23
24
#include "utils/project.hpp"

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

30
// DocString: cls_feat_space
31
32
33
34
35
/**
 * @brief Feature Space for SISSO calculations
 * @details Stores and performs all feature calculations for SIS
 *
 */
Thomas Purcell's avatar
Thomas Purcell committed
36
37
class FeatureSpace
{
38
    std::vector<node_ptr> _phi_selected; //!< selected features
39
    std::vector<node_ptr> _phi; //!< all features
40
    const std::vector<node_ptr> _phi_0; //!< initial feature space
41

42
    #ifdef PARAMETERIZE
Thomas Purcell's avatar
Thomas Purcell committed
43
44
45
46
    std::vector<node_ptr> _phi_reparam; //!< The list of nodes used for reparameterization
    std::vector<int> _end_no_params; //!< The list of indexes of each rung where parameterized nodes start
    std::vector<int> _start_gen_reparam; //!< The list of indexes of each rung where parameterized nodes start

47
48
49
    std::vector<un_param_op_node_gen> _un_param_operators; //!< list of all parameterized unary operators with free parameters
    std::vector<bin_param_op_node_gen> _com_bin_param_operators; //!< list of all parameterized commutable binary operators with free parameters
    std::vector<bin_param_op_node_gen> _bin_param_operators; //!< list of all parameterized binary operators with free parameters
50
    std::vector<std::string> _allowed_param_ops; //!< Map of parameterization operator set (set of operators and non-linear parameters used for a non-linear least squares fit to property)
51
    #endif
52

53
54
55
56
57
    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

58
    std::vector<double> _prop; //!< The property to fit
59
60
    std::vector<double> _scores; //!< projection scores for each feature

61
    const std::vector<int> _task_sizes; //!< The number of elements in each task (training data)
62
    std::vector<int> _start_gen; //!< list of the indexes where each generation starts in _phi
Thomas Purcell's avatar
Thomas Purcell committed
63
    const std::string _project_type; //!< The type of projection that should be done during SIS
64
65
    const std::string _feature_space_file; //!< File to store information about the selected features
    const std::string _feature_space_summary_file; //!< File to store information about the selected features
66

67
68
    std::function<bool(const double*, const int, const double, const std::vector<double>&, const double, const int, const int)> _is_valid; //!< Function used to calculate the scores for SIS
    std::function<bool(const double*, const int, const double, const std::vector<node_ptr>&, const std::vector<double>&, const double)> _is_valid_feat_list; //!< Function used to calculate the scores for SIS without changing omp environment
69

70
    std::shared_ptr<MPI_Interface> _mpi_comm; //!< MPI communicator
71

72
73
74
    const double _cross_cor_max; //!< Maximum cross-correlation used for selecting features
    const double _l_bound; //!< lower bound for absolute value of the features
    const double _u_bound; //!< upper bound for absolute value of the features
75

Thomas Purcell's avatar
Thomas Purcell committed
76
    int _n_rung_store; //!< Total rungs stored
77
78
    int _n_feat; //!< Total number of features
    int _max_phi; //!< Maximum rung for the feature creation
79

80
81
82
    const int _n_sis_select; //!< Number of features to select for each dimensions
    const int _n_samp; //!< Number of samples (training data)
    const int _n_rung_generate; //!< Total number of rungs to generate on the fly
83

84
    int _max_param_depth; //!< Max depth to parameterize a feature (default=_max_rung)
85
    const bool _reparam_residual; //!< If True then reparameterize using the residuals of each model
86

Thomas Purcell's avatar
Thomas Purcell committed
87
public:
Thomas Purcell's avatar
Thomas Purcell committed
88

89
    #ifdef PARAMETERIZE
90
91
    /**
     * @brief Constructor for the feature space
92
     * @details constructs the feature space from an initial set of features and a list of allowed operators
93
94
     *
     * @param mpi_comm MPI communicator for the calculations
95
     * @param phi_0 The initial set of features to combine
96
     * @param allowed_ops list of allowed operators
97
     * @param allowed_param_ops dictionary of the parameterizable operators and their associated free parameters
98
     * @param prop The property to be learned (training data)
99
100
     * @param task_sizes The number of samples per task
     * @param project_type The projection operator to use
101
102
     * @param max_phi highest rung value for the calculation
     * @param n_sis_select number of features to select during each SIS step
103
104
     * @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
105
     * @param cross_corr_max Maximum cross-correlation used for selecting features
106
     * @param min_abs_feat_val minimum absolute feature value
107
     * @param max_abs_feat_val maximum absolute feature value
108
     * @param max_param_depth the maximum paremterization depths for features
109
     * @param reparam_residual If True then reparameterize using the residuals of each model
110
     */
Thomas Purcell's avatar
Thomas Purcell committed
111
    FeatureSpace(
Thomas Purcell's avatar
Thomas Purcell committed
112
        std::shared_ptr<MPI_Interface> mpi_comm,
Thomas Purcell's avatar
Thomas Purcell committed
113
114
        std::vector<node_ptr> phi_0,
        std::vector<std::string> allowed_ops,
Thomas Purcell's avatar
Thomas Purcell committed
115
        std::vector<std::string> allowed_param_ops,
116
        std::vector<double> prop,
Thomas Purcell's avatar
Thomas Purcell committed
117
        std::vector<int> task_sizes,
118
        std::string project_type="regression",
Thomas Purcell's avatar
Thomas Purcell committed
119
120
        int max_phi=1,
        int n_sis_select=1,
121
122
        int max_store_rung=-1,
        int n_rung_generate=0,
Thomas Purcell's avatar
Thomas Purcell committed
123
        double cross_corr_max=1.0,
124
        double min_abs_feat_val=1e-50,
125
        double max_abs_feat_val=1e50,
126
127
        int max_param_depth=-1,
        bool reparam_residual=false
128
    );
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    #else
    /**
     * @brief Constructor for the feature space
     * @details constructs the feature space from an initial set of features and a list of allowed operators
     *
     * @param mpi_comm MPI communicator for the calculations
     * @param phi_0 The initial set of features to combine
     * @param allowed_ops list of allowed operators
     * @param prop The property to be learned (training data)
     * @param task_sizes The number of samples per task
     * @param project_type The projection operator to use
     * @param max_phi highest rung value for the calculation
     * @param n_sis_select number of features to select during each SIS step
     * @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)
     * @param cross_corr_max Maximum cross-correlation used for selecting features
     * @param min_abs_feat_val minimum absolute feature value
     * @param max_abs_feat_val maximum absolute feature value
     */
    FeatureSpace(
        std::shared_ptr<MPI_Interface> mpi_comm,
        std::vector<node_ptr> phi_0,
        std::vector<std::string> allowed_ops,
        std::vector<double> prop,
        std::vector<int> task_sizes,
        std::string project_type="regression",
        int max_phi=1,
        int n_sis_select=1,
        int max_store_rung=-1,
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50
    );
    #endif
164
165
166
    /**
     * @brief Initialize the feature set given a property vector
     */
Thomas Purcell's avatar
Thomas Purcell committed
167
    void initialize_fs();
168

169
170
171
172
173
174
175
176
    /**
     * @brief Uses _allowed_ops to set the operator lists
     */
    void set_op_lists();

    /**
     * @brief Initializes the output files for SIS
     */
177
    void initialize_fs_output_files() const;
178
179
180
181
    /**
     * @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
     */
182
    void generate_feature_space();
Thomas Purcell's avatar
Thomas Purcell committed
183

184
    /**
185
     * @brief The selected feature space
186
     */
187
    inline std::vector<node_ptr> phi_selected() const {return _phi_selected;};
188
189

    /**
190
     * @brief The full feature space
191
     */
192
    inline std::vector<node_ptr> phi() const {return _phi;};
193
194

    /**
195
     * @brief The initial feature space
196
     */
197
    inline std::vector<node_ptr> phi0() const {return _phi_0;};
198
199

    /**
200
     * @brief The vector of projection scores for SIS
201
     */
202
    inline std::vector<double> scores() const {return _scores;}
203

204
    /**
205
     * @brief The MPI Communicator
206
     */
207
    inline std::shared_ptr<MPI_Interface> mpi_comm() const {return _mpi_comm;}
208

209
    /**
210
     * @brief The vector storing the number of samples in each task
211
     */
212
    inline std::vector<int> task_sizes() const {return _task_sizes;}
213

214
    // DocString: feat_space_feature_space_file
215
    /**
216
     * @brief The feature space filename
217
     */
218
    inline std::string feature_space_file() const {return _feature_space_file;}
219

220
    // DocString: feat_space_l_bound
221
    /**
222
     * @brief The minimum absolute value of the feature
223
     */
224
    inline double l_bound() const {return _l_bound;}
225

226
    // DocString: feat_space_u_bound
227
    /**
228
     * @brief The maximum absolute value of the feature
229
     */
230
    inline double u_bound() const {return _u_bound;}
231

232
    // DocString: feat_space_max_phi
233
    /**
234
     * @brief The maximum rung of the feature space
235
     */
236
    inline int max_phi() const {return _max_phi;}
237

238
    // DocString: feat_space_n_sis_select
239
    /**
240
     * @brief The number of features selected in each SIS step
241
     */
242
    inline int n_sis_select() const {return _n_sis_select;}
243

244
    // DocString: feat_space_n_samp
245
    /**
246
     * @brief The number of samples per feature
247
     */
248
    inline int n_samp() const {return _n_samp;}
249

250
    // DocString: feat_space_n_feat
251
    /**
252
     * @brief The number of features in the feature space
253
     */
254
    inline int n_feat() const {return _n_feat;}
255

256
    // DocString: feat_space_n_rung_store
257
    /**
258
     * @brief The number of rungs whose feature training data is stored in memory
259
     */
260
    inline int n_rung_store() const {return _n_rung_store;}
261

262
    // DocString: feat_space_n_rung_generate
263
    /**
264
     * @brief The number of rungs to be generated on the fly during SIS
265
     */
266
    inline int n_rung_generate() const {return _n_rung_generate;}
267

Thomas Purcell's avatar
Thomas Purcell committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    /**
     * @brief Generate a new set of non-parameterized 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 optimizer The object used to optimize the parameterized features
     * @param l_bound lower bound for the absolute value of the feature
     * @param u_bound upper bound for the abosulte value of the feature
     */
    void generate_non_param_feats(
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
282
        const std::vector<node_ptr>::iterator& start,
Thomas Purcell's avatar
Thomas Purcell committed
283
284
285
286
287
        unsigned long int& feat_ind,
        const double l_bound=1e-50,
        const double u_bound=1e50
    );

288
#ifdef PARAMETERIZE
289
    /**
Thomas Purcell's avatar
Thomas Purcell committed
290
     * @brief Generate a new set of parameterized features from a single feature
291
292
293
294
295
296
297
298
299
     * @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 optimizer The object used to optimize the parameterized features
     * @param l_bound lower bound for the absolute value of the feature
     * @param u_bound upper bound for the abosulte value of the feature
     */
Thomas Purcell's avatar
Thomas Purcell committed
300
    void generate_param_feats(
301
302
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
303
        const std::vector<node_ptr>::iterator& start,
304
305
        unsigned long int& feat_ind,
        std::shared_ptr<NLOptimizer> optimizer,
306
307
        const double l_bound=1e-50,
        const double u_bound=1e50
308
    );
Thomas Purcell's avatar
Thomas Purcell committed
309

310
    /**
Thomas Purcell's avatar
Thomas Purcell committed
311
     * @brief Generate a new set of parameterized features for the residuals
312
313
314
315
     *
     * @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
Thomas Purcell's avatar
Thomas Purcell committed
316
     * @param optimizer The object used to optimize the parameterized features
317
318
319
     * @param l_bound lower bound for the absolute value of the feature
     * @param u_bound upper bound for the abosulte value of the feature
     */
Thomas Purcell's avatar
Thomas Purcell committed
320
    void generate_reparam_feats(
321
322
323
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
        unsigned long int& feat_ind,
Thomas Purcell's avatar
Thomas Purcell committed
324
        std::shared_ptr<NLOptimizer> optimizer,
325
326
        const double l_bound=1e-50,
        const double u_bound=1e50
327
    );
Thomas Purcell's avatar
Thomas Purcell committed
328
329
330
331
332
333
334

    /**
     * @brief Generate reparameterized feature set
     *
     * @param prop The property to optimize against
     */
    void generate_reparam_feature_set(const std::vector<double>& prop);
335
#endif
336

337
338
339
340
341
342
343
344
345
    /**
     * @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
     */
Thomas Purcell's avatar
Thomas Purcell committed
346
    void project_generated(std::shared_ptr<LossFunction> loss, std::vector<node_ptr>& phi_selected, std::vector<double>& scores_selected);
347

348
349
    /**
     * @brief Perform SIS on a feature set with a specified property
350
     * @details Perform sure-independence screening with either the correct property or the error
351
     *
352
     * @param prop The property to perform SIS over
353
     */
354
    void sis(const std::vector<double>& prop);
355

Thomas Purcell's avatar
Thomas Purcell committed
356
357
358
359
360
361
362
363
    /**
     * @brief Perform SIS on a feature set with a specified loss function
     * @details Perform sure-independence screening with either the correct property or the error
     *
     * @param loss The LossFunction to project over
     */
    void sis(std::shared_ptr<LossFunction> loss);

364
    // DocString: feat_space_feat_in_phi
365
366
367
    /**
     * @brief Is a feature in this process' _phi?
     *
368
     * @param ind The index of the feature
Thomas Purcell's avatar
Thomas Purcell committed
369
     *
370
     * @return True if feature is in this rank's _phi
371
     */
372
    inline bool feat_in_phi(int ind) const {return (ind >= _phi[0]->feat_ind()) && (ind <= _phi.back()->feat_ind());}
373

374
375
376
377
    // DocString: feat_space_remove_feature
    /**
     * @brief Remove a feature from phi
     *
Thomas Purcell's avatar
Thomas Purcell committed
378
     * @param ind (int) index of feature to remove
379
     */
380
    void remove_feature(const int ind);
381

382
383
    // Python Interface Functions
    #ifdef PY_BINDINGS
384
    #ifdef PARAMETERIZE
Thomas Purcell's avatar
Thomas Purcell committed
385
386

    // DocString: feat_space_init_py_list
387
388
389
390
    /**
     * @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>)
     *
Thomas Purcell's avatar
Thomas Purcell committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
     * @param phi_0 (list) The initial set of features to combine
     * @param allowed_ops (list) list of allowed operators
     * @param allowed_param_ops (list) dictionary of the parameterizable operators and their associated free parameters
     * @param prop (list) The property to be learned (training data)
     * @param project_type (str) The projection operator to use
     * @param max_phi (int) highest rung value for the calculation
     * @param n_sis_select (int) number of features to select during each SIS step
     * @param max_store_rung (int) number of rungs to calculate and store the value of the features for all samples
     * @param n_rung_generate (int) 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)
     * @param cross_corr_max (double) Maximum cross-correlation used for selecting features
     * @param min_abs_feat_val (double) minimum absolute feature value
     * @param max_abs_feat_val (double) maximum absolute feature value
     * @param max_param_depth (int) the maximum paremterization depths for features
     * @param reparam_residual (bool) If True then reparameterize using the residuals of each model
405
406
407
408
409
410
411
412
413
414
415
416
417
418
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        py::list allowed_param_ops,
        py::list prop,
        std::string project_type="regression",
        int max_phi=1,
        int n_sis_select=1,
        int max_store_rung=-1,
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50,
419
420
        int max_param_depth = -1,
        bool reparam_residual=false
421
422
    );

Thomas Purcell's avatar
Thomas Purcell committed
423
    // DocString: feat_space_init_np_array
424
425
426
427
    /**
     * @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>)
     *
Thomas Purcell's avatar
Thomas Purcell committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
     * @param phi_0 (list) The initial set of features to combine
     * @param allowed_ops (list) list of allowed operators
     * @param allowed_param_ops (list) dictionary of the parameterizable operators and their associated free parameters
     * @param prop (np.ndarray) The property to be learned (training data)
     * @param project_type (str) The projection operator to use
     * @param max_phi (int) highest rung value for the calculation
     * @param n_sis_select (int) number of features to select during each SIS step
     * @param max_store_rung (int) number of rungs to calculate and store the value of the features for all samples
     * @param n_rung_generate (int) 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)
     * @param cross_corr_max (double) Maximum cross-correlation used for selecting features
     * @param min_abs_feat_val (double) minimum absolute feature value
     * @param max_abs_feat_val (double) maximum absolute feature value
     * @param max_param_depth (int) the maximum paremterization depths for features
     * @param reparam_residual (bool) If True then reparameterize using the residuals of each model
442
443
444
445
446
447
448
449
450
451
452
453
454
455
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        py::list allowed_param_ops,
        np::ndarray prop,
        std::string project_type="regression",
        int max_phi=1,
        int n_sis_select=1,
        int max_store_rung=-1,
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50,
456
457
        int max_param_depth = -1,
        bool reparam_residual=false
458
    );
Thomas Purcell's avatar
Thomas Purcell committed
459

460
    #else
Thomas Purcell's avatar
Thomas Purcell committed
461
462

    // DocString: feat_space_ini_no_param_py_list
463
464
465
466
    /**
     * @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>)
     *
Thomas Purcell's avatar
Thomas Purcell committed
467
468
469
470
471
472
473
474
475
476
477
     * @param phi_0 (list) The initial set of features to combine
     * @param allowed_ops (list) list of allowed operators
     * @param prop (list) The property to be learned (training data)
     * @param project_type (str) The projection operator to use
     * @param max_phi (int) highest rung value for the calculation
     * @param n_sis_select (int) number of features to select during each SIS step
     * @param max_store_rung (int) number of rungs to calculate and store the value of the features for all samples
     * @param n_rung_generate (int) 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)
     * @param cross_corr_max (double) Maximum cross-correlation used for selecting features
     * @param min_abs_feat_val (double) minimum absolute feature value
     * @param max_abs_feat_val (double) maximum absolute feature value
478
479
480
481
482
483
484
485
486
487
488
489
490
491
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        py::list prop,
        std::string project_type="regression",
        int max_phi=1,
        int n_sis_select=1,
        int max_store_rung=-1,
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50
    );
492

Thomas Purcell's avatar
Thomas Purcell committed
493
    // DocString: feat_space_init_no_param_np_array
494
495
496
497
    /**
     * @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>)
     *
Thomas Purcell's avatar
Thomas Purcell committed
498
499
500
501
502
503
504
505
506
507
508
     * @param phi_0 (list) The initial set of features to combine
     * @param allowed_ops (list) list of allowed operators
     * @param prop (np.ndarray) The property to be learned (training data)
     * @param project_type (str) The projection operator to use
     * @param max_phi (int) highest rung value for the calculation
     * @param n_sis_select (int) number of features to select during each SIS step
     * @param max_store_rung (int) number of rungs to calculate and store the value of the features for all samples
     * @param n_rung_generate (int) 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)
     * @param cross_corr_max (double) Maximum cross-correlation used for selecting features
     * @param min_abs_feat_val (double) minimum absolute feature value
     * @param max_abs_feat_val (double) maximum absolute feature value
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        np::ndarray prop,
        std::string project_type="regression",
        int max_phi=1,
        int n_sis_select=1,
        int max_store_rung=-1,
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50
    );
    #endif
Thomas Purcell's avatar
Thomas Purcell committed
524
525

    // DocString: feat_space_init_file_py_list
526
527
528
529
    /**
     * @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>)
     *
Thomas Purcell's avatar
Thomas Purcell committed
530
531
532
533
534
535
536
     * @param feature_file (str) The file with the postfix expressions for the feature space
     * @param phi_0 (list) The initial set of features to combine
     * @param prop (np.ndarray) The property to be learned (training data)
     * @param task_sizes (list) The number of samples per task
     * @param project_type (str) The projection operator to use
     * @param n_sis_select (int) number of features to select during each SIS step
     * @param cross_corr_max (double) Maximum cross-correlation used for selecting features
537
538
539
540
541
542
     */
    FeatureSpace(
        std::string feature_file,
        py::list phi_0,
        np::ndarray prop,
        py::list task_sizes,
Thomas Purcell's avatar
Thomas Purcell committed
543
        std::string project_type="regression",
544
545
546
547
        int n_sis_select=1,
        double cross_corr_max=1.0
    );

Thomas Purcell's avatar
Thomas Purcell committed
548
    // DocString: feat_space_init_file_np_array
549
550
551
552
    /**
     * @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>)
     *
Thomas Purcell's avatar
Thomas Purcell committed
553
554
555
556
557
558
559
     * @param feature_file (str) The file with the postfix expressions for the feature space
     * @param phi_0 (list) The initial set of features to combine
     * @param prop (list) The property to be learned (training data)
     * @param task_sizes (list) The number of samples per task
     * @param project_type (str) The projection operator to use
     * @param n_sis_select (int) number of features to select during each SIS step
     * @param cross_corr_max (double) Maximum cross-correlation used for selecting features
560
561
562
563
564
565
     */
    FeatureSpace(
        std::string feature_file,
        py::list phi_0,
        py::list prop,
        py::list task_sizes,
Thomas Purcell's avatar
Thomas Purcell committed
566
        std::string project_type="regression",
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
        int n_sis_select=1,
        double cross_corr_max=1.0
    );

    // DocString: feat_space_sis_arr
    /**
     * @brief Wrapper function for SIS using a numpy array
     *
     * @param prop(np.ndarray) The property to perform SIS over as a numpy array
     */
    inline void sis(np::ndarray prop)
    {
        std::vector<double> prop_vec = python_conv_utils::from_ndarray<double>(prop);
        sis(prop_vec);
    }

    // DocString: feat_space_sis_list
    /**
     * @brief Wrapper function for SIS using a python list
     *
     * @param prop(list) The property to perform SIS over as a python list
     */
    inline void sis(py::list prop)
    {
        std::vector<double> prop_vec = python_conv_utils::from_list<double>(prop);
        sis(prop_vec);
    }

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

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

    // 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();

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

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

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

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

    // 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
     */
651
    inline ModelNode get_feature(const int ind) const {return ModelNode(_phi[ind]);}
652
    #endif
Thomas Purcell's avatar
Thomas Purcell committed
653
654
};

655
#endif