From eb52b33aea23bac35a0a9a13c4c003a8e5b00285 Mon Sep 17 00:00:00 2001 From: Spencer Fleming Date: Tue, 17 Sep 2019 20:19:48 -0600 Subject: [PATCH] max_iter is now a function argument again instead of a instance variable (#317) Also fixed PulseData testSetterGetter test (#314) --- src/GaussianFitter.cpp | 15 +++++++------- src/GaussianFitter.hpp | 7 +++---- src/GaussianFitter_unittests.cpp | 34 ++++++++++++++++---------------- src/LidarDriver.cpp | 11 ++++------- src/PulseData.cpp | 13 ++++++------ src/PulseData_unittests.cpp | 5 ++--- 6 files changed, 40 insertions(+), 45 deletions(-) diff --git a/src/GaussianFitter.cpp b/src/GaussianFitter.cpp index 02f2a65..ee50360 100644 --- a/src/GaussianFitter.cpp +++ b/src/GaussianFitter.cpp @@ -14,9 +14,7 @@ GaussianFitter::GaussianFitter(){ pass = 0; total = 0; - // Set fitter params to default values - max_iter = MAX_ITER; - + //Set instance variables to default values, as defined in header. tolerance_scales = TOL_SCALES; x_tolerance = X_TOL; g_tolerance = G_TOL; @@ -293,9 +291,8 @@ void handler (const char * reason, * @return */ int GaussianFitter::solve_system(gsl_vector *x, gsl_multifit_nlinear_fdf *fdf, - gsl_multifit_nlinear_parameters *params, int max) { - const size_t max_iter = this->max_iter; - + gsl_multifit_nlinear_parameters *params, int max, + const size_t max_iter) { const gsl_multifit_nlinear_type *T = gsl_multifit_nlinear_trust; const double xtol = tolerance_scales ? max / x_tolerance : x_tolerance; @@ -371,11 +368,13 @@ int GaussianFitter::solve_system(gsl_vector *x, gsl_multifit_nlinear_fdf *fdf, * @param results pointer to vector to store peaks * @param ampData * @param idxData + * @param max_iter * @return count of found peaks */ int GaussianFitter::find_peaks(std::vector* results, std::vector ampData, - std::vector idxData) { + std::vector idxData, + const size_t max_iter) { incr_total(); //Error handling @@ -464,7 +463,7 @@ int GaussianFitter::find_peaks(std::vector* results, spdlog::error("peakCount = {}",peakCount); - if(!solve_system(x, &fdf, &fdf_params, max)){ + if(!solve_system(x, &fdf, &fdf_params, max, max_iter)){ incr_pass(); //this loop is going through every peak diff --git a/src/GaussianFitter.hpp b/src/GaussianFitter.hpp index c20bfd4..960585d 100644 --- a/src/GaussianFitter.hpp +++ b/src/GaussianFitter.hpp @@ -35,7 +35,7 @@ class GaussianFitter{ public: int find_peaks(std::vector* results,std::vector ampData, - std::vector idxData); + std::vector idxData, const size_t max_iter); int noise_level; int guess_peaks(std::vector* results, std::vector ampData, @@ -58,8 +58,6 @@ class GaussianFitter{ // *** Fitter parameters (that were magic numbers once) *** - size_t max_iter; - bool tolerance_scales; double x_tolerance; double g_tolerance; @@ -78,7 +76,8 @@ class GaussianFitter{ int solve_system (gsl_vector *x, gsl_multifit_nlinear_fdf *fdf, - gsl_multifit_nlinear_parameters *params, int max); + gsl_multifit_nlinear_parameters *params, int max, + const size_t max_iter); std::vector calculateFirstDifferences(std::vectorampData); void incr_fail(); diff --git a/src/GaussianFitter_unittests.cpp b/src/GaussianFitter_unittests.cpp index 4469c84..a61b10a 100644 --- a/src/GaussianFitter_unittests.cpp +++ b/src/GaussianFitter_unittests.cpp @@ -74,7 +74,7 @@ TEST_F(GaussianFitterTest, NayaniClipped1){ EXPECT_EQ(34 ,peaks.at(1)->amp); fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); @@ -115,7 +115,7 @@ TEST_F(GaussianFitterTest, NayaniClipped2){ EXPECT_EQ(15, peaks.at(1)->amp); fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); @@ -158,7 +158,7 @@ TEST_F(GaussianFitterTest, gaussianFitter){ EXPECT_EQ(8, peaks.at(2)->amp); //this should't be here. fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(3,count); @@ -195,7 +195,7 @@ TEST_F(GaussianFitterTest, NayaniClipped3){ EXPECT_EQ(15,peaks.at(1)->amp); fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(2,count); //std::cerr << "--After guess_peaks and find_peaks--\n " << std::endl; @@ -236,7 +236,7 @@ TEST_F(GaussianFitterTest, NayaniClipped4){ EXPECT_EQ(7, peaks.at(1)->amp); fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(2,count); @@ -277,7 +277,7 @@ TEST_F(GaussianFitterTest, NayaniClipped5){ EXPECT_EQ(146,peaks.at(1)->amp); fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(2,count); @@ -322,7 +322,7 @@ TEST_F(GaussianFitterTest, NayaniClipped6){ fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(5,count); @@ -361,7 +361,7 @@ TEST_F(GaussianFitterTest, NayaniClipped7){ fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(3,count); @@ -400,7 +400,7 @@ TEST_F(GaussianFitterTest, NayaniClipped8){ EXPECT_EQ(21,peaks.at(2)->amp); fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(3,count); @@ -445,7 +445,7 @@ TEST_F(GaussianFitterTest, max_iter_1){ fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(3,count); @@ -484,7 +484,7 @@ TEST_F(GaussianFitterTest, max_iter_2){ fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(3,count); } @@ -521,7 +521,7 @@ TEST_F(GaussianFitterTest, max_iter_3){ EXPECT_EQ(33,peaks.at(2)->amp); fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(3,count); @@ -562,7 +562,7 @@ TEST_F(GaussianFitterTest, max_iter_4){ fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(3,count); } @@ -611,7 +611,7 @@ TEST_F(GaussianFitterTest, max_iter_5){ fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(8,count); @@ -649,7 +649,7 @@ TEST_F(GaussianFitterTest, trig_loc_1){ EXPECT_EQ(172,peaks.at(1)->amp); fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(2,count); @@ -689,7 +689,7 @@ TEST_F(GaussianFitterTest, trig_loc_2){ fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(3,count); @@ -727,7 +727,7 @@ TEST_F(GaussianFitterTest, num_iterations_10){ EXPECT_EQ(190, peaks.at(1)->amp); fitter.smoothing_expt(&Data); - int count = fitter.find_peaks(&peaks,ampData,idxData); + int count = fitter.find_peaks(&peaks,ampData,idxData,200); EXPECT_EQ(2,count); } diff --git a/src/LidarDriver.cpp b/src/LidarDriver.cpp index efa41f3..b01cea4 100644 --- a/src/LidarDriver.cpp +++ b/src/LidarDriver.cpp @@ -137,7 +137,7 @@ void LidarDriver::fit_data(FlightLineData &raw_data, LidarVolume &fitted_data, // Check parameter for using gaussian fitting or first differencing if (cmdLine.useGaussianFitting) { peak_count = fitter.find_peaks(&peaks, pd.returningWave, - pd.returningIdx); + pd.returningIdx, 200); } else { peak_count = fitter.guess_peaks(&peaks, pd.returningWave, pd.returningIdx); @@ -235,7 +235,7 @@ void LidarDriver::fit_data_csv(FlightLineData &raw_data, // Check parameter for using gaussian fitting or first differencing if (cmdLine.useGaussianFitting) { fitter.find_peaks(&peaks, pd.returningWave, - pd.returningIdx); + pd.returningIdx, 200); } else { fitter.guess_peaks(&peaks, pd.returningWave, pd.returningIdx); @@ -305,7 +305,7 @@ void LidarDriver::fit_data_csv(TxtWaveReader &raw_data, // Check parameter for using gaussian fitting or first differencing if (cmdLine.useGaussianFitting) { fitter.find_peaks(&peaks, raw_data.wave, - raw_data.idx); + raw_data.idx, 200); } else { fitter.guess_peaks(&peaks, raw_data.wave, raw_data.idx); @@ -397,11 +397,8 @@ void LidarDriver::peak_calculations(PulseData &pulse, std::vector &peaks, std::vector emitted_peaks; if (cmdLine.useGaussianFitting){ - size_t oldval = fitter.max_iter; - fitter.max_iter = 10; fitter.find_peaks(&emitted_peaks, pulse.outgoingWave, - pulse.outgoingIdx); - fitter.max_iter = oldval; + pulse.outgoingIdx, 200); } else { fitter.guess_peaks(&emitted_peaks, pulse.outgoingWave, pulse.outgoingIdx); diff --git a/src/PulseData.cpp b/src/PulseData.cpp index a4b59d9..043dbc0 100644 --- a/src/PulseData.cpp +++ b/src/PulseData.cpp @@ -47,28 +47,29 @@ void PulseData::setReturning(std::vector *idxArray, */ void PulseData::displayPulseData(std::ostream *outStream){ - *outStream << "\nOutgoing:\n" << std::endl; - *outStream << "\tindices:" << std::endl; + *outStream << "\nOutgoing:\n\n"; + *outStream << "\tindices:\n"; *outStream << "\t"; for(int i = 0; i<(int)outgoingIdx.size(); i++){ *outStream << outgoingIdx.at(i) << " "; } - *outStream << "\n\tamplitudes:" << std::endl; + *outStream << "\n\tamplitudes:\n"; *outStream << "\t"; for(int i = 0; i<(int)outgoingWave.size(); i++){ *outStream << outgoingWave.at(i) << " "; } - *outStream << "\n\nReturning Wave:\n" << std::endl; - *outStream << "\tindices:" << std::endl; + *outStream << "\n\nReturning Wave:\n\n"; + *outStream << "\tindices:\n"; *outStream << "\t"; for(int i = 0; i<(int)returningIdx.size(); i++){ *outStream << returningIdx.at(i) << " "; } - *outStream << "\n\tamplitudes:" << std::endl; + *outStream << "\n\tamplitudes:\n"; *outStream << "\t"; for(int i = 0; i<(int)returningWave.size(); i++){ *outStream << returningWave.at(i) << " "; } + outStream->flush(); } diff --git a/src/PulseData_unittests.cpp b/src/PulseData_unittests.cpp index 4064e3f..4b3cc18 100644 --- a/src/PulseData_unittests.cpp +++ b/src/PulseData_unittests.cpp @@ -54,11 +54,10 @@ TEST_F(PulseDataTest, testSetterGetter){ streamData = stream.str(); - realStream = "\nOutgoing:\n\n\tindices:\n\t0 1 2 \n\tamplitudes:" - " \n\t34 45 67 \n\nReturning Wave:\n\n\tindices:\n\t0 1 2 \n\t" + realStream = "\nOutgoing:\n\n\tindices:\n\t0 1 2 \n\tamplitudes:\n" + "\t34 45 67 \n\nReturning Wave:\n\n\tindices:\n\t0 1 2 \n\t" "amplitudes:\n\t76 65 43 "; - // testing for string will go here ASSERT_EQ( streamData, realStream); }