Skip to content

Commit

Permalink
Merge pull request #173 from aglowacki/master
Browse files Browse the repository at this point in the history
General bug fixes
  • Loading branch information
aglowacki authored Dec 1, 2023
2 parents 5594d4a + 91c93e3 commit 473405b
Show file tree
Hide file tree
Showing 28 changed files with 356 additions and 193 deletions.
4 changes: 2 additions & 2 deletions src/core/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ POSSIBILITY OF SUCH DAMAGE.
static std::time_t now_c;
#define logit now_c=std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());std::cout<<std::put_time(std::localtime(&now_c),"[%F_%T]\t")<<__FILE__<<"::"<<__FUNCTION__<<"():"<<__LINE__<<"\t"
#define logit_s std::cout
#define logE logit<<"Error: "
#define logW logit<<"Warning: "
#define logE logit<<"\033[1;31mError: \033[0;m"
#define logW logit<<"\033[1;33mWarning: \033[0;m"
#define logI logit<<"Info: "

#if defined _WIN32 || defined __CYGWIN__
Expand Down
2 changes: 1 addition & 1 deletion src/core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void help()
logit_s<<"--quick-and-dirty : Integrate the detector range into 1 spectra.\n";
// logit_s<< "--mem-limit <limit> : Limit the memory usage. Append M for megabytes or G for gigabytes\n";
logit_s<<"--optimize-fit-override-params : <int> Integrate the 8 largest mda datasets and fit with multiple params.\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";
" 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";
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";
logit_s<<"--optimizer <lmfit, mpfit> : Choose which optimizer to use for --optimize-fit-override-params or matrix fit routine \n";
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";
Expand Down
2 changes: 1 addition & 1 deletion src/data_struct/detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void Detector<T_real>::update_element_quants(Fitting_Routines routine,
}
else
{
eq_itr.e_cal_ratio += 1.0e-10;
eq_itr.e_cal_ratio += (T_real)1.0e-10;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fitting/models/base_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class DLL_EXPORT Base_Model
/**
* @brief ~Base_Model : Destructor
*/
~Base_Model() {}
virtual ~Base_Model() {}

/**
* @brief fit_parameters : returns Fit_Parameters class of the required fit parameters to run a fitting
Expand Down
2 changes: 1 addition & 1 deletion src/fitting/models/gaussian_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Fit_Parameters<T_real> Gaussian_Model<T_real>::_generate_default_fit_parameters(

fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_ANGLE, (T_real)0.0, (T_real)359.0, (T_real)90.0, (T_real)0.1, E_Bound_Type::LIMITED_LO_HI));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_FWHM_CORR, (T_real)1.0, (T_real)4.0, (T_real)1.0, (T_real)0.1, E_Bound_Type::LIMITED_LO_HI));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_AMPLITUDE, (T_real)-0.0001, (T_real)0.0001, (T_real)5.0, (T_real)0.00001, E_Bound_Type::FIT));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_AMPLITUDE, (T_real)0.00001, (T_real)10.00, (T_real)5.0, (T_real)0.00001, E_Bound_Type::FIT));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_F_STEP, (T_real)0.0, (T_real)1.0, (T_real)0.0, (T_real)0.1, E_Bound_Type::FIXED));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_F_TAIL, (T_real)0.0, (T_real)1.0, (T_real)0.1, (T_real)0.1, E_Bound_Type::LIMITED_LO));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_GAMMA, (T_real)0.1, (T_real)10., (T_real)1.0, (T_real)0.1, E_Bound_Type::FIXED));
Expand Down
2 changes: 1 addition & 1 deletion src/fitting/models/gaussian_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DLL_EXPORT Gaussian_Model: public Base_Model<T_real>
public:
Gaussian_Model();

~Gaussian_Model();
virtual ~Gaussian_Model();

virtual const Fit_Parameters<T_real>& fit_parameters() const { return _fit_parameters; }

Expand Down
39 changes: 32 additions & 7 deletions src/fitting/optimizers/lmfit_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void residuals_lmfit( const T_real *par, int m_dat, const void *data, T_real *fv
{
(*ud->status_callback)(ud->cur_itr, ud->total_itr);
}
catch (int e)
catch (...)
{
logI << "Cancel fitting" << std::endl;
*userbreak = 1;
Expand Down Expand Up @@ -175,6 +175,7 @@ void quantification_residuals_lmfit( const T_real *par, int m_dat, const void *d
template<typename T_real>
LMFit_Optimizer<T_real>::LMFit_Optimizer() : Optimizer<T_real>()
{
this->_last_outcome = -1;

if (std::is_same<T_real, float>::value)
{
Expand Down Expand Up @@ -282,6 +283,10 @@ OPTIMIZER_OUTCOME LMFit_Optimizer<T_real>::minimize(Fit_Parameters<T_real> *fit_

User_Data<T_real> ud;
std::vector<T_real> fitp_arr = fit_params->to_array();
if (fitp_arr.size() == 0)
{
return OPTIMIZER_OUTCOME::STOPPED;
}
std::vector<T_real> perror(fitp_arr.size());

size_t total_itr = _options.patience * (fitp_arr.size() + 1);
Expand Down Expand Up @@ -330,9 +335,9 @@ OPTIMIZER_OUTCOME LMFit_Optimizer<T_real>::minimize(Fit_Parameters<T_real> *fit_
//control.verbosity = 3;

/* perform the fit */
lmmin( fitp_arr.size(), &fitp_arr[0], energy_range.count(), (const void*) &ud, residuals_lmfit, &_options, &status );
lmmin( (int)fitp_arr.size(), &fitp_arr[0], (int)energy_range.count(), (const void*) &ud, residuals_lmfit, &_options, &status );
logI<< "Outcome: "<<lm_infmsg[status.outcome]<<"\nNum iter: "<<status.nfev<<"\n Norm of the residue vector: "<<status.fnorm<<"\n";

this->_last_outcome = status.outcome;
fit_params->from_array(fitp_arr);

if (fit_params->contains(STR_NUM_ITR) )
Expand Down Expand Up @@ -386,12 +391,16 @@ OPTIMIZER_OUTCOME LMFit_Optimizer<T_real>::minimize_func(Fit_Parameters<T_real>*
fill_gen_user_data(ud, fit_params, spectra, energy_range, background, gen_func, use_weights);

std::vector<T_real> fitp_arr = fit_params->to_array();
if (fitp_arr.size() == 0)
{
return OPTIMIZER_OUTCOME::STOPPED;
}
std::vector<T_real> perror(fitp_arr.size());

lm_status_struct<T_real> status;

lmmin( fitp_arr.size(), &fitp_arr[0], energy_range.count(), (const void*) &ud, general_residuals_lmfit, &_options, &status );

lmmin((int)fitp_arr.size(), &fitp_arr[0], (int)energy_range.count(), (const void*) &ud, general_residuals_lmfit, &_options, &status );
this->_last_outcome = status.outcome;
fit_params->from_array(fitp_arr);

if (fit_params->contains(STR_NUM_ITR) )
Expand Down Expand Up @@ -437,12 +446,16 @@ OPTIMIZER_OUTCOME LMFit_Optimizer<T_real>::minimize_quantification(Fit_Parameter
ud.fit_parameters = fit_params;

std::vector<T_real> fitp_arr = fit_params->to_array();
if (fitp_arr.size() == 0)
{
return OPTIMIZER_OUTCOME::STOPPED;
}
std::vector<T_real> perror(fitp_arr.size());

lm_status_struct<T_real> status;
lmmin( fitp_arr.size(), &fitp_arr[0], quant_map->size(), (const void*) &ud, quantification_residuals_lmfit, &_options, &status );
lmmin((int)fitp_arr.size(), &fitp_arr[0], (int)ud.quant_map.size(), (const void*) &ud, quantification_residuals_lmfit, &_options, &status );
logI << "\nOutcome: " << lm_infmsg[status.outcome] << "\nNum iter: " << status.nfev << "\nNorm of the residue vector: " << status.fnorm << "\n";

this->_last_outcome = status.outcome;
fit_params->from_array(fitp_arr);

if (fit_params->contains(STR_NUM_ITR))
Expand Down Expand Up @@ -481,6 +494,18 @@ OPTIMIZER_OUTCOME LMFit_Optimizer<T_real>::minimize_quantification(Fit_Parameter

// ----------------------------------------------------------------------------

template<typename T_real>
std::string LMFit_Optimizer<T_real>::detailed_outcome(int outcome)
{
if (outcome > -1 && outcome < 13)
{
return lm_infmsg[outcome];
}
return "Unknown";
}

// ----------------------------------------------------------------------------

TEMPLATE_CLASS_DLL_EXPORT LMFit_Optimizer<float>;
TEMPLATE_CLASS_DLL_EXPORT LMFit_Optimizer<double>;

Expand Down
4 changes: 3 additions & 1 deletion src/fitting/optimizers/lmfit_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class DLL_EXPORT LMFit_Optimizer: public Optimizer<T_real>
public:
LMFit_Optimizer();

~LMFit_Optimizer() {}
virtual ~LMFit_Optimizer() {}

virtual OPTIMIZER_OUTCOME minimize(Fit_Parameters<T_real>*fit_params,
const Spectra<T_real>* const spectra,
Expand All @@ -94,6 +94,8 @@ class DLL_EXPORT LMFit_Optimizer: public Optimizer<T_real>

virtual void set_options(std::unordered_map<std::string, T_real> opt);

virtual std::string detailed_outcome(int outcome);

private:

struct lm_control_struct<T_real> _options;
Expand Down
Loading

0 comments on commit 473405b

Please sign in to comment.