forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EstimatePolynomialTest.h
119 lines (101 loc) · 4.42 KB
/
EstimatePolynomialTest.h
1
2
3
4
5
6
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include <cxxtest/TestSuite.h>
#include "MantidHistogramData/EstimatePolynomial.h"
#include "MantidHistogramData/Histogram.h"
#include "MantidHistogramData/LinearGenerator.h"
#include "MantidHistogramData/QuadraticGenerator.h"
using Mantid::HistogramData::Counts;
using Mantid::HistogramData::estimateBackground;
using Mantid::HistogramData::estimatePolynomial;
using Mantid::HistogramData::Histogram;
using Mantid::HistogramData::LinearGenerator;
using Mantid::HistogramData::Points;
using Mantid::HistogramData::QuadraticGenerator;
class EstimatePolynomialTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static EstimatePolynomialTest *createSuite() { return new EstimatePolynomialTest(); }
static void destroySuite(EstimatePolynomialTest *suite) { delete suite; }
void test_BadParameters() {
Histogram histo(Points{10, LinearGenerator(0., 1.)}, Counts{10, LinearGenerator(10., 0.)});
double bg0, bg1, bg2, chisq;
// bad order
TS_ASSERT_THROWS(Mantid::HistogramData::estimatePolynomial(3, histo, 0, histo.size(), bg0, bg1, bg2, chisq),
const std::runtime_error &);
// bad range i_max < i_min
TS_ASSERT_THROWS(estimatePolynomial(2, histo, 1, 0, bg0, bg1, bg2, chisq), const std::runtime_error &);
// bad range x.size() < i_max
TS_ASSERT_THROWS(estimatePolynomial(2, histo, 0, 30, bg0, bg1, bg2, chisq), const std::runtime_error &);
}
void test_FlatData() {
Histogram histo(Points{10, LinearGenerator(0., 1.)}, Counts{10, LinearGenerator(10., 0.)});
double bg0, bg1, bg2, chisq;
for (size_t order = 0; order < 3; ++order) { // should always return that constant is best
estimatePolynomial(order, histo, 0, histo.size(), bg0, bg1, bg2, chisq);
TS_ASSERT_EQUALS(bg0, 10.);
TS_ASSERT_EQUALS(bg1, 0.);
TS_ASSERT_EQUALS(bg2, 0.);
}
}
void test_LinearData() {
Histogram histo(Points{10, LinearGenerator(0., 1.)}, Counts{10, LinearGenerator(0., 12.)});
double bg0, bg1, bg2, chisq;
// flat
std::cout << "*** *** order=" << 0 << std::endl;
estimatePolynomial(0, histo, 0, histo.size(), bg0, bg1, bg2, chisq);
std::cout << "chisq=" << chisq << std::endl;
TS_ASSERT_DELTA(bg0, 54., .00001);
TS_ASSERT_EQUALS(bg1, 0.);
TS_ASSERT_EQUALS(bg2, 0.);
// linear
std::cout << "*** *** order=" << 1 << std::endl;
estimatePolynomial(1, histo, 0, histo.size(), bg0, bg1, bg2, chisq);
std::cout << "chisq=" << chisq << std::endl;
TS_ASSERT_EQUALS(bg0, 0.);
TS_ASSERT_EQUALS(bg1, 12.);
TS_ASSERT_EQUALS(bg2, 0.);
// quadratic
std::cout << "*** *** order=" << 2 << std::endl;
estimatePolynomial(2, histo, 0, histo.size(), bg0, bg1, bg2, chisq);
std::cout << "chisq=" << chisq << std::endl;
TS_ASSERT_EQUALS(bg0, 0.);
TS_ASSERT_EQUALS(bg1, 12.);
TS_ASSERT_EQUALS(bg2, 0.);
}
void test_QuadraticData() {
Histogram histo(Points{10, LinearGenerator(0., 1.)}, Counts{10, QuadraticGenerator(10., 12., -3.)});
std::cout << "-> quad: ";
for (const auto &val : histo.y())
std::cout << val << " ";
std::cout << std::endl;
double bg0, bg1, bg2, chisq;
// flat
std::cout << "*** *** order=" << 0 << std::endl;
estimatePolynomial(0, histo, 0, histo.size(), bg0, bg1, bg2, chisq);
std::cout << "chisq=" << chisq << std::endl;
TS_ASSERT_DELTA(bg0, -21.5, .00001);
TS_ASSERT_EQUALS(bg1, 0.);
TS_ASSERT_EQUALS(bg2, 0.);
// linear
std::cout << "*** *** order=" << 1 << std::endl;
estimatePolynomial(1, histo, 0, histo.size(), bg0, bg1, bg2, chisq);
std::cout << "chisq=" << chisq << std::endl;
TS_ASSERT_DELTA(bg0, 46., .00001);
TS_ASSERT_DELTA(bg1, -15., .00001);
TS_ASSERT_EQUALS(bg2, 0.);
// quadratic
std::cout << "*** *** order=" << 2 << std::endl;
estimatePolynomial(2, histo, 0, histo.size(), bg0, bg1, bg2, chisq);
std::cout << "chisq=" << chisq << std::endl;
TS_ASSERT_EQUALS(bg0, 10.);
TS_ASSERT_EQUALS(bg1, 12.);
TS_ASSERT_EQUALS(bg2, -3.);
}
};