diff --git a/src/feature_creation/feature_space/FeatureSpace.cpp b/src/feature_creation/feature_space/FeatureSpace.cpp index 9a7f9c33f3a0e8a8d93fe6ee8c20ebe4a562794f..c376fd56bea32633aace37719bbba67ec91e9256 100644 --- a/src/feature_creation/feature_space/FeatureSpace.cpp +++ b/src/feature_creation/feature_space/FeatureSpace.cpp @@ -731,29 +731,6 @@ void FeatureSpace::generate_feature_space() _phi.erase(_phi.begin() - del_inds[inds[ii]]); } - // Reorder features based on the number of parameters they have (none goes first) - std::vector<int> feat_n_params(_phi.size() - _start_gen.back()); - std::transform( - _phi.begin() + _start_gen.back(), - _phi.end(), - feat_n_params.begin(), - [](node_ptr feat){return feat->n_params();} - ); - inds = util_funcs::argsort<int>(feat_n_params); - next_phi.resize(feat_n_params.size()); - std::copy_n(_phi.begin() + _start_gen.back(), feat_n_params.size(), next_phi.begin()); - std::transform( - inds.begin(), - inds.end(), - _phi.begin() + _start_gen.back(), - [&next_phi](int ind){return next_phi[ind];} - ); - - // Set how many features have no parameters - _end_no_params.push_back( - std::count_if(feat_n_params.begin(), feat_n_params.end(), [](int n_param){return n_param == 0;}) - ); - // Reindex for(int ff = _start_gen.back(); ff < _phi.size(); ++ff) { @@ -921,6 +898,37 @@ void FeatureSpace::generate_feature_space() _phi[ff]->set_test_value(); } } + +#ifdef PARAMETERIZE + // Reorder features based on the number of parameters they have (none goes first) + std::vector<int> feat_n_params(_phi.size() - _start_gen.back()); + std::transform( + _phi.begin() + _start_gen.back(), + _phi.end(), + feat_n_params.begin(), + [](node_ptr feat){return feat->n_params();} + ); + inds = util_funcs::argsort<int>(feat_n_params); + next_phi.resize(feat_n_params.size()); + std::copy_n(_phi.begin() + _start_gen.back(), feat_n_params.size(), next_phi.begin()); + std::transform( + inds.begin(), + inds.end(), + _phi.begin() + _start_gen.back(), + [&next_phi](int ind){return next_phi[ind];} + ); + int cur_ind = _start_gen.back(); + std::for_each( + _phi.begin() + _start_gen.back(), + _phi.end(), + [&cur_ind](node_ptr feat){feat->reindex(cur_ind); ++cur_ind;} + ); + + // Set how many features have no parameters + _end_no_params.push_back( + std::count_if(feat_n_params.begin(), feat_n_params.end(), [](int n_param){return n_param == 0;}) + ); +#endif } _n_feat = _phi.size(); } diff --git a/src/python/feature_creation/FeatureSpace.cpp b/src/python/feature_creation/FeatureSpace.cpp index 70816ce8ec12f701c377209443dc58c06fafa478..a739122394a3e34da53a3510f0f4b02225e5a786 100644 --- a/src/python/feature_creation/FeatureSpace.cpp +++ b/src/python/feature_creation/FeatureSpace.cpp @@ -20,6 +20,8 @@ FeatureSpace::FeatureSpace( ): _phi(python_conv_utils::shared_ptr_vec_from_list<Node, FeatureNode>(phi_0)), _phi_0(_phi), + _end_no_params(1, 0), + _start_gen_reparam(1, 0), _allowed_param_ops(python_conv_utils::from_list<std::string>(allowed_param_ops)), _allowed_ops(python_conv_utils::from_list<std::string>(allowed_ops)), _prop(python_conv_utils::from_list<double>(prop)), @@ -64,6 +66,8 @@ FeatureSpace::FeatureSpace( ): _phi(python_conv_utils::shared_ptr_vec_from_list<Node, FeatureNode>(phi_0)), _phi_0(_phi), + _end_no_params(1, 0), + _start_gen_reparam(1, 0), _allowed_param_ops(python_conv_utils::from_list<std::string>(allowed_param_ops)), _allowed_ops(python_conv_utils::from_list<std::string>(allowed_ops)), _prop(python_conv_utils::from_ndarray<double>(prop)), @@ -181,6 +185,7 @@ FeatureSpace::FeatureSpace( _phi_0(python_conv_utils::shared_ptr_vec_from_list<Node, FeatureNode>(phi_0)), _prop(python_conv_utils::from_ndarray<double>(prop)), _scores(py::len(phi_0), 0.0), + _start_gen(1, 0), _task_sizes(python_conv_utils::from_list<int>(task_sizes)), _feature_space_file("feature_space/selected_features.txt"), _feature_space_summary_file("feature_space/SIS_summary.txt"), @@ -202,6 +207,7 @@ FeatureSpace::FeatureSpace( mpi_reduce_op::set_op(_project_type, _cross_cor_max, _n_sis_select); std::vector<node_ptr> phi_temp = str2node::phi_from_file(feature_file, _phi_0); + phi_temp.insert(phi_temp.begin(), _phi_0.begin(), _phi_0.end()); _n_feat = phi_temp.size(); _phi.resize(_n_feat); @@ -250,7 +256,9 @@ FeatureSpace::FeatureSpace( _start_gen.push_back(ff); } } - #ifdef PARAMETERIZE +#ifdef PARAMETERIZE + _start_gen_reparam = {0}; + _end_no_params = {0}; for(int rr = 1; rr < _max_phi; ++rr) { nlopt_wrapper::MAX_PARAM_DEPTH = rr; @@ -275,7 +283,44 @@ FeatureSpace::FeatureSpace( throw std::logic_error("The maximum parameter depth could not be determined from the file."); } node_value_arrs::initialize_param_storage(); - #endif + + for(int rr = 1; rr <= _max_phi; ++rr) + { + int n_feat_in_rung = (rr < _max_phi ?_start_gen[rr + 1] : _phi.size()) - _start_gen[rr]; + + // Reorder features based on the number of parameters they have (none goes first) + std::vector<int> feat_n_params(n_feat_in_rung); + std::transform( + _phi.begin() + _start_gen[rr], + _phi.begin() + _start_gen[rr] + n_feat_in_rung, + feat_n_params.begin(), + [](node_ptr feat){return feat->n_params();} + ); + std::vector<int> inds = util_funcs::argsort<int>(feat_n_params); + std::vector<node_ptr>phi_copy(n_feat_in_rung); + std::copy_n(_phi.begin() + _start_gen.back(), n_feat_in_rung, phi_copy.begin()); + std::transform( + inds.begin(), + inds.end(), + _phi.begin() + _start_gen[rr], + [&phi_copy](int ind){return phi_copy[ind];} + ); + + // Reindex the features to match the sorted order + int cur_ind = _start_gen.back(); + std::for_each( + _phi.begin() + _start_gen.back(), + _phi.begin() + _start_gen.back() + n_feat_in_rung, + [&cur_ind](node_ptr feat){feat->reindex(cur_ind); ++cur_ind;} + ); + + // Set how many features have no parameters + _end_no_params.push_back( + std::count_if(feat_n_params.begin(), feat_n_params.end(), [](int n_param){return n_param == 0;}) + ); + std::cout << "out" << std::endl; + } +#endif _scores.resize(_n_feat); } @@ -291,6 +336,7 @@ FeatureSpace::FeatureSpace( _phi_0(python_conv_utils::shared_ptr_vec_from_list<Node, FeatureNode>(phi_0)), _prop(python_conv_utils::from_list<double>(prop)), _scores(py::len(phi_0), 0.0), + _start_gen(1, 0), _task_sizes(python_conv_utils::from_list<int>(task_sizes)), _feature_space_file("feature_space/selected_features.txt"), _feature_space_summary_file("feature_space/SIS_summary.txt"), @@ -311,6 +357,7 @@ FeatureSpace::FeatureSpace( mpi_reduce_op::set_op(_project_type, _cross_cor_max, _n_sis_select); std::vector<node_ptr> phi_temp = str2node::phi_from_file(feature_file, _phi_0); + phi_temp.insert(phi_temp.begin(), _phi_0.begin(), _phi_0.end()); _n_feat = phi_temp.size(); _phi.resize(_n_feat); @@ -360,6 +407,8 @@ FeatureSpace::FeatureSpace( } } #ifdef PARAMETERIZE + _start_gen_reparam = {0}; + _end_no_params = {0}; for(int rr = 1; rr < _max_phi; ++rr) { nlopt_wrapper::MAX_PARAM_DEPTH = rr; @@ -384,6 +433,43 @@ FeatureSpace::FeatureSpace( throw std::logic_error("The maximum parameter depth could not be determined from the file."); } node_value_arrs::initialize_param_storage(); + + for(int rr = 1; rr <= _max_phi; ++rr) + { + int n_feat_in_rung = (rr < _max_phi ?_start_gen[rr + 1] : _phi.size()) - _start_gen[rr]; + + // Reorder features based on the number of parameters they have (none goes first) + std::vector<int> feat_n_params(n_feat_in_rung); + std::transform( + _phi.begin() + _start_gen[rr], + _phi.begin() + _start_gen[rr] + n_feat_in_rung, + feat_n_params.begin(), + [](node_ptr feat){return feat->n_params();} + ); + std::vector<int> inds = util_funcs::argsort<int>(feat_n_params); + std::vector<node_ptr>phi_copy(n_feat_in_rung); + std::copy_n(_phi.begin() + _start_gen.back(), n_feat_in_rung, phi_copy.begin()); + std::transform( + inds.begin(), + inds.end(), + _phi.begin() + _start_gen[rr], + [&phi_copy](int ind){return phi_copy[ind];} + ); + + // Reindex the features to match the sorted order + int cur_ind = _start_gen.back(); + std::for_each( + _phi.begin() + _start_gen.back(), + _phi.begin() + _start_gen.back() + n_feat_in_rung, + [&cur_ind](node_ptr feat){feat->reindex(cur_ind); ++cur_ind;} + ); + + // Set how many features have no parameters + _end_no_params.push_back( + std::count_if(feat_n_params.begin(), feat_n_params.end(), [](int n_param){return n_param == 0;}) + ); + } + #endif _scores.resize(_n_feat); }