Skip to content

Commit

Permalink
Merge pull request #1628 from pace-neutrons/1627_gen_sqw_with_tmp
Browse files Browse the repository at this point in the history
fixed gen_sqw for tmp files and removed broken accumulate_sqw code.
  • Loading branch information
abuts committed Apr 3, 2024
2 parents d57652a + 8a91a3d commit f0eb934
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 362 deletions.
48 changes: 38 additions & 10 deletions _test/test_gen_sqw_workflow/gen_sqw_accumulate_sqw_tests_common.m
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,25 @@ function test_gen_sqw(obj,varargin)
end
% ---------------------------------------
% Test gen_sqw ---------------------------------------
clConf = set_temporary_config_options('hor_config','delete_tmp',true);

[en,efix, emode, alatt, angdeg, u, v, psi, omega, dpsi, gl, gs]=unpack(obj);
%hc.threads = 1;


[dummy,grid1,pix_data_range1]=gen_sqw (obj.spe_file, '', ...
[tmp_files,grid1,pix_data_range1]=gen_sqw (obj.spe_file, '', ...
sqw_file_123456, efix, emode, alatt, angdeg, u, v, psi, omega, dpsi, gl, gs);
for i=1:numel(tmp_files)
assertFalse(is_file(tmp_files{i}));
end
%hc.build_sqw_in_parallel=0;
[dummy,grid2,pix_data_range2]=gen_sqw (obj.spe_file([1,4,5,6,2,3]),...
[tmp_files,grid2,pix_data_range2]=gen_sqw (obj.spe_file([1,4,5,6,2,3]),...
'', sqw_file_145623, efix([1,4,5,6,2,3]), emode, alatt, angdeg,...
u, v, psi([1,4,5,6,2,3]), omega([1,4,5,6,2,3]), ...
dpsi([1,4,5,6,2,3]), gl([1,4,5,6,2,3]), gs([1,4,5,6,2,3]));
for i=1:numel(tmp_files)
assertFalse(is_file(tmp_files{i}));
end

assertEqual(grid1,grid2);
assertElementsAlmostEqual(pix_data_range1,pix_data_range2);
Expand Down Expand Up @@ -401,10 +408,11 @@ function test_gen_sqw_sym(obj,varargin)
assertEqual(w_inf_sym.npixels,w_mem_sym.npixels)
%TODO: with proper projection the error should be lower. The
%difference due to various borders added in different places
clear hc_config_cleanup;
skipTest('Memory based and file-based symmetrisation ranges are different Ticket #798');
assertEqualToTol(w_mem_sym,w_inf_sym,'ignore_str',true,'tol',2.e-5)

clear config_cleanup;


end

Expand All @@ -419,6 +427,7 @@ function test_accumulate_sqw14(obj,varargin)
clobS = onCleanup(@()obj.tearDown());
end
%-------------------------------------------------------------
clConf = set_temporary_config_options('hor_config','delete_tmp',true);
file_pref = obj.test_pref;
wk_dir = obj.working_dir;

Expand All @@ -434,8 +443,11 @@ function test_accumulate_sqw14(obj,varargin)
% ---------------------------------------------------------------------------
[~,efix, emode, alatt, angdeg, u, v, psi, omega, dpsi, gl, gs]=unpack(obj);

[~,grid1,pix_range14]=gen_sqw (obj.spe_file([1,4]), '', sqw_file_14, efix([1,4]),...
[tmp_files,grid1,pix_range14]=gen_sqw (obj.spe_file([1,4]), '', sqw_file_14, efix([1,4]),...
emode, alatt, angdeg, u, v, psi([1,4]), omega([1,4]), dpsi([1,4]), gl([1,4]), gs([1,4]));
for i=1:numel(tmp_files)
assertFalse(is_file(tmp_files{i}));
end

% Now use accumulate sqw ----------------------
obj.proj.u=u;
Expand Down Expand Up @@ -550,11 +562,12 @@ function test_accumulate_and_combine1to4(obj,varargin)


gen_sqw_accumulate_sqw_tests_common.rename_file_list(new_names{2},'.nxspe');
[~,~,pix_range_f1456]=accumulate_sqw(spe_names, '', sqw_file_accum, ...
[tmp_fls,~,pix_range_f1456]=accumulate_sqw(spe_names, '', sqw_file_accum, ...
efix, emode, alatt, angdeg, u, v, psi, omega, dpsi, gl, gs);
assertTrue(all(pix_range_f145(1,:)>=pix_range_f1456(1,:)));
assertTrue(all(pix_range_f145(2,:)<=pix_range_f1456(2,:)));

%
clAccFls=onCleanup(@()obj.delete_files(tmp_fls));
%
ldr = sqw_formats_factory.instance().get_loader(sqw_file_accum);
dat = ldr.get_data();
Expand Down Expand Up @@ -612,10 +625,13 @@ function test_accumulate_sqw1456(obj,varargin)
hc = hpc_config;
disp(hc);

[~,~,pix_range1456]=...
[tmp_fls,~,pix_range1456]=...
gen_sqw (obj.spe_file([1,4,5,6]), '',sqw_file_1456, efix([1,4,5,6]),...
emode, alatt, angdeg, u, v,...
psi([1,4,5,6]), omega([1,4,5,6]), dpsi([1,4,5,6]), gl([1,4,5,6]), gs([1,4,5,6]));
for i=1:numel(tmp_fls)
assertTrue(is_file(tmp_fls{i}));
end
ldr = sqw_formats_factory.instance().get_loader(sqw_file_1456);
dat = ldr.get_data();
data_range = ldr.get_data_range();
Expand All @@ -628,13 +644,19 @@ function test_accumulate_sqw1456(obj,varargin)
% Now use accumulate sqw ----------------------

spe_accum={obj.spe_file{1},'','',obj.spe_file{4},obj.spe_file{5},obj.spe_file{6}};
[~,~,acc_pix_range1456]=...
[tmp_fls_t,~,acc_pix_range1456]=...
accumulate_sqw (spe_accum, '', sqw_file_accum,efix,...
emode, alatt, angdeg, u, v, psi, omega, dpsi, gl, gs);
clFls = onCleanup(@()obj.delete_files(tmp_fls_t));
for i=1:numel(tmp_fls)
assertTrue(is_file(tmp_fls_t{i}));
end


ldr = sqw_formats_factory.instance().get_loader(sqw_file_accum);
dat = ldr.get_data();
data_range = ldr.get_data_range();
assertElementsAlmostEqual(data_range,acc_pix_range1456,'relative',4*eps('single'));
assertElementsAlmostEqual(data_range,acc_pix_range1456,'relative',4*eps('single'));
clear ldr;
img_db_range2 = dat.img_range;
% img_db_range in second case is wider then in the first, as
Expand Down Expand Up @@ -695,7 +717,10 @@ function test_accumulate_sqw11456(obj,varargin)
omega([1,3,4,5,6]), dpsi([1,3,4,5,6]), gl([1,3,4,5,6]), gs([1,3,4,5,6]),...
'replicate');
assertEqual(exist(sqw_file_11456,'file'),2)
clobT = onCleanup(@()obj.delete_files(tmp_files));
for i=1:numel(tmp_files)
assertTrue(is_file(tmp_files{i}));
end


% Now use accumulate sqw ----------------------
obj.proj.u=u;
Expand All @@ -709,6 +734,7 @@ function test_accumulate_sqw11456(obj,varargin)
sqw_file_accum,efix, emode, alatt, angdeg, u, v, psi,...
omega, dpsi, gl, gs,...
'replicate'); %grid_size1,pix_range1,
clobT = onCleanup(@()obj.delete_files(tmp_fls));

assertEqualToTol(pix_range1,pix_range2,[2.e-7,2.e-7]);
% Estimated ranges the pixels rebinned onto are different but
Expand Down Expand Up @@ -745,6 +771,8 @@ function test_accumulate_sqw11456(obj,varargin)

assertTrue(ok,['Cuts from gen_sqw output and accumulate_sqw are not the same: ',...
mess]);
clear cleanup_obj1;
clear clobT;
end
%
end
Expand Down
62 changes: 48 additions & 14 deletions _test/test_gen_sqw_workflow/test_gen_sqw_accumulate_sqw_nomex.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
classdef test_gen_sqw_accumulate_sqw_nomex < ...
gen_sqw_accumulate_sqw_tests_common & gen_sqw_common_config

% Series of tests of gen_sqw and associated functions
% when mex code is disabled or not available
%
Expand Down Expand Up @@ -29,11 +29,11 @@
%
%>>tc=test_gen_sqw_accumulate_sqw_nomex ('save');
%>>tc.save():


properties
end

methods
function obj=test_gen_sqw_accumulate_sqw_nomex(test_name)
% Series of tests of gen_sqw and associated functions
Expand All @@ -52,21 +52,57 @@
obj = obj@gen_sqw_common_config(0,0,'matlab',-1);
obj = obj@gen_sqw_accumulate_sqw_tests_common(test_name,'nomex');
end

%
function test_gen_sqw_works_with_existing_tmp(obj,varargin)
if nargin > 1 % running in single test method mode.
obj.setUp();
co1 = onCleanup(@()obj.tearDown());
end
sqw_file_1234=fullfile(tmp_dir,['sqw_1234_',obj.test_pref,'.sqw']); % output sqw file which should never be created
clWarn = set_temporary_warning('off','HORACE:push_warning','HORACE:valid_tmp_files_exist');

[~,efix, emode, alatt, angdeg, u, v, psi, omega, dpsi, gl, gs]=unpack(obj);
spe_files1 = obj.spe_file([1,2,3,4]);
spe_files1{4} = 'missing.nxspe';
spe_files2 = obj.spe_file([1,2,3,4]);
[tmp_files,grid_size,~,pix_db_range] =gen_sqw (spe_files1, '', sqw_file_1234, efix([1,2,3,4]),...
emode, alatt, angdeg, u, v, psi([1,2,3,4]), omega([1,2,3,4]),...
dpsi([1,2,3,4]), gl([1,2,3,4]), gs([1,2,3,4]),'tmp_only','accumulate');
assertEqual(numel(tmp_files),3);
for i=1:numel(tmp_files)
assertTrue(isfile(tmp_files{i}))
end

warning('HORACE:push_warning','push warning issued to ensure correct warning will appear below');
[tmp_files,~,~,wout_sqw] =gen_sqw (spe_files2, '', sqw_file_1234, efix([1,2,3,4]),...
emode, alatt, angdeg, u, v, psi([1,2,3,4]), omega([1,2,3,4]),...
dpsi([1,2,3,4]), gl([1,2,3,4]), gs([1,2,3,4]),grid_size,pix_db_range);
[w_mess,warn_id] = lastwarn;
assertEqual(warn_id,'HORACE:valid_tmp_files_exist')
assertTrue(strncmp(w_mess(2:end),'*** There are 3 previously generated tmp files present',23))
assertEqual(numel(tmp_files),4);
for i=1:numel(tmp_files)
% all tmp were deleted when sqw was generated
assertFalse(isfile(tmp_files{i}))
end

assertTrue(isa(wout_sqw,'sqw'))
assertEqual(wout_sqw.data.img_range,pix_db_range);

end
%
function test_wrong_params_gen_sqw(obj,varargin)
if nargin > 1 % running in single test method mode.
obj.setUp();
co1 = onCleanup(@()obj.tearDown());

end
% something wrong with this test -- it was with 'replicate'
% option and apparently failing
sqw_file_15456=fullfile(tmp_dir,['sqw_123456_',obj.test_pref,'.sqw']); % output sqw file which should never be created
[en,efix, emode, alatt, angdeg, u, v, psi, omega, dpsi, gl, gs]=unpack(obj);

[~,efix, emode, alatt, angdeg, u, v, psi, omega, dpsi, gl, gs]=unpack(obj);
spe_files = obj.spe_file([1,5,4,5,6]);

try
gen_sqw (spe_files, '', sqw_file_15456, efix([1,5,4,5,6]),...
emode, alatt, angdeg, u, v, psi([1,5,4,5,6]), omega([1,5,4,5,6]),...
Expand All @@ -75,7 +111,6 @@ function test_wrong_params_gen_sqw(obj,varargin)
catch ME
ok=true;
assertEqual(ME.identifier,'HORACE:algorithms:invalid_argument')

end
assertTrue(ok,'Should have failed because of repeated spe file name and parameters');
end
Expand All @@ -84,10 +119,10 @@ function test_wrong_params_accum_sqw(obj)
%-------------------------------------------------------------
%-------------------------------------------------------------
sqw_file_accum=fullfile(tmp_dir,['sqw_accum_',obj.test_pref,'.sqw']); % output sqw file which should never be created


[en,efix, emode, alatt, angdeg, u, v, psi, omega, dpsi, gl, gs]=unpack(obj);

% Repeat a file
spe_accum={obj.spe_file{1},'',obj.spe_file{5},obj.spe_file{4},obj.spe_file{5},obj.spe_file{6}};
try
Expand All @@ -98,7 +133,6 @@ function test_wrong_params_accum_sqw(obj)
assertEqual(ME.identifier,'HORACE:algorithms:invalid_argument');
end
assertTrue(ok,'Should have failed because of repeated spe file name');

end
end
end
21 changes: 21 additions & 0 deletions herbert_core/utilities/maths/is_range_wider.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function is = is_range_wider(range1,range2)
% Function accepts two ranges defined by 2xN matrices containing min/max
% values, and returns true if first range surrounds the second one, i.e.
% all minimas of first range are smaller or equal to minimas of the second
% ranges and all maximas of the first range are larger or equal to maximas
% of the first range
%
% Input:
% range1 -- 2xN matrix with each column containing [min1;max1] value
% range1 -- another 2xN matrix with each column containing [min2;max2] value
%
% Returns:
% true if first matrix surrounds second matrix or false otherwise
if any(size(range1)~=size(range2)) || size(range1,1) ~=2
error('HORACE:utilities:invalid_argument', ...
['The ranges sizes needs to be the same and have form: 2xN.\n' ...
'Size of range1 is: %s and size of range2 is %s'], ...
disp2str(range1),disp2str(range2));
end

is = all(range1(1,:)<=range2(1,:))&&all(range1(2,:)>=range2(2,:));
Loading

0 comments on commit f0eb934

Please sign in to comment.