forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhaseQuadMuonTest.h
324 lines (288 loc) · 12.5 KB
/
PhaseQuadMuonTest.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// 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 <math.h>
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/TableRow.h"
#include "MantidDataObjects/TableWorkspace.h"
#include <cxxtest/TestSuite.h>
#include <utility>
using namespace Mantid::DataObjects;
using namespace Mantid::API;
namespace {
const int dead1 = 4;
const int dead2 = 12;
void populatePhaseTableWithDeadDetectors(const ITableWorkspace_sptr &phaseTable, const MatrixWorkspace_sptr &ws) {
phaseTable->addColumn("int", "DetectprID");
phaseTable->addColumn("double", "Asymmetry");
phaseTable->addColumn("double", "phase");
double asym(1.);
for (size_t i = 0; i < ws->getNumberHistograms(); i++) {
TableRow phaseRow1 = phaseTable->appendRow();
if (i == dead1 || i == dead2) {
phaseRow1 << int(i) << 999. << 0.0;
} else {
phaseRow1 << int(i) << asym << 2. * M_PI * double(i + 1) / (1. + double(ws->getNumberHistograms()));
}
}
}
void populatePhaseTable(const ITableWorkspace_sptr &phaseTable, std::vector<std::string> names, bool swap = false) {
phaseTable->addColumn("int", names[0]);
phaseTable->addColumn("double", names[1]);
phaseTable->addColumn("double", names[2]);
double asym(1.), phase(2.);
if (swap) {
std::swap(asym, phase);
}
for (int i = 0; i < 16; i++) {
TableRow phaseRow1 = phaseTable->appendRow();
phaseRow1 << i << asym << phase;
TableRow phaseRow2 = phaseTable->appendRow();
phaseRow2 << i << asym << phase;
}
}
void populatePhaseTable(const ITableWorkspace_sptr &phaseTable) {
populatePhaseTable(std::move(phaseTable), {"DetectorID", "Asymmetry", "Phase"});
}
IAlgorithm_sptr setupAlg(const MatrixWorkspace_sptr &m_loadedData, bool isChildAlg,
const ITableWorkspace_sptr &phaseTable) {
// Set up PhaseQuad
auto phaseQuad = AlgorithmManager::Instance().create("PhaseQuad");
phaseQuad->setChild(isChildAlg);
phaseQuad->initialize();
phaseQuad->setProperty("InputWorkspace", m_loadedData);
phaseQuad->setProperty("PhaseTable", phaseTable);
phaseQuad->setPropertyValue("OutputWorkspace", "outputWs");
return phaseQuad;
}
IAlgorithm_sptr setupAlg(const MatrixWorkspace_sptr &m_loadedData, bool isChildAlg) {
// Create and populate a detector table
std::shared_ptr<ITableWorkspace> phaseTable(new Mantid::DataObjects::TableWorkspace);
populatePhaseTable(phaseTable);
return setupAlg(std::move(m_loadedData), isChildAlg, phaseTable);
}
IAlgorithm_sptr setupAlg(const MatrixWorkspace_sptr &m_loadedData, bool isChildAlg, std::vector<std::string> names,
bool swap = false) {
// Create and populate a detector table
std::shared_ptr<ITableWorkspace> phaseTable(new Mantid::DataObjects::TableWorkspace);
populatePhaseTable(phaseTable, std::move(names), swap);
return setupAlg(std::move(m_loadedData), isChildAlg, phaseTable);
}
IAlgorithm_sptr setupAlgDead(const MatrixWorkspace_sptr &m_loadedData) {
// Create and populate a detector table
std::shared_ptr<ITableWorkspace> phaseTable(new Mantid::DataObjects::TableWorkspace);
populatePhaseTableWithDeadDetectors(phaseTable, m_loadedData);
return setupAlg(m_loadedData, true, phaseTable);
}
MatrixWorkspace_sptr setupWS(const MatrixWorkspace_sptr &m_loadedData) {
std::shared_ptr<ITableWorkspace> phaseTable(new Mantid::DataObjects::TableWorkspace);
MatrixWorkspace_sptr ws = m_loadedData->clone();
// create toy data set
populatePhaseTableWithDeadDetectors(phaseTable, ws);
auto xData = ws->points(0);
for (size_t spec = 0; spec < ws->getNumberHistograms(); spec++) {
for (size_t j = 0; j < xData.size(); j++) {
if (spec == dead1 || spec == dead2) {
ws->mutableY(spec)[j] = 0.0;
ws->mutableE(spec)[j] = 0.0;
} else {
ws->mutableY(spec)[j] = sin(2.3 * xData[j] + phaseTable->Double(spec, 2)) * exp(-xData[j] / 2.19703);
ws->mutableE(spec)[j] = cos(0.2 * xData[j]);
}
}
}
return ws;
}
MatrixWorkspace_sptr loadMuonDataset() {
auto loader = AlgorithmManager::Instance().create("Load");
loader->setChild(true);
loader->initialize();
loader->setProperty("Filename", "emu00006473.nxs");
loader->setPropertyValue("OutputWorkspace", "outputWs");
loader->execute();
Workspace_sptr temp = loader->getProperty("OutputWorkspace");
MatrixWorkspace_sptr m_loadedData = std::dynamic_pointer_cast<MatrixWorkspace>(temp);
return m_loadedData;
}
} // namespace
class PhaseQuadMuonTest : 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 PhaseQuadMuonTest *createSuite() { return new PhaseQuadMuonTest(); }
static void destroySuite(PhaseQuadMuonTest *suite) { delete suite; }
void setUp() override {
if (!m_loadedData) {
m_loadedData = loadMuonDataset();
}
}
void testTheBasics() {
IAlgorithm_sptr phaseQuad = AlgorithmManager::Instance().create("PhaseQuad");
TS_ASSERT_EQUALS(phaseQuad->name(), "PhaseQuad");
TS_ASSERT_EQUALS(phaseQuad->category(), "Muon");
TS_ASSERT_THROWS_NOTHING(phaseQuad->initialize());
TS_ASSERT(phaseQuad->isInitialized());
}
void testDead() {
MatrixWorkspace_sptr ws = setupWS(m_loadedData);
size_t nspec = ws->getNumberHistograms();
// check got some dead detectors
std::vector<bool> emptySpectrum;
for (size_t h = 0; h < nspec; h++) {
emptySpectrum.emplace_back(
std::all_of(ws->y(h).begin(), ws->y(h).end(), [](double value) { return value == 0.; }));
}
for (size_t j = 0; j < emptySpectrum.size(); j++) {
if (j == dead1 || j == dead2) {
TS_ASSERT(emptySpectrum[j]);
} else {
TS_ASSERT(!emptySpectrum[j]);
}
}
// do phase Quad
auto phaseQuad = setupAlgDead(ws);
TS_ASSERT_THROWS_NOTHING(phaseQuad->execute());
TS_ASSERT(phaseQuad->isExecuted());
// Get the output ws
MatrixWorkspace_sptr outputWs = phaseQuad->getProperty("OutputWorkspace");
TS_ASSERT_EQUALS(outputWs->getNumberHistograms(), 2);
TS_ASSERT_EQUALS(outputWs->getSpectrum(0).readX(),
m_loadedData->getSpectrum(0).readX()); // Check outputWs X values
TS_ASSERT_EQUALS(outputWs->getSpectrum(1).readX(), m_loadedData->getSpectrum(1).readX());
// Check output log is not empty
TS_ASSERT(outputWs->mutableRun().getLogData().size() > 0);
const auto specReY = outputWs->getSpectrum(0).y();
const auto specReE = outputWs->getSpectrum(0).e();
const auto specImY = outputWs->getSpectrum(1).y();
const auto specImE = outputWs->getSpectrum(1).e();
// Check real Y values
TS_ASSERT_DELTA(specReY[0], -0.6149, 0.0001);
TS_ASSERT_DELTA(specReY[20], 0.2987, 0.0001);
TS_ASSERT_DELTA(specReY[50], 1.2487, 0.0001);
// Check real E values
TS_ASSERT_DELTA(specReE[0], 0.2927, 0.0001);
TS_ASSERT_DELTA(specReE[20], 0.31489, 0.0001);
TS_ASSERT_DELTA(specReE[50], 0.3512, 0.0001);
// Check imaginary Y values
TS_ASSERT_DELTA(specImY[0], 1.0823, 0.0001);
TS_ASSERT_DELTA(specImY[20], 1.3149, 0.0001);
TS_ASSERT_DELTA(specImY[50], 0.4965, 0.0001);
// Check imaginary E values
TS_ASSERT_DELTA(specImE[0], 0.2801, 0.0001);
TS_ASSERT_DELTA(specImE[20], 0.3013, 0.0001);
TS_ASSERT_DELTA(specImE[50], 0.3360, 0.0001);
}
void testExecPhaseTable() {
auto phaseQuad = setupAlg(m_loadedData, true);
TS_ASSERT_THROWS_NOTHING(phaseQuad->execute());
TS_ASSERT(phaseQuad->isExecuted());
// Get the output ws
MatrixWorkspace_sptr outputWs = phaseQuad->getProperty("OutputWorkspace");
TS_ASSERT_EQUALS(outputWs->getNumberHistograms(), 2);
TS_ASSERT_EQUALS(outputWs->getSpectrum(0).readX(),
m_loadedData->getSpectrum(0).readX()); // Check outputWs X values
TS_ASSERT_EQUALS(outputWs->getSpectrum(1).readX(), m_loadedData->getSpectrum(1).readX());
// Check output log is not empty
TS_ASSERT(outputWs->mutableRun().getLogData().size() > 0);
const auto specReY = outputWs->getSpectrum(0).y();
const auto specReE = outputWs->getSpectrum(0).e();
const auto specImY = outputWs->getSpectrum(1).y();
const auto specImE = outputWs->getSpectrum(1).e();
// Check real Y values
TS_ASSERT_DELTA(specReY[0], 2.1969, 0.0001);
TS_ASSERT_DELTA(specReY[20], 0.0510, 0.0001);
TS_ASSERT_DELTA(specReY[50], -0.0525, 0.0001);
// Check real E values
TS_ASSERT_DELTA(specReE[0], 0.0024, 0.0001);
TS_ASSERT_DELTA(specReE[20], 0.0041, 0.0001);
TS_ASSERT_DELTA(specReE[50], 0.0047, 0.0001);
// Check imaginary Y values
TS_ASSERT_DELTA(specImY[0], -0.1035, 0.0001);
TS_ASSERT_DELTA(specImY[20], -0.0006, 0.0001);
TS_ASSERT_DELTA(specImY[50], 0.0047, 0.0001);
// Check imaginary E values
TS_ASSERT_DELTA(specImE[0], 0.0002, 0.0001);
TS_ASSERT_DELTA(specImE[20], 0.0004, 0.0001);
TS_ASSERT_DELTA(specImE[50], 0.0005, 0.0001);
}
void testNoPhase() {
std::vector<std::string> names = {"ID", "Asym", "dummy"};
IAlgorithm_sptr phaseQuad = setupAlg(m_loadedData, true, names);
TS_ASSERT_THROWS(phaseQuad->execute(), const std::runtime_error &);
}
void testNoAsymm() {
std::vector<std::string> names = {"ID", "AsYMg", "phase"};
MatrixWorkspace_sptr m_loadedData = loadMuonDataset();
IAlgorithm_sptr phaseQuad = setupAlg(m_loadedData, true, names);
TS_ASSERT_THROWS(phaseQuad->execute(), const std::runtime_error &);
}
void testTwoPhases() {
std::vector<std::string> names = {"ID", "Phase", "phi"};
IAlgorithm_sptr phaseQuad = setupAlg(m_loadedData, true, names);
TS_ASSERT_THROWS(phaseQuad->execute(), const std::runtime_error &);
}
void testTwoAsymm() {
std::vector<std::string> names = {"ID", "Asym", "Asymm"};
IAlgorithm_sptr phaseQuad = setupAlg(m_loadedData, true, names);
TS_ASSERT_THROWS(phaseQuad->execute(), const std::runtime_error &);
}
void testSwapOrder() {
std::vector<std::string> names = {"ID", "phase", "Asymm"};
auto phaseQuad = setupAlg(m_loadedData, true, names, true);
TS_ASSERT_THROWS_NOTHING(phaseQuad->execute());
TS_ASSERT(phaseQuad->isExecuted());
// Get the output ws
MatrixWorkspace_sptr outputWs = phaseQuad->getProperty("OutputWorkspace");
TS_ASSERT_EQUALS(outputWs->getNumberHistograms(), 2);
TS_ASSERT_EQUALS(outputWs->getSpectrum(0).readX(),
m_loadedData->getSpectrum(0).readX()); // Check outputWs X values
TS_ASSERT_EQUALS(outputWs->getSpectrum(1).readX(), m_loadedData->getSpectrum(1).readX());
const auto specReY = outputWs->getSpectrum(0).y();
const auto specReE = outputWs->getSpectrum(0).e();
const auto specImY = outputWs->getSpectrum(1).y();
const auto specImE = outputWs->getSpectrum(1).e();
// Check real Y values
TS_ASSERT_DELTA(specReY[0], 2.1969, 0.0001);
TS_ASSERT_DELTA(specReY[20], 0.0510, 0.0001);
TS_ASSERT_DELTA(specReY[50], -0.0525, 0.0001);
// Check real E values
TS_ASSERT_DELTA(specReE[0], 0.0024, 0.0001);
TS_ASSERT_DELTA(specReE[20], 0.0041, 0.0001);
TS_ASSERT_DELTA(specReE[50], 0.0047, 0.0001);
// Check imaginary Y values
TS_ASSERT_DELTA(specImY[0], -0.1035, 0.0001);
TS_ASSERT_DELTA(specImY[20], -0.0006, 0.0001);
TS_ASSERT_DELTA(specImY[50], 0.0047, 0.0001);
// Check imaginary E values
TS_ASSERT_DELTA(specImE[0], 0.0002, 0.0001);
TS_ASSERT_DELTA(specImE[20], 0.0004, 0.0001);
TS_ASSERT_DELTA(specImE[50], 0.0005, 0.0001);
}
// add test for different order
private:
MatrixWorkspace_sptr m_loadedData;
};
class PhaseQuadMuonTestPerformance : 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 PhaseQuadMuonTestPerformance *createSuite() { return new PhaseQuadMuonTestPerformance(); }
static void destroySuite(PhaseQuadMuonTestPerformance *suite) { delete suite; }
void setUp() override {
m_loadedData = loadMuonDataset();
phaseQuad = setupAlg(m_loadedData, false);
}
void tearDown() override { Mantid::API::AnalysisDataService::Instance().remove("outputWs"); }
void testPerformanceWs() { phaseQuad->execute(); }
private:
MatrixWorkspace_sptr m_loadedData;
IAlgorithm_sptr phaseQuad;
};