-
Notifications
You must be signed in to change notification settings - Fork 122
/
FindPeaks.cpp
1471 lines (1270 loc) · 54.8 KB
/
FindPeaks.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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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/FindPeaks.h"
#include "MantidAlgorithms/SmoothData.h"
#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/FuncMinimizerFactory.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/TableRow.h"
#include "MantidAlgorithms/FitPeak.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidIndexing/GlobalSpectrumIndex.h"
#include "MantidIndexing/IndexInfo.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/StartsWithValidator.h"
#include "MantidKernel/VectorHelper.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"
#include <boost/algorithm/string.hpp>
#include <boost/math/special_functions/round.hpp>
#include <numeric>
#include <fstream>
using namespace Mantid;
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::HistogramData;
using namespace Mantid::Indexing;
// const double MINHEIGHT = 2.00000001;
namespace Mantid {
namespace Algorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(FindPeaks)
//----------------------------------------------------------------------------------------------
/** Constructor
*/
FindPeaks::FindPeaks()
: API::ParallelAlgorithm(), m_peakParameterNames(), m_bkgdParameterNames(), m_bkgdOrder(0), m_outPeakTableWS(),
m_dataWS(), m_inputPeakFWHM(0), m_highBackground(false), m_rawPeaksTable(false), m_numTableParams(0),
m_centreIndex(1) /* for Gaussian */, m_peakFuncType(""), m_backgroundType(""), m_vecPeakCentre(),
m_vecFitWindows(), m_backgroundFunction(), m_peakFunction(), m_minGuessedPeakWidth(0), m_maxGuessedPeakWidth(0),
m_stepGuessedPeakWidth(0), m_usePeakPositionTolerance(false), m_peakPositionTolerance(0.0), m_fitFunctions(),
m_peakLeftIndexes(), m_peakRightIndexes(), m_minimizer("Levenberg-MarquardtMD"), m_costFunction(),
m_minHeight(0.0), m_leastMaxObsY(0.), m_useObsCentre(false) {}
//----------------------------------------------------------------------------------------------
/** Initialize and declare properties.
*/
void FindPeaks::init() {
declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
"Name of the workspace to search");
auto mustBeNonNegative = std::make_shared<BoundedValidator<int>>();
mustBeNonNegative->setLower(0);
declareProperty("WorkspaceIndex", EMPTY_INT(), mustBeNonNegative,
"If set, only this spectrum will be searched for peaks "
"(otherwise all are)");
auto min = std::make_shared<BoundedValidator<int>>();
min->setLower(1);
// The estimated width of a peak in terms of number of channels
declareProperty("FWHM", 7, min, "Estimated number of points covered by the fwhm of a peak (default 7)");
// The tolerance allowed in meeting the conditions
declareProperty("Tolerance", 4, min,
"A measure of the strictness desired in "
"meeting the condition on peak "
"candidates,\n"
"Mariscotti recommends 2 (default 4)");
declareProperty(std::make_unique<ArrayProperty<double>>("PeakPositions"),
"Optional: enter a comma-separated list of the expected "
"X-position of the centre of the peaks. Only peaks near "
"these positions will be fitted.");
declareProperty(std::make_unique<ArrayProperty<double>>("FitWindows"),
"Optional: enter a comma-separated list of the expected "
"X-position of windows to fit. The number of values must be "
"exactly double the number of specified peaks.");
std::vector<std::string> peakNames = FunctionFactory::Instance().getFunctionNames<API::IPeakFunction>();
declareProperty("PeakFunction", "Gaussian", std::make_shared<StringListValidator>(peakNames));
std::vector<std::string> bkgdtypes{"Flat", "Linear", "Quadratic"};
declareProperty("BackgroundType", "Linear", std::make_shared<StringListValidator>(bkgdtypes), "Type of Background.");
declareProperty("HighBackground", true, "Relatively weak peak in high background");
auto mustBePositive = std::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(1);
declareProperty("MinGuessedPeakWidth", 2, mustBePositive,
"Minimum guessed peak width for fit. It is in unit of number of pixels.");
declareProperty("MaxGuessedPeakWidth", 10, mustBePositive,
"Maximum guessed peak width for fit. It is in unit of number of pixels.");
declareProperty("GuessedPeakWidthStep", 2, mustBePositive,
"Step of guessed peak width. It is in unit of number of pixels.");
auto mustBePositiveDBL = std::make_shared<BoundedValidator<double>>();
declareProperty("PeakPositionTolerance", EMPTY_DBL(), mustBePositiveDBL,
"Tolerance on the found peaks' positions against the input "
"peak positions. Non-positive value indicates that this "
"option is turned off.");
// The found peaks in a table
declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>("PeaksList", "", Direction::Output),
"The name of the TableWorkspace in which to store the list "
"of peaks found");
declareProperty("RawPeakParameters", false,
"false generates table with effective centre/width/height "
"parameters. true generates a table with peak function "
"parameters");
declareProperty("MinimumPeakHeight", DBL_MIN, "Minimum allowed peak height. ");
declareProperty("MinimumPeakHeightObs", 0.0,
"Least value of the maximum observed Y value of a peak within "
"specified region. If any peak's maximum observed Y value is smaller, "
"then "
"this peak will not be fit. It is designed for EventWorkspace with "
"integer counts.");
std::array<std::string, 2> costFuncOptions = {{"Chi-Square", "Rwp"}};
declareProperty("CostFunction", "Chi-Square",
Kernel::IValidator_sptr(new Kernel::ListValidator<std::string>(costFuncOptions)), "Cost functions");
std::vector<std::string> minimizerOptions = API::FuncMinimizerFactory::Instance().getKeys();
declareProperty("Minimizer", "Levenberg-MarquardtMD",
Kernel::IValidator_sptr(new Kernel::StartsWithValidator(minimizerOptions)),
"Minimizer to use for fitting. Minimizers available are "
"\"Levenberg-Marquardt\", \"Simplex\","
"\"Conjugate gradient (Fletcher-Reeves imp.)\", \"Conjugate "
"gradient (Polak-Ribiere imp.)\", \"BFGS\", and "
"\"Levenberg-MarquardtMD\"");
declareProperty("StartFromObservedPeakCentre", true, "Use observed value as the starting value of peak centre. ");
}
//----------------------------------------------------------------------------------------------
/** Execute the findPeaks algorithm.
*/
void FindPeaks::exec() {
// Process input
processAlgorithmProperties();
// Create those functions to fit
createFunctions();
// Set up output table workspace
generateOutputPeakParameterTable();
// Fit
if (!m_vecPeakCentre.empty()) {
if (!m_vecFitWindows.empty()) {
if (m_vecFitWindows.size() != (m_vecPeakCentre.size() * 2)) {
throw std::invalid_argument("Number of FitWindows must be exactly "
"twice the number of PeakPositions");
}
}
// Perform fit with fixed start positions.
findPeaksGivenStartingPoints(m_vecPeakCentre, m_vecFitWindows);
} else {
// Use Mariscotti's method to find the peak centers
m_usePeakPositionTolerance = false;
this->findPeaksUsingMariscotti();
}
// Set output properties
g_log.information() << "Total " << m_outPeakTableWS->rowCount() << " peaks found and successfully fitted.\n";
setProperty("PeaksList", m_outPeakTableWS);
} // END: exec()
//----------------------------------------------------------------------------------------------
/** Process algorithm's properties
*/
void FindPeaks::processAlgorithmProperties() {
// Input workspace
m_dataWS = getProperty("InputWorkspace");
// WorkspaceIndex
int wsIndex = getProperty("WorkspaceIndex");
if (!isEmpty(wsIndex)) {
if (wsIndex >= static_cast<int>(m_dataWS->getNumberHistograms())) {
g_log.warning() << "The value of WorkspaceIndex provided (" << wsIndex
<< ") is larger than the size of this workspace (" << m_dataWS->getNumberHistograms() << ")\n";
throw Kernel::Exception::IndexError(wsIndex, m_dataWS->getNumberHistograms() - 1,
"FindPeaks WorkspaceIndex property");
}
m_indexSet = m_dataWS->indexInfo().makeIndexSet({static_cast<Indexing::GlobalSpectrumIndex>(wsIndex)});
} else {
m_indexSet = m_dataWS->indexInfo().makeIndexSet();
}
// Peak width
m_inputPeakFWHM = getProperty("FWHM");
int t1 = getProperty("MinGuessedPeakWidth");
int t2 = getProperty("MaxGuessedPeakWidth");
int t3 = getProperty("GuessedPeakWidthStep");
if (t1 > t2 || t1 <= 0 || t3 <= 0) {
std::stringstream errss;
errss << "User specified wrong guessed peak width parameters (must be "
"postive and make sense). "
<< "User inputs are min = " << t1 << ", max = " << t2 << ", step = " << t3;
g_log.warning(errss.str());
throw std::runtime_error(errss.str());
}
m_minGuessedPeakWidth = t1;
m_maxGuessedPeakWidth = t2;
m_stepGuessedPeakWidth = t3;
m_peakPositionTolerance = getProperty("PeakPositionTolerance");
m_usePeakPositionTolerance = true;
if (isEmpty(m_peakPositionTolerance))
m_usePeakPositionTolerance = false;
// Specified peak positions, which is optional
m_vecPeakCentre = getProperty("PeakPositions");
if (!m_vecPeakCentre.empty())
std::sort(m_vecPeakCentre.begin(), m_vecPeakCentre.end());
m_vecFitWindows = getProperty("FitWindows");
// Peak and ground type
m_peakFuncType = getPropertyValue("PeakFunction");
m_backgroundType = getPropertyValue("BackgroundType");
// Fit algorithm
m_highBackground = getProperty("HighBackground");
// Peak parameters are give via a table workspace
m_rawPeaksTable = getProperty("RawPeakParameters");
// Minimum peak height
m_minHeight = getProperty("MinimumPeakHeight");
// About Fit
m_minimizer = getPropertyValue("Minimizer");
m_costFunction = getPropertyValue("CostFunction");
m_useObsCentre = getProperty("StartFromObservedPeakCentre");
m_leastMaxObsY = getProperty("MinimumPeakHeightObs");
}
//----------------------------------------------------------------------------------------------
/** Generate a table workspace for output peak parameters
*/
void FindPeaks::generateOutputPeakParameterTable() {
m_outPeakTableWS = std::make_shared<TableWorkspace>();
m_outPeakTableWS->addColumn("int", "spectrum");
if (m_rawPeaksTable) {
// Output raw peak parameters
size_t numpeakpars = m_peakFunction->nParams();
size_t numbkgdpars = m_backgroundFunction->nParams();
m_numTableParams = numpeakpars + numbkgdpars;
if (m_peakFuncType == "Gaussian")
m_centreIndex = 1;
else if (m_peakFuncType == "LogNormal")
m_centreIndex = 1;
else if (m_peakFuncType == "Lorentzian")
m_centreIndex = 1;
else if (m_peakFuncType == "PseudoVoigt")
m_centreIndex = 2;
else
m_centreIndex = m_numTableParams; // bad value
for (size_t i = 0; i < numpeakpars; ++i)
m_outPeakTableWS->addColumn("double", m_peakParameterNames[i]);
for (size_t i = 0; i < numbkgdpars; ++i)
m_outPeakTableWS->addColumn("double", m_bkgdParameterNames[i]);
// m_outPeakTableWS->addColumn("double", "f1.A2");
} else {
// Output centre, weight, height, A0, A1 and A2
m_numTableParams = 6;
m_centreIndex = 0;
m_outPeakTableWS->addColumn("double", "centre");
m_outPeakTableWS->addColumn("double", "width");
m_outPeakTableWS->addColumn("double", "height");
m_outPeakTableWS->addColumn("double", "backgroundintercept");
m_outPeakTableWS->addColumn("double", "backgroundslope");
m_outPeakTableWS->addColumn("double", "A2");
}
m_outPeakTableWS->addColumn("double", "chi2");
}
//----------------------------------------------------------------------------------------------
/** Find the start positions to fit peaks with given estimated peak centres
* @param peakcentres :: vector of the center x-positions specified to perform
* fits.
* @param fitwindows :: vector of windows around each peak. Otherwise, windows
* will be determined automatically.
*/
void FindPeaks::findPeaksGivenStartingPoints(const std::vector<double> &peakcentres,
const std::vector<double> &fitwindows) {
bool useWindows = (!fitwindows.empty());
std::size_t numPeaks = peakcentres.size();
// Loop over the spectra searching for peaks
m_progress = std::make_unique<Progress>(this, 0.0, 1.0, m_indexSet.size());
for (const auto spec : m_indexSet) {
const auto &vecX = m_dataWS->x(spec);
double practical_x_min = vecX.front();
double practical_x_max = vecX.back();
g_log.information() << "actual x-range = [" << practical_x_min << " -> " << practical_x_max << "]\n";
{
const auto &vecY = m_dataWS->y(spec);
const auto &vecE = m_dataWS->e(spec);
const size_t numY = vecY.size();
size_t i_min = 1;
for (; i_min < numY; ++i_min) {
if ((vecY[i_min] != 0.) || (vecE[i_min] != 0)) {
--i_min; // bring it back one
break;
}
}
practical_x_min = vecX[i_min];
size_t i_max = numY - 2;
for (; i_max > i_min; --i_max) {
if ((vecY[i_max] != 0.) || (vecE[i_max] != 0)) {
++i_max; // bring it back one
break;
}
}
g_log.debug() << "Finding peaks from giving starting point, with interval i_min = " << i_min
<< " i_max = " << i_max << '\n';
practical_x_max = vecX[i_max];
}
g_log.information() << "practical x-range = [" << practical_x_min << " -> " << practical_x_max << "]\n";
for (std::size_t ipeak = 0; ipeak < numPeaks; ipeak++) {
// Try to fit at this center
double x_center = peakcentres[ipeak];
std::stringstream infoss;
infoss << "Spectrum " << spec << ": Fit peak @ d = " << x_center;
if (useWindows) {
infoss << " inside fit window [" << fitwindows[2 * ipeak] << ", " << fitwindows[2 * ipeak + 1] << "]";
}
g_log.information(infoss.str());
// Check whether it is the in data range
if (x_center > practical_x_min && x_center < practical_x_max) {
if (useWindows)
fitPeakInWindow(m_dataWS, static_cast<int>(spec), x_center, fitwindows[2 * ipeak], fitwindows[2 * ipeak + 1]);
else {
bool hasLeftPeak = (ipeak > 0);
double leftpeakcentre = 0.;
if (hasLeftPeak)
leftpeakcentre = peakcentres[ipeak - 1];
bool hasRightPeak = (ipeak < numPeaks - 1);
double rightpeakcentre = 0.;
if (hasRightPeak)
rightpeakcentre = peakcentres[ipeak + 1];
fitPeakGivenFWHM(m_dataWS, static_cast<int>(spec), x_center, m_inputPeakFWHM, hasLeftPeak, leftpeakcentre,
hasRightPeak, rightpeakcentre);
}
} else {
g_log.warning() << "Given peak centre " << x_center << " is out side of given data's range (" << practical_x_min
<< ", " << practical_x_max << ").\n";
addNonFitRecord(spec, x_center);
}
} // loop through the peaks specified
m_progress->report();
} // loop over spectra
}
//----------------------------------------------------------------------------------------------
/** Use the Mariscotti method to find the start positions and fit gaussian peaks
*/
void FindPeaks::findPeaksUsingMariscotti() {
// At this point the data has not been smoothed yet.
auto smoothedData = this->calculateSecondDifference(m_dataWS);
// The optimum number of points in the smoothing, according to Mariscotti, is
// 0.6*fwhm
auto w = static_cast<int>(0.6 * m_inputPeakFWHM);
// w must be odd
if (!(w % 2))
++w;
smoothData(smoothedData, w, g_z);
// Now calculate the errors on the smoothed data
this->calculateStandardDeviation(m_dataWS, smoothedData, w);
// Calculate n1 (Mariscotti eqn. 18)
const double kz = 1.22; // This kz corresponds to z=5 & w=0.6*fwhm - see Mariscotti Fig. 8
const int n1 = boost::math::iround(kz * m_inputPeakFWHM);
// Can't calculate n2 or n3 yet because they need i0
const int tolerance = getProperty("Tolerance");
// Loop over the spectra searching for peaks
m_progress = std::make_unique<Progress>(this, 0.0, 1.0, m_indexSet.size());
for (size_t k_out = 0; k_out < m_indexSet.size(); ++k_out) {
const size_t k = m_indexSet[k_out];
const auto &S = smoothedData[k_out].y();
const auto &F = smoothedData[k_out].e();
// This implements the flow chart given on page 320 of Mariscotti
int i0 = 0, i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0;
for (int i = 1; i < static_cast<int>(S.size()); ++i) {
int M = 0;
if (S[i] > F[i])
M = 1;
else {
S[i] > 0 ? M = 2 : M = 3;
}
if (S[i - 1] > F[i - 1]) {
switch (M) {
case 3:
i3 = i;
/* no break */
// intentional fall-through
case 2:
i2 = i - 1;
break;
case 1:
// do nothing
break;
default:
assert(false);
// should never happen
break;
}
} else if (S[i - 1] > 0) {
switch (M) {
case 3:
i3 = i;
break;
case 2:
// do nothing
break;
case 1:
i1 = i;
break;
default:
assert(false);
// should never happen
break;
}
} else {
switch (M) {
case 3:
// do nothing
break;
case 2: // fall through (i.e. same action if M = 1 or 2)
case 1:
i5 = i - 1;
break;
default:
assert(false);
// should never happen
break;
}
}
if (i5 && i1 && i2 && i3) // If i5 has been set then we should have the
// full set and can check conditions
{
i4 = i3; // Starting point for finding i4 - calculated below
double num = 0.0, denom = 0.0;
for (int j = i3; j <= i5; ++j) {
// Calculate i4 - it's at the minimum value of Si between i3 & i5
if (S[j] <= S[i4])
i4 = j;
// Calculate sums for i0 (Mariscotti eqn. 27)
num += j * S[j];
denom += S[j];
}
i0 = static_cast<int>(num / denom);
// Check we have a correctly ordered set of points. If not, reset and
// continue
if (i1 > i2 || i2 > i3 || i3 > i4 || i5 <= i4) {
i5 = 0;
continue;
}
// Check if conditions are fulfilled - if any are not, loop onto the
// next i in the spectrum
// Mariscotti eqn. (14)
if (std::abs(S[i4]) < 2 * F[i4]) {
i5 = 0;
continue;
}
// Mariscotti eqn. (19)
if (abs(i5 - i3 + 1 - n1) > tolerance) {
i5 = 0;
continue;
}
// Calculate n2 (Mariscotti eqn. 20)
int n2 = std::abs(boost::math::iround(0.5 * (F[i0] / S[i0]) * (n1 + tolerance)));
const int n2b = std::abs(boost::math::iround(0.5 * (F[i0] / S[i0]) * (n1 - tolerance)));
if (n2b > n2)
n2 = n2b;
// Mariscotti eqn. (21)
const int testVal = n2 ? n2 : 1;
if (i3 - i2 - 1 > testVal) {
i5 = 0;
continue;
}
// Calculate n3 (Mariscotti eqn. 22)
int n3 = std::abs(boost::math::iround((n1 + tolerance) * (1 - 2 * (F[i0] / S[i0]))));
const int n3b = std::abs(boost::math::iround((n1 - tolerance) * (1 - 2 * (F[i0] / S[i0]))));
if (n3b < n3)
n3 = n3b;
// Mariscotti eqn. (23)
if (i2 - i1 + 1 < n3) {
i5 = 0;
continue;
}
// If we get to here then we've identified a peak
g_log.debug() << "Spectrum=" << k << " i0=" << i0 << " X=" << m_dataWS->x(k)[i0] << " i1=" << i1 << " i2=" << i2
<< " i3=" << i3 << " i4=" << i4 << " i5=" << i5 << '\n';
// Use i0, i2 and i4 to find out i_min and i_max, i0: right, i2: left,
// i4: centre
auto wssize = static_cast<int>(m_dataWS->x(k).size());
int iwidth = i0 - i2;
if (iwidth <= 0)
iwidth = 1;
int i_min = 1;
if (i4 > 5 * iwidth)
i_min = i4 - 5 * iwidth;
int i_max = i4 + 5 * iwidth;
if (i_max >= wssize)
i_max = wssize - 1;
this->fitSinglePeak(m_dataWS, static_cast<int>(k), i_min, i_max, i4);
// reset and go searching for the next peak
i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0;
}
} // loop through a single spectrum
m_progress->report();
} // loop over spectra
}
//----------------------------------------------------------------------------------------------
/** Calculates the second difference of the data (Y values) in a workspace.
* Done according to equation (3) in Mariscotti: \f$ S_i = N_{i+1} - 2N_i +
* N_{i+1} \f$.
* In the output workspace, the 2nd difference is in Y, X is unchanged and E
* is zero.
* @param input :: The workspace to calculate the second difference of
* @return A workspace containing the second difference
*/
std::vector<Histogram> FindPeaks::calculateSecondDifference(const API::MatrixWorkspace_const_sptr &input) {
std::vector<Histogram> diffed;
// Loop over spectra
for (const auto i : m_indexSet) {
diffed.emplace_back(input->histogram(i));
diffed.back().mutableY() = 0.0;
diffed.back().mutableE() = 0.0;
const auto &Y = input->y(i);
auto &S = diffed.back().mutableY();
// Go through each spectrum calculating the second difference at each point
// First and last points in each spectrum left as zero (you'd never be able
// to find peaks that close to the edge anyway)
for (size_t j = 1; j < S.size() - 1; ++j) {
S[j] = Y[j - 1] - 2 * Y[j] + Y[j + 1];
}
}
return diffed;
}
//----------------------------------------------------------------------------------------------
/** Smooth data for Mariscotti.
* @param histograms :: Vector of histograms to be smoothed (inplace).
* @param w :: The number of data points which should contribute to each
* smoothed point
* @param g_z :: The number of smoothing steps given by g_z (should be 5)
*/
void FindPeaks::smoothData(std::vector<Histogram> &histograms, const int w, const int g_z) {
for (auto &histogram : histograms)
for (int i = 0; i < g_z; ++i)
histogram = smooth(histogram, w);
}
//----------------------------------------------------------------------------------------------
/** Calculates the statistical error on the smoothed data.
* Uses Mariscotti equation (11), amended to use errors of input data rather
* than sqrt(Y).
* @param input :: The input data to the algorithm
* @param smoothed :: The smoothed dataBackgroud type is not supported in
* FindPeak.cpp
* @param w :: The value of w (the size of the smoothing 'window')
* @throw std::invalid_argument if w is greater than 19
*/
void FindPeaks::calculateStandardDeviation(const API::MatrixWorkspace_const_sptr &input,
std::vector<HistogramData::Histogram> &smoothed, const int &w) {
// Guard against anyone changing the value of z, which would mean different
// phi values were needed (see Marriscotti p.312)
static_assert(g_z == 5, "Value of z has changed!");
// Have to adjust for fact that I normalise Si (unlike the paper)
const auto factor = static_cast<int>(std::pow(static_cast<double>(w), g_z));
const double constant = sqrt(static_cast<double>(this->computePhi(w))) / factor;
for (size_t i = 0; i < m_indexSet.size(); ++i) {
size_t i_in = m_indexSet[i];
smoothed[i].mutableE() = input->e(i_in) * constant;
}
}
//----------------------------------------------------------------------------------------------
/** Calculates the coefficient phi which goes into the calculation of the error
* on the smoothed data
* Uses Mariscotti equation (11). Pinched from the GeneralisedSecondDifference
* code.
* Can return a very big number, hence the type.
* @param w The value of w (the size of the smoothing 'window')
* @return The value of phi(g_z,w)
*/
long long FindPeaks::computePhi(const int &w) const {
const int m = (w - 1) / 2;
int zz = 0;
int max_index_prev = 1;
int n_el_prev = 3;
std::vector<long long> previous(n_el_prev);
previous[0] = 1;
previous[1] = -2;
previous[2] = 1;
// Can't happen at present
if (g_z == 0)
return std::accumulate(previous.begin(), previous.end(), static_cast<long long>(0),
VectorHelper::SumSquares<long long>());
std::vector<long long> next;
// Calculate the Cij iteratively.
do {
zz++;
int max_index = zz * m + 1;
int n_el = 2 * max_index + 1;
next.resize(n_el);
std::fill(next.begin(), next.end(), 0);
for (int i = 0; i < n_el; ++i) {
int delta = -max_index + i;
for (int l = delta - m; l <= delta + m; l++) {
int index = l + max_index_prev;
if (index >= 0 && index < n_el_prev)
next[i] += previous[index];
}
}
previous.resize(n_el);
std::copy(next.begin(), next.end(), previous.begin());
max_index_prev = max_index;
n_el_prev = n_el;
} while (zz != g_z);
const long long retval = std::accumulate(previous.begin(), previous.end(), static_cast<long long>(0),
VectorHelper::SumSquares<long long>());
g_log.debug() << "FindPeaks::computePhi - calculated value = " << retval << "\n";
return retval;
}
//----------------------------------------------------------------------------------------------
/** Find the index of a value (or nearest) in a given the X data
* @param vecX :: vector
* @param x :: value to search
* @return index of x in vector
*/
int FindPeaks::getIndex(const HistogramX &vecX, double x) {
int index;
if (x <= vecX.front()) {
// Left or equal to lower boundary
index = 0;
} else if (x >= vecX.back()) {
// Right or equal to upper boundary
index = static_cast<int>(vecX.size()) - 1;
} else {
// within the range
index = static_cast<int>(std::lower_bound(vecX.begin(), vecX.end(), x) - vecX.begin());
// check lower boundary
if (index == 0) {
std::stringstream errss;
errss << "Returned index = 0 for x = " << x << " with X[0] = " << vecX[0]
<< ". This situation is ruled out in this algorithm.";
g_log.warning(errss.str());
throw std::runtime_error(errss.str());
} else if (x < vecX[index - 1] || x > vecX[index]) {
std::stringstream errss;
errss << "Returned x = " << x << " is not between " << vecX[index - 1] << " and " << vecX[index]
<< ", which are returned by lower_bound.";
g_log.warning(errss.str());
throw std::runtime_error(errss.str());
}
// Find the index of the nearest value to return
if (x - vecX[index - 1] < vecX[index] - x)
--index;
}
return index;
}
//----------------------------------------------------------------------------------------------
/** Attempts to fit a candidate peak given a center and width guess.
* (This is not the CORE fit peak method)
*
* @param input :: The input workspace
* @param wsIndex :: The workspace index of the peak
* @param center_guess :: A guess of the X-value of the center of the peak, in
*whatever units of the X-axis of the workspace.
* @param fitWidth :: A guess of the full-width-half-max of the peak, in # of
*bins.
* @param hasleftpeak :: flag to show that there is a specified peak to its
*left
* @param leftpeakcentre :: centre of left peak if existed
* @param hasrightpeak :: flag to show that there is a specified peak to its
*right
* @param rightpeakcentre :: centre of the right peak if existed
*/
void FindPeaks::fitPeakGivenFWHM(const API::MatrixWorkspace_sptr &input, const int wsIndex, const double center_guess,
const int fitWidth, const bool hasleftpeak, const double leftpeakcentre,
const bool hasrightpeak, const double rightpeakcentre) {
// The X axis you are looking at
const auto &vecX = input->x(wsIndex);
const auto &vecY = input->y(wsIndex);
// Find i_center - the index of the center - The guess is within the X axis?
int i_centre = this->getIndex(vecX, center_guess);
// Set up lower fit boundary
int i_min = i_centre - 5 * fitWidth;
if (i_min < 1)
i_min = 1;
if (hasleftpeak) {
// Use 2/3 distance as the seperation for right peak
double xmin = vecX[i_min];
double peaksepline = center_guess - (center_guess - leftpeakcentre) * 0.66;
if (xmin < peaksepline)
i_min = getIndex(vecX, peaksepline);
}
// Set up upper boundary
int i_max = i_centre + 5 * fitWidth;
if (i_max >= static_cast<int>(vecX.size()) - 1)
i_max = static_cast<int>(vecY.size()) - 2;
if (hasrightpeak) {
// Use 2/3 distance as the separation for right peak
double xmax = vecX[i_max];
double peaksepline = center_guess + (rightpeakcentre - center_guess) * 0.66;
if (xmax > peaksepline)
i_max = getIndex(vecX, peaksepline);
}
// Check
if (i_max - i_min <= 0)
throw std::runtime_error("Impossible to i_min >= i_max.");
std::stringstream outss;
outss << "Fit peak with guessed FWHM: starting center = " << center_guess << ", FWHM = " << fitWidth
<< ". Estimated peak fit window from given FWHM: " << vecX[i_min] << ", " << vecX[i_max];
g_log.information(outss.str());
fitSinglePeak(input, wsIndex, i_min, i_max, i_centre);
}
//----------------------------------------------------------------------------------------------
/** Attempts to fit a candidate peak with a given window of where peak resides
*
* @param input The input workspace
* @param wsIndex The workspace index of the peak
* @param centre_guess :: Channel number of peak candidate i0 - the higher
*side of the peak (right side)
* @param xmin Minimum x value to find the peak
* @param xmax Maximum x value to find the peak
*/
void FindPeaks::fitPeakInWindow(const API::MatrixWorkspace_sptr &input, const int wsIndex, const double centre_guess,
const double xmin, const double xmax) {
// Check
g_log.information() << "Fit Peak with given window: Guessed center = " << centre_guess << " x-min = " << xmin
<< ", x-max = " << xmax << "\n";
if (xmin >= centre_guess || xmax <= centre_guess) {
g_log.warning("Peak centre is on the edge of Fit window. ");
addNonFitRecord(wsIndex, centre_guess);
return;
}
// The X axis you are looking at
const auto &vecX = input->x(wsIndex);
// The centre index
int i_centre = this->getIndex(vecX, centre_guess);
// The left index
int i_min = getIndex(vecX, xmin);
if (i_min >= i_centre) {
g_log.warning() << "Input peak centre @ " << centre_guess << " is out side of minimum x = " << xmin
<< ". Input X ragne = " << vecX.front() << ", " << vecX.back() << "\n";
addNonFitRecord(wsIndex, centre_guess);
return;
}
// The right index
int i_max = getIndex(vecX, xmax);
if (i_max < i_centre) {
g_log.warning() << "Input peak centre @ " << centre_guess << " is out side of maximum x = " << xmax << "\n";
addNonFitRecord(wsIndex, centre_guess);
return;
}
// finally do the actual fit
fitSinglePeak(input, wsIndex, i_min, i_max, i_centre);
}
//----------------------------------------------------------------------------------------------
/** Fit a single peak
* This is the fundametary peak fit function used by all kinds of input
*/
void FindPeaks::fitSinglePeak(const API::MatrixWorkspace_sptr &input, const int spectrum, const int i_min,
const int i_max, const int i_centre) {
const auto &vecX = input->x(spectrum);
const auto &vecY = input->y(spectrum);
// Exclude peak with peak counts
bool hasHighCounts = false;
for (int i = i_min; i <= i_max; ++i)
if (vecY[i] > m_leastMaxObsY) {
hasHighCounts = true;
break;
}
if (!hasHighCounts) {
std::stringstream ess;
ess << "Peak supposed at " << vecY[i_centre] << " does not have enough counts as " << m_leastMaxObsY;
g_log.debug(ess.str());
addNonFitRecord(spectrum, vecY[i_centre]);
return;
}
{
std::stringstream outss;
outss << "Fit single peak in X-range " << vecX[i_min] << ", " << vecX[i_max] << ", centre at " << vecX[i_centre]
<< " (index = " << i_centre << "). ";
g_log.information(outss.str());
}
// Estimate background
std::vector<double> vecbkgdparvalue(3, 0.);
std::vector<double> vecpeakrange(3, 0.);
int usefpdresult = findPeakBackground(input, spectrum, i_min, i_max, vecbkgdparvalue, vecpeakrange);
if (usefpdresult < 0) {
// Estimate background roughly for a failed case
estimateBackground(vecX, vecY, i_min, i_max, vecbkgdparvalue);
}
for (size_t i = 0; i < vecbkgdparvalue.size(); ++i)
if (i < m_bkgdOrder)
m_backgroundFunction->setParameter(i, vecbkgdparvalue[i]);
// Estimate peak parameters
double est_height(0.0), est_fwhm(0.0);
size_t i_obscentre(0);
double est_leftfwhm(0.0), est_rightfwhm(0.0);
std::string errmsg = estimatePeakParameters(vecX, vecY, i_min, i_max, vecbkgdparvalue, i_obscentre, est_height,
est_fwhm, est_leftfwhm, est_rightfwhm);
if (!errmsg.empty()) {
// Unable to estimate peak
i_obscentre = i_centre;
est_fwhm = 1.;
est_height = 1.;
g_log.warning(errmsg);
}
// Set peak parameters to
if (m_useObsCentre)
m_peakFunction->setCentre(vecX[i_obscentre]);
else
m_peakFunction->setCentre(vecX[i_centre]);
m_peakFunction->setHeight(est_height);
m_peakFunction->setFwhm(est_fwhm);
if (usefpdresult < 0) {
// Estimate peak range based on estimated linear background and peak
// parameter estimated from observation
if (!m_useObsCentre)
i_obscentre = i_centre;
estimatePeakRange(vecX, i_obscentre, i_min, i_max, est_leftfwhm, est_rightfwhm, vecpeakrange);
}
//-------------------------------------------------------------------------
// Fit Peak
//-------------------------------------------------------------------------
std::vector<double> fitwindow(2);
fitwindow[0] = vecX[i_min];
fitwindow[1] = vecX[i_max];
double costfuncvalue = callFitPeak(input, spectrum, m_peakFunction, m_backgroundFunction, fitwindow, vecpeakrange,
m_minGuessedPeakWidth, m_maxGuessedPeakWidth, m_stepGuessedPeakWidth);
bool fitsuccess = false;
if (costfuncvalue < DBL_MAX && costfuncvalue >= 0. && m_peakFunction->height() > m_minHeight) {
fitsuccess = true;
}
if (fitsuccess && m_usePeakPositionTolerance) {
fitsuccess = (fabs(m_peakFunction->centre() - vecX[i_centre]) < m_peakPositionTolerance);
}
//-------------------------------------------------------------------------
// Process Fit result
//-------------------------------------------------------------------------
// Update output
if (fitsuccess)
addInfoRow(spectrum, m_peakFunction, m_backgroundFunction, m_rawPeaksTable, costfuncvalue);
else
addNonFitRecord(spectrum, m_peakFunction->centre());
}
//----------------------------------------------------------------------------------------------
/** Find peak background given a certain range by
* calling algorithm "FindPeakBackground"
*/
int FindPeaks::findPeakBackground(const MatrixWorkspace_sptr &input, int spectrum, size_t i_min, size_t i_max,
std::vector<double> &vecBkgdParamValues, std::vector<double> &vecpeakrange) {
const auto &vecX = input->x(spectrum);
// Call FindPeakBackground
auto estimate = createChildAlgorithm("FindPeakBackground");
estimate->setLoggingOffset(1);
estimate->setProperty("InputWorkspace", input);
estimate->setProperty("WorkspaceIndex", spectrum);
// estimate->setProperty("SigmaConstant", 1.0);
std::vector<double> fwvec;
fwvec.emplace_back(vecX[i_min]);
fwvec.emplace_back(vecX[i_max]);
estimate->setProperty("BackgroundType", m_backgroundType);
estimate->setProperty("FitWindow", fwvec);
estimate->executeAsChildAlg();
// Get back the result
Mantid::API::ITableWorkspace_sptr peaklisttablews = estimate->getProperty("OutputWorkspace");
// Determine whether to use FindPeakBackground's result.
int fitresult = -1;
if (peaklisttablews->columnCount() < 7)
throw std::runtime_error("No 7th column for use FindPeakBackground result or not. ");
if (peaklisttablews->rowCount() > 0) {
/// @todo Allow setting of fitresult. Currently, fitresult is left
/// deliberately hidden by creating a new variable here with the same
/// name. This should be fixed but it causes different behaviour which
/// breaks several unit tests. The issue to deal with this is #13950. Other
/// related issues are #13667, #15978 and #19773.
const int hiddenFitresult = peaklisttablews->Int(0, 6);
g_log.information() << "fitresult=" << hiddenFitresult << "\n";
}
// Local check whether FindPeakBackground gives a reasonable value
vecpeakrange.resize(2);
/// @todo Remove this cppcheck suppression when #13950 is fixed
// cppcheck-suppress knownConditionTrueFalse
if (fitresult > 0) {
// Use FitPeakBackgroud's result
size_t i_peakmin, i_peakmax;
i_peakmin = peaklisttablews->Int(0, 1);