FeatureSpace.hpp 28.8 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"
Thomas Purcell's avatar
Thomas Purcell committed
30
31

#include "mpi_interface/MPI_Interface.hpp"
32
#include "mpi_interface/MPI_Ops.hpp"
Thomas Purcell's avatar
Thomas Purcell committed
33
34
#include "mpi_interface/serialize_tuple.h"

Thomas Purcell's avatar
Thomas Purcell committed
35
36
#include "utils/project.hpp"

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

42
// DocString: cls_feat_space
43
44
45
46
47
/**
 * @brief Feature Space for SISSO calculations
 * @details Stores and performs all feature calculations for SIS
 *
 */
Thomas Purcell's avatar
Thomas Purcell committed
48
49
class FeatureSpace
{
Thomas Purcell's avatar
Thomas Purcell committed
50
51
52
    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)
    const 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

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

Thomas Purcell's avatar
Thomas Purcell committed
73
74
75
76
77
    const std::vector<int> _task_sizes; //!< Number of training samples per task
    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
93
94
    const int _n_sis_select; //!< Number of features to select during each SIS iteration
    const int _n_samp; //!< Number of samples in the training set
    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
    #ifdef PARAMETERIZE
102
    /**
Thomas Purcell's avatar
Thomas Purcell committed
103
     * @brief FeatureSpace constructor given a set of primary features and operators
104
105
     *
     * @param mpi_comm MPI communicator for the calculations
Thomas Purcell's avatar
Thomas Purcell committed
106
107
108
109
     * @param phi_0 The set of primary features
     * @param allowed_ops The list of allowed operators
     * @param allowed_param_ops The list of allowed operators to be used with non-linear optimization
     * @param prop List containing the property vector (training data only)
110
     * @param task_sizes The number of samples per task
Thomas Purcell's avatar
Thomas Purcell committed
111
112
113
114
115
116
117
118
119
120
     * @param project_type The type of loss function/projection operator to use
     * @param max_rung The maximum rung of the feature (Height of the binary expression tree -1)
     * @param n_sis_select The number of features to select during each SIS step
     * @param n_rung_store The number of rungs whose feature's data is always stored in memory
     * @param n_rung_generate Either 0 or 1, and is the number of rungs to generate on the fly during SIS
     * @param cross_corr_max The maximum allowed cross-correlation value between selected features
     * @param min_abs_feat_val The minimum allowed absolute feature value for a feature
     * @param max_abs_feat_val The maximum allowed absolute feature value for a feature
     * @param max_param_depth The maximum depth in the binary expression tree to set non-linear optimization
     * @param reparam_residual If True then reparameterize features using the residuals of each model
121
     */
Thomas Purcell's avatar
Thomas Purcell committed
122
    FeatureSpace(
Thomas Purcell's avatar
Thomas Purcell committed
123
        std::shared_ptr<MPI_Interface> mpi_comm,
Thomas Purcell's avatar
Thomas Purcell committed
124
125
        std::vector<node_ptr> phi_0,
        std::vector<std::string> allowed_ops,
Thomas Purcell's avatar
Thomas Purcell committed
126
        std::vector<std::string> allowed_param_ops,
127
        std::vector<double> prop,
Thomas Purcell's avatar
Thomas Purcell committed
128
        std::vector<int> task_sizes,
129
        std::string project_type="regression",
Thomas Purcell's avatar
Thomas Purcell committed
130
        int max_rung=1,
Thomas Purcell's avatar
Thomas Purcell committed
131
        int n_sis_select=1,
Thomas Purcell's avatar
Thomas Purcell committed
132
        int n_rung_store=-1,
133
        int n_rung_generate=0,
Thomas Purcell's avatar
Thomas Purcell committed
134
        double cross_corr_max=1.0,
135
        double min_abs_feat_val=1e-50,
136
        double max_abs_feat_val=1e50,
137
138
        int max_param_depth=-1,
        bool reparam_residual=false
139
    );
140
141
    #else
    /**
Thomas Purcell's avatar
Thomas Purcell committed
142
     * @brief FeatureSpace constructor given a set of primary features and operators
143
144
     *
     * @param mpi_comm MPI communicator for the calculations
Thomas Purcell's avatar
Thomas Purcell committed
145
146
147
     * @param phi_0 The set of primary features
     * @param allowed_ops The list of allowed operators
     * @param prop List containing the property vector (training data only)
148
     * @param task_sizes The number of samples per task
Thomas Purcell's avatar
Thomas Purcell committed
149
150
151
152
153
154
155
156
     * @param project_type The type of loss function/projection operator to use
     * @param max_rung The maximum rung of the feature (Height of the binary expression tree -1)
     * @param n_sis_select The number of features to select during each SIS step
     * @param n_rung_store The number of rungs whose feature's data is always stored in memory
     * @param n_rung_generate Either 0 or 1, and is the number of rungs to generate on the fly during SIS
     * @param cross_corr_max The maximum allowed cross-correlation value between selected features
     * @param min_abs_feat_val The minimum allowed absolute feature value for a feature
     * @param max_abs_feat_val The maximum allowed absolute feature value for a feature
157
158
159
160
161
162
163
164
     */
    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",
Thomas Purcell's avatar
Thomas Purcell committed
165
        int max_rung=1,
166
        int n_sis_select=1,
Thomas Purcell's avatar
Thomas Purcell committed
167
        int n_rung_store=-1,
168
169
170
171
172
173
        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
174
    /**
Thomas Purcell's avatar
Thomas Purcell committed
175
     * @brief Initialize members of the FeatureSpace using _prop
176
     */
Thomas Purcell's avatar
Thomas Purcell committed
177
    void initialize_fs();
178

179
    /**
Thomas Purcell's avatar
Thomas Purcell committed
180
     * @brief Populate the operator lists using _allowed_ops and _allowed_param_ops
181
182
183
184
     */
    void set_op_lists();

    /**
Thomas Purcell's avatar
Thomas Purcell committed
185
     * @brief Create SIS output files and write their headers
186
     */
187
    void initialize_fs_output_files() const;
Thomas Purcell's avatar
Thomas Purcell committed
188

189
    /**
Thomas Purcell's avatar
Thomas Purcell committed
190
     * @brief Populate _phi using _phi_0 and the allowed operators up to (_max_rung - _n_rung_generate)^th rung
191
     */
192
    void generate_feature_space();
Thomas Purcell's avatar
Thomas Purcell committed
193

194
    /**
Thomas Purcell's avatar
Thomas Purcell committed
195
     * @brief A vector containing all of the selected features
196
     */
197
    inline std::vector<node_ptr> phi_selected() const {return _phi_selected;};
198
199

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

    /**
Thomas Purcell's avatar
Thomas Purcell committed
205
     * @brief A vector containing all of the Primary features
206
     */
207
    inline std::vector<node_ptr> phi0() const {return _phi_0;};
208
209

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

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

219
    /**
Thomas Purcell's avatar
Thomas Purcell committed
220
     * @brief Number of training samples per task
221
     */
222
    inline std::vector<int> task_sizes() const {return _task_sizes;}
223

224
    // DocString: feat_space_feature_space_file
225
    /**
Thomas Purcell's avatar
Thomas Purcell committed
226
     * @brief Filename of the file to output the computer readable representation of the selected features to
227
     */
228
    inline std::string feature_space_file() const {return _feature_space_file;}
229

Thomas Purcell's avatar
Thomas Purcell committed
230
231
232
233
234
235
    // 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;}

236
    // DocString: feat_space_l_bound
237
    /**
Thomas Purcell's avatar
Thomas Purcell committed
238
     * @brief The mlower bound for the maximum absolute value of the features
239
     */
240
    inline double l_bound() const {return _l_bound;}
241

242
    // DocString: feat_space_u_bound
243
    /**
Thomas Purcell's avatar
Thomas Purcell committed
244
     * @brief The upper bound for the maximum absolute value of the features
245
     */
246
    inline double u_bound() const {return _u_bound;}
247

Thomas Purcell's avatar
Thomas Purcell committed
248
    // DocString: feat_space_max_rung
249
    /**
Thomas Purcell's avatar
Thomas Purcell committed
250
     * @brief The maximum rung for the feature creation
251
     */
Thomas Purcell's avatar
Thomas Purcell committed
252
    inline int max_rung() const {return _max_rung;}
253

254
    // DocString: feat_space_n_sis_select
255
    /**
Thomas Purcell's avatar
Thomas Purcell committed
256
     * @brief The number of features to select during each SIS iteration
257
     */
258
    inline int n_sis_select() const {return _n_sis_select;}
259

260
    // DocString: feat_space_n_samp
261
    /**
Thomas Purcell's avatar
Thomas Purcell committed
262
     * @brief The nuumber of samples in the training set
263
     */
264
    inline int n_samp() const {return _n_samp;}
265

266
    // DocString: feat_space_n_feat
267
    /**
Thomas Purcell's avatar
Thomas Purcell committed
268
     * @brief The total number of features in the feature space
269
     */
270
    inline int n_feat() const {return _n_feat;}
271

272
    // DocString: feat_space_n_rung_store
273
    /**
Thomas Purcell's avatar
Thomas Purcell committed
274
     * @brief The number of rungs to calculate and store the value of the features for all samples
275
     */
276
    inline int n_rung_store() const {return _n_rung_store;}
277

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

Thomas Purcell's avatar
Thomas Purcell committed
284
285
    /**
     * @brief Generate a new set of non-parameterized features from a single feature
Thomas Purcell's avatar
Thomas Purcell committed
286
     * @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
287
288
     *
     * @param feat The feature to spawn new features from
Thomas Purcell's avatar
Thomas Purcell committed
289
290
     * @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
291
     * @param feat_ind starting index for the next feature generated
Thomas Purcell's avatar
Thomas Purcell committed
292
293
     * @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
294
295
296
297
     */
    void generate_non_param_feats(
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
298
        const std::vector<node_ptr>::iterator& start,
Thomas Purcell's avatar
Thomas Purcell committed
299
300
301
302
303
        unsigned long int& feat_ind,
        const double l_bound=1e-50,
        const double u_bound=1e50
    );

304
#ifdef PARAMETERIZE
305
    /**
Thomas Purcell's avatar
Thomas Purcell committed
306
     * @brief Generate a new set of parameterized features from a single feature
Thomas Purcell's avatar
Thomas Purcell committed
307
     * @details Perform all valid algebraic operations on the passed feature and all features that appear before it in _phi.
308
309
     *
     * @param feat The feature to spawn new features from
Thomas Purcell's avatar
Thomas Purcell committed
310
311
     * @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
312
313
     * @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
314
315
     * @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
316
     */
Thomas Purcell's avatar
Thomas Purcell committed
317
    void generate_param_feats(
318
319
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
320
        const std::vector<node_ptr>::iterator& start,
321
322
        unsigned long int& feat_ind,
        std::shared_ptr<NLOptimizer> optimizer,
323
324
        const double l_bound=1e-50,
        const double u_bound=1e50
325
    );
Thomas Purcell's avatar
Thomas Purcell committed
326

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

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

354
    /**
Thomas Purcell's avatar
Thomas Purcell committed
355
     * @brief Generate the final rung of features on the fly and calculate their projection scores for SISat can be selected by SIS.
356
     *
Thomas Purcell's avatar
Thomas Purcell committed
357
358
359
     * @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
360
     */
Thomas Purcell's avatar
Thomas Purcell committed
361
    void generate_and_project(std::shared_ptr<LossFunction> loss, std::vector<node_ptr>& phi_selected, std::vector<double>& scores_selected);
362

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

Thomas Purcell's avatar
Thomas Purcell committed
370
    /**
371
     * @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
372
     *
373
     * @param loss The LossFunction used to project over all of the features
Thomas Purcell's avatar
Thomas Purcell committed
374
375
376
     */
    void sis(std::shared_ptr<LossFunction> loss);

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

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

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

    // DocString: feat_space_init_py_list
400
    /**
401
     * @brief FeatureSpace constructor given a set of primary features and operators
402
     *
403
404
405
406
407
     * @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
408
     * @param max_rung (int) The maximum rung of the feature (Height of the binary expression tree -1)
409
     * @param n_sis_select (int) The number of features to select during each SIS step
Thomas Purcell's avatar
Thomas Purcell committed
410
     * @param n_rung_store (int) The number of rungs whose feature's data is always stored in memory
411
412
413
414
415
416
     * @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
417
418
419
420
421
422
423
     */
    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
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
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50,
431
432
        int max_param_depth = -1,
        bool reparam_residual=false
433
434
    );

Thomas Purcell's avatar
Thomas Purcell committed
435
    // DocString: feat_space_init_np_array
436
    /**
437
     * @brief FeatureSpace constructor given a set of primary features and operators
438
     *
439
440
441
442
443
     * @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
444
     * @param max_rung (int) The maximum rung of the feature (Height of the binary expression tree -1)
445
     * @param n_sis_select (int) The number of features to select during each SIS step
Thomas Purcell's avatar
Thomas Purcell committed
446
     * @param n_rung_store (int) The number of rungs whose feature's data is always stored in memory
447
448
449
450
451
452
     * @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
453
454
455
456
457
458
459
     */
    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
460
        int max_rung=1,
461
        int n_sis_select=1,
Thomas Purcell's avatar
Thomas Purcell committed
462
        int n_rung_store=-1,
463
464
465
466
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50,
467
468
        int max_param_depth = -1,
        bool reparam_residual=false
469
    );
Thomas Purcell's avatar
Thomas Purcell committed
470

471
    #else
Thomas Purcell's avatar
Thomas Purcell committed
472
473

    // DocString: feat_space_ini_no_param_py_list
474
    /**
475
     * @brief FeatureSpace constructor given a set of primary features and operators
476
     *
477
478
479
480
     * @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
481
     * @param max_rung (int) The maximum rung of the feature (Height of the binary expression tree -1)
482
     * @param n_sis_select (int) The number of features to select during each SIS step
Thomas Purcell's avatar
Thomas Purcell committed
483
     * @param n_rung_store (int) The number of rungs whose feature's data is always stored in memory
484
485
486
487
     * @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
488
489
490
491
492
493
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        py::list prop,
        std::string project_type="regression",
Thomas Purcell's avatar
Thomas Purcell committed
494
        int max_rung=1,
495
        int n_sis_select=1,
Thomas Purcell's avatar
Thomas Purcell committed
496
        int n_rung_store=-1,
497
498
499
500
501
        int n_rung_generate=0,
        double cross_corr_max=1.0,
        double min_abs_feat_val=1e-50,
        double max_abs_feat_val=1e50
    );
502

Thomas Purcell's avatar
Thomas Purcell committed
503
    // DocString: feat_space_init_no_param_np_array
504
    /**
505
     * @brief FeatureSpace constructor given a set of primary features and operators
506
     *
507
508
509
510
     * @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
511
     * @param max_rung (int) The maximum rung of the feature (Height of the binary expression tree -1)
512
     * @param n_sis_select (int) The number of features to select during each SIS step
Thomas Purcell's avatar
Thomas Purcell committed
513
     * @param n_rung_store (int) The number of rungs whose feature's data is always stored in memory
514
515
516
517
     * @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
518
519
520
521
522
523
     */
    FeatureSpace(
        py::list phi_0,
        py::list allowed_ops,
        np::ndarray prop,
        std::string project_type="regression",
Thomas Purcell's avatar
Thomas Purcell committed
524
        int max_rung=1,
525
        int n_sis_select=1,
Thomas Purcell's avatar
Thomas Purcell committed
526
        int n_rung_store=-1,
527
528
529
530
531
532
        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
533

534
    // DocString: feat_space_init_file_np_array
535
    /**
536
     * @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>)
537
     *
538
539
540
541
542
543
544
     * @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
545
546
547
548
549
550
     */
    FeatureSpace(
        std::string feature_file,
        py::list phi_0,
        np::ndarray prop,
        py::list task_sizes,
Thomas Purcell's avatar
Thomas Purcell committed
551
        std::string project_type="regression",
552
553
554
555
        int n_sis_select=1,
        double cross_corr_max=1.0
    );

556
    // DocString: feat_space_init_file_py_list
557
    /**
558
     * @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>)
559
     *
560
561
562
563
564
565
566
     * @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
567
568
569
570
571
572
     */
    FeatureSpace(
        std::string feature_file,
        py::list phi_0,
        py::list prop,
        py::list task_sizes,
Thomas Purcell's avatar
Thomas Purcell committed
573
        std::string project_type="regression",
574
575
576
577
578
579
        int n_sis_select=1,
        double cross_corr_max=1.0
    );

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

    // DocString: feat_space_phi0_py
    /**
Thomas Purcell's avatar
Thomas Purcell committed
610
     * @brief A list containing all features generated (Not including those created on the Fly during SIS)
611
     */
Thomas Purcell's avatar
Thomas Purcell committed
612
    py::list phi_py();
613
614
615

    // DocString: feat_space_phi_py
    /**
Thomas Purcell's avatar
Thomas Purcell committed
616
     * @brief A list containing all of the Primary features
617
     */
Thomas Purcell's avatar
Thomas Purcell committed
618
    py::list phi0_py();
619
620
621

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

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

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

Thomas Purcell's avatar
Thomas Purcell committed
638
    // DocString: feat_space_start_rung_py
639
    /**
640
     * @brief A list containing the index of the first feature of each rung in the feature space.
641
     */
Thomas Purcell's avatar
Thomas Purcell committed
642
    inline py::list start_rung_py(){return python_conv_utils::to_list<int>(_start_rung);}
643
644
645

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

655
#endif