-
Notifications
You must be signed in to change notification settings - Fork 122
/
SmoothNeighbours.cpp
790 lines (675 loc) · 28.8 KB
/
SmoothNeighbours.cpp
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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
// 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 +
#include "MantidAlgorithms/SmoothNeighbours.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceNearestNeighbourInfo.h"
#include "MantidDataObjects/EventList.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/OffsetsWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidGeometry/ICompAssembly.h"
#include "MantidGeometry/IComponent.h"
#include "MantidGeometry/Instrument/DetectorGroup.h"
#include "MantidGeometry/Instrument/DetectorInfo.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/EnabledWhenProperty.h"
#include "MantidKernel/ListValidator.h"
#include <boost/algorithm/string.hpp>
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
using VecProperties = std::vector<Mantid::Kernel::Property *>;
using ConstVecProperties = const VecProperties;
namespace Mantid {
namespace Algorithms {
// Register the class into the algorithm factory
DECLARE_ALGORITHM(SmoothNeighbours)
// Used in custom GUI. Make sure you change them in SmoothNeighboursDialog.cpp
// as well.
const std::string SmoothNeighbours::NON_UNIFORM_GROUP = "NonUniform Detectors";
const std::string SmoothNeighbours::RECTANGULAR_GROUP = "Rectangular Detectors";
const std::string SmoothNeighbours::INPUT_WORKSPACE = "InputWorkspace";
SmoothNeighbours::SmoothNeighbours()
: API::Algorithm(), AdjX(0), AdjY(0), Edge(0), Radius(0.), nNeighbours(0),
WeightedSum(new NullWeighting), PreserveEvents(false),
expandSumAllPixels(false), outWI(0), inWS(), m_neighbours(),
m_progress(nullptr) {}
/** Initialisation method.
*
*/
void SmoothNeighbours::init() {
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(
INPUT_WORKSPACE, "", Direction::Input,
std::make_shared<InstrumentValidator>()),
"The workspace containing the spectra to be averaged.");
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"The name of the workspace to be created as the output of "
"the algorithm.");
// Unsigned double
auto mustBePositiveDouble = std::make_shared<BoundedValidator<double>>();
mustBePositiveDouble->setLower(0.0);
// Unsigned int.
auto mustBePositive = std::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(0);
std::vector<std::string> propOptions{"Flat", "Linear", "Parabolic",
"Gaussian"};
declareProperty("WeightedSum", "Flat",
std::make_shared<StringListValidator>(propOptions),
"What sort of Weighting scheme to use?\n"
" Flat: Effectively no-weighting, all weights are 1.\n"
" Linear: Linear weighting 1 - r/R from origin.\n"
" Parabolic : Weighting as cutoff - x + cutoff - y + 1."
" Gaussian : Uses the absolute distance x^2 + y^2 ... "
"normalised by the cutoff^2");
declareProperty(
"Sigma", 0.5, mustBePositiveDouble,
"Sigma value for gaussian weighting schemes. Defaults to 0.5. ");
setPropertySettings("Sigma", std::make_unique<EnabledWhenProperty>(
"WeightedSum", IS_EQUAL_TO, "Gaussian"));
declareProperty(
"IgnoreMaskedDetectors", true,
"If true, do not consider masked detectors in the NN search.");
declareProperty("PreserveEvents", true,
"If the InputWorkspace is an "
"EventWorkspace, this will preserve "
"the full event list (warning: this "
"will use much more memory!).");
// -- Rectangular properties
// ----------------------------------------------------------------------
declareProperty(
"AdjX", 1, mustBePositive,
"The number of X (horizontal) adjacent pixels to average together. "
"Only for instruments with RectangularDetectors. ");
declareProperty(
"AdjY", 1, mustBePositive,
"The number of Y (vertical) adjacent pixels to average together. "
"Only for instruments with RectangularDetectors. ");
declareProperty(
"SumPixelsX", 1, mustBePositive,
"The total number of X (horizontal) adjacent pixels to sum together. "
"Only for instruments with RectangularDetectors. AdjX will be ignored "
"if SumPixelsX > 1.");
declareProperty(
"SumPixelsY", 1, mustBePositive,
"The total number of Y (vertical) adjacent pixels to sum together. "
"Only for instruments with RectangularDetectors. AdjY will be ignored if "
"SumPixelsY > 1");
declareProperty("ZeroEdgePixels", 0, mustBePositive,
"The number of pixels to zero at edges. "
"Only for instruments with RectangularDetectors. ");
setPropertyGroup("AdjX", RECTANGULAR_GROUP);
setPropertyGroup("AdjY", RECTANGULAR_GROUP);
setPropertyGroup("SumPixelsX", RECTANGULAR_GROUP);
setPropertyGroup("SumPixelsY", RECTANGULAR_GROUP);
setPropertyGroup("ZeroEdgePixels", RECTANGULAR_GROUP);
// -- Non-uniform properties
// ----------------------------------------------------------------------
std::vector<std::string> radiusPropOptions{"Meters", "NumberOfPixels"};
declareProperty(
"RadiusUnits", "Meters",
std::make_shared<StringListValidator>(radiusPropOptions),
"Units used to specify the radius.\n"
" Meters : Radius is in meters.\n"
" NumberOfPixels : Radius is in terms of the number of pixels.");
declareProperty(
"Radius", 0.0, mustBePositiveDouble,
"The radius cut-off around a pixel to look for nearest neighbours to "
"average. \n"
"This radius cut-off is applied to a set of nearest neighbours whose "
"number is "
"defined in the NumberOfNeighbours property. See below for more details. "
"\n"
"If 0, will use the AdjX and AdjY parameters for rectangular detectors "
"instead.");
declareProperty("NumberOfNeighbours", 8, mustBePositive,
"Number of nearest neighbouring pixels.\n"
"The default is 8.");
declareProperty("SumNumberOfNeighbours", 1,
"Sum nearest neighbouring pixels with same parent.\n"
"Number of pixels will be reduced. The default is false.");
declareProperty("ExpandSumAllPixels", false,
"OuputWorkspace will have same number of pixels as "
"InputWorkspace using SumPixelsX and SumPixelsY. Individual "
"pixels will have averages.");
setPropertyGroup("RadiusUnits", NON_UNIFORM_GROUP);
setPropertyGroup("Radius", NON_UNIFORM_GROUP);
setPropertyGroup("NumberOfNeighbours", NON_UNIFORM_GROUP);
setPropertyGroup("SumNumberOfNeighbours", NON_UNIFORM_GROUP);
}
//--------------------------------------------------------------------------------------------
/** Fill the neighbours list given the AdjX AdjY parameters and an
* instrument with rectangular detectors.
*/
void SmoothNeighbours::findNeighboursRectangular() {
g_log.debug("SmoothNeighbours processing assuming rectangular detectors.");
m_progress->resetNumSteps(inWS->getNumberHistograms(), 0.2, 0.5);
Instrument_const_sptr inst = inWS->getInstrument();
// To get the workspace index from the detector ID
const detid2index_map pixel_to_wi =
inWS->getDetectorIDToWorkspaceIndexMap(true);
// std::cout << " inst->nelements() " << inst->nelements() << "\n";
Progress prog(this, 0.0, 1.0, inst->nelements());
// Build a list of Rectangular Detectors
std::vector<std::shared_ptr<RectangularDetector>> detList;
for (int i = 0; i < inst->nelements(); i++) {
std::shared_ptr<RectangularDetector> det;
std::shared_ptr<ICompAssembly> assem;
std::shared_ptr<ICompAssembly> assem2;
det = std::dynamic_pointer_cast<RectangularDetector>((*inst)[i]);
if (det) {
detList.emplace_back(det);
} else {
// Also, look in the first sub-level for RectangularDetectors (e.g. PG3).
// We are not doing a full recursive search since that will be very long
// for lots of pixels.
assem = std::dynamic_pointer_cast<ICompAssembly>((*inst)[i]);
if (assem) {
for (int j = 0; j < assem->nelements(); j++) {
det = std::dynamic_pointer_cast<RectangularDetector>((*assem)[j]);
if (det) {
detList.emplace_back(det);
} else {
// Also, look in the second sub-level for RectangularDetectors (e.g.
// PG3).
// We are not doing a full recursive search since that will be very
// long for lots of pixels.
assem2 = std::dynamic_pointer_cast<ICompAssembly>((*assem)[j]);
if (assem2) {
for (int k = 0; k < assem2->nelements(); k++) {
det = std::dynamic_pointer_cast<RectangularDetector>(
(*assem2)[k]);
if (det) {
detList.emplace_back(det);
}
}
}
}
}
}
}
}
if (detList.empty()) {
// Not rectangular so use Nearest Neighbours
Radius = translateToMeters("NumberOfPixels", std::max(AdjX, AdjY));
setWeightingStrategy("Flat", Radius);
nNeighbours = AdjX * AdjY - 1;
findNeighboursUbiqutious();
}
// Resize the vector we are setting
m_neighbours.resize(inWS->getNumberHistograms());
int StartX = -AdjX;
int StartY = -AdjY;
int EndX = AdjX;
int EndY = AdjY;
int SumX = getProperty("SumPixelsX");
int SumY = getProperty("SumPixelsY");
bool sum = SumX * SumY > 1;
if (sum) {
StartX = 0;
StartY = 0;
EndX = SumX - 1;
EndY = SumY - 1;
}
outWI = 0;
// Build a map to sort by the detectorID
std::vector<std::pair<int, int>> v1;
for (int i = 0; i < static_cast<int>(detList.size()); i++)
v1.emplace_back(detList[i]->getAtXY(0, 0)->getID(), i);
// To sort in descending order
if (sum)
stable_sort(v1.begin(), v1.end());
std::vector<std::pair<int, int>>::iterator Iter1;
// Loop through the RectangularDetector's we listed before.
for (Iter1 = v1.begin(); Iter1 != v1.end(); ++Iter1) {
int i = (*Iter1).second;
std::shared_ptr<RectangularDetector> det = detList[i];
std::string det_name = det->getName();
if (det) {
for (int j = 0; j < det->xpixels(); j += SumX) {
for (int k = 0; k < det->ypixels(); k += SumY) {
double totalWeight = 0;
// Neighbours and weights
std::vector<weightedNeighbour> neighbours;
for (int ix = StartX; ix <= EndX; ix++)
for (int iy = StartY; iy <= EndY; iy++) {
// Weights for corners=1; higher for center and adjacent pixels
double smweight = WeightedSum->weightAt(AdjX, ix, AdjY, iy);
// Find the pixel ID at that XY position on the rectangular
// detector
if (j + ix >= det->xpixels() - Edge || j + ix < Edge)
continue;
if (k + iy >= det->ypixels() - Edge || k + iy < Edge)
continue;
int pixelID = det->getAtXY(j + ix, k + iy)->getID();
// Find the corresponding workspace index, if any
auto mapEntry = pixel_to_wi.find(pixelID);
if (mapEntry != pixel_to_wi.end()) {
size_t wi = mapEntry->second;
neighbours.emplace_back(wi, smweight);
// Count the total weight
totalWeight += smweight;
}
}
// Adjust the weights of each neighbour to normalize to unity
if (!sum || expandSumAllPixels)
for (auto &neighbour : neighbours)
neighbour.second /= totalWeight;
// Save the list of neighbours for this output workspace index.
m_neighbours[outWI] = neighbours;
outWI++;
m_progress->report("Finding Neighbours");
}
}
}
prog.report(det_name);
}
}
//--------------------------------------------------------------------------------------------
/** Use NearestNeighbours to find the neighbours for any instrument
*/
void SmoothNeighbours::findNeighboursUbiqutious() {
g_log.debug(
"SmoothNeighbours processing NOT assuming rectangular detectors.");
m_progress->resetNumSteps(inWS->getNumberHistograms(), 0.2, 0.5);
this->progress(0.2, "Building Neighbour Map");
Instrument_const_sptr inst = inWS->getInstrument();
const spec2index_map spec2index = inWS->getSpectrumToWorkspaceIndexMap();
// Resize the vector we are setting
m_neighbours.resize(inWS->getNumberHistograms());
bool ignoreMaskedDetectors = getProperty("IgnoreMaskedDetectors");
WorkspaceNearestNeighbourInfo neighbourInfo(*inWS, ignoreMaskedDetectors,
nNeighbours);
// Cull by radius
RadiusFilter radiusFilter(Radius);
// Go through every input workspace pixel
outWI = 0;
int sum = getProperty("SumNumberOfNeighbours");
std::shared_ptr<const Geometry::IComponent> parent, neighbParent, grandparent,
neighbGParent;
auto used = new bool[inWS->getNumberHistograms()];
if (sum > 1) {
for (size_t wi = 0; wi < inWS->getNumberHistograms(); wi++)
used[wi] = false;
}
const auto &detectorInfo = inWS->detectorInfo();
for (size_t wi = 0; wi < inWS->getNumberHistograms(); wi++) {
if (sum > 1)
if (used[wi])
continue;
// We want to skip monitors
try {
// Get the list of detectors in this pixel
const auto &dets = inWS->getSpectrum(wi).getDetectorIDs();
const auto index = detectorInfo.indexOf(*dets.begin());
if (detectorInfo.isMonitor(index))
continue; // skip monitor
if (detectorInfo.isMasked(index)) {
// Calibration masks many detectors, but there should be 0s after
// smoothing
if (sum == 1)
outWI++;
continue; // skip masked detectors
}
if (sum > 1) {
const auto &det = detectorInfo.detector(index);
parent = det.getParent();
if (parent)
grandparent = parent->getParent();
}
} catch (Kernel::Exception::NotFoundError &) {
continue; // skip missing detector
}
specnum_t inSpec = inWS->getSpectrum(wi).getSpectrumNo();
// Step one - Get the number of specified neighbours
SpectraDistanceMap insideGrid = neighbourInfo.getNeighboursExact(inSpec);
// Step two - Filter the results by the radius cut off.
SpectraDistanceMap neighbSpectra = radiusFilter.apply(insideGrid);
// Force the central pixel to always be there
// There seems to be a bug in nearestNeighbours, returns distance != 0.0 for
// the central pixel. So we force distance = 0
neighbSpectra[inSpec] = V3D(0.0, 0.0, 0.0);
// Neighbours and weights list
double totalWeight = 0;
int noNeigh = 0;
std::vector<weightedNeighbour> neighbours;
// Convert from spectrum numbers to workspace indices
for (auto &specDistance : neighbSpectra) {
specnum_t spec = specDistance.first;
// Use the weighting strategy to calculate the weight.
double weight = WeightedSum->weightAt(specDistance.second);
if (weight > 0) {
// Find the corresponding workspace index
auto mapIt = spec2index.find(spec);
if (mapIt != spec2index.end()) {
size_t neighWI = mapIt->second;
if (sum > 1) {
// Get the list of detectors in this pixel
const std::set<detid_t> &dets =
inWS->getSpectrum(neighWI).getDetectorIDs();
const auto &det = detectorInfo.detector(*dets.begin());
neighbParent = det.getParent();
neighbGParent = neighbParent->getParent();
if (noNeigh >= sum ||
neighbParent->getName() != parent->getName() ||
neighbGParent->getName() != grandparent->getName() ||
used[neighWI])
continue;
noNeigh++;
used[neighWI] = true;
}
neighbours.emplace_back(neighWI, weight);
totalWeight += weight;
}
}
}
// Adjust the weights of each neighbour to normalize to unity
if (sum == 1)
for (auto &neighbour : neighbours)
neighbour.second /= totalWeight;
// Save the list of neighbours for this output workspace index.
m_neighbours[outWI] = neighbours;
outWI++;
m_progress->report("Finding Neighbours");
} // each workspace index
delete[] used;
}
/**
Attempts to reset the Weight based on the strategyName provided. Note that if
these conditional statements fail to override the existing WeightedSum member,
it should stay as a NullWeighting, which will throw during usage.
@param strategyName : The name of the weighting strategy to use
@param cutOff : The cutoff distance
*/
void SmoothNeighbours::setWeightingStrategy(const std::string &strategyName,
double &cutOff) {
if (strategyName == "Flat") {
boost::scoped_ptr<WeightingStrategy> flatStrategy(new FlatWeighting);
WeightedSum.swap(flatStrategy);
g_log.information("Smoothing with Flat Weighting");
} else if (strategyName == "Linear") {
boost::scoped_ptr<WeightingStrategy> linearStrategy(
new LinearWeighting(cutOff));
WeightedSum.swap(linearStrategy);
g_log.information("Smoothing with Linear Weighting");
} else if (strategyName == "Parabolic") {
boost::scoped_ptr<WeightingStrategy> parabolicStrategy(
new ParabolicWeighting(cutOff));
WeightedSum.swap(parabolicStrategy);
g_log.information("Smoothing with Parabolic Weighting");
} else if (strategyName == "Gaussian") {
double sigma = getProperty("Sigma");
boost::scoped_ptr<WeightingStrategy> gaussian1DStrategy(
new GaussianWeightingnD(cutOff, sigma));
WeightedSum.swap(gaussian1DStrategy);
g_log.information("Smoothing with Gaussian Weighting");
}
}
/**
Translate the radius into meters.
@param radiusUnits : The name of the radius units
@param enteredRadius : The numerical value of the radius in whatever units have
been specified
*/
double SmoothNeighbours::translateToMeters(const std::string &radiusUnits,
const double &enteredRadius) const {
double translatedRadius = 0;
if (radiusUnits == "Meters") {
// Nothing more to do.
translatedRadius = enteredRadius;
} else if (radiusUnits == "NumberOfPixels") {
// Get the first idetector from the workspace index 0.
const auto &firstDet = inWS->spectrumInfo().detector(0);
// Find the bounding box of that detector
BoundingBox bbox;
firstDet.getBoundingBox(bbox);
// Multiply (meters/pixels) by number of pixels, note that enteredRadius
// takes on meaning of the number of pixels.
translatedRadius = bbox.width().norm() * enteredRadius;
} else {
const std::string message =
"SmoothNeighbours::translateToMeters, Unknown Unit: " + radiusUnits;
throw std::invalid_argument(message);
}
return translatedRadius;
}
/**
Check whether the properties provided are all in their default state.
@param properties : Vector of mantid property pointers
@return True only if they are all default, otherwise False.
*/
bool areAllDefault(ConstVecProperties &properties) {
for (auto property : properties) {
if (!property->isDefault()) {
return false;
}
}
return true;
}
//--------------------------------------------------------------------------------------------
/** Executes the algorithm
*
*/
void SmoothNeighbours::exec() {
inWS = getProperty("InputWorkspace");
PreserveEvents = getProperty("PreserveEvents");
expandSumAllPixels = getProperty("ExpandSumAllPixels");
// Use the unit type to translate the entered radius into meters.
Radius = translateToMeters(getProperty("RadiusUnits"), getProperty("Radius"));
setWeightingStrategy(getProperty("WeightedSum"), Radius);
AdjX = getProperty("AdjX");
AdjY = getProperty("AdjY");
Edge = getProperty("ZeroEdgePixels");
nNeighbours = getProperty("NumberOfNeighbours");
// Progress reporting, first for the sorting
m_progress =
std::make_unique<Progress>(this, 0.0, 0.2, inWS->getNumberHistograms());
// Run the appropriate method depending on the type of the instrument
if (inWS->getInstrument()->containsRectDetectors() ==
Instrument::ContainsState::Full)
findNeighboursRectangular();
else
findNeighboursUbiqutious();
EventWorkspace_sptr wsEvent = std::dynamic_pointer_cast<EventWorkspace>(inWS);
if (wsEvent)
wsEvent->sortAll(TOF_SORT, m_progress.get());
if (!wsEvent || !PreserveEvents)
this->execWorkspace2D();
else if (wsEvent)
this->execEvent(wsEvent);
else
throw std::runtime_error("This algorithm requires a Workspace2D or "
"EventWorkspace as its input.");
}
//--------------------------------------------------------------------------------------------
/** Execute the algorithm for a Workspace2D/don't preserve events input */
void SmoothNeighbours::execWorkspace2D() {
m_progress->resetNumSteps(inWS->getNumberHistograms(), 0.5, 1.0);
// Get some stuff from the input workspace
const size_t numberOfSpectra = outWI;
const size_t YLength = inWS->blocksize();
MatrixWorkspace_sptr outWS;
// Make a brand new Workspace2D
if (std::dynamic_pointer_cast<OffsetsWorkspace>(inWS)) {
g_log.information() << "Creating new OffsetsWorkspace\n";
outWS = MatrixWorkspace_sptr(new OffsetsWorkspace(inWS->getInstrument()));
} else {
outWS = std::dynamic_pointer_cast<MatrixWorkspace>(
API::WorkspaceFactory::Instance().create("Workspace2D", numberOfSpectra,
YLength + 1, YLength));
}
this->setProperty("OutputWorkspace", outWS);
setupNewInstrument(*outWS);
// Copy geometry over.
// API::WorkspaceFactory::Instance().initializeFromParent(inWS, outWS, false);
// Go through all the output workspace
PARALLEL_FOR_IF(Kernel::threadSafe(*inWS, *outWS))
for (int outWIi = 0; outWIi < int(numberOfSpectra); outWIi++) {
PARALLEL_START_INTERUPT_REGION
auto &outSpec = outWS->getSpectrum(outWIi);
// Reset the Y and E vectors
outSpec.clearData();
auto &outY = outSpec.mutableY();
// We will temporarily carry the squared error
auto &outE = outSpec.mutableE();
// tmp to carry the X Data.
auto &outX = outSpec.mutableX();
// Which are the neighbours?
std::vector<weightedNeighbour> &neighbours = m_neighbours[outWIi];
std::vector<weightedNeighbour>::iterator it;
for (it = neighbours.begin(); it != neighbours.end(); ++it) {
size_t inWI = it->first;
double weight = it->second;
double weightSquared = weight * weight;
const auto &inSpec = inWS->getSpectrum(inWI);
const auto &inY = inSpec.y();
const auto &inE = inSpec.e();
const auto &inX = inSpec.x();
for (size_t i = 0; i < YLength; i++) {
// Add the weighted signal
outY[i] += inY[i] * weight;
// Square the error, scale by weight (which you have to square too),
// then add in quadrature
double errorSquared = inE[i];
errorSquared *= errorSquared;
errorSquared *= weightSquared;
outE[i] += errorSquared;
// Copy the X values as well
outX[i] = inX[i];
}
if (inWS->isHistogramData()) {
outX[YLength] = inX[YLength];
}
} //(each neighbour)
// Now un-square the error, since we summed it in quadrature
for (size_t i = 0; i < YLength; i++)
outE[i] = sqrt(outE[i]);
// Copy the single detector ID (of the center) and spectrum number from the
// input workspace
// outSpec->copyInfoFrom(*inWS->getSpectrum(outWIi));
m_progress->report("Summing");
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
if (expandSumAllPixels)
spreadPixels(outWS);
}
//--------------------------------------------------------------------------------------------
/** Build the instrument/detector setup in workspace
*/
void SmoothNeighbours::setupNewInstrument(MatrixWorkspace &outws) const {
// Copy geometry over.
API::WorkspaceFactory::Instance().initializeFromParent(*inWS, outws, false);
// Go through all the output workspace
size_t numberOfSpectra = outws.getNumberHistograms();
for (int outWIi = 0; outWIi < int(numberOfSpectra); outWIi++) {
auto &outSpec = outws.getSpectrum(outWIi);
// Reset detectors
outSpec.clearDetectorIDs();
// Which are the neighbours?
for (const auto &neighbor : m_neighbours[outWIi]) {
const auto &inSpec = inWS->getSpectrum(neighbor.first);
outSpec.addDetectorIDs(inSpec.getDetectorIDs());
}
}
}
//--------------------------------------------------------------------------------------------
/** Spread the average over all the pixels
*/
void SmoothNeighbours::spreadPixels(const MatrixWorkspace_sptr &outws) {
// Get some stuff from the input workspace
const size_t numberOfSpectra = inWS->getNumberHistograms();
const size_t YLength = inWS->blocksize();
MatrixWorkspace_sptr outws2;
// Make a brand new Workspace2D
if (std::dynamic_pointer_cast<OffsetsWorkspace>(inWS)) {
g_log.information() << "Creating new OffsetsWorkspace\n";
outws2 = MatrixWorkspace_sptr(new OffsetsWorkspace(inWS->getInstrument()));
} else {
outws2 = std::dynamic_pointer_cast<MatrixWorkspace>(
API::WorkspaceFactory::Instance().create("Workspace2D", numberOfSpectra,
YLength + 1, YLength));
}
// Copy geometry over.
API::WorkspaceFactory::Instance().initializeFromParent(*inWS, *outws2, false);
// Go through all the input workspace
for (int outWIi = 0; outWIi < int(numberOfSpectra); outWIi++) {
const auto &inSpec = inWS->getSpectrum(outWIi);
auto &outSpec2 = outws2->getSpectrum(outWIi);
outSpec2.mutableX() = inSpec.x();
outSpec2.addDetectorIDs(inSpec.getDetectorIDs());
// Zero the Y and E vectors
outSpec2.clearData();
}
// Go through all the output workspace
const size_t numberOfSpectra2 = outws->getNumberHistograms();
for (int outWIi = 0; outWIi < int(numberOfSpectra2); outWIi++) {
// Which are the neighbours?
for (const auto &neighbor : m_neighbours[outWIi]) {
outws2->setHistogram(neighbor.first, outws->histogram(outWIi));
}
}
this->setProperty("OutputWorkspace", outws2);
}
//--------------------------------------------------------------------------------------------
/** Execute the algorithm for a EventWorkspace input
* @param ws :: EventWorkspace
*/
void SmoothNeighbours::execEvent(Mantid::DataObjects::EventWorkspace_sptr &ws) {
m_progress->resetNumSteps(inWS->getNumberHistograms(), 0.5, 1.0);
// Get some stuff from the input workspace
const size_t numberOfSpectra = outWI;
const auto YLength = static_cast<int>(inWS->blocksize());
EventWorkspace_sptr outWS;
// Make a brand new EventWorkspace
outWS = std::dynamic_pointer_cast<EventWorkspace>(
API::WorkspaceFactory::Instance().create(
"EventWorkspace", numberOfSpectra, YLength + 1, YLength));
// Copy geometry over.
API::WorkspaceFactory::Instance().initializeFromParent(*ws, *outWS, false);
// Ensure thread-safety
outWS->sortAll(TOF_SORT, nullptr);
this->setProperty("OutputWorkspace",
std::dynamic_pointer_cast<MatrixWorkspace>(outWS));
// Go through all the output workspace
PARALLEL_FOR_IF(Kernel::threadSafe(*ws, *outWS))
for (int outWIi = 0; outWIi < int(numberOfSpectra); outWIi++) {
PARALLEL_START_INTERUPT_REGION
// Create the output event list (empty)
EventList &outEL = outWS->getSpectrum(outWIi);
// Which are the neighbours?
std::vector<weightedNeighbour> &neighbours = m_neighbours[outWIi];
std::vector<weightedNeighbour>::iterator it;
for (it = neighbours.begin(); it != neighbours.end(); ++it) {
size_t inWI = it->first;
// if(sum)outEL.copyInfoFrom(*ws->getSpectrum(inWI));
double weight = it->second;
// Copy the event list
EventList tmpEL = ws->getSpectrum(inWI);
// Scale it
tmpEL *= weight;
// Add it
outEL += tmpEL;
}
// Copy the single detector ID (of the center) and spectrum number from the
// input workspace
// if (!sum) outEL.copyInfoFrom(*ws->getSpectrum(outWIi));
m_progress->report("Summing");
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
// Give the 0-th X bins to all the output spectra.
outWS->setAllX(inWS->binEdges(0));
if (expandSumAllPixels)
spreadPixels(outWS);
}
} // namespace Algorithms
} // namespace Mantid