Line data Source code
1 : /**
2 : * @file test_HardThresholding.cpp
3 : *
4 : * @brief Tests for the HardThresholding class
5 : *
6 : * @author Andi Braimllari
7 : */
8 :
9 : #include "Error.h"
10 : #include "HardThresholding.h"
11 : #include "VolumeDescriptor.h"
12 :
13 : #include "doctest/doctest.h"
14 : #include <testHelpers.h>
15 :
16 : using namespace elsa;
17 : using namespace doctest;
18 :
19 : TEST_SUITE_BEGIN("proximity_operators");
20 :
21 28 : TEST_CASE_TEMPLATE("HardThresholding: Testing construction", data_t, float, double)
22 : {
23 8 : GIVEN("a DataDescriptor")
24 : {
25 8 : IndexVector_t numCoeff(3);
26 4 : numCoeff << 8, 4, 52;
27 8 : VolumeDescriptor volDescr(numCoeff);
28 :
29 6 : WHEN("instantiating a HardThresholding operator")
30 : {
31 4 : HardThresholding<data_t> hThrOp(volDescr);
32 :
33 4 : THEN("the DataDescriptors are equal")
34 : {
35 2 : REQUIRE_EQ(hThrOp.getRangeDescriptor(), volDescr);
36 : }
37 : }
38 :
39 6 : WHEN("cloning a HardThresholding operator")
40 : {
41 4 : HardThresholding<data_t> hThrOp(volDescr);
42 4 : auto hThrOpClone = hThrOp.clone();
43 :
44 4 : THEN("cloned HardThresholding operator equals original HardThresholding operator")
45 : {
46 2 : REQUIRE_NE(hThrOpClone.get(), &hThrOp);
47 2 : REQUIRE_EQ(*hThrOpClone, hThrOp);
48 : }
49 : }
50 : }
51 4 : }
52 :
53 26 : TEST_CASE_TEMPLATE("HardThresholding: Testing in 1D", data_t, float, double)
54 : {
55 4 : GIVEN("a DataDescriptor")
56 : {
57 4 : IndexVector_t numCoeff(1);
58 2 : numCoeff << 8;
59 4 : VolumeDescriptor volDescr(numCoeff);
60 :
61 4 : WHEN("Using HardThresholding operator in 1D")
62 : {
63 4 : HardThresholding<data_t> hThrOp(volDescr);
64 :
65 4 : THEN("Values under threshold=4 are 0 and values above remain the same")
66 : {
67 4 : Vector_t<data_t> data(volDescr.getNumberOfCoefficients());
68 2 : data << -2, 3, 4, -7, 7, 8, 8, 3;
69 4 : DataContainer<data_t> dC(volDescr, data);
70 :
71 4 : Vector_t<data_t> expectedRes(hThrOp.getRangeDescriptor().getNumberOfCoefficients());
72 2 : expectedRes << 0, 0, 0, -7, 7, 8, 8, 0;
73 2 : DataContainer<data_t> dCRes(hThrOp.getRangeDescriptor(), expectedRes);
74 :
75 2 : REQUIRE_UNARY(isApprox(dCRes, hThrOp.apply(dC, geometry::Threshold<data_t>{4})));
76 : }
77 : }
78 : }
79 2 : }
80 :
81 26 : TEST_CASE_TEMPLATE("HardThresholding: Testing in 3D", data_t, float, double)
82 : {
83 4 : GIVEN("a DataDescriptor")
84 : {
85 4 : IndexVector_t numCoeff(3);
86 2 : numCoeff << 3, 2, 3;
87 4 : VolumeDescriptor volumeDescriptor(numCoeff);
88 :
89 4 : WHEN("Using HardThresholding operator in 3D")
90 : {
91 4 : HardThresholding<data_t> hThrOp(volumeDescriptor);
92 :
93 4 : THEN("Values under threshold=5 are 0 and values above remain the same")
94 : {
95 4 : Vector_t<data_t> data(volumeDescriptor.getNumberOfCoefficients());
96 2 : data << 2, 1, 6, 6, 1, 4, 2, -9, 7, 7, 7, 3, 1, 2, 8, 9, -4, 5;
97 4 : DataContainer<data_t> dC(volumeDescriptor, data);
98 :
99 4 : Vector_t<data_t> expectedRes(hThrOp.getRangeDescriptor().getNumberOfCoefficients());
100 2 : expectedRes << 0, 0, 6, 6, 0, 0, 0, -9, 7, 7, 7, 0, 0, 0, 8, 9, 0, 0;
101 2 : DataContainer<data_t> dCRes(hThrOp.getRangeDescriptor(), expectedRes);
102 :
103 2 : REQUIRE_UNARY(isApprox(dCRes, hThrOp.apply(dC, geometry::Threshold<data_t>{5})));
104 : }
105 : }
106 : }
107 2 : }
108 :
109 32 : TEST_CASE_TEMPLATE("HardThresholding: Testing general behaviour", data_t, float, double)
110 : {
111 16 : GIVEN("a DataDescriptor")
112 : {
113 16 : IndexVector_t numCoeff(1);
114 8 : numCoeff << 8;
115 16 : VolumeDescriptor volDescr(numCoeff);
116 :
117 16 : WHEN("Using HardThresholding operator")
118 : {
119 16 : HardThresholding<data_t> hThrOp(volDescr);
120 :
121 10 : THEN("The zero vector is returned when the zero vector is given")
122 : {
123 4 : Vector_t<data_t> data(volDescr.getNumberOfCoefficients());
124 2 : data << 0, 0, 0, 0, 0, 0, 0, 0;
125 4 : DataContainer<data_t> dataContainer(volDescr, data);
126 :
127 4 : Vector_t<data_t> expectedRes(hThrOp.getRangeDescriptor().getNumberOfCoefficients());
128 2 : expectedRes << 0, 0, 0, 0, 0, 0, 0, 0;
129 2 : DataContainer<data_t> dCRes(hThrOp.getRangeDescriptor(), expectedRes);
130 :
131 2 : REQUIRE_UNARY(
132 : isApprox(dCRes, hThrOp.apply(dataContainer, geometry::Threshold<data_t>{4})));
133 : }
134 :
135 10 : THEN("HardThresholding operator throws exception for t = 0")
136 : {
137 4 : Vector_t<data_t> data(volDescr.getNumberOfCoefficients());
138 2 : data << 0, 0, 0, 0, 0, 0, 0, 0;
139 4 : DataContainer<data_t> dC(volDescr, data);
140 :
141 : // actually the geometry::Threshold throws this
142 4 : REQUIRE_THROWS_AS(hThrOp.apply(dC, geometry::Threshold<data_t>{0}),
143 : InvalidArgumentError);
144 : }
145 :
146 10 : THEN("HardThresholding operator throws exception for t < 0")
147 : {
148 4 : Vector_t<data_t> data(volDescr.getNumberOfCoefficients());
149 2 : data << 0, 0, 0, 0, 0, 0, 0, 0;
150 4 : DataContainer<data_t> dC(volDescr, data);
151 :
152 : // actually the geometry::Threshold throws this
153 4 : REQUIRE_THROWS_AS(hThrOp.apply(dC, geometry::Threshold<data_t>{-1}),
154 : InvalidArgumentError);
155 : }
156 :
157 10 : THEN("HardThresholding operator throws exception for differently sized v and prox")
158 : {
159 4 : Vector_t<data_t> data(volDescr.getNumberOfCoefficients());
160 2 : data << 0, 0, 0, 0, 0, 0, 0, 0;
161 4 : DataContainer<data_t> dC(volDescr, data);
162 :
163 4 : IndexVector_t numCoeff1(1);
164 2 : numCoeff1 << 9;
165 4 : VolumeDescriptor volDescr1(numCoeff1);
166 4 : Vector_t<data_t> data1(volDescr1.getNumberOfCoefficients());
167 2 : data1 << 0, 0, 0, 0, 0, 0, 0, 0, 0;
168 4 : DataContainer<data_t> dC1(volDescr1, data1);
169 :
170 4 : REQUIRE_THROWS_AS(hThrOp.apply(dC, geometry::Threshold<data_t>{1}, dC1),
171 : LogicError);
172 : }
173 : }
174 : }
175 8 : }
176 :
177 : TEST_SUITE_END();
|