Skip to content

Commit

Permalink
#320 fixing merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
FloCiaglia committed Oct 23, 2019
2 parents 2646466 + 43c61e4 commit eeaed34
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 125 deletions.
31 changes: 23 additions & 8 deletions src/CmdLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ void CmdLine::setUsageMessage()
<< " :Disables gaussian fitter, using first diff method instead" << std::endl;
buffer << " -h"
<< " :Prints this help message" << std::endl;
buffer << " -n <level>"
<< " :Sets the noise level. Defaults to 6.\n";
buffer << std::endl;
buffer << "Product Type Options:" << std::endl;
buffer << " -e <list of products>"
Expand Down Expand Up @@ -133,7 +135,6 @@ CmdLine::CmdLine(){
printUsageMessage = false;
useGaussianFitting = true;
calcBackscatter = false;
verb = (verbosity) NULL;
exeName = "";
setUsageMessage();
}
Expand All @@ -151,14 +152,17 @@ std::string CmdLine::getInputFileName(bool pls){
/**
* Tries to match option given with verbosity flag to a known value,
* and if so, sets verbosity instance variable to match it.
* @param new_verb logger level inputted in the command line
* @return true if valid leve, else false
*/
void CmdLine::set_verbosity (char* new_verb) {
int i;
for (i = 0; i < num_verbs; i++) {
bool CmdLine::set_verbosity (char* new_verb) {
for (int i = 0; i < num_verbs; i++) {
if (!std::strncmp (new_verb, verbs[i].c_str(), 9)) {
verb = (verbosity) i;
verb = new_verb;
return true;
}
}
return false;
}

/**
Expand Down Expand Up @@ -191,6 +195,7 @@ bool CmdLine::parse_args(int argc,char *argv[]){
{
{"file", required_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"noise_level", required_argument, NULL, 'n'},
{"firstdiff", no_argument, NULL, 'd'},
{"verbosity", required_argument, NULL, 'v'},
{"elevation", required_argument,NULL,'e'},
Expand All @@ -210,7 +215,7 @@ bool CmdLine::parse_args(int argc,char *argv[]){
* ":hf:s:" indicate that option 'h' is without arguments while
* option 'f' and 's' require arguments
*/
while((optionChar = getopt_long (argc, argv, "-:hdf:e:a:w:r:b:l:v:",
while((optionChar = getopt_long (argc, argv, "-:hdf:n:e:a:w:r:b:l:v:",
long_options, &option_index))!= -1){
if (optionChar == 'f') { //Set the filename to parse
fArg = optarg;
Expand All @@ -219,9 +224,19 @@ bool CmdLine::parse_args(int argc,char *argv[]){
printUsageMessage = true;
} else if (optionChar == 'd') { //Sets analysis method
useGaussianFitting = false;
}else if (optionChar == 'n'){
try{
noise_level = std::stoi(optarg);
}catch(const std::invalid_argument& e){
msgs.push_back("Cannot convert noise level to int. Error: " + std::string(e.what()));
printUsageMessage = true;
}catch(const std::out_of_range& e){
msgs.push_back("Cannot fit noise level in type int. Error: " + std::string(e.what()));
printUsageMessage = true;
}
} else if (optionChar == 'v') {
set_verbosity(optarg);
if (verb == (verbosity) NULL) {
if (!set_verbosity(optarg)) {
msgs.push_back("Invalid logging level");
printUsageMessage = true;
}
} else if (optionChar == 'e' || optionChar == 'a' || optionChar == 'w'
Expand Down
11 changes: 7 additions & 4 deletions src/CmdLine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cstring>
#include <stdexcept>
#include <stdlib.h>
#include <map>

class CmdLine{

Expand All @@ -31,7 +32,7 @@ class CmdLine{
// else, use smooth second difference
bool max_elev_flag;

void set_verbosity(char* new_verb);
bool set_verbosity(char* new_verb);

public:
//calibration constant (for backscatter option)
Expand All @@ -43,15 +44,17 @@ class CmdLine{
// True = gaussian fitting, False = first differencing
bool useGaussianFitting;

//Default noise level
int noise_level = 6;

// Whether or not backscatter coefficient has been requested
bool calcBackscatter;
bool calcBackscatter;

//True stifles all output statements
bool quiet;

// For conveying verbosity to main function in a readable way
enum verbosity { trace, debug, info, warn, error, critical };
verbosity verb;
std::string verb = "";

CmdLine();

Expand Down
119 changes: 47 additions & 72 deletions src/GaussianFitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ GaussianFitter::GaussianFitter(){
g_tolerance = G_TOL;
f_tolerance = F_TOL;

guess_lt0_default = GUESS_LT0_DEFAULT;
guess_lessthan_0_default = GUESS_LT0_DEFAULT;
guess_upper_lim = GUESS_UPPER_LIM;
guess_gt_upper_lim_default = GUESS_GT_UPPER_LIM_DEFAULT;
guess_upper_lim_default = GUESS_UPPER_LIM_DEFAULT;

amp_upper_bound = AMP_UPPER_BOUND;
max_amp_multiplier = MAX_AMP_MULTIPLIER;
amp_lower_bound = AMP_LOWER_BOUND;

log_diagnostics = false;
Expand Down Expand Up @@ -375,6 +375,8 @@ int GaussianFitter::find_peaks(std::vector<Peak*>* results,
std::vector<int> ampData,
std::vector<int> idxData,
const size_t max_iter) {
spdlog::trace("--NEW WAVEFORM--");

incr_total();

//Error handling
Expand All @@ -390,12 +392,15 @@ int GaussianFitter::find_peaks(std::vector<Peak*>* results,
//get guessed peaks and figure out how many peaks there are
size_t peakCount = guess_peaks(results, ampData, idxData);

spdlog::trace("Peak Count = {}", peakCount);

//No peaks found
//Prvents the "Parameter 7 to routine source_gemv_r.h was incorrect" error
if(peakCount == 0){
return 0;
}


// FOR TESTING PURPOSES
// fprintf(stderr, "Peak count is %d\n", peakCount);

Expand Down Expand Up @@ -446,23 +451,21 @@ int GaussianFitter::find_peaks(std::vector<Peak*>* results,
}

// PRINT DATA AND MODEL FOR TESTING PURPOSES
spdlog::trace("Gaussian sum based on guesses - before solve system:");
for (int i = 0; i < n; ++i){
double ti = fit_data.t[i];
// double yi = fit_data.y[i];
double fi = gaussianSum(x, ti);
printf("%f ", fi);
}
spdlog::trace("Gaussian sum based on guesses - before solve system:");
std::ostringstream function;
for (int i = 0; i < n; ++i){
double ti = fit_data.t[i];
// double yi = fit_data.y[i];
function << gaussianSum(x, ti) << " ";
}
spdlog::trace("Model Data: {}", function.str());


fdf_params.trs = gsl_multifit_nlinear_trs_dogleg;
fdf_params.scale = gsl_multifit_nlinear_scale_more;
fdf_params.solver = gsl_multifit_nlinear_solver_svd;
fdf_params.fdtype = GSL_MULTIFIT_NLINEAR_CTRDIFF;

spdlog::error("peakCount = {}",peakCount);

if(!solve_system(x, &fdf, &fdf_params, max, max_iter)){
incr_pass();

Expand Down Expand Up @@ -514,7 +517,7 @@ int GaussianFitter::find_peaks(std::vector<Peak*>* results,
//calculate rise time
peak->rise_time = peak->location - peak->triggering_location;

if(peak->amp >= amp_upper_bound*max || peak->amp < amp_lower_bound
if(peak->amp >= max_amp_multiplier*max || peak->amp < amp_lower_bound
|| peak->triggering_location > n
|| peak->triggering_location <0) {
delete(peak);
Expand All @@ -526,50 +529,41 @@ int GaussianFitter::find_peaks(std::vector<Peak*>* results,
peak->position_in_wave = i+1;
}

spdlog::error("--------------------");
spdlog::error("results.size = {}", results->size());
spdlog::error("is empty = {}", results->empty());

if (!results->empty()) {
Peak* final_peak_ptr = results->back();
final_peak_ptr->is_final_peak = true; //mark the last peak as
//final
}
i++;
}
// PRINT DATA AND MODEL FOR TESTING PURPOSES
spdlog::trace("Gaussian sum in solve system and not failed:");

for (int i = 0; i < n; ++i){
double ti = fit_data.t[i];
// double yi = fit_data.y[i];
double fi = gaussianSum(x, ti);
printf("%f ", fi);
}

spdlog::trace("Number of Peaks Found: {}", results->size());

// PRINT DATA AND MODEL FOR TESTING PURPOSES
spdlog::trace("Gaussian sum in solve system and not failed:");
std::ostringstream model;
for (int i = 0; i < n; ++i){
double ti = fit_data.t[i];
model << gaussianSum(x, ti) << " ";
}
spdlog::trace("Model Data: {}", model.str());
}
else{
// FOR TESTING PURPOSES
spdlog::trace("In solve system and failed:");
spdlog::error("Amplitudes: ");

for(int i=0; i< (int)ampData.size(); i++){
spdlog::error("{}", ampData[i]);
}
spdlog::error("Indices in time: ");
for(int i=0; i< (int)idxData.size(); i++){
spdlog::error( "{}", idxData[i]);
}

incr_fail();

// PRINT DATA AND MODEL FOR TESTING PURPOSES
spdlog::trace("Gaussian sum in solve system failed:");
for (int i = 0; i < n; ++i){
double ti = fit_data.t[i];
double yi = fit_data.y[i];
double fi = gaussianSum(x, ti);
printf("%f %f %f\n", ti, yi, fi);
}
// PRINT DATA AND MODEL FOR TESTING PURPOSES
spdlog::trace("Gaussian sum in solve system failed:");
std::ostringstream time, data, model;
for (int i = 0; i < n; ++i){
double ti = fit_data.t[i];
time << ti << " ";
data << fit_data.y[i] << " ";
model << gaussianSum(x, ti) << " ";
}
spdlog::trace("Time Data: {}",time.str());
spdlog::trace("Amp Data: {}",data.str());
spdlog::trace("Model Data: {}",model.str());
}

free(fit_data.t);
Expand Down Expand Up @@ -628,30 +622,7 @@ int GaussianFitter::guess_peaks(std::vector<Peak*>* results,
//We need to start this function with a clear vector.
//We can't call destructors because we don't know if the pointers
//are pointing to space used in LidarVolume
results->clear();

//UPDATE: We are only using a noise level of 6 because we want all peaks
//with an amplitude >= 10
//Level up to and including which peaks will be excluded
//For the unaltered wave, noise_level = 16
//for the second derivative of the wave, noise_level = 3
// this is creating guesses for a guassian fitter that does not do
// well if we have guesses that have an amplitude more than an order
// of magnitute apart. We are going to set the noise level to be the
// max value/ 10 - max*.05;
max = 0;
for(int i = 0; i<(int)ampData.size(); i++){
if(ampData[i]>max){
max = ampData[i];
}
}
noise_level = ((float)max)*.09;

spdlog::error("Max = {} Noise = {}", max, ((float)max)*.09);

if (noise_level < 6){
noise_level = 6;
}
results->clear();

//Sign of gradient:
// = 1 for increasing
Expand Down Expand Up @@ -729,7 +700,11 @@ int GaussianFitter::guess_peaks(std::vector<Peak*>* results,
}

if(guess< 0) {
guess = guess_lt0_default;
spdlog::warn(
"Guess for peak width less than zero, reverting to default"
);

guess = guess_lessthan_0_default;
}

if (log_diagnostics) {
Expand All @@ -739,7 +714,7 @@ int GaussianFitter::guess_peaks(std::vector<Peak*>* results,
guess);
}

if(guess > 20) {guess = 10;}
if(guess > guess_upper_lim) {guess = guess_upper_lim_default;}
peaks_found++;

//Create a peak
Expand Down
20 changes: 10 additions & 10 deletions src/GaussianFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
#define MAX_ITER 200

#define TOL_SCALES true
#define X_TOL 100
#define G_TOL 100
#define F_TOL 100
#define X_TOL 100.
#define G_TOL 100.
#define F_TOL 100.

#define GUESS_LT0_DEFAULT 4
#define GUESS_UPPER_LIM 20
#define GUESS_GT_UPPER_LIM_DEFAULT 10
#define GUESS_UPPER_LIM_DEFAULT 10

#define AMP_UPPER_BOUND 2
#define MAX_AMP_MULTIPLIER 2.
#define AMP_LOWER_BOUND 10

class GaussianFitter{
Expand Down Expand Up @@ -63,12 +63,12 @@ class GaussianFitter{
double g_tolerance;
double f_tolerance;

int guess_lt0_default;
int guess_upper_lim;
int guess_gt_upper_lim_default;
int guess_lessthan_0_default; // If guess less than 0, it is set to this
int guess_upper_lim; // If guess greater than this value...
int guess_upper_lim_default; // It is set to this value

int amp_upper_bound; // Val is multiplied by max data point in wave
int amp_lower_bound; // Val is unmodified (no multiplication)
float max_amp_multiplier; // Val is multiplied by max data point in wave
float amp_lower_bound; // Val is unmodified (no multiplication)


private:
Expand Down
Loading

0 comments on commit eeaed34

Please sign in to comment.