Line data Source code
1 : /** 2 : * @file test_RepresentationProblem.cpp 3 : * 4 : * @brief Tests for the RepresentationProblem class 5 : * 6 : * @author Jonas Buerger 7 : */ 8 : 9 : #include "doctest/doctest.h" 10 : 11 : #include "RepresentationProblem.h" 12 : #include "VolumeDescriptor.h" 13 : #include "Dictionary.h" 14 : #include "testHelpers.h" 15 : 16 : using namespace elsa; 17 : using namespace doctest; 18 : 19 : TEST_SUITE_BEGIN("problems"); 20 : 21 : TEST_CASE_TEMPLATE("RepresentationProblem: Setup with a dictionary and a signal vector", data_t, 22 : float, double) 23 8 : { 24 8 : GIVEN("some dictionary operator and a signal vector") 25 8 : { 26 8 : const index_t nAtoms = 10; 27 8 : VolumeDescriptor signalDescriptor({5}); 28 : 29 8 : Dictionary<data_t> dictOp(signalDescriptor, nAtoms); 30 : 31 8 : Vector_t<data_t> signalVec(signalDescriptor.getNumberOfCoefficients()); 32 8 : signalVec.setRandom(); 33 8 : DataContainer<data_t> dcSignal(signalDescriptor, signalVec); 34 : 35 8 : RepresentationProblem<data_t> reprProblem(dictOp, dcSignal); 36 : 37 8 : WHEN("cloning a RepresentationProblem") 38 8 : { 39 2 : auto reprProblemClone = reprProblem.clone(); 40 2 : THEN("cloned RepresentationProblem equals original RepresentationProblem") 41 2 : { 42 2 : REQUIRE_NE(reprProblemClone.get(), &reprProblem); 43 2 : REQUIRE_EQ(*reprProblemClone, reprProblem); 44 2 : } 45 2 : } 46 : 47 8 : WHEN("evaluating the problem for a random representation") 48 8 : { 49 2 : VolumeDescriptor reprDescriptor({nAtoms}); 50 2 : Vector_t<data_t> reprVec(reprDescriptor.getNumberOfCoefficients()); 51 2 : reprVec.setRandom(); 52 2 : DataContainer<data_t> dcRepresentation(reprDescriptor, reprVec); 53 : 54 2 : reprProblem.getCurrentSolution() = dcRepresentation; 55 2 : auto evaluation = reprProblem.evaluate(); 56 : 57 2 : THEN("the evaluation is as expected") 58 2 : { 59 2 : DataContainer<data_t> residual = (dcSignal - dictOp.apply(dcRepresentation)); 60 2 : data_t expected = as<data_t>(0.5) * residual.squaredL2Norm(); 61 : 62 2 : REQUIRE_UNARY(checkApproxEq(evaluation, expected)); 63 2 : } 64 2 : } 65 : 66 8 : WHEN("getting the dictionary back") 67 8 : { 68 2 : const auto& reprProblemDict = reprProblem.getDictionary(); 69 : 70 2 : THEN("it equals the original dictionary") { REQUIRE_EQ(reprProblemDict, dictOp); } 71 2 : } 72 : 73 8 : WHEN("getting the signal back") 74 8 : { 75 2 : auto reprProblemSignal = reprProblem.getSignal(); 76 : 77 2 : THEN("it equals the original signal") { REQUIRE_EQ(reprProblemSignal, dcSignal); } 78 2 : } 79 8 : } 80 8 : } 81 : 82 : TEST_SUITE_END();