FeatureSpace.hpp 25.4 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
/** @file feature_creation/feature_space/FeatureSpace.hpp
16
 *  @brief Defines the class for creating/operating on a feature space in SISSO
17
 *
18
 *  @author Thomas A. R. Purcell (tpurcell90)
19
20
21
 *  @bug No known bugs.
 */

Thomas Purcell's avatar
Thomas Purcell committed
22
23
24
#ifndef FEATURE_SPACE
#define FEATURE_SPACE

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

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

Thomas Purcell's avatar
Thomas Purcell committed
29
#include "feature_creation/node/utils.hpp"
30
#include "inputs/InputParser.hpp"
Thomas Purcell's avatar
Thomas Purcell committed
31
32

#include "mpi_interface/MPI_Interface.hpp"
33
#include "mpi_interface/MPI_Ops.hpp"
Thomas Purcell's avatar
Thomas Purcell committed
34
35
#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
 * @brief Feature Space for SISSO calculations. It stores and performs all actions on the feature space for SISSO.
46
47
 *
 */
Thomas Purcell's avatar
Thomas Purcell committed
48
49
class FeatureSpace
{
Thomas Purcell's avatar
Thomas Purcell committed
50
51
    std::vector<node_ptr> _phi_selected; //!< A vector containing all of the selected features
    std::vector<node_ptr> _phi; //!< A vector containing all features generated (Not including those created on the Fly during SIS)
52
    std::vector<node_ptr> _phi_0; //!< A vector containing all of the Primary features
53

54
    #ifdef PARAMETERIZE
Thomas Purcell's avatar
Thomas Purcell committed
55
56
57
58
59
60
61
62
    std::vector<node_ptr> _phi_reparam; //!< A vector containing the features created when reparameterizating using the residuals
    std::vector<int> _end_no_params; //!< A vector containing the indexes of each rung where parameterized nodes start
    std::vector<int> _start_rung_reparam; //!< A vector containing the indexes of each rung where parameterized nodes start

    std::vector<un_param_op_node_gen> _un_param_operators; //!< Vector containing all parameterized unary operators with free parameters
    std::vector<bin_param_op_node_gen> _com_bin_param_operators; //!< Vector containing all parameterized commutable binary operators with free parameters
    std::vector<bin_param_op_node_gen> _bin_param_operators; //!< Vector containing all parameterized binary operators with free parameters
    std::vector<std::string> _allowed_param_ops; //!< Vector containing all allowed operators strings for operators with free parameters
63
    #endif
64

Thomas Purcell's avatar
Thomas Purcell committed
65
66
67
68
    std::vector<std::string> _allowed_ops; //!< Vector containing all allowed operators strings
    std::vector<un_op_node_gen> _un_operators; //!< Vector containing all unary operators
    std::vector<bin_op_node_gen> _com_bin_operators; //!< Vector containing all commutable binary operators
    std::vector<bin_op_node_gen> _bin_operators; //!< Vector containing all binary operators
69

70
    std::vector<double> _prop_train; //!< The value of the property vector for each training sample
Thomas Purcell's avatar
Thomas Purcell committed
71
    std::vector<double> _scores; //!< The projection scores for each feature
72

73
    const std::vector<int> _task_sizes_train; //!< Number of training samples per task
Thomas Purcell's avatar
Thomas Purcell committed
74
75
76
77
    std::vector<int> _start_rung; //!< Vector containing the indexes where each rung starts in _phi
    const std::string _project_type; //!< The type of LossFunction to use when projecting the features onto a property
    const std::string _feature_space_file; //!< File to output the computer readable representation of the selected features to
    const std::string _feature_space_summary_file; //!< File to output the human readable representation of the selected features to
78

Thomas Purcell's avatar
Thomas Purcell committed
79
80
    std::function<bool(const double*, const int, const double, const std::vector<double>&, const double, const int, const int)> _is_valid; //!< Function used to determine of a feature is too correlated to previously selected features
    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 determine of a feature is too correlated to previously selected features within a given list
81

Thomas Purcell's avatar
Thomas Purcell committed
82
    std::shared_ptr<MPI_Interface> _mpi_comm; //!< the MPI communicator for the calculation
83

84
    const double _cross_cor_max; //!< Maximum cross-correlation used for selecting features
Thomas Purcell's avatar
Thomas Purcell committed
85
86
    const double _l_bound; //!< The lower bound for the maximum absolute value of the features
    const double _u_bound; //!< The upper bound for the maximum absolute value of the features
87

Thomas Purcell's avatar
Thomas Purcell committed
88
89
90
    int _n_rung_store; //!< The number of rungs to calculate and store the value of the features for all samples
    int _n_feat; //!< Total number of features in the feature space
    int _max_rung; //!< Maximum rung for the feature creation
91

Thomas Purcell's avatar
Thomas Purcell committed
92
    const int _n_sis_select; //!< Number of features to select during each SIS iteration
93
    const int _n_samp_train; //!< Number of samples in the training set
Thomas Purcell's avatar
Thomas Purcell committed
94
    const int _n_rung_generate; //!< Either 0 or 1, and is the number of rungs to generate on the fly during SIS
95

Thomas Purcell's avatar
Thomas Purcell committed
96
97
    int _max_param_depth; //!< The maximum depth in the binary expression tree to set non-linear optimization
    const bool _reparam_residual; //!< If True then reparameterize features using the residuals of each model
98

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

101
    // DocString: feat_space_init
102
    /**
103
     * @brief Construct a FeatureSpace using an InputParser object
104
     *
105
     * @param inputs InputParser object used to build the FeatureSpace
106
     */
107
    FeatureSpace(InputParser inputs);
108

109
    /**
Thomas Purcell's avatar
Thomas Purcell committed
110
     * @brief Populate the operator lists using _allowed_ops and _allowed_param_ops
111
112
113
114
     */
    void set_op_lists();

    /**
Thomas Purcell's avatar
Thomas Purcell committed
115
     * @brief Create SIS output files and write their headers
116
     */
117
    void initialize_fs_output_files() const;
Thomas Purcell's avatar
Thomas Purcell committed
118

119
    /**
Thomas Purcell's avatar
Thomas Purcell committed
120
     * @brief Populate _phi using _phi_0 and the allowed operators up to (_max_rung - _n_rung_generate)^th rung
121
     */
122
    void generate_feature_space();
Thomas Purcell's avatar
Thomas Purcell committed
123

124
    /**
Thomas Purcell's avatar
Thomas Purcell committed
125
     * @brief A vector containing all of the selected features
126
     */
127
    inline std::vector<node_ptr> phi_selected() const {return _phi_selected;};
128
129

    /**
Thomas Purcell's avatar
Thomas Purcell committed
130
     * @brief A vector containing all features generated (Not including those created on the Fly during SIS)
131
     */
132
    inline std::vector<node_ptr> phi() const {return _phi;};
133
134

    /**
Thomas Purcell's avatar
Thomas Purcell committed
135
     * @brief A vector containing all of the Primary features
136
     */
137
    inline std::vector<node_ptr> phi0() const {return _phi_0;};
138
139

    /**
Thomas Purcell's avatar
Thomas Purcell committed
140
     * @brief The projection scores for each feature in _phi
141
     */
142
    inline std::vector<double> scores() const {return _scores;}
143

144
    /**
145
     * @brief The MPI Communicator
146
     */
147
    inline std::shared_ptr<MPI_Interface> mpi_comm() const {return _mpi_comm;}
148

149
    /**
Thomas Purcell's avatar
Thomas Purcell committed
150
     * @brief Number of training samples per task
151
     */
152
    inline std::vector<int> task_sizes_train() const {return _task_sizes_train;}
153

154
    // DocString: feat_space_feature_space_file
155
    /**
Thomas Purcell's avatar
Thomas Purcell committed
156
     * @brief Filename of the file to output the computer readable representation of the selected features to
157
     */
158
    inline std::string feature_space_file() const {return _feature_space_file;}
159

Thomas Purcell's avatar
Thomas Purcell committed
160
161
162
163
164
165
    // DocString: feat_space_feature_space_file
    /**
     * @brief Filename of the file to output the human readable representation of the selected features to
     */
    inline std::string feature_space_summary_file() const {return _feature_space_summary_file;}

166
    // DocString: feat_space_l_bound
167
    /**
Thomas Purcell's avatar
Thomas Purcell committed
168
     * @brief The mlower bound for the maximum absolute value of the features
169
     */
170
    inline double l_bound() const {return _l_bound;}
171

172
    // DocString: feat_space_u_bound
173
    /**
Thomas Purcell's avatar
Thomas Purcell committed
174
     * @brief The upper bound for the maximum absolute value of the features
175
     */
176
    inline double u_bound() const {return _u_bound;}
177

Thomas Purcell's avatar
Thomas Purcell committed
178
    // DocString: feat_space_max_rung
179
    /**
Thomas Purcell's avatar
Thomas Purcell committed
180
     * @brief The maximum rung for the feature creation
181
     */
Thomas Purcell's avatar
Thomas Purcell committed
182
    inline int max_rung() const {return _max_rung;}
183

184
    // DocString: feat_space_n_sis_select
185
    /**
Thomas Purcell's avatar
Thomas Purcell committed
186
     * @brief The number of features to select during each SIS iteration
187
     */
188
    inline int n_sis_select() const {return _n_sis_select;}
189

190
    // DocString: feat_space_n_samp_train
191
    /**
Thomas Purcell's avatar
Thomas Purcell committed
192
     * @brief The nuumber of samples in the training set
193
     */
194
    inline int n_samp_train() const {return _n_samp_train;}
195

196
    // DocString: feat_space_n_feat
197
    /**
Thomas Purcell's avatar
Thomas Purcell committed
198
     * @brief The total number of features in the feature space
199
     */
200
    inline int n_feat() const {return _n_feat;}
201

202
    // DocString: feat_space_n_rung_store
203
    /**
Thomas Purcell's avatar
Thomas Purcell committed
204
     * @brief The number of rungs to calculate and store the value of the features for all samples
205
     */
206
    inline int n_rung_store() const {return _n_rung_store;}
207

208
    // DocString: feat_space_n_rung_generate
209
    /**
210
     * @brief Either 0 or 1, and is the number of rungs to generate on the fly during SIS
211
     */
212
    inline int n_rung_generate() const {return _n_rung_generate;}
213

Thomas Purcell's avatar
Thomas Purcell committed
214
215
    /**
     * @brief Generate a new set of non-parameterized features from a single feature
Thomas Purcell's avatar
Thomas Purcell committed
216
     * @details Perform all valid algebraic operations on the passed feature and all features that appear before it in _phi.
Thomas Purcell's avatar
Thomas Purcell committed
217
218
     *
     * @param feat The feature to spawn new features from
Thomas Purcell's avatar
Thomas Purcell committed
219
220
     * @param feat_set The feature set to pull features from for binary operations
     * @param start The point in feat_set to begin pulling features from for binary operations
Thomas Purcell's avatar
Thomas Purcell committed
221
     * @param feat_ind starting index for the next feature generated
Thomas Purcell's avatar
Thomas Purcell committed
222
223
     * @param l_bound lower bound for the maximum absolute value of the feature
     * @param u_bound upper bound for the maximum abosulte value of the feature
Thomas Purcell's avatar
Thomas Purcell committed
224
225
226
227
     */
    void generate_non_param_feats(
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
228
        const std::vector<node_ptr>::iterator& start,
Thomas Purcell's avatar
Thomas Purcell committed
229
230
231
232
233
        unsigned long int& feat_ind,
        const double l_bound=1e-50,
        const double u_bound=1e50
    );

234
#ifdef PARAMETERIZE
235
    /**
Thomas Purcell's avatar
Thomas Purcell committed
236
     * @brief Generate a new set of parameterized features from a single feature
Thomas Purcell's avatar
Thomas Purcell committed
237
     * @details Perform all valid algebraic operations on the passed feature and all features that appear before it in _phi.
238
239
     *
     * @param feat The feature to spawn new features from
Thomas Purcell's avatar
Thomas Purcell committed
240
241
     * @param feat_set The feature set to pull features from for binary operations
     * @param start The point in feat_set to begin pulling features from for binary operations
242
243
     * @param feat_ind starting index for the next feature generated
     * @param optimizer The object used to optimize the parameterized features
Thomas Purcell's avatar
Thomas Purcell committed
244
245
     * @param l_bound lower bound for the maximum absolute value of the feature
     * @param u_bound upper bound for the maximum abosulte value of the feature
246
     */
Thomas Purcell's avatar
Thomas Purcell committed
247
    void generate_param_feats(
248
249
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
250
        const std::vector<node_ptr>::iterator& start,
251
252
        unsigned long int& feat_ind,
        std::shared_ptr<NLOptimizer> optimizer,
253
254
        const double l_bound=1e-50,
        const double u_bound=1e50
255
    );
Thomas Purcell's avatar
Thomas Purcell committed
256

257
    /**
Thomas Purcell's avatar
Thomas Purcell committed
258
     * @brief Generate a new set of parameterized features for the residuals
259
260
     *
     * @param feat The feature to spawn new features from
Thomas Purcell's avatar
Thomas Purcell committed
261
     * @param feat_set The feature set to pull features from for binary operations
262
     * @param feat_ind starting index for the next feature generated
Thomas Purcell's avatar
Thomas Purcell committed
263
     * @param optimizer The object used to optimize the parameterized features
Thomas Purcell's avatar
Thomas Purcell committed
264
265
     * @param l_bound lower bound for the maximum absolute value of the feature
     * @param u_bound upper bound for the maximum abosulte value of the feature
266
     */
Thomas Purcell's avatar
Thomas Purcell committed
267
    void generate_reparam_feats(
268
269
270
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
        unsigned long int& feat_ind,
Thomas Purcell's avatar
Thomas Purcell committed
271
        std::shared_ptr<NLOptimizer> optimizer,
272
273
        const double l_bound=1e-50,
        const double u_bound=1e50
274
    );
Thomas Purcell's avatar
Thomas Purcell committed
275
276
277
278
279
280
281

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

284
    /**
Thomas Purcell's avatar
Thomas Purcell committed
285
     * @brief Generate the final rung of features on the fly and calculate their projection scores for SISat can be selected by SIS.
286
     *
Thomas Purcell's avatar
Thomas Purcell committed
287
288
289
     * @param loss The LossFunction used to project over all of the features
     * @param phi_selected The set of features that would be selected excluding the final rung
     * @param scores_selected The projection scores of all features in phi_selected
290
     */
Thomas Purcell's avatar
Thomas Purcell committed
291
    void generate_and_project(std::shared_ptr<LossFunction> loss, std::vector<node_ptr>& phi_selected, std::vector<double>& scores_selected);
292

293
    /**
294
     * @brief Perform Sure-Independence Screening over the FeatureSpace. The features are ranked using a projection operator constructed using _project_type and the Property vector
295
     *
296
     * @param prop Vector containing the property vector (training data only)
297
     */
298
    void sis(const std::vector<double>& prop);
299

Thomas Purcell's avatar
Thomas Purcell committed
300
    /**
301
     * @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
302
     *
303
     * @param loss The LossFunction used to project over all of the features
Thomas Purcell's avatar
Thomas Purcell committed
304
305
306
     */
    void sis(std::shared_ptr<LossFunction> loss);

307
    // DocString: feat_space_feat_in_phi
308
309
310
    /**
     * @brief Is a feature in this process' _phi?
     *
311
     * @param ind (int) The index of the feature
Thomas Purcell's avatar
Thomas Purcell committed
312
     *
313
     * @return True if feature is in this rank's _phi
314
     */
315
    inline bool feat_in_phi(int ind) const {return (ind >= _phi[0]->feat_ind()) && (ind <= _phi.back()->feat_ind());}
316

317
318
319
320
    // DocString: feat_space_remove_feature
    /**
     * @brief Remove a feature from phi
     *
Thomas Purcell's avatar
Thomas Purcell committed
321
     * @param ind (int) index of feature to remove
322
     */
323
    void remove_feature(const int ind);
324

325
326
    // Python Interface Functions
    #ifdef PY_BINDINGS
327
    #ifdef PARAMETERIZE
Thomas Purcell's avatar
Thomas Purcell committed
328
329

    // DocString: feat_space_init_py_list
330
    /**
331
     * @brief FeatureSpace constructor given a set of primary features and operators
332
     *
333
334
335
336
337
     * @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
Thomas Purcell's avatar
Thomas Purcell committed
338
     * @param max_rung (int) The maximum rung of the feature (Height of the binary expression tree -1)
339
     * @param n_sis_select (int) The number of features to select during each SIS step
Thomas Purcell's avatar
Thomas Purcell committed
340
     * @param n_rung_store (int) The number of rungs whose feature's data is always stored in memory
341
342
343
344
345
346
     * @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
347
348
349
350
351
352
353
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        py::list allowed_param_ops,
        py::list prop,
        std::string project_type="regression",
Thomas Purcell's avatar
Thomas Purcell committed
354
        int max_rung=1,
355
        int n_sis_select=1,
Thomas Purcell's avatar
Thomas Purcell committed
356
        int n_rung_store=-1,
357
358
359
360
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50,
361
362
        int max_param_depth = -1,
        bool reparam_residual=false
363
364
    );

Thomas Purcell's avatar
Thomas Purcell committed
365
    // DocString: feat_space_init_np_array
366
    /**
367
     * @brief FeatureSpace constructor given a set of primary features and operators
368
     *
369
370
371
372
373
     * @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
Thomas Purcell's avatar
Thomas Purcell committed
374
     * @param max_rung (int) The maximum rung of the feature (Height of the binary expression tree -1)
375
     * @param n_sis_select (int) The number of features to select during each SIS step
Thomas Purcell's avatar
Thomas Purcell committed
376
     * @param n_rung_store (int) The number of rungs whose feature's data is always stored in memory
377
378
379
380
381
382
     * @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
383
384
385
386
387
388
389
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        py::list allowed_param_ops,
        np::ndarray prop,
        std::string project_type="regression",
Thomas Purcell's avatar
Thomas Purcell committed
390
        int max_rung=1,
391
        int n_sis_select=1,
Thomas Purcell's avatar
Thomas Purcell committed
392
        int n_rung_store=-1,
393
394
395
396
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50,
397
398
        int max_param_depth = -1,
        bool reparam_residual=false
399
    );
Thomas Purcell's avatar
Thomas Purcell committed
400

401
    #else
Thomas Purcell's avatar
Thomas Purcell committed
402
403

    // DocString: feat_space_ini_no_param_py_list
404
    /**
405
     * @brief FeatureSpace constructor given a set of primary features and operators
406
     *
407
408
409
410
     * @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
Thomas Purcell's avatar
Thomas Purcell committed
411
     * @param max_rung (int) The maximum rung of the feature (Height of the binary expression tree -1)
412
     * @param n_sis_select (int) The number of features to select during each SIS step
Thomas Purcell's avatar
Thomas Purcell committed
413
     * @param n_rung_store (int) The number of rungs whose feature's data is always stored in memory
414
415
416
417
     * @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
418
419
420
421
422
423
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        py::list prop,
        std::string project_type="regression",
Thomas Purcell's avatar
Thomas Purcell committed
424
        int max_rung=1,
425
        int n_sis_select=1,
Thomas Purcell's avatar
Thomas Purcell committed
426
        int n_rung_store=-1,
427
428
429
430
431
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50
    );
432

Thomas Purcell's avatar
Thomas Purcell committed
433
    // DocString: feat_space_init_no_param_np_array
434
    /**
435
     * @brief FeatureSpace constructor given a set of primary features and operators
436
     *
437
438
439
440
     * @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
Thomas Purcell's avatar
Thomas Purcell committed
441
     * @param max_rung (int) The maximum rung of the feature (Height of the binary expression tree -1)
442
     * @param n_sis_select (int) The number of features to select during each SIS step
Thomas Purcell's avatar
Thomas Purcell committed
443
     * @param n_rung_store (int) The number of rungs whose feature's data is always stored in memory
444
445
446
447
     * @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
448
449
450
451
452
453
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        np::ndarray prop,
        std::string project_type="regression",
Thomas Purcell's avatar
Thomas Purcell committed
454
        int max_rung=1,
455
        int n_sis_select=1,
Thomas Purcell's avatar
Thomas Purcell committed
456
        int n_rung_store=-1,
457
458
459
460
461
462
        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
463

464
    // DocString: feat_space_init_file_np_array
465
    /**
466
     * @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>)
467
     *
468
469
470
     * @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)
471
     * @param task_sizes_train (list) The number of samples in the training data per task
472
473
474
     * @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
475
476
477
478
479
     */
    FeatureSpace(
        std::string feature_file,
        py::list phi_0,
        np::ndarray prop,
480
        py::list task_sizes_train,
Thomas Purcell's avatar
Thomas Purcell committed
481
        std::string project_type="regression",
482
483
484
485
        int n_sis_select=1,
        double cross_corr_max=1.0
    );

486
    // DocString: feat_space_init_file_py_list
487
    /**
488
     * @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>)
489
     *
490
491
492
     * @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)
493
     * @param task_sizes_train (list) The number of samples in the training data per task
494
495
496
     * @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
497
498
499
500
501
     */
    FeatureSpace(
        std::string feature_file,
        py::list phi_0,
        py::list prop,
502
        py::list task_sizes_train,
Thomas Purcell's avatar
Thomas Purcell committed
503
        std::string project_type="regression",
504
505
506
507
508
509
        int n_sis_select=1,
        double cross_corr_max=1.0
    );

    // DocString: feat_space_sis_arr
    /**
510
     * @brief Perform Sure-Independence Screening over the FeatureSpace. The features are ranked using a projection operator constructed using _project_type and the Property vector
511
     *
512
     * @param prop (np.ndarray) Array containing the property vector (training data only)
513
514
515
516
517
518
519
520
521
     */
    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
    /**
522
     * @brief Perform Sure-Independence Screening over the FeatureSpace. The features are ranked using a projection operator constructed using _project_type and the Property vector
523
     *
524
     * @param prop (list) List containing the property vector (training data only)
525
526
527
528
529
530
531
532
533
     */
    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
    /**
Thomas Purcell's avatar
Thomas Purcell committed
534
     * @brief A list containing all of the selected features
535
536
537
538
539
     */
    py::list phi_selected_py();

    // DocString: feat_space_phi0_py
    /**
Thomas Purcell's avatar
Thomas Purcell committed
540
     * @brief A list containing all features generated (Not including those created on the Fly during SIS)
541
     */
Thomas Purcell's avatar
Thomas Purcell committed
542
    py::list phi_py();
543
544
545

    // DocString: feat_space_phi_py
    /**
Thomas Purcell's avatar
Thomas Purcell committed
546
     * @brief A list containing all of the Primary features
547
     */
Thomas Purcell's avatar
Thomas Purcell committed
548
    py::list phi0_py();
549
550
551

    // DocString: feat_space_scores_py
    /**
552
     * @brief An array of all stored projection scores from SIS
553
554
555
     */
    inline np::ndarray scores_py(){return python_conv_utils::to_ndarray<double>(_scores);};

556
    // DocString: feat_space_task_sizes_train_py
557
    /**
558
     * @brief A list of the number of samples in each task for the training data
559
     */
560
    inline py::list task_sizes_train_py(){return python_conv_utils::to_list<int>(_task_sizes_train);};
561
562
563

    // DocString: feat_space_allowed_ops_py
    /**
564
     * @brief The list of allowed operators
565
566
567
     */
    inline py::list allowed_ops_py(){return python_conv_utils::to_list<std::string>(_allowed_ops);}

Thomas Purcell's avatar
Thomas Purcell committed
568
    // DocString: feat_space_start_rung_py
569
    /**
570
     * @brief A list containing the index of the first feature of each rung in the feature space.
571
     */
Thomas Purcell's avatar
Thomas Purcell committed
572
    inline py::list start_rung_py(){return python_conv_utils::to_list<int>(_start_rung);}
573
574
575

    // DocString: feat_space_get_feature
    /**
576
     * @brief Access the feature in _phi with an index ind
577
     *
578
     * @param ind (int) The index of the feature to get
579
580
     * @return A ModelNode of the feature at index ind
     */
581
    inline ModelNode get_feature(const int ind) const {return ModelNode(_phi[ind]);}
582
    #endif
Thomas Purcell's avatar
Thomas Purcell committed
583
584
};

585
#endif