Skip to content

Commit

Permalink
Merge pull request #1789 from pace-neutrons/1788_hashable
Browse files Browse the repository at this point in the history
hashable
  • Loading branch information
abuts authored Dec 18, 2024
2 parents 02be891 + d72a376 commit f298c27
Show file tree
Hide file tree
Showing 28 changed files with 784 additions and 538 deletions.
3 changes: 2 additions & 1 deletion _test/shared/matlab_xunit/xunit/assertEqual.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function assertEqual(A, B, custom_message,tol)
if isempty(name_b)
name_b ='B';
end
if nargin<4 && isa(A,'serializable')
if nargin<4 && isa(A,'serializable') &&~isa(A,'hashable') %TODO: Remove complex eq in serializable!
% Re #1147
[ok,custom_message] = eq(A,B,'name_a',name_a,'name_b',name_b);
if ok
return
Expand Down
36 changes: 31 additions & 5 deletions _test/test_IX_classes/test_IX_experiment.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function test_combine_single_runs_eq_headers_changeID(~)

[result,file_id_array,skipped_inputs,this_runid_map] = Input{1}.combine(Input(2:end),true,false);

hash_defined = arrayfun(@(x)(x.hash_defined),result);
assertTrue(all(hash_defined));

data = [data(1:6),data(8:10)];
for i=1:9
data(i).run_id = i;
Expand Down Expand Up @@ -49,6 +52,9 @@ function test_combine_single_runs_changing_ID(~)
Input = num2cell(data);

[result,file_id_array,skipped_inputs,this_runid_map] = Input{1}.combine(Input(2:end),true,false);
hash_defined = arrayfun(@(x)(x.hash_defined),result);
assertTrue(all(hash_defined));


for i=1:10
data(i).run_id = i;
Expand All @@ -69,6 +75,10 @@ function test_combine_single_runs_changing_ID(~)
function test_combine_empty_change_ID(~)
data = test_IX_experiment.build_IX_array(10);
[result,file_id_array,skipped_inputs,this_runid_map] = data.combine({},true,false);

hash_defined = arrayfun(@(x)(x.hash_defined),result);
assertTrue(all(hash_defined));

for i=1:10
data(i).run_id = i;
end
Expand All @@ -92,6 +102,10 @@ function test_combine_multirun_same_headers_works(~)

[result,file_id_array,skipped_inputs,this_runid_map] = Input{1}.combine(Input(2:end),true,true);

hash_defined = arrayfun(@(x)(x.hash_defined),result);
assertTrue(all(hash_defined));


cai = [Input{1},Input{2}(2:10),Input{3}(2:10)];
assertEqual(cai,result);
assertEqual(file_id_array,fids);
Expand All @@ -114,6 +128,9 @@ function test_combine_multirun_works(~)

[result,file_id_array,skipped_inputs,this_runid_map] = Input{1}.combine(Input(2:end));

hash_defined = arrayfun(@(x)(x.hash_defined),result);
assertTrue(all(hash_defined));

cai = [Input{:}];
assertEqual(cai,result);
assertEqual(file_id_array,fids);
Expand All @@ -137,6 +154,9 @@ function test_combine_single_runs_eq_headers(~)
Input = num2cell(data);

[result,file_id_array,skipped_inputs,this_runid_map] = Input{1}.combine(Input(2:end),true,true);
hash_defined = arrayfun(@(x)(x.hash_defined),result);
assertTrue(all(hash_defined));


assertEqual([data(1:6),data(8:10)],result);
assertEqual(file_id_array,fids);
Expand All @@ -156,6 +176,9 @@ function test_combine_single_runs_eq_headers(~)
function test_combine_empty(~)
[data,fids] = test_IX_experiment.build_IX_array(10);
[result,file_id_array,skipped_inputs,this_runid_map] = data.combine({},true,true);
hash_defined = arrayfun(@(x)(x.hash_defined),result);
assertTrue(all(hash_defined));


assertEqual(data,result);
assertEqual(fids,file_id_array);
Expand Down Expand Up @@ -185,6 +208,9 @@ function test_combine_single_runs_works(~)
Input = num2cell(data);

[result,file_id_array,skipped_inputs,this_runid_map] = Input{1}.combine(Input(2:end));
hash_defined = arrayfun(@(x)(x.hash_defined),result);
assertTrue(all(hash_defined));


assertEqual(data,result);
assertEqual(file_id_array,fids);
Expand All @@ -205,8 +231,8 @@ function test_comparison_hash_neq(~)
exp2 = exp1;
exp2.omega = 4;

ch1 = exp1.get_neq_hash();
ch2 = exp2.get_neq_hash();
ch1 = exp1.build_hash();
ch2 = exp2.build_hash();
assertFalse(isequal(ch1,ch2));
end
function test_goniometer_key_construction(~)
Expand Down Expand Up @@ -239,8 +265,8 @@ function test_comparison_hash_eq(~)
exp2 = exp1;
exp2.filepath = 'other_path';

ch1 = exp1.get_neq_hash();
ch2 = exp2.get_neq_hash();
ch1 = exp1.build_hash();
ch2 = exp2.build_hash();
assertEqual(ch1,ch2);
end

Expand Down Expand Up @@ -333,7 +359,7 @@ function test_recover_from_v1_structure_array(~)
v1_struct.version = 1;
v1_struct.array_dat = rmfield(v1_struct.array_dat,'run_id');

exp_rec = serializable.from_struct(v1_struct);
exp_rec = hashable.from_struct(v1_struct);
% old IX_experiment structures in all practical cases were storing
% angular units in radian, so we restoring old versions as
% radians
Expand Down
8 changes: 4 additions & 4 deletions _test/test_mex_nomex/test_main_mex.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ function test_hashing(obj)

% make hashes with mex off
hcf.use_mex = false;
hash1_nomex = Hashing.hashify_obj(obj1);
hash2_nomex = Hashing.hashify_obj(obj2);
[~,hash1_nomex] = build_hash(obj1);
[~,hash2_nomex] = build_hash(obj2);

% make hashes with mex on
hcf.use_mex = true;
hash1_mex = Hashing.hashify_obj(obj1);
hash2_mex = Hashing.hashify_obj(obj2);
[~,hash1_mex] = build_hash(obj1);
[~,hash2_mex] = build_hash(obj2);

% compare mex and nomex hashes
assertTrue( strcmp(hash1_nomex, hash1_mex) );
Expand Down
8 changes: 8 additions & 0 deletions _test/test_sqw_pageOpMethods/test_join.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
hc = horace_paths;
obj.test_dir = hc.test_common;
obj.sample_obj = read_sqw(fullfile(obj.test_dir,'sqw_2d_1.sqw'));

% TODO: Re #1147 This should be removed when ticket is
% fixed ---------------------------------------------------
ex = obj.sample_obj.experiment_info.expdata;
ex = build_hash(ex);
obj.sample_obj.experiment_info.expdata = ex;
% Re #1147 ------------------------------------------------

obj.sqw_to_join = obj.sample_obj.split();
n_parts = numel(obj.sqw_to_join);
obj.files_to_join = cell(n_parts,1);
Expand Down
148 changes: 148 additions & 0 deletions _test/test_unique_objects_container/test_hashable_methods.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
classdef test_hashable_methods < TestCase
%Test class to test IX_experiment constructor and methods
%

properties
end

methods
function obj=test_hashable_methods(varargin)
if nargin == 0
name = 'test_hashable_methods';
else
name = varargin{1};
end
obj = obj@TestCase(name);
end
%==================================================================
function test_exposing_hash_array(~)
data = test_hashable_methods.build_IX_array(10);
[data,hashes,is_new] = build_hash(data);
assertTrue(iscell(hashes))
assertTrue(is_new);


hash_defined = arrayfun(@(x)(x.hash_defined),data);
assertTrue(all(hash_defined));

S = data.to_struct();

assertTrue(isfield(S,'hash_value'));
assertTrue(isa(S.hash_value,'cell'));

is_empty = cellfun(@(x)isempty(x),S.hash_value);
assertFalse(any(is_empty));

rec_data = hashable.from_struct(S);

assertEqual(data,rec_data);
hash_defined = arrayfun(@(x)(x.hash_defined),rec_data);
assertTrue(all(hash_defined));
end
function test_exposing_hash_value(~)
data = test_hashable_methods.build_IX_array(1);
[data,hash,is_new] = build_hash(data);
assertTrue(ischar(hash))
assertTrue(is_new);


hash_defined = arrayfun(@(x)(x.hash_defined),data);
assertTrue(all(hash_defined));

S = data.to_struct();

assertTrue(isfield(S,'hash_value'));
assertTrue(isa(S.hash_value,'cell'));

is_empty = cellfun(@(x)isempty(x),S.hash_value);
assertFalse(any(is_empty));

rec_data = hashable.from_struct(S);

assertEqual(data,rec_data);
hash_defined = arrayfun(@(x)(x.hash_defined),rec_data);
assertTrue(all(hash_defined));

end

function test_exposing_empty_hash_array(~)
data = test_hashable_methods.build_IX_array(10);

hash_defined = arrayfun(@(x)(x.hash_defined),data);
assertFalse(any(hash_defined));

S = data.to_struct();

assertTrue(isfield(S,'hash_value'));
assertTrue(isa(S.hash_value,'cell'));

is_empty = cellfun(@(x)isempty(x),S.hash_value);
assertTrue(all(is_empty));

rec_data = hashable.from_struct(S);

assertEqual(data,rec_data);
end
function test_exposing_empty_hash_value(~)
data = test_hashable_methods.build_IX_array(1);

hash_defined = arrayfun(@(x)(x.hash_defined),data);
assertFalse(any(hash_defined));

S = data.to_struct();

assertTrue(isfield(S,'hash_value'));
assertTrue(isa(S.hash_value,'cell'));

is_empty = cellfun(@(x)isempty(x),S.hash_value);
assertTrue(all(is_empty));

rec_data = hashable.from_struct(S);

assertEqual(data,rec_data);
end
end
methods(Static,Access=private)
function [data,run_id] = build_IX_array(n_elements)
par_names={...
'filename', 'run_id', 'efix','en',...
'psi','omega','dpsi','gl','gs'};
par_val = {'my_file',6666,10,[1,2,4,8]',70,5,5,5,5};
data = repmat(IX_experiment,1,n_elements);
for i=1:n_elements
expd = data(i);
expd.do_check_combo_arg = false;
for j=1:numel(par_names)
if ischar(par_val{j})
val = build_tmp_file_name('nxspe_file','');
elseif numel(par_val{j})>1
expd.efix = expd.efix+5;
val = sort(rand(size(par_val{j}))*expd.efix);
else
val = round(rand()*par_val{j});
end

expd.(par_names{j}) = val;
end
expd.filepath = 'some_file_path';
expd.emode = 1;
expd.do_check_combo_arg = true;
data(i) = expd.check_combo_arg();
end
% ensure run_id are unique to avoid random tests failures
run_id = arrayfun(@(x)x.run_id,data);
uniq_id = unique(run_id);
was_nonunique = false;
while numel(uniq_id) ~= numel(run_id)
was_nonunique = true;
run_id = round(rand(1,n_elements)*par_val{2});
uniq_id = unique(run_id);
end
if was_nonunique
for i=1:n_elements
data(i).run_id = run_id(i);
end
end
end
end
end
Loading

0 comments on commit f298c27

Please sign in to comment.