Skip to content

More usability improvements sw fitpowder (DRAFT IN PROG) #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
72 changes: 69 additions & 3 deletions +sw_tests/+unit_tests/unittest_sw_fitpowder.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,41 @@ function test_init_data_1d_indep_bg(testCase)
testCase.verify_results(out, expected_fitpow);
end

function test_set_background_strategy(testCase)
function test_set_background_strategy_to_planar(testCase)
out = sw_fitpowder(testCase.swobj, testCase.data_1d_cuts, ...
testCase.fit_func, testCase.j1, "independent");
% set some background parameters (correpsonding to planar bg
% with slope_en=slope_q=intercept =2
out.set_bg_parameters(1, 2); % en_slope = 2
out.set_bg_parameters(2, 10, 1); % intercept = 10 for cut 1
out.set_bg_parameters(2, 12, 2); % intercept = 12 for cut 2
out.set_background_strategy("planar");
expected_fitpow = testCase.default_fitpow;
expected_fitpow.params(2:end-1) = 2;
expected_fitpow.modQ_cens = testCase.default_modQ_cens_1d;
testCase.verify_results(out, expected_fitpow);
testCase.verify_results(out, expected_fitpow, ...
testCase.default_fields, ...
'abs_tol', 1e-10);
end

function test_set_background_strategy_to_indep(testCase)
out = sw_fitpowder(testCase.swobj, testCase.data_1d_cuts, ...
testCase.fit_func, testCase.j1, "planar");
% set some background parameters (correpsonding to planar bg
% with slope_en=slope_q=intercept =2
out.set_bg_parameters(1:3, [2,2,2]); % en_slope = 2
out.set_background_strategy("independent");
expected_fitpow = testCase.default_fitpow;
% add extra background param
expected_fitpow.params = expected_fitpow.params([1:2,2:end],:);
expected_fitpow.bounds = expected_fitpow.bounds([1:2,2:end],:);
expected_fitpow.params(2:2:end-1) = 2; % en slope
expected_fitpow.params(3) = 10; % intercept = 10 for cut 1
expected_fitpow.params(5) = 12; % intercept = 12 for cut 2
expected_fitpow.modQ_cens = testCase.default_modQ_cens_1d;
testCase.verify_results(out, expected_fitpow, ...
testCase.default_fields, ...
'abs_tol', 1e-10);
end

function test_replace_2D_data_with_1D_cuts(testCase)
Expand Down Expand Up @@ -342,6 +370,21 @@ function test_fit_background(testCase, fit_params)
'abs_tol', 1e-4);
end

function test_fit_background_and_scale(testCase)
out = sw_fitpowder(testCase.swobj, testCase.data_2d, ...
testCase.fit_func, testCase.j1);
out.fix_bg_parameters(1:2); % fix slopes of background to 0
out.set_bg_parameters(3, 1.5); % initial guess
out.fit_background_and_scale();
expected_fitpow = testCase.default_fitpow;
expected_fitpow.params(end-1) = 0.0029;
expected_fitpow.params(end) = 15.47;
expected_fitpow.bounds(2:3,:) = 0; % fixed bg slopes
testCase.verify_results(out, expected_fitpow, ...
testCase.default_fields, ...
'abs_tol', 1e-3);
end

function test_calc_cost_func_of_background_indep(testCase)
out = sw_fitpowder(testCase.swobj, testCase.data_1d_cuts, ...
testCase.fit_func, testCase.j1, "independent", 2);
Expand Down Expand Up @@ -406,11 +449,12 @@ function test_calc_uncertainty(testCase)
function test_estimate_scale_factor(testCase)
out = sw_fitpowder(testCase.swobj, testCase.data_2d, ...
testCase.fit_func, testCase.j1);
out.set_bg_parameters(3, 0.05); % set constant bg
out.powspec_args.dE = 0.1; % constant energy resolution
out.powspec_args.hermit = true;
out.estimate_scale_factor()
expected_fitpow = testCase.default_fitpow;
expected_fitpow.params(end) = 17.6;
expected_fitpow.params(end) = 17.36;
testCase.verify_results(out, expected_fitpow, ...
testCase.default_fields, 'abs_tol', 1e-1);
end
Expand Down Expand Up @@ -450,6 +494,28 @@ function test_add_1Dcuts_after_2D_data(testCase)
expected_fitpow.modQ_cens = testCase.default_modQ_cens_1d; % integrtate over nQ pts
testCase.verify_results(out, expected_fitpow);
end

function test_set_bg_region_data_2d(testCase)
out = sw_fitpowder(testCase.swobj, testCase.data_2d, ...
testCase.fit_func, testCase.j1);
out.set_bg_region(0,1.5); % for all Q
out.set_bg_region(2.5,3.5,4.5,inf); % for highest Q
expected_fitpow = testCase.default_fitpow;
expected_fitpow.ibg = [1;4;6];
testCase.verify_results(out, expected_fitpow);
end

function test_set_bg_region_data_1d(testCase)
out = sw_fitpowder(testCase.swobj, testCase.data_1d_cuts, ...
testCase.fit_func, testCase.j1);
expected_fitpow = testCase.default_fitpow;
expected_fitpow.modQ_cens = testCase.default_modQ_cens_1d;
out.set_bg_region(0,1.5); % for all cuts
out.set_bg_region(2.5,3.5,2); % for last cut
expected_fitpow.ibg = [1;4;6];
testCase.verify_results(out, expected_fitpow);
end

end

end
Loading
Loading