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

Update FeatureSpace tests

More tests for cases where FeatureSpace should fail
parent 8c31b715
{
"desc_dim": 2,
"n_sis_select": 5,
"max_rung": 1,
"max_rung": 2,
"n_residual": 5,
"data_file": "data.csv",
"data_file_relatice_to_json": true,
......
......@@ -31,7 +31,7 @@ namespace
std::vector<int> task_sizes = {5, 5};
int n_samp = std::accumulate(task_sizes.begin(), task_sizes.end(), 0);
node_value_arrs::initialize_values_arr(task_sizes, {0, 0}, 3, 2, false);
node_value_arrs::initialize_values_arr(task_sizes, {0, 0}, 4, 2, false);
node_value_arrs::initialize_d_matrix_arr();
std::vector<double> value_1(n_samp, 0.0);
......@@ -61,6 +61,7 @@ namespace
FeatureNode feat_1(0, "A", value_1, std::vector<double>(), Unit("m"));
FeatureNode feat_2(1, "B", value_2, std::vector<double>(), Unit("m"));
FeatureNode feat_3(2, "C", value_3, std::vector<double>(), Unit("s"));
FeatureNode feat_4(3, "D", std::vector<double>(10, 1.0), std::vector<double>(), Unit("s"));
std::vector<FeatureNode> phi_0 = {feat_1, feat_2, feat_3};
......@@ -232,4 +233,15 @@ namespace
boost::filesystem::remove_all("feature_space/");
prop_sorted_d_mat::finalize_sorted_d_matrix_arr();
}
TEST_F(FeatSpaceTest, CheckFailedSISTest)
{
_inputs.set_calc_type("regression");
_inputs.set_prop_train(_prop);
_inputs.set_n_sis_select(1000000);
FeatureSpace feat_space(_inputs);
EXPECT_THROW(feat_space.sis(_prop), std::logic_error);
}
}
......@@ -150,12 +150,12 @@ namespace
std::vector<double> expected_val(900, 0.0);
allowed_op_funcs::sixth_pow(900, _phi[0]->value_ptr(), copy_test->parameters()[0], copy_test->parameters()[1], expected_val.data());
EXPECT_LT(std::abs(copy_test->value_ptr()[0] - expected_val[0]), 1e-10);
EXPECT_LT(std::abs(copy_test->value()[0] - expected_val[0]), 1e-10);
EXPECT_LT(std::abs(copy_test->value_ptr()[0] - expected_val[0]), 1e-4);
EXPECT_LT(std::abs(copy_test->value()[0] - expected_val[0]), 1e-4);
allowed_op_funcs::sixth_pow(10, _phi[0]->test_value_ptr(), copy_test->parameters()[0], copy_test->parameters()[1], expected_val.data());
EXPECT_LT(std::abs(copy_test->test_value_ptr()[0] - expected_val[0]), 1e-10);
EXPECT_LT(std::abs(copy_test->test_value()[0] - expected_val[0]), 1e-10);
EXPECT_LT(std::abs(copy_test->test_value_ptr()[0] - expected_val[0]), 1e-4);
EXPECT_LT(std::abs(copy_test->test_value()[0] - expected_val[0]), 1e-4);
std::stringstream postfix;
postfix << "0|sp: " << std::setprecision(13) << std::scientific <<copy_test->parameters()[0] << ',' << copy_test->parameters()[1];
......@@ -169,8 +169,8 @@ namespace
double a = copy_test->parameters()[1];
double df_dp = 6.0 * std::pow(alpha * v1 + a, 5.0);
EXPECT_LT(std::abs(_gradient[0] - df_dp * v1), 1e-10);
EXPECT_LT(std::abs(_gradient[_task_sizes_train[0]] - df_dp), 1e-10);
EXPECT_LT(std::abs(_gradient[0] - df_dp * v1), 1e-4);
EXPECT_LT(std::abs(_gradient[_task_sizes_train[0]] - df_dp), 1e-4);
}
TEST_F(SixPowParamNodeTest, AttributesTest)
......@@ -183,12 +183,12 @@ namespace
std::vector<double> expected_val(900, 0.0);
allowed_op_funcs::sixth_pow(900, _phi[0]->value_ptr(), _six_pow_test->parameters()[0], _six_pow_test->parameters()[1], expected_val.data());
EXPECT_LT(std::abs(_six_pow_test->value_ptr()[0] - expected_val[0]), 1e-10);
EXPECT_LT(std::abs(_six_pow_test->value()[0] - expected_val[0]), 1e-10);
EXPECT_LT(std::abs(_six_pow_test->value_ptr()[0] - expected_val[0]), 1e-4);
EXPECT_LT(std::abs(_six_pow_test->value()[0] - expected_val[0]), 1e-4);
allowed_op_funcs::sixth_pow(10, _phi[0]->test_value_ptr(), _six_pow_test->parameters()[0], _six_pow_test->parameters()[1], expected_val.data());
EXPECT_LT(std::abs(_six_pow_test->test_value_ptr()[0] - expected_val[0]), 1e-10);
EXPECT_LT(std::abs(_six_pow_test->test_value()[0] - expected_val[0]), 1e-10);
EXPECT_LT(std::abs(_six_pow_test->test_value_ptr()[0] - expected_val[0]), 1e-4);
EXPECT_LT(std::abs(_six_pow_test->test_value()[0] - expected_val[0]), 1e-4);
std::stringstream postfix;
postfix << "0|sp: " << std::setprecision(13) << std::scientific <<_six_pow_test->parameters()[0] << ',' << _six_pow_test->parameters()[1];
......
......@@ -36,7 +36,7 @@ def test_feature_space():
f"feat_{ff}",
np.random.random(task_sizes_train[0]) * 1e2 - 50,
np.random.random(task_sizes_test[0]) * 1e2 - 50,
Unit(),
Unit("s"),
)
for ff in range(10)
]
......@@ -50,6 +50,42 @@ def test_feature_space():
inputs.max_rung = 2
inputs.n_sis_select = 10
try:
inputs.max_param_depth = 10
feat_space = FeatureSpace(inputs)
raise ValueError("FeatureSpace created with invalid parameters")
except RuntimeError:
inputs.max_param_depth = 0
pass
try:
inputs.n_rung_generate = 2
feat_space = FeatureSpace(inputs)
raise ValueError("FeatureSpace created with invalid parameters")
except RuntimeError:
inputs.n_rung_generate = 0
pass
try:
inputs.n_rung_generate = 1
inputs.n_rung_store = 2
feat_space = FeatureSpace(inputs)
raise ValueError("FeatureSpace created with invalid parameters")
except RuntimeError:
inputs.n_rung_generate = 0
inputs.n_rung_store = 1
pass
try:
inputs.allowed_ops = ["exp"]
feat_space = FeatureSpace(inputs)
raise ValueError(
"FeatureSpace created when there is a rung with no features created"
)
except RuntimeError:
inputs.allowed_ops = ["add", "sub", "mult", "sq", "cb", "sqrt", "cbrt"]
pass
feat_space = FeatureSpace(inputs)
feat_space.sis(inputs.prop_train)
......@@ -76,6 +112,12 @@ def test_feature_space():
assert feat_space.get_feature(0).expr == "feat_0"
assert feat_space.phi_selected[1].d_mat_ind == 1
try:
feat_space.remove_feature(feat_space.phi_selected[0].feat_ind)
raise ValueError("Removed selected feature.")
except RuntimeError:
pass
test_expr = feat_space.get_feature(len(feat_space.phi) - 2).expr
feat_space.remove_feature(len(feat_space.phi) - 2)
assert feat_space.get_feature(len(feat_space.phi) - 2).expr != test_expr
......
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