Newer
Older
#include "MantidMDAlgorithms/IntegrateEllipsoidsTwoStep.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/Sample.h"
#include "MantidAPI/DetectorInfo.h"
#include "MantidKernel/NearestNeighbours.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidKernel/V3D.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidTestHelpers/SingleCrystalDiffractionTestHelper.h"
#include <cxxtest/TestSuite.h>
using namespace Mantid;
using namespace Mantid::MDAlgorithms;
using namespace Mantid::DataObjects;
using Mantid::Kernel::V3D;
using Mantid::Geometry::OrientedLattice;
using namespace Mantid::SingleCrystalDiffractionTestHelper;
class IntegrateEllipsoidsTwoStepTest : public CxxTest::TestSuite {
public:
void test_init() {
Mantid::MDAlgorithms::IntegrateEllipsoidsTwoStep alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
}
void test_exec_events_with_no_background() {
const int numEventsPerPeak = 10000;
// Very tight distribution with events happening at a single point
const auto sigmas = std::make_tuple(.002, .002, 0.1);
// Build some diffraction data
builder.addBackground(false);
builder.addPeakByHKL(V3D(1, -5, -3), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -4), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -3, -5), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -2), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, 0), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(2, -3, -4), numEventsPerPeak, sigmas);
auto data = builder.build();
auto eventWS = std::get<0>(data);
auto peaksWS = std::get<1>(data);
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
alg.setProperty("InputWorkspace", eventWS);
alg.setProperty("PeaksWorkspace", peaksWS);
TS_ASSERT_THROWS_NOTHING(alg.setProperty("SpecifySize", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("PeakSize", 0.35));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundInnerSize", 0.35));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundOuterSize", 0.4));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WeakPeakThreshold", 0.1));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IntegrateIfOnEdge", true));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "dummy"));
TS_ASSERT_THROWS_NOTHING(alg.execute() );
// Check output
TS_ASSERT( alg.isExecuted() );
PeaksWorkspace_sptr integratedPeaksWS = alg.getProperty("OutputWorkspace");
TS_ASSERT( integratedPeaksWS );
TSM_ASSERT_EQUALS("Wrong number of peaks in output workspace",
integratedPeaksWS->getNumberPeaks(),
peaksWS->getNumberPeaks());
const auto& run = integratedPeaksWS->mutableRun();
TSM_ASSERT("Output workspace must be integrated",
run.hasProperty("PeaksIntegrated"));
TSM_ASSERT_EQUALS("Output workspace must be integrated",
run.getProperty("PeaksIntegrated")->value(), "1");
for (int i = 0; i < 5; ++i) {
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(i),
integratedPeaksWS->getPeak(i).getIntensity(), numEventsPerPeak, 0.01);
}
}
void test_exec_histogram_with_no_background() {
const int numEventsPerPeak = 10000;
const auto sigmas = std::make_tuple(.002, .002, 0.01);
const std::vector<double> rebinParams = { 800, 5, 10000 };
builder.addBackground(false);
builder.outputAsHistogram(true);
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
builder.setRebinParameters(rebinParams);
builder.addPeakByHKL(V3D(1, -5, -3), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -4), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -3, -5), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -2), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, 0), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(2, -3, -4), numEventsPerPeak, sigmas);
auto data = builder.build();
auto histoWS = std::get<0>(data);
auto peaksWS = std::get<1>(data);
IntegrateEllipsoidsTwoStep alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", histoWS);
alg.setProperty("PeaksWorkspace", peaksWS);
TS_ASSERT_THROWS_NOTHING(alg.setProperty("SpecifySize", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("PeakSize", .5));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundInnerSize", .5));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundOuterSize", .6));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WeakPeakThreshold", 0.1));
alg.setPropertyValue("OutputWorkspace", "dummy");
alg.execute();
PeaksWorkspace_sptr integratedPeaksWS = alg.getProperty("OutputWorkspace");
TSM_ASSERT_EQUALS("Wrong number of peaks in output workspace",
integratedPeaksWS->getNumberPeaks(),
peaksWS->getNumberPeaks());
for (int i = 0; i < 5; ++i) {
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(i),
integratedPeaksWS->getPeak(i).getIntensity(), numEventsPerPeak, 0.01);
}
void test_exec_events_with_background() {
const int numEventsPerPeak = 10000;
// Very tight distribution with events happening at a single point
const auto sigmas = std::make_tuple(.002, .002, 0.1);
const auto backgroundDetSize = 0.05;
const auto backgroundTOFSize = 100.0;
const auto nBackgroundEvents = 1000;
// Build some diffraction data
builder.setNumPixels(100);
builder.addBackground(true);
builder.setBackgroundParameters(nBackgroundEvents, backgroundDetSize, backgroundTOFSize);
builder.addPeakByHKL(V3D(1, -5, -3), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -4), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -3, -5), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -2), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, 0), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(2, -3, -4), numEventsPerPeak, sigmas);
auto data = builder.build();
auto eventWS = std::get<0>(data);
auto peaksWS = std::get<1>(data);
// Run algorithm
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
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
alg.setProperty("InputWorkspace", eventWS);
alg.setProperty("PeaksWorkspace", peaksWS);
TS_ASSERT_THROWS_NOTHING(alg.setProperty("SpecifySize", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("PeakSize", 0.35));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundInnerSize", 0.35));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundOuterSize", 0.4));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WeakPeakThreshold", 0.1));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IntegrateIfOnEdge", true));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "dummy"));
TS_ASSERT_THROWS_NOTHING(alg.execute() );
// Check output
TS_ASSERT( alg.isExecuted() );
PeaksWorkspace_sptr integratedPeaksWS = alg.getProperty("OutputWorkspace");
TS_ASSERT( integratedPeaksWS );
TSM_ASSERT_EQUALS("Wrong number of peaks in output workspace",
integratedPeaksWS->getNumberPeaks(),
peaksWS->getNumberPeaks());
const auto& run = integratedPeaksWS->mutableRun();
TSM_ASSERT("Output workspace must be integrated",
run.hasProperty("PeaksIntegrated"));
TSM_ASSERT_EQUALS("Output workspace must be integrated",
run.getProperty("PeaksIntegrated")->value(), "1");
for (int i = 0; i < 5; ++i) {
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(i),
integratedPeaksWS->getPeak(i).getIntensity(), numEventsPerPeak, 450);
}
}
void test_exec_histogram_with_background() {
const int numEventsPerPeak = 10000;
const auto sigmas = std::make_tuple(.002, .002, 0.01);
const std::vector<double> rebinParams = { 800, 5, 10000 };
const auto backgroundDetSize = 0.05;
const auto backgroundTOFSize = 100.0;
const auto nBackgroundEvents = 1000;
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
builder.setNumPixels(100);
builder.addBackground(true);
builder.setBackgroundParameters(nBackgroundEvents, backgroundDetSize, backgroundTOFSize);
builder.outputAsHistogram(true);
builder.setRebinParameters(rebinParams);
builder.addPeakByHKL(V3D(1, -5, -3), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -4), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -3, -5), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -2), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, 0), numEventsPerPeak, sigmas);
builder.addPeakByHKL(V3D(2, -3, -4), numEventsPerPeak, sigmas);
auto data = builder.build();
auto histoWS = std::get<0>(data);
auto peaksWS = std::get<1>(data);
IntegrateEllipsoidsTwoStep alg;
alg.setChild(true);
alg.setRethrows(true);
alg.initialize();
alg.setProperty("InputWorkspace", histoWS);
alg.setProperty("PeaksWorkspace", peaksWS);
TS_ASSERT_THROWS_NOTHING(alg.setProperty("SpecifySize", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("PeakSize", .5));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundInnerSize", .5));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundOuterSize", .6));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WeakPeakThreshold", 0.1));
alg.setPropertyValue("OutputWorkspace", "dummy");
alg.execute();
PeaksWorkspace_sptr integratedPeaksWS = alg.getProperty("OutputWorkspace");
TSM_ASSERT_EQUALS("Wrong number of peaks in output workspace",
integratedPeaksWS->getNumberPeaks(),
peaksWS->getNumberPeaks());
for (int i = 0; i < 5; ++i) {
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(i),
integratedPeaksWS->getPeak(i).getIntensity(), numEventsPerPeak, 700);
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
void test_exec_events_with_weak_peaks() {
const int numEventsPerStrongPeak = 10000;
const int numEventsPerWeakPeak = 100;
// Very tight distribution with events happening at a single point
const auto sigmas = std::make_tuple(.002, .002, 0.1);
const auto backgroundDetSize = 0.05;
const auto backgroundTOFSize = 100.0;
const auto nBackgroundEvents = 1000;
// Build some diffraction data
WorkspaceBuilder builder;
builder.setNumPixels(100);
builder.addBackground(true);
builder.setBackgroundParameters(nBackgroundEvents, backgroundDetSize, backgroundTOFSize);
builder.addPeakByHKL(V3D(1, -5, -3), numEventsPerStrongPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -4), numEventsPerStrongPeak, sigmas);
builder.addPeakByHKL(V3D(1, -3, -5), numEventsPerWeakPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -2), numEventsPerWeakPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, 0), numEventsPerStrongPeak, sigmas);
builder.addPeakByHKL(V3D(2, -3, -4), numEventsPerStrongPeak, sigmas);
auto data = builder.build();
auto eventWS = std::get<0>(data);
auto peaksWS = std::get<1>(data);
// Run algorithm
IntegrateEllipsoidsTwoStep alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
alg.setProperty("InputWorkspace", eventWS);
alg.setProperty("PeaksWorkspace", peaksWS);
TS_ASSERT_THROWS_NOTHING(alg.setProperty("SpecifySize", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("PeakSize", 0.35));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundInnerSize", 0.35));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundOuterSize", 0.4));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WeakPeakThreshold", 5.0));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IntegrateIfOnEdge", true));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "dummy"));
TS_ASSERT_THROWS_NOTHING(alg.execute() );
// Check output
TS_ASSERT( alg.isExecuted() );
PeaksWorkspace_sptr integratedPeaksWS = alg.getProperty("OutputWorkspace");
TS_ASSERT( integratedPeaksWS );
TSM_ASSERT_EQUALS("Wrong number of peaks in output workspace",
integratedPeaksWS->getNumberPeaks(),
peaksWS->getNumberPeaks());
const auto& run = integratedPeaksWS->mutableRun();
TSM_ASSERT("Output workspace must be integrated",
run.hasProperty("PeaksIntegrated"));
TSM_ASSERT_EQUALS("Output workspace must be integrated",
run.getProperty("PeaksIntegrated")->value(), "1");
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(0),
integratedPeaksWS->getPeak(0).getIntensity(), numEventsPerStrongPeak, 300);
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(1),
integratedPeaksWS->getPeak(1).getIntensity(), numEventsPerStrongPeak, 300);
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(2),
integratedPeaksWS->getPeak(2).getIntensity(), numEventsPerWeakPeak, 100);
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(3),
integratedPeaksWS->getPeak(3).getIntensity(), numEventsPerWeakPeak, 100);
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(4),
integratedPeaksWS->getPeak(4).getIntensity(), numEventsPerStrongPeak, 450);
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(5),
integratedPeaksWS->getPeak(5).getIntensity(), numEventsPerStrongPeak, 800);
}
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
void test_exec_events_with_adaptive_q() {
const int numEventsPerStrongPeak = 10000;
const int numEventsPerWeakPeak = 100;
// Very tight distribution with events happening at a single point
const auto sigmas = std::make_tuple(.002, .002, 0.1);
const auto backgroundDetSize = 0.05;
const auto backgroundTOFSize = 100.0;
const auto nBackgroundEvents = 1000;
// Build some diffraction data
WorkspaceBuilder builder;
builder.setNumPixels(100);
builder.addBackground(true);
builder.setBackgroundParameters(nBackgroundEvents, backgroundDetSize, backgroundTOFSize);
builder.addPeakByHKL(V3D(1, -5, -3), numEventsPerStrongPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -4), numEventsPerStrongPeak, sigmas);
builder.addPeakByHKL(V3D(2, -3, -4), numEventsPerStrongPeak, sigmas);
builder.addPeakByHKL(V3D(1, -3, -5), numEventsPerWeakPeak, sigmas);
builder.addPeakByHKL(V3D(1, -4, -2), numEventsPerWeakPeak, sigmas);
auto data = builder.build();
auto eventWS = std::get<0>(data);
auto peaksWS = std::get<1>(data);
// Run algorithm
IntegrateEllipsoidsTwoStep alg;
alg.setChild(true);
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
alg.setProperty("InputWorkspace", eventWS);
alg.setProperty("PeaksWorkspace", peaksWS);
TS_ASSERT_THROWS_NOTHING(alg.setProperty("SpecifySize", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("PeakSize", 0.35));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundInnerSize", 0.35));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("BackgroundOuterSize", 0.4));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("WeakPeakThreshold", 100.0));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("IntegrateIfOnEdge", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("AdaptiveQBackground", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("AdaptiveQMultiplier", 0.01));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "dummy"));
TS_ASSERT_THROWS_NOTHING(alg.execute() );
// Check output
TS_ASSERT( alg.isExecuted() );
PeaksWorkspace_sptr integratedPeaksWS = alg.getProperty("OutputWorkspace");
TS_ASSERT( integratedPeaksWS );
TSM_ASSERT_EQUALS("Wrong number of peaks in output workspace",
integratedPeaksWS->getNumberPeaks(),
peaksWS->getNumberPeaks());
const auto& run = integratedPeaksWS->mutableRun();
TSM_ASSERT("Output workspace must be integrated",
run.hasProperty("PeaksIntegrated"));
TSM_ASSERT_EQUALS("Output workspace must be integrated",
run.getProperty("PeaksIntegrated")->value(), "1");
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(0),
integratedPeaksWS->getPeak(0).getIntensity(), numEventsPerStrongPeak, 150);
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(1),
integratedPeaksWS->getPeak(1).getIntensity(), numEventsPerStrongPeak, 150);
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(2),
integratedPeaksWS->getPeak(2).getIntensity(), numEventsPerStrongPeak, 900);
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(3),
integratedPeaksWS->getPeak(3).getIntensity(), numEventsPerWeakPeak, 300);
TSM_ASSERT_DELTA("Wrong intensity for peak " + std::to_string(4),
integratedPeaksWS->getPeak(4).getIntensity(), numEventsPerWeakPeak, 300);
}