Skip to content

Commit

Permalink
Merge pull request #171 from aglowacki/master
Browse files Browse the repository at this point in the history
clean up
  • Loading branch information
aglowacki authored Nov 20, 2023
2 parents c48dee5 + 07e98a6 commit 3af107e
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 68 deletions.
8 changes: 6 additions & 2 deletions 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";
" 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";
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 Expand Up @@ -136,7 +136,7 @@ void set_optimizer(Command_Line_Parser& clp, data_struct::Analysis_Job<T_real>&

if (fx_exists || fxg_exists)
{
T_real fxg_tol;
T_real fxg_tol = 0.00000000000000001;
if (std::is_same<T_real, float>::value)
{
if (fxg_exists)
Expand Down Expand Up @@ -540,6 +540,10 @@ int run_optimization(Command_Line_Parser& clp)
{
analysis_job.optimize_fit_params_preset = fitting::models::Fit_Params_Preset::BATCH_FIT_WITH_FREE_ENERGY;
}
else if (opt == "5")
{
analysis_job.optimize_fit_params_preset = fitting::models::Fit_Params_Preset::BATCH_FIT_NO_TAILS_E_QUAD;
}
else
{
logI << "Defaulting optimize_fit_params_preset to batch fit without tails" << "\n";
Expand Down
2 changes: 2 additions & 0 deletions src/data_struct/fit_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ DLL_EXPORT Range get_energy_range(T_real min_energy, T_real max_energy, size_t s
{

struct Range energy_range;

//data_struct::ArrayTr<double> ev = energy_offset + (energy * energy_slope) + (Eigen::pow(energy, (double)2.0) * energy_quad);
energy_range.min = static_cast<size_t>(round((min_energy - energy_offset) / energy_slope));
energy_range.max = static_cast<size_t>(round((max_energy - energy_offset) / energy_slope));
//if (xmax > used_chan - 1) or (xmax <= np.amin([xmin, used_chan / 20.])):
Expand Down
2 changes: 1 addition & 1 deletion src/data_struct/params_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class DLL_EXPORT Params_Override
el_name.erase(std::remove_if(el_name.begin(), el_name.end(), ::isspace), el_name.end());


for (unsigned int i = 0; i < amt; i++)
for (int i = 0; i < amt; i++)
{
T_real factor = 1.0;
std::getline(f, s, ',');
Expand Down
2 changes: 1 addition & 1 deletion src/data_struct/quantification_standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct DLL_EXPORT Quantification_Scaler_Struct
Quantification_Scaler_Struct(unsigned int max_z= CALIBRATION_CURVE_SIZE)
{
std::vector<Element_Quant<T_real>> e_quants;
for (int i = 0; i < max_z; i++)
for (unsigned int i = 0; i < max_z; i++)
{
e_quants.emplace_back(Element_Quant<T_real>(i + 1));
}
Expand Down
20 changes: 10 additions & 10 deletions src/data_struct/spectra.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class Spectra : public ArrayTr<_T>
*/
Spectra() : ArrayTr<_T>()
{
_elapsed_livetime = default_time_and_io_counts;
_elapsed_realtime = default_time_and_io_counts;
_elapsed_livetime = (_T)default_time_and_io_counts;
_elapsed_realtime = (_T)default_time_and_io_counts;
_input_counts = 0.0;
_output_counts = 0.0;
}
Expand All @@ -91,8 +91,8 @@ class Spectra : public ArrayTr<_T>
Spectra(size_t sample_size) : ArrayTr<_T>(sample_size)
{
this->setZero();
_elapsed_livetime = default_time_and_io_counts;
_elapsed_realtime = default_time_and_io_counts;
_elapsed_livetime = (_T)default_time_and_io_counts;
_elapsed_realtime = (_T)default_time_and_io_counts;
_input_counts = 0.0;
_output_counts = 0.0;
}
Expand All @@ -108,8 +108,8 @@ class Spectra : public ArrayTr<_T>

Spectra(const ArrayTr<_T>& arr) : ArrayTr<_T>(arr)
{
_elapsed_livetime = default_time_and_io_counts;
_elapsed_realtime = default_time_and_io_counts;
_elapsed_livetime = (_T)default_time_and_io_counts;
_elapsed_realtime = (_T)default_time_and_io_counts;
_input_counts = 0.0;
_output_counts = 0.0;
}
Expand All @@ -124,8 +124,8 @@ class Spectra : public ArrayTr<_T>

Spectra(const ArrayTr<_T>&& arr) : ArrayTr<_T>(arr)
{
_elapsed_livetime = default_time_and_io_counts;
_elapsed_realtime = default_time_and_io_counts;
_elapsed_livetime = (_T)default_time_and_io_counts;
_elapsed_realtime = (_T)default_time_and_io_counts;
_input_counts = 0.0;
_output_counts = 0.0;
}
Expand All @@ -140,8 +140,8 @@ class Spectra : public ArrayTr<_T>

Spectra(Eigen::Index& rows, Eigen::Index& cols) : ArrayTr<_T>(rows, cols)
{
_elapsed_livetime = default_time_and_io_counts;
_elapsed_realtime = default_time_and_io_counts;
_elapsed_livetime = (_T)default_time_and_io_counts;
_elapsed_realtime = (_T)default_time_and_io_counts;
_input_counts = 0.0;
_output_counts = 0.0;
}
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 @@ -66,7 +66,7 @@ using namespace data_struct;
3 batch_c: batch_fit_w_tails
4 batch fit with free E, everything else fixed
*/
enum class Fit_Params_Preset { NOT_SET, MATRIX_BATCH_FIT, BATCH_FIT_NO_TAILS, BATCH_FIT_WITH_TAILS, BATCH_FIT_WITH_FREE_ENERGY };
enum class Fit_Params_Preset { NOT_SET, MATRIX_BATCH_FIT, BATCH_FIT_NO_TAILS, BATCH_FIT_WITH_TAILS, BATCH_FIT_WITH_FREE_ENERGY, BATCH_FIT_NO_TAILS_E_QUAD};

/**
* @brief The Base_Model class: base class for modeling spectra and fitting elements
Expand Down
60 changes: 49 additions & 11 deletions src/fitting/models/gaussian_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,22 @@ Fit_Parameters<T_real> Gaussian_Model<T_real>::_generate_default_fit_parameters(

fit_params.add_parameter(Fit_Param<T_real>(STR_ENERGY_OFFSET, (T_real)-0.2, (T_real)0.2, (T_real)0.0, (T_real)0.00001, E_Bound_Type::LIMITED_LO_HI));
fit_params.add_parameter(Fit_Param<T_real>(STR_ENERGY_SLOPE, (T_real)0.001, (T_real)10.0, (T_real)1.0, (T_real)0.00001, E_Bound_Type::LIMITED_LO_HI));
fit_params.add_parameter(Fit_Param<T_real>(STR_ENERGY_QUADRATIC, (T_real)-0.000000001, (T_real)0.00001, (T_real)0.0, (T_real)0.000000001, E_Bound_Type::LIMITED_LO_HI));
fit_params.add_parameter(Fit_Param<T_real>(STR_ENERGY_QUADRATIC, (T_real)-0.000000001, (T_real)0.00001, (T_real)0.0, (T_real)0.000000001, E_Bound_Type::FIXED));

fit_params.add_parameter(Fit_Param<T_real>(STR_FWHM_OFFSET, (T_real)0.005, (T_real)0.5, (T_real)0.12, (T_real)0.00001, E_Bound_Type::LIMITED_LO_HI));
fit_params.add_parameter(Fit_Param<T_real>(STR_FWHM_FANOPRIME, (T_real)0.000001, (T_real)0.05, (T_real)0.00012, (T_real)0.000001, E_Bound_Type::LIMITED_LO_HI));

fit_params.add_parameter(Fit_Param<T_real>(STR_COHERENT_SCT_ENERGY, (T_real)9.4, (T_real)10.4, (T_real)9.99, (T_real)0.001, E_Bound_Type::LIMITED_LO_HI));
fit_params.add_parameter(Fit_Param<T_real>(STR_COHERENT_SCT_AMPLITUDE, (T_real)0.000001, (T_real)10.0, (T_real)5.0, (T_real)0.00001, E_Bound_Type::FIT));

fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_ANGLE, (T_real)-0.0001, (T_real)0.0001, (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)-0.0001, (T_real)0.0001, (T_real)1.0, (T_real)0.1, E_Bound_Type::LIMITED_LO_HI));
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_F_STEP, (T_real)-0.0001, (T_real)0.0001, (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.0001, (T_real)0.4, (T_real)0.1, (T_real)0.1, E_Bound_Type::LIMITED_LO_HI));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_GAMMA, (T_real)-0.0001, (T_real)10., (T_real)1.0, (T_real)0.1, E_Bound_Type::FIXED));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_HI_F_TAIL, (T_real)-0.0001, (T_real)0.1, (T_real)0.013, (T_real)0.0000001, E_Bound_Type::LIMITED_LO_HI));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_HI_GAMMA, (T_real)-0.0001, (T_real)10., (T_real)1.0, (T_real)0.01, E_Bound_Type::FIXED));
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_HI));
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));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_HI_F_TAIL, (T_real)0.000001, (T_real)1.0, (T_real)0.013, (T_real)0.0000001, E_Bound_Type::LIMITED_LO_HI));
fit_params.add_parameter(Fit_Param<T_real>(STR_COMPTON_HI_GAMMA, (T_real)0.1, (T_real)3., (T_real)1.0, (T_real)0.01, E_Bound_Type::FIXED));

fit_params.add_parameter(Fit_Param<T_real>(STR_SNIP_WIDTH, (T_real)0.1, (T_real)2.828427, (T_real)0.25, (T_real)0.01, E_Bound_Type::FIXED)); //max = 2* sqrt(2)

Expand Down Expand Up @@ -188,7 +188,7 @@ void Gaussian_Model<T_real>::set_fit_params_preset(Fit_Params_Preset preset)
case Fit_Params_Preset::BATCH_FIT_NO_TAILS: // batch fit without tails
_fit_parameters[STR_ENERGY_OFFSET].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_SLOPE].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_QUADRATIC].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_QUADRATIC].bound_type = E_Bound_Type::FIXED;

_fit_parameters[STR_FWHM_OFFSET].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_FWHM_FANOPRIME].bound_type = E_Bound_Type::LIMITED_LO_HI;
Expand Down Expand Up @@ -226,7 +226,7 @@ void Gaussian_Model<T_real>::set_fit_params_preset(Fit_Params_Preset preset)
case Fit_Params_Preset::BATCH_FIT_WITH_TAILS: //batch fit with tails
_fit_parameters[STR_ENERGY_OFFSET].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_SLOPE].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_QUADRATIC].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_QUADRATIC].bound_type = E_Bound_Type::FIXED;

_fit_parameters[STR_FWHM_OFFSET].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_FWHM_FANOPRIME].bound_type = E_Bound_Type::LIMITED_LO_HI;
Expand Down Expand Up @@ -264,7 +264,7 @@ void Gaussian_Model<T_real>::set_fit_params_preset(Fit_Params_Preset preset)
case Fit_Params_Preset::BATCH_FIT_WITH_FREE_ENERGY: // batch fit with free E, everything else fixed
_fit_parameters[STR_ENERGY_OFFSET].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_SLOPE].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_QUADRATIC].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_QUADRATIC].bound_type = E_Bound_Type::FIXED;

_fit_parameters[STR_FWHM_OFFSET].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_FWHM_FANOPRIME].bound_type = E_Bound_Type::FIXED;
Expand Down Expand Up @@ -295,6 +295,44 @@ void Gaussian_Model<T_real>::set_fit_params_preset(Fit_Params_Preset preset)
_fit_parameters[STR_GAMMA_LINEAR].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_GAMMA_QUADRATIC].bound_type = E_Bound_Type::FIXED;

_fit_parameters[STR_KB_F_TAIL_OFFSET].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_KB_F_TAIL_LINEAR].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_KB_F_TAIL_QUADRATIC].bound_type = E_Bound_Type::FIXED;
break;
case Fit_Params_Preset::BATCH_FIT_NO_TAILS_E_QUAD:
_fit_parameters[STR_ENERGY_OFFSET].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_SLOPE].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_ENERGY_QUADRATIC].bound_type = E_Bound_Type::LIMITED_LO_HI;

_fit_parameters[STR_FWHM_OFFSET].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_FWHM_FANOPRIME].bound_type = E_Bound_Type::LIMITED_LO_HI;

_fit_parameters[STR_COHERENT_SCT_ENERGY].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_COHERENT_SCT_AMPLITUDE].bound_type = E_Bound_Type::FIT;

_fit_parameters[STR_COMPTON_ANGLE].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_COMPTON_FWHM_CORR].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_COMPTON_AMPLITUDE].bound_type = E_Bound_Type::FIT;
_fit_parameters[STR_COMPTON_F_STEP].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_COMPTON_F_TAIL].bound_type = E_Bound_Type::LIMITED_LO;
_fit_parameters[STR_COMPTON_GAMMA].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_COMPTON_HI_F_TAIL].bound_type = E_Bound_Type::LIMITED_LO_HI;
_fit_parameters[STR_COMPTON_HI_GAMMA].bound_type = E_Bound_Type::FIXED;

_fit_parameters[STR_SNIP_WIDTH].bound_type = E_Bound_Type::FIXED;

_fit_parameters[STR_F_STEP_OFFSET].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_F_STEP_LINEAR].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_F_STEP_QUADRATIC].bound_type = E_Bound_Type::FIXED;

_fit_parameters[STR_F_TAIL_OFFSET].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_F_TAIL_LINEAR].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_F_TAIL_QUADRATIC].bound_type = E_Bound_Type::FIXED;

_fit_parameters[STR_GAMMA_OFFSET].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_GAMMA_LINEAR].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_GAMMA_QUADRATIC].bound_type = E_Bound_Type::FIXED;

_fit_parameters[STR_KB_F_TAIL_OFFSET].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_KB_F_TAIL_LINEAR].bound_type = E_Bound_Type::FIXED;
_fit_parameters[STR_KB_F_TAIL_QUADRATIC].bound_type = E_Bound_Type::FIXED;
Expand Down
10 changes: 9 additions & 1 deletion src/fitting/optimizers/lmfit_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ void residuals_lmfit( const T_real *par, int m_dat, const void *data, T_real *fv
ud->cur_itr++;
if (ud->status_callback != nullptr)
{
(*ud->status_callback)(ud->cur_itr, ud->total_itr);
try
{
(*ud->status_callback)(ud->cur_itr, ud->total_itr);
}
catch (int e)
{
logI << "Cancel fitting" << std::endl;
*userbreak = 1;
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/fitting/optimizers/mpfit_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ int residuals_mpfit(int m, int params_size, T_real *params, T_real *dy, T_real *
ud->cur_itr++;
if (ud->status_callback != nullptr)
{
(*ud->status_callback)(ud->cur_itr, ud->total_itr);
try
{
(*ud->status_callback)(ud->cur_itr, ud->total_itr);
}
catch (int e)
{
logI << "Cancel fitting" << std::endl;
return -1;
}
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/fitting/optimizers/optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ void fill_user_data(User_Data<T_real> &ud,
weights = convolve1d(weights, 5);
weights = Eigen::abs(weights);
weights /= weights.maxCoeff();
*/
ud.weights = weights.segment(energy_range.min, energy_range.count());
*/
}
else
{
Expand Down
Loading

0 comments on commit 3af107e

Please sign in to comment.