Skip to content

Commit

Permalink
Support resid_handle in bg fitting of pwader class
Browse files Browse the repository at this point in the history
And add test
  • Loading branch information
RichardWaiteSTFC committed Nov 5, 2024
1 parent 3dc3f2f commit 520e53c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 6 additions & 2 deletions +sw_tests/+unit_tests/unittest_sw_fitpowder.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
default_modQ_cens_1d = 3.55:0.1:5.45; % integrate over nQ pts
end

properties (TestParameter)
fit_params = {{}, {'resid_handle', true}};
end

methods (TestClassSetup)
function setup_spinw_obj_and_expected_result(testCase)
% setup spinw object
Expand Down Expand Up @@ -321,13 +325,13 @@ function test_estimate_constant_background(testCase)
testCase.verify_results(out, expected_fitpow);
end

function test_fit_background(testCase)
function test_fit_background(testCase, fit_params)
out = sw_fitpowder(testCase.swobj, testCase.data_2d, ...
testCase.fit_func, testCase.j1);
out.y(1) = 10; % higher so other bins are background
out.fix_bg_parameters(1:2); % fix slopes of background to 0
out.set_bg_parameters(3, 1.5); % initial guess
out.fit_background()
out.fit_background(fit_params{:})
expected_fitpow = testCase.default_fitpow;
expected_fitpow.y(1) = 10;
expected_fitpow.ibg = [3;6;2;5;4];
Expand Down
8 changes: 7 additions & 1 deletion swfiles/sw_fitpowder.m
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,13 @@ function fit_background(obj, varargin)
error('spinw:sw_fitpowder:fit_background', ...
'Not enough points to fit the function.');
end
[bg_params, ~, stat] = ndbase.simplex([], @obj.calc_cost_func_of_background, ...
if any(cellfun(@(elem) elem =="resid_handle", varargin(1:2:end)))
fobj = @obj.calc_residuals_of_background; % minimise using residual array
else
fobj = @obj.calc_cost_func_of_background; % minimise scalar
end

[bg_params, ~, stat] = ndbase.simplex([], fobj, ...
obj.params(ibg_par)', ...
'lb', obj.bounds(ibg_par,1)', ...
'ub', obj.bounds(ibg_par,2)', ...
Expand Down

0 comments on commit 520e53c

Please sign in to comment.