Commit e783ee2c authored by Thomas Purcell's avatar Thomas Purcell
Browse files

Transfer some of the initialize_fs code into seperate functions

Done to remove copy/paste from python binding functions to C++ constructors
parent a0ee7ee3
......@@ -67,49 +67,24 @@ void FeatureSpace::initialize_fs(std::vector<double> prop, std::string project_t
else if(_max_phi - _n_rung_generate < _n_rung_store)
throw std::logic_error("Requesting to store more rungs than what can be pre-generated.");
if(_mpi_comm->rank() == 0)
{
std::ofstream out_file_stream = std::ofstream();
std::ofstream sum_file_stream = std::ofstream();
out_file_stream.open(_feature_space_file);
sum_file_stream.open(_feature_space_summary_file);
out_file_stream << std::setw(14) <<std::left << "# FEAT_ID" << "Feature Postfix Expression (RPN)" << std::endl;
sum_file_stream << std::setw(14) <<std::left << "# FEAT_ID" << std::setw(24) << std::left << "Score" << "Feature Expression" << std::endl;
out_file_stream.close();
sum_file_stream.close();
}
initialize_fs_output_files();
project_funcs::set_project_fxn(project_type, _task_sizes.size(), _project, _project_no_omp);
comp_feats::set_is_valid_fxn(project_type, _cross_cor_max, _n_samp, _is_valid, _is_valid_feat_list);
set_op_lists();
if(project_type.compare("regression") == 0)
{
_project = project_funcs::project_r2;
_project_no_omp = project_funcs::project_r2_no_omp;
}
else if(project_type.compare("classification") == 0)
{
_project = project_funcs::project_classify;
_project_no_omp = project_funcs::project_classify_no_omp;
}
else if(project_type.compare("log_regression") == 0)
{
if(_task_sizes.size() > 1)
throw std::logic_error("Log Regression can not be done using multiple tasks.");
_project = project_funcs::project_log_r2;
_project_no_omp = project_funcs::project_log_r2_no_omp;
}
else
throw std::logic_error("Wrong projection type passed to FeatureSpace constructor.");
double start = omp_get_wtime();
generate_feature_space(prop);
_mpi_comm->barrier();
double duration = omp_get_wtime() - start;
if(_mpi_comm->rank() == 0)
std::cout << "time to generate feat sapce: " << duration << " s" << std::endl;
if(_cross_cor_max < 0.99999)
{
_is_valid = comp_feats::valid_feature_against_selected;
_is_valid_feat_list = comp_feats::valid_feature_against_selected_feat_list;
}
else
{
_is_valid = comp_feats::valid_feature_against_selected_max_corr_1;
_is_valid_feat_list = comp_feats::valid_feature_against_selected_max_corr_1_feat_list;
}
_scores.reserve(_phi.size());
_scores.resize(_phi.size());
}
void FeatureSpace::set_op_lists()
{
for(auto & op : _allowed_ops)
{
if((op.compare("add") == 0) || (op.compare("mult") == 0) || (op.compare("abs_diff") == 0) || (op.compare("sub") == 0))
......@@ -119,15 +94,21 @@ void FeatureSpace::initialize_fs(std::vector<double> prop, std::string project_t
else
_un_operators.push_back(allowed_op_maps::unary_operator_map[op]);
}
double start = omp_get_wtime();
generate_feature_space(prop);
_mpi_comm->barrier();
double duration = omp_get_wtime() - start;
if(_mpi_comm->rank() == 0)
std::cout << "time to generate feat sapce: " << duration << " s" << std::endl;
}
_scores.reserve(_phi.size());
_scores.resize(_phi.size());
void FeatureSpace::initialize_fs_output_files()
{
if(_mpi_comm->rank() == 0)
{
std::ofstream out_file_stream = std::ofstream();
std::ofstream sum_file_stream = std::ofstream();
out_file_stream.open(_feature_space_file);
sum_file_stream.open(_feature_space_summary_file);
out_file_stream << std::setw(14) <<std::left << "# FEAT_ID" << "Feature Postfix Expression (RPN)" << std::endl;
sum_file_stream << std::setw(14) <<std::left << "# FEAT_ID" << std::setw(24) << std::left << "Score" << "Feature Expression" << std::endl;
out_file_stream.close();
sum_file_stream.close();
}
}
void FeatureSpace::generate_new_feats(std::vector<node_ptr>::iterator& feat, std::vector<node_ptr>& feat_set, int& feat_ind, double l_bound, double u_bound)
......
......@@ -115,6 +115,15 @@ public:
*/
void initialize_fs(std::vector<double> prop, std::string project_type);
/**
* @brief Uses _allowed_ops to set the operator lists
*/
void set_op_lists();
/**
* @brief Initializes the output files for SIS
*/
void initialize_fs_output_files();
/**
* @brief Generate the full feature set from the allowed operators and initial feature set
* @details populates phi with all features from an initial set and the allowed operators
......
......@@ -96,38 +96,8 @@ FeatureSpace::FeatureSpace(
{
_n_samp = _phi_0[0]->n_samp();
if(project_type.compare("regression") == 0)
{
_project = project_funcs::project_r2;
_project_no_omp = project_funcs::project_r2_no_omp;
}
else if(project_type.compare("classification") == 0)
{
_project = project_funcs::project_classify;
_project_no_omp = project_funcs::project_classify_no_omp;
}
else if(project_type.compare("log_regression") == 0)
{
if(_task_sizes.size() > 1)
throw std::logic_error("Log Regression can not be done using multiple tasks.");
_project = project_funcs::project_log_r2;
_project_no_omp = project_funcs::project_log_r2_no_omp;
}
else
{
throw std::logic_error("Wrong projection type passed to FeatureSpace constructor.");
}
if(_cross_cor_max < 0.99999)
{
_is_valid = comp_feats::valid_feature_against_selected;
_is_valid_feat_list = comp_feats::valid_feature_against_selected_feat_list;
}
else
{
_is_valid = comp_feats::valid_feature_against_selected_max_corr_1;
_is_valid_feat_list = comp_feats::valid_feature_against_selected_max_corr_1_feat_list;
}
project_funcs::set_project_fxn(project_type, _task_sizes.size(), _project, _project_no_omp);
comp_feats::set_is_valid_fxn(project_type, _cross_cor_max, _n_samp, _is_valid, _is_valid_feat_list);
std::vector<node_ptr> phi_temp = str2node::phi_from_file(feature_file, _phi_0);
......@@ -146,15 +116,8 @@ FeatureSpace::FeatureSpace(
_allowed_ops.push_back(nt);
}
for(auto & op : _allowed_ops)
{
if((op.compare("add") == 0) || (op.compare("sub") == 0) || (op.compare("mult") == 0) || (op.compare("abs_diff") == 0))
_com_bin_operators.push_back(allowed_op_maps::binary_operator_map[op]);
else if((op.compare("div") == 0))
_bin_operators.push_back(allowed_op_maps::binary_operator_map[op]);
else
_un_operators.push_back(allowed_op_maps::unary_operator_map[op]);
}
initialize_fs_output_files();
set_op_lists();
std::vector<int> rung_inds = util_funcs::argsort(rungs);
_max_phi = *std::max_element(rungs.begin(), rungs.end());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment