Commit 91ddd24c authored by Thomas Purcell's avatar Thomas Purcell
Browse files

Test the new phi output

Testing the phi ouput scheme
parent de2ee8fa
......@@ -154,6 +154,13 @@ FeatureSpace::FeatureSpace(InputParser inputs):
double start = omp_get_wtime();
generate_feature_space(_phi, _start_rung, _prop_train);
_n_feat = _phi.size();
if(_phi_out_file.size() > 0)
{
output_phi();
}
_mpi_comm->barrier();
double duration = omp_get_wtime() - start;
if(_mpi_comm->rank() == 0)
......@@ -779,15 +786,6 @@ void FeatureSpace::generate_feature_space(
}
#endif
}
if(!reparam)
{
_n_feat = feat_set.size();
}
if(_phi_out_file.size() > 0)
{
output_phi();
}
}
void FeatureSpace::generate_and_project(std::shared_ptr<LossFunction> loss, std::vector<node_ptr>& phi_sel, std::vector<double>& scores_sel)
......@@ -1213,6 +1211,7 @@ void FeatureSpace::remove_feature(const int ind)
}
_phi.erase(_phi.begin() + ind);
--_n_feat;
}
void FeatureSpace::output_phi()
......
......@@ -95,12 +95,6 @@ CbrtNode::CbrtNode(const node_ptr feat, const unsigned long int feat_ind, const
throw InvalidFeatureException();
}
double* val_ptr = feat->value_ptr(rung() + 2);
if(*std::min_element(val_ptr, val_ptr + _n_samp) < 0.0)
{
throw InvalidFeatureException();
}
set_value();
// Check if the feature is NaN, greater than the allowed max of less than the allowed min
......
......@@ -63,7 +63,7 @@ void generateDivNode(std::vector<node_ptr>& feat_list, const node_ptr feat_1, co
double div_mult_tot_first = div_mult_leaves.begin()->second;
if(
(std::abs(div_mult_tot_first) != 1.0) &&
std::all_of(div_mult_leaves.begin(), div_mult_leaves.end(), [&div_mult_tot_first](auto el){return el.second == div_mult_tot_first;})
std::all_of(div_mult_leaves.begin(), div_mult_leaves.end(), [&div_mult_tot_first](auto el){return std::abs(el.second) == div_mult_tot_first;})
)
{
return;
......
......@@ -391,7 +391,9 @@ std::vector<node_ptr> str2node::phi_from_file(const std::string filename, const
std::ifstream file_stream;
file_stream.open(filename, std::ios::in);
int numLines = 0;
int num_lines = 0;
int n_prim_feat = 0;
int n_invalid_feat = 0;
std::string line;
std::vector<node_ptr> phi;
......@@ -399,7 +401,7 @@ std::vector<node_ptr> str2node::phi_from_file(const std::string filename, const
while(std::getline(file_stream, line))
{
++numLines;
++num_lines;
if(line[0] == '#')
{
continue;
......@@ -408,18 +410,22 @@ std::vector<node_ptr> str2node::phi_from_file(const std::string filename, const
{
node_ptr feat = postfix2node(line, phi_0, feat_ind);
if(feat->type() == NODE_TYPE::FEAT)
{
++n_prim_feat;
continue;
}
phi.push_back(feat);
phi.back()->set_selected(false);
++feat_ind;
}
catch(const InvalidFeatureException& e)
{
++n_invalid_feat;
// Do Nothing
}
}
file_stream.close();
if(numLines < 1)
if(num_lines < 1)
{
throw std::logic_error("File, " + filename + " not present");
}
......
......@@ -231,6 +231,7 @@ void sisso::registerInputs()
.add_property("fix_intercept", &InputParser::fix_intercept, &InputParser::set_fix_intercept, "@DocString_inputs_fix_intercept@")
.add_property("global_param_opt", &InputParser::global_param_opt, &InputParser::set_global_param_opt, "@DocString_inputs_global_param_opt@")
.add_property("reparam_residual", &InputParser::reparam_residual, &InputParser::set_reparam_residual, "@DocString_inputs_reparam_residual@")
.add_property("phi_out_file", &InputParser::phi_out_file, &InputParser::set_phi_out_file, "@DocString_inputs_phi_out_file@")
;
}
......@@ -285,6 +286,7 @@ void sisso::feature_creation::registerFeatureSpace()
.def("feat_in_phi", &FeatureSpace::feat_in_phi, (arg("self"), arg("ind")), "@DocString_feat_space_feat_in_phi@")
.def("remove_feature", &FeatureSpace::remove_feature, (arg("self"), arg("ind")), "@DocString_feat_space_remove_feature@")
.def("get_feature", &FeatureSpace::get_feature, (arg("self"), arg("ind")), "@DocString_feat_space_get_feature@")
.def("output_phi", &FeatureSpace::output_phi, (arg("self")), "@DocString_feat_space_output_phi@")
.add_property("phi_selected", &FeatureSpace::phi_selected_py, "@DocString_feat_space_phi_selected_py@")
.add_property("phi0", &FeatureSpace::phi0_py, "@DocString_feat_space_phi0_py@")
.add_property("phi", &FeatureSpace::phi_py, "@DocString_feat_space_phi_py@")
......
......@@ -56,6 +56,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();
......
This diff is collapsed.
......@@ -11,5 +11,5 @@
"n_models_store": 1,
"leave_out_inds": [0, 1, 2, 60, 61],
"fix_intercept": false,
"phi_out_file": "feature_space/phi.out"
"phi_out_file": "feature_space/phi.txt"
}
......@@ -22,6 +22,10 @@ from sissopp import (
initialize_values_arr,
)
import pathlib
parent = pathlib.Path(__file__).parent.absolute()
def test_feature_space():
task_sizes_train = [90]
......@@ -49,6 +53,7 @@ def test_feature_space():
inputs.calc_type = "regression"
inputs.max_rung = 2
inputs.n_sis_select = 10
inputs.phi_out_file = "feature_space/phi.txt"
try:
inputs.n_rung_generate = 2
......@@ -81,8 +86,6 @@ def test_feature_space():
feat_space = FeatureSpace(inputs)
feat_space.sis(inputs.prop_train)
shutil.rmtree("feature_space/")
assert feat_space.phi_selected[0].postfix_expr == "1|0|add|sq"
feat_space.sis(list(inputs.prop_train))
......@@ -123,6 +126,26 @@ def test_feature_space():
inputs.max_param_depth = 0
pass
feat_space.output_phi()
feat_space_2 = FeatureSpace(
f"{parent}/feature_space/phi.txt",
inputs.phi_0,
inputs.prop_train,
inputs.task_sizes_train,
inputs.calc_type,
inputs.n_sis_select,
inputs.cross_cor_max,
)
# shutil.rmtree("feature_space/")
assert feat_space.n_feat == feat_space_2.n_feat
feat_space_2.sis(inputs.prop_train)
assert (
feat_space.phi_selected[0].postfix_expr
== feat_space_2.phi_selected[0].postfix_expr
)
if __name__ == "__main__":
test_feature_space()
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