Skip to content

Commit

Permalink
Change lmfit to use abs(raw_spec - model_spec) * weights
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Glowacki committed Dec 5, 2023
1 parent 84019c7 commit ebb6d02
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/fitting/optimizers/lmfit_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void residuals_lmfit( const T_real *par, int m_dat, const void *data, T_real *fv
// Calculate residuals
for (int i = 0; i < m_dat; i++ )
{
fvec[i] = pow((ud->spectra[i] - ud->spectra_model[i]),2) * ud->weights[i];
fvec[i] = abs(ud->spectra[i] - ud->spectra_model[i]) * ud->weights[i];
if (std::isfinite(fvec[i]) == false)
{
//logE << "\n\n\n";
Expand Down Expand Up @@ -124,7 +124,7 @@ void general_residuals_lmfit( const T_real *par, int m_dat, const void *data, T_
// Calculate residuals
for (int i = 0; i < m_dat; i++ )
{
fvec[i] = pow(( ud->spectra[i] - ud->spectra_model[i] ),2) * ud->weights[i];
fvec[i] = abs( ud->spectra[i] - ud->spectra_model[i] ) * ud->weights[i];
if (std::isfinite(fvec[i]) == false)
{
fvec[i] = ud->spectra[i];
Expand Down Expand Up @@ -163,7 +163,7 @@ void quantification_residuals_lmfit( const T_real *par, int m_dat, const void *d
}
else
{
fvec[idx] = itr.second.e_cal_ratio - result_map[itr.first];
fvec[idx] = abs(itr.second.e_cal_ratio - result_map[itr.first]);
}
idx++;
}
Expand Down
4 changes: 2 additions & 2 deletions src/fitting/optimizers/mpfit_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int gen_residuals_mpfit(int m, int params_size, T_real *params, T_real *dy, T_re
// Calculate residuals
for (int i=0; i<m; i++)
{
dy[i] = pow(( ud->spectra[i] - ud->spectra_model[i] ),2) * ud->weights[i];
dy[i] = abs( ud->spectra[i] - ud->spectra_model[i] ) * ud->weights[i];
if (std::isfinite(dy[i]) == false)
{
dy[i] = ud->spectra[i];
Expand Down Expand Up @@ -174,7 +174,7 @@ int quantification_residuals_mpfit(int m, int params_size, T_real *params, T_rea
}
else
{
dy[idx] = itr.second.e_cal_ratio - result_map[itr.first];
dy[idx] = abs(itr.second.e_cal_ratio - result_map[itr.first]);
}
idx++;
}
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 @@ -186,7 +186,7 @@ void fill_user_data(User_Data<T_real> &ud,
weights = convolve1d(weights, 5);
weights = Eigen::abs(weights);
weights /= weights.maxCoeff();
weights = weights.unaryExpr([](T_real v) { return std::isfinite(v) ? v : (T_real)0.0; });
weights = weights.unaryExpr([](T_real v) { return std::isfinite(v) ? v : (T_real)1.0; });
ud.weights = weights.segment(energy_range.min, energy_range.count());
/*
ArrayTr<T_real> weights = (*spectra);
Expand Down

0 comments on commit ebb6d02

Please sign in to comment.