test_Math.cpp 6.32 KB
Newer Older
David Frank's avatar
David Frank committed
1
2
3
#include "doctest/doctest.h"

#include "Math.hpp"
andibraimllari's avatar
andibraimllari committed
4
5
6
#include "VolumeDescriptor.h"

#include <testHelpers.h>
David Frank's avatar
David Frank committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

TEST_SUITE_BEGIN("Math");

using namespace elsa;

TEST_CASE("Math::factorial")
{
    CHECK_EQ(1, math::factorial(0));
    CHECK_EQ(1, math::factorial(1));

    auto fac = 1;
    for (int i = 1; i < 10; ++i) {
        fac *= i;
        CHECK_EQ(fac, math::factorial(i));
    }
}

TEST_CASE("Math::binom")
{
    GIVEN("n == 10")
    {
        const index_t n = 10;

        WHEN("k larger than n") { CHECK_EQ(math::binom(n, 15), 0); }

        WHEN("k == 0 or k == n")
        {
            CHECK_EQ(math::binom(n, 0), 1);
            CHECK_EQ(math::binom(n, 10), 1);
        }

        CHECK_EQ(math::binom(n, 1), 10);
        CHECK_EQ(math::binom(n, 2), 45);
        CHECK_EQ(math::binom(n, 3), 120);
        CHECK_EQ(math::binom(n, 4), 210);
        CHECK_EQ(math::binom(n, 5), 252);
        CHECK_EQ(math::binom(n, 6), 210);
        CHECK_EQ(math::binom(n, 7), 120);
        CHECK_EQ(math::binom(n, 8), 45);
        CHECK_EQ(math::binom(n, 9), 10);
    }
}

TEST_CASE("Math::heaviside")
{
    constexpr index_t size = 200;
    constexpr real_t c = 0.5;
    const auto linspace = Vector_t<real_t>::LinSpaced(size, -2, 2);

    for (std::size_t i = 0; i < size; ++i) {
        auto res = math::heaviside(linspace[i], c);
        if (linspace[i] == 0.) {
            CHECK_EQ(res, c);
        } else if (linspace[i] < 0) {
            CHECK_EQ(res, 0);
        } else if (linspace[i] > 0) {
            CHECK_EQ(res, 1);
        }
    }
}

andibraimllari's avatar
andibraimllari committed
68
69
TEST_CASE_TEMPLATE("Math: Testing the statistics", TestType, float, double)
{
70
    GIVEN("some DataContainers")
andibraimllari's avatar
andibraimllari committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    {
        IndexVector_t sizeVector(2);
        sizeVector << 2, 4;
        VolumeDescriptor volDescr(sizeVector);

        Vector_t<TestType> vect1(volDescr.getNumberOfCoefficients());
        vect1 << 4, 2, 0.7f, 1, 0, 9, 53, 8;

        Vector_t<TestType> vect2(volDescr.getNumberOfCoefficients());
        vect2 << 5, 1, 6, 12, 22, 23, 9, 9;

        DataContainer<TestType> dataCont1(volDescr, vect1);

        DataContainer<TestType> dataCont2(volDescr, vect2);

        WHEN("running the Mean Squared Error")
        {
88
89
90
91
92
93
94
            DataContainer<TestType> dataCont3(VolumeDescriptor{{4, 3, 8}});
            THEN("it throws if the containers have different shapes")
            {
                REQUIRE_THROWS_AS(statistics::meanSquaredError<TestType>(dataCont1, dataCont3),
                                  InvalidArgumentError);
            }

andibraimllari's avatar
andibraimllari committed
95
96
            THEN("it produces the correct result")
            {
97
                auto meanSqErr = statistics::meanSquaredError<TestType>(dataCont1, dataCont2);
andibraimllari's avatar
andibraimllari committed
98
99
100
101
102
103
                REQUIRE_UNARY(checkApproxEq(meanSqErr, 346.01125f));
            }
        }

        WHEN("running the Relative Error")
        {
104
105
106
107
108
109
110
            DataContainer<TestType> dataCont3(VolumeDescriptor{{7, 6, 5, 4}});
            THEN("it throws if the containers have different shapes")
            {
                REQUIRE_THROWS_AS(statistics::relativeError<TestType>(dataCont1, dataCont3),
                                  InvalidArgumentError);
            }

andibraimllari's avatar
andibraimllari committed
111
112
            THEN("it produces the correct result")
            {
113
                auto relErr = statistics::relativeError<TestType>(dataCont1, dataCont2);
andibraimllari's avatar
andibraimllari committed
114
115
116
117
118
119
                REQUIRE_UNARY(checkApproxEq(relErr, 1.4157718f));
            }
        }

        WHEN("running the Peak Signal-to-Noise Ratio")
        {
120
121
122
123
124
125
126
127
            DataContainer<TestType> dataCont3(VolumeDescriptor{4});
            THEN("it throws if the containers have different shapes")
            {
                REQUIRE_THROWS_AS(
                    statistics::peakSignalToNoiseRatio<TestType>(dataCont1, dataCont3),
                    InvalidArgumentError);
            }

AndiBraimllari's avatar
AndiBraimllari committed
128
129
            auto psnr = statistics::peakSignalToNoiseRatio<TestType>(dataCont1, dataCont2);
            auto expectedpsnr = static_cast<TestType>(9.09461);
andibraimllari's avatar
andibraimllari committed
130
131
132
133
134
135
            THEN("it produces the correct result")
            {
                REQUIRE_UNARY(checkApproxEq(psnr, expectedpsnr));
            }
        }
    }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

    GIVEN("some vectors of numbers")
    {

        std::vector<TestType> numbers = {5, 0, 14, -4, 8};
        std::vector<TestType> manyNumbers = {2, 6,  9, 4,  2, 0,  7, 9,  4, 8, 6, 6,
                                             9, 4,  2, 15, 6, 91, 4, 22, 2, 3, 9, 4,
                                             1, -2, 6, 9,  4, 2,  5, 6,  4, 4, 9};

        WHEN("calculating the mean, standard deviation and a 95% confidence interval")
        {
            auto [mean, stddev] = statistics::calculateMeanStddev(numbers);

            THEN("it produces the correct mean and standard deviation")
            {
                TestType expectedmean = 4.6f;
                TestType expectedstddev = 6.2481997f;

                REQUIRE_UNARY(checkApproxEq(mean, expectedmean));
                REQUIRE_UNARY(checkApproxEq(stddev, expectedstddev));
            }

            auto [lower, upper] = statistics::confidenceInterval95(numbers.size(), mean, stddev);

            THEN("it produces the expected 95% confidence interval")
            {
                float expectedlower = -12.74778f;
                float expectedupper = 21.94778f;

                REQUIRE_UNARY(checkApproxEq(lower, expectedlower));
                REQUIRE_UNARY(checkApproxEq(upper, expectedupper));
            }
        }

        WHEN("calculating the mean, standard deviation and a 95% confidence interval for more than "
             "30 numbers")
        {
            auto [mean, stddev] = statistics::calculateMeanStddev(manyNumbers);

            THEN("it produces the correct mean and standard deviation")
            {
                TestType expectedmean = 8.0571429f;
                TestType expectedstddev = 14.851537f;

                REQUIRE_UNARY(checkApproxEq(mean, expectedmean));
                REQUIRE_UNARY(checkApproxEq(stddev, expectedstddev));
            }

            auto [lower, upper] =
                statistics::confidenceInterval95(manyNumbers.size(), mean, stddev);

            THEN("it produces the expected 95% confidence interval")
            {
                float expectedlower = -21.05187f;
                float expectedupper = 37.16616f;

                REQUIRE_UNARY(checkApproxEq(lower, expectedlower));
                REQUIRE_UNARY(checkApproxEq(upper, expectedupper));
            }
        }
    }
andibraimllari's avatar
andibraimllari committed
197
198
}

David Frank's avatar
David Frank committed
199
TEST_SUITE_END();