-
Notifications
You must be signed in to change notification settings - Fork 122
/
IPeakFunctionIntensityTest.h
152 lines (119 loc) · 4.92 KB
/
IPeakFunctionIntensityTest.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef IPEAKFUNCTIONINTENSITYTEST_H
#define IPEAKFUNCTIONINTENSITYTEST_H
#include <cxxtest/TestSuite.h>
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/IPeakFunction.h"
#include <boost/lexical_cast.hpp>
using namespace Mantid::API;
#define DBL2STR(x) boost::lexical_cast<std::string>(x)
struct ParameterSet {
ParameterSet(double c, double h, double f) : center(c), height(h), fwhm(f) {}
double center;
double height;
double fwhm;
};
class IPeakFunctionIntensityTest : 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 IPeakFunctionIntensityTest *createSuite() {
return new IPeakFunctionIntensityTest();
}
static void destroySuite(IPeakFunctionIntensityTest *suite) { delete suite; }
IPeakFunctionIntensityTest() : m_blackList() {
FrameworkManager::Instance();
m_blackList.insert("DeltaFunction");
m_blackList.insert("ElasticDiffRotDiscreteCircle");
m_blackList.insert("ElasticDiffSphere");
m_blackList.insert("Muon_ExpDecayOscTest");
m_peakFunctions = getAllPeakFunctions(m_blackList);
m_parameterSets = getParameterSets();
}
/* This test sets all peak function parameters (center, fwhm, height)
* to the values supplied in the first ParameterSet contained in
* m_parameterSets.
*
* Then it retrieves the intensities of the peak functions and stores them.
* Each time new parameters are set, the ratio of the height parameter to
* the previous step is compared to the intensity ratio - they should be
* the same.
*/
void testAllFunctions() {
initializePeakFunctions(m_peakFunctions, m_parameterSets[0]);
std::vector<double> initialIntensities = getIntensities(m_peakFunctions);
for (size_t i = 1; i < m_parameterSets.size(); ++i) {
double oldHeight = m_parameterSets[i - 1].height;
double newHeight = m_parameterSets[i].height;
double heightRatio = newHeight / oldHeight;
initializePeakFunctions(m_peakFunctions, m_parameterSets[i]);
std::vector<double> newIntensities = getIntensities(m_peakFunctions);
for (size_t j = 0; j < initialIntensities.size(); ++j) {
double oldIntensity = initialIntensities[j];
double newIntensity = newIntensities[j];
double intensityRatio = newIntensity / oldIntensity;
TSM_ASSERT_DELTA(
"ITERATION " + DBL2STR(i) + ", " + m_peakFunctions[j]->name() +
": Height was increased from " + DBL2STR(oldHeight) + " to " +
DBL2STR(newHeight) + " (ratio " + DBL2STR(heightRatio) +
"), but intensity changed from " + DBL2STR(oldIntensity) +
" to " + DBL2STR(newIntensity) + " (ratio " +
DBL2STR(intensityRatio) + ").",
intensityRatio, heightRatio, 1e-8);
}
initialIntensities = newIntensities;
}
}
private:
std::vector<IPeakFunction_sptr>
getAllPeakFunctions(const std::set<std::string> &blackList) const {
std::vector<IPeakFunction_sptr> peakFunctions;
std::vector<std::string> registeredFunctions =
FunctionFactory::Instance().getFunctionNames<IPeakFunction>();
for (auto it = registeredFunctions.begin(); it != registeredFunctions.end();
++it) {
if (blackList.count(*it) == 0) {
IPeakFunction_sptr peakFunction =
boost::dynamic_pointer_cast<IPeakFunction>(
FunctionFactory::Instance().createFunction(*it));
if (peakFunction) {
peakFunctions.push_back(peakFunction);
}
}
}
return peakFunctions;
}
void initializePeakFunctions(const std::vector<IPeakFunction_sptr> &peaks,
const ParameterSet ¶meters) const {
for (auto it = peaks.begin(); it != peaks.end(); ++it) {
(*it)->setCentre(parameters.center);
// for Ikeda-Carpenter it's not allowed to set Fwhm
try {
(*it)->setFwhm(parameters.fwhm);
}
catch (std::invalid_argument) {
}
(*it)->setHeight(parameters.height);
}
}
std::vector<ParameterSet> getParameterSets() const {
std::vector<ParameterSet> parameterSets;
parameterSets.push_back(ParameterSet(0.0, 4.34, 0.25));
parameterSets.push_back(ParameterSet(0.0, 5.34, 0.25));
parameterSets.push_back(ParameterSet(0.0, 6.34, 0.25));
parameterSets.push_back(ParameterSet(0.0, 7.34, 0.25));
return parameterSets;
}
std::vector<double>
getIntensities(const std::vector<IPeakFunction_sptr> &peaks) const {
std::vector<double> intensities;
for (auto it = peaks.begin(); it != peaks.end(); ++it) {
intensities.push_back((*it)->intensity());
}
return intensities;
}
std::vector<IPeakFunction_sptr> m_peakFunctions;
std::vector<ParameterSet> m_parameterSets;
std::set<std::string> m_blackList;
};
#endif // IPEAKFUNCTIONINTENSITYTEST_H