Skip to content

Commit

Permalink
Merge pull request #92 from aglowacki/master
Browse files Browse the repository at this point in the history
Replace mpfit with nlopt
  • Loading branch information
aglowacki authored Oct 4, 2024
2 parents 6ac77ea + 15cd7d5 commit 9e09550
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 258 deletions.
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ find_package(Threads)
find_package(hdf5 CONFIG REQUIRED)
find_package(netCDF CONFIG REQUIRED)
find_package(yaml-cpp CONFIG REQUIRED)
find_package(ZeroMQ CONFIG REQUIRED)
find_package(cppzmq CONFIG REQUIRED)
find_package(TIFF REQUIRED)
find_package(XRF_Maps CONFIG REQUIRED COMPONENTS libxrf_io libxrf_fit)
find_package(OpenCV CONFIG REQUIRED)
find_package(NLopt CONFIG REQUIRED)

if (libxrf_fit_FOUND)
message("Found libxrf_fit")
Expand Down Expand Up @@ -210,7 +211,7 @@ endforeach()
#qt_use_modules(uProbeX Core Widgets Gui)
IF (MSVC)
set_target_properties(uProbeX PROPERTIES COMPILE_FLAGS "/D_WINSOCKAPI_")
set_target_properties(uProbeX PROPERTIES COMPILE_FLAGS_RELEASE "/O3 /Qvec-report:2 /GL /MP /bigobj /Qansi-alias")
set_target_properties(uProbeX PROPERTIES COMPILE_FLAGS_RELEASE "/O2 /Qvec-report:2 /GL /MP /bigobj /Qansi-alias")
set_target_properties(uProbeX PROPERTIES LINK_FLAGS "/LTCG")
target_link_libraries (uProbeX PRIVATE
yaml-cpp::yaml-cpp
Expand All @@ -228,6 +229,8 @@ IF (MSVC)
opencv_flann
netCDF::netcdf
hdf5::hdf5-shared
cppzmq cppzmq-static
NLopt::nlopt
libxrf_fit
libxrf_io
${CMAKE_THREAD_LIBS_INIT}
Expand All @@ -236,7 +239,7 @@ ELSEIF (UNIX)
#IF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# set_target_properties(uProbeX PROPERTIES MACOSX_BUNDLE TRUE)
#ENDIF()
set_target_properties(uProbeX PROPERTIES COMPILE_FLAGS "-O3")
set_target_properties(uProbeX PROPERTIES COMPILE_FLAGS "-O2")

target_link_libraries (uProbeX PRIVATE
libzmq
Expand All @@ -250,6 +253,8 @@ ELSEIF (UNIX)
opencv_dnn
opencv_flann
netCDF::netcdf
cppzmq cppzmq-static
NLopt::nlopt
libxrf_fit
libxrf_io
${CMAKE_THREAD_LIBS_INIT} )
Expand Down
41 changes: 5 additions & 36 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_MP_FIT);
_cb_opt_method->addItem(STR_HYBRID_MP_FIT);
_cb_opt_method->addItem(STR_LM_FIT);
connect(_cb_opt_method, &QComboBox::currentTextChanged, this, &BatchRoiFitWidget::optimizer_changed);

_file_list_model = new QStandardItemModel();
_file_list_view = new QListView();
Expand All @@ -80,12 +74,11 @@ void BatchRoiFitWidget::createLayout()
detector_hbox->addWidget(_le_detectors);

_optimizer_widget = new OptimizerOptionsWidget();
fitting::optimizers::LMFit_Optimizer<double> lmfit_optimizer;
_optimizer_widget->setOptimizer(STR_LM_FIT, lmfit_optimizer);
fitting::optimizers::NLOPT_Optimizer<double> 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_LM_FIT)
{
fitting::optimizers::LMFit_Optimizer<double> lmfit_optimizer;
_optimizer_widget->setOptimizer(val, lmfit_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("mpfit");
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("lmfit");
}
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/BlueskyComm.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include "support/zmq/zmq.hpp"
#include <zmq.hpp>
#include "mvc/BlueskyPlan.h"
#include <string>
//---------------------------------------------------------------------------
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
4 changes: 2 additions & 2 deletions src/mvc/FitParamsTableModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ QVariant FitParamsTableModel::data(const QModelIndex &index, int role) const
{
if (fitp.bound_type == data_struct::E_Bound_Type::LIMITED_LO || fitp.bound_type == data_struct::E_Bound_Type::LIMITED_LO_HI)
{
if (fitp.value <= fitp.min_val)
if (fitp.value < fitp.min_val)
{
return QColor(Qt::red);
}
Expand All @@ -199,7 +199,7 @@ QVariant FitParamsTableModel::data(const QModelIndex &index, int role) const
{
if (fitp.bound_type == data_struct::E_Bound_Type::LIMITED_HI || fitp.bound_type == data_struct::E_Bound_Type::LIMITED_LO_HI)
{
if (fitp.value >= fitp.max_val)
if (fitp.value > fitp.max_val)
{
return QColor(Qt::red);
}
Expand Down
83 changes: 4 additions & 79 deletions src/mvc/FitSpectraWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "data_struct/element_info.h"
#include "io/file/hl_file_io.h"
#include "fitting//optimizers/lmfit_optimizer.h"
#include <mvc/NumericPrecDelegate.h>
#include <preferences/Preferences.h>
#include "io/file/aps/aps_roi.h"
Expand Down Expand Up @@ -138,6 +137,7 @@ void FitSpectraWidget::createLayout()

_fit_params_table_model = new FitParamsTableModel();
_fit_params_table_model->setFitParams(g_model.fit_parameters());
_fit_params_table_model->setOptimizerSupportsMinMax(true);
connect(_fit_params_table_model, &FitParamsTableModel::onEnergyChange, this, &FitSpectraWidget::replot_integrated_spectra_with_background);
ComboBoxDelegate *cbDelegate = new ComboBoxDelegate(bound_types);
NumericPrecDelegate* npDelegate = new NumericPrecDelegate();
Expand All @@ -161,6 +161,7 @@ void FitSpectraWidget::createLayout()
SLOT(fit_params_customMenuRequested(QPoint)));

_fit_elements_table_model = new FitElementsTableModel(_detector_element);
//_fit_elements_table_model->setDisplayHeaderMinMax(true);

connect(_spectra_widget, SIGNAL(y_axis_changed(bool)), _fit_elements_table_model, SLOT(update_counts_log10(bool)));
connect(_fit_elements_table_model, &FitElementsTableModel::braching_ratio_changed, this, &FitSpectraWidget::on_braching_ratio_update);
Expand Down Expand Up @@ -248,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_MP_FIT);
_cb_opttimizer->addItem(STR_HYBRID_MP_FIT);
_cb_opttimizer->addItem(STR_LM_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 @@ -271,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 @@ -287,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 @@ -315,7 +307,6 @@ void FitSpectraWidget::createLayout()

_cb_fitting_preset->setCurrentIndex(3);
//optimizer_preset_changed(3); // batch with tails
optimizer_changed(STR_MP_FIT);

QLayout* layout = new QVBoxLayout();
// layout->addWidget(splitter);
Expand Down Expand Up @@ -892,13 +883,6 @@ void FitSpectraWidget::Fit_Spectra_Click()

if(_elements_to_fit != nullptr && _int_spec != nullptr)
{
////std::lock_guard<std::mutex> lock(_mutex);

//fitting::optimizers::LMFit_Optimizer lmfit_optimizer;
//fitting::optimizers::MPFit_Optimizer mpfit_optimizer;
//fitting::models::Gaussian_Model model;


data_struct::Fit_Parameters<double> out_fit_params = _fit_params_table_model->getFitParams();
data_struct::Fit_Parameters<double> element_fit_params = _fit_elements_table_model->getAsFitParams();

Expand All @@ -908,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 All @@ -921,51 +905,6 @@ void FitSpectraWidget::Fit_Spectra_Click()
_spectra_widget->getDisplayHeightMax());
_fitting_dialog->exec();

//Range of energy in spectra to fit
//fitting::models::Range energy_range;
//energy_range.min = 0;
//energy_range.max = _int_spec->rows() -1;

//data_struct::Spectra s1 = _integrated_spectra.sub_spectra(energy_range);

//Fitting routines
//fitting::routines::Param_Optimized_Fit_Routine fit_routine;
//fit_routine.set_update_coherent_amplitude_on_fit(false);

//if(_cb_opttimizer->currentText() == STR_LM_FIT)
//{
// fit_routine.set_optimizer(&lmfit_optimizer);
//}
//else if(_cb_opttimizer->currentText() == STR_MP_FIT)
//{
// fit_routine.set_optimizer(&mpfit_optimizer);
//}

//reset model fit parameters to defaults
//model.reset_to_default_fit_params();
//Update fit parameters by override values
//model.update_fit_params_values(&out_fit_params);
//model.update_and_add_fit_params_values_gt_zero(&element_fit_params);

//model.set_fit_params_preset(fitting::models::Fit_Params_Preset::BATCH_FIT_WITH_TAILS);

//Initialize the fit routine
//fit_routine.initialize(&model, _elements_to_fit, energy_range);

//Fit the spectra saving the element counts in element_fit_count_dict
// single threaded
//out_fit_params = fit_routine.fit_spectra_parameters(&model, int_spectra, _elements_to_fit);
// use background thread to not freeze up the gui
//std::future<data_struct::Fit_Parameters> ret = global_threadpool.enqueue(&fitting::routines::Param_Optimized_Fit_Routine::fit_spectra_parameters, fit_routine, &model, (Spectra*)_int_spec, _elements_to_fit);

//while (ret._Is_ready() == false)
//{
// QCoreApplication::processEvents();
//}
//out_fit_params = ret.get();

//out_fit_params = fit_routine.fit_spectra_parameters(&model, &s1, _elements_to_fit);

if (_fitting_dialog->accepted_fit())
{

Expand Down Expand Up @@ -1104,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 Expand Up @@ -1362,20 +1301,6 @@ void FitSpectraWidget::optimizer_preset_changed(int val)

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


void FitSpectraWidget::optimizer_changed(QString val)
{
if(val == STR_LM_FIT)
_fit_params_table_model->setOptimizerSupportsMinMax(false);
else if(val == STR_MP_FIT || val == STR_HYBRID_MP_FIT)
_fit_params_table_model->setOptimizerSupportsMinMax(true);

_fit_params_table->resizeColumnToContents(0);

}

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

void FitSpectraWidget::setIntegratedSpectra(ArrayDr* int_spec)
{
{
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
Loading

0 comments on commit 9e09550

Please sign in to comment.