Newer
Older
#ifndef MANTID_ALGORITHMS_FindPeakBackground_H_
#define MANTID_ALGORITHMS_FindPeakBackgroundTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidAlgorithms/FindPeakBackground.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include <cmath>
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::DataObjects;
using namespace std;
using Mantid::Algorithms::FindPeakBackground;
class FindPeakBackgroundTest : public CxxTest::TestSuite {
public:
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
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static FindPeakBackgroundTest *createSuite() {
return new FindPeakBackgroundTest();
}
static void destroySuite(FindPeakBackgroundTest *suite) { delete suite; }
void test_Calculation() {
// 1. Generate input workspace
MatrixWorkspace_sptr inWS = generateTestWorkspace();
// 2. Create
Algorithms::FindPeakBackground alg;
alg.initialize();
TS_ASSERT(alg.isInitialized());
alg.setProperty("InputWorkspace", inWS);
alg.setProperty("OutputWorkspace", "Signal");
alg.setProperty("WorkspaceIndex", 0);
alg.execute();
TS_ASSERT(alg.isExecuted());
Mantid::API::ITableWorkspace_sptr peaklist =
boost::dynamic_pointer_cast<Mantid::API::ITableWorkspace>(
Mantid::API::AnalysisDataService::Instance().retrieve("Signal"));
TS_ASSERT(peaklist);
TS_ASSERT_EQUALS(peaklist->rowCount(), 1);
TS_ASSERT_DELTA(peaklist->Int(0, 1), 4, 0.01);
TS_ASSERT_DELTA(peaklist->Int(0, 2), 19, 0.01);
TS_ASSERT_DELTA(peaklist->Double(0, 3), 1.2, 0.01);
TS_ASSERT_DELTA(peaklist->Double(0, 4), 0.04, 0.01);
TS_ASSERT_DELTA(peaklist->Double(0, 5), 0.0, 0.01);
// Clean
AnalysisDataService::Instance().remove("Signal");
return;
}
/** Generate a workspace for test
*/
MatrixWorkspace_sptr generateTestWorkspace() {
std::array<double, size> data = {{1, 2, 1, 1, 9, 11, 13, 20, 24, 32,
28, 48, 42, 77, 67, 33, 27, 20, 9, 2}};
MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(
WorkspaceFactory::Instance().create("Workspace2D", 1, size, size));
ws->mutableY(0).assign(data.begin(), data.end());
std::iota(ws->mutableX(0).begin(), ws->mutableX(0).end(), 0);
std::transform(ws->y(0).cbegin(), ws->y(0).cend(), ws->mutableE(0).begin(),
[](const double &y) { return sqrt(y); });
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
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
return ws;
}
//--------------------------------------------------------------------------------------------
/** Test on a spectrum without peak
*/
void test_FindBackgroundOnFlat() {
// Add workspace
MatrixWorkspace_sptr testws = generate2SpectraTestWorkspace();
AnalysisDataService::Instance().addOrReplace("Test2Workspace", testws);
// Set up algorithm
Algorithms::FindPeakBackground alg;
alg.initialize();
TS_ASSERT(alg.isInitialized());
alg.setProperty("InputWorkspace", "Test2Workspace");
alg.setProperty("OutputWorkspace", "Signal3");
alg.setProperty("WorkspaceIndex", 0);
// Execute
alg.execute();
TS_ASSERT(alg.isExecuted());
// Check result
ITableWorkspace_sptr outws = boost::dynamic_pointer_cast<ITableWorkspace>(
AnalysisDataService::Instance().retrieve("Signal3"));
TS_ASSERT(outws);
if (!outws)
return;
TS_ASSERT_EQUALS(outws->rowCount(), 1);
if (outws->rowCount() < 1)
return;
int ipeakmin = outws->Int(0, 1);
int ipeakmax = outws->Int(0, 2);
TS_ASSERT(ipeakmin >= ipeakmax);
// Clean
AnalysisDataService::Instance().remove("Signal3");
AnalysisDataService::Instance().remove("Test2Workspace");
return;
}
//--------------------------------------------------------------------------------------------
/** Test on a spectrum without peak
*/
void test_FindBackgroundOnSpec1() {
// Add workspace
MatrixWorkspace_sptr testws = generate2SpectraTestWorkspace();
AnalysisDataService::Instance().addOrReplace("Test2Workspace", testws);
// Set up algorithm
Algorithms::FindPeakBackground alg;
alg.initialize();
TS_ASSERT(alg.isInitialized());
alg.setProperty("InputWorkspace", "Test2Workspace");
alg.setProperty("OutputWorkspace", "Signal2");
alg.setProperty("WorkspaceIndex", 1);
// Execute
alg.execute();
TS_ASSERT(alg.isExecuted());
// Check result
ITableWorkspace_sptr outws = boost::dynamic_pointer_cast<ITableWorkspace>(
AnalysisDataService::Instance().retrieve("Signal2"));
TS_ASSERT(outws);
if (!outws)
return;
TS_ASSERT_EQUALS(outws->rowCount(), 1);
if (outws->rowCount() < 1)
return;
TS_ASSERT_EQUALS(outws->rowCount(), 1);
TS_ASSERT_DELTA(outws->Int(0, 1), 4, 0.01);
TS_ASSERT_DELTA(outws->Int(0, 2), 19, 0.01);
TS_ASSERT_DELTA(outws->Double(0, 3), 1.2, 0.01);
TS_ASSERT_DELTA(outws->Double(0, 4), 0.04, 0.01);
TS_ASSERT_DELTA(outws->Double(0, 5), 0.0, 0.01);
// Clean
AnalysisDataService::Instance().remove("Signal2");
AnalysisDataService::Instance().remove("Test2Workspace");
return;
}
//--------------------------------------------------------------------------------------------
/** Generate a workspace with 2 spectra for test
*/
MatrixWorkspace_sptr generate2SpectraTestWorkspace() {
vector<double> data{1, 2, 1, 1, 9, 11, 13, 20, 24, 32,
28, 48, 42, 77, 67, 33, 27, 20, 9, 2};
MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(
WorkspaceFactory::Instance().create("Workspace2D", 2, data.size(),
data.size()));
// Workspace index = 0
ws->mutableY(0).assign(data.size(), 0.0);
ws->mutableE(0).assign(data.size(), 1.0);
std::iota(ws->mutableX(0).begin(), ws->mutableX(0).end(), 0);
ws->mutableY(1).assign(data.cbegin(), data.cend());
std::iota(ws->mutableX(1).begin(), ws->mutableX(1).end(), 0);
std::transform(ws->y(1).cbegin(), ws->y(1).cend(), ws->mutableE(1).begin(),
[](const double &y) { return sqrt(y); });
};
#endif /* MANTID_ALGORITHMS_FindPeakBackgroundTEST_H_ */