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

Clean up code of the Node objects

parent eac6b4d0
......@@ -39,9 +39,13 @@ bool FeatureNode::is_const()
void FeatureNode::update_add_sub_leaves(std::map<std::string, int>& add_sub_leaves, int pl_mn, int& expected_abs_tot)
{
if(add_sub_leaves.count(_expr) > 0)
{
add_sub_leaves[_expr] += pl_mn;
}
else
{
add_sub_leaves[_expr] = pl_mn;
}
++expected_abs_tot;
}
......@@ -49,9 +53,13 @@ void FeatureNode::update_add_sub_leaves(std::map<std::string, int>& add_sub_leav
void FeatureNode::update_div_mult_leaves(std::map<std::string, double>& div_mult_leaves, double fact, double& expected_abs_tot)
{
if(div_mult_leaves.count(_expr) > 0)
{
div_mult_leaves[_expr] += fact;
}
else
{
div_mult_leaves[_expr] = fact;
}
expected_abs_tot += std::abs(fact);
}
......@@ -66,9 +74,13 @@ std::map<int, int> FeatureNode::primary_feature_decomp()
void FeatureNode::update_primary_feature_decomp(std::map<int, int>& pf_decomp)
{
if(pf_decomp.count(_arr_ind) > 0)
{
pf_decomp[_arr_ind] += 1;
}
else
{
pf_decomp[_arr_ind] = 1;
}
}
// BOOST_CLASS_EXPORT(FeatureNode)
......@@ -21,8 +21,8 @@
#include <feature_creation/node/Node.hpp>
#ifdef PY_BINDINGS
namespace np = boost::python::numpy;
namespace py = boost::python;
namespace np = boost::python::numpy;
namespace py = boost::python;
#endif
// DocString: cls_feat_node
......@@ -72,27 +72,27 @@ public:
*/
FeatureNode(unsigned long int feat_ind, std::string expr, std::vector<double> value, std::vector<double> test_value, Unit unit, bool set_val = true);
#ifdef PY_BINDINGS
/**
* @brief Constructs a feature node using numpy arrays (cpp definition in <python/feature_creation/FeatureNode.cpp)
*
* @param feat_ind index of the feature
* @param expr Expression for the feature
* @param value Value of the feature for each sample
* @param value Value of the feature for each test sample
* @param unit Unit of the feature
*/
FeatureNode(unsigned long int feat_ind, std::string expr, np::ndarray value, np::ndarray test_value, Unit unit);
/**
* @brief Constructs a feature node using Python lists (cpp definition in <python/feature_creation/FeatureNode.cpp)
*
* @param feat_ind index of the feature
* @param expr Expression for the feature
* @param value Value of the feature for each sample
* @param value Value of the feature for each test sample
* @param unit Unit of the feature
*/
FeatureNode(unsigned long int feat_ind, std::string expr, py::list value, py::list test_value, Unit unit);
/**
* @brief Constructs a feature node using numpy arrays (cpp definition in <python/feature_creation/FeatureNode.cpp)
*
* @param feat_ind index of the feature
* @param expr Expression for the feature
* @param value Value of the feature for each sample
* @param value Value of the feature for each test sample
* @param unit Unit of the feature
*/
FeatureNode(unsigned long int feat_ind, std::string expr, np::ndarray value, np::ndarray test_value, Unit unit);
/**
* @brief Constructs a feature node using Python lists (cpp definition in <python/feature_creation/FeatureNode.cpp)
*
* @param feat_ind index of the feature
* @param expr Expression for the feature
* @param value Value of the feature for each sample
* @param value Value of the feature for each test sample
* @param unit Unit of the feature
*/
FeatureNode(unsigned long int feat_ind, std::string expr, py::list value, py::list test_value, Unit unit);
#endif
/**
......@@ -171,7 +171,10 @@ public:
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
*/
inline void set_value(int offset=-1, bool for_comp=false){std::copy_n(_value.data(), _n_samp, value_ptr());}
inline void set_value(int offset=-1, bool for_comp=false)
{
std::copy_n(_value.data(), _n_samp, value_ptr());
}
// DocString: feat_node_set_test_value
/**
......@@ -179,13 +182,19 @@ public:
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
*/
inline void set_test_value(int offset=-1, bool for_comp=false){if(!_selected) std::copy_n(_test_value.data(), _n_test_samp, test_value_ptr());}
inline void set_test_value(int offset=-1, bool for_comp=false)
{
if(!_selected) std::copy_n(_test_value.data(), _n_test_samp, test_value_ptr());
}
// DocString: feat_node_is_nan
/**
* @brief Check if the feature contains NaN
*/
inline bool is_nan(){return std::any_of(value_ptr(), value_ptr() + _n_samp, [](double d){return !std::isfinite(d);});}
inline bool is_nan()
{
return std::any_of(value_ptr(), value_ptr() + _n_samp, [](double d){return !std::isfinite(d);});
}
// DocString: feat_node_is_const
/**
......@@ -203,14 +212,20 @@ public:
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
*/
inline double* value_ptr(int offset=-1, bool for_comp=false){return _selected ? node_value_arrs::get_d_matrix_ptr(_d_mat_ind) : node_value_arrs::get_value_ptr(_arr_ind, _feat_ind, 0, offset, for_comp);}
inline double* value_ptr(int offset=-1, bool for_comp=false)
{
return _selected ? node_value_arrs::get_d_matrix_ptr(_d_mat_ind) : node_value_arrs::get_value_ptr(_arr_ind, _feat_ind, 0, offset, for_comp);
}
/**
* @brief The pointer to where the feature's test data is stored
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
*/
inline double* test_value_ptr(int offset=-1, bool for_comp=false){return node_value_arrs::get_test_value_ptr(_arr_ind, _feat_ind, 0, offset, for_comp);}
inline double* test_value_ptr(int offset=-1, bool for_comp=false)
{
return node_value_arrs::get_test_value_ptr(_arr_ind, _feat_ind, 0, offset, for_comp);
}
// DocString: feat_node_rung
/**
......@@ -241,7 +256,10 @@ public:
* @param cur_expr The current expression
* @return The current postfix expression of the feature
*/
inline void update_postfix(std::string& cur_expr, bool add_params=true){cur_expr = get_postfix_term() + "|" + cur_expr;};
inline void update_postfix(std::string& cur_expr, bool add_params=true)
{
cur_expr = get_postfix_term() + "|" + cur_expr;
}
/**
* @brief Get the three character representation of the operator
......@@ -266,7 +284,9 @@ public:
inline node_ptr feat(int ind)
{
if(ind > 0)
{
throw std::logic_error("Index not found in _feats");
}
return nullptr;
}
......@@ -288,107 +308,110 @@ public:
void update_div_mult_leaves(std::map<std::string, double>& div_mult_leaves, double fact, double& expected_abs_tot);
#ifdef PARAMETERIZE
/**
* @brief The parameters used for introducing more non linearity in the operators
*/
inline std::vector<double> parameters(){return std::vector<double>();};
/**
* @brief Set the non-linear parameters
*/
inline void set_parameters(std::vector<double> params, bool check_sz=true){};
/**
* @brief returns the number of parameters for this feature
* @return the number of parameters (_params.size())
*/
inline int n_params(int n_cur=0, int depth = 1){return n_cur;};
/**
* @brief Set the values of the training data for the feature inside of the value storage arrays
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
*/
inline void set_value(const double* params, int offset=-1, bool for_comp=false, int depth=0){set_value(offset);};
/**
* @brief The pointer to where the feature's training data is stored
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
* @returns the pointer to the feature's data
*/
inline double* value_ptr(const double* params, int offset=-1, bool for_comp=false, int depth=0){return value_ptr(offset);};
/**
* @brief Set the values of the test data for the feature inside of the value storage arrays
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
*/
inline void set_test_value(const double* params, int offset=-1, bool for_comp=false, int depth=0){set_test_value(offset);};
/**
* @brief The pointer to where the feature's test data is stored
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
* @returns the pointer to the feature's data
*/
inline double* test_value_ptr(const double* params, int offset=-1, bool for_comp=false, int depth=0){return test_value_ptr(offset);};
/**
* @brief The expression of the feature
*
* @param params parameter values for non-linear operations
* @param params parameter values for non-linear operations
* @return feature expression
*/
inline std::string expr(double* params, int depth = 1){return _expr;};
/**
* @brief Get the latexified expression for the overall feature (From root node down)
*
* @param cap string to place in the start/end of the string
* @param params parameter values for non-linear operations
* @param depth the current depth of the node on the Binary expression tree
* @return the latexified expression
*/
inline std::string get_latex_expr(double* params, int depth=1){return str_utils::latexify(_expr);}
/**
* @brief Set the bounds for the nl parameterization
*
* @param lb pointer to the lower bounds data
* @param ub pointer to the upper bounds data
*/
inline void set_bounds(double* lb, double* ub, int from_parent=2, int depth = 1){};
/**
* @brief Calculates the derivative of an operation with respect to the parameters for a given sample
*
* @param params pointer to the parameters
* @param samp_ind sample index number
*/
void param_derivative(const double* params, double* dfdp){}
/**
* @brief Get the parameter gradient for non-linear optimization
*
* @param grad pointer to the gradient storage
* @param samp_ind sample index to calculate the gradient for
*/
virtual void gradient(double* grad, double* dfdp){throw std::logic_error("Asking for the gradient of non-parameterized feature");}
/**
* @brief Get the parameter gradient for non-linear optimization
*
* @param grad pointer to the gradient storage
* @param samp_ind sample index to calculate the gradient for
* @param params pointer to the parameters vector
*/
inline void gradient(double* grad, double* dfdp, const double* params){};
/**
* @brief The parameters used for introducing more non linearity in the operators
*/
inline std::vector<double> parameters(){return std::vector<double>();};
/**
* @brief Set the non-linear parameters
*/
inline void set_parameters(std::vector<double> params, bool check_sz=true){};
/**
* @brief returns the number of parameters for this feature
* @return the number of parameters (_params.size())
*/
inline int n_params(int n_cur=0, int depth = 1){return n_cur;};
/**
* @brief Set the values of the training data for the feature inside of the value storage arrays
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
*/
inline void set_value(const double* params, int offset=-1, bool for_comp=false, int depth=0){set_value(offset);};
/**
* @brief The pointer to where the feature's training data is stored
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
* @returns the pointer to the feature's data
*/
inline double* value_ptr(const double* params, int offset=-1, bool for_comp=false, int depth=0){return value_ptr(offset);};
/**
* @brief Set the values of the test data for the feature inside of the value storage arrays
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
*/
inline void set_test_value(const double* params, int offset=-1, bool for_comp=false, int depth=0){set_test_value(offset);};
/**
* @brief The pointer to where the feature's test data is stored
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
* @returns the pointer to the feature's data
*/
inline double* test_value_ptr(const double* params, int offset=-1, bool for_comp=false, int depth=0){return test_value_ptr(offset);};
/**
* @brief The expression of the feature
*
* @param params parameter values for non-linear operations
* @param params parameter values for non-linear operations
* @return feature expression
*/
inline std::string expr(double* params, int depth = 1){return _expr;};
/**
* @brief Get the latexified expression for the overall feature (From root node down)
*
* @param cap string to place in the start/end of the string
* @param params parameter values for non-linear operations
* @param depth the current depth of the node on the Binary expression tree
* @return the latexified expression
*/
inline std::string get_latex_expr(double* params, int depth=1){return str_utils::latexify(_expr);}
/**
* @brief Set the bounds for the nl parameterization
*
* @param lb pointer to the lower bounds data
* @param ub pointer to the upper bounds data
*/
inline void set_bounds(double* lb, double* ub, int from_parent=2, int depth = 1){};
/**
* @brief Calculates the derivative of an operation with respect to the parameters for a given sample
*
* @param params pointer to the parameters
* @param samp_ind sample index number
*/
void param_derivative(const double* params, double* dfdp){}
/**
* @brief Get the parameter gradient for non-linear optimization
*
* @param grad pointer to the gradient storage
* @param samp_ind sample index to calculate the gradient for
*/
virtual void gradient(double* grad, double* dfdp)
{
throw std::logic_error("Asking for the gradient of non-parameterized feature");
}
/**
* @brief Get the parameter gradient for non-linear optimization
*
* @param grad pointer to the gradient storage
* @param samp_ind sample index to calculate the gradient for
* @param params pointer to the parameters vector
*/
inline void gradient(double* grad, double* dfdp, const double* params){};
#endif
};
......
This diff is collapsed.
......@@ -253,14 +253,14 @@ public:
void update_div_mult_leaves(std::map<std::string, double>& div_mult_leaves, double fact, double& expected_abs_tot);
#ifdef PY_BINDINGS
inline double eval_py(np::ndarray x_in){return eval(python_conv_utils::from_ndarray<double>(x_in));}
inline double eval_py(py::list x_in){return eval(python_conv_utils::from_list<double>(x_in));}
inline double eval_py(py::dict x_in){return eval(python_conv_utils::from_dict<std::string, double>(x_in));}
inline double eval_py(np::ndarray x_in){return eval(python_conv_utils::from_ndarray<double>(x_in));}
inline double eval_py(py::list x_in){return eval(python_conv_utils::from_list<double>(x_in));}
inline double eval_py(py::dict x_in){return eval(python_conv_utils::from_dict<std::string, double>(x_in));}
np::ndarray eval_many_py(np::ndarray x_in);
np::ndarray eval_many_py(py::dict x_in);
np::ndarray eval_many_py(np::ndarray x_in);
np::ndarray eval_many_py(py::dict x_in);
inline py::list x_in_expr_list_py(){return python_conv_utils::to_list<std::string>(_x_in_expr_list);}
inline py::list x_in_expr_list_py(){return python_conv_utils::to_list<std::string>(_x_in_expr_list);}
#endif
};
......
......@@ -13,9 +13,6 @@
#include <feature_creation/node/value_storage/nodes_value_containers.hpp>
#include <feature_creation/units/Unit.hpp>
#ifdef PY_BINDINGS
#include <python/conversion_utils.hpp>
#endif
#include <utils/math_funcs.hpp>
#include <utils/enum.hpp>
......@@ -28,8 +25,9 @@
#include <boost/serialization/unique_ptr.hpp>
#ifdef PY_BINDINGS
namespace py = boost::python;
namespace np = boost::python::numpy;
#include <python/conversion_utils.hpp>
namespace py = boost::python;
namespace np = boost::python::numpy;
#endif
// DocString: cls_node
......@@ -340,107 +338,107 @@ public:
#ifdef PARAMETERIZE
/**
* @brief The parameters used for introducing more non linearity in the operators
*/
virtual std::vector<double> parameters() = 0;
/**
* @brief Set the non-linear parameters
*/
virtual void set_parameters(std::vector<double>, bool check_sz=true) = 0;
/**
* @brief returns the number of parameters for this feature
* @return the number of parameters (_params.size())
*/
virtual int n_params(int n_cur = 0, int depth = 1) = 0;
/**
* @brief Set the values of the training data for the feature inside of the value storage arrays
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
*/
virtual void set_value(const double* params, int offset=-1, bool for_comp=false, int depth=0) = 0;
/**
* @brief The pointer to where the feature's training data is stored
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
* @returns the pointer to the feature's data
*/
virtual double* value_ptr(const double* params, int offset=-1, bool for_comp=false, int depth=0) = 0;
/**
* @brief Set the values of the test data for the feature inside of the value storage arrays
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
*/
virtual void set_test_value(const double* params, int offset=-1, bool for_comp=false, int depth=0) = 0;
/**
* @brief The pointer to where the feature's test data is stored
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
* @returns the pointer to the feature's data
*/
virtual double* test_value_ptr(const double* params, int offset=-1, bool for_comp=false, int depth=0) = 0;
/**
* @brief The expression of the feature
*
* @param params parameter values for non-linear operations
* @param depth the current depth of the node on the Binary expression tree
* @return feature expression
*/
virtual std::string expr(double* params, int depth = 1) = 0;
/**
* @brief Get the latexified expression for the overall feature (From root node down)
*
* @param cap string to place in the start/end of the string
* @param params parameter values for non-linear operations
* @param depth the current depth of the node on the Binary expression tree
* @return the latexified expression
*/
virtual std::string get_latex_expr(double* params, int depth=1) = 0;
/**
* @brief Set the bounds for the nl parameterization
*
* @param lb pointer to the lower bounds data
* @param ub pointer to the upper bounds data
*/
virtual void set_bounds(double* lb, double* ub, int from_parent=2, int depth = 1) = 0;
/**
* @brief Calculates the derivative of an operation with respect to the parameters for a given sample
*
* @param params pointer to the parameters
* @param samp_ind sample index number
*/
virtual void param_derivative(const double* params, double* dfdp) = 0;
/**
* @brief Get the parameter gradient for non-linear optimization
*
* @param grad pointer to the gradient storage
* @param samp_ind sample index to calculate the gradient for
*/
virtual void gradient(double* grad, double* dfdp) = 0;
/**
* @brief Get the parameter gradient for non-linear optimization
*
* @param grad pointer to the gradient storage
* @param samp_ind sample index to calculate the gradient for
* @param params pointer to the parameters vector
*/
virtual void gradient(double* grad, double* dfdp, const double* params) = 0;
/**
* @brief The parameters used for introducing more non linearity in the operators
*/
virtual std::vector<double> parameters() = 0;
/**
* @brief Set the non-linear parameters
*/
virtual void set_parameters(std::vector<double>, bool check_sz=true) = 0;
/**
* @brief returns the number of parameters for this feature
* @return the number of parameters (_params.size())
*/
virtual int n_params(int n_cur = 0, int depth = 1) = 0;
/**
* @brief Set the values of the training data for the feature inside of the value storage arrays
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
*/
virtual void set_value(const double* params, int offset=-1, bool for_comp=false, int depth=0) = 0;
/**
* @brief The pointer to where the feature's training data is stored
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
* @returns the pointer to the feature's data
*/
virtual double* value_ptr(const double* params, int offset=-1, bool for_comp=false, int depth=0) = 0;
/**
* @brief Set the values of the test data for the feature inside of the value storage arrays
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
*/
virtual void set_test_value(const double* params, int offset=-1, bool for_comp=false, int depth=0) = 0;
/**
* @brief The pointer to where the feature's test data is stored
*
* @param offset(int) Key to determine which part of the temporary storage array to look into
* @param params pointer to the parameter values
* @returns the pointer to the feature's data
*/
virtual double* test_value_ptr(const double* params, int offset=-1, bool for_comp=false, int depth=0) = 0;
/**
* @brief The expression of the feature
*
* @param params parameter values for non-linear operations
* @param depth the current depth of the node on the Binary expression tree
* @return feature expression
*/
virtual std::string expr(double* params, int depth = 1) = 0;