-
Notifications
You must be signed in to change notification settings - Fork 121
/
FindSXPeaksHelper.cpp
843 lines (706 loc) · 30.9 KB
/
FindSXPeaksHelper.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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
// 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 "MantidCrystal/FindSXPeaksHelper.h"
#include "MantidAPI/Progress.h"
#include "MantidGeometry/Instrument/DetectorGroup.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/Unit.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidTypes/SpectrumDefinition.h"
#define BOOST_ALLOW_DEPRECATED_HEADERS
#include <boost/graph/adjacency_list.hpp>
#undef BOOST_ALLOW_DEPRECATED_HEADERS
#include <boost/graph/connected_components.hpp>
#include <cmath>
namespace {
const double TWO_PI = 2 * M_PI;
bool isDifferenceLargerThanTolerance(const double angle1, const double angle2, const double tolerance) {
auto difference = std::abs(angle1 - angle2);
// If we have more than 360 degree angle difference then we need to wrap it
// back to 360
if (difference > TWO_PI) {
difference = std::fmod(difference, TWO_PI);
}
// If we have more than 180 degrees then we must take the smaller angle
if (difference > M_PI) {
difference = TWO_PI - difference;
}
return difference > tolerance;
}
} // namespace
using namespace boost;
using Mantid::Kernel::UnitFactory;
namespace Mantid::Crystal::FindSXPeaksHelper {
Mantid::Kernel::Logger g_log("FindSXPeaksHelper");
/* ------------------------------------------------------------------------------------------
* Single Crystal peak representation
* ------------------------------------------------------------------------------------------
*/
/**
Constructor
@param t : tof
@param phi : psi angle
@param intensity : peak intensity
@param spectral : contributing spectra
@param wsIndex : ws index of the contributing spectrum
@param spectrumInfo: spectrum info of the original ws.
*/
SXPeak::SXPeak(double t, double phi, double intensity, const std::vector<int> &spectral, const size_t wsIndex,
const API::SpectrumInfo &spectrumInfo)
: m_tof(t), m_phi(phi), m_intensity(intensity), m_spectra(spectral), m_wsIndex(wsIndex) {
// Sanity checks
if (intensity < 0) {
throw std::invalid_argument("SXPeak: Cannot have an intensity < 0");
}
if (spectral.empty()) {
throw std::invalid_argument("SXPeak: Cannot have zero sized spectral list");
}
if (!spectrumInfo.hasDetectors(m_wsIndex)) {
throw std::invalid_argument("SXPeak: Spectrum at ws index " + std::to_string(wsIndex) + " doesn't have detectors");
}
const auto l1 = spectrumInfo.l1();
const auto l2 = spectrumInfo.l2(m_wsIndex);
m_twoTheta = spectrumInfo.twoTheta(m_wsIndex);
m_LTotal = l1 + l2;
if (m_LTotal < 0) {
throw std::invalid_argument("SXPeak: Cannot have detector distance < 0");
}
m_detId = spectrumInfo.detector(m_wsIndex).getID();
m_nPixels = 1;
Mantid::Kernel::Units::TOF tof;
const auto unit = Mantid::Kernel::UnitFactory::Instance().create("dSpacing");
Kernel::UnitParametersMap pmap{};
spectrumInfo.getDetectorValues(tof, *unit, Kernel::DeltaEMode::Elastic, false, m_wsIndex, pmap);
unit->initialize(l1, 0, pmap);
try {
m_dSpacing = unit->singleFromTOF(m_tof);
} catch (std::exception &) {
m_dSpacing = 0;
}
const auto samplePos = spectrumInfo.samplePosition();
const auto sourcePos = spectrumInfo.sourcePosition();
const auto detPos = spectrumInfo.position(m_wsIndex);
// Normalized beam direction
const auto beamDir = normalize(samplePos - sourcePos);
// Normalized detector direction
const auto detDir = normalize(detPos - samplePos);
m_unitWaveVector = beamDir - detDir;
m_qConvention = Kernel::ConfigService::Instance().getString("Q.convention");
}
/**
Object comparision
@param rhs : other SXPeak
@param tolerance : tolerance
*/
bool SXPeak::compare(const SXPeak &rhs, double tolerance) const {
if (std::abs(m_tof / m_nPixels - rhs.m_tof / rhs.m_nPixels) > tolerance * m_tof / m_nPixels)
return false;
if (std::abs(m_phi / m_nPixels - rhs.m_phi / rhs.m_nPixels) > tolerance * m_phi / m_nPixels)
return false;
if (std::abs(m_twoTheta / m_nPixels - rhs.m_twoTheta / rhs.m_nPixels) > tolerance * m_twoTheta / m_nPixels)
return false;
return true;
}
bool SXPeak::compare(const SXPeak &rhs, const double xTolerance, const double phiTolerance, const double thetaTolerance,
const XAxisUnit units) const {
const auto x_1 = (units == XAxisUnit::TOF) ? m_tof : m_dSpacing;
const auto x_2 = (units == XAxisUnit::TOF) ? rhs.m_tof : rhs.m_dSpacing;
if (std::abs(x_1 - x_2) > xTolerance) {
return false;
}
if (isDifferenceLargerThanTolerance(m_phi, rhs.m_phi, phiTolerance)) {
return false;
}
if (isDifferenceLargerThanTolerance(m_twoTheta, rhs.m_twoTheta, thetaTolerance)) {
return false;
}
return true;
}
/**
Getter for LabQ
@return q vector
*/
Mantid::Kernel::V3D SXPeak::getQ() const {
double qSign = 1.0;
if (m_qConvention == "Crystallography") {
qSign = -1.0;
}
double vi = m_LTotal / (m_tof * 1e-6);
// wavenumber = h_bar / mv
double wi = PhysicalConstants::h_bar / (PhysicalConstants::NeutronMass * vi);
// in angstroms
wi *= 1e10;
// wavevector=1/wavenumber = 2pi/wavelength
double wvi = 1.0 / wi;
// Now calculate the wavevector of the scattered neutron
return m_unitWaveVector * wvi * qSign;
}
/**
Operator addition overload
@param rhs : Right hand slide peak for addition.
*/
SXPeak &SXPeak::operator+=(const SXPeak &rhs) {
m_tof += rhs.m_tof;
m_phi += rhs.m_phi;
m_twoTheta += rhs.m_twoTheta;
m_intensity += rhs.m_intensity;
m_LTotal += rhs.m_LTotal;
m_nPixels += 1;
m_spectra.insert(m_spectra.end(), rhs.m_spectra.cbegin(), rhs.m_spectra.cend());
return *this;
}
/// Normalise by number of pixels
void SXPeak::reduce() {
m_tof /= m_nPixels;
m_phi /= m_nPixels;
m_twoTheta /= m_nPixels;
m_intensity /= m_nPixels;
m_LTotal /= m_nPixels;
m_nPixels = 1;
}
/**
Getter for the intensity.
*/
const double &SXPeak::getIntensity() const { return m_intensity; }
/**
Getter for the detector Id.
*/
detid_t SXPeak::getDetectorId() const { return m_detId; }
/**
Getter for spectrum indexes of a peak
*/
const std::vector<int> &SXPeak::getPeakSpectras() const { return m_spectra; }
PeakContainer::PeakContainer(const HistogramData::HistogramY &y)
: m_y(y), m_startIndex(0), m_stopIndex(m_y.size() - 1), m_maxIndex(0) {}
void PeakContainer::startRecord(yIt item) {
m_startIndex = std::distance(m_y.begin(), item);
m_maxIndex = m_startIndex;
m_maxSignal = *item;
}
void PeakContainer::record(yIt item) {
if (*item > m_maxSignal) {
m_maxIndex = std::distance(m_y.begin(), item);
m_maxSignal = *item;
}
}
void PeakContainer::stopRecord(yIt item) {
if (*item > m_maxSignal) {
m_maxIndex = std::distance(m_y.begin(), item);
m_maxSignal = *item;
}
m_stopIndex = std::distance(m_y.begin(), item);
// Peak end is one back though
--m_stopIndex;
}
size_t PeakContainer::getNumberOfPointsInPeak() const {
// If we didn't record anything then the start iterator is at the end
if (m_startIndex >= m_y.size()) {
return 0;
}
if (m_stopIndex >= m_startIndex) {
return m_stopIndex - m_startIndex + 1;
}
return 0;
}
yIt PeakContainer::getMaxIterator() const { return m_y.begin() + m_maxIndex; }
double PeakContainer::getStartingSignal() const { return m_y[m_startIndex]; }
/* ------------------------------------------------------------------------------------------
* Background
* ------------------------------------------------------------------------------------------
*/
AbsoluteBackgroundStrategy::AbsoluteBackgroundStrategy(const double background) : m_background(background) {}
bool AbsoluteBackgroundStrategy::isBelowBackground(const double intensity,
const HistogramData::HistogramY & /*y*/) const {
return intensity < m_background;
}
PerSpectrumBackgroundStrategy::PerSpectrumBackgroundStrategy(const double backgroundMultiplier)
: m_backgroundMultiplier(backgroundMultiplier) {}
bool PerSpectrumBackgroundStrategy::isBelowBackground(const double intensity,
const HistogramData::HistogramY &y) const {
auto background = 0.5 * (1.0 + y.front() + y.back());
background *= m_backgroundMultiplier;
return intensity < background;
}
/* ------------------------------------------------------------------------------------------
* Peak Finding Strategy
* ------------------------------------------------------------------------------------------
*/
PeakFindingStrategy::PeakFindingStrategy(const BackgroundStrategy *backgroundStrategy,
const API::SpectrumInfo &spectrumInfo, const double minValue,
const double maxValue, const XAxisUnit units)
: m_backgroundStrategy(backgroundStrategy), m_minValue(minValue), m_maxValue(maxValue),
m_spectrumInfo(spectrumInfo), m_units(units) {}
PeakList PeakFindingStrategy::findSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y,
const HistogramData::HistogramE &e, const int workspaceIndex) const {
// ---------------------------------------
// Get the lower and upper bound iterators
// ---------------------------------------
auto boundsIterator = getBounds(x);
auto lowit = boundsIterator.first;
auto highit = boundsIterator.second;
// If range specified doesn't overlap with this spectrum then bail out
if (lowit == x.end() || highit == x.begin()) {
return PeakList();
}
// Upper limit is the bin before, i.e. the last value smaller than MaxRange
--highit;
// ---------------------------------------
// Perform the search of the peaks
// ---------------------------------------
return dofindSXPeaks(x, y, e, lowit, highit, workspaceIndex);
}
void PeakFindingStrategy::setMinNBinsPerPeak(int minNBinsPerPeak) { m_minNBinsPerPeak = minNBinsPerPeak; }
void PeakFindingStrategy::filterPeaksForMinBins(std::vector<std::unique_ptr<PeakContainer>> &inputPeakList) const {
if (m_minNBinsPerPeak == EMPTY_INT() || inputPeakList.empty()) {
return;
}
for (auto inputIt = inputPeakList.begin(); inputIt != inputPeakList.end();) {
if ((*inputIt)->getNumberOfPointsInPeak() < m_minNBinsPerPeak) {
inputIt = inputPeakList.erase(inputIt);
} else {
++inputIt;
}
}
}
BoundsIterator PeakFindingStrategy::getBounds(const HistogramData::HistogramX &x) const {
// Find the range [min,max]
auto lowit = (m_minValue == EMPTY_DBL()) ? x.begin() : std::lower_bound(x.begin(), x.end(), m_minValue);
using std::placeholders::_1;
auto highit = (m_maxValue == EMPTY_DBL())
? x.end()
: std::find_if(lowit, x.end(), std::bind(std::greater<double>(), _1, m_maxValue));
return std::make_pair(lowit, highit);
}
/**
* Calculates the average phi value if the workspace contains
* multiple detectors per spectrum, or returns the value
* of phi if it is a single detector to spectrum mapping.
* @param workspaceIndex :: The index to return the phi value of
* @return :: The averaged or exact value of phi
*/
double PeakFindingStrategy::calculatePhi(size_t workspaceIndex) const {
double phi;
// Get the detectors for the workspace index
const auto &spectrumDefinition = m_spectrumInfo.spectrumDefinition(workspaceIndex);
const auto numberOfDetectors = spectrumDefinition.size();
const auto &det = m_spectrumInfo.detector(workspaceIndex);
if (numberOfDetectors == 1) {
phi = det.getPhi();
} else {
// Have to average the value for phi
auto detectorGroup = dynamic_cast<const Mantid::Geometry::DetectorGroup *>(&det);
if (!detectorGroup) {
throw std::runtime_error("Could not cast to detector group");
}
phi = detectorGroup->getPhi();
}
if (phi < 0) {
phi += 2.0 * M_PI;
}
return phi;
}
double PeakFindingStrategy::getXValue(const HistogramData::HistogramX &x, const size_t peakLocation) const {
auto leftBinPosition = x.begin() + peakLocation;
const double leftBinEdge = *leftBinPosition;
const double rightBinEdge = *std::next(leftBinPosition);
return 0.5 * (leftBinEdge + rightBinEdge);
}
double PeakFindingStrategy::convertToTOF(const double xValue, const size_t workspaceIndex) const {
if (m_units == XAxisUnit::TOF) {
// we're already using TOF units
return xValue;
} else {
const auto unit = UnitFactory::Instance().create("dSpacing");
Mantid::Kernel::Units::TOF tof;
Kernel::UnitParametersMap pmap{};
m_spectrumInfo.getDetectorValues(*unit, tof, Kernel::DeltaEMode::Elastic, false, workspaceIndex, pmap);
// we're using d-spacing, convert the point to TOF
unit->initialize(m_spectrumInfo.l1(), 0, pmap);
return unit->singleToTOF(xValue);
}
}
PeakList PeakFindingStrategy::convertToSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y,
const std::vector<std::unique_ptr<PeakContainer>> &foundPeaks,
const int workspaceIndex) const {
PeakList peaks;
if (foundPeaks.empty()) {
return peaks;
}
// Add a vector to the boost optional
peaks = std::vector<FindSXPeaksHelper::SXPeak>();
for (const auto &peak : foundPeaks) {
// Get the index of the bin
auto maxY = peak->getMaxIterator();
const auto distance = std::distance(y.begin(), maxY);
const auto xValue = getXValue(x, distance);
const auto tof = convertToTOF(xValue, workspaceIndex);
const double phi = calculatePhi(workspaceIndex);
std::vector<int> specs(1, workspaceIndex);
(*peaks).emplace_back(tof, phi, *maxY, specs, workspaceIndex, m_spectrumInfo);
}
return peaks;
}
StrongestPeaksStrategy::StrongestPeaksStrategy(const BackgroundStrategy *backgroundStrategy,
const API::SpectrumInfo &spectrumInfo, const double minValue,
const double maxValue, const XAxisUnit units)
: PeakFindingStrategy(backgroundStrategy, spectrumInfo, minValue, maxValue, units) {}
PeakList StrongestPeaksStrategy::dofindSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y,
const HistogramData::HistogramE &e, Bound low, Bound high,
const int workspaceIndex) const {
auto distmin = std::distance(x.begin(), low);
auto distmax = std::distance(x.begin(), high);
// Find the max element
auto maxY = (y.size() > 1) ? std::max_element(y.begin() + distmin, y.begin() + distmax) : y.begin();
// Perform a check against the background
double intensity = (*maxY);
if (m_backgroundStrategy->isBelowBackground(intensity, y)) {
return PeakList();
}
// Create the SXPeak information
const auto distance = std::distance(y.begin(), maxY);
const auto xValue = getXValue(x, distance);
const auto tof = convertToTOF(xValue, workspaceIndex);
const double phi = calculatePhi(workspaceIndex);
std::vector<int> specs(1, workspaceIndex);
std::vector<SXPeak> peaks;
peaks.emplace_back(tof, phi, *maxY, specs, workspaceIndex, m_spectrumInfo);
return peaks;
}
AllPeaksStrategy::AllPeaksStrategy(const BackgroundStrategy *backgroundStrategy, const API::SpectrumInfo &spectrumInfo,
const double minValue, const double maxValue, const XAxisUnit units)
: PeakFindingStrategy(backgroundStrategy, spectrumInfo, minValue, maxValue, units) {
// We only allow the AbsoluteBackgroundStrategy for now
if (!dynamic_cast<const AbsoluteBackgroundStrategy *>(m_backgroundStrategy)) {
throw std::invalid_argument("The AllPeaksStrategy has to be initialized "
"with the AbsoluteBackgroundStrategy.");
}
}
PeakList AllPeaksStrategy::dofindSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y,
const HistogramData::HistogramE &e, Bound low, Bound high,
const int workspaceIndex) const {
// Get all peaks from the container
auto foundPeaks = getAllPeaks(x, y, low, high, m_backgroundStrategy);
// Filter the found peaks having the mininum number of bins
filterPeaksForMinBins(foundPeaks);
// Convert the found peaks to SXPeaks
auto peaks = convertToSXPeaks(x, y, foundPeaks, workspaceIndex);
return peaks;
}
std::vector<std::unique_ptr<PeakContainer>>
AllPeaksStrategy::getAllPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y, Bound low,
Bound high,
const Mantid::Crystal::FindSXPeaksHelper::BackgroundStrategy *backgroundStrategy) const {
// We iterate over the data and only consider data which is above the
// threshold.
// Once data starts to be above the threshold we start to record it and add it
// to a peak. Once it falls below, it concludes recording of that particular
// peak
bool isRecording = false;
std::unique_ptr<PeakContainer> currentPeak = nullptr;
std::vector<std::unique_ptr<PeakContainer>> peaks;
// We want to the upper boundary to be inclusive hence we need to increment it
// by one
if (high != x.end()) {
++high;
}
auto distanceMin = std::distance(x.begin(), low);
auto distanceMax = std::distance(x.begin(), high);
const auto lowY = y.begin() + distanceMin;
auto highY = distanceMax < static_cast<int>(y.size()) ? y.begin() + distanceMax : y.end();
for (auto it = lowY; it != highY; ++it) {
const auto signal = *it;
const auto isAboveThreshold = !backgroundStrategy->isBelowBackground(signal, y);
// There are four scenarios:
// 1. Not recording + below threshold => continue
// 2. Not recording + above treshold => start recording
// 3. Recording + below threshold => stop recording
// 4. Recording + above threshold => continue recording
if (!isRecording && (!std::isfinite(signal) || !isAboveThreshold)) {
continue;
} else if (!isRecording && isAboveThreshold && std::isfinite(signal)) {
// only start recording if is finite as NaN values will be found to be above threshold
currentPeak = std::make_unique<PeakContainer>(y);
currentPeak->startRecord(it);
isRecording = true;
} else if (isRecording && !isAboveThreshold) {
currentPeak->stopRecord(it);
peaks.emplace_back(std::move(currentPeak));
currentPeak = nullptr;
isRecording = false;
} else {
// this will continue to record NaN if previous point was above the background
currentPeak->record(it);
}
}
// Handle a peak on the edge if it exists
if (isRecording) {
if (highY == y.end()) {
--highY;
}
currentPeak->stopRecord(highY);
peaks.emplace_back(std::move(currentPeak));
currentPeak = nullptr;
}
return peaks;
}
NSigmaPeaksStrategy::NSigmaPeaksStrategy(const API::SpectrumInfo &spectrumInfo, const double nsigma,
const double minValue, const double maxValue, const XAxisUnit units)
: PeakFindingStrategy(nullptr, spectrumInfo, minValue, maxValue, units), m_nsigma(nsigma) {}
PeakList NSigmaPeaksStrategy::dofindSXPeaks(const HistogramData::HistogramX &x, const HistogramData::HistogramY &y,
const HistogramData::HistogramE &e, Bound low, Bound high,
const int workspaceIndex) const {
auto nsigmaPeaks = getAllNSigmaPeaks(x, y, e, low, high);
// Filter the found peaks having the mininum number of bins
filterPeaksForMinBins(nsigmaPeaks);
auto sxPeaks = convertToSXPeaks(x, y, nsigmaPeaks, workspaceIndex);
return sxPeaks;
}
std::vector<std::unique_ptr<PeakContainer>> NSigmaPeaksStrategy::getAllNSigmaPeaks(const HistogramData::HistogramX &x,
const HistogramData::HistogramY &y,
const HistogramData::HistogramE &e,
Bound low, Bound high) const {
/*
Credits to the author of SXD2001 for the idea of using NSigma as a threshold for peak finding:
Gutmann, M. J. (2005). SXD2001. ISIS Facility, Rutherford Appleton Laboratory, Oxfordshire, England.
*/
bool isRecording = false;
std::unique_ptr<PeakContainer> currentPeak = nullptr;
std::vector<std::unique_ptr<PeakContainer>> peaks;
// We want to the upper boundary to be inclusive hence we need to increment by one
if (high != x.end()) {
++high;
}
auto distanceMin = std::distance(x.begin(), low);
auto distanceMax = std::distance(x.begin(), high);
const auto lowY = y.begin() + distanceMin;
auto highY = distanceMax < static_cast<int>(y.size()) ? y.begin() + distanceMax : y.end();
const auto lowE = e.begin() + distanceMin;
const auto highE = distanceMax < static_cast<int>(e.size()) ? e.begin() + distanceMax : e.begin();
auto yIt = lowY + 1;
auto eIt = lowE + 1;
for (; yIt != highY && eIt != highE; ++yIt, ++eIt) {
const auto signalDiff = *yIt - *(yIt - 1);
const auto isStartPeak = signalDiff > (m_nsigma * (*eIt)) + NSIGMA_COMPARISON_THRESHOLD;
const auto isSigDropSignificant = (signalDiff * (-1.)) > (m_nsigma * (*eIt)) + NSIGMA_COMPARISON_THRESHOLD;
const auto isEndPeak = (currentPeak != nullptr ? (isSigDropSignificant || currentPeak->getStartingSignal() > *yIt)
: isSigDropSignificant);
/*Possible scenarios
1. isRecording is False and isStartPeak True(== isEndPeak is False) => start recording
2. isRecording is True and isEndPeak False - continue recording
3. isRecording is True and isEndpeak is True(== isStartpeak is False) - end peak
4. else - continue
*/
if (!isRecording && isStartPeak) {
currentPeak = std::make_unique<PeakContainer>(y);
currentPeak->startRecord(yIt);
isRecording = true;
} else if (isRecording && !isEndPeak) {
currentPeak->record(yIt);
} else if (isRecording && isEndPeak) {
currentPeak->stopRecord(yIt);
peaks.emplace_back(std::move(currentPeak));
currentPeak = nullptr;
isRecording = false;
} else {
continue;
}
}
// Handle a peak on the edge if it exists
if (isRecording) {
if (highY == y.end()) {
--highY;
}
currentPeak->stopRecord(highY);
peaks.emplace_back(std::move(currentPeak));
currentPeak = nullptr;
}
return peaks;
}
/* ------------------------------------------------------------------------------------------
* PeakList Reduction Strategy
* ------------------------------------------------------------------------------------------
*/
ReducePeakListStrategy::ReducePeakListStrategy(const CompareStrategy *compareStrategy)
: m_compareStrategy(compareStrategy) {}
void ReducePeakListStrategy::setMinNSpectraPerPeak(int minNSpectraPerPeak) {
m_minNSpectraPerPeak = minNSpectraPerPeak;
}
void ReducePeakListStrategy::setMaxNSpectraPerPeak(int maxSpectrasForPeak) {
m_maxNSpectraPerPeak = maxSpectrasForPeak;
}
SimpleReduceStrategy::SimpleReduceStrategy(const CompareStrategy *compareStrategy)
: ReducePeakListStrategy(compareStrategy) {}
std::vector<SXPeak> SimpleReduceStrategy::reduce(const std::vector<SXPeak> &peaks,
Mantid::Kernel::ProgressBase & /*progress*/) const {
// If the peaks are empty then do nothing
if (peaks.empty()) {
return peaks;
}
std::vector<SXPeak> finalPeaks;
for (const auto ¤tPeak : peaks) {
auto pos = std::find_if(finalPeaks.begin(), finalPeaks.end(), [¤tPeak, this](SXPeak &peak) {
auto result = this->m_compareStrategy->compare(currentPeak, peak);
// bool result = currentPeak.compare(peak,
// resolution);
if (result)
peak += currentPeak;
return result;
});
if (pos == finalPeaks.end()) {
finalPeaks.emplace_back(currentPeak);
}
}
reducePeaksFromNumberOfSpectras(finalPeaks);
return finalPeaks;
}
void SimpleReduceStrategy::reducePeaksFromNumberOfSpectras(std::vector<SXPeak> &inputPeaks) const {
if (m_minNSpectraPerPeak == EMPTY_INT() && m_maxNSpectraPerPeak == EMPTY_INT()) {
return;
}
for (auto peakIt = inputPeaks.begin(); peakIt != inputPeaks.end();) {
if (((m_minNSpectraPerPeak != EMPTY_INT()) && ((*peakIt).getPeakSpectras().size() < m_minNSpectraPerPeak)) ||
((m_maxNSpectraPerPeak != EMPTY_INT()) && ((*peakIt).getPeakSpectras().size() > m_maxNSpectraPerPeak))) {
peakIt = inputPeaks.erase(peakIt);
} else {
++peakIt;
}
}
}
FindMaxReduceStrategy::FindMaxReduceStrategy(const CompareStrategy *compareStrategy)
: ReducePeakListStrategy(compareStrategy) {}
std::vector<SXPeak> FindMaxReduceStrategy::reduce(const std::vector<SXPeak> &peaks,
Mantid::Kernel::ProgressBase &progress) const {
// If the peaks are empty then do nothing
if (peaks.empty()) {
return peaks;
}
// Groups the peaks into elements which are considered alike
auto peakGroups = getPeakGroups(peaks, progress);
// Now reduce the peaks groups
return getFinalPeaks(peakGroups);
}
// Define some graph elements
using PeakGraph = adjacency_list<vecS, vecS, undirectedS, SXPeak *>;
using Vertex = boost::graph_traits<PeakGraph>::vertex_descriptor;
using Edge = boost::graph_traits<PeakGraph>::edge_descriptor;
std::vector<std::vector<SXPeak *>> FindMaxReduceStrategy::getPeakGroups(const std::vector<SXPeak> &peakList,
Mantid::Kernel::ProgressBase &progress) const {
// Create a vector of addresses. Note that the peaks live on the stack. This
// here only works, because the peaks are always in a stack frame below.
std::vector<SXPeak *> peaks;
peaks.reserve(peakList.size());
std::transform(peakList.cbegin(), peakList.cend(), std::back_inserter(peaks),
[](const auto &peak) { return &const_cast<SXPeak &>(peak); });
// Add the peaks to a graph
Edge edge;
PeakGraph graph;
PeakGraph::vertex_iterator vertexIt, vertexEnd;
// Provide a warning if there are more than 500 peaks found.
const size_t numberOfPeaksFound = peaks.size();
if (numberOfPeaksFound > 500) {
std::string warningMessage = std::string("There are ") + std::to_string(numberOfPeaksFound) +
std::string(" peaks being processed. This might take a long time. "
"Please check that the cutoff of the background that "
"you have selected is high enough, else the algorithm will"
" mistake background noise for peaks. The instrument view "
"allows you to easily inspect the typical background level.");
g_log.warning(warningMessage);
}
std::string message = std::string("There are ") + std::to_string(numberOfPeaksFound) +
std::string(" peaks. Investigating peak number ");
int peakCounter = 0;
for (auto peak : peaks) {
++peakCounter;
// 1. Add the vertex
auto vertex = add_vertex(peak, graph);
// 2. Iterate over all elements already in the graph and check if they need
// to edstablish an edge between them.
std::tie(vertexIt, vertexEnd) = vertices(graph);
// Provide a progress report such that users can escape the graph generation
if (peakCounter > 50) {
progress.doReport(message + std::to_string(peakCounter));
}
for (; vertexIt != vertexEnd; ++vertexIt) {
// 2.1 Check if we are looking at the new vertex itself. We don't want
// self-loops
if (vertex == *vertexIt) {
continue;
}
// 2.2 Check if the edge exists already
if (boost::edge(vertex, *vertexIt, graph).second) {
continue;
}
// 2.3 Check if the two vertices should have an edge
const auto toCheck = graph[*vertexIt];
if (m_compareStrategy->compare(*peak, *toCheck)) {
// We need to create an edge
add_edge(vertex, *vertexIt, graph);
}
}
}
// Create disjoined graphs from graph above
std::vector<int> components(boost::num_vertices(graph));
const int numberOfPeaks = connected_components(graph, &components[0]);
std::vector<std::vector<SXPeak *>> peakGroups(numberOfPeaks);
for (auto i = 0u; i < components.size(); ++i) {
auto index = components[i];
peakGroups[index].emplace_back(graph[i]);
}
return peakGroups;
}
std::vector<SXPeak> FindMaxReduceStrategy::getFinalPeaks(const std::vector<std::vector<SXPeak *>> &peakGroups) const {
std::vector<SXPeak> peaks;
// For each peak groupf find one peak
// Currently we select the peak with the largest signal (this strategy could
// be changed to something like a weighted mean or similar)
for (const auto &group : peakGroups) {
// When MinNSpectraPerPeak or maxNSpectraPerPeak parameters are provided,
// a group will be ignored if it does not satisfy the minimum or the maximum number of spectrums
// required to identify as a peak.
if ((m_minNSpectraPerPeak != EMPTY_INT() && group.size() < m_minNSpectraPerPeak) ||
(m_maxNSpectraPerPeak != EMPTY_INT() && group.size() > m_maxNSpectraPerPeak)) {
continue;
}
SXPeak *maxPeak = nullptr;
double maxIntensity = std::numeric_limits<double>::min();
for (auto *element : group) {
if (element->getIntensity() > maxIntensity) {
maxIntensity = element->getIntensity();
maxPeak = element;
}
}
// Add the max peak if valid
if (maxPeak) {
// check not null as is case when intensity is NaN (though this shouldn't occur now)
peaks.emplace_back(*maxPeak);
}
}
return peaks;
}
/* ------------------------------------------------------------------------------------------
* Comparison Strategy
* ------------------------------------------------------------------------------------------
*/
RelativeCompareStrategy::RelativeCompareStrategy(const double resolution) : m_resolution(resolution) {}
bool RelativeCompareStrategy::compare(const SXPeak &lhs, const SXPeak &rhs) const {
return lhs.compare(rhs, m_resolution);
}
AbsoluteCompareStrategy::AbsoluteCompareStrategy(const double xUnitResolution, const double phiResolution,
const double twoThetaResolution, const XAxisUnit units)
: m_xUnitResolution(xUnitResolution), m_phiResolution(phiResolution), m_twoThetaResolution(twoThetaResolution),
m_units(units) {
// Convert the input from degree to radians
constexpr double rad2deg = M_PI / 180.;
m_phiResolution *= rad2deg;
m_twoThetaResolution *= rad2deg;
}
bool AbsoluteCompareStrategy::compare(const SXPeak &lhs, const SXPeak &rhs) const {
return lhs.compare(rhs, m_xUnitResolution, m_phiResolution, m_twoThetaResolution, m_units);
}
} // namespace Mantid::Crystal::FindSXPeaksHelper