-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpspm_load1_test.m
338 lines (338 loc) · 14.1 KB
/
pspm_load1_test.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
classdef pspm_load1_test < matlab.unittest.TestCase
% ● Description
% unittest class for the pspm_load1 function
% ● Authorship
% (C) 2015 Tobias Moser (University of Zurich)
properties (Constant)
% define testfile name
fn = 'testdata_load1.mat';
end
properties
modelfiles = {};
dummyfiles = {};
dummy_fn = '';
defaults;
end
methods(TestClassSetup)
function generate_testdata(this)
% ensure pspm_init values are set
global settings;
if isempty(settings), pspm_init; end
this.defaults = settings;
% generate aquisition data
c{1}.chantype = 'scr';
c{2}.chantype = 'hb';
pspm_testdata_gen(c, 100, this.fn);
% generate model data
model.datafile = this.fn;
model.timeunits = 'seconds';
for i = 1:length(settings.first)
if ~strcmpi(settings.first{i},'tam')
mbn = ['model_', settings.first{i}];
fn_ok = false;
j = 0;
while fn_ok == false
mfn = [mbn, num2str(j), '.mat'];
if exist(mfn, 'file') == false, fn_ok = true; end
j = j+1;
end
j = j-1;
dfn = ['dummy_',settings.first{i}, num2str(j), '.mat'];
switch settings.first{i}
case 'glm'
model.timing{1}.names = {'a';'b';'c'};
model.timing{1}.onsets = {[10, 20, 30], ...
[15, 25, 35], [18, 28, 38]};
otherwise
model.timing{1} = [10,20; 23,38; 40,70;];
model.condition{1}.name = {'a';'b'};
model.condition{1}.index = [1;2];
end
model.modelfile = mfn;
this.modelfiles{i} = mfn;
this.dummyfiles{i} = dfn;
fh = str2func(['pspm_', settings.first{i}]);
fh(model, struct());
if ~exist(mfn), keyboard; end
copyfile(this.modelfiles{i}, this.dummyfiles{i});
end
end
end
end
methods(TestClassTeardown)
function remove_testdata(this)
% Remove Testdata
if exist(pspm_load1_test.fn, 'file')
delete(pspm_load1_test.fn);
end
for i=1:numel(this.modelfiles)
if exist(this.modelfiles{i}, 'file'), delete(this.modelfiles{i}); end;
end
for i=1:numel(this.dummyfiles)
if exist(this.dummyfiles{i}, 'file'), delete(this.dummyfiles{i}); end;
end
end
end
methods
function basic_function_test(this, f, sts, mdltype)
mdl = load(f);
mdltypes = this.defaults.first;
mdt = find(ismember(mdltypes, fieldnames(mdl)));
mdt = mdltypes{mdt};
this.verifyEqual(mdl.(mdt).modeltype, mdltype, 'The returned modeltype does not match the modeltype set in the model structure.');
this.verifyEqual(sts, 1, 'Function did not complete without error.');
end
end
methods(Test)
function invalid_inputargs(this)
this.verifyWarning(@()pspm_load1(), 'ID:invalid_input');
this.verifyWarning(@()pspm_load1('some_file'), 'ID:invalid_input');
f = this.modelfiles{1};
this.verifyWarning(@()pspm_load1(f, 'unknown_action'), 'ID:unknown_action');
this.verifyWarning(@()pspm_load1(f, 'save'), 'ID:missing_data');
this.verifyWarning(@()pspm_load1(f, 'savecon'), 'ID:missing_data');
end
function invalid_model_structure_general(this)
% test with defect structure
% in general
dfn = this.dummyfiles{1};
dummy = load(dfn);
dummy_backup = dummy;
mdltypes = this.defaults.first;
mdltype = find(ismember(mdltypes, fieldnames(dummy)));
mdltype = mdltypes{mdltype};
% test missing field with model content (glm/dcm/sf)
empty = struct();
save(dfn, '-struct', 'empty');
this.verifyWarning(@()pspm_load1(dfn, 'none'), 'ID:invalid_data_structure');
% test missing fields in model data
dummy = dummy_backup;
dummy.(mdltype) = rmfield(dummy.(mdltype), 'modelfile');
dummy.(mdltype) = rmfield(dummy.(mdltype), 'modeltype');
dummy.(mdltype) = rmfield(dummy.(mdltype), 'modality');
dummy.(mdltype) = rmfield(dummy.(mdltype), 'stats');
dummy.(mdltype) = rmfield(dummy.(mdltype), 'names');
save(dfn, '-struct', 'dummy', mdltype);
% order is important (because of if / else statements)
% modelfile
this.verifyWarning(@()pspm_load1(dfn, 'none'), 'ID:invalid_data_structure');
dummy.(mdltype).modelfile = dummy_backup.(mdltype).modelfile;
save(dfn, '-struct', 'dummy', mdltype);
% modeltype
this.verifyWarning(@()pspm_load1(dfn, 'none'), 'ID:invalid_data_structure');
dummy.(mdltype).modeltype = dummy_backup.(mdltype).modeltype;
save(dfn, '-struct', 'dummy', mdltype);
% modality
this.verifyWarning(@()pspm_load1(dfn, 'none'), 'ID:invalid_data_structure');
dummy.(mdltype).modality = dummy_backup.(mdltype).modality;
save(dfn, '-struct', 'dummy', mdltype);
% stats
this.verifyWarning(@()pspm_load1(dfn, 'none'), 'ID:invalid_data_structure');
dummy.(mdltype).stats = dummy_backup.(mdltype).stats;
save(dfn, '-struct', 'dummy', mdltype);
% names
this.verifyWarning(@()pspm_load1(dfn, 'none'), 'ID:invalid_data_structure');
dummy.(mdltype).names = dummy_backup.(mdltype).names;
% restore in order to use the dummy file for the specific
% structure tests
save(dfn, '-struct', 'dummy', mdltype);
this.verifyWarningFree(@()pspm_load1(dfn, 'none'));
end
function invalid_model_structure_specific(this)
% specific structure tests
for i=1:numel(this.dummyfiles)
dfn = this.dummyfiles{i};
dummy = load(dfn);
dummy_backup = dummy;
mdltypes = this.defaults.first;
mdltype = find(ismember(mdltypes, fieldnames(dummy)));
mdltype = mdltypes{mdltype};
switch mdltype
case 'glm'
% try out glm data structure size constraints
dummy.glm.stats = [dummy.glm.stats, dummy.glm.stats];
save(dfn, '-struct', 'dummy', 'glm');
this.verifyWarning(@()pspm_load1(dfn, 'none'), 'ID:invalid_data_structure');
dummy.glm.stats = [dummy.glm.stats(:,1); dummy.glm.stats(:,2)];
save(dfn, '-struct', 'dummy', 'glm');
this.verifyWarning(@()pspm_load1(dfn, 'none'), 'ID:invalid_data_structure');
dummy = dummy_backup;
save(dfn, '-struct', 'dummy', 'glm');
options.zscored = 1;
this.verifyWarning(@()pspm_load1(dfn, 'cond', {}, options), 'ID:invalid_input');
otherwise
dummy.(mdltype).trlnames = 1:size(dummy.(mdltype).stats,1)+1;
save(dfn, '-struct', 'dummy', mdltype);
this.verifyWarning(@()pspm_load1(dfn, 'none'), 'ID:invalid_data_structure');
dummy.(mdltype) = rmfield(dummy.(mdltype), 'trlnames');
save(dfn, '-struct', 'dummy', mdltype);
this.verifyWarning(@()pspm_load1(dfn, 'none'), 'ID:invalid_data_structure');
dummy = dummy_backup;
dummy.(mdltype).names = 1:size(dummy.(mdltype).stats,2)+1;
save(dfn, '-struct', 'dummy', mdltype);
dummy = dummy_backup;
save(dfn, '-struct', 'dummy', mdltype);
this.verifyWarning(@()pspm_load1(dfn, 'recon'), 'ID:invalid_input');
end;
options.zscored = 1;
if strcmpi(mdltype, 'dcm')
this.verifyWarning(@()pspm_load1(dfn, 'none', {}, options), 'ID:invalid_input');
this.verifyWarningFree(@()pspm_load1(dfn, 'cond', {}, options));
this.verifyWarningFree(@()pspm_load1(dfn, 'stats', {}, options));
else
this.verifyWarning(@()pspm_load1(dfn, 'cond', {}, options), 'ID:invalid_input');
end
end;
end
function test_action_none(this)
for i=1:numel(this.modelfiles)
f = this.modelfiles{i};
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'none'));
this.basic_function_test(f, sts, mdltype);
this.verifyEmpty(data, 'Returned data is not empty.');
end
end
function test_action_stats(this)
for i=1:numel(this.modelfiles)
f = this.modelfiles{i};
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'stats'));
this.basic_function_test(f, sts, mdltype);
% check for fields
this.verifyTrue(isfield(data, 'stats'), 'No ''stats'' returned.');
this.verifyTrue(isfield(data, 'names'), 'No ''names'' returned.');
if ~strcmpi(mdltype, 'glm')
this.verifyTrue(isfield(data, 'trlnames'), 'No ''trlnames'' returned.');
this.verifyTrue(isfield(data, 'condnames'), 'No ''condnames'' returned.');
end
end
end
function test_action_cond(this)
for i=1:numel(this.modelfiles)
f = this.modelfiles{i};
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'cond'));
this.basic_function_test(f, sts, mdltype);
this.verifyTrue(isfield(data, 'stats'), 'No ''stats'' returned.');
this.verifyTrue(isfield(data, 'names'), 'No ''names'' returned.');
switch mdltype
case 'glm'
otherwise
this.verifyTrue(isfield(data, 'trlnames'), 'No ''trlnames'' returned.');
this.verifyTrue(isfield(data, 'condnames'), 'No ''condnames'' returned.');
end;
end;
end
function test_action_recon(this)
for i=1:numel(this.modelfiles)
f = this.modelfiles{i};
mdl = load(f);
mdltypes = this.defaults.first;
mdltype = find(ismember(mdltypes, fieldnames(mdl)));
mdltype = mdltypes{mdltype};
if strcmpi(mdltype, 'glm')
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'recon'));
this.basic_function_test(f, sts, mdltype);
this.verifyTrue(isfield(data, 'stats'), 'No ''stats'' returned.');
this.verifyTrue(isfield(data, 'names'), 'No ''names'' returned.');
end
% non-linear alternative already checked in specific
% structure test
end;
end
% run before con in order to create a con field
function test_action_savecon(this)
for i=1:numel(this.modelfiles)
f = this.modelfiles{i};
x = rand(1);
savecon = struct('test', x, 'con', 0);
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'savecon', savecon, struct()));
this.basic_function_test(f, sts, mdltype);
% check for fields
% just check if it was written, do not check for structure
% this should be done by pspm_con1_test
mdl = load(f);
this.verifyTrue(isfield(mdl.(mdltype), 'con'), 'No field ''con'' in model.');
this.verifyTrue(isfield(mdl.(mdltype).con, 'test'), 'No field ''con.test'' in model.');
this.verifyEqual(mdl.(mdltype).con.test, x, 'Test field does not contain the expected value.');
end;
end
function test_action_con(this)
for i=1:numel(this.modelfiles)
f = this.modelfiles{i};
mdl = load(f);
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'con'));
this.basic_function_test(f, sts, mdltype);
% check for fields
if isfield(mdl.(mdltype), 'con')
this.verifyTrue(isfield(data, 'test'), 'No ''con'' returned.');
end;
end;
end
function test_action_all(this)
for i=1:numel(this.modelfiles)
f = this.modelfiles{i};
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'stats'));
this.basic_function_test(f, sts, mdltype);
% check for fields
this.verifyNotEmpty(data, 'Returned data is empty.');
end
end
function test_action_save(this)
for i=1:numel(this.modelfiles)
f = this.modelfiles{i};
x = rand(1);
mdl = load(f);
mdltypes = this.defaults.first;
mdltype = find(ismember(mdltypes, fieldnames(mdl)));
mdltype = mdltypes{mdltype};
mdl.(mdltype).test = x;
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'save', mdl, struct('overwrite', 1)));
this.basic_function_test(f, sts, mdltype);
% check for fields
mdl = load(f);
this.verifyTrue(isfield(mdl.(mdltype), 'test'), 'No field ''test'' in model.');
this.verifyEqual(mdl.(mdltype).test, x, 'Test field does not contain the expected value.');
end
end
function test_options(this)
options = struct();
for i=1:numel(this.modelfiles)
f = this.modelfiles{i};
x = rand(1);
mdl = load(f);
mdltypes = this.defaults.first;
mdltype = find(ismember(mdltypes, fieldnames(mdl)));
mdltype = mdltypes{mdltype};
mdl.(mdltype).test = x;
% do overwrite
mdl.(mdltype).test = x;
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'save', mdl, struct('overwrite', 1)));
mdl = load(f);
this.verifyEqual(mdl.(mdltype).test, x);
% test zscored
if strcmpi(mdltype, 'dcm')
% zscore stats of model to compare with 'stats'
% should be equal
stats = zscore(mdl.(mdltype).stats);
options.zscored = 0;
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'stats', mdl, options));
this.basic_function_test(f, sts, mdltype);
this.verifyNotEqual(data.stats, stats, 'Not zscoring did not yield the expected value!');
options.zscored = 1;
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'stats', mdl, options));
this.basic_function_test(f, sts, mdltype);
this.verifyEqual(data.stats, stats, 'Zscore did not yield the expected value!');
% test for 'cond'
options.zscored = 0;
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'cond', mdl, options));
this.basic_function_test(f, sts, mdltype);
cond_stats = data.stats;
options.zscored = 1;
[sts, data, mdltype] = this.verifyWarningFree(@()pspm_load1(f, 'cond', mdl, options));
this.basic_function_test(f, sts, mdltype);
% ensure data is not zscored if not specified
this.verifyNotEqual(data.stats, cond_stats, 'Zscore seems to be done even if not specified so.');
end
end
end
end
end