FeatureSpace.hpp 27.7 KB
Newer Older
1
// Copyright 2021 Thomas A. R. Purcell
2
//
3
4
5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
10
11
12
13
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
14
15
16
17
18
19
20
21
22
/** @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
23
24
25
#ifndef FEATURE_SPACE
#define FEATURE_SPACE

26
#include <boost/filesystem.hpp>
Thomas Purcell's avatar
Thomas Purcell committed
27

28
#include <utility>
Thomas Purcell's avatar
Thomas Purcell committed
29

Thomas Purcell's avatar
Thomas Purcell committed
30
#include "feature_creation/node/utils.hpp"
Thomas Purcell's avatar
Thomas Purcell committed
31
32
33
34
35

#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
36
37
#include "utils/project.hpp"

38
39
40
41
#ifdef PY_BINDINGS
    namespace np = boost::python::numpy;
    namespace py = boost::python;
#endif
42

43
// DocString: cls_feat_space
44
45
46
47
48
/**
 * @brief Feature Space for SISSO calculations
 * @details Stores and performs all feature calculations for SIS
 *
 */
Thomas Purcell's avatar
Thomas Purcell committed
49
50
class FeatureSpace
{
51
    std::vector<node_ptr> _phi_selected; //!< selected features
52
    std::vector<node_ptr> _phi; //!< all features
53
    const std::vector<node_ptr> _phi_0; //!< initial feature space
54

55
    #ifdef PARAMETERIZE
Thomas Purcell's avatar
Thomas Purcell committed
56
57
58
59
    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

60
61
62
    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
63
    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)
64
    #endif
65

66
67
68
69
70
    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

71
    std::vector<double> _prop; //!< The property to fit
72
73
    std::vector<double> _scores; //!< projection scores for each feature

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

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

83
    std::shared_ptr<MPI_Interface> _mpi_comm; //!< MPI communicator
84

85
86
87
    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
88

Thomas Purcell's avatar
Thomas Purcell committed
89
    int _n_rung_store; //!< Total rungs stored
90
91
    int _n_feat; //!< Total number of features
    int _max_phi; //!< Maximum rung for the feature creation
92

93
94
95
    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
96

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

Thomas Purcell's avatar
Thomas Purcell committed
100
public:
Thomas Purcell's avatar
Thomas Purcell committed
101

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

182
183
184
185
186
187
188
189
    /**
     * @brief Uses _allowed_ops to set the operator lists
     */
    void set_op_lists();

    /**
     * @brief Initializes the output files for SIS
     */
190
    void initialize_fs_output_files() const;
191
192
193
194
    /**
     * @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
     */
195
    void generate_feature_space();
Thomas Purcell's avatar
Thomas Purcell committed
196

197
    /**
198
     * @brief The selected feature space
199
     */
200
    inline std::vector<node_ptr> phi_selected() const {return _phi_selected;};
201
202

    /**
203
     * @brief The full feature space
204
     */
205
    inline std::vector<node_ptr> phi() const {return _phi;};
206
207

    /**
208
     * @brief The initial feature space
209
     */
210
    inline std::vector<node_ptr> phi0() const {return _phi_0;};
211
212

    /**
213
     * @brief The vector of projection scores for SIS
214
     */
215
    inline std::vector<double> scores() const {return _scores;}
216

217
    /**
218
     * @brief The MPI Communicator
219
     */
220
    inline std::shared_ptr<MPI_Interface> mpi_comm() const {return _mpi_comm;}
221

222
    /**
223
     * @brief The vector storing the number of samples in each task
224
     */
225
    inline std::vector<int> task_sizes() const {return _task_sizes;}
226

227
    // DocString: feat_space_feature_space_file
228
    /**
229
     * @brief The feature space filename
230
     */
231
    inline std::string feature_space_file() const {return _feature_space_file;}
232

233
    // DocString: feat_space_l_bound
234
    /**
235
     * @brief The minimum absolute value of the feature
236
     */
237
    inline double l_bound() const {return _l_bound;}
238

239
    // DocString: feat_space_u_bound
240
    /**
241
     * @brief The maximum absolute value of the feature
242
     */
243
    inline double u_bound() const {return _u_bound;}
244

245
    // DocString: feat_space_max_phi
246
    /**
247
     * @brief The maximum rung of the feature space
248
     */
249
    inline int max_phi() const {return _max_phi;}
250

251
    // DocString: feat_space_n_sis_select
252
    /**
253
     * @brief The number of features selected in each SIS step
254
     */
255
    inline int n_sis_select() const {return _n_sis_select;}
256

257
    // DocString: feat_space_n_samp
258
    /**
259
     * @brief The number of samples per feature
260
     */
261
    inline int n_samp() const {return _n_samp;}
262

263
    // DocString: feat_space_n_feat
264
    /**
265
     * @brief The number of features in the feature space
266
     */
267
    inline int n_feat() const {return _n_feat;}
268

269
    // DocString: feat_space_n_rung_store
270
    /**
271
     * @brief The number of rungs whose feature's data is always stored in memory
272
     */
273
    inline int n_rung_store() const {return _n_rung_store;}
274

275
    // DocString: feat_space_n_rung_generate
276
    /**
277
     * @brief Either 0 or 1, and is the number of rungs to generate on the fly during SIS
278
     */
279
    inline int n_rung_generate() const {return _n_rung_generate;}
280

Thomas Purcell's avatar
Thomas Purcell committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    /**
     * @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,
295
        const std::vector<node_ptr>::iterator& start,
Thomas Purcell's avatar
Thomas Purcell committed
296
297
298
299
300
        unsigned long int& feat_ind,
        const double l_bound=1e-50,
        const double u_bound=1e50
    );

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

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

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

350
351
352
353
354
355
356
357
358
    /**
     * @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
359
    void project_generated(std::shared_ptr<LossFunction> loss, std::vector<node_ptr>& phi_selected, std::vector<double>& scores_selected);
360

361
    /**
362
     * @brief Perform Sure-Independence Screening over the FeatureSpace. The features are ranked using a projection operator constructed using _project_type and the Property vector
363
     *
364
     * @param prop Vector containing the property vector (training data only)
365
     */
366
    void sis(const std::vector<double>& prop);
367

Thomas Purcell's avatar
Thomas Purcell committed
368
    /**
369
     * @brief Perform Sure-Independence Screening over the FeatureSpace. The features are ranked using a projection operator defined in loss
Thomas Purcell's avatar
Thomas Purcell committed
370
     *
371
     * @param loss The LossFunction used to project over all of the features
Thomas Purcell's avatar
Thomas Purcell committed
372
373
374
     */
    void sis(std::shared_ptr<LossFunction> loss);

375
    // DocString: feat_space_feat_in_phi
376
377
378
    /**
     * @brief Is a feature in this process' _phi?
     *
379
     * @param ind (int) The index of the feature
Thomas Purcell's avatar
Thomas Purcell committed
380
     *
381
     * @return True if feature is in this rank's _phi
382
     */
383
    inline bool feat_in_phi(int ind) const {return (ind >= _phi[0]->feat_ind()) && (ind <= _phi.back()->feat_ind());}
384

385
386
387
388
    // DocString: feat_space_remove_feature
    /**
     * @brief Remove a feature from phi
     *
Thomas Purcell's avatar
Thomas Purcell committed
389
     * @param ind (int) index of feature to remove
390
     */
391
    void remove_feature(const int ind);
392

393
394
    // Python Interface Functions
    #ifdef PY_BINDINGS
395
    #ifdef PARAMETERIZE
Thomas Purcell's avatar
Thomas Purcell committed
396
397

    // DocString: feat_space_init_py_list
398
    /**
399
     * @brief FeatureSpace constructor given a set of primary features and operators
400
     *
401
402
403
404
405
406
407
408
409
410
411
412
413
414
     * @param phi_0 (list) The set of primary features
     * @param allowed_ops (list) The list of allowed operators
     * @param allowed_param_ops (list) The list of allowed operators to be used with non-linear optimization
     * @param prop (list) List containing the property vector (training data only)
     * @param project_type (str) The type of loss function/projection operator to use
     * @param max_phi (int) The maximum rung of the feature (Height of the binary expression tree -1)
     * @param n_sis_select (int) The number of features to select during each SIS step
     * @param max_store_rung (int) The number of rungs whose feature's data is always stored in memory
     * @param n_rung_generate (int) Either 0 or 1, and is the number of rungs to generate on the fly during SIS
     * @param cross_corr_max (double) The maximum allowed cross-correlation value between selected features
     * @param min_abs_feat_val (double) The minimum allowed absolute feature value for a feature
     * @param max_abs_feat_val (double) The maximum allowed absolute feature value for a feature
     * @param max_param_depth (int) The maximum depth in the binary expression tree to set non-linear optimization
     * @param reparam_residual (bool) If True then reparameterize features using the residuals of each model
415
416
417
418
419
420
421
422
423
424
425
426
427
428
     */
    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,
429
430
        int max_param_depth = -1,
        bool reparam_residual=false
431
432
    );

Thomas Purcell's avatar
Thomas Purcell committed
433
    // DocString: feat_space_init_np_array
434
    /**
435
     * @brief FeatureSpace constructor given a set of primary features and operators
436
     *
437
438
439
440
441
442
443
444
445
446
447
448
449
450
     * @param phi_0 (list) The set of primary features
     * @param allowed_ops (list) The list of allowed operators
     * @param allowed_param_ops (list) The list of allowed operators to be used with non-linear optimization
     * @param prop (np.ndarray) List containing the property vector (training data only)
     * @param project_type (str) The type of loss function/projection operator to use
     * @param max_phi (int) The maximum rung of the feature (Height of the binary expression tree -1)
     * @param n_sis_select (int) The number of features to select during each SIS step
     * @param max_store_rung (int) The number of rungs whose feature's data is always stored in memory
     * @param n_rung_generate (int) Either 0 or 1, and is the number of rungs to generate on the fly during SIS
     * @param cross_corr_max (double) The maximum allowed cross-correlation value between selected features
     * @param min_abs_feat_val (double) The minimum allowed absolute feature value for a feature
     * @param max_abs_feat_val (double) The maximum allowed absolute feature value for a feature
     * @param max_param_depth (int) The maximum depth in the binary expression tree to set non-linear optimization
     * @param reparam_residual (bool) If True then reparameterize features using the residuals of each model
451
452
453
454
455
456
457
458
459
460
461
462
463
464
     */
    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,
465
466
        int max_param_depth = -1,
        bool reparam_residual=false
467
    );
Thomas Purcell's avatar
Thomas Purcell committed
468

469
    #else
Thomas Purcell's avatar
Thomas Purcell committed
470
471

    // DocString: feat_space_ini_no_param_py_list
472
    /**
473
     * @brief FeatureSpace constructor given a set of primary features and operators
474
     *
475
476
477
478
479
480
481
482
483
484
485
     * @param phi_0 (list) The set of primary features
     * @param allowed_ops (list) The list of allowed operators
     * @param prop (list) List containing the property vector (training data only)
     * @param project_type (str) The type of loss function/projection operator to use
     * @param max_phi (int) The maximum rung of the feature (Height of the binary expression tree -1)
     * @param n_sis_select (int) The number of features to select during each SIS step
     * @param max_store_rung (int) The number of rungs whose feature's data is always stored in memory
     * @param n_rung_generate (int) Either 0 or 1, and is the number of rungs to generate on the fly during SIS
     * @param cross_corr_max (double) The maximum allowed cross-correlation value between selected features
     * @param min_abs_feat_val (double) The minimum allowed absolute feature value for a feature
     * @param max_abs_feat_val (double) The maximum allowed absolute feature value for a feature
486
487
488
489
490
491
492
493
494
495
496
497
498
499
     */
    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
    );
500

Thomas Purcell's avatar
Thomas Purcell committed
501
    // DocString: feat_space_init_no_param_np_array
502
    /**
503
     * @brief FeatureSpace constructor given a set of primary features and operators
504
     *
505
506
507
508
509
510
511
512
513
514
515
     * @param phi_0 (list) The set of primary features
     * @param allowed_ops (list) The list of allowed operators
     * @param prop (np.ndarray) List containing the property vector (training data only)
     * @param project_type (str) The type of loss function/projection operator to use
     * @param max_phi (int) The maximum rung of the feature (Height of the binary expression tree -1)
     * @param n_sis_select (int) The number of features to select during each SIS step
     * @param max_store_rung (int) The number of rungs whose feature's data is always stored in memory
     * @param n_rung_generate (int) Either 0 or 1, and is the number of rungs to generate on the fly during SIS
     * @param cross_corr_max (double) The maximum allowed cross-correlation value between selected features
     * @param min_abs_feat_val (double) The minimum allowed absolute feature value for a feature
     * @param max_abs_feat_val (double) The maximum allowed absolute feature value for a feature
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
     */
    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
531

532
    // DocString: feat_space_init_file_np_array
533
    /**
534
     * @brief FeatureSpace constructor that uses a file containing postfix feature expressions to describe all features in Phi, and a primary feature setn <python/feature_creation/FeatureSpace.cpp>)
535
     *
536
537
538
539
540
541
542
     * @param feature_file (str) The file containing the postfix expressions of all features in the FeatureSpace
     * @param phi_0 (list) The set of primary features
     * @param prop (np.ndarray) List containing the property vector (training data only)
     * @param task_sizes (list) The number of samples in the training data per task
     * @param project_type (str) The type of loss function/projection operator to use
     * @param n_sis_select (int) The number of features to select during each SIS step
     * @param cross_corr_max (double) The maximum allowed cross-correlation value between selected features
543
544
545
546
547
548
     */
    FeatureSpace(
        std::string feature_file,
        py::list phi_0,
        np::ndarray prop,
        py::list task_sizes,
Thomas Purcell's avatar
Thomas Purcell committed
549
        std::string project_type="regression",
550
551
552
553
        int n_sis_select=1,
        double cross_corr_max=1.0
    );

554
    // DocString: feat_space_init_file_py_list
555
    /**
556
     * @brief FeatureSpace constructor that uses a file containing postfix feature expressions to describe all features in Phi, and a primary feature setn <python/feature_creation/FeatureSpace.cpp>)
557
     *
558
559
560
561
562
563
564
     * @param feature_file (str) The file containing the postfix expressions of all features in the FeatureSpace
     * @param phi_0 (list) The set of primary features
     * @param prop (list) List containing the property vector (training data only)
     * @param task_sizes (list) The number of samples in the training data per task
     * @param project_type (str) The type of loss function/projection operator to use
     * @param n_sis_select (int) The number of features to select during each SIS step
     * @param cross_corr_max (double) The maximum allowed cross-correlation value between selected features
565
566
567
568
569
570
     */
    FeatureSpace(
        std::string feature_file,
        py::list phi_0,
        py::list prop,
        py::list task_sizes,
Thomas Purcell's avatar
Thomas Purcell committed
571
        std::string project_type="regression",
572
573
574
575
576
577
        int n_sis_select=1,
        double cross_corr_max=1.0
    );

    // DocString: feat_space_sis_arr
    /**
578
     * @brief Perform Sure-Independence Screening over the FeatureSpace. The features are ranked using a projection operator constructed using _project_type and the Property vector
579
     *
580
     * @param prop (np.ndarray) Array containing the property vector (training data only)
581
582
583
584
585
586
587
588
589
     */
    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
    /**
590
     * @brief Perform Sure-Independence Screening over the FeatureSpace. The features are ranked using a projection operator constructed using _project_type and the Property vector
591
     *
592
     * @param prop (list) List containing the property vector (training data only)
593
594
595
596
597
598
599
600
601
     */
    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
    /**
602
     * @brief A list of all of the selected features
603
604
605
606
607
     */
    py::list phi_selected_py();

    // DocString: feat_space_phi0_py
    /**
608
     * @brief A list containing all of the primary features
609
610
611
612
613
     */
    py::list phi0_py();

    // DocString: feat_space_phi_py
    /**
614
     * @brief A list of all features in the FeatureSpace
615
616
617
618
619
     */
    py::list phi_py();

    // DocString: feat_space_scores_py
    /**
620
     * @brief An array of all stored projection scores from SIS
621
622
623
624
625
     */
    inline np::ndarray scores_py(){return python_conv_utils::to_ndarray<double>(_scores);};

    // DocString: feat_space_task_sizes_py
    /**
626
     * @brief A list of the number of samples in each task for the training data
627
628
629
630
631
     */
    inline py::list task_sizes_py(){return python_conv_utils::to_list<int>(_task_sizes);};

    // DocString: feat_space_allowed_ops_py
    /**
632
     * @brief The list of allowed operators
633
634
635
636
637
     */
    inline py::list allowed_ops_py(){return python_conv_utils::to_list<std::string>(_allowed_ops);}

    // DocString: feat_space_start_gen_py
    /**
638
     * @brief A list containing the index of the first feature of each rung in the feature space.
639
640
641
642
643
     */
    inline py::list start_gen_py(){return python_conv_utils::to_list<int>(_start_gen);}

    // DocString: feat_space_get_feature
    /**
644
     * @brief Access the feature in _phi with an index ind
645
     *
646
     * @param ind (int) The index of the feature to get
647
648
     * @return A ModelNode of the feature at index ind
     */
649
    inline ModelNode get_feature(const int ind) const {return ModelNode(_phi[ind]);}
650
    #endif
Thomas Purcell's avatar
Thomas Purcell committed
651
652
};

653
#endif