FeatureSpace.hpp 27.2 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 <boost/serialization/shared_ptr.hpp>
14
#include <boost/filesystem.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
15

Thomas Purcell's avatar
Thomas Purcell committed
16
#include <iostream>
Thomas Purcell's avatar
Thomas Purcell committed
17
#include <iomanip>
18
#include <utility>
Thomas Purcell's avatar
Thomas Purcell committed
19

Thomas Purcell's avatar
Thomas Purcell committed
20
21
22
23
24
25
26
27
28
29
#include "mpi_interface/MPI_Interface.hpp"
#include "mpi_interface/MPI_ops.hpp"
#include "mpi_interface/serialize_tuple.h"
#include "feature_creation/node/ModelNode.hpp"
#include "feature_creation/node/operator_nodes/allowed_ops.hpp"
#include "feature_creation/node/utils.hpp"
#include "feature_creation/node/value_storage/nodes_value_containers.hpp"
#include "utils/compare_features.hpp"
#include "utils/project.hpp"

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

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

47
    #ifdef PARAMETERIZE
Thomas Purcell's avatar
Thomas Purcell committed
48
49
50
51
    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

52
53
54
    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
55
    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)
56
    #endif
57

58
59
60
61
62
    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

63
    std::vector<double> _prop; //!< The property to fit
64
65
    std::vector<double> _scores; //!< projection scores for each feature

66
    const std::vector<int> _task_sizes; //!< The number of elements in each task (training data)
67
    std::vector<int> _start_gen; //!< list of the indexes where each generation starts in _phi
Thomas Purcell's avatar
Thomas Purcell committed
68
    const std::string _project_type; //!< The type of projection that should be done during SIS
69
70
    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
71

72
73
74
75
    std::function<void(const double*, double*, const std::vector<node_ptr>&, const std::vector<int>&, const int)> _project; //!< Function used to calculate the scores for SIS
    std::function<void(const double*, double*, const std::vector<node_ptr>&, const std::vector<int>&, const int)> _project_no_omp; //!< Function used to calculate the scores for SIS without changing omp environment
    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
76

77
    std::shared_ptr<MPI_Interface> _mpi_comm; //!< MPI communicator
78

79
80
81
    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
82

Thomas Purcell's avatar
Thomas Purcell committed
83
    int _n_rung_store; //!< Total rungs stored
84
85
    int _n_feat; //!< Total number of features
    int _max_phi; //!< Maximum rung for the feature creation
86

87
88
89
    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
90

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

Thomas Purcell's avatar
Thomas Purcell committed
94
public:
Thomas Purcell's avatar
Thomas Purcell committed
95

96
    #ifdef PARAMETERIZE
97
98
    /**
     * @brief Constructor for the feature space
99
     * @details constructs the feature space from an initial set of features and a list of allowed operators
100
101
     *
     * @param mpi_comm MPI communicator for the calculations
102
     * @param phi_0 The initial set of features to combine
103
     * @param allowed_ops list of allowed operators
104
     * @param allowed_param_ops dictionary of the parameterizable operators and their associated free parameters
105
     * @param prop The property to be learned (training data)
106
107
     * @param task_sizes The number of samples per task
     * @param project_type The projection operator to use
108
109
     * @param max_phi highest rung value for the calculation
     * @param n_sis_select number of features to select during each SIS step
110
111
     * @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
112
     * @param cross_corr_max Maximum cross-correlation used for selecting features
113
     * @param min_abs_feat_val minimum absolute feature value
114
     * @param max_abs_feat_val maximum absolute feature value
115
     * @param max_param_depth the maximum paremterization depths for features
116
     * @param reparam_residual If True then reparameterize using the residuals of each model
117
     */
Thomas Purcell's avatar
Thomas Purcell committed
118
    FeatureSpace(
Thomas Purcell's avatar
Thomas Purcell committed
119
        std::shared_ptr<MPI_Interface> mpi_comm,
Thomas Purcell's avatar
Thomas Purcell committed
120
121
        std::vector<node_ptr> phi_0,
        std::vector<std::string> allowed_ops,
Thomas Purcell's avatar
Thomas Purcell committed
122
        std::vector<std::string> allowed_param_ops,
123
        std::vector<double> prop,
Thomas Purcell's avatar
Thomas Purcell committed
124
        std::vector<int> task_sizes,
125
        std::string project_type="regression",
Thomas Purcell's avatar
Thomas Purcell committed
126
127
        int max_phi=1,
        int n_sis_select=1,
128
129
        int max_store_rung=-1,
        int n_rung_generate=0,
Thomas Purcell's avatar
Thomas Purcell committed
130
        double cross_corr_max=1.0,
131
        double min_abs_feat_val=1e-50,
132
        double max_abs_feat_val=1e50,
133
134
        int max_param_depth=-1,
        bool reparam_residual=false
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
164
165
166
167
168
169
170
    #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
171
172
173
    /**
     * @brief Initialize the feature set given a property vector
     */
Thomas Purcell's avatar
Thomas Purcell committed
174
    void initialize_fs();
175

176
177
178
179
180
181
182
183
    /**
     * @brief Uses _allowed_ops to set the operator lists
     */
    void set_op_lists();

    /**
     * @brief Initializes the output files for SIS
     */
184
    void initialize_fs_output_files() const;
185
186
187
188
    /**
     * @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
     */
189
    void generate_feature_space();
Thomas Purcell's avatar
Thomas Purcell committed
190

191
    /**
192
     * @brief The selected feature space
193
     */
194
    inline std::vector<node_ptr> phi_selected() const {return _phi_selected;};
195
196

    /**
197
     * @brief The full feature space
198
     */
199
    inline std::vector<node_ptr> phi() const {return _phi;};
200
201

    /**
202
     * @brief The initial feature space
203
     */
204
    inline std::vector<node_ptr> phi0() const {return _phi_0;};
205
206

    /**
207
     * @brief The vector of projection scores for SIS
208
     */
209
    inline std::vector<double> scores() const {return _scores;}
210

211
    /**
212
     * @brief The MPI Communicator
213
     */
214
    inline std::shared_ptr<MPI_Interface> mpi_comm() const {return _mpi_comm;}
215

216
    /**
217
     * @brief The vector storing the number of samples in each task
218
     */
219
    inline std::vector<int> task_sizes() const {return _task_sizes;}
220

221
    // DocString: feat_space_feature_space_file
222
    /**
223
     * @brief The feature space filename
224
     */
225
    inline std::string feature_space_file() const {return _feature_space_file;}
226

227
    // DocString: feat_space_l_bound
228
    /**
229
     * @brief The minimum absolute value of the feature
230
     */
231
    inline double l_bound() const {return _l_bound;}
232

233
    // DocString: feat_space_u_bound
234
    /**
235
     * @brief The maximum absolute value of the feature
236
     */
237
    inline double u_bound() const {return _u_bound;}
238

239
    // DocString: feat_space_max_phi
240
    /**
241
     * @brief The maximum rung of the feature space
242
     */
243
    inline int max_phi() const {return _max_phi;}
244

245
    // DocString: feat_space_n_sis_select
246
    /**
247
     * @brief The number of features selected in each SIS step
248
     */
249
    inline int n_sis_select() const {return _n_sis_select;}
250

251
    // DocString: feat_space_n_samp
252
    /**
253
     * @brief The number of samples per feature
254
     */
255
    inline int n_samp() const {return _n_samp;}
256

257
    // DocString: feat_space_n_feat
258
    /**
259
     * @brief The number of features in the feature space
260
     */
261
    inline int n_feat() const {return _n_feat;}
262

263
    // DocString: feat_space_n_rung_store
264
    /**
265
     * @brief The number of rungs whose feature training data is stored in memory
266
     */
267
    inline int n_rung_store() const {return _n_rung_store;}
268

269
    // DocString: feat_space_n_rung_generate
270
    /**
271
     * @brief The number of rungs to be generated on the fly during SIS
272
     */
273
    inline int n_rung_generate() const {return _n_rung_generate;}
274

Thomas Purcell's avatar
Thomas Purcell committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
    /**
     * @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,
        unsigned long int& feat_ind,
        const double l_bound=1e-50,
        const double u_bound=1e50
    );

Thomas Purcell's avatar
Thomas Purcell committed
294
    #ifdef PARAMETERIZE
295
    /**
Thomas Purcell's avatar
Thomas Purcell committed
296
     * @brief Generate a new set of parameterized features from a single feature
297
298
299
300
301
302
303
304
305
     * @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
306
    void generate_param_feats(
307
308
309
310
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
        unsigned long int& feat_ind,
        std::shared_ptr<NLOptimizer> optimizer,
311
312
        const double l_bound=1e-50,
        const double u_bound=1e50
313
    );
Thomas Purcell's avatar
Thomas Purcell committed
314

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

    /**
     * @brief Generate reparameterized feature set
     *
     * @param prop The property to optimize against
     */
    void generate_reparam_feature_set(const std::vector<double>& prop);
Thomas Purcell's avatar
Thomas Purcell committed
340
    #endif
341

342
343
344
345
346
347
348
349
350
    /**
     * @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
     */
351
    void project_generated(const double* prop, const int size, std::vector<node_ptr>& phi_selected, std::vector<double>& scores_selected);
352

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

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

370
371
372
373
374
375
    // DocString: feat_space_remove_feature
    /**
     * @brief Remove a feature from phi
     *
     * @param ind index of feature to remove
     */
376
    void remove_feature(const int ind);
377

378
379
    // Python Interface Functions
    #ifdef PY_BINDINGS
380
    #ifdef PARAMETERIZE
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
    /**
     * @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>)
     *
     * @param phi_0 The initial set of features to combine
     * @param allowed_ops list of allowed operators
     * @param allowed_param_ops dictionary of the parameterizable operators and their associated free parameters
     * @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
398
     * @param max_param_depth the maximum paremterization depths for features
399
     * @param reparam_residual If True then reparameterize using the residuals of each model
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        py::list allowed_param_ops,
        py::list prop,
        py::list 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,
415
416
        int max_param_depth = -1,
        bool reparam_residual=false
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
    );

    /**
     * @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>)
     *
     * @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
435
     * @param reparam_residual If True then reparameterize using the residuals of each model
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        py::list allowed_param_ops,
        np::ndarray prop,
        py::list 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,
451
452
        int max_param_depth = -1,
        bool reparam_residual=false
453
    );
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
    #else
    /**
     * @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>)
     *
     * @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(
        py::list phi_0,
        py::list allowed_ops,
        py::list prop,
        py::list 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
    );
486

487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
    /**
     * @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>)
     *
     * @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(
        py::list phi_0,
        py::list allowed_ops,
        np::ndarray prop,
        py::list 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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
    /**
     * @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
     * @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 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,
        np::ndarray prop,
        py::list task_sizes,
Thomas Purcell's avatar
Thomas Purcell committed
536
        std::string project_type="regression",
537
538
539
540
541
542
543
544
545
546
        int n_sis_select=1,
        double cross_corr_max=1.0
    );

    /**
     * @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
Thomas Purcell's avatar
Thomas Purcell committed
547
     * @param prop The property to be learned (training data)
548
549
550
551
552
553
554
555
556
557
     * @param task_sizes The number of samples per task
     * @param project_type The projection operator to use
     * @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 prop,
        py::list task_sizes,
Thomas Purcell's avatar
Thomas Purcell committed
558
        std::string project_type="regression",
559
560
561
562
563
564
565
566
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
        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
     */
643
    inline ModelNode get_feature(const int ind) const {return ModelNode(_phi[ind]);}
644
    #endif
Thomas Purcell's avatar
Thomas Purcell committed
645
646
};

647
#endif