migrate preprocess_* and related test from boost to catch2

This commit is contained in:
jeffin143
2020-07-20 13:40:04 +05:30
parent ca7045a702
commit c9a56fdb33
9 changed files with 356 additions and 353 deletions
+8 -8
View File
@@ -14,7 +14,6 @@ add_executable(mlpack_test
async_learning_test.cpp
augmented_rnns_tasks_test.cpp
bias_svd_test.cpp
binarize_test.cpp
block_krylov_svd_test.cpp
callback_test.cpp
cf_test.cpp
@@ -41,7 +40,6 @@ add_executable(mlpack_test
hoeffding_tree_test.cpp
hpt_test.cpp
hyperplane_test.cpp
imputation_test.cpp
init_rules_test.cpp
kde_test.cpp
kernel_pca_test.cpp
@@ -94,7 +92,6 @@ add_executable(mlpack_test
regularized_svd_test.cpp
reward_clipping_test.cpp
rl_components_test.cpp
scaling_test.cpp
serialization.cpp
serialization.hpp
serialization_test.cpp
@@ -104,7 +101,6 @@ add_executable(mlpack_test
sparse_autoencoder_test.cpp
sparse_coding_test.cpp
spill_tree_test.cpp
split_data_test.cpp
string_encoding_test.cpp
sumtree_test.cpp
svd_batch_test.cpp
@@ -156,10 +152,6 @@ add_executable(mlpack_test
main_tests/nmf_test.cpp
main_tests/pca_test.cpp
main_tests/perceptron_test.cpp
main_tests/preprocess_binarize_test.cpp
main_tests/preprocess_imputer_test.cpp
main_tests/preprocess_scale_test.cpp
main_tests/preprocess_split_test.cpp
main_tests/radical_test.cpp
main_tests/random_forest_test.cpp
main_tests/range_search_test.cpp
@@ -173,8 +165,16 @@ add_executable(mlpack_catch_test
serialization_catch.cpp
serialization_catch.hpp
test_catch_tools.hpp
binarize_test.cpp
image_load_test.cpp
imputation_test.cpp
scaling_test.cpp
split_data_test.cpp
main_tests/image_converter_test.cpp
main_tests/preprocess_binarize_test.cpp
main_tests/preprocess_imputer_test.cpp
main_tests/preprocess_scale_test.cpp
main_tests/preprocess_split_test.cpp
main_tests/test_helper.hpp
)
+23 -27
View File
@@ -13,16 +13,14 @@
#include <mlpack/core/data/binarize.hpp>
#include <mlpack/core/math/random.hpp>
#include <boost/test/unit_test.hpp>
#include "test_tools.hpp"
#include "test_catch_tools.hpp"
#include "catch.hpp"
using namespace mlpack;
using namespace arma;
using namespace mlpack::data;
BOOST_AUTO_TEST_SUITE(BinarizeTest);
BOOST_AUTO_TEST_CASE(BinerizeOneDimension)
TEST_CASE("BinerizeOneDimension", "[BinarizeTest]")
{
mat input;
input << 1 << 2 << 3 << endr
@@ -34,18 +32,18 @@ BOOST_AUTO_TEST_CASE(BinerizeOneDimension)
const size_t dimension = 1;
Binarize<double>(input, output, threshold, dimension);
BOOST_REQUIRE_CLOSE(output(0, 0), 1, 1e-5); // 1
BOOST_REQUIRE_CLOSE(output(0, 1), 2, 1e-5); // 2
BOOST_REQUIRE_CLOSE(output(0, 2), 3, 1e-5); // 3
BOOST_REQUIRE_SMALL(output(1, 0), 1e-5); // 4 target
BOOST_REQUIRE_SMALL(output(1, 1), 1e-5); // 5 target
BOOST_REQUIRE_CLOSE(output(1, 2), 1, 1e-5); // 6 target
BOOST_REQUIRE_CLOSE(output(2, 0), 7, 1e-5); // 7
BOOST_REQUIRE_CLOSE(output(2, 1), 8, 1e-5); // 8
BOOST_REQUIRE_CLOSE(output(2, 2), 9, 1e-5); // 9
REQUIRE(output(0, 0)== Approx(1.0).epsilon(1e-5 / 100)); // 1
REQUIRE(output(0, 1)== Approx(2.0).epsilon(1e-5 / 100)); // 2
REQUIRE(output(0, 2)== Approx(3.0).epsilon(1e-5 / 100)); // 3
REQUIRE(output(1, 0) == Approx(0.0).margin(1e-5)); // 4 target
REQUIRE(output(1, 1) == Approx(0.0).margin(1e-5)); // 5 target
REQUIRE(output(1, 2)== Approx(1.0).epsilon(1e-5 / 100)); // 6 target
REQUIRE(output(2, 0)== Approx(7.0).epsilon(1e-5 / 100)); // 7
REQUIRE(output(2, 1)== Approx(8.0).epsilon(1e-5 / 100)); // 8
REQUIRE(output(2, 2)== Approx(9.0).epsilon(1e-5 / 100)); // 9
}
BOOST_AUTO_TEST_CASE(BinerizeAll)
TEST_CASE("BinerizeAll", "[BinarizeTest]")
{
mat input;
input << 1 << 2 << 3 << endr
@@ -57,15 +55,13 @@ BOOST_AUTO_TEST_CASE(BinerizeAll)
Binarize<double>(input, output, threshold);
BOOST_REQUIRE_SMALL(output(0, 0), 1e-5); // 1
BOOST_REQUIRE_SMALL(output(0, 1), 1e-5); // 2
BOOST_REQUIRE_SMALL(output(0, 2), 1e-5); // 3
BOOST_REQUIRE_SMALL(output(1, 0), 1e-5); // 4
BOOST_REQUIRE_SMALL(output(1, 1), 1e-5); // 5
BOOST_REQUIRE_CLOSE(output(1, 2), 1.0, 1e-5); // 6
BOOST_REQUIRE_CLOSE(output(2, 0), 1.0, 1e-5); // 7
BOOST_REQUIRE_CLOSE(output(2, 1), 1.0, 1e-5); // 8
BOOST_REQUIRE_CLOSE(output(2, 2), 1.0, 1e-5); // 9
}
BOOST_AUTO_TEST_SUITE_END();
REQUIRE(output(0, 0) == Approx(0.0).margin(1e-5)); // 1
REQUIRE(output(0, 1) == Approx(0.0).margin(1e-5)); // 2
REQUIRE(output(0, 2) == Approx(0.0).margin(1e-5)); // 3
REQUIRE(output(1, 0) == Approx(0.0).margin(1e-5)); //4
REQUIRE(output(1, 1) == Approx(0.0).margin(1e-5)); // 5
REQUIRE(output(1, 2)== Approx(1.0).epsilon(1e-5 / 100)); // 6
REQUIRE(output(2, 0)== Approx(1.0).epsilon(1e-5 / 100)); // 7
REQUIRE(output(2, 1)== Approx(1.0).epsilon(1e-5 / 100)); // 8
REQUIRE(output(2, 2)== Approx(1.0).epsilon(1e-5 / 100)); // 9
}
+131 -134
View File
@@ -22,21 +22,20 @@
#include <mlpack/core/data/imputation_methods/mean_imputation.hpp>
#include <mlpack/core/data/imputation_methods/median_imputation.hpp>
#include <boost/test/unit_test.hpp>
#include "test_tools.hpp"
#include "test_catch_tools.hpp"
#include "catch.hpp"
using namespace mlpack;
using namespace mlpack::data;
using namespace std;
BOOST_AUTO_TEST_SUITE(ImputationTest);
/**
* 1. Make sure a CSV is loaded correctly with mappings using MissingPolicy.
* 2. Try Imputer object with CustomImputation method to impute data "a".
* (It is ok to test on one method since the other ones will be covered in the
* next cases).
*/
BOOST_AUTO_TEST_CASE(DatasetMapperImputerTest)
TEST_CASE("DatasetMapperImputerTest", "[ImputationTest]")
{
fstream f;
f.open("test_file.csv", fstream::out);
@@ -48,23 +47,23 @@ BOOST_AUTO_TEST_CASE(DatasetMapperImputerTest)
arma::mat input;
MissingPolicy policy({"a"});
DatasetMapper<MissingPolicy> info(policy);
BOOST_REQUIRE(data::Load("test_file.csv", input, info) == true);
REQUIRE(data::Load("test_file.csv", input, info) == true);
// row and column test.
BOOST_REQUIRE_EQUAL(input.n_rows, 3);
BOOST_REQUIRE_EQUAL(input.n_cols, 3);
REQUIRE(input.n_rows == 3);
REQUIRE(input.n_cols == 3);
// Load check
// MissingPolicy should convert strings to nans.
BOOST_REQUIRE(std::isnan(input(0, 0)) == true);
BOOST_REQUIRE_CLOSE(input(0, 1), 5.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(0, 2), 8.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(1, 0), 2.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(1, 1), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(1, 2), 9.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(2, 0), 3.0, 1e-5);
BOOST_REQUIRE(std::isnan(input(2, 1)) == true);
BOOST_REQUIRE_CLOSE(input(2, 2), 10.0, 1e-5);
REQUIRE(std::isnan(input(0, 0)) == true);
REQUIRE(input(0, 1) == Approx(5.0).epsilon(1e-5 / 100));
REQUIRE(input(0, 2) == Approx(8.0).epsilon(1e-5 / 100));
REQUIRE(input(1, 0) == Approx(2.0).epsilon(1e-5 / 100));
REQUIRE(input(1, 1) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(input(1, 2) == Approx(9.0).epsilon(1e-5 / 100));
REQUIRE(input(2, 0) == Approx(3.0).epsilon(1e-5 / 100));
REQUIRE(std::isnan(input(2, 1)) == true);
REQUIRE(input(2, 2) == Approx(10.0).epsilon(1e-5 / 100));
// convert missing vals to 99.
CustomImputation<double> customStrategy(99);
@@ -75,15 +74,15 @@ BOOST_AUTO_TEST_CASE(DatasetMapperImputerTest)
imputer.Impute(input, "a", 0);
// Custom imputation result check.
BOOST_REQUIRE_CLOSE(input(0, 0), 99.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(0, 1), 5.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(0, 2), 8.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(1, 0), 2.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(1, 1), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(1, 2), 9.0, 1e-5);
BOOST_REQUIRE_CLOSE(input(2, 0), 3.0, 1e-5);
BOOST_REQUIRE(std::isnan(input(2, 1)) == true); // remains as NaN
BOOST_REQUIRE_CLOSE(input(2, 2), 10.0, 1e-5);
REQUIRE(input(0, 0) == Approx(99.0).epsilon(1e-5 / 100));
REQUIRE(input(0, 1) == Approx(5.0).epsilon(1e-5 / 100));
REQUIRE(input(0, 2) == Approx(8.0).epsilon(1e-5 / 100));
REQUIRE(input(1, 0) == Approx(2.0).epsilon(1e-5 / 100));
REQUIRE(input(1, 1) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(input(1, 2) == Approx(9.0).epsilon(1e-5 / 100));
REQUIRE(input(2, 0) == Approx(3.0).epsilon(1e-5 / 100));
REQUIRE(std::isnan(input(2, 1)) == true); // remains as NaN
REQUIRE(input(2, 2) == Approx(10.0).epsilon(1e-5 / 100));
// Remove the file.
remove("test_file.csv");
@@ -92,7 +91,7 @@ BOOST_AUTO_TEST_CASE(DatasetMapperImputerTest)
/**
* Make sure CustomImputation method replaces data 0 to 99.
*/
BOOST_AUTO_TEST_CASE(CustomImputationTest)
TEST_CASE("CustomImputationTest", "[ImputationTest]")
{
arma::mat columnWiseInput("3.0 0.0 2.0 0.0;"
"5.0 6.0 0.0 6.0;"
@@ -106,41 +105,41 @@ BOOST_AUTO_TEST_CASE(CustomImputationTest)
// column wise
imputer.Impute(columnWiseInput, mappedValue, 0/*dimension*/, true);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 0), 3.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 1), 99.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 2), 2.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 3), 99.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 0), 5.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 1), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 2), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 3), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 0), 9.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 1), 8.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 2), 4.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 3), 8.0, 1e-5);
REQUIRE(columnWiseInput(0, 0) == Approx(3.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(0, 1) == Approx(99.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(0, 2) == Approx(2.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(0, 3) == Approx(99.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 0) == Approx(5.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 1) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 2) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 3) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 0) == Approx(9.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 1) == Approx(8.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 2) == Approx(4.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 3) == Approx(8.0).epsilon(1e-5 / 100));
// row wise
imputer.Impute(rowWiseInput, mappedValue, 1, false);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 0), 3.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 1), 99.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 2), 2.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 3), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 0), 5.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 1), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 2), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 3), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 0), 9.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 1), 8.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 2), 4.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 3), 8.0, 1e-5);
REQUIRE(rowWiseInput(0, 0) == Approx(3.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 1) == Approx(99.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 2) == Approx(2.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 3) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 0) == Approx(5.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 1) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 2) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 3) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 0) == Approx(9.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 1) == Approx(8.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 2) == Approx(4.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 3) == Approx(8.0).epsilon(1e-5 / 100));
}
/**
* Make sure MeanImputation method replaces data 0 to mean value of each
* dimensions.
*/
BOOST_AUTO_TEST_CASE(MeanImputationTest)
TEST_CASE("MeanImputationTest", "[ImputationTest]")
{
arma::mat columnWiseInput("3.0 0.0 2.0 0.0;"
"5.0 6.0 0.0 6.0;"
@@ -153,41 +152,41 @@ BOOST_AUTO_TEST_CASE(MeanImputationTest)
// column wise
imputer.Impute(columnWiseInput, mappedValue, 0, true);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 0), 3.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 1), 2.5, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 2), 2.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 3), 2.5, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 0), 5.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 1), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 2), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 3), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 0), 9.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 1), 8.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 2), 4.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 3), 8.0, 1e-5);
REQUIRE(columnWiseInput(0, 0) == Approx(3.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(0, 1) == Approx(2.5).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(0, 2) == Approx(2.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(0, 3) == Approx(2.5).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 0) == Approx(5.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 1) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 2) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 3) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 0) == Approx(9.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 1) == Approx(8.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 2) == Approx(4.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 3) == Approx(8.0).epsilon(1e-5 / 100));
// row wise
imputer.Impute(rowWiseInput, mappedValue, 1, false);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 0), 3.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 1), 7.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 2), 2.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 3), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 0), 5.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 1), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 2), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 3), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 0), 9.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 1), 8.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 2), 4.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 3), 8.0, 1e-5);
REQUIRE(rowWiseInput(0, 0) == Approx(3.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 1) == Approx(7.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 2) == Approx(2.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 3) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 0) == Approx(5.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 1) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 2) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 3) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 0) == Approx(9.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 1) == Approx(8.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 2) == Approx(4.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 3) == Approx(8.0).epsilon(1e-5 / 100));
}
/**
* Make sure MedianImputation method replaces data 0 to median value of each
* dimensions.
*/
BOOST_AUTO_TEST_CASE(MedianImputationTest)
TEST_CASE("MedianImputationTest", "[ImputationTest]")
{
arma::mat columnWiseInput("3.0 0.0 2.0 0.0;"
"5.0 6.0 0.0 6.0;"
@@ -200,41 +199,41 @@ BOOST_AUTO_TEST_CASE(MedianImputationTest)
// column wise
imputer.Impute(columnWiseInput, mappedValue, 1, true);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 0), 3.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 1), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 2), 2.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 3), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 0), 5.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 1), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 2), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 3), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 0), 9.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 1), 8.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 2), 4.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 3), 8.0, 1e-5);
REQUIRE(columnWiseInput(0, 0) == Approx(3.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(0, 1) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(0, 2) == Approx(2.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(0, 3) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 0) == Approx(5.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 1) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 2) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 3) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 0) == Approx(9.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 1) == Approx(8.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 2) == Approx(4.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 3) == Approx(8.0).epsilon(1e-5 / 100));
// row wise
imputer.Impute(rowWiseInput, mappedValue, 1, false);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 0), 3.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 1), 7.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 2), 2.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 3), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 0), 5.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 1), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 2), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 3), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 0), 9.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 1), 8.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 2), 4.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(2, 3), 8.0, 1e-5);
REQUIRE(rowWiseInput(0, 0) == Approx(3.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 1) == Approx(7.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 2) == Approx(2.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 3) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 0) == Approx(5.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 1) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 2) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 3) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 0) == Approx(9.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 1) == Approx(8.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 2) == Approx(4.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(2, 3) == Approx(8.0).epsilon(1e-5 / 100));
}
/**
* Make sure ListwiseDeletion method deletes the whole column (if column wise)
* or the row (if row wise) containing value of 0.
*/
BOOST_AUTO_TEST_CASE(ListwiseDeletionTest)
TEST_CASE("ListwiseDeletionTest", "[ImputationTest]")
{
arma::mat columnWiseInput("3.0 0.0 2.0 0.0;"
"5.0 6.0 0.0 6.0;"
@@ -247,30 +246,30 @@ BOOST_AUTO_TEST_CASE(ListwiseDeletionTest)
// column wise
imputer.Impute(columnWiseInput, mappedValue, 0, true); // column wise
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 0), 3.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(0, 1), 2.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 0), 5.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(1, 1), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 0), 9.0, 1e-5);
BOOST_REQUIRE_CLOSE(columnWiseInput(2, 1), 4.0, 1e-5);
REQUIRE(columnWiseInput(0, 0) == Approx(3.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(0, 1) == Approx(2.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 0) == Approx(5.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(1, 1) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 0) == Approx(9.0).epsilon(1e-5 / 100));
REQUIRE(columnWiseInput(2, 1) == Approx(4.0).epsilon(1e-5 / 100));
// row wise
imputer.Impute(rowWiseInput, mappedValue, 1, false); // row wise
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 0), 5.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 1), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 2), 0.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(0, 3), 6.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 0), 9.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 1), 8.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 2), 4.0, 1e-5);
BOOST_REQUIRE_CLOSE(rowWiseInput(1, 3), 8.0, 1e-5);
REQUIRE(rowWiseInput(0, 0) == Approx(5.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 1) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 2) == Approx(0.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(0, 3) == Approx(6.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 0) == Approx(9.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 1) == Approx(8.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 2) == Approx(4.0).epsilon(1e-5 / 100));
REQUIRE(rowWiseInput(1, 3) == Approx(8.0).epsilon(1e-5 / 100));
}
/**
* Make sure we can map non-strings.
*/
BOOST_AUTO_TEST_CASE(DatasetMapperNonStringMapping)
TEST_CASE("DatasetMapperNonStringMapping", "[ImputationTest]")
{
IncrementPolicy incr(true);
DatasetMapper<IncrementPolicy, double> dm(incr, 1);
@@ -278,23 +277,23 @@ BOOST_AUTO_TEST_CASE(DatasetMapperNonStringMapping)
dm.MapString<size_t>(4.3, 0);
dm.MapString<size_t>(1.1, 0);
BOOST_REQUIRE_EQUAL(dm.NumMappings(0), 3);
REQUIRE(dm.NumMappings(0) == 3);
BOOST_REQUIRE(dm.Type(0) == data::Datatype::categorical);
REQUIRE(dm.Type(0) == data::Datatype::categorical);
BOOST_REQUIRE_EQUAL(dm.UnmapValue(5.0, 0), 0);
BOOST_REQUIRE_EQUAL(dm.UnmapValue(4.3, 0), 1);
BOOST_REQUIRE_EQUAL(dm.UnmapValue(1.1, 0), 2);
REQUIRE(dm.UnmapValue(5.0, 0) == 0);
REQUIRE(dm.UnmapValue(4.3, 0) == 1);
REQUIRE(dm.UnmapValue(1.1, 0) == 2);
BOOST_REQUIRE_EQUAL(dm.UnmapString(0, 0), 5.0);
BOOST_REQUIRE_EQUAL(dm.UnmapString(1, 0), 4.3);
BOOST_REQUIRE_EQUAL(dm.UnmapString(2, 0), 1.1);
REQUIRE(dm.UnmapString(0, 0) == 5.0);
REQUIRE(dm.UnmapString(1, 0) == 4.3);
REQUIRE(dm.UnmapString(2, 0) == 1.1);
}
/**
* Make sure we can map strange types.
*/
BOOST_AUTO_TEST_CASE(DatasetMapperPointerMapping)
TEST_CASE("DatasetMapperPointerMapping", "[ImputationTest]")
{
int a = 1, b = 2, c = 3;
IncrementPolicy incr(true);
@@ -304,15 +303,13 @@ BOOST_AUTO_TEST_CASE(DatasetMapperPointerMapping)
dm.MapString<size_t>(&b, 0);
dm.MapString<size_t>(&c, 0);
BOOST_REQUIRE_EQUAL(dm.NumMappings(0), 3);
REQUIRE(dm.NumMappings(0) == 3);
BOOST_REQUIRE_EQUAL(dm.UnmapValue(&a, 0), 0);
BOOST_REQUIRE_EQUAL(dm.UnmapValue(&b, 0), 1);
BOOST_REQUIRE_EQUAL(dm.UnmapValue(&c, 0), 2);
REQUIRE(dm.UnmapValue(&a, 0) == 0);
REQUIRE(dm.UnmapValue(&b, 0) == 1);
REQUIRE(dm.UnmapValue(&c, 0) == 2);
BOOST_REQUIRE_EQUAL(dm.UnmapString(0, 0), &a);
BOOST_REQUIRE_EQUAL(dm.UnmapString(1, 0), &b);
BOOST_REQUIRE_EQUAL(dm.UnmapString(2, 0), &c);
REQUIRE(dm.UnmapString(0, 0) == &a);
REQUIRE(dm.UnmapString(1, 0) == &b);
REQUIRE(dm.UnmapString(2, 0) == &c);
}
BOOST_AUTO_TEST_SUITE_END();
@@ -18,8 +18,8 @@ static const std::string testName = "PreprocessBinarize";
#include <mlpack/methods/preprocess/preprocess_binarize_main.cpp>
#include "test_helper.hpp"
#include <boost/test/unit_test.hpp>
#include "../test_tools.hpp"
#include "../test_catch_tools.hpp"
#include "../catch.hpp"
using namespace mlpack;
@@ -40,13 +40,12 @@ struct PreprocessBinarizeTestFixture
}
};
BOOST_FIXTURE_TEST_SUITE(PreprocessBinarizeMainTest,
PreprocessBinarizeTestFixture);
/**
* Check that input and output have same dimensions.
*/
BOOST_AUTO_TEST_CASE(PreprocessBinarizeDimensionTest)
TEST_CASE_METHOD(
PreprocessBinarizeTestFixture, "PreprocessBinarizeDimensionTest",
"PreprocessBinarizeMainTest")
{
// Create a synthetic dataset.
arma::mat inputData = arma::randu<arma::mat>(2, 5);
@@ -62,14 +61,16 @@ BOOST_AUTO_TEST_CASE(PreprocessBinarizeDimensionTest)
mlpackMain();
// Now check that the output has desired dimensions.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_rows, 2);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("output").n_cols, inputSize);
REQUIRE(IO::GetParam<arma::mat>("output").n_rows == 2);
REQUIRE(IO::GetParam<arma::mat>("output").n_cols == inputSize);
}
/**
* Check that specified dimension is non-negative.
*/
BOOST_AUTO_TEST_CASE(PreprocessBinarizeNegativeDimensionTest)
TEST_CASE_METHOD(
PreprocessBinarizeTestFixture, "PreprocessBinarizeNegativeDimensionTest",
"PreprocessBinarizeMainTest")
{
arma::mat inputData = arma::randu<arma::mat>(2, 2);
@@ -78,14 +79,16 @@ BOOST_AUTO_TEST_CASE(PreprocessBinarizeNegativeDimensionTest)
SetInputParam("dimension", (int) -2); // Invalid.
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
/**
* Check that we can't specify a dimension larger than input.
*/
BOOST_AUTO_TEST_CASE(PreprocessBinarizelargerDimensionTest)
TEST_CASE_METHOD(
PreprocessBinarizeTestFixture, "PreprocessBinarizelargerDimensionTest",
"PreprocessBinarizeMainTest")
{
arma::mat inputData = arma::randu<arma::mat>(2, 2);
@@ -94,14 +97,16 @@ BOOST_AUTO_TEST_CASE(PreprocessBinarizelargerDimensionTest)
SetInputParam("dimension", (int) 6); // Invalid.
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
/**
* Check that binarization took place for the specified dimension.
*/
BOOST_AUTO_TEST_CASE(PreprocessBinarizeVerificationTest)
TEST_CASE_METHOD(
PreprocessBinarizeTestFixture, "PreprocessBinarizeVerificationTest",
"PreprocessBinarizeMainTest")
{
arma::mat inputData({{7.0, 4.0, 5.0}, {2.0, 5.0, 9.0}, {7.0, 3.0, 8.0}});
@@ -115,25 +120,27 @@ BOOST_AUTO_TEST_CASE(PreprocessBinarizeVerificationTest)
output = std::move(IO::GetParam<arma::mat>("output"));
// All values dimension should remain unchanged.
BOOST_REQUIRE_CLOSE(output(0, 0), 7.0, 1e-5);
BOOST_REQUIRE_CLOSE(output(0, 1), 4.0, 1e-5);
BOOST_REQUIRE_CLOSE(output(0, 2), 5.0, 1e-5);
REQUIRE(output(0, 0)== Approx(7.0).epsilon(1e-5 / 100));
REQUIRE(output(0, 1)== Approx(4.0).epsilon(1e-5 / 100));
REQUIRE(output(0, 2)== Approx(5.0).epsilon(1e-5 / 100));
// All values should be binarized according to the threshold.
BOOST_REQUIRE_SMALL(output(1, 0), 1e-5);
BOOST_REQUIRE_SMALL(output(1, 1), 1e-5);
BOOST_REQUIRE_CLOSE(output(1, 2), 1.0, 1e-5);
REQUIRE(output(1, 0) == Approx(0.0).margin(1e-5));
REQUIRE(output(1, 1) == Approx(0.0).margin(1e-5));
REQUIRE(output(1, 2)== Approx(1.0).epsilon(1e-5 / 100));
// All values dimension should remain unchanged.
BOOST_REQUIRE_CLOSE(output(2, 0), 7.0, 1e-5);
BOOST_REQUIRE_CLOSE(output(2, 1), 3.0, 1e-5);
BOOST_REQUIRE_CLOSE(output(2, 2), 8.0, 1e-5);
REQUIRE(output(2, 0)== Approx(7.0).epsilon(1e-5 / 100));
REQUIRE(output(2, 1)== Approx(3.0).epsilon(1e-5 / 100));
REQUIRE(output(2, 2)== Approx(8.0).epsilon(1e-5 / 100));
}
/**
* Check that all dimensions are binarized when dimension is not specified.
*/
BOOST_AUTO_TEST_CASE(PreprocessBinarizeDimensionLessVerTest)
TEST_CASE_METHOD(
PreprocessBinarizeTestFixture, "PreprocessBinarizeDimensionLessVerTest",
"PreprocessBinarizeMainTest")
{
arma::mat inputData({{7.0, 4.0, 5.0}, {2.0, 5.0, 9.0}, {7.0, 3.0, 8.0}});
@@ -146,15 +153,13 @@ BOOST_AUTO_TEST_CASE(PreprocessBinarizeDimensionLessVerTest)
output = std::move(IO::GetParam<arma::mat>("output"));
// All values should be binarized according to the threshold.
BOOST_REQUIRE_CLOSE(output(0, 0), 1.0, 1e-5);
BOOST_REQUIRE_SMALL(output(0, 1), 1e-5);
BOOST_REQUIRE_SMALL(output(0, 2), 1e-5);
BOOST_REQUIRE_SMALL(output(1, 0), 1e-5);
BOOST_REQUIRE_SMALL(output(1, 1), 1e-5);
BOOST_REQUIRE_CLOSE(output(1, 2), 1.0, 1e-5);
BOOST_REQUIRE_CLOSE(output(2, 0), 1.0, 1e-5);
BOOST_REQUIRE_SMALL(output(2, 1), 1e-5);
BOOST_REQUIRE_CLOSE(output(2, 2), 1.0, 1e-5);
REQUIRE(output(0, 0)== Approx(1.0).epsilon(1e-5 / 100));
REQUIRE(output(0, 1) == Approx(0.0).margin(1e-5));
REQUIRE(output(0, 2) == Approx(0.0).margin(1e-5));
REQUIRE(output(1, 0) == Approx(0.0).margin(1e-5));
REQUIRE(output(1, 1) == Approx(0.0).margin(1e-5));
REQUIRE(output(1, 2)== Approx(1.0).epsilon(1e-5 / 100));
REQUIRE(output(2, 0)== Approx(1.0).epsilon(1e-5 / 100));
REQUIRE(output(2, 1) == Approx(0.0).margin(1e-5));
REQUIRE(output(2, 2)== Approx(1.0).epsilon(1e-5 / 100));
}
BOOST_AUTO_TEST_SUITE_END();
@@ -18,8 +18,8 @@ static const std::string testName = "PreprocessImputer";
#include <mlpack/methods/preprocess/preprocess_imputer_main.cpp>
#include "test_helper.hpp"
#include <boost/test/unit_test.hpp>
#include "../test_tools.hpp"
#include "../test_catch_tools.hpp"
#include "../catch.hpp"
#include <cmath>
@@ -42,14 +42,13 @@ struct PreprocessImputerTestFixture
}
};
BOOST_FIXTURE_TEST_SUITE(PreprocessImputerMainTest,
PreprocessImputerTestFixture);
/**
* Check that input and output have same dimensions
* except for listwise_deletion strategy.
*/
BOOST_AUTO_TEST_CASE(PreprocessImputerDimensionTest)
TEST_CASE_METHOD(
PreprocessImputerTestFixture, "PreprocessImputerDimensionTest",
"PreprocessImputerMainTest")
{
// Load synthetic dataset.
arma::mat inputData;
@@ -73,8 +72,8 @@ BOOST_AUTO_TEST_CASE(PreprocessImputerDimensionTest)
// Now check that the output has desired dimensions.
data::Load(IO::GetParam<std::string>("output_file"), outputData);
BOOST_REQUIRE_EQUAL(outputData.n_cols, inputSize);
BOOST_REQUIRE_EQUAL(outputData.n_rows, 3); // Input Dimension.
REQUIRE(outputData.n_cols == inputSize);
REQUIRE(outputData.n_rows == 3); // Input Dimension.
// Reset passed strategy.
IO::GetSingleton().Parameters()["strategy"].wasPassed = false;
@@ -86,8 +85,8 @@ BOOST_AUTO_TEST_CASE(PreprocessImputerDimensionTest)
// Now check that the output has desired dimensions.
data::Load(IO::GetParam<std::string>("output_file"), outputData);
BOOST_REQUIRE_EQUAL(outputData.n_cols, inputSize);
BOOST_REQUIRE_EQUAL(outputData.n_rows, 3); // Input Dimension.
REQUIRE(outputData.n_cols == inputSize);
REQUIRE(outputData.n_rows == 3); // Input Dimension.
// Reset passed strategy.
IO::GetSingleton().Parameters()["strategy"].wasPassed = false;
@@ -100,14 +99,16 @@ BOOST_AUTO_TEST_CASE(PreprocessImputerDimensionTest)
// Now check that the output has desired dimensions.
data::Load(IO::GetParam<std::string>("output_file"), outputData);
BOOST_REQUIRE_EQUAL(outputData.n_cols, inputSize);
BOOST_REQUIRE_EQUAL(outputData.n_rows, 3); // Input Dimension.
REQUIRE(outputData.n_cols == inputSize);
REQUIRE(outputData.n_rows == 3); // Input Dimension.
}
/**
* Check that output has fewer points in case of listwise_deletion strategy.
*/
BOOST_AUTO_TEST_CASE(PreprocessImputerListwiseDimensionTest)
TEST_CASE_METHOD(
PreprocessImputerTestFixture, "PreprocessImputerListwiseDimensionTest",
"PreprocessImputerMainTest")
{
// Load synthetic dataset.
arma::mat inputData;
@@ -140,14 +141,16 @@ BOOST_AUTO_TEST_CASE(PreprocessImputerListwiseDimensionTest)
// Now check that the output has desired dimensions.
arma::mat outputData;
data::Load(IO::GetParam<std::string>("output_file"), outputData);
BOOST_REQUIRE_EQUAL(outputData.n_cols + countNaN, inputSize);
BOOST_REQUIRE_EQUAL(outputData.n_rows, 3); // Input Dimension.
REQUIRE(outputData.n_cols + countNaN == inputSize);
REQUIRE(outputData.n_rows == 3); // Input Dimension.
}
/**
* Check that invalid strategy can't be specified.
*/
BOOST_AUTO_TEST_CASE(PreprocessImputerStrategyTest)
TEST_CASE_METHOD(
PreprocessImputerTestFixture, "PreprocessImputerStrategyTest",
"PreprocessImputerMainTest")
{
// Load synthetic dataset.
arma::mat inputData;
@@ -159,8 +162,6 @@ BOOST_AUTO_TEST_CASE(PreprocessImputerStrategyTest)
SetInputParam("strategy", (std::string) "notmean"); // Invalid.
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
BOOST_AUTO_TEST_SUITE_END();
@@ -18,14 +18,15 @@ static const std::string testName = "PreprocessScale";
#include <mlpack/methods/preprocess/preprocess_scale_main.cpp>
#include "test_helper.hpp"
#include <boost/test/unit_test.hpp>
#include "../test_tools.hpp"
#include "../test_catch_tools.hpp"
#include "../catch.hpp"
using namespace mlpack;
struct PreprocessScaleTestFixture
{
public:
static arma::mat dataset;
PreprocessScaleTestFixture()
{
// Cache in the options for this program.
@@ -40,16 +41,14 @@ struct PreprocessScaleTestFixture
}
};
BOOST_FIXTURE_TEST_SUITE(PreprocessScaleMainTest,
PreprocessScaleTestFixture);
arma::mat dataset = "-1 -0.5 0 1;"
"2 6 10 18;";
arma::mat PreprocessScaleTestFixture::dataset = "-1 -0.5 0 1;"
"2 6 10 18;";
/**
* Check that two different scalers give two different output.
*/
BOOST_AUTO_TEST_CASE(TwoScalerTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "TwoScalerTest",
"PreprocessScaleMainTest")
{
// Input custom data points.
std::string method = "max_abs_scaler";
@@ -75,7 +74,8 @@ BOOST_AUTO_TEST_CASE(TwoScalerTest)
* Check that two different option for a particular scaler give two
* different output.
*/
BOOST_AUTO_TEST_CASE(TwoOptionTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "TwoOptionTest",
"PreprocessScaleMainTest")
{
std::string method = "min_max_scaler";
// Input custom data points.
@@ -101,7 +101,8 @@ BOOST_AUTO_TEST_CASE(TwoOptionTest)
/**
* Check that passing unrelated option don't change anything.
*/
BOOST_AUTO_TEST_CASE(UnrelatedOptionTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "UnrelatedOptionTest",
"PreprocessScaleMainTest")
{
std::string method = "standard_scaler";
// Input custom data points.
@@ -128,7 +129,8 @@ BOOST_AUTO_TEST_CASE(UnrelatedOptionTest)
/**
* Check Inverse Scaling is working.
*/
BOOST_AUTO_TEST_CASE(InverseScalingTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "InverseScalingTest",
"PreprocessScaleMainTest")
{
std::string method = "zca_whitening";
// Input custom data points.
@@ -151,7 +153,8 @@ BOOST_AUTO_TEST_CASE(InverseScalingTest)
/**
* Check Saved model is working.
*/
BOOST_AUTO_TEST_CASE(SavedModelTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "SavedModelTest",
"PreprocessScaleMainTest")
{
std::string method = "pca_whitening";
// Input custom data points.
@@ -173,7 +176,8 @@ BOOST_AUTO_TEST_CASE(SavedModelTest)
/**
* Check different epsilon for PCA give two different output.
*/
BOOST_AUTO_TEST_CASE(EpsilonTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "EpsilonTest",
"PreprocessScaleMainTest")
{
std::string method = "pca_whitening";
// Input custom data points.
@@ -198,7 +202,8 @@ BOOST_AUTO_TEST_CASE(EpsilonTest)
/**
* Check for invalid epsilon.
*/
BOOST_AUTO_TEST_CASE(InvalidEpsilonTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "InvalidEpsilonTest",
"PreprocessScaleMainTest")
{
std::string method = "pca_whitening";
// Input custom data points.
@@ -206,13 +211,14 @@ BOOST_AUTO_TEST_CASE(InvalidEpsilonTest)
SetInputParam("scaler_method", std::move(method));
SetInputParam("epsilon", -1.0);
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
}
/**
* Check for invalid range in min_max_scaler.
*/
BOOST_AUTO_TEST_CASE(InvalidRangeTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "InvalidRangeTest",
"PreprocessScaleMainTest")
{
std::string method = "min_max_scaler";
// Input custom data points.
@@ -221,13 +227,14 @@ BOOST_AUTO_TEST_CASE(InvalidRangeTest)
SetInputParam("min_value", 4);
SetInputParam("max_value", 2);
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
}
/**
* Check for invalid scaler type.
*/
BOOST_AUTO_TEST_CASE(InvalidScalerTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "InvalidScalerTest",
"PreprocessScaleMainTest")
{
std::string method = "invalid_scaler";
// Input custom data points.
@@ -237,50 +244,53 @@ BOOST_AUTO_TEST_CASE(InvalidScalerTest)
SetInputParam("max_value", 2);
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
/**
* Check for Standard scaler type.
*/
BOOST_AUTO_TEST_CASE(StandardScalerTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "StandardScalerBindingTest",
"PreprocessScaleMainTest")
{
std::string method = "standard_scaler";
// Input custom data points.
SetInputParam("input", dataset);
SetInputParam("scaler_method", method);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
SetInputParam("scaler_method", std::move(method));
SetInputParam("input", dataset);
SetInputParam("input_model",
IO::GetParam<ScalingModel*>("output_model"));
SetInputParam("inverse_scaling", true);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
}
/**
* Check for MaxAbs scaler type.
*/
BOOST_AUTO_TEST_CASE(MaxAbsScalerTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "MaxAbsScalerBindingTest",
"PreprocessScaleMainTest")
{
std::string method = "max_abs_scaler";
// Input custom data points.
SetInputParam("input", dataset);
SetInputParam("scaler_method", method);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
SetInputParam("scaler_method", std::move(method));
SetInputParam("input", dataset);
SetInputParam("input_model",
IO::GetParam<ScalingModel*>("output_model"));
SetInputParam("inverse_scaling", true);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
}
/**
* Check for MinMax scaler type.
*/
BOOST_AUTO_TEST_CASE(MinMaxScalerTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "MinMaxScalerBindingTest",
"PreprocessScaleMainTest")
{
std::string method = "min_max_scaler";
// Input custom data points.
@@ -288,69 +298,70 @@ BOOST_AUTO_TEST_CASE(MinMaxScalerTest)
SetInputParam("scaler_method", method);
SetInputParam("min_value", 2);
SetInputParam("max_value", 4);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
SetInputParam("scaler_method", std::move(method));
SetInputParam("input", dataset);
SetInputParam("input_model",
IO::GetParam<ScalingModel*>("output_model"));
SetInputParam("inverse_scaling", true);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
}
/**
* Check for PCA scaler type.
*/
BOOST_AUTO_TEST_CASE(PCAScalerTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "PCAScalerBindingTest",
"PreprocessScaleMainTest")
{
std::string method = "pca_whitening";
// Input custom data points.
SetInputParam("input", dataset);
SetInputParam("scaler_method", method);
SetInputParam("epsilon", 1.0);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
SetInputParam("scaler_method", std::move(method));
SetInputParam("input", dataset);
SetInputParam("input_model",
IO::GetParam<ScalingModel*>("output_model"));
SetInputParam("inverse_scaling", true);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
}
/**
* Check for ZCA scaler type.
*/
BOOST_AUTO_TEST_CASE(ZCAScalerTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "ZCAScalerBindingTest",
"PreprocessScaleMainTest")
{
std::string method = "zca_whitening";
// Input custom data points.
SetInputParam("input", dataset);
SetInputParam("scaler_method", method);
SetInputParam("epsilon", 1.0);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
SetInputParam("scaler_method", std::move(method));
SetInputParam("input", dataset);
SetInputParam("input_model",
IO::GetParam<ScalingModel*>("output_model"));
SetInputParam("inverse_scaling", true);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
}
/**
* Check for Mean Normalization scaler type.
*/
BOOST_AUTO_TEST_CASE(MeanNormalizationTest)
TEST_CASE_METHOD(PreprocessScaleTestFixture, "MeanNormalizationBindingTest",
"PreprocessScaleMainTest")
{
std::string method = "mean_normalization";
// Input custom data points.
SetInputParam("input", dataset);
SetInputParam("scaler_method", method);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
SetInputParam("scaler_method", std::move(method));
SetInputParam("input", dataset);
SetInputParam("input_model",
IO::GetParam<ScalingModel*>("output_model"));
SetInputParam("inverse_scaling", true);
BOOST_REQUIRE_NO_THROW(mlpackMain());
REQUIRE_NOTHROW(mlpackMain());
}
BOOST_AUTO_TEST_SUITE_END();
@@ -18,8 +18,8 @@ static const std::string testName = "PreprocessSplit";
#include <mlpack/methods/preprocess/preprocess_split_main.cpp>
#include "test_helper.hpp"
#include <boost/test/unit_test.hpp>
#include "../test_tools.hpp"
#include "../test_catch_tools.hpp"
#include "../catch.hpp"
#include <cmath>
@@ -42,14 +42,12 @@ struct PreprocessSplitTestFixture
}
};
BOOST_FIXTURE_TEST_SUITE(PreprocessSplitMainTest,
PreprocessSplitTestFixture);
/**
* Check that desired output dimensions are received for both input data and
* labels.
*/
BOOST_AUTO_TEST_CASE(PreprocessSplitDimensionTest)
TEST_CASE_METHOD(PreprocessSplitTestFixture, "PreprocessSplitDimensionTest",
"PreprocessSplitMainTest")
{
// Load custom dataset.
arma::mat inputData;
@@ -71,15 +69,15 @@ BOOST_AUTO_TEST_CASE(PreprocessSplitDimensionTest)
mlpackMain();
// Now check that the output has desired dimensions.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("training").n_cols,
std::ceil(0.9 * inputSize));
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("test").n_cols,
std::floor(0.1 * inputSize));
REQUIRE(IO::GetParam<arma::mat>("training").n_cols ==
std::ceil(0.9 * inputSize));
REQUIRE(IO::GetParam<arma::mat>("test").n_cols ==
std::floor(0.1 * inputSize));
BOOST_REQUIRE_EQUAL(
IO::GetParam<arma::Mat<size_t>>("training_labels").n_cols,
REQUIRE(
IO::GetParam<arma::Mat<size_t>>("training_labels").n_cols ==
std::ceil(0.9 * labelSize));
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::Mat<size_t>>("test_labels").n_cols,
REQUIRE(IO::GetParam<arma::Mat<size_t>>("test_labels").n_cols ==
std::floor(0.1 * labelSize));
}
@@ -87,7 +85,9 @@ BOOST_AUTO_TEST_CASE(PreprocessSplitDimensionTest)
* Check that desired output dimensions are received for the input data when
* labels are not provided.
*/
BOOST_AUTO_TEST_CASE(PreprocessSplitLabelLessDimensionTest)
TEST_CASE_METHOD(
PreprocessSplitTestFixture,
"PreprocessSplitLabelLessDimensionTest", "PreprocessSplitMainTest")
{
// Load custom dataset.
arma::mat inputData;
@@ -105,16 +105,17 @@ BOOST_AUTO_TEST_CASE(PreprocessSplitLabelLessDimensionTest)
mlpackMain();
// Now check that the output has desired dimensions.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("training").n_cols,
REQUIRE(IO::GetParam<arma::mat>("training").n_cols ==
std::ceil(0.9 * inputSize));
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("test").n_cols,
REQUIRE(IO::GetParam<arma::mat>("test").n_cols ==
std::floor(0.1 * inputSize));
}
/**
* Ensure that test ratio is always a non-negative number.
*/
BOOST_AUTO_TEST_CASE(PreprocessSplitTestRatioTest)
TEST_CASE_METHOD(PreprocessSplitTestFixture, "PreprocessSplitTestRatioTest",
"PreprocessSplitMainTest")
{
// Load custom dataset.
arma::mat inputData;
@@ -129,14 +130,16 @@ BOOST_AUTO_TEST_CASE(PreprocessSplitTestRatioTest)
SetInputParam("test_ratio", (double) -0.2);
Log::Fatal.ignoreInput = true;
BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error);
REQUIRE_THROWS_AS(mlpackMain(), std::runtime_error);
Log::Fatal.ignoreInput = false;
}
/**
* Check that if test size is 0 then train consist of whole input data.
*/
BOOST_AUTO_TEST_CASE(PreprocessSplitZeroTestRatioTest)
TEST_CASE_METHOD(
PreprocessSplitTestFixture, "PreprocessSplitZeroTestRatioTest",
"PreprocessSplitMainTest")
{
// Load custom dataset.
arma::mat inputData;
@@ -157,19 +160,19 @@ BOOST_AUTO_TEST_CASE(PreprocessSplitZeroTestRatioTest)
mlpackMain();
// Now check that the output has desired dimensions.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("training").n_cols, inputSize);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("test").n_cols, 0);
REQUIRE(IO::GetParam<arma::mat>("training").n_cols == inputSize);
REQUIRE(IO::GetParam<arma::mat>("test").n_cols == 0);
BOOST_REQUIRE_EQUAL(
IO::GetParam<arma::Mat<size_t>>("training_labels").n_cols, labelSize);
BOOST_REQUIRE_EQUAL(
IO::GetParam<arma::Mat<size_t>>("test_labels").n_cols, 0);
REQUIRE(IO::GetParam<arma::Mat<size_t>>("training_labels").n_cols == labelSize);
REQUIRE(IO::GetParam<arma::Mat<size_t>>("test_labels").n_cols == 0);
}
/**
* Check that if test size is 1 then test consist of whole input data.
*/
BOOST_AUTO_TEST_CASE(PreprocessSplitUnityTestRatioTest)
TEST_CASE_METHOD(
PreprocessSplitTestFixture, "PreprocessSplitUnityTestRatioTest",
"PreprocessSplitMainTest")
{
// Load custom dataset.
arma::mat inputData;
@@ -190,19 +193,19 @@ BOOST_AUTO_TEST_CASE(PreprocessSplitUnityTestRatioTest)
mlpackMain();
// Now check that the output has desired dimensions.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("training").n_cols, 0);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("test").n_cols, inputSize);
REQUIRE(IO::GetParam<arma::mat>("training").n_cols == 0);
REQUIRE(IO::GetParam<arma::mat>("test").n_cols == inputSize);
BOOST_REQUIRE_EQUAL(
IO::GetParam<arma::Mat<size_t>>("training_labels").n_cols, 0);
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::Mat<size_t>>("test_labels").n_cols,
labelSize);
REQUIRE(IO::GetParam<arma::Mat<size_t>>("training_labels").n_cols == 0);
REQUIRE(IO::GetParam<arma::Mat<size_t>>("test_labels").n_cols == labelSize);
}
/**
* Check shuffle_data flag is working as expected.
*/
BOOST_AUTO_TEST_CASE(PreprocessSplitLabelShuffleDataTest)
TEST_CASE_METHOD(
PreprocessSplitTestFixture, "PreprocessSplitLabelShuffleDataTest",
"PreprocessSplitMainTest")
{
// Load custom dataset.
arma::mat inputData;
@@ -220,14 +223,12 @@ BOOST_AUTO_TEST_CASE(PreprocessSplitLabelShuffleDataTest)
mlpackMain();
// Now check that the output has desired dimensions.
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("training").n_cols,
REQUIRE(IO::GetParam<arma::mat>("training").n_cols ==
std::ceil(0.9 * inputSize));
BOOST_REQUIRE_EQUAL(IO::GetParam<arma::mat>("test").n_cols,
REQUIRE(IO::GetParam<arma::mat>("test").n_cols ==
std::floor(0.1 * inputSize));
arma::mat concat = arma::join_rows(IO::GetParam<arma::mat>("training"),
IO::GetParam<arma::mat>("test"));
CheckMatrices(inputData, concat);
}
BOOST_AUTO_TEST_SUITE_END();
+13 -17
View File
@@ -16,15 +16,13 @@
#include <mlpack/core/data/scaler_methods/standard_scaler.hpp>
#include <mlpack/core/data/scaler_methods/mean_normalization.hpp>
#include <boost/test/unit_test.hpp>
#include "test_tools.hpp"
#include "test_catch_tools.hpp"
#include "catch.hpp"
using namespace mlpack;
using namespace mlpack::data;
using namespace std;
BOOST_AUTO_TEST_SUITE(ScalingTest);
arma::mat dataset = "-1 -0.5 0 1;"
"2 6 10 18;";
arma::mat scaleddataset;
@@ -33,7 +31,7 @@ arma::mat temp;
/**
* Test For MinMax Scaler Class.
*/
BOOST_AUTO_TEST_CASE(MinMaxScalerTest)
TEST_CASE("MinMaxScalerTest", "[ScalingTest]")
{
arma::mat scaled = "0 0.2500 0.5000 1.000;"
"0 0.2500 0.5000 1.000;";
@@ -48,7 +46,7 @@ BOOST_AUTO_TEST_CASE(MinMaxScalerTest)
/**
* Test For MaxAbs Scaler Class.
*/
BOOST_AUTO_TEST_CASE(MaxAbsScalerTest)
TEST_CASE("MaxAbsScalerTest", "[ScalingTest]")
{
arma::mat scaled = "-1 -0.5 0 1;"
"0.1111111111 0.3333333333 0.55555556 1.0000;";
@@ -63,7 +61,7 @@ BOOST_AUTO_TEST_CASE(MaxAbsScalerTest)
/**
* Test For Standard Scaler Class.
*/
BOOST_AUTO_TEST_CASE(StandardScalerTest)
TEST_CASE("StandardScalerTest", "[ScalingTest]")
{
arma::mat scaled = "-1.18321596 -0.50709255 0.16903085 1.52127766;"
"-1.18321596 -0.50709255 0.16903085 1.52127766;";
@@ -78,7 +76,7 @@ BOOST_AUTO_TEST_CASE(StandardScalerTest)
/**
* Test For MeanNormalization Scaler Class.
*/
BOOST_AUTO_TEST_CASE(MeanNormalizationTest)
TEST_CASE("MeanNormalizationTest", "[ScalingTest]")
{
arma::mat scaled = "-0.43750000000 -0.187500000 0.062500000 0.562500000;"
"-0.43750000000 -0.187500000 0.062500000 0.562500000;";
@@ -93,7 +91,7 @@ BOOST_AUTO_TEST_CASE(MeanNormalizationTest)
/**
* Test to pass same matrix as input and output
*/
BOOST_AUTO_TEST_CASE(SameInputOutputTest)
TEST_CASE("SameInputOutputTest", "[ScalingTest]")
{
temp = dataset;
arma::mat scaled = "-0.43750000000 -0.187500000 0.062500000 0.562500000;"
@@ -109,7 +107,7 @@ BOOST_AUTO_TEST_CASE(SameInputOutputTest)
/**
* Test for Zero Matrix.
*/
BOOST_AUTO_TEST_CASE(ZeroMatrixTest)
TEST_CASE("ZeroMatrixTest", "[ScalingTest]")
{
arma::mat input(2, 4, arma::fill::zeros);
data::MeanNormalization scale;
@@ -123,7 +121,7 @@ BOOST_AUTO_TEST_CASE(ZeroMatrixTest)
/**
* Test for Zero Scale.
*/
BOOST_AUTO_TEST_CASE(ZeroScaleTest)
TEST_CASE("ZeroScaleTest", "[ScalingTest]")
{
dataset = "1 1 1 1;"
"2 6 10 18;";
@@ -140,7 +138,7 @@ BOOST_AUTO_TEST_CASE(ZeroScaleTest)
/**
* Test for PCA whitening Scale.
*/
BOOST_AUTO_TEST_CASE(PCAWhiteningTest)
TEST_CASE("PCAWhiteningTest", "[ScalingTest]")
{
data::PCAWhitening scale;
arma::mat output;
@@ -151,7 +149,7 @@ BOOST_AUTO_TEST_CASE(PCAWhiteningTest)
double ccovsum = 0.0;
for (size_t i = 0; i < diagonals.n_elem; ++i)
ccovsum += diagonals(i);
BOOST_REQUIRE_CLOSE(ccovsum, 1.0, 1e-3);
REQUIRE(ccovsum == Approx(1.0).epsilon(1e-3 / 100));
scale.InverseTransform(output, temp);
CheckMatrices(dataset, temp);
}
@@ -159,7 +157,7 @@ BOOST_AUTO_TEST_CASE(PCAWhiteningTest)
/**
* Test for ZCA whitening Scale.
*/
BOOST_AUTO_TEST_CASE(ZCAWhiteningTest)
TEST_CASE("ZCAWhiteningTest", "[ScalingTest]")
{
data::ZCAWhitening scale;
arma::mat output;
@@ -170,9 +168,7 @@ BOOST_AUTO_TEST_CASE(ZCAWhiteningTest)
double ccovsum = 0.0;
for (size_t i = 0; i < diagonals.n_elem; ++i)
ccovsum += diagonals(i);
BOOST_REQUIRE_CLOSE(ccovsum, 1.0, 1e-3);
REQUIRE(ccovsum == Approx(1.0).epsilon(1e-3 / 100));
scale.InverseTransform(output, temp);
CheckMatrices(dataset, temp);
}
BOOST_AUTO_TEST_SUITE_END();
+34 -38
View File
@@ -12,15 +12,13 @@
#include <mlpack/core.hpp>
#include <mlpack/core/data/split_data.hpp>
#include <boost/test/unit_test.hpp>
#include "test_tools.hpp"
#include "test_catch_tools.hpp"
#include "catch.hpp"
using namespace mlpack;
using namespace arma;
using namespace mlpack::data;
BOOST_AUTO_TEST_SUITE(SplitDataTest);
/**
* Compare the data after train test split. This assumes that the labels
* correspond to each column, so that we can easily check each point against its
@@ -42,9 +40,9 @@ void CompareData(const mat& inputData,
for (size_t j = 0; j != lhsCol.n_rows; ++j)
{
if (std::abs(rhsCol(j)) < 1e-5)
BOOST_REQUIRE_SMALL(lhsCol(j), 1e-5);
REQUIRE(lhsCol(j) == Approx(0.0).margin(1e-5));
else
BOOST_REQUIRE_CLOSE(lhsCol(j), rhsCol(j), 1e-5);
REQUIRE(lhsCol(j) == Approx(rhsCol(j)).epsilon(1e-5 / 100));
}
}
}
@@ -61,9 +59,9 @@ void CheckMatEqual(const mat& inputData,
for (size_t j = 0; j < lhsCol.n_rows; ++j)
{
if (std::abs(rhsCol(j)) < 1e-5)
BOOST_REQUIRE_SMALL(lhsCol(j), 1e-5);
REQUIRE(lhsCol(j) == Approx(0.0).margin(1e-5));
else
BOOST_REQUIRE_CLOSE(lhsCol(j), rhsCol(j), 1e-5);
REQUIRE(lhsCol(j) == Approx(rhsCol(j)).epsilon(1e-5 / 100));
}
}
}
@@ -80,80 +78,80 @@ void CheckDuplication(const Row<size_t>& trainLabels,
for (size_t i = 0; i < trainLabels.n_elem; ++i)
{
BOOST_REQUIRE_LT(trainLabels[i], counts.n_elem);
REQUIRE(trainLabels[i] < counts.n_elem);
counts[trainLabels[i]]++;
}
for (size_t i = 0; i < testLabels.n_elem; ++i)
{
BOOST_REQUIRE_LT(testLabels[i], counts.n_elem);
REQUIRE(testLabels[i] < counts.n_elem);
counts[testLabels[i]]++;
}
// Now make sure each point has been used once.
for (size_t i = 0; i < counts.n_elem; ++i)
BOOST_REQUIRE_EQUAL(counts[i], 1);
REQUIRE(counts[i] == 1);
}
BOOST_AUTO_TEST_CASE(SplitShuffleDataResultMat)
TEST_CASE("SplitShuffleDataResultMat", "[SplitDataTest]")
{
mat input(2, 10);
size_t count = 0; // Counter for unique sequential values.
input.imbue([&count] () { return ++count; });
const auto value = Split(input, 0.2);
BOOST_REQUIRE_EQUAL(std::get<0>(value).n_cols, 8); // Train data.
BOOST_REQUIRE_EQUAL(std::get<1>(value).n_cols, 2); // Test data.
REQUIRE(std::get<0>(value).n_cols == 8); // Train data.
REQUIRE(std::get<1>(value).n_cols == 2); // Test data.
mat concat = arma::join_rows(std::get<0>(value), std::get<1>(value));
CheckMatEqual(input, concat);
}
BOOST_AUTO_TEST_CASE(SplitDataResultMat)
TEST_CASE("SplitDataResultMat", "[SplitDataTest]")
{
mat input(2, 10);
size_t count = 0; // Counter for unique sequential values.
input.imbue([&count] () { return ++count; });
const auto value = Split(input, 0.2, false);
BOOST_REQUIRE_EQUAL(std::get<0>(value).n_cols, 8); // Train data.
BOOST_REQUIRE_EQUAL(std::get<1>(value).n_cols, 2); // Test data.
REQUIRE(std::get<0>(value).n_cols == 8); // Train data.
REQUIRE(std::get<1>(value).n_cols == 2); // Test data.
mat concat = arma::join_rows(std::get<0>(value), std::get<1>(value));
// Order matters here.
CheckMatrices(input, concat);
}
BOOST_AUTO_TEST_CASE(ZeroRatioSplitData)
TEST_CASE("ZeroRatioSplitData", "[SplitDataTest]")
{
mat input(2, 10);
size_t count = 0; // Counter for unique sequential values.
input.imbue([&count] () { return ++count; });
const auto value = Split(input, 0, false);
BOOST_REQUIRE_EQUAL(std::get<0>(value).n_cols, 10); // Train data.
BOOST_REQUIRE_EQUAL(std::get<1>(value).n_cols, 0); // Test data.
REQUIRE(std::get<0>(value).n_cols == 10); // Train data.
REQUIRE(std::get<1>(value).n_cols == 0); // Test data.
mat concat = arma::join_rows(std::get<0>(value), std::get<1>(value));
// Order matters here.
CheckMatrices(input, concat);
}
BOOST_AUTO_TEST_CASE(TotalRatioSplitData)
TEST_CASE("TotalRatioSplitData", "[SplitDataTest]")
{
mat input(2, 10);
size_t count = 0; // Counter for unique sequential values.
input.imbue([&count] () { return ++count; });
const auto value = Split(input, 1, false);
BOOST_REQUIRE_EQUAL(std::get<0>(value).n_cols, 0); // Train data.
BOOST_REQUIRE_EQUAL(std::get<1>(value).n_cols, 10); // Test data.
REQUIRE(std::get<0>(value).n_cols == 0); // Train data.
REQUIRE(std::get<1>(value).n_cols == 10); // Test data.
mat concat = arma::join_rows(std::get<0>(value), std::get<1>(value));
// Order matters here.
CheckMatrices(input, concat);
}
BOOST_AUTO_TEST_CASE(SplitLabeledDataResultMat)
TEST_CASE("SplitLabeledDataResultMat", "[SplitDataTest]")
{
mat input(2, 10);
input.randu();
@@ -164,10 +162,10 @@ BOOST_AUTO_TEST_CASE(SplitLabeledDataResultMat)
input.n_cols);
const auto value = Split(input, labels, 0.2);
BOOST_REQUIRE_EQUAL(std::get<0>(value).n_cols, 8);
BOOST_REQUIRE_EQUAL(std::get<1>(value).n_cols, 2);
BOOST_REQUIRE_EQUAL(std::get<2>(value).n_cols, 8);
BOOST_REQUIRE_EQUAL(std::get<3>(value).n_cols, 2);
REQUIRE(std::get<0>(value).n_cols == 8);
REQUIRE(std::get<1>(value).n_cols == 2);
REQUIRE(std::get<2>(value).n_cols == 8);
REQUIRE(std::get<3>(value).n_cols == 2);
CompareData(input, std::get<0>(value), std::get<2>(value));
CompareData(input, std::get<1>(value), std::get<3>(value));
@@ -180,21 +178,21 @@ BOOST_AUTO_TEST_CASE(SplitLabeledDataResultMat)
/**
* The same test as above, but on a larger dataset.
*/
BOOST_AUTO_TEST_CASE(SplitDataLargerTest)
TEST_CASE("SplitDataLargerTest", "[SplitDataTest]")
{
size_t count = 0;
mat input(10, 497);
input.imbue([&count] () { return ++count; });
const auto value = Split(input, 0.3);
BOOST_REQUIRE_EQUAL(std::get<0>(value).n_cols, 497 - size_t(0.3 * 497));
BOOST_REQUIRE_EQUAL(std::get<1>(value).n_cols, size_t(0.3 * 497));
REQUIRE(std::get<0>(value).n_cols == 497 - size_t(0.3 * 497));
REQUIRE(std::get<1>(value).n_cols == size_t(0.3 * 497));
mat concat = arma::join_rows(std::get<0>(value), std::get<1>(value));
CheckMatEqual(input, concat);
}
BOOST_AUTO_TEST_CASE(SplitLabeledDataLargerTest)
TEST_CASE("SplitLabeledDataLargerTest", "[SplitDataTest]")
{
mat input(10, 497);
input.randu();
@@ -204,15 +202,13 @@ BOOST_AUTO_TEST_CASE(SplitLabeledDataLargerTest)
input.n_cols);
const auto value = Split(input, labels, 0.3);
BOOST_REQUIRE_EQUAL(std::get<0>(value).n_cols, 497 - size_t(0.3 * 497));
BOOST_REQUIRE_EQUAL(std::get<1>(value).n_cols, size_t(0.3 * 497));
BOOST_REQUIRE_EQUAL(std::get<2>(value).n_cols, 497 - size_t(0.3 * 497));
BOOST_REQUIRE_EQUAL(std::get<3>(value).n_cols, size_t(0.3 * 497));
REQUIRE(std::get<0>(value).n_cols == 497 - size_t(0.3 * 497));
REQUIRE(std::get<1>(value).n_cols == size_t(0.3 * 497));
REQUIRE(std::get<2>(value).n_cols == 497 - size_t(0.3 * 497));
REQUIRE(std::get<3>(value).n_cols == size_t(0.3 * 497));
CompareData(input, std::get<0>(value), std::get<2>(value));
CompareData(input, std::get<1>(value), std::get<3>(value));
CheckDuplication(std::get<2>(value), std::get<3>(value));
}
BOOST_AUTO_TEST_SUITE_END();