Skip to content
Snippets Groups Projects
Commit adec90d4 authored by Zhou, Wenduo's avatar Zhou, Wenduo
Browse files

Refs #20796. Tried to fix issue with unit test timeout.

parent 0965b56d
No related branches found
No related tags found
No related merge requests found
...@@ -74,10 +74,6 @@ private: ...@@ -74,10 +74,6 @@ private:
/// create output workspace /// create output workspace
void createOutputWorkspaces(); void createOutputWorkspaces();
/// main method to find peak background
int findBackground(std::vector<size_t> &peak_min_max_indexes,
std::vector<double> &bkgd3);
/// set histogram data to find background /// set histogram data to find background
void setHistogram(const HistogramData::Histogram &histogram); void setHistogram(const HistogramData::Histogram &histogram);
...@@ -90,10 +86,9 @@ private: ...@@ -90,10 +86,9 @@ private:
/// set fit window /// set fit window
void setFitWindow(const std::vector<double> &window); void setFitWindow(const std::vector<double> &window);
/// add result to output workpsace int findBackground(const size_t &l0, const size_t &n,
void addResultToOutput(const int &goodfit, std::vector<size_t> &peak_min_max_indexes,
const std::vector<size_t> &peakindexes, std::vector<double> &bkgd3);
const std::vector<double> &bkgdparams);
/// histogram data to find peak background /// histogram data to find peak background
boost::shared_ptr<const HistogramData::Histogram> m_histogram; boost::shared_ptr<const HistogramData::Histogram> m_histogram;
...@@ -101,8 +96,6 @@ private: ...@@ -101,8 +96,6 @@ private:
// HistogramData::Histogram& m_histogram; // HistogramData::Histogram& m_histogram;
/// fit window /// fit window
std::vector<double> m_vecFitWindows; std::vector<double> m_vecFitWindows;
size_t m_iStartX; /// l0 in original code
size_t m_iStopX; /// n in original code
/// background order: 0 for flat, 1 for linear, 2 for quadratic /// background order: 0 for flat, 1 for linear, 2 for quadratic
size_t m_backgroundOrder; size_t m_backgroundOrder;
/// constant sigma /// constant sigma
...@@ -118,6 +111,8 @@ private: ...@@ -118,6 +111,8 @@ private:
// /// get result // /// get result
// void getBackgroundResult(); // void getBackgroundResult();
void findWindowIndex(size_t &l0, size_t &n);
struct cont_peak { struct cont_peak {
size_t start; size_t start;
size_t stop; size_t stop;
......
...@@ -64,6 +64,23 @@ void FindPeakBackground::init() { ...@@ -64,6 +64,23 @@ void FindPeakBackground::init() {
"quadratic terms."); "quadratic terms.");
} }
void FindPeakBackground::findWindowIndex(size_t &l0, size_t &n) {
auto &inpX = m_histogram->x();
auto &inpY = m_histogram->y();
size_t sizey = inpY.size(); // inpWS->y(inpwsindex).size();
// determine the fit window with their index in X (or Y)
n = sizey;
l0 = 0;
if (m_vecFitWindows.size() > 1) {
Mantid::Algorithms::FindPeaks fp;
l0 = fp.getIndex(inpX, m_vecFitWindows[0]);
n = fp.getIndex(inpX, m_vecFitWindows[1]);
if (n < sizey)
n++;
}
}
//---------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------
/** Execute body /** Execute body
*/ */
...@@ -71,36 +88,31 @@ void FindPeakBackground::exec() { ...@@ -71,36 +88,31 @@ void FindPeakBackground::exec() {
// Get input and validate // Get input and validate
processInputProperties(); processInputProperties();
// auto &inpX = m_histogram->x(); size_t l0, n;
// auto &inpY = m_histogram->y(); findWindowIndex(l0, n);
// size_t sizex = inpX.size(); // inpWS->x(inpwsindex).size();
// size_t sizey = inpY.size(); // inpWS->y(inpwsindex).size(); // m_vecFitWindows won't be used again form this point till end.
// // determine the fit window with their index in X (or Y)
// size_t n = sizey;
// size_t l0 = 0;
// if (m_vecFitWindows.size() > 1) {
// Mantid::Algorithms::FindPeaks fp;
// l0 = fp.getIndex(inpX, m_vecFitWindows[0]);
// n = fp.getIndex(inpX, m_vecFitWindows[1]);
// if (n < sizey)
// n++;
// }
// // m_vecFitWindows won't be used again form this point till end.
// Set up output table workspace // Set up output table workspace
createOutputWorkspaces(); createOutputWorkspaces();
// set progress 10% // 3. Get Y values
Progress prog(this, 0.0, 1.0, 1); Progress prog(this, 0.0, 1.0, 1);
// Find background std::vector<size_t> peak_min_max_indexes;
std::vector<size_t> peakindexes; std::vector<double> bkgd3;
std::vector<double> bkgdparams; int goodfit = findBackground(l0, n, peak_min_max_indexes, bkgd3);
int goodfit = findBackground(peakindexes, bkgdparams);
// add to output workspace if (goodfit > 0) {
addResultToOutput(goodfit, peakindexes, bkgdparams); size_t min_peak = peak_min_max_indexes[0];
size_t max_peak = peak_min_max_indexes[1];
double a0 = bkgd3[0];
double a1 = bkgd3[1];
double a2 = bkgd3[2];
API::TableRow t = m_outPeakTableWS->getRow(0);
t << static_cast<int>(m_inputWSIndex) << static_cast<int>(min_peak)
<< static_cast<int>(max_peak) << a0 << a1 << a2 << goodfit;
}
prog.report(); prog.report();
...@@ -108,48 +120,17 @@ void FindPeakBackground::exec() { ...@@ -108,48 +120,17 @@ void FindPeakBackground::exec() {
setProperty("OutputWorkspace", m_outPeakTableWS); setProperty("OutputWorkspace", m_outPeakTableWS);
} }
//----------------------------------------------------------------------------------------------
/**
* @brief FindPeakBackground::addResultToOutput
* @param goodfit
* @param peakindexes
* @param bkgdparams
*/
void FindPeakBackground::addResultToOutput(
const int &goodfit, const std::vector<size_t> &peakindexes,
const std::vector<double> &bkgdparams) {
if (goodfit > 0) {
int min_peak = static_cast<int>(peakindexes[0]);
int max_peak = static_cast<int>(peakindexes[1]);
double a0 = bkgdparams[0];
double a1 = bkgdparams[1];
double a2 = bkgdparams[2];
API::TableRow t = m_outPeakTableWS->getRow(0);
t << static_cast<int>(m_inputWSIndex) << min_peak << max_peak << a0 << a1
<< a2 << goodfit;
}
return;
}
//----------------------------------------------------------------------------------------------
/** Main method to process find peak background
* Note: all inputs shall be set up by this method
* @brief FindPeakBackground::findBackground
* @param peak_min_max_indexes
* @param bkgd3
* @return integer as good fit or not!
*/
int FindPeakBackground::findBackground( int FindPeakBackground::findBackground(
const size_t &l0, const size_t &n,
std::vector<size_t> &peak_min_max_indexes, std::vector<double> &bkgd3) { std::vector<size_t> &peak_min_max_indexes, std::vector<double> &bkgd3) {
// get something from previously set up class variable
auto &inpX = m_histogram->x(); auto &inpX = m_histogram->x();
auto &inpY = m_histogram->y(); auto &inpY = m_histogram->y();
size_t l0 = m_iStartX; size_t sizex = inpX.size(); // inpWS->x(inpwsindex).size();
size_t n = m_iStopX; size_t sizey = inpY.size(); // inpWS->y(inpwsindex).size();
int goodfit(0); int goodfit(0);
// Find background
double Ymean, Yvariance, Ysigma; double Ymean, Yvariance, Ysigma;
MantidVec maskedY; MantidVec maskedY;
auto in = std::min_element(inpY.cbegin(), inpY.cend()); auto in = std::min_element(inpY.cbegin(), inpY.cend());
...@@ -217,8 +198,7 @@ int FindPeakBackground::findBackground( ...@@ -217,8 +198,7 @@ int FindPeakBackground::findBackground(
if (inpY[l + l0] > peaks[ipeak].maxY) if (inpY[l + l0] > peaks[ipeak].maxY)
peaks[ipeak].maxY = inpY[l + l0]; peaks[ipeak].maxY = inpY[l + l0];
} }
} // END-FOR (l) }
size_t min_peak, max_peak; size_t min_peak, max_peak;
double a0 = 0., a1 = 0., a2 = 0.; double a0 = 0., a1 = 0., a2 = 0.;
if (!peaks.empty()) { if (!peaks.empty()) {
...@@ -231,9 +211,7 @@ int FindPeakBackground::findBackground( ...@@ -231,9 +211,7 @@ int FindPeakBackground::findBackground(
// save endpoints // save endpoints
min_peak = peaks[0].start; min_peak = peaks[0].start;
// extra point for histogram input // extra point for histogram input
// size_t sizex = inpX.size(); // inpWS->x(inpwsindex).size(); max_peak = peaks[0].stop + sizex - sizey;
// size_t sizey = inpY.size(); // inpWS->y(inpwsindex).size();
max_peak = peaks[0].stop + inpX.size() - inpY.size();
goodfit = 1; goodfit = 1;
} else { } else {
// assume the whole thing is background // assume the whole thing is background
...@@ -243,16 +221,11 @@ int FindPeakBackground::findBackground( ...@@ -243,16 +221,11 @@ int FindPeakBackground::findBackground(
goodfit = 2; goodfit = 2;
} }
estimateBackground(inpX, inpY, l0, n, min_peak, max_peak, (!peaks.empty()), estimateBackground(inpX, inpY, l0, n, min_peak, max_peak, (!peaks.empty()),
a0, a1, a2); a0, a1, a2);
// Add a new row // Add a new row
// API::TableRow t = m_outPeakTableWS->getRow(0);
// t << static_cast<int>(m_inputWSIndex) << static_cast<int>(min_peak)
// << static_cast<int>(max_peak) << a0 << a1 << a2 << goodfit;
// set up good fit result
peak_min_max_indexes.resize(2); peak_min_max_indexes.resize(2);
peak_min_max_indexes[0] = min_peak; peak_min_max_indexes[0] = min_peak;
peak_min_max_indexes[1] = max_peak; peak_min_max_indexes[1] = max_peak;
...@@ -261,8 +234,7 @@ int FindPeakBackground::findBackground( ...@@ -261,8 +234,7 @@ int FindPeakBackground::findBackground(
bkgd3[0] = a0; bkgd3[0] = a0;
bkgd3[1] = a1; bkgd3[1] = a1;
bkgd3[2] = a2; bkgd3[2] = a2;
}
} // END-IF: if (n - l0 > 5)
return goodfit; return goodfit;
} }
...@@ -467,9 +439,9 @@ void FindPeakBackground::processInputProperties() { ...@@ -467,9 +439,9 @@ void FindPeakBackground::processInputProperties() {
// background // background
m_backgroundType = getPropertyValue("BackgroundType"); m_backgroundType = getPropertyValue("BackgroundType");
size_t bkgdorder = 0; size_t bkgdorder = 0;
if (m_backgroundType.compare("Linear") == 0) if (m_backgroundType == "Linear")
bkgdorder = 1; bkgdorder = 1;
else if (m_backgroundType.compare("Quadratic") == 0) else if (m_backgroundType == "Quadratic")
bkgdorder = 2; bkgdorder = 2;
setBackgroundOrder(bkgdorder); setBackgroundOrder(bkgdorder);
...@@ -494,25 +466,12 @@ void FindPeakBackground::setBackgroundOrder(size_t order) { ...@@ -494,25 +466,12 @@ void FindPeakBackground::setBackgroundOrder(size_t order) {
m_backgroundOrder = order; m_backgroundOrder = order;
} }
////----------------------------------------------------------------------------------------------
///** find background (main algorithm)
// * @brief FindPeakBackground::findPeakBackground
// */
// void FindPeakBackground::findPeakBackground() {return;}
///// get result
// void getBackgroundResult();
//---------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------
/** set fit window /** set fit window
* @brief FindPeakBackground::setFitWindow * @brief FindPeakBackground::setFitWindow
* @param fitwindow * @param fitwindow
*/ */
void FindPeakBackground::setFitWindow(const std::vector<double> &fitwindow) { void FindPeakBackground::setFitWindow(const std::vector<double> &fitwindow) {
size_t l0 = 0;
size_t n = m_histogram->y().size();
// validate input // validate input
if (m_vecFitWindows.size() == 0) { if (m_vecFitWindows.size() == 0) {
m_vecFitWindows.resize(2); m_vecFitWindows.resize(2);
...@@ -522,39 +481,11 @@ void FindPeakBackground::setFitWindow(const std::vector<double> &fitwindow) { ...@@ -522,39 +481,11 @@ void FindPeakBackground::setFitWindow(const std::vector<double> &fitwindow) {
m_vecFitWindows[0] >= m_vecFitWindows[1]) { m_vecFitWindows[0] >= m_vecFitWindows[1]) {
throw std::invalid_argument("Fit window has either wrong item number or " throw std::invalid_argument("Fit window has either wrong item number or "
"window value is not in ascending order."); "window value is not in ascending order.");
} else {
Mantid::Algorithms::FindPeaks fp;
l0 = fp.getIndex(m_histogram->x(), m_vecFitWindows[0]);
n = fp.getIndex(m_histogram->x(), m_vecFitWindows[1]);
if (n < m_histogram->y().size())
n++;
} }
// m_vecFitWindows.resize(2);
// copy the input to class variable // copy the input to class variable
m_vecFitWindows = fitwindow; m_vecFitWindows = fitwindow;
m_iStartX = l0;
m_iStopX = n;
// ---- from here .. all from original code
// auto &inpX = m_histogram->x();
// auto &inpY = m_histogram->y();
// size_t sizex = inpX.size(); // inpWS->x(inpwsindex).size();
// size_t sizey = inpY.size(); // inpWS->y(inpwsindex).size();
// determine the fit window with their index in X (or Y)
// size_t n = sizey;
// size_t l0 = 0;
// if (m_vecFitWindows.size() > 1) {
// Mantid::Algorithms::FindPeaks fp;
// l0 = fp.getIndex(inpX, m_vecFitWindows[0]);
// n = fp.getIndex(inpX, m_vecFitWindows[1]);
// if (n < sizey)
// n++;
// }
// m_vecFitWindows won't be used again form this point till end.
return;
} }
//---------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment