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

Update Tests to cover more of a Node function

parent e9184b1c
......@@ -346,7 +346,6 @@ public:
{
throw std::logic_error("Can't get a child node from a FeatureNode.");
}
return nullptr;
}
/**
......
......@@ -900,7 +900,9 @@ double ModelNode::eval(double* x_in)
}
}
if(stack.size() != 1)
{
throw std::logic_error("The final stack size is not one, something wrong happened during the calculation.");
}
return *stack[0];
}
......@@ -923,11 +925,6 @@ double ModelNode::eval(std::map<std::string, double> x_in_dct)
{
throw std::logic_error("The value of " + in_expr + " is not in x_in_dct.");
}
else if(x_in_dct.count(in_expr) > 1)
{
throw std::logic_error("Multiple values of " + in_expr + " defined in x_in_dct.");
}
x_in.push_back(x_in_dct[in_expr]);
}
return eval(x_in.data());
......@@ -995,10 +992,6 @@ std::vector<double> ModelNode::eval(std::map<std::string, std::vector<double>> x
{
throw std::logic_error("The value of " + in_expr + " is not in x_in_dct.");
}
else if(x_in_dct.count(in_expr) > 1)
{
throw std::logic_error("Multiple values of " + in_expr + " defined in x_in_dct.");
}
x_in.push_back(x_in_dct[in_expr]);
}
......
......@@ -317,7 +317,6 @@ public:
inline double* value_ptr(int offset=-1, const bool for_comp=false) const
{
throw std::logic_error("const version of value_ptr for ModelNode is impossible.");
return nullptr;
}
/**
......@@ -329,7 +328,6 @@ public:
inline double* test_value_ptr(int offset=-1, const bool for_comp=false) const
{
throw std::logic_error("const version of test_value_ptr for ModelNode is impossible.");
return nullptr;
}
// DocString: model_node_rung
......
......@@ -418,7 +418,6 @@ public:
virtual inline const double* param_pointer() const
{
throw std::logic_error("Trying to access the parameter pointer to a node with no parameters.");
return nullptr;
}
/**
......
......@@ -589,7 +589,7 @@ public:
*/
void gradient(double* grad, double* dfdp) const
{
if(n_params_possible() == 0)
if(n_params() == 0)
{
throw std::logic_error("Asking for the gradient of non-parameterized feature");
}
......
......@@ -316,8 +316,9 @@ public:
*/
inline void param_derivative(const double* params, double* dfdp) const
{
double* val_ptr_1 = _feats[0]->value_ptr(params, 2);
double* val_ptr_2 = _feats[1]->value_ptr(params, 1);
double* val_ptr_1 = _feats[0]->value_ptr(params + _feats[1]->n_params_possible() + 2, 2);
double* val_ptr_2 = _feats[1]->value_ptr(params + 2, 1);
std::transform(
val_ptr_1,
val_ptr_1 + _n_samp,
......
......@@ -22,70 +22,9 @@
#include "feature_creation/node/FeatureNode.hpp"
FeatureNode::FeatureNode(unsigned long int feat_ind, std::string expr, np::ndarray value, np::ndarray test_value, Unit unit) :
Node(feat_ind, value.shape(0), test_value.shape(0)),
_value(python_conv_utils::from_ndarray<double>(value)),
_test_value(python_conv_utils::from_ndarray<double>(test_value)),
_unit(unit),
_expr(expr)
{
// Automatically resize the storage arrays
if(node_value_arrs::N_STORE_FEATURES == 0)
{
node_value_arrs::initialize_values_arr(_n_samp, _n_samp_test, 1);
}
else if((_n_samp != node_value_arrs::N_SAMPLES) || (_n_samp_test != node_value_arrs::N_SAMPLES_TEST))
{
throw std::logic_error(
"Number of samples in current feature is not the same as the others, (" +
std::to_string(_n_samp) +
" and " + std::to_string(_n_samp_test) +
" vs. " +
std::to_string(node_value_arrs::N_SAMPLES) +
" and " +
std::to_string(node_value_arrs::N_SAMPLES_TEST) +
")"
);
}
else if(feat_ind >= node_value_arrs::N_STORE_FEATURES)
{
node_value_arrs::resize_values_arr(0, node_value_arrs::N_STORE_FEATURES + 1);
}
set_value();
set_test_value();
}
FeatureNode(feat_ind, expr, python_conv_utils::from_ndarray<double>(value), python_conv_utils::from_ndarray<double>(test_value), unit)
{}
FeatureNode::FeatureNode(unsigned long int feat_ind, std::string expr, py::list value, py::list test_value, Unit unit) :
Node(feat_ind, py::len(value), py::len(test_value)),
_value(python_conv_utils::from_list<double>(value)),
_test_value(python_conv_utils::from_list<double>(test_value)),
_unit(unit),
_expr(expr)
{
// Automatically resize the storage arrays
if(node_value_arrs::N_STORE_FEATURES == 0)
{
node_value_arrs::initialize_values_arr(_n_samp, _n_samp_test, 1);
}
else if((_n_samp != node_value_arrs::N_SAMPLES) || (_n_samp_test != node_value_arrs::N_SAMPLES_TEST))
{
throw std::logic_error(
"Number of samples in current feature is not the same as the others, (" +
std::to_string(_n_samp) +
" and " +
std::to_string(_n_samp_test) +
" vs. " +
std::to_string(node_value_arrs::N_SAMPLES) +
" and " +
std::to_string(node_value_arrs::N_SAMPLES_TEST) +
")"
);
}
else if(feat_ind >= node_value_arrs::N_STORE_FEATURES)
{
node_value_arrs::resize_values_arr(0, node_value_arrs::N_STORE_FEATURES + 1);
}
set_value();
set_test_value();
}
FeatureNode(feat_ind, expr, python_conv_utils::from_list<double>(value), python_conv_utils::from_list<double>(test_value), unit)
{}
,purcell,theobook,03.09.2021 15:57,file:///home/purcell/.config/libreoffice/4;
\ No newline at end of file
......@@ -25,8 +25,11 @@ namespace
protected:
void SetUp() override
{
#ifdef PARAMETERIZE
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, true);
#else
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, false);
#endif
std::vector<double> value_1 = {1.0, 2.0, 3.0, 4.0};
std::vector<double> test_value_1 = {5.0};
......@@ -180,8 +183,8 @@ namespace
TEST_F(AbsDiffNodeTest, AttributesTest)
{
_abs_diff_test = std::make_shared<AbsDiffNode>(_phi[0], _phi[1], 5, 1e-50, 1e50);
_abs_diff_test = std::make_shared<AbsDiffNode>(_abs_diff_test, _phi[1], 6, 1e-50, 1e50);
node_ptr feat_r1 = std::make_shared<AbsDiffNode>(_phi[0], _phi[1], 5, 1e-50, 1e50);
_abs_diff_test = std::make_shared<AbsDiffNode>(feat_r1, _phi[1], 6, 1e-50, 1e50);
EXPECT_EQ(_abs_diff_test->rung(), 2);
......@@ -195,5 +198,35 @@ namespace
EXPECT_STREQ(_abs_diff_test->expr().c_str(), "(|(|A - B|) - B|)");
EXPECT_STREQ(_abs_diff_test->postfix_expr().c_str(), "0|1|abd|1|abd");
#ifdef PARAMETERIZE
EXPECT_THROW(_abs_diff_test->param_pointer(), std::logic_error);
EXPECT_EQ(_abs_diff_test->n_params(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 0;
EXPECT_EQ(_abs_diff_test->n_params_possible(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 1;
EXPECT_EQ(_abs_diff_test->n_params_possible(), 2);
EXPECT_EQ(_abs_diff_test->parameters().size(), 0);
std::vector<double> params = {1.0, -2.0, 1.0, 0.0};
std::vector<double> dfdp(4, 0.0);
std::vector<double> grad(4, 0.0);
EXPECT_THROW(_abs_diff_test->gradient(grad.data(), dfdp.data()), std::logic_error);
_abs_diff_test->set_parameters({}, true);
_abs_diff_test->set_parameters(nullptr);
_abs_diff_test->param_derivative(params.data(), dfdp.data());
EXPECT_EQ(dfdp[0], -1.0);
EXPECT_EQ(dfdp[1], 0.0);
EXPECT_EQ(dfdp[2], 1.0);
EXPECT_EQ(dfdp[3], 1.0);
EXPECT_EQ(util_funcs::norm(grad), 0.0);
#endif
}
}
......@@ -24,7 +24,11 @@ namespace
protected:
void SetUp() override
{
#ifdef PARAMETERIZE
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, true);
#else
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, false);
#endif
std::vector<double> value_1 = {-1.0, -2.0, -3.0, -4.0};
std::vector<double> test_value_1 = {50.0};
......@@ -182,5 +186,32 @@ namespace
EXPECT_STREQ(_abs_test->expr().c_str(), "(|A|)");
EXPECT_STREQ(_abs_test->postfix_expr().c_str(), "0|abs");
#ifdef PARAMETERIZE
EXPECT_THROW(_abs_test->param_pointer(), std::logic_error);
EXPECT_EQ(_abs_test->n_params(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 0;
EXPECT_EQ(_abs_test->n_params_possible(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 1;
EXPECT_EQ(_abs_test->n_params_possible(), 2);
EXPECT_EQ(_abs_test->parameters().size(), 0);
std::vector<double> params = {1.0, 2.0};
std::vector<double> dfdp(4, 0.0);
std::vector<double> grad(2, 0.0);
EXPECT_THROW(_abs_test->gradient(grad.data(), dfdp.data()), std::logic_error);
_abs_test->set_parameters({}, true);
_abs_test->set_parameters(nullptr);
_abs_test->param_derivative(params.data(), dfdp.data());
EXPECT_EQ(dfdp[0], 1.0);
EXPECT_EQ(dfdp[1], 0.0);
EXPECT_EQ(dfdp[2], -1.0);
EXPECT_EQ(dfdp[3], -1.0);
#endif
}
}
......@@ -24,7 +24,11 @@ namespace
protected:
void SetUp() override
{
#ifdef PARAMETERIZE
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, true);
#else
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, false);
#endif
std::vector<double> value_1 = {1.0, 2.0, 3.0, 4.0};
std::vector<double> test_value_1 = {5.0};
......@@ -194,5 +198,32 @@ namespace
EXPECT_STREQ(_add_test->expr().c_str(), "((A + B) + B)");
EXPECT_STREQ(_add_test->postfix_expr().c_str(), "0|1|add|1|add");
#ifdef PARAMETERIZE
EXPECT_THROW(_add_test->param_pointer(), std::logic_error);
EXPECT_EQ(_add_test->n_params(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 0;
EXPECT_EQ(_add_test->n_params_possible(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 1;
EXPECT_EQ(_add_test->n_params_possible(), 2);
EXPECT_EQ(_add_test->parameters().size(), 0);
std::vector<double> params = {1.0, 0.0};
std::vector<double> dfdp(4, 0.0);
std::vector<double> grad(4, 0.0);
EXPECT_THROW(_add_test->gradient(grad.data(), dfdp.data()), std::logic_error);
_add_test->set_parameters({}, true);
_add_test->set_parameters(nullptr);
_add_test->param_derivative(params.data(), dfdp.data());
EXPECT_EQ(dfdp[0], 1.0);
EXPECT_EQ(dfdp[1], 1.0);
EXPECT_EQ(dfdp[2], 1.0);
EXPECT_EQ(dfdp[3], 1.0);
#endif
}
}
......@@ -28,7 +28,11 @@ namespace
protected:
void SetUp() override
{
#ifdef PARAMETERIZE
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, true);
#else
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, false);
#endif
std::vector<double> value_1 = {1.0, 2.0, 3.0, 8.0};
std::vector<double> test_value_1 = {2.0};
......@@ -180,5 +184,32 @@ namespace
EXPECT_STREQ(_cb_test->expr().c_str(), "(A^3)");
EXPECT_STREQ(_cb_test->postfix_expr().c_str(), "0|cb");
#ifdef PARAMETERIZE
EXPECT_THROW(_cb_test->param_pointer(), std::logic_error);
EXPECT_EQ(_cb_test->n_params(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 0;
EXPECT_EQ(_cb_test->n_params_possible(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 1;
EXPECT_EQ(_cb_test->n_params_possible(), 2);
EXPECT_EQ(_cb_test->parameters().size(), 0);
std::vector<double> params = {1.0, 0.0};
std::vector<double> dfdp(4, 0.0);
std::vector<double> grad(2, 0.0);
EXPECT_THROW(_cb_test->gradient(grad.data(), dfdp.data()), std::logic_error);
_cb_test->set_parameters({}, true);
_cb_test->set_parameters(nullptr);
_cb_test->param_derivative(params.data(), dfdp.data());
EXPECT_EQ(dfdp[0], 3.0);
EXPECT_EQ(dfdp[1], 12.0);
EXPECT_EQ(dfdp[2], 27.0);
EXPECT_EQ(dfdp[3], 192.0);
#endif
}
}
......@@ -29,7 +29,11 @@ namespace
protected:
void SetUp() override
{
#ifdef PARAMETERIZE
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, true);
#else
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, false);
#endif
std::vector<double> value_1 = {1.0, 2.0, 3.0, 8.0};
std::vector<double> test_value_1 = {8.0};
......@@ -205,5 +209,29 @@ namespace
EXPECT_STREQ(_cbrt_test->expr().c_str(), "cbrt(A)");
EXPECT_STREQ(_cbrt_test->postfix_expr().c_str(), "0|cbrt");
#ifdef PARAMETERIZE
EXPECT_THROW(_cbrt_test->param_pointer(), std::logic_error);
EXPECT_EQ(_cbrt_test->n_params(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 0;
EXPECT_EQ(_cbrt_test->n_params_possible(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 1;
EXPECT_EQ(_cbrt_test->n_params_possible(), 2);
EXPECT_EQ(_cbrt_test->parameters().size(), 0);
std::vector<double> params = {1.0, 0.0};
std::vector<double> dfdp(4, 0.0);
std::vector<double> grad(2, 0.0);
EXPECT_THROW(_cbrt_test->gradient(grad.data(), dfdp.data()), std::logic_error);
_cbrt_test->set_parameters({}, true);
_cbrt_test->set_parameters(nullptr);
_cbrt_test->param_derivative(params.data(), dfdp.data());
EXPECT_LT(std::abs(dfdp[0] - 1/3.0), 1e-10);
EXPECT_LT(std::abs(dfdp[3] - 1/12.0), 1e-10);
#endif
}
}
......@@ -25,7 +25,11 @@ namespace
protected:
void SetUp() override
{
#ifdef PARAMETERIZE
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, true);
#else
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, false);
#endif
std::vector<double> value_1 = {0.0, 2.0, 3.0, 4.0};
std::vector<double> test_value_1 = {0.0};
......@@ -181,5 +185,32 @@ namespace
EXPECT_STREQ(_cos_test->expr().c_str(), "cos(A)");
EXPECT_STREQ(_cos_test->postfix_expr().c_str(), "0|cos");
#ifdef PARAMETERIZE
EXPECT_THROW(_cos_test->param_pointer(), std::logic_error);
EXPECT_EQ(_cos_test->n_params(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 0;
EXPECT_EQ(_cos_test->n_params_possible(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 1;
EXPECT_EQ(_cos_test->n_params_possible(), 2);
EXPECT_EQ(_cos_test->parameters().size(), 0);
std::vector<double> params = {M_PI / 2.0, 0.0};
std::vector<double> dfdp(4, 0.0);
std::vector<double> grad(2, 0.0);
EXPECT_THROW(_cos_test->gradient(grad.data(), dfdp.data()), std::logic_error);
_cos_test->set_parameters({}, true);
_cos_test->set_parameters(nullptr);
_cos_test->param_derivative(params.data(), dfdp.data());
EXPECT_LT(std::abs(dfdp[0] - 0.0), 1e-10);
EXPECT_LT(std::abs(dfdp[1] - 0.0), 1e-10);
EXPECT_LT(std::abs(dfdp[2] - 1.0), 1e-10);
EXPECT_LT(std::abs(dfdp[3] - 0.0), 1e-10);
#endif
}
}
......@@ -25,7 +25,11 @@ namespace
protected:
void SetUp() override
{
#ifdef PARAMETERIZE
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, true);
#else
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, false);
#endif
std::vector<double> value_1 = {1.0, 2.0, 3.0, 4.0};
std::vector<double> test_value_1 = {5.0};
......@@ -244,5 +248,34 @@ namespace
EXPECT_STREQ(_div_test->expr().c_str(), "((A / B) / B)");
EXPECT_STREQ(_div_test->postfix_expr().c_str(), "0|1|div|1|div");
#ifdef PARAMETERIZE
EXPECT_THROW(_div_test->param_pointer(), std::logic_error);
EXPECT_EQ(_div_test->n_params(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 0;
EXPECT_EQ(_div_test->n_params_possible(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 1;
EXPECT_EQ(_div_test->n_params_possible(), 2);
std::vector<double> params = {1.0, 0.0};
std::vector<double> dfdp(4, 0.0);
std::vector<double> grad(4, 0.0);
EXPECT_THROW(_div_test->gradient(grad.data(), dfdp.data()), std::logic_error);
EXPECT_EQ(_div_test->parameters().size(), 0);
_div_test->set_parameters({}, true);
_div_test->set_parameters(nullptr);
_div_test->param_derivative(params.data(), dfdp.data());
std::vector<double> value_1 = {1.0, 2.0, 3.0, 4.0};
std::vector<double> value_2 = {10.0, 25.0, 30.0, 40.0};
EXPECT_LT(std::abs(dfdp[0] + (1.0 / 10.0) / (10.0 * 10.0)), 1e-10);
EXPECT_LT(std::abs(dfdp[1] + (2.0 / 25.0) / (25.0 * 25.0)), 1e-10);
EXPECT_LT(std::abs(dfdp[2] + (3.0 / 30.0) / (30.0 * 30.0)), 1e-10);
EXPECT_LT(std::abs(dfdp[3] + (4.0 / 40.0) / (40.0 * 40.0)), 1e-10);
#endif
}
}
......@@ -29,7 +29,11 @@ namespace
protected:
void SetUp() override
{
node_value_arrs::initialize_values_arr({4}, {1}, 3, 2, false);
#ifdef PARAMETERIZE
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, true);
#else
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, false);
#endif
std::vector<double> value_1 = {0.0, 2.0, 3.0, 4.0};
std::vector<double> test_value_1 = {0.0};
......@@ -210,5 +214,32 @@ namespace
EXPECT_STREQ(_exp_test->expr().c_str(), "exp(A)");
EXPECT_STREQ(_exp_test->postfix_expr().c_str(), "0|exp");
#ifdef PARAMETERIZE
EXPECT_THROW(_exp_test->param_pointer(), std::logic_error);
EXPECT_EQ(_exp_test->n_params(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 0;
EXPECT_EQ(_exp_test->n_params_possible(), 0);
nlopt_wrapper::MAX_PARAM_DEPTH = 1;
EXPECT_EQ(_exp_test->n_params_possible(), 2);
EXPECT_EQ(_exp_test->parameters().size(), 0);
std::vector<double> params = {1.0, 0.0};
std::vector<double> dfdp(4, 0.0);
std::vector<double> grad(2, 0.0);
EXPECT_THROW(_exp_test->gradient(grad.data(), dfdp.data()), std::logic_error);
_exp_test->set_parameters({}, true);
_exp_test->set_parameters(nullptr);
_exp_test->param_derivative(params.data(), dfdp.data());
EXPECT_LT(std::abs(dfdp[0] - _exp_test->value()[0]), 1e-10);
EXPECT_LT(std::abs(dfdp[1] - _exp_test->value()[1]), 1e-10);
EXPECT_LT(std::abs(dfdp[2] - _exp_test->value()[2]), 1e-10);
EXPECT_LT(std::abs(dfdp[3] - _exp_test->value()[3]), 1e-10);
#endif
}
}
......@@ -22,7 +22,7 @@ namespace
protected:
void SetUp() override
{
node_value_arrs::initialize_values_arr({4}, {1}, 3, 0, false);
node_value_arrs::initialize_values_arr({4}, {1}, 4, 2, false);
_value_1 = {1.0, 2.0, 3.0, 4.0};
_test_value_1 = {5.0};
......@@ -32,6 +32,9 @@ namespace
_value_3 = {1.0, 2.0, 3.0, 1.0};
_test_value_3 = {5.0};
_value_4 = {1.0, 2.0, 3.0};
_test_value_4 = {};
}
void TearDown() override
......@@ -47,6 +50,9 @@ namespace
std::vector<double> _value_3;
std::vector<double> _test_value_3;
std::vector<double> _value_4;