Skip to content

Commit

Permalink
Removed mpfit, replaced with nlopt
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Glowacki committed Oct 3, 2024
1 parent 8e766fa commit 15cd7d5
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 182 deletions.
39 changes: 4 additions & 35 deletions src/mvc/BatchRoiFitWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ void BatchRoiFitWidget::createLayout()
connect(_btn_cancel, &QPushButton::released, this, &BatchRoiFitWidget::onClose);

_le_detectors = new QLineEdit("0,1,2,3,4,5,6");

_cb_opt_method = new QComboBox();
_cb_opt_method->addItem(STR_NL_FIT);
_cb_opt_method->addItem(STR_HYBRID_MP_FIT);
_cb_opt_method->addItem(STR_MP_FIT);
connect(_cb_opt_method, &QComboBox::currentTextChanged, this, &BatchRoiFitWidget::optimizer_changed);

_file_list_model = new QStandardItemModel();
_file_list_view = new QListView();
Expand All @@ -81,11 +75,10 @@ void BatchRoiFitWidget::createLayout()

_optimizer_widget = new OptimizerOptionsWidget();
fitting::optimizers::NLOPT_Optimizer<double> optimizer;
_optimizer_widget->setOptimizer(STR_NL_FIT, optimizer);
_optimizer_widget->setOptimizer(optimizer);

QVBoxLayout* layout = new QVBoxLayout();
layout->addItem(detector_hbox);
layout->addWidget(_cb_opt_method);
layout->addWidget(_optimizer_widget);
layout->addWidget(_file_list_view);
layout->addItem(buttonlayout);
Expand All @@ -97,20 +90,6 @@ void BatchRoiFitWidget::createLayout()

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

void BatchRoiFitWidget::optimizer_changed(QString val)
{
if (val == STR_NL_FIT)
{
fitting::optimizers::NLOPT_Optimizer<double> optimizer;
_optimizer_widget->setOptimizer(val, optimizer);
}
else if (val == STR_MP_FIT || val == STR_HYBRID_MP_FIT)
{
fitting::optimizers::MPFit_Optimizer<double> mpfit_optimizer;
_optimizer_widget->setOptimizer(val, mpfit_optimizer);
}
}

void BatchRoiFitWidget::onClose()
{
_canceled = true;
Expand Down Expand Up @@ -189,27 +168,17 @@ void BatchRoiFitWidget::runProcessing()
QCoreApplication::processEvents();
data_struct::Analysis_Job<double> analysis_job;

QString val = _cb_opt_method->currentText();
analysis_job.set_optimizer(val.toStdString());

//analysis_job.set_optimizer_algorithm();

//run in thread
analysis_job.dataset_directory = _directory;
if (_cb_opt_method->currentText() == STR_HYBRID_MP_FIT)
if (_optimizer_widget->isHybrid())
{
analysis_job.set_optimizer("nlopt");
analysis_job.optimize_fit_routine = OPTIMIZE_FIT_ROUTINE::HYBRID;
}
else
{
if (_cb_opt_method->currentText() == STR_MP_FIT)
{
analysis_job.set_optimizer("mpfit");
}
else
{
analysis_job.set_optimizer("nlopt");
}
analysis_job.optimize_fit_routine = OPTIMIZE_FIT_ROUTINE::ALL_PARAMS;
}

_optimizer_widget->updateOptimizerOptions(*(analysis_job.optimizer()));
Expand Down
4 changes: 0 additions & 4 deletions src/mvc/BatchRoiFitWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ class BatchRoiFitWidget : public QWidget

public slots:
void runProcessing();

void optimizer_changed(QString val);

void onClose();

Expand Down Expand Up @@ -86,8 +84,6 @@ public slots:

QLineEdit* _le_detectors;

QComboBox* _cb_opt_method;

std::unordered_map<QString, QFileInfo> _roi_map;

size_t _total_itr;
Expand Down
2 changes: 1 addition & 1 deletion src/mvc/FitElementsTableModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FitElementsTableModel

~FitElementsTableModel();

void setDisplayHeaderMinMax(bool val);
//void setDisplayHeaderMinMax(bool val);

data_struct::Fit_Element_Map_Dict<double> getElementsToFit();

Expand Down
15 changes: 2 additions & 13 deletions src/mvc/FitSpectraWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,6 @@ void FitSpectraWidget::createLayout()
_cb_fitting_preset->addItem("Fit No Tails Energy Quadratic");
connect(_cb_fitting_preset, &QComboBox::currentIndexChanged, this, &FitSpectraWidget::optimizer_preset_changed);


_cb_opttimizer = new QComboBox();
_cb_opttimizer->addItem(STR_NL_FIT);
_cb_opttimizer->addItem(STR_HYBRID_MP_FIT);
_cb_opttimizer->addItem(STR_MP_FIT);
// connect(_cb_opttimizer, &QComboBox::currentTextChanged, this, &FitSpectraWidget::optimizer_changed);

_chk_auto_model = new QCheckBox("Auto Update Model");
_chk_auto_model->setChecked(false);
connect(_chk_auto_model,
Expand All @@ -272,7 +265,6 @@ void FitSpectraWidget::createLayout()
orient_layout = new QHBoxLayout();
//splitter->setOrientation(Qt::Horizontal);
grid_layout->addWidget(_cb_fitting_preset, 0, 0);
grid_layout->addWidget(_cb_opttimizer, 0, 1);

grid_layout->addWidget(_btn_fit_spectra, 1, 0);
grid_layout->addWidget(_btn_fit_roi_spectra, 1, 1);
Expand All @@ -288,7 +280,6 @@ void FitSpectraWidget::createLayout()
orient_layout = new QVBoxLayout();
//splitter->setOrientation(Qt::Vertical);
grid_layout->addWidget(_cb_fitting_preset, 0, 0);
grid_layout->addWidget(_cb_opttimizer, 0, 1);
grid_layout->addWidget(_chk_auto_model, 0, 2);
grid_layout->addWidget(_btn_export_csv, 0, 3);

Expand Down Expand Up @@ -317,8 +308,6 @@ void FitSpectraWidget::createLayout()
_cb_fitting_preset->setCurrentIndex(3);
//optimizer_preset_changed(3); // batch with tails

_cb_opttimizer->setCurrentText(STR_NL_FIT);

QLayout* layout = new QVBoxLayout();
// layout->addWidget(splitter);
layout->addItem(orient_layout);
Expand Down Expand Up @@ -903,7 +892,7 @@ void FitSpectraWidget::Fit_Spectra_Click()
_fitting_dialog = new FittingDialog();
}
_fitting_dialog->updateFitParams(out_fit_params, element_fit_params);
_fitting_dialog->setOptimizer(_cb_opttimizer->currentText());
// _fitting_dialog->setOptimizer(_cb_opttimizer->currentText());
_fitting_dialog->setSpectra((Spectra<double>*)_int_spec, _ev);
_fitting_dialog->setElementsToFit(_elements_to_fit);
if (_fit_spec.size() > 0)
Expand Down Expand Up @@ -1054,7 +1043,7 @@ void FitSpectraWidget::Fit_ROI_Spectra_Click()
_fitting_dialog = new FittingDialog();
}
_fitting_dialog->updateFitParams(out_fit_params, element_fit_params);
_fitting_dialog->setOptimizer(_cb_opttimizer->currentText());
//_fitting_dialog->setOptimizer(_cb_opttimizer->currentText());
_fitting_dialog->setSpectra((Spectra<double>*)roi_spec, _ev);
_fitting_dialog->setElementsToFit(_elements_to_fit);
if (_fit_spec.size() > 0)
Expand Down
4 changes: 0 additions & 4 deletions src/mvc/FitSpectraWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ public slots:

void check_auto_model(int state);

//void optimizer_changed(QString val);

void optimizer_preset_changed(int idx);

// void element_clicked(QModelIndex index);
Expand Down Expand Up @@ -193,8 +191,6 @@ private slots:

QCheckBox *_chk_auto_model;

QComboBox *_cb_opttimizer;

QComboBox *_cb_fitting_preset;

QTabWidget *_fit_params_tab_widget;
Expand Down
52 changes: 21 additions & 31 deletions src/mvc/FittingDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,25 @@ FittingDialog::FittingDialog(QWidget *parent) : QDialog(parent)
_accepted = false;
_canceled = false;
_running = false;
_is_hybrid_fit = false;
_elements_to_fit = nullptr;
_param_fit_routine.set_update_coherent_amplitude_on_fit(false);
_param_fit_routine.set_optimizer(&_nlopt_optimizer);
_hybrid_fit_routine.set_update_coherent_amplitude_on_fit(false);
_hybrid_fit_routine.set_optimizer(&_nlopt_optimizer);

/*
void FittingDialog::setOptimizer(QString opt)
{
_param_fit_routine.set_optimizer(_optimizer);
_is_hybrid_fit = true;
_hybrid_fit_routine.set_optimizer(_optimizer);
}
_optimizer_widget->setOptimizer(opt, _nlopt_optimizer);
}
*/

////_model.set_num_threads(std::thread::hardware_concurrency());
_createLayout();
}
Expand Down Expand Up @@ -143,6 +158,7 @@ void FittingDialog::_createLayout()
hbox_progresss_blocks->addWidget(_progressBarBlocks);

_optimizer_widget = new OptimizerOptionsWidget();
_optimizer_widget->setOptimizer(_nlopt_optimizer);

QVBoxLayout* layout = new QVBoxLayout();

Expand Down Expand Up @@ -211,31 +227,6 @@ void FittingDialog::updateFitParams(data_struct::Fit_Parameters<double> out_fit_
_btn_accept->setEnabled(false);
}

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

void FittingDialog::setOptimizer(QString opt)
{
if (opt == STR_NL_FIT)
{
_optimizer = &_nlfit_optimizer;
_param_fit_routine.set_optimizer(_optimizer);
}
else if (opt == STR_MP_FIT)
{
_optimizer = &_mpfit_optimizer;
_param_fit_routine.set_optimizer(_optimizer);
_is_hybrid_fit = false;
}
else if (opt == STR_HYBRID_MP_FIT)
{
_optimizer = &_nlfit_optimizer;
_is_hybrid_fit = true;
_hybrid_fit_routine.set_optimizer(_optimizer);
}
_optimizer_widget->setOptimizer(opt, *_optimizer);
}


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

void FittingDialog::setSpectra(data_struct::Spectra<double>* spectra, ArrayDr energy)
Expand Down Expand Up @@ -366,8 +357,7 @@ void FittingDialog::runProcessing()

bool use_weights = _optimizer_widget->useWeights();

_optimizer_widget->updateOptimizerOptions(*_optimizer);

_optimizer_widget->updateOptimizerOptions(_nlopt_optimizer);
//data_struct::Spectra s1 = _integrated_spectra.sub_spectra(energy_range);

//Fitting routines
Expand All @@ -384,7 +374,7 @@ void FittingDialog::runProcessing()
//_model.set_fit_params_preset(fitting::models::Fit_Params_Preset::BATCH_FIT_WITH_TAILS);

//Initialize the fit routine
if (_is_hybrid_fit)
if (_optimizer_widget->isHybrid())
{
_hybrid_fit_routine.initialize(&_model, _elements_to_fit, _energy_range);
}
Expand All @@ -400,7 +390,7 @@ void FittingDialog::runProcessing()
Callback_Func_Status_Def cb_func = std::bind(&FittingDialog::status_callback, this, std::placeholders::_1, std::placeholders::_2);
try
{
if (_is_hybrid_fit)
if (_optimizer_widget->isHybrid())
{
_outcome = _hybrid_fit_routine.fit_spectra_parameters(&_model, &_int_spec, _elements_to_fit, use_weights, _new_out_fit_params, &cb_func);
}
Expand All @@ -412,7 +402,7 @@ void FittingDialog::runProcessing()
std::string out_str = optimizer_outcome_to_str(_outcome);
_le_outcome->setText(QString(out_str.c_str()));

std::string det_out = _optimizer->get_last_detailed_outcome();
std::string det_out = _nlopt_optimizer.get_last_detailed_outcome();
_le_detailed_outcome->setText(QString(det_out.c_str()));

if (_new_out_fit_params.contains(STR_RESIDUAL))
Expand Down
11 changes: 1 addition & 10 deletions src/mvc/FittingDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "mvc/FitParamsTableModel.h"
#include "mvc/SpectraWidget.h"
#include "fitting//optimizers/nlopt_optimizer.h"
#include "fitting//optimizers/mpfit_optimizer.h"
#include "fitting/routines/param_optimized_fit_routine.h"
#include "fitting/routines/hybrid_param_nnls_fit_routine.h"
#include "fitting/models/gaussian_model.h"
Expand Down Expand Up @@ -65,8 +64,6 @@ class FittingDialog : public QDialog

void status_callback(size_t cur_block, size_t total_blocks);

void setOptimizer(QString opt);

bool accepted_fit() { return _accepted; }

void setSpectra(data_struct::Spectra<double>* spectra, ArrayDr energy);
Expand Down Expand Up @@ -151,11 +148,7 @@ public slots:

OptimizerOptionsWidget* _optimizer_widget;

fitting::optimizers::NLOPT_Optimizer<double> _nlfit_optimizer;

fitting::optimizers::MPFit_Optimizer<double> _mpfit_optimizer;

fitting::optimizers::Optimizer<double>*_optimizer;
fitting::optimizers::NLOPT_Optimizer<double> _nlopt_optimizer;

std::unordered_map<std::string, ArrayDr> _labeled_spectras;

Expand Down Expand Up @@ -185,8 +178,6 @@ public slots:

ArrayDr _spectra_background;

bool _is_hybrid_fit;

// energy vector
ArrayDr _ev;

Expand Down
1 change: 0 additions & 1 deletion src/mvc/MapsH5Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "core/defines.h"
#include "io/file/hdf5_io.h"
#include "fitting/routines/param_optimized_fit_routine.h"
#include "fitting/optimizers/mpfit_optimizer.h"
#include "fitting/models/gaussian_model.h"
#include "data_struct/stream_block.h"

Expand Down
Loading

0 comments on commit 15cd7d5

Please sign in to comment.