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

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

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

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

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

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

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

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

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

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

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

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

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

Thomas Purcell's avatar
Thomas Purcell committed
161
162
163
164
165
166
    // 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;}

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

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

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

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

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

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

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

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

Thomas Purcell's avatar
Thomas Purcell committed
215
216
    /**
     * @brief Generate a new set of non-parameterized features from a single feature
Thomas Purcell's avatar
Thomas Purcell committed
217
     * @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
218
219
     *
     * @param feat The feature to spawn new features from
Thomas Purcell's avatar
Thomas Purcell committed
220
221
     * @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
222
     * @param feat_ind starting index for the next feature generated
Thomas Purcell's avatar
Thomas Purcell committed
223
224
     * @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
225
226
227
228
     */
    void generate_non_param_feats(
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
229
        const std::vector<node_ptr>::iterator& start,
Thomas Purcell's avatar
Thomas Purcell committed
230
231
232
233
234
        unsigned long int& feat_ind,
        const double l_bound=1e-50,
        const double u_bound=1e50
    );

235
#ifdef PARAMETERIZE
236
    /**
Thomas Purcell's avatar
Thomas Purcell committed
237
     * @brief Generate a new set of parameterized features from a single feature
Thomas Purcell's avatar
Thomas Purcell committed
238
     * @details Perform all valid algebraic operations on the passed feature and all features that appear before it in _phi.
239
240
     *
     * @param feat The feature to spawn new features from
Thomas Purcell's avatar
Thomas Purcell committed
241
242
     * @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
243
244
     * @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
245
246
     * @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
247
     */
Thomas Purcell's avatar
Thomas Purcell committed
248
    void generate_param_feats(
249
250
        std::vector<node_ptr>::iterator& feat,
        std::vector<node_ptr>& feat_set,
251
        const std::vector<node_ptr>::iterator& start,
252
253
        unsigned long int& feat_ind,
        std::shared_ptr<NLOptimizer> optimizer,
254
255
        const double l_bound=1e-50,
        const double u_bound=1e50
256
    );
Thomas Purcell's avatar
Thomas Purcell committed
257

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

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

285
    /**
Thomas Purcell's avatar
Thomas Purcell committed
286
     * @brief Generate the final rung of features on the fly and calculate their projection scores for SISat can be selected by SIS.
287
     *
Thomas Purcell's avatar
Thomas Purcell committed
288
289
290
     * @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
291
     */
Thomas Purcell's avatar
Thomas Purcell committed
292
    void generate_and_project(std::shared_ptr<LossFunction> loss, std::vector<node_ptr>& phi_selected, std::vector<double>& scores_selected);
293

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

586
#endif