-
Notifications
You must be signed in to change notification settings - Fork 122
/
PolarizationCorrectionTest.h
226 lines (193 loc) · 7.8 KB
/
PolarizationCorrectionTest.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#ifndef MANTID_ALGORITHMS_POLARIZATIONCORRECTION_TEST_H_
#define MANTID_ALGORITHMS_POLARIZATIONCORRECTION_TEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidAlgorithms/PolarizationCorrection.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/TableWorkspace.h"
#include <boost/make_shared.hpp>
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidAPI/AlgorithmManager.h"
using namespace Mantid::API;
using namespace Mantid::Algorithms;
using namespace Mantid::DataObjects;
using namespace WorkspaceCreationHelper;
class PolarizationCorrectionTest: 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 PolarizationCorrectionTest *createSuite()
{
return new PolarizationCorrectionTest();
}
static void destroySuite(PolarizationCorrectionTest *suite)
{
delete suite;
}
void test_Init()
{
PolarizationCorrection alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize())
TS_ASSERT( alg.isInitialized())
}
void test_set_wrong_workspace_type_throws()
{
MatrixWorkspace_sptr ws = boost::make_shared<Workspace2D>();
PolarizationCorrection alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize());
TS_ASSERT_THROWS( alg.setProperty("InputWorkspace", ws), std::invalid_argument&);
}
void test_set_analysis_to_PA()
{
PolarizationCorrection alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize());
TS_ASSERT_THROWS_NOTHING( alg.setProperty("PolarizationAnalysis", "PA"));
}
void test_set_analysis_to_PNR()
{
PolarizationCorrection alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize())
TS_ASSERT_THROWS_NOTHING( alg.setProperty("PolarizationAnalysis", "PNR"));
}
void test_set_analysis_to_invalid_throws()
{
PolarizationCorrection alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize())
TS_ASSERT_THROWS( alg.setProperty("PolarizationAnalysis", "_"), std::invalid_argument&);
}
Mantid::API::WorkspaceGroup_sptr makeWorkspaceGroup()
{
Mantid::API::WorkspaceGroup_sptr group = boost::make_shared<WorkspaceGroup>();
return group;
}
void test_throw_if_PA_and_group_is_wrong_size_throws()
{
Mantid::API::WorkspaceGroup_sptr inWS = boost::make_shared<WorkspaceGroup>(); // Empty group ws.
// Name of the output workspace.
std::string outWSName("PolarizationCorrectionTest_OutputWS");
PolarizationCorrection alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", inWS);
alg.setProperty("PolarizationAnalysis", "PA");
alg.setPropertyValue("CRho", "1,1,1,1");
alg.setPropertyValue("CAlpha", "1,1,1,1");
alg.setPropertyValue("CAp", "1,1,1,1");
alg.setPropertyValue("CPp", "1,1,1,1");
alg.setPropertyValue("OutputWorkspace", outWSName);
TSM_ASSERT_THROWS("Wrong number of grouped workspaces, should throw", alg.execute(),
std::invalid_argument&);
}
void test_throw_if_PNR_and_group_is_wrong_size_throws()
{
Mantid::API::WorkspaceGroup_sptr inWS = boost::make_shared<WorkspaceGroup>(); // Empty group ws.
// Name of the output workspace.
std::string outWSName("PolarizationCorrectionTest_OutputWS");
PolarizationCorrection alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", inWS);
alg.setProperty("PolarizationAnalysis", "PNR");
alg.setPropertyValue("OutputWorkspace", outWSName);
alg.setPropertyValue("CRho", "1,1,1,1");
alg.setPropertyValue("CAlpha", "1,1,1,1");
alg.setPropertyValue("CAp", "1,1,1,1");
alg.setPropertyValue("CPp", "1,1,1,1");
TSM_ASSERT_THROWS("Wrong number of grouped workspaces, should throw", alg.execute(),
std::invalid_argument&);
}
void test_throw_group_contains_other_workspace_types()
{
Mantid::API::WorkspaceGroup_sptr inWS = boost::make_shared<WorkspaceGroup>(); // Empty group ws.
inWS->addWorkspace(boost::make_shared<TableWorkspace>()); // Group now contains a table workspace
// Name of the output workspace.
std::string outWSName("PolarizationCorrectionTest_OutputWS");
PolarizationCorrection alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", inWS);
alg.setProperty("PolarizationAnalysis", "PNR");
alg.setPropertyValue("OutputWorkspace", outWSName);
alg.setPropertyValue("CRho", "1,1,1,1");
alg.setPropertyValue("CAlpha", "1,1,1,1");
alg.setPropertyValue("CAp", "1,1,1,1");
alg.setPropertyValue("CPp", "1,1,1,1");
TSM_ASSERT_THROWS("Wrong workspace types in group", alg.execute(), std::invalid_argument&);
}
MatrixWorkspace_sptr create1DWorkspace(int size, double signal, double error)
{
auto ws = Create1DWorkspaceConstant(size, signal, error);
ws->getAxis(0)->setUnit("Wavelength");
return ws;
}
// If polynomials are unity, no changes should be made.
void test_run_PA_unity()
{
auto groupWS = boost::make_shared<WorkspaceGroup>(); // Empty group ws.
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
PolarizationCorrection alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", groupWS);
alg.setPropertyValue("OutputWorkspace", "dummy");
alg.setProperty("PolarizationAnalysis", "PA");
alg.setPropertyValue("CRho", "1,0,0,0");
alg.setPropertyValue("CAlpha", "1,0,0,0");
alg.setPropertyValue("CAp", "1,0,0,0");
alg.setPropertyValue("CPp", "1,0,0,0");
alg.execute();
WorkspaceGroup_sptr outWS = alg.getProperty("OutputWorkspace");
TSM_ASSERT_EQUALS("Wrong number of output workspaces", outWS->size(), groupWS->size());
for (size_t i = 0; i < outWS->size(); ++i)
{
std::cout << "Checking equivalent workspaces at index : " << i << std::endl;
auto checkAlg = AlgorithmManager::Instance().createUnmanaged("CheckWorkspacesMatch");
checkAlg->initialize();
checkAlg->setChild(true);
checkAlg->setProperty("Workspace1", groupWS->getItem(i));
checkAlg->setProperty("Workspace2", outWS->getItem(i));
checkAlg->setProperty("Tolerance", 3e-16);
checkAlg->execute();
const std::string result = checkAlg->getProperty("Result");
TS_ASSERT_EQUALS("Success!", result);
}
}
void test_run_PNR_unity()
{
auto groupWS = boost::make_shared<WorkspaceGroup>(); // Empty group ws.
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
groupWS->addWorkspace(create1DWorkspace(4, 1, 1));
PolarizationCorrection alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", groupWS);
alg.setPropertyValue("OutputWorkspace", "dummy");
alg.setProperty("PolarizationAnalysis", "PNR");
alg.setPropertyValue("CRho", "1,0,0,0");
alg.setPropertyValue("CPp", "1,0,0,0");
alg.execute();
WorkspaceGroup_sptr outWS = alg.getProperty("OutputWorkspace");
TSM_ASSERT_EQUALS("Wrong number of output workspaces", outWS->size(), groupWS->size());
for (size_t i = 0; i < outWS->size(); ++i)
{
std::cout << "Checking equivalent workspaces at index : " << i << std::endl;
auto checkAlg = AlgorithmManager::Instance().createUnmanaged("CheckWorkspacesMatch");
checkAlg->initialize();
checkAlg->setChild(true);
checkAlg->setProperty("Workspace1", groupWS->getItem(i));
checkAlg->setProperty("Workspace2", outWS->getItem(i));
checkAlg->execute();
const std::string result = checkAlg->getProperty("Result");
TS_ASSERT_EQUALS("Success!", result);
}
}
};
#endif /* MANTID_ALGORITHMS_POLARIZATIONCORRECTION_TEST_H_ */