Skip to content

Commit 03ba51f

Browse files
authored
Merge pull request #205 from aglowacki/master
Bug fixes
2 parents 090750d + 264e7c2 commit 03ba51f

File tree

8 files changed

+68
-68
lines changed

8 files changed

+68
-68
lines changed

src/core/main.cpp

+23-36
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ void help()
7878
logit_s<<"--optimize-fit-override-params : <int> Integrate the 8 largest mda datasets and fit with multiple params.\n"<<
7979
" 0 = use override file\n 1 = matrix batch fit\n 2 = batch fit without tails\n 3 = batch fit with tails\n 4 = batch fit with free E, everything else fixed \n 5 = batch fit without tails, and fit energy quadratic\n";
8080
logit_s<<"--optimize-fit-routine : <general,hybrid> General (default): passes elements amplitudes as fit parameters. Hybrid only passes fit parameters and fits element amplitudes using NNLS\n";
81-
//logit_s<<"--optimizer <lmfit, mpfit> : Choose which optimizer to use for --optimize-fit-override-params or matrix fit routine \n";
82-
// logit_s<<"--optimizer-fx-tols <tol_override_val> : F_TOL, X_TOL, Default is LM_FIT = " << DP_LM_USERTOL << " , MP_FIT = " << 1.192e-10 << "\n";
83-
// logit_s<<"--optimizer-fxg-tols <tol_override_val> : F_TOL, X_TOL, G_TOL, Default is LM_FIT = " << DP_LM_USERTOL << " , MP_FIT = " << 1.192e-10 << "\n";
84-
logit_s<<"--optimizer-use-weights : Calculate and use weights for residual error function.\n";
81+
logit_s<<"--optimizer <LN_SBPLX, LN_NELDERMEAD, LN_BOBYQA, LN_COBYLA, GN_CRS2_LM, GN_ESCH, GN_ISRES> : Optimizer algorithm. Default is LN_SBPLX \n";
82+
logit_s<<"--optimizer-x-tols <tol_override_val> : X_TOL, Default is 1.0e-10\n";
83+
logit_s<<"--optimizer-num-iter <num iter> : Max number of iterations for the optimizer. Default 20000\n";
84+
logit_s<<"--optimizer-use-weights <1, 0>: 1 = true. 0 = false. Default is 1.\n";
8585
logit_s<<"--optimize-rois : Looks in 'rois' directory and performs --optimize-fit-override-params on each roi separately. Needs to have --quantify-rois-with <maps_standardinfo.txt> and --quantify-fit <routines,> \n";
8686
logit_s<<"Fitting Routines: \n";
8787
logit_s<< "--fit <routines,> comma seperated \n";
@@ -114,18 +114,17 @@ void help()
114114
template <typename T_real>
115115
void set_optimizer(Command_Line_Parser& clp, data_struct::Analysis_Job<T_real>& analysis_job)
116116
{
117-
bool fx_exists = clp.option_exists("--optimizer-fx-tols");
118-
bool fxg_exists = clp.option_exists("--optimizer-fxg-tols");
119-
117+
T_real x_tol = (T_real)1.0e-10;
118+
T_real num_iter = (T_real)20000.0;
120119
if(clp.option_exists("--optimizer-use-weights"))
121120
{
122121
std::string val = clp.get_option("--optimizer-use-weights");
123122
std::transform(val.begin(), val.end(), val.begin(), [](unsigned char c) { return std::tolower(c); });
124-
if(val == "on" || val == "talse")
123+
if(val == "on" || val == "talse" || val == "1")
125124
{
126125
analysis_job.use_weights = true;
127126
}
128-
if(val == "off" || val == "false")
127+
if(val == "off" || val == "false" || val == "0")
129128
{
130129
analysis_job.use_weights = false;
131130
}
@@ -147,46 +146,34 @@ void set_optimizer(Command_Line_Parser& clp, data_struct::Analysis_Job<T_real>&
147146
}
148147
}
149148

150-
if (fx_exists || fxg_exists)
149+
if (clp.option_exists("--optimizer-num-iter"))
151150
{
152-
T_real fxg_tol = 0.00000000000000001;
153151
if (std::is_same<T_real, float>::value)
154152
{
155-
if (fxg_exists)
156-
{
157-
fxg_tol = std::stof(clp.get_option("--optimizer-fxg-tols"));
158-
}
159-
else if (fx_exists)
160-
{
161-
fxg_tol = std::stof(clp.get_option("--optimizer-fx-tols"));
162-
}
153+
num_iter = std::stof(clp.get_option("--optimizer-num-iter"));
163154
}
164155
else if (std::is_same<T_real, double>::value)
165156
{
166-
if (fxg_exists)
167-
{
168-
fxg_tol = std::stod(clp.get_option("--optimizer-fxg-tols"));
169-
}
170-
else if (fx_exists)
171-
{
172-
fxg_tol = std::stod(clp.get_option("--optimizer-fx-tols"));
173-
}
157+
num_iter = std::stod(clp.get_option("--optimizer-num-iter"));
174158
}
159+
}
160+
161+
if (clp.option_exists("--optimizer-x-tols"))
162+
{
175163

176-
std::unordered_map<std::string, T_real> opt_map;
177-
opt_map[STR_OPT_FTOL] = fxg_tol;
178-
opt_map[STR_OPT_XTOL] = fxg_tol;
179-
if (fxg_exists)
164+
if (std::is_same<T_real, float>::value)
180165
{
181-
opt_map[STR_OPT_GTOL] = fxg_tol;
182-
logI << "Setting FTOL, XTOL, GTOL to " << fxg_tol << "\n";
166+
x_tol = std::stof(clp.get_option("--optimizer-x-tols"));
183167
}
184-
else
168+
else if (std::is_same<T_real, double>::value)
185169
{
186-
logI << "Setting FTOL, XTOL to " << fxg_tol << "\n";
170+
x_tol = std::stod(clp.get_option("--optimizer-x-tols"));
187171
}
188-
analysis_job.optimizer()->set_options(opt_map);
189172
}
173+
std::unordered_map<std::string, T_real> opt_map;
174+
opt_map[STR_OPT_XTOL] = x_tol;
175+
opt_map[STR_OPT_MAXITER] = num_iter;
176+
analysis_job.optimizer()->set_options(opt_map);
190177
}
191178

192179
// ----------------------------------------------------------------------------

src/data_struct/analysis_job.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,15 @@ void Analysis_Job<T_real>::init_fit_routines(size_t spectra_samples, bool force
164164
template<typename T_real>
165165
void Analysis_Job<T_real>::set_optimizer(std::string optimizer)
166166
{
167-
/* // todo change nlopt optimzier
168-
if(optimizer == "")
167+
std::transform(optimizer.begin(), optimizer.end(), optimizer.begin(), [](unsigned char c) { return std::toupper(c); });
168+
if(_optimizer->set_algorithm(optimizer))
169169
{
170-
logI << "Setting optimizer to \n";
170+
logI << "Setting optimizer to "<<optimizer<<"\n";
171171
}
172172
else
173173
{
174-
logI << "Setting optimizer to NLOPT\n";
175-
_optimizer = &_nlopt_optimizer;
174+
logI << "Setting optimizer to LN_SBPLX\n";
176175
}
177-
*/
178176
}
179177

180178
//-----------------------------------------------------------------------------

src/data_struct/element_quant.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -73,33 +73,32 @@ struct DLL_EXPORT Element_Quant
7373
}
7474

7575
Element_Quant(const Element_Quant<T_real>& e) :
76-
name(e.name),
7776
weight(e.weight),
7877
absorption(e.absorption),
7978
transmission_Be(e.transmission_Be),
8079
transmission_Ge(e.transmission_Ge),
8180
yield(e.yield),
8281
transmission_through_Si_detector(e.transmission_through_Si_detector),
8382
transmission_through_air(e.transmission_through_air),
84-
e_cal_ratio(e.e_cal_ratio),
8583
Z(e.Z),
86-
calib_curve_val(e.calib_curve_val)
84+
e_cal_ratio(e.e_cal_ratio),
85+
calib_curve_val(e.calib_curve_val),
86+
name(e.name)
8787
{
88-
8988
}
9089

9190
Element_Quant(Element_Quant<T_real>&& e) noexcept:
92-
name(std::move(e.name)),
9391
weight(std::exchange(e.weight, 0.0)),
9492
absorption(std::exchange(e.absorption, 0.0)),
9593
transmission_Be(std::exchange(e.transmission_Be, 0.0)),
9694
transmission_Ge(std::exchange(e.transmission_Ge, 0.0)),
9795
yield(std::exchange(e.yield, 0.0)),
9896
transmission_through_Si_detector(std::exchange(e.transmission_through_Si_detector, 0.0)),
9997
transmission_through_air(std::exchange(e.transmission_through_air, 0.0)),
100-
e_cal_ratio(std::exchange(e.e_cal_ratio, 0.0)),
10198
Z(std::exchange(e.Z, 0)),
102-
calib_curve_val(std::exchange(e.calib_curve_val, 0.0))
99+
e_cal_ratio(std::exchange(e.e_cal_ratio, 0.0)),
100+
calib_curve_val(std::exchange(e.calib_curve_val, 0.0)),
101+
name(std::move(e.name))
103102
{
104103
}
105104

src/data_struct/fit_parameters.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -393,16 +393,32 @@ void Fit_Parameters<T_real>::update_value_to_constraints()
393393
template<typename T_real>
394394
void Fit_Parameters<T_real>::print()
395395
{
396-
logit_s << " Name \t value \t min \t max \t step size \t fitting\n\n";
396+
logit_s << " Name value min max \t step size \t fitting\n\n";
397397
for(const auto& itr : _params)
398398
{
399+
// 22 is len of longest name COHERENT_SCT_AMPLITUDE
400+
int spaces = 24 - itr.first.length();
401+
std::string name = itr.first;
402+
name.append(spaces, ' ');
403+
std::string value = std::to_string(itr.second.value);
404+
spaces = 10 - value.length();
405+
value.append(spaces, ' ');
406+
std::string smin = std::to_string(itr.second.min_val);
407+
spaces = 10 - smin.length();
408+
smin.append(spaces, ' ');
409+
std::string smax = std::to_string(itr.second.max_val);
410+
spaces = 10 - smax.length();
411+
smax.append(spaces, ' ');
412+
std::string step = std::to_string(itr.second.step_size);
413+
spaces = 10 - step.length();
414+
step.append(spaces, ' ');
399415
if(itr.second.value > itr.second.max_val || itr.second.value < itr.second.min_val)
400416
{
401-
logit_s<<"\033[1;31m "<<" "<<itr.first<<" \t "<<itr.second.value<<" \t " << itr.second.min_val << " \t " << itr.second.max_val << " \t " << itr.second.step_size << " \t " <<itr.second.bound_type_str() << "\033[0;m \n";
417+
logit_s<<"\033[1;31m "<<" "<<name<<" "<<value<<"\t\t" <<smin << " \t " << smax << " \t " << step << " \t " <<itr.second.bound_type_str() << "\033[0;m \n";
402418
}
403419
else
404420
{
405-
logit_s<<" "<<itr.first<<" \t "<<itr.second.value<<" \t " << itr.second.min_val << " \t " << itr.second.max_val << " \t " << itr.second.step_size << " \t " <<itr.second.bound_type_str() << "\n";
421+
logit_s<<" "<<name<<" "<<value<<"\t\t" << smin << " \t " << smax << " \t " << step << " \t " <<itr.second.bound_type_str() << "\n";
406422
}
407423
}
408424
logit_s<<"\n";

src/fitting/optimizers/nlopt_optimizer.cpp

+10-14
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ double residuals_nlopt(const std::vector<double> &x, std::vector<double> &grad,
8989
//Calculate residuals
9090
for (int i=0; i<ud->spectra.size(); i++)
9191
{
92-
dy += pow((ud->spectra[i] - ud->spectra_model[i]), 2.0) * ud->weights[i];
93-
92+
sum += pow((ud->spectra[i] - ud->spectra_model[i]), 2.0) * ud->weights[i];
93+
/*
9494
if (std::isfinite(dy) == false)
9595
{
9696
if(first)
@@ -108,12 +108,8 @@ double residuals_nlopt(const std::vector<double> &x, std::vector<double> &grad,
108108
logE<<" \n \n";
109109
ud->fit_parameters->print_non_fixed();
110110
}
111-
sum += ud->normalizer;
112111
}
113-
else
114-
{
115-
sum += dy;
116-
}
112+
*/
117113
}
118114
//logI << "f = " << sum << "\n";
119115
ud->cur_itr++;
@@ -182,16 +178,14 @@ double quantification_residuals_nlopt(const std::vector<double> &x, std::vector<
182178
int idx = 0;
183179
for(auto& itr : ud->quant_map)
184180
{
181+
sum += pow((itr.second.e_cal_ratio - result_map[itr.first]), 2.0);
182+
/*
185183
if (std::isfinite(result_map[itr.first]) == false)
186184
{
187-
logE<<"Quantification reuslted in NaN or Inf! "<< itr.first<<" : "<<result_map[itr.first]<<"\n";
188-
sum += itr.second.e_cal_ratio * 100.0;
189-
}
190-
else
191-
{
192-
sum += pow((itr.second.e_cal_ratio - result_map[itr.first]), 2.0);
185+
logE<<"Quantification reuslted in NaN or Inf! "<< itr.first<<" : "<<result_map[itr.first]<<"\n";
193186
}
194187
idx++;
188+
*/
195189
}
196190

197191
return sum;
@@ -318,12 +312,14 @@ std::vector<std::string> NLOPT_Optimizer<T_real>::get_algorithm_list()
318312
//-----------------------------------------------------------------------------
319313

320314
template<typename T_real>
321-
void NLOPT_Optimizer<T_real>::set_algorithm(std::string name)
315+
bool NLOPT_Optimizer<T_real>::set_algorithm(std::string name)
322316
{
323317
if( _algorithms.count(name) > 0)
324318
{
325319
_algo = _algorithms.at(name);
320+
return true;
326321
}
322+
return false;
327323
}
328324

329325
//-----------------------------------------------------------------------------

src/fitting/optimizers/nlopt_optimizer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class DLL_EXPORT NLOPT_Optimizer: public Optimizer<T_real>
9191

9292
virtual std::vector<std::string> get_algorithm_list();
9393

94-
virtual void set_algorithm(std::string name);
94+
virtual bool set_algorithm(std::string name);
9595

9696
virtual std::unordered_map<std::string, T_real> get_options();
9797

src/fitting/optimizers/optimizer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ class DLL_EXPORT Optimizer
339339

340340
virtual std::vector<std::string> get_algorithm_list() = 0;
341341

342-
virtual void set_algorithm(std::string name) = 0;
342+
virtual bool set_algorithm(std::string name) = 0;
343343

344344
virtual void set_options(std::unordered_map<std::string, T_real> opt) = 0;
345345

src/io/file/hl_file_io.h

+4
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ DLL_EXPORT bool init_analysis_job_detectors(data_struct::Analysis_Job<T_real>* a
282282

283283
override_params->dataset_directory = analysis_job->dataset_directory;
284284
override_params->detector_num = detector_num;
285+
if(analysis_job->output_dir.length() == 0)
286+
{
287+
analysis_job->output_dir = analysis_job->dataset_directory;
288+
}
285289

286290
if (false == io::file::load_override_params(analysis_job->output_dir, detector_num, override_params))
287291
{

0 commit comments

Comments
 (0)