This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
psom_cmp_var.m
206 lines (188 loc) · 7.95 KB
/
psom_cmp_var.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
function flag_equal = psom_cmp_var(var1,var2,opt)
% Compare two matlab variables, whatever their types may be
%
% SYNTAX:
% FLAG_EQUAL = PSOM_CMP_VAR(VAR1,VAR2,OPT)
%
% _________________________________________________________________________
% INPUTS:
%
% VAR1 (any type) a matlab variable
% VAR2 (any type) a matlab variable
% OPT (boolean) a structure with the following fields:
% EPS (float, default system variable EPS) the max tolerable difference
% between numerical variables, for them to be declared equal.
% FLAG_SOURCE_ONLY (boolean, default false) if the flag is true, for
% structures, test only equality for fields found in the source.
% FLAG_TEST_ALL (boolean, default false) if the flag is false, the tests
% are interrupted as soon as a difference is detected.
% FLAG_VERBOSE (boolean, default false) if the flag is true, print
% which parts of the variable differ.
%
% _________________________________________________________________________
% OUTPUTS:
%
% FLAG_EQUAL (boolean) true if VAR1 and VAR2 are identical, false otherwise.
%
% _________________________________________________________________________
% COMMENTS:
%
% The actual matlab data types supported here are string (or char),
% numeric, logical,
% Copyright (c) Pierre Bellec, Montreal Neurological Institute, 2008-2010.
% Maintainer : [email protected]
% See licensing information in the code.
% Keywords :
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
% THE SOFTWARE.
%% defaults
if nargin < 3
opt.eps = eps;
opt.flag_source_only = false;
opt.flag_verbose = false;
opt.verbose{1} = inputname(1);
opt.verbose{2} = inputname(2);
opt.flag_test_all = false;
elseif isfield(opt,'gb_psom_tested')
else
opt = psom_struct_defaults(opt,{'eps','flag_source_only','flag_verbose','verbose','flag_test_all'},{eps,false,false,{inputname(1),inputname(2)},false});
opt.gb_psom_tested = true; % avoid checking the default options in recursive calls
end
%% Get type of variable 1
type_var1 = sub_type_var(var1);
type_var2 = sub_type_var(var2);
if ~strcmp(type_var1,type_var2)
flag_equal = false;
if opt.flag_verbose
fprintf('%s and %s are not of the same type.\n',opt.verbose{1},opt.verbose{2})
end
else
switch type_var1
case 'char'
flag_equal = strcmp(var1,var2);
if ~flag_equal && opt.flag_verbose
fprintf('The strings %s and %s are different.\n',opt.verbose{1},opt.verbose{2})
end
case {'numeric','logical'}
if min(size(var1) == size(var2)) == 0
flag_equal = false;
if opt.flag_verbose
fprintf('The numerical arrays %s and %s do not have the same size.\n',opt.verbose{1},opt.verbose{2})
end
elseif isempty(var1)
flag_equal = true;
else
var1 = var1(:);
var2 = var2(:);
mask_nan = isnan(var1);
flag_equal = false(size(var1));
flag_equal(mask_nan) = isnan(var2(mask_nan));
flag_equal(~mask_nan) = (abs(var1(~mask_nan) - var2(~mask_nan)) <= opt.eps)|((var1(~mask_nan)==Inf)&(var2(~mask_nan)==Inf))|((var1(~mask_nan)==-Inf)&(var2(~mask_nan)==-Inf));
flag_equal = min(flag_equal);
if ~flag_equal && opt.flag_verbose
fprintf('The numerical arrays %s and %s have different values.\n',opt.verbose{1},opt.verbose{2})
end
end
case 'cell'
if min(size(var1) == size(var2)) == 0
flag_equal = false;
return
else
var1 = var1(:);
var2 = var2(:);
flag_equal = true;
for num_e = 1:length(var1)
opt_c = opt;
opt_c.verbose{1} = sprintf('%s{%i}',opt.verbose{1},num_e);
opt_c.verbose{2} = sprintf('%s{%i}',opt.verbose{2},num_e);
if ~psom_cmp_var(var1{num_e},var2{num_e},opt_c)
flag_equal = false;
if ~opt.flag_test_all
return
end
end
end
end
case 'struct'
%% compare sizes
if min(size(var1)==size(var2))==0
flag_equal = false;
return
end
var1 = var1(:);
var2 = var2(:);
if length(var1) > 1
%% if there are more than one entry, loop over all entries
flag_equal = true;
for num_e = 1:length(var1)
opt_c = opt;
opt_c.verbose{1} = sprintf('%s(%i)',opt.verbose{1},num_e);
opt_c.verbose{2} = sprintf('%s(%i)',opt.verbose{2},num_e);
flag_equal = flag_equal&&psom_cmp_var(var1(num_e),var2(num_e),opt_c);
if ~flag_equal && ~opt.flag_test_all
return
end
end
else
%% compare field names
list_fields1 = fieldnames(var1);
list_fields2 = fieldnames(var2);
if opt.flag_source_only
list_fields2 = list_fields2(ismember(list_fields2,list_fields1));
end
if (length(list_fields1)~=length(list_fields2)) || (length(unique([list_fields1 list_fields2]))~=length(list_fields1))
flag_equal = false;
if opt.flag_verbose
fprintf('The structures %s and %s do not have the same field names',opt.verbose{1},opt.verbose{2});
end
return
end
%% Compare the values of all fields
flag_equal = true;
for num_e = 1:length(list_fields1)
opt_c = opt;
opt_c.verbose{1} = sprintf('%s.%s',opt.verbose{1},list_fields1{num_e});
opt_c.verbose{2} = sprintf('%s.%s',opt.verbose{2},list_fields1{num_e});
if ~psom_cmp_var(var1.(list_fields1{num_e}),var2.(list_fields1{num_e}),opt_c)
flag_equal = false;
if ~opt.flag_test_all
return
end
end
end
end
end
end
%%%%%%%%%%%%%%%%%%
%% Subfunctions %%
%%%%%%%%%%%%%%%%%%
function type_var = sub_type_var(var)
if ischar(var)
type_var = 'char';
elseif isnumeric(var)
type_var = 'numeric';
elseif islogical(var)
type_var = 'logical';
elseif iscell(var)
type_var = 'cell';
elseif isstruct(var)
type_var = 'struct';
else
var
error('I could not determine the type of the preceeding variable')
end