From 5be974bf1389e1bd4bcc05bd4116f9d807ba57a2 Mon Sep 17 00:00:00 2001 From: schalkdaniel Date: Fri, 19 Jan 2018 22:32:59 +0100 Subject: [PATCH 1/3] update tests --- tests/testthat/test_factory.R | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/testthat/test_factory.R b/tests/testthat/test_factory.R index 04874a44..8720530d 100644 --- a/tests/testthat/test_factory.R +++ b/tests/testthat/test_factory.R @@ -110,3 +110,26 @@ test_that("custom factory works", { predictFun(mod.test, X.test) ) }) + + +test_that("custom cpp factory works", { + suppressWarnings( + Rcpp::sourceCpp("../../tutorials/custom_cpp_learner.cpp") + ) + + set.seed(pi) + X = matrix(1:10, ncol = 1) + y = 3 * as.numeric(X) + rnorm(10, 0, 2) + + X.test = as.matrix(runif(200)) + + custom.cpp.factory = CustomCppFactory$new(X, "my_variable_name", dataFunSetter(), + trainFunSetter(), predictFunSetter()) + + custom.cpp.factory$testTrain(y) + + expect_equal(custom.cpp.factory$getData(), X) + expect_equal(custom.cpp.factory$testGetParameter(), solve(t(X) %*% X) %*% t(X) %*% y) + expect_equal(custom.cpp.factory$testPredict(), X %*% solve(t(X) %*% X) %*% t(X) %*% y) + expect_equal(custom.cpp.factory$testPredictNewdata(X.test), X.test %*% solve(t(X) %*% X) %*% t(X) %*% y) +}) \ No newline at end of file From 7dfb0b23b9aba4f646be4737424ee3d88888c1d1 Mon Sep 17 00:00:00 2001 From: schalkdaniel Date: Fri, 19 Jan 2018 22:43:28 +0100 Subject: [PATCH 2/3] create external_test_file folder! --- external_test_files/custom_cpp_learner.cpp | 62 ++++++++++++++++++++++ tests/testthat/test_factory.R | 3 +- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 external_test_files/custom_cpp_learner.cpp diff --git a/external_test_files/custom_cpp_learner.cpp b/external_test_files/custom_cpp_learner.cpp new file mode 100644 index 00000000..1365e1a9 --- /dev/null +++ b/external_test_files/custom_cpp_learner.cpp @@ -0,0 +1,62 @@ +// Example for a linear baselearner: +// --------------------------------- + + +#include + +typedef arma::mat (*instantiateDataFunPtr) (arma::mat& X); +typedef arma::mat (*trainFunPtr) (arma::vec& y, arma::mat& X); +typedef arma::mat (*predictFunPtr) (arma::mat& newdata, arma::mat& parameter); + + +// instantiateDataFun: +// ------------------- + +arma::mat instantiateDataFun (arma::mat& X) +{ + return X; +} + +// trainFun: +// ------------------- + +arma::mat trainFun (arma::vec& y, arma::mat& X) +{ + return arma::solve(X, y); +} + +// predictFun: +// ------------------- + +arma::mat predictFun (arma::mat& newdata, arma::mat& parameter) +{ + return newdata * parameter; +} + + +// Setter function: +// ------------------ + +// Now here we wrap the function to an XPtr. This one stores the pointer +// to the function and can be used as parameter for the CustomCppFactory. + +// Note that we don't have to export the upper functions since we are just +// interested in the pointer of the functions. + +// [[Rcpp::export]] +Rcpp::XPtr dataFunSetter () +{ + return Rcpp::XPtr (new instantiateDataFunPtr (&instantiateDataFun)); +} + +// [[Rcpp::export]] +Rcpp::XPtr trainFunSetter () +{ + return Rcpp::XPtr (new trainFunPtr (&trainFun)); +} + +// [[Rcpp::export]] +Rcpp::XPtr predictFunSetter () +{ + return Rcpp::XPtr (new predictFunPtr (&predictFun)); +} diff --git a/tests/testthat/test_factory.R b/tests/testthat/test_factory.R index 8720530d..9671058f 100644 --- a/tests/testthat/test_factory.R +++ b/tests/testthat/test_factory.R @@ -113,8 +113,9 @@ test_that("custom factory works", { test_that("custom cpp factory works", { + suppressWarnings( - Rcpp::sourceCpp("../../tutorials/custom_cpp_learner.cpp") + Rcpp::sourceCpp("../../external_test_files/custom_cpp_learner.cpp") ) set.seed(pi) From 7577e40870e5c8ef8c2460a2b82f58a54c537bda Mon Sep 17 00:00:00 2001 From: schalkdaniel Date: Fri, 19 Jan 2018 22:51:53 +0100 Subject: [PATCH 3/3] update tests --- external_test_files/custom_cpp_learner.cpp | 62 ---------------------- tests/testthat/test_factory.R | 44 +++++++-------- 2 files changed, 22 insertions(+), 84 deletions(-) delete mode 100644 external_test_files/custom_cpp_learner.cpp diff --git a/external_test_files/custom_cpp_learner.cpp b/external_test_files/custom_cpp_learner.cpp deleted file mode 100644 index 1365e1a9..00000000 --- a/external_test_files/custom_cpp_learner.cpp +++ /dev/null @@ -1,62 +0,0 @@ -// Example for a linear baselearner: -// --------------------------------- - - -#include - -typedef arma::mat (*instantiateDataFunPtr) (arma::mat& X); -typedef arma::mat (*trainFunPtr) (arma::vec& y, arma::mat& X); -typedef arma::mat (*predictFunPtr) (arma::mat& newdata, arma::mat& parameter); - - -// instantiateDataFun: -// ------------------- - -arma::mat instantiateDataFun (arma::mat& X) -{ - return X; -} - -// trainFun: -// ------------------- - -arma::mat trainFun (arma::vec& y, arma::mat& X) -{ - return arma::solve(X, y); -} - -// predictFun: -// ------------------- - -arma::mat predictFun (arma::mat& newdata, arma::mat& parameter) -{ - return newdata * parameter; -} - - -// Setter function: -// ------------------ - -// Now here we wrap the function to an XPtr. This one stores the pointer -// to the function and can be used as parameter for the CustomCppFactory. - -// Note that we don't have to export the upper functions since we are just -// interested in the pointer of the functions. - -// [[Rcpp::export]] -Rcpp::XPtr dataFunSetter () -{ - return Rcpp::XPtr (new instantiateDataFunPtr (&instantiateDataFun)); -} - -// [[Rcpp::export]] -Rcpp::XPtr trainFunSetter () -{ - return Rcpp::XPtr (new trainFunPtr (&trainFun)); -} - -// [[Rcpp::export]] -Rcpp::XPtr predictFunSetter () -{ - return Rcpp::XPtr (new predictFunPtr (&predictFun)); -} diff --git a/tests/testthat/test_factory.R b/tests/testthat/test_factory.R index 9671058f..e5c3481f 100644 --- a/tests/testthat/test_factory.R +++ b/tests/testthat/test_factory.R @@ -112,25 +112,25 @@ test_that("custom factory works", { }) -test_that("custom cpp factory works", { - - suppressWarnings( - Rcpp::sourceCpp("../../external_test_files/custom_cpp_learner.cpp") - ) - - set.seed(pi) - X = matrix(1:10, ncol = 1) - y = 3 * as.numeric(X) + rnorm(10, 0, 2) - - X.test = as.matrix(runif(200)) - - custom.cpp.factory = CustomCppFactory$new(X, "my_variable_name", dataFunSetter(), - trainFunSetter(), predictFunSetter()) - - custom.cpp.factory$testTrain(y) - - expect_equal(custom.cpp.factory$getData(), X) - expect_equal(custom.cpp.factory$testGetParameter(), solve(t(X) %*% X) %*% t(X) %*% y) - expect_equal(custom.cpp.factory$testPredict(), X %*% solve(t(X) %*% X) %*% t(X) %*% y) - expect_equal(custom.cpp.factory$testPredictNewdata(X.test), X.test %*% solve(t(X) %*% X) %*% t(X) %*% y) -}) \ No newline at end of file +# test_that("custom cpp factory works", { +# +# suppressWarnings( +# Rcpp::sourceCpp("../../external_test_files/custom_cpp_learner.cpp") +# ) +# +# set.seed(pi) +# X = matrix(1:10, ncol = 1) +# y = 3 * as.numeric(X) + rnorm(10, 0, 2) +# +# X.test = as.matrix(runif(200)) +# +# custom.cpp.factory = CustomCppFactory$new(X, "my_variable_name", dataFunSetter(), +# trainFunSetter(), predictFunSetter()) +# +# custom.cpp.factory$testTrain(y) +# +# expect_equal(custom.cpp.factory$getData(), X) +# expect_equal(custom.cpp.factory$testGetParameter(), solve(t(X) %*% X) %*% t(X) %*% y) +# expect_equal(custom.cpp.factory$testPredict(), X %*% solve(t(X) %*% X) %*% t(X) %*% y) +# expect_equal(custom.cpp.factory$testPredictNewdata(X.test), X.test %*% solve(t(X) %*% X) %*% t(X) %*% y) +# }) \ No newline at end of file