From d001324372c5d378bde821d18a5d3504b35726bf Mon Sep 17 00:00:00 2001 From: Samuel Felton Date: Fri, 24 May 2024 18:06:56 +0200 Subject: [PATCH] Added a bit more testing on PCA --- .../test/feature/testLuminanceMapping.cpp | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/modules/visual_features/test/feature/testLuminanceMapping.cpp b/modules/visual_features/test/feature/testLuminanceMapping.cpp index 36ce3b8ed6..dfe07bc502 100644 --- a/modules/visual_features/test/feature/testLuminanceMapping.cpp +++ b/modules/visual_features/test/feature/testLuminanceMapping.cpp @@ -90,9 +90,17 @@ SCENARIO("Using PCA features", "[visual_features]") REQUIRE_THROWS(vpLuminancePCA::learn(data.transpose(), k)); } } + WHEN("Learning with more images than pixels") + { + vpMatrix wrongData(20, 50); + THEN("An exception is thrown") + { + REQUIRE_THROWS(vpLuminancePCA::learn(wrongData.transpose(), 32)); + } + } WHEN("Learning PCA basis") { - for (unsigned int k = 1; k <= trueComponents; ++k) { + for (unsigned int k = 2; k <= trueComponents; ++k) { vpLuminancePCA pca = vpLuminancePCA::learn(data.transpose(), k); const vpMatrix &basis = *pca.getBasis(); @@ -123,7 +131,27 @@ SCENARIO("Using PCA features", "[visual_features]") REQUIRE(pca.getMean()->getRows() == dataDim); REQUIRE(pca.getMean()->getCols() == 1); } + THEN("Modifying the basis size (number of inputs) by hand and saving") + { + const std::string tempDir = vpIoTools::makeTempDirectory("visp_test_pca_wrong"); + const std::string basisFile = vpIoTools::createFilePath(tempDir, "basis.txt"); + const std::string meanFile = vpIoTools::createFilePath(tempDir, "mean.txt"); + const std::string varFile = vpIoTools::createFilePath(tempDir, "var.txt"); + pca.getBasis()->resize(pca.getBasis()->getRows(), pca.getBasis()->getCols() - 1); + REQUIRE_THROWS(pca.save(basisFile, meanFile, varFile)); + } + THEN("Modifying the mean Columns by hand") + { + const std::string tempDir = vpIoTools::makeTempDirectory("visp_test_pca_wrong"); + const std::string basisFile = vpIoTools::createFilePath(tempDir, "basis.txt"); + const std::string meanFile = vpIoTools::createFilePath(tempDir, "mean.txt"); + const std::string varFile = vpIoTools::createFilePath(tempDir, "var.txt"); + + std::shared_ptr mean = pca.getMean(); + mean->resize(mean->getRows() + 1, false); + REQUIRE_THROWS(pca.save(basisFile, meanFile, varFile)); + } THEN("Saving and loading pca leads to same basis and mean") { const std::string tempDir = vpIoTools::makeTempDirectory("visp_test_pca"); @@ -226,6 +254,18 @@ SCENARIO("Using PCA features", "[visual_features]") } } } + WHEN("Saving unintialized PCA") + { + vpLuminancePCA pca; + const std::string tempDir = vpIoTools::makeTempDirectory("visp_test_pca"); + const std::string basisFile = vpIoTools::createFilePath(tempDir, "basis.txt"); + const std::string meanFile = vpIoTools::createFilePath(tempDir, "mean.txt"); + const std::string varFile = vpIoTools::createFilePath(tempDir, "var.txt"); + THEN("an exception is thrown") + { + REQUIRE_THROWS(pca.save(basisFile, meanFile, varFile)); + } + } } SCENARIO("Using DCT features", "[visual_features]") @@ -352,7 +392,6 @@ SCENARIO("Using DCT features", "[visual_features]") REQUIRE(s.sum() == Approx(s[0]).margin(1e-5)); } - //dct.interaction(I, ); vpImage Ir; dct.inverse(s, Ir); REQUIRE((Ir.getRows() == I.getRows() && Ir.getCols() == I.getCols()));