Skip to content

Commit

Permalink
sg_options for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
gf712 committed Mar 2, 2019
1 parent cd8b321 commit 7224abe
Show file tree
Hide file tree
Showing 33 changed files with 136 additions and 73 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,3 @@ $RECYCLE.BIN/
# -*- mode: gitignore; -*-
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8
cmake-*/
1 change: 0 additions & 1 deletion examples/meta/generator/targets/cpp.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
"get_real_matrix": "$object->get<SGMatrix<float64_t>>($arguments)",
"get_int_string_list": "$object->get<SGStringList<int32_t>>($arguments)",
"get_char_string_list": "$object->get<SGStringList<char>>($arguments)",
"get_option": "$object->get<std::string>($arguments)"
"get_word_string_list": "$object->get<SGStringList<uint16_t>>($arguments)",
"get_option": "$object->get<std::string>($arguments)"
},
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/base/SGObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ class CSGObject
* @param _tag name and type information of parameter
* @param value value of the parameter
*/
template <typename T, typename std::enable_if_t<is_string<T>::value>* = nullptr>
template <typename T, typename std::enable_if_t<is_string<T>::value>* = nullptr>
void put(const Tag<T>& _tag, const T& value) noexcept(false)
{
std::string val_string(value);
Expand Down
75 changes: 63 additions & 12 deletions src/shogun/base/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@

#define VALUE_TO_STRING_MACRO(s) #s

#define SG_ADD_OPTION(...) VARARG(SG_ADD_OPTION, __VA_ARGS__)

#define SG_OPTIONS(...) __VA_ARGS__

#define SG_ADD_OPTION_BASE(param_name, enum_value) \
{ \
static_assert( \
Expand All @@ -73,6 +77,7 @@
static_assert(false, "Need to provide enums to add as options"); \
}

#ifdef _MSC_VER
#define SG_ADD_OPTION2(param_name, enum_value) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
Expand All @@ -81,46 +86,92 @@
#define SG_ADD_OPTION3(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION2(param_name, __VA_ARGS__) \
SG_ADD_OPTION(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION4(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION3(param_name, __VA_ARGS__) \
SG_ADD_OPTION(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION5(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION4(param_name, __VA_ARGS__) \
SG_ADD_OPTION(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION6(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION5(param_name, __VA_ARGS__) \
SG_ADD_OPTION(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION7(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION6(param_name, __VA_ARGS__) \
SG_ADD_OPTION(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION8(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION7(param_name, __VA_ARGS__) \
SG_ADD_OPTION(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION9(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION8(param_name, __VA_ARGS__) \
SG_ADD_OPTION(param_name, __VA_ARGS__) \
}
#else
#define SG_ADD_OPTION2(param_name, enum_value) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
}

#define SG_ADD_OPTION(...) VARARG(SG_ADD_OPTION, __VA_ARGS__)
#define SG_ADD_OPTION3(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION2(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION4(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION3(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION5(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION4(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION6(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION5(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION7(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION6(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION8(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION7(param_name, __VA_ARGS__) \
}

#define SG_ADD_OPTION9(param_name, enum_value, ...) \
{ \
SG_ADD_OPTION_BASE(param_name, enum_value) \
SG_ADD_OPTION8(param_name, __VA_ARGS__) \
}
#endif

// TODO: in C++17 add a if constexpr in the macro to support SG_ADD3 and
// SG_ADD4. This is needed because param_properties is a enum class
Expand All @@ -130,12 +181,12 @@
// decltype(param_properties), ParameterProperties>::value)
// {
// SG_ADD(param, name, description, param_properties)
// VARARG(SG_ADD_OPTION, name, __VA_ARGS__)
// SG_ADD_OPTION(name, __VA_ARGS__)
// }
// else
// {
// SG_ADD(param, name, description)
// VARARG(SG_ADD_OPTION, name, param_properties, __VA_ARGS__)
// SG_ADD_OPTION(name, param_properties, __VA_ARGS__)
// }
#define SG_ADD_OPTIONS(param, name, description, param_properties, ...) \
{ \
Expand All @@ -149,7 +200,7 @@
decltype(param_properties), ParameterProperties>::value, \
"SG_ADD_OPTIONS requires ParameterProperties in the fourth " \
"argument position"); \
SG_ADD(param, name, description, param_properties) \
SG_ADD_OPTION(name, __VA_ARGS__) \
SG_ADD(param, name, description, param_properties); \
SG_ADD_OPTION(name, __VA_ARGS__); \
}
#endif
4 changes: 2 additions & 2 deletions src/shogun/classifier/LDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ void CLDA::init()
SG_ADD(&m_bdc_svd, "m_bdc_svd", "Use BDC-SVD algorithm");
SG_ADD_OPTIONS(
(machine_int_t*)&m_method, "m_method",
"Method used for LDA calculation", ParameterProperties::NONE, AUTO_LDA,
SVD_LDA, FLD_LDA);
"Method used for LDA calculation", ParameterProperties::NONE,
SG_OPTIONS(AUTO_LDA, SVD_LDA, FLD_LDA));
}

CLDA::~CLDA()
Expand Down
7 changes: 4 additions & 3 deletions src/shogun/classifier/svm/LibLinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ void CLibLinear::init()
SG_ADD(&m_linear_term, "linear_term", "Linear Term");
SG_ADD_OPTIONS(
(machine_int_t*)&liblinear_solver_type, "liblinear_solver_type",
"Type of LibLinear solver.", ParameterProperties::NONE, L2R_LR,
L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL,
L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL)
"Type of LibLinear solver.", ParameterProperties::NONE,
SG_OPTIONS(
L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL,
L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL))
}

CLibLinear::~CLibLinear()
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/classifier/svm/LibSVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ void CLibSVM::register_params()
{
SG_ADD_OPTIONS(
(machine_int_t*)&solver_type, "libsvm_solver_type",
"LibSVM Solver type", ParameterProperties::NONE, LIBSVM_C_SVC,
LIBSVM_NU_SVC);
"LibSVM Solver type", ParameterProperties::NONE,
SG_OPTIONS(LIBSVM_C_SVC, LIBSVM_NU_SVC));
}

bool CLibSVM::train_machine(CFeatures* data)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/svm/SVMOcas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ void CSVMOcas::init()
SG_ADD(&bufsize, "bufsize", "Maximum number of cutting planes.");
SG_ADD_OPTIONS(
(machine_int_t*)&method, "method", "SVMOcas solver type.",
ParameterProperties::NONE, SVM_OCAS, SVM_BMRM);
ParameterProperties::NONE, SG_OPTIONS(SVM_OCAS, SVM_BMRM));
}

float64_t CSVMOcas::compute_primal_objective() const
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/converter/StochasticProximityEmbedding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void CStochasticProximityEmbedding::init()
SG_ADD(&m_max_iteration, "max_iteration", "maximum number of iterations");
SG_ADD_OPTIONS(
(machine_int_t*)&m_strategy, "m_strategy", "SPE strategy",
ParameterProperties::NONE, SPE_GLOBAL, SPE_LOCAL);
ParameterProperties::NONE, SG_OPTIONS(SPE_GLOBAL, SPE_LOCAL));
}

CStochasticProximityEmbedding::~CStochasticProximityEmbedding()
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/distributions/Gaussian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void CGaussian::register_params()
SG_ADD(&m_constant, "m_constant", "Constant part.");
SG_ADD_OPTIONS(
(machine_int_t*)&m_cov_type, "m_cov_type", "Covariance type.",
ParameterProperties::NONE, FULL, DIAG, SPHERICAL);
ParameterProperties::NONE, SG_OPTIONS(FULL, DIAG, SPHERICAL));
}

void CGaussian::decompose_cov(SGMatrix<float64_t> cov)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/RandomFourierDotFeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ CRandomFourierDotFeatures::~CRandomFourierDotFeatures()
SG_ADD(&constant, "constant", "A constant needed");
SG_ADD_OPTIONS(
(machine_int_t*)&kernel, "kernel", "The kernel to approximate",
ParameterProperties::NONE, GAUSSIAN, NOT_SPECIFIED);
ParameterProperties::NONE, SG_OPTIONS(GAUSSIAN, NOT_SPECIFIED));
}

CFeatures* CRandomFourierDotFeatures::duplicate() const
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/kernel/Kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,8 @@ void CKernel::register_params()

SG_ADD_OPTIONS(
(machine_int_t*)&opt_type, "opt_type", "Optimization type.",
ParameterProperties::NONE, FASTBUTMEMHUNGRY, SLOWBUTMEMEFFICIENT);
ParameterProperties::NONE,
SG_OPTIONS(FASTBUTMEMHUNGRY, SLOWBUTMEMEFFICIENT));
}


Expand Down
2 changes: 1 addition & 1 deletion src/shogun/kernel/normalizer/KernelNormalizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class CKernelNormalizer : public CSGObject
{
SG_ADD_OPTIONS(
(machine_int_t*)&m_type, "m_type", "Normalizer type.",
ParameterProperties::NONE, N_REGULAR, N_MULTITASK);
ParameterProperties::NONE, SG_OPTIONS(N_REGULAR, N_MULTITASK));
}

/** getter for normalizer type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1968,7 +1968,8 @@ void CWeightedDegreePositionStringKernel::init()
SG_ADD((CSGObject**)&alphabet, "alphabet", "Alphabet of Features.");
SG_ADD_OPTIONS(
(machine_int_t*)&type, "type", "WeightedDegree kernel type.",
ParameterProperties::HYPER, E_WD, E_EXTERNAL, E_BLOCK_CONST,
E_BLOCK_LINEAR, E_BLOCK_SQPOLY, E_BLOCK_CUBICPOLY, E_BLOCK_EXP,
E_BLOCK_LOG);
ParameterProperties::HYPER,
SG_OPTIONS(
E_WD, E_EXTERNAL, E_BLOCK_CONST, E_BLOCK_LINEAR, E_BLOCK_SQPOLY,
E_BLOCK_CUBICPOLY, E_BLOCK_EXP, E_BLOCK_LOG));
}
7 changes: 4 additions & 3 deletions src/shogun/kernel/string/WeightedDegreeStringKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,8 @@ void CWeightedDegreeStringKernel::init()
SG_ADD((CSGObject**)&alphabet, "alphabet", "Alphabet of Features.");
SG_ADD_OPTIONS(
(machine_int_t*)&type, "type", "WeightedDegree kernel type.",
ParameterProperties::HYPER, E_WD, E_EXTERNAL, E_BLOCK_CONST,
E_BLOCK_LINEAR, E_BLOCK_SQPOLY, E_BLOCK_CUBICPOLY, E_BLOCK_EXP,
E_BLOCK_LOG);
ParameterProperties::HYPER,
SG_OPTIONS(
E_WD, E_EXTERNAL, E_BLOCK_CONST, E_BLOCK_LINEAR, E_BLOCK_SQPOLY,
E_BLOCK_CUBICPOLY, E_BLOCK_EXP, E_BLOCK_LOG));
}
6 changes: 4 additions & 2 deletions src/shogun/machine/Machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ CMachine::CMachine()
SG_ADD(&m_data_locked, "data_locked", "Indicates whether data is locked");
SG_ADD_OPTIONS(
(machine_int_t*)&m_solver_type, "solver_type", "Type of solver.",
ParameterProperties::NONE, ST_AUTO, ST_CPLEX, ST_GLPK, ST_NEWTON,
ST_DIRECT, ST_ELASTICNET, ST_BLOCK_NORM);
ParameterProperties::NONE,
SG_OPTIONS(
ST_AUTO, ST_CPLEX, ST_GLPK, ST_NEWTON, ST_DIRECT, ST_ELASTICNET,
ST_BLOCK_NORM));
}

CMachine::~CMachine()
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/multiclass/KNN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ void CKNN::init()
SG_ADD(&m_leaf_size, "leaf_size", "Leaf size for KDTree");
SG_ADD_OPTIONS(
(machine_int_t*)&m_knn_solver, "knn_solver", "Algorithm to solve knn",
ParameterProperties::NONE, KNN_BRUTE, KNN_KDTREE, KNN_COVER_TREE,
KNN_LSH);
ParameterProperties::NONE,
SG_OPTIONS(KNN_BRUTE, KNN_KDTREE, KNN_COVER_TREE, KNN_LSH));
}

CKNN::~CKNN()
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/multiclass/MulticlassLibSVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void CMulticlassLibSVM::register_params()
{
SG_ADD_OPTIONS(
(machine_int_t*)&solver_type, "libsvm_solver_type",
"LibSVM solver type", ParameterProperties::NONE, LIBSVM_C_SVC,
LIBSVM_NU_SVC);
"LibSVM solver type", ParameterProperties::NONE,
SG_OPTIONS(LIBSVM_C_SVC, LIBSVM_NU_SVC));
}

bool CMulticlassLibSVM::train_machine(CFeatures* data)
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/multiclass/MulticlassStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ void CMulticlassStrategy::init()
SG_ADD_OPTIONS(
(machine_int_t*)&m_prob_heuris, "prob_heuris",
"Probability estimation heuristics", ParameterProperties::NONE,
PROB_HEURIS_NONE, OVA_NORM, OVA_SOFTMAX, OVO_PRICE, OVO_HASTIE,
OVO_HAMAMURA)
SG_OPTIONS(PROB_HEURIS_NONE, OVA_NORM, OVA_SOFTMAX, OVO_PRICE, OVO_HASTIE,
OVO_HAMAMURA))
}

void CMulticlassStrategy::train_start(CMulticlassLabels *orig_labels, CBinaryLabels *train_labels)
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/multiclass/ScatterSVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ void CScatterSVM::register_params()

SG_ADD_OPTIONS(
(machine_int_t*)&scatter_type, "scatter_type", "Type of scatter SVM",
ParameterProperties::NONE, NO_BIAS_LIBSVM,
ParameterProperties::NONE, SG_OPTIONS(NO_BIAS_LIBSVM,
#ifdef USE_SVMLIGHT
NO_BIAS_SVMLIGHT,
#endif // USE_SVMLIGHT
TEST_RULE1, TEST_RULE2);
TEST_RULE1, TEST_RULE2));
}

bool CScatterSVM::train_machine(CFeatures* data)
Expand Down
5 changes: 3 additions & 2 deletions src/shogun/multiclass/tree/CARTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,7 @@ void CCARTree::init()
SG_ADD_OPTIONS(
(machine_int_t*)&m_mode, "mode",
"problem type (multiclass or regression)", ParameterProperties::NONE,
PT_BINARY, PT_REGRESSION, PT_MULTICLASS, PT_STRUCTURED, PT_LATENT,
PT_CLASS);
SG_OPTIONS(
PT_BINARY, PT_REGRESSION, PT_MULTICLASS, PT_STRUCTURED, PT_LATENT,
PT_CLASS));
}
3 changes: 2 additions & 1 deletion src/shogun/neuralnets/Autoencoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,6 @@ void CAutoencoder::init()
"Contraction Coefficient");
SG_ADD_OPTIONS(
(machine_int_t*)&m_noise_type, "noise_type", "Noise Type",
ParameterProperties::NONE, AENT_NONE, AENT_DROPOUT, AENT_GAUSSIAN);
ParameterProperties::NONE,
SG_OPTIONS(AENT_NONE, AENT_DROPOUT, AENT_GAUSSIAN));
}
4 changes: 2 additions & 2 deletions src/shogun/neuralnets/DeepBeliefNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,6 @@ void CDeepBeliefNetwork::init()

SG_ADD_OPTIONS(
(machine_int_t*)&m_visible_units_type, "visible_units_type",
"Type of the visible units", ParameterProperties::HYPER, RBMVUT_BINARY,
RBMVUT_GAUSSIAN, RBMVUT_SOFTMAX);
"Type of the visible units", ParameterProperties::HYPER,
SG_OPTIONS(RBMVUT_BINARY, RBMVUT_GAUSSIAN, RBMVUT_SOFTMAX));
}
7 changes: 4 additions & 3 deletions src/shogun/neuralnets/NeuralConvolutionalLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,10 @@ void CNeuralConvolutionalLayer::init()

SG_ADD_OPTIONS(
(machine_int_t*)&m_initialization_mode, "initialization_mode",
"Initialization Mode", ParameterProperties::NONE, NORMAL, RE_NORMAL);
"Initialization Mode", ParameterProperties::NONE,
SG_OPTIONS(NORMAL, RE_NORMAL));
SG_ADD_OPTIONS(
(machine_int_t*)&m_activation_function, "activation_function",
"Activation Function", ParameterProperties::NONE, CMAF_IDENTITY,
CMAF_LOGISTIC, CMAF_RECTIFIED_LINEAR)
"Activation Function", ParameterProperties::NONE,
SG_OPTIONS(CMAF_IDENTITY, CMAF_LOGISTIC, CMAF_RECTIFIED_LINEAR))
}
4 changes: 2 additions & 2 deletions src/shogun/neuralnets/NeuralLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ void CNeuralLayer::init()

SG_ADD_OPTIONS(
(machine_int_t*)&autoencoder_position, "autoencoder_position",
"Autoencoder Position", ParameterProperties::NONE, NLAP_NONE,
NLAP_ENCODING, NLAP_DECODING);
"Autoencoder Position", ParameterProperties::NONE,
SG_OPTIONS(NLAP_NONE, NLAP_ENCODING, NLAP_DECODING));
}

0 comments on commit 7224abe

Please sign in to comment.