-
Notifications
You must be signed in to change notification settings - Fork 122
/
FindPeaks.cpp
1540 lines (1312 loc) · 58 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
/*WIKI*
This algorithm searches the specified spectra in a workspace for peaks, returning a list of the found and successfully fitted peaks. The search algorithm is described in full in reference [1]. In summary: the second difference of each spectrum is computed and smoothed. This smoothed data is then searched for patterns consistent with the presence of a peak. The list of candidate peaks found is passed to a fitting routine and those that are successfully fitted are kept and returned in the output workspace (and logged at information level).
The output [[TableWorkspace]] contains the following columns, which reflect the fact that the peak has been fitted to a Gaussian atop a linear background: spectrum, centre, width, height, backgroundintercept & backgroundslope.
=== Subalgorithms used ===
FindPeaks uses the [[SmoothData]] algorithm to, well, smooth the data - a necessary step to identify peaks in statistically fluctuating data. The [[Fit]] algorithm is used to fit candidate peaks.
=== Treating weak peaks vs. high background ===
FindPeaks uses a more complicated approach to fit peaks if '''HighBackground''' is flagged. In this case, FindPeak will fit the background first, and then do a Gaussian fit the peak with the fitted background removed. This procedure will be repeated for a couple of times with different guessed peak widths. And the parameters of the best result is selected. The last step is to fit the peak with a combo function including background and Gaussian by using the previously recorded best background and peak parameters as the starting values.
=== Criteria To Validate Peaks Found ===
FindPeaks finds peaks by fitting a Guassian with background to a certain range in the input histogram. [[Fit]] may not give a correct result even if chi^2 is used as criteria alone. Thus some other criteria are provided as options to validate the result
# Peak position. If peak positions are given, and trustful, then the fitted peak position must be within a short distance to the give one.
# Peak height. In the certain number of trial, peak height can be used to select the best fit among various starting sigma values.
=== Fit Window ===
If FitWindows is defined, then a peak's range to fit (i.e., x-min and x-max) is confined by this window.
If FitWindows is defined, starting peak centres are NOT user's input, but found by highest value within peak window. (Is this correct???)
==== References ====
# M.A.Mariscotti, ''A method for automatic identification of peaks in the presence of background and its application to spectrum analysis'', NIM '''50''' (1967) 309.
==== Estimation of peak's background and range ====
If FindPeaksBackground fails, then it is necessary to estimate a rough peak range and background according to
observed data.
1. Assume the local background (within the given fitting window) is close to linear;
2. Take the first 3 and last 3 data points to calcualte the linear background;
3. Remove background (rougly) and calcualte peak's height, width, and centre;
4. If the peak centre (starting value) uses observed value, then set peakcentre to that value. Otherwise, set it to given value;
5. Get the bin indexes of xmin, xmax and peakcentre;
6. Calcualte peak range, i.e., left and right boundary;
7. If any peak boundary exceeds or too close to the boundary, there will be 2 methods to solve this issue;
7.1 If peak centre is restricted to given value, then the peak range will be from 1/6 to 5/6 of the given data points;
7.2 If peak centre is set to observed value, then the 3 leftmost data points will be used for background.
==== References ====
# M.A.Mariscotti, ''A method for automatic identification of peaks in the presence of background and its application to spectrum analysis'', NIM '''50''' (1967) 309.
*WIKI*/
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/FindPeaks.h"
#include "MantidAlgorithms/FitPeak.h"
#include "MantidAPI/CostFunctionFactory.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/FuncMinimizerFactory.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/WorkspaceValidators.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/StartsWithValidator.h"
#include "MantidKernel/VectorHelper.h"
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <numeric>
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"
#include <fstream>
using namespace Mantid;
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
// const double MINHEIGHT = 2.00000001;
namespace Mantid
{
namespace Algorithms
{
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(FindPeaks)
//----------------------------------------------------------------------------------------------
/** Constructor
*/
FindPeaks::FindPeaks() : API::Algorithm(), m_progress(NULL)
{
m_minimizer = "Levenberg-MarquardtMD";
}
//----------------------------------------------------------------------------------------------
/** Sets documentation strings for this algorithm
*/
void FindPeaks::initDocs()
{
this->setWikiSummary("Searches for peaks in a dataset.");
this->setOptionalMessage("Searches for peaks in a dataset.");
}
//----------------------------------------------------------------------------------------------
/** Initialize and declare properties.
*/
void FindPeaks::init()
{
declareProperty(new WorkspaceProperty<>("InputWorkspace", "", Direction::Input),
"Name of the workspace to search");
auto mustBeNonNegative = boost::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 = boost::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(new 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(new 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", boost::make_shared<StringListValidator>(peakNames));
std::vector<std::string> bkgdtypes;
bkgdtypes.push_back("Flat");
bkgdtypes.push_back("Linear");
bkgdtypes.push_back("Quadratic");
declareProperty("BackgroundType", "Linear", boost::make_shared<StringListValidator>(bkgdtypes),
"Type of Background.");
declareProperty("HighBackground", true, "Relatively weak peak in high background");
auto mustBePositive = boost::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 = boost::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(new 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. ");
std::vector<std::string> costFuncOptions;
costFuncOptions.push_back("Chi-Square");
costFuncOptions.push_back("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. ");
return;
}
//----------------------------------------------------------------------------------------------
/** 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." << std::endl;
setProperty("PeaksList", m_outPeakTableWS);
return;
} // END: exec()
//----------------------------------------------------------------------------------------------
/** Process algorithm's properties
*/
void FindPeaks::processAlgorithmProperties()
{
// Input workspace
m_dataWS = getProperty("InputWorkspace");
// WorkspaceIndex
m_wsIndex = getProperty("WorkspaceIndex");
singleSpectrum = !isEmpty(m_wsIndex);
if (singleSpectrum && m_wsIndex >= static_cast<int>(m_dataWS->getNumberHistograms()))
{
g_log.error() << "The value of WorkspaceIndex provided (" << m_wsIndex
<< ") is larger than the size of this workspace (" << m_dataWS->getNumberHistograms()
<< ")\n";
throw Kernel::Exception::IndexError(m_wsIndex, m_dataWS->getNumberHistograms() - 1,
"FindPeaks WorkspaceIndex property");
}
// 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.error(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.size() > 0)
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");
return;
}
//----------------------------------------------------------------------------------------------
/** Generate a table workspace for output peak parameters
*/
void FindPeaks::generateOutputPeakParameterTable()
{
m_outPeakTableWS = WorkspaceFactory::Instance().createTable("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;
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_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
const int start = singleSpectrum ? m_wsIndex : 0;
const int end = singleSpectrum ? m_wsIndex + 1 : static_cast<int>(m_dataWS->getNumberHistograms());
m_progress = new Progress(this, 0.0, 1.0, end - start);
for (int spec = start; spec < end; ++spec)
{
const MantidVec& vecX = m_dataWS->readX(spec);
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 > vecX.front() && x_center < vecX.back())
{
if (useWindows)
fitPeakInWindow(m_dataWS, 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, 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 ("
<< vecX.front() << ", " << vecX.back() << ").\n";
}
} // 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.
MatrixWorkspace_sptr smoothedData = this->calculateSecondDifference(m_dataWS);
// The optimum number of points in the smoothing, according to Mariscotti, is 0.6*fwhm
int w = static_cast<int>(0.6 * m_inputPeakFWHM);
// w must be odd
if (!(w % 2))
++w;
// Carry out the number of smoothing steps given by g_z (should be 5)
for (int i = 0; i < g_z; ++i)
{
this->smoothData(smoothedData, w);
}
// 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 = static_cast<int>(kz * m_inputPeakFWHM + 0.5);
// Can't calculate n2 or n3 yet because they need i0
const int tolerance = getProperty("Tolerance");
// // Temporary - to allow me to look at smoothed data
// setProperty("SmoothedData",smoothedData);
// Loop over the spectra searching for peaks
const int start = singleSpectrum ? m_wsIndex : 0;
const int end = singleSpectrum ? m_wsIndex + 1 : static_cast<int>(smoothedData->getNumberHistograms());
m_progress = new Progress(this, 0.0, 1.0, end - start);
const int blocksize = static_cast<int>(smoothedData->blocksize());
for (int k = start; k < end; ++k)
{
const MantidVec &S = smoothedData->readY(k);
const MantidVec &F = smoothedData->readE(k);
// 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 < blocksize; ++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 = abs(static_cast<int>(0.5 * (F[i0] / S[i0]) * (n1 + tolerance) + 0.5));
const int n2b = abs(static_cast<int>(0.5 * (F[i0] / S[i0]) * (n1 - tolerance) + 0.5));
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 = abs(static_cast<int>((n1 + tolerance) * (1 - 2 * (F[i0] / S[i0])) + 0.5));
const int n3b = abs(static_cast<int>((n1 - tolerance) * (1 - 2 * (F[i0] / S[i0])) + 0.5));
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->readX(k)[i0] << " i1="
<< i1 << " i2=" << i2 << " i3=" << i3 << " i4=" << i4 << " i5=" << i5 << std::endl;
// Use i0, i2 and i4 to find out i_min and i_max, i0: right, i2: left, i4: centre
int wssize = static_cast<int>(m_dataWS->readX(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, 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
*/
API::MatrixWorkspace_sptr FindPeaks::calculateSecondDifference(
const API::MatrixWorkspace_const_sptr &input)
{
// We need a new workspace the same size as the input ont
MatrixWorkspace_sptr diffed = WorkspaceFactory::Instance().create(input);
const size_t numHists = input->getNumberHistograms();
const size_t blocksize = input->blocksize();
// Loop over spectra
for (size_t i = 0; i < size_t(numHists); ++i)
{
// Copy over the X values
diffed->dataX(i) = input->readX(i);
const MantidVec &Y = input->readY(i);
MantidVec &S = diffed->dataY(i);
// 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 < blocksize - 1; ++j)
{
S[j] = Y[j - 1] - 2 * Y[j] + Y[j + 1];
}
}
return diffed;
}
//----------------------------------------------------------------------------------------------
/** Calls the SmoothData algorithm as a Child Algorithm on a workspace.
* It is used in Mariscotti
* @param WS :: The workspace containing the data to be smoothed. The smoothed result will be stored in this pointer.
* @param w :: The number of data points which should contribute to each smoothed point
*/
void FindPeaks::smoothData(API::MatrixWorkspace_sptr &WS, const int &w)
{
g_log.information("Smoothing the input data");
IAlgorithm_sptr smooth = createChildAlgorithm("SmoothData");
smooth->setProperty("InputWorkspace", WS);
// The number of points which contribute to each smoothed point
std::vector<int> wvec;
wvec.push_back(w);
smooth->setProperty("NPoints", wvec);
smooth->executeAsChildAlg();
// Get back the result
WS = smooth->getProperty("OutputWorkspace");
}
//----------------------------------------------------------------------------------------------
/** 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,
const API::MatrixWorkspace_sptr &smoothed, const int &w)
{
// Guard against anyone changing the value of z, which would mean different phi values were needed (see Marriscotti p.312)
assert( g_z == 5);
// Have to adjust for fact that I normalise Si (unlike the paper)
const int factor = static_cast<int>(std::pow(static_cast<double>(w), g_z));
const double constant = sqrt(static_cast<double>(this->computePhi(w))) / factor;
const size_t numHists = smoothed->getNumberHistograms();
const size_t blocksize = smoothed->blocksize();
for (size_t i = 0; i < size_t(numHists); ++i)
{
const MantidVec &E = input->readE(i);
MantidVec &Fi = smoothed->dataE(i);
for (size_t j = 0; j < blocksize; ++j)
{
Fi[j] = constant * E[j];
}
}
}
//----------------------------------------------------------------------------------------------
/** 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 sorted vector (vector of x axis)
* @param vecX :: vector
* @param x :: value to search
* @return index of x in vector
*/
int FindPeaks::getVectorIndex(const MantidVec &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.error(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.error(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 spectrum :: The spectrum index of the peak (is actually the WorkspaceIndex)
* @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 spectrum,
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 MantidVec &vecX = input->readX(spectrum);
const MantidVec &vecY = input->readY(spectrum);
// Find i_center - the index of the center - The guess is within the X axis?
int i_centre = this->getVectorIndex(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 = getVectorIndex(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 = getVectorIndex(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, spectrum, i_min, i_max, i_centre);
return;
}
//----------------------------------------------------------------------------------------------
/** Attempts to fit a candidate peak with a given window of where peak resides
*
* @param input The input workspace
* @param spectrum The spectrum index of the peak (is actually the WorkspaceIndex)
* @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 spectrum,
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.error("Peak centre is on the edge of Fit window. ");
addNonFitRecord(spectrum);
return;
}
// The X axis you are looking at
const MantidVec &vecX = input->readX(spectrum);
// The centre index
int i_centre = this->getVectorIndex(vecX, centre_guess);
// The left index
int i_min = getVectorIndex(vecX, xmin);
if (i_min >= i_centre)
{
g_log.error() << "Input peak centre @ " << centre_guess << " is out side of minimum x = "
<< xmin << ". Input X ragne = " << vecX.front() << ", " << vecX.back() << "\n";
addNonFitRecord(spectrum);
return;
}
// The right index
int i_max = getVectorIndex(vecX, xmax);
if (i_max < i_centre)
{
g_log.error() << "Input peak centre @ " << centre_guess << " is out side of maximum x = "
<< xmax << "\n";
addNonFitRecord(spectrum);
return;
}
// finally do the actual fit
fitSinglePeak(input, spectrum, i_min, i_max, i_centre);
return;
}
//----------------------------------------------------------------------------------------------
/** 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 MantidVec& vecX = input->readX(spectrum);
const MantidVec& vecY = input->readY(spectrum);
//-------------------------------------------------------------------------
// Estimate peak and background parameters for better fitting
//-------------------------------------------------------------------------
std::stringstream outss;
outss << "Fit single peak in X-range " << input->readX(spectrum)[i_min] << ", "
<< input->readX(spectrum)[i_max] << ", centre at " << input->readX(spectrum)[i_centre]
<< " (index = " << i_centre << "). ";
g_log.information(outss.str());
// Estimate background
std::vector<double> vecbkgdparvalue(3, 0.);
std::vector<double> vecpeakrange(3, 0.);
bool usefpdresult = findPeakBackground(input, spectrum, i_min, i_max, vecbkgdparvalue, vecpeakrange);
if (!usefpdresult)
{
// 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, est_fwhm;
size_t i_obscentre;
double est_leftfwhm, est_rightfwhm;
std::string errmsg = estimatePeakParameters(vecX, vecY, i_min, i_max, vecbkgdparvalue, i_obscentre, est_height,
est_fwhm, est_leftfwhm, est_rightfwhm);
if (errmsg.size() > 0)
{
// Unable to estimate peak
i_obscentre = i_centre;
est_fwhm = 1.;
est_height = 1.;
g_log.error(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)
{
// 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;
//-------------------------------------------------------------------------