forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetMDFrameTest.h
255 lines (210 loc) · 10.1 KB
/
SetMDFrameTest.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
// 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 "MantidFrameworkTestHelpers/MDEventsTestHelper.h"
#include "MantidFrameworkTestHelpers/WorkspaceCreationHelper.h"
#include "MantidGeometry/MDGeometry/HKL.h"
#include "MantidGeometry/MDGeometry/QLab.h"
#include "MantidGeometry/MDGeometry/QSample.h"
#include "MantidGeometry/MDGeometry/UnknownFrame.h"
#include "MantidKernel/MDUnit.h"
#include "MantidMDAlgorithms/SetMDFrame.h"
#include <memory>
#include <utility>
using Mantid::MDAlgorithms::SetMDFrame;
class SetMDFrameTest : 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 SetMDFrameTest *createSuite() { return new SetMDFrameTest(); }
static void destroySuite(SetMDFrameTest *suite) { delete suite; }
void test_Init() {
SetMDFrame alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
}
void test_that_is_not_executed_when_non_mdevent_and_non_mdhisto() {
// Arrange
auto inputWorkspace = WorkspaceCreationHelper::create2DWorkspace(2, 5);
SetMDFrame alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
alg.setProperty("InputWorkspace", inputWorkspace);
// Act + Assert
TSM_ASSERT_THROWS_ANYTHING("Should not accept a MatrixWorkspace", alg.execute());
}
void test_that_accepts_mdevent_and_mdhisto() {
// Arrange
auto eventType = Mantid::DataObjects::MDEventsTestHelper::makeMDEW<2>(3, 0.0, 10.0, 1);
auto histoType = Mantid::DataObjects::MDEventsTestHelper::makeFakeMDHistoWorkspace(1.0, 2, 10, 10.0, 1.0, "A");
std::vector<int> axes;
axes.emplace_back(1);
SetMDFrame alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
auto dim0Selection = Mantid::Geometry::QLab::QLabName;
TS_ASSERT_THROWS_NOTHING(alg.setProperty("MDFrame", dim0Selection));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("Axes", axes));
// Act + Assert
alg.setProperty("InputWorkspace", eventType);
TSM_ASSERT_THROWS_NOTHING("Should accept an MDEvent workspace", alg.execute());
alg.setProperty("InputWorkspace", histoType);
TSM_ASSERT_THROWS_NOTHING("Should accept an MDHisto workspace", alg.execute());
}
void test_that_index_out_of_bounds_is_not_valid() {
// Arrange
auto eventType = Mantid::DataObjects::MDEventsTestHelper::makeMDEW<2>(3, 0.0, 10.0, 1);
auto histoType = Mantid::DataObjects::MDEventsTestHelper::makeFakeMDHistoWorkspace(1.0, 1, 10, 10.0, 1.0, "A");
std::vector<int> axes;
axes.emplace_back(0);
axes.emplace_back(7);
SetMDFrame alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
// Act + Assert
auto dim0Selection = Mantid::Geometry::QLab::QLabName;
TS_ASSERT_THROWS_NOTHING(alg.setProperty("MDFrame", dim0Selection));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("Axes", axes));
alg.setProperty("InputWorkspace", eventType);
TSM_ASSERT_THROWS_ANYTHING("Should not accept an index out of bounds", alg.execute());
}
void test_that_can_set_to_QLab() {
// Arrange
const size_t numberOfDimensions = 2;
std::vector<Mantid::Geometry::MDFrame_sptr> frames;
frames.emplace_back(std::make_shared<Mantid::Geometry::UnknownFrame>("test"));
frames.emplace_back(std::make_shared<Mantid::Geometry::UnknownFrame>("test"));
auto inputWorkspace =
Mantid::DataObjects::MDEventsTestHelper::makeMDEWWithIndividualFrames<numberOfDimensions>(5, -2, 2, frames, 3);
std::vector<int> axes;
axes.emplace_back(0);
// Act
SetMDFrame alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
alg.setProperty("InputWorkspace", inputWorkspace);
auto dim0Selection = Mantid::Geometry::QLab::QLabName;
TS_ASSERT_THROWS_NOTHING(alg.setProperty("MDFrame", dim0Selection));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("Axes", axes));
TSM_ASSERT_THROWS_NOTHING("Should accept an MDEvent workspace", alg.execute());
// Assert
auto dimension0 = inputWorkspace->getDimension(0);
TSM_ASSERT_EQUALS("Should be a QLab frame", dimension0->getMDFrame().name(), dim0Selection);
auto dimension1 = inputWorkspace->getDimension(1);
TSM_ASSERT_EQUALS("Should be an Unknown frame", dimension1->getMDFrame().name(),
Mantid::Geometry::UnknownFrame::UnknownFrameName);
}
void test_that_cannot_set_to_HKL_when_units_are_correct_on_multiple_axes() {
// Arrange
const size_t numberOfDimensions = 2;
std::vector<Mantid::Geometry::MDFrame_sptr> frames;
// Create unit which is suitable for HKL
auto unitFactory = Mantid::Kernel::makeMDUnitFactoryChain();
std::string unitString0 = "in 2.6437 A^-1";
std::string unitString1 = "in 1.6437 A^-1";
auto unit0 = unitFactory->create(unitString0);
auto unit1 = unitFactory->create(unitString1);
frames.emplace_back(std::make_shared<Mantid::Geometry::UnknownFrame>(std::move(unit0)));
frames.emplace_back(std::make_shared<Mantid::Geometry::UnknownFrame>(std::move(unit1)));
auto inputWorkspace =
Mantid::DataObjects::MDEventsTestHelper::makeMDEWWithIndividualFrames<numberOfDimensions>(5, -2, 2, frames, 3);
std::vector<int> axes;
axes.emplace_back(0);
axes.emplace_back(1);
// Act
SetMDFrame alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
alg.setProperty("InputWorkspace", inputWorkspace);
auto dim0Selection = Mantid::Geometry::HKL::HKLName;
TS_ASSERT_THROWS_NOTHING(alg.setProperty("MDFrame", dim0Selection));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("Axes", axes));
TSM_ASSERT_THROWS_NOTHING("Should accept an MDEvent workspace", alg.execute());
// Assert
auto dimension0 = inputWorkspace->getDimension(0);
TSM_ASSERT_EQUALS("Should be an HKL frame", dimension0->getMDFrame().name(), dim0Selection);
TSM_ASSERT_EQUALS("Should have the original units", dimension0->getMDFrame().getUnitLabel(), unitString0);
auto dimension1 = inputWorkspace->getDimension(1);
TSM_ASSERT_EQUALS("Should be an HKL frame", dimension1->getMDFrame().name(), dim0Selection);
TSM_ASSERT_EQUALS("Should have the original units", dimension1->getMDFrame().getUnitLabel(), unitString1);
}
void test_that_cannot_set_to_HKL_when_units_are_wrong() {
// Arrange
const size_t numberOfDimensions = 2;
std::vector<Mantid::Geometry::MDFrame_sptr> frames;
// Create unit which is unsuitable for HKL
auto unitFactory = Mantid::Kernel::makeMDUnitFactoryChain();
std::string unitString0 = "wrongUNits";
std::string unitString1 = "wrongUnits";
auto unit0 = unitFactory->create(unitString0);
auto unit1 = unitFactory->create(unitString1);
frames.emplace_back(std::make_shared<Mantid::Geometry::UnknownFrame>(std::move(unit0)));
frames.emplace_back(std::make_shared<Mantid::Geometry::UnknownFrame>(std::move(unit1)));
auto inputWorkspace =
Mantid::DataObjects::MDEventsTestHelper::makeMDEWWithIndividualFrames<numberOfDimensions>(5, -2, 2, frames, 3);
std::vector<int> axes;
axes.emplace_back(1);
// Act
SetMDFrame alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
alg.setProperty("InputWorkspace", inputWorkspace);
auto dim0Selection = Mantid::Geometry::HKL::HKLName;
TS_ASSERT_THROWS_NOTHING(alg.setProperty("MDFrame", dim0Selection));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("Axes", axes));
TSM_ASSERT_THROWS_ANYTHING("Should not accept a wrong HKL Units", alg.execute());
}
void test_that_can_convert_to_GeneralFrame() {
// Arrange
const size_t numberOfDimensions = 2;
std::vector<Mantid::Geometry::MDFrame_sptr> frames;
// Create unit which is suitable for HKL
auto unitFactory = Mantid::Kernel::makeMDUnitFactoryChain();
std::string unitString0 = "in 2.6437 A^-1";
std::string unitString1 = "in 1.6437 A^-1";
auto unit0 = unitFactory->create(unitString0);
auto unit1 = unitFactory->create(unitString1);
frames.emplace_back(std::make_shared<Mantid::Geometry::UnknownFrame>(std::move(unit0)));
frames.emplace_back(std::make_shared<Mantid::Geometry::UnknownFrame>(std::move(unit1)));
auto inputWorkspace =
Mantid::DataObjects::MDEventsTestHelper::makeMDEWWithIndividualFrames<numberOfDimensions>(5, -2, 2, frames, 3);
std::vector<int> axes;
axes.emplace_back(0);
axes.emplace_back(1);
// Act
SetMDFrame alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
alg.setProperty("InputWorkspace", inputWorkspace);
auto dim0Selection = Mantid::Geometry::GeneralFrame::GeneralFrameName;
TS_ASSERT_THROWS_NOTHING(alg.setProperty("MDFrame", dim0Selection));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("Axes", axes));
TSM_ASSERT_THROWS_NOTHING("Should accept an MDEvent workspace", alg.execute());
// Assert
auto dimension0 = inputWorkspace->getDimension(0);
TSM_ASSERT_EQUALS("Should be a General frame", dimension0->getMDFrame().name(), dim0Selection);
TSM_ASSERT_EQUALS("Should have the original units", dimension0->getMDFrame().getUnitLabel(), unitString0);
auto dimension1 = inputWorkspace->getDimension(1);
TSM_ASSERT_EQUALS("Should be a General frame", dimension1->getMDFrame().name(), dim0Selection);
TSM_ASSERT_EQUALS("Should have the original units", dimension1->getMDFrame().getUnitLabel(), unitString1);
}
};