Skip to content

Commit fddc32c

Browse files
authored
Merge pull request #210 from aglowacki/master
Bug fixes
2 parents 428d267 + f3dcd14 commit fddc32c

6 files changed

Lines changed: 47 additions & 15 deletions

File tree

.github/workflows/cmake.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
key: ${{ runner.os }}-vcpkg
3333
- name: Run vcpkg
3434
if: ${{ steps.cache-vcpkg-restore.outputs.cache-hit != 'true' }}
35-
run: ./vcpkg/vcpkg install hdf5 netcdf-c yaml-cpp zeromq jsoncpp
35+
run: ./vcpkg/vcpkg install hdf5 netcdf-c yaml-cpp zeromq cppzmq jsoncpp nlopt
3636
- name: Save vcpkg cache
3737
id: cache-vcpkg-save
3838
uses: actions/cache/save@v3
@@ -87,7 +87,7 @@ jobs:
8787
rsync -RavzP bin install
8888
cd install
8989
echo '{"branchName":"${{ github.ref }}","commitHash":"${{ github.sha }}"}' >> version.json
90-
- uses: actions/upload-artifact@v3
90+
- uses: actions/upload-artifact@v4
9191
with:
9292
name: XRF-Maps_${{ matrix.os }}_x86_64
9393
path: ${{github.workspace}}/install/

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
path: $(VCPKG_CONFIG_CACHE)
2525
cacheHitVar: CACHE_RESTORED
2626
displayName: Cache vcpkg packages
27-
- script: .\vcpkg\vcpkg.exe install hdf5 netcdf-c yaml-cpp zeromq jsoncpp --triplet x64-windows
27+
- script: .\vcpkg\vcpkg.exe install hdf5 netcdf-c yaml-cpp zeromq cppzmq jsoncpp nlopt --triplet x64-windows
2828
condition: ne(variables.CACHE_RESTORED, 'true')
2929
- script: mkdir build
3030
- task: CMake@1

src/core/process_whole.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ bool perform_quantification(data_struct::Analysis_Job<double>* analysis_job, boo
473473
logI << Fitting_Routine_To_Str.at(fit_itr.first) << " " << quant_itr.first << "\n";
474474
Fit_Parameters<double> fit_params;
475475
// min, and max values doen't matter because we are free fitting amplitude only
476-
fit_params.add_parameter(Fit_Param<double>("quantifier", 0.0, std::numeric_limits<double>::max()/2.0, 1.0, 0.0001, E_Bound_Type::FIT));
476+
fit_params.add_parameter(Fit_Param<double>("quantifier", 0.0000000001, std::numeric_limits<double>::max()*.00001, 1.0, 0.0001, E_Bound_Type::LIMITED_LO_HI));
477477
//initial guess: parinfo_value[0] = 100000.0 / factor
478478
fit_params["quantifier"].value = (double)100000.0 / quant_itr.second;
479479
optimizer->minimize_quantification(&fit_params, &detector->all_element_quants[fit_itr.first][quant_itr.first], &quantification_model);

src/fitting/optimizers/nlopt_optimizer.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ double residuals_nlopt(const std::vector<double> &x, std::vector<double> &grad,
8686

8787
double sum = 0.0;
8888
double dy = 0.;
89+
double dt = 0.;
8990
//Calculate residuals
9091
for (int i=0; i<ud->spectra.size(); i++)
9192
{
92-
sum += pow((ud->spectra[i] - ud->spectra_model[i]), 2.0) * ud->weights[i];
93+
// orig
94+
//sum += pow((ud->spectra[i] - ud->spectra_model[i]), 2.0) * ud->weights[i];
95+
dy += pow((ud->spectra[i] - ud->spectra_model[i]), 2.0);
96+
dt += pow((ud->spectra[i] - ud->spec_avg), 2.0);
9397
/*
9498
if (std::isfinite(dy) == false)
9599
{
@@ -111,6 +115,7 @@ double residuals_nlopt(const std::vector<double> &x, std::vector<double> &grad,
111115
}
112116
*/
113117
}
118+
sum = dy / dt;
114119
//logI << "f = " << sum << "\n";
115120
ud->cur_itr++;
116121
if (ud->status_callback != nullptr)
@@ -145,11 +150,16 @@ double gen_residuals_nlopt(const std::vector<double> &x, std::vector<double> &gr
145150
ud->spectra_model += ud->spectra_background;
146151

147152
double sum = 0.0;
153+
double dy = 0.;
154+
double dt = 0.;
148155
// Calculate residuals
149156
for (int i=0; i<ud->spectra.size(); i++)
150157
{
151-
sum += pow((ud->spectra[i] - ud->spectra_model[i]), 2.0) * ud->weights[i];
158+
//sum += pow((ud->spectra[i] - ud->spectra_model[i]), 2.0) * ud->weights[i];
159+
dy += pow((ud->spectra[i] - ud->spectra_model[i]), 2.0);
160+
dt += pow((ud->spectra[i] - ud->spec_avg), 2.0);
152161
}
162+
sum = dy / dt;
153163

154164
return sum;
155165
}
@@ -513,13 +523,17 @@ OPTIMIZER_OUTCOME NLOPT_Optimizer<T_real>::minimize_quantification(Fit_Parameter
513523
return OPTIMIZER_OUTCOME::STOPPED;
514524
}
515525

516-
nlopt::opt opt(_algo, fitp_arr.size());
526+
//nlopt::opt opt(nlopt::algorithm::GN_ESCH, fitp_arr.size());
527+
//nlopt::opt opt(nlopt::algorithm::GN_ISRES, fitp_arr.size());
528+
//nlopt::opt opt(nlopt::algorithm::LN_SBPLX, fitp_arr.size());
529+
nlopt::opt opt(nlopt::algorithm::GN_CRS2_LM, fitp_arr.size());
517530
opt.set_lower_bounds(lb_arr);
518531
opt.set_upper_bounds(ub_arr);
519532
opt.set_default_initial_step(step_arr);
520533
opt.set_min_objective(quantification_residuals_nlopt<T_real>, (void*)&ud);
521534
opt.set_xtol_rel(_options.at(STR_OPT_XTOL));
522-
opt.set_maxeval(_options.at(STR_OPT_MAXITER));
535+
//opt.set_maxeval(_options.at(STR_OPT_MAXITER));
536+
opt.set_maxeval(1000000);
523537

524538
double minf;
525539
nlopt::result result;

src/fitting/optimizers/optimizer.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ struct User_Data
114114
Range energy_range;
115115
Spectra<T_real> spectra_model;
116116
const Spectra<T_real> *orig_spectra;
117-
ArrayTr<T_real> norm_arr;
118-
T_real normalizer;
117+
T_real spec_avg;
118+
//T_real normalizer;
119119
Callback_Func_Status_Def* status_callback;
120120
size_t cur_itr;
121121
size_t total_itr;
@@ -137,7 +137,8 @@ struct Gen_User_Data
137137
Range energy_range;
138138
Gen_Func_Def<T_real> func;
139139
Spectra<T_real> spectra_model;
140-
T_real normalizer;
140+
T_real spec_avg;
141+
//T_real normalizer;
141142
};
142143

143144
template<typename T_real>
@@ -171,8 +172,7 @@ void fill_user_data(User_Data<T_real> &ud,
171172
ud.fit_model = (Base_Model<T_real>*)model;
172173
// set spectra to fit
173174
ud.spectra = spectra->sub_spectra(energy_range.min, energy_range.count());
174-
ud.norm_arr = ud.spectra.pow(2.0); // square the spectra and sum it
175-
ud.normalizer = ud.norm_arr.sum();
175+
ud.spec_avg = ud.spectra.sum() / energy_range.count();
176176
//not allocating memory. see https://eigen.tuxfamily.org/dox/group__TutorialMapClass.html
177177
//new (&(ud.spectra)) Eigen::Map<const ArrayTr<T_real>>(spectra->data() + energy_range.min, energy_range.count());
178178
ud.orig_spectra = spectra;
@@ -243,8 +243,9 @@ void fill_gen_user_data(Gen_User_Data<T_real>& ud,
243243
ud.fit_model = (Base_Model<T_real>*)model;
244244
// set spectra to fit
245245
ud.spectra = spectra->sub_spectra(energy_range.min, energy_range.count());
246-
ArrayTr<T_real> norm_arr = ud.spectra.pow(2.0); // square the spectra and sum it
247-
ud.normalizer = norm_arr.sum();
246+
ud.spec_avg = ud.spectra.sum() / energy_range.count();
247+
//ArrayTr<T_real> norm_arr = ud.spectra.pow(2.0); // square the spectra and sum it
248+
//ud.normalizer = norm_arr.sum();
248249
//not allocating memory. see https://eigen.tuxfamily.org/dox/group__TutorialMapClass.html
249250
//new (&ud.spectra) Eigen::Map<const ArrayTr<T_real>>(spectra->data() + energy_range.min, energy_range.count());
250251
ud.fit_parameters = fit_params;

src/io/file/hdf5_io.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ POSSIBILITY OF SUCH DAMAGE.
5757
#include <mutex>
5858
#include <algorithm>
5959
#include <cctype>
60+
#include <filesystem>
6061

6162

6263
const std::vector<std::string> xrf_analysis_save_names = { STR_FIT_ROI,
@@ -381,7 +382,23 @@ bool HDF5_IO::start_save_seq(const std::string filename, bool force_new_file, bo
381382
}
382383

383384
if(false == force_new_file)
385+
{
384386
_cur_file_id = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
387+
}
388+
else
389+
{
390+
try
391+
{
392+
if (std::filesystem::remove(filename))
393+
{
394+
logI << "file " << filename << " deleted.\n";
395+
}
396+
}
397+
catch(const std::filesystem::filesystem_error& err)
398+
{
399+
logE << "filesystem error: " << err.what() << '\n';
400+
}
401+
}
385402
if (_cur_file_id < 1)
386403
{
387404
if (open_file_only == false)

0 commit comments

Comments
 (0)