Newer
Older
#ifndef MANTID_ALGORITHMS_SORTXAXISTEST_H_
#define MANTID_ALGORITHMS_SORTXAXISTEST_H_
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataObjects/WorkspaceCreation.h"
#include "MantidHistogramData/Histogram.h"
#include <cxxtest/TestSuite.h>
/**
Copyright © 2018 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
National Laboratory & European Spallation Source
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::Algorithms;
using namespace Mantid::DataObjects;
using namespace Mantid::HistogramData;
MatrixWorkspace_sptr createWorkspaceE(const std::vector<double> xData,
const std::vector<double> yData,
const std::vector<double> eData,
const int nSpec = 1) {
Workspace2D_sptr outputWorkspace = create<DataObjects::Workspace2D>(
nSpec, Mantid::HistogramData::Histogram(
Mantid::HistogramData::Points(xData.size())));
for (int i = 0; i < nSpec; ++i) {
outputWorkspace->mutableY(i) = yData;
outputWorkspace->mutableE(i) = eData;
outputWorkspace->mutableX(i) = xData;
}
return outputWorkspace;
}
MatrixWorkspace_sptr createHistoWorkspaceE(const std::vector<double> xData,
const std::vector<double> yData,
const std::vector<double> eData,
const int nSpec = 1) {
Workspace2D_sptr outputWorkspace = create<DataObjects::Workspace2D>(
nSpec, Mantid::HistogramData::Histogram(
Mantid::HistogramData::BinEdges(xData.size())));
for (int i = 0; i < nSpec; ++i) {
outputWorkspace->mutableY(i) = yData;
outputWorkspace->mutableE(i) = eData;
outputWorkspace->mutableX(i) = xData;
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
return outputWorkspace;
}
MatrixWorkspace_sptr createWorkspaceDx(const std::vector<double> xData,
const std::vector<double> yData,
const std::vector<double> dxData,
const int nSpec = 1) {
Workspace2D_sptr outputWorkspace = create<DataObjects::Workspace2D>(
nSpec, Mantid::HistogramData::Histogram(
Mantid::HistogramData::Points(xData.size())));
for (int i = 0; i < nSpec; ++i) {
outputWorkspace->mutableY(i) = yData;
outputWorkspace->mutableX(i) = xData;
outputWorkspace->setPointStandardDeviations(i, dxData);
}
return outputWorkspace;
}
MatrixWorkspace_sptr createHistoWorkspaceDx(const std::vector<double> xData,
const std::vector<double> yData,
const std::vector<double> dxData,
const int nSpec = 1) {
Workspace2D_sptr outputWorkspace = create<DataObjects::Workspace2D>(
nSpec, Mantid::HistogramData::Histogram(
Mantid::HistogramData::BinEdges(xData.size())));
for (int i = 0; i < nSpec; ++i) {
outputWorkspace->mutableY(i) = yData;
outputWorkspace->mutableX(i) = xData;
outputWorkspace->setPointStandardDeviations(i, dxData);
}
return outputWorkspace;
}
MatrixWorkspace_sptr createHistoWorkspace(const std::vector<double> xData,
const std::vector<double> yData,
const int nSpec = 1) {
Workspace2D_sptr outputWorkspace = create<DataObjects::Workspace2D>(
nSpec, Mantid::HistogramData::Histogram(
Mantid::HistogramData::BinEdges(xData.size())));
for (int i = 0; i < nSpec; ++i) {
outputWorkspace->mutableY(i) = yData;
outputWorkspace->mutableX(i) = xData;
}
return outputWorkspace;
}
} // namespace
class SortXAxisTest : public CxxTest::TestSuite {
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static SortXAxisTest *createSuite() { return new SortXAxisTest(); }
static void destroySuite(SortXAxisTest *suite) { delete suite; }
void testXAscending() {
std::vector<double> xData = {1, 2, 3};
std::vector<double> yData = {1, 2, 3};
std::vector<double> eData = {1, 2, 3};
MatrixWorkspace_sptr unsortedws = createWorkspaceE(xData, yData, eData);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
TS_ASSERT_THROWS_NOTHING(alg.execute());
MatrixWorkspace_sptr sortedws;
TS_ASSERT_THROWS_NOTHING(
sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"sortedws"));
TS_ASSERT(sortedws);
TS_ASSERT_EQUALS(sortedws->x(0), xData);
TS_ASSERT_EQUALS(sortedws->y(0), yData);
TS_ASSERT_EQUALS(sortedws->e(0), eData);
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("unsortedws"));
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("sortedws"));
void testXDescending() {
std::vector<double> xData = {3, 2, 1};
std::vector<double> sortedXData = {1, 2, 3};
std::vector<double> yData = {1, 2, 3};
std::vector<double> reverseYData = {3, 2, 1};
std::vector<double> eData = {1, 2, 3};
std::vector<double> reverseEData = {3, 2, 1};
MatrixWorkspace_sptr unsortedws = createWorkspaceE(xData, yData, eData);
SortXAxis alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
TS_ASSERT_THROWS_NOTHING(alg.execute());
MatrixWorkspace_sptr sortedws;
TS_ASSERT_THROWS_NOTHING(
sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"sortedws"));
TS_ASSERT(sortedws);
TS_ASSERT_EQUALS(sortedws->x(0), sortedXData);
TS_ASSERT_EQUALS(sortedws->y(0), reverseYData);
TS_ASSERT_EQUALS(sortedws->e(0), reverseEData);
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("unsortedws"));
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("sortedws"));
}
void testOnMultipleSpectrum() {
std::vector<double> xData = {3, 2, 1};
std::vector<double> sortedXData = {1, 2, 3};
std::vector<double> yData = {1, 2, 3};
std::vector<double> reverseYData = {3, 2, 1};
std::vector<double> eData = {1, 2, 3};
std::vector<double> reverseEData = {3, 2, 1};
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
MatrixWorkspace_sptr unsortedws = createWorkspaceE(xData, yData, eData, 2);
SortXAxis alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
TS_ASSERT_THROWS_NOTHING(alg.execute());
MatrixWorkspace_sptr sortedws;
TS_ASSERT_THROWS_NOTHING(
sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"sortedws"));
TS_ASSERT(sortedws);
TS_ASSERT_EQUALS(sortedws->x(0), sortedXData);
TS_ASSERT_EQUALS(sortedws->y(0), reverseYData);
TS_ASSERT_EQUALS(sortedws->e(0), reverseEData);
TS_ASSERT_EQUALS(sortedws->x(1), sortedXData);
TS_ASSERT_EQUALS(sortedws->y(1), reverseYData);
TS_ASSERT_EQUALS(sortedws->e(1), reverseEData);
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("unsortedws"));
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("sortedws"));
}
void testSortsXHistogramAscending() {
std::vector<double> xData = {1, 2, 3, 4};
std::vector<double> yData = {1, 2, 3};
std::vector<double> eData = {1, 2, 3};
MatrixWorkspace_sptr unsortedws =
createHistoWorkspaceE(xData, yData, eData);
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
SortXAxis alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
TS_ASSERT_THROWS_NOTHING(alg.execute());
MatrixWorkspace_sptr sortedws;
TS_ASSERT_THROWS_NOTHING(
sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"sortedws"));
TS_ASSERT(sortedws);
TS_ASSERT_EQUALS(sortedws->x(0), xData);
TS_ASSERT_EQUALS(sortedws->y(0), yData);
TS_ASSERT_EQUALS(sortedws->e(0), eData);
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("unsortedws"));
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("sortedws"));
}
void testSortsXHistogramDescending() {
std::vector<double> xData = {4, 3, 2, 1};
std::vector<double> sortedXData = {1, 2, 3, 4};
std::vector<double> yData = {1, 2, 3};
std::vector<double> reverseYData = {3, 2, 1};
std::vector<double> eData = {1, 2, 3};
std::vector<double> reverseEData = {3, 2, 1};
MatrixWorkspace_sptr unsortedws =
createHistoWorkspaceE(xData, yData, eData);
SortXAxis alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
TS_ASSERT_THROWS_NOTHING(alg.execute());
MatrixWorkspace_sptr sortedws;
TS_ASSERT_THROWS_NOTHING(
sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"sortedws"));
TS_ASSERT(sortedws);
TS_ASSERT_EQUALS(sortedws->x(0), sortedXData);
TS_ASSERT_EQUALS(sortedws->y(0), reverseYData);
TS_ASSERT_EQUALS(sortedws->e(0), reverseEData);
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("unsortedws"));
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("sortedws"));
}
void testDxMultipleSpectrum() {
std::vector<double> xData = {3, 2, 1};
std::vector<double> yData = {1, 2, 3};
std::vector<double> dxData = {1, 2, 3};
std::vector<double> reverseDxData = {3, 2, 1};
MatrixWorkspace_sptr unsortedws =
createWorkspaceDx(xData, yData, dxData, 2);
SortXAxis alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
TS_ASSERT_THROWS_NOTHING(alg.execute());
MatrixWorkspace_sptr sortedws;
TS_ASSERT_THROWS_NOTHING(
sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"sortedws"));
TS_ASSERT(sortedws);
TS_ASSERT_EQUALS(sortedws->dx(0), reverseDxData);
TS_ASSERT_EQUALS(sortedws->dx(1), reverseDxData);
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("unsortedws"));
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("sortedws"));
}
void testDxHistogramAscending() {
std::vector<double> xData = {1, 2, 3, 4};
std::vector<double> yData = {1, 2, 3};
std::vector<double> dxData = {1, 2, 3};
MatrixWorkspace_sptr unsortedws =
createHistoWorkspaceDx(xData, yData, dxData, 2);
SortXAxis alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
TS_ASSERT_THROWS_NOTHING(alg.execute());
MatrixWorkspace_sptr sortedws;
TS_ASSERT_THROWS_NOTHING(
sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"sortedws"));
TS_ASSERT(sortedws);
TS_ASSERT_EQUALS(sortedws->dx(0), dxData);
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("unsortedws"));
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("sortedws"));
}
void testSortDescending() {
std::vector<double> xData = {1, 2, 3, 4};
std::vector<double> reverseXData = {4, 3, 2, 1};
std::vector<double> yData = {1, 2, 3};
std::vector<double> reverseYData = {3, 2, 1};
MatrixWorkspace_sptr unsortedws = createHistoWorkspace(xData, yData, 2);
SortXAxis alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", unsortedws));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "sortedws"));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("Ordering", "Descending"));
TS_ASSERT_THROWS_NOTHING(alg.execute());
MatrixWorkspace_sptr sortedws;
TS_ASSERT_THROWS_NOTHING(
sortedws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"sortedws"));
TS_ASSERT(sortedws);
TS_ASSERT_EQUALS(sortedws->x(0), reverseXData);
TS_ASSERT_EQUALS(sortedws->y(0), reverseYData);
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("unsortedws"));
TS_ASSERT_THROWS_NOTHING(
AnalysisDataService::Instance().remove("sortedws"));
}
};
#endif /*MANTID_ALGORITHMS_SORTXAXISTEST_H_*/