-
Notifications
You must be signed in to change notification settings - Fork 50
/
test_crossvalidation_measure.m
298 lines (226 loc) · 9.25 KB
/
test_crossvalidation_measure.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
function test_suite = test_crossvalidation_measure
% tests for cosmo_crossvalidation_measure
%
% # For CoSMoMVPA's copyright information and license terms, #
% # see the COPYING file distributed with CoSMoMVPA. #
initTestSuite;
function test_crossvalidation_measure_basics
ds=cosmo_synthetic_dataset('ntargets',6,'nchunks',4);
ds.sa.targets=ds.sa.targets+10;
ds.sa.chunks=ds.sa.chunks+20;
opt=struct();
opt.partitions=cosmo_nfold_partitioner(ds);
opt.classifier=@cosmo_classify_lda;
res=cosmo_crossvalidation_measure(ds,opt);
assertEqual(res.samples,0.6250);
assertEqual(res.sa,cosmo_structjoin('labels',{'accuracy'}));
opt.output='accuracy';
res2=cosmo_crossvalidation_measure(ds,opt);
assertEqual(res,res2);
opt.output='predictions';
res3=cosmo_crossvalidation_measure(ds,opt);
assertEqual(res3.samples,10+[1 2 3 4 5 5 4 6 2 4 2 6 ...
1 2 3 4 6 6 1 3 3 4 3 1]');
assertEqual(res3.sa,ds.sa);
opt.output='accuracy_by_chunk';
res4=cosmo_crossvalidation_measure(ds,opt);
assertElementsAlmostEqual(res4.samples,[5 2 5 3]'/6);
opt.partitions=cosmo_nchoosek_partitioner(ds,2);
opt.partitions.test_indices{1}=find(ds.sa.chunks==21);
opt.partitions.test_indices{4}=find(ds.sa.chunks==22);
opt.partitions.test_indices{5}=find(ds.sa.chunks==24);
res5=cosmo_crossvalidation_measure(ds,opt);
assertElementsAlmostEqual(res5.samples,[3 NaN NaN NaN]'/6);
assertEqual(res5.sa,cosmo_structjoin('chunks',[22 NaN NaN NaN]'));
% test different classifier
opt.classifier=@cosmo_classify_nn;
opt.partitions=cosmo_nfold_partitioner(ds);
opt.output='predictions';
res6=cosmo_crossvalidation_measure(ds,opt);
assertEqual(res6.samples,10+[1 2 3 1 5 6 4 6 5 4 6 6 6 ...
2 3 4 2 5 1 2 3 4 3 1]');
% test normalization option
opt.normalization='zscore';
res7=cosmo_crossvalidation_measure(ds,opt);
assertEqual(res7.samples,10+[1 2 3 5 5 6 4 6 5 4 6 6 6 ...
2 3 4 5 5 1 5 3 1 3 1]');
% test with averaging samples
opt=rmfield(opt,'normalization');
opt.average_train_count=1;
res8=cosmo_crossvalidation_measure(ds,opt);
assertEqual(res8.samples,10+[1 2 3 1 5 6 4 6 5 4 6 6 6 ...
2 3 4 2 5 1 2 3 4 3 1]');
opt.average_train_count=2;
opt.average_train_resamplings=5;
res9=cosmo_crossvalidation_measure(ds,opt);
assertEqual(res9.samples,10+[1 2 3 4 5 6 4 6 2 4 6 6 1 ...
2 3 4 5 6 1 2 3 4 5 1]');
function test_crossvalidation_measure_exceptions
aet=@(varargin)assertExceptionThrown(@()...
cosmo_crossvalidation_measure(varargin{:}),'');
opt=struct();
opt.partitions=struct();
opt.classifier=@abs;
aet(struct,opt);
ds=cosmo_synthetic_dataset();
opt.partitions=cosmo_nfold_partitioner(ds);
opt.classifier=@cosmo_classify_lda;
aet(struct,opt)
opt.output='foo';
aet(ds,opt);
function test_balanced_accuracy()
nclasses=10;
nchunks=20;
ds=cosmo_synthetic_dataset('ntargets',nclasses,...
'nchunks',nchunks,...
'nreps',4);
% shuffle targets, use random data - assume data is unbalanced
% afterwards
ds.samples=randn(size(ds.samples));
nsamples=size(ds.samples,1);
while true
ds.sa.targets=ceil(rand(nsamples,1)*nclasses);
ds.sa.chunks=ceil(rand(nsamples,1)*nchunks);
h_t=histc(ds.sa.targets,1:nclasses);
h_c=histc(ds.sa.chunks,1:nchunks);
if numel(h_t)~=nclasses || ...
numel(h_c)~=nchunks
% classes or chunsk missing, regenerate
continue;
end
if any(h_t~=nclasses) && any(h_c~=nchunks)
% inbalance
break;
end
end
% keep subset of all partitions, so that there are missing predictions
% for some of the samples
partitions=cosmo_nfold_partitioner(ds);
nkeep=ceil(.3*nchunks);
partitions.train_indices=partitions.train_indices(1:nkeep);
partitions.test_indices=partitions.test_indices(1:nkeep);
% compute balanced accuracy
opt=struct();
opt.classifier=@cosmo_classify_nn;
opt.partitions=partitions;
% without check_partitions, an exception should be thrown as the
% partitions are supposed to be unbalanced
assertExceptionThrown(@()...
cosmo_check_partitions(partitions,ds),'');
assertExceptionThrown(@()...
cosmo_crossvalidation_measure(ds,opt),'');
opt.check_partitions=false;
% compute accuracy
opt.output='balanced_accuracy';
ba_result=cosmo_crossvalidation_measure(ds,opt);
opt.output='predictions';
pred_result=cosmo_crossvalidation_measure(ds,opt);
opt.output='accuracy';
acc_result=cosmo_crossvalidation_measure(ds,opt);
% check fields
result_cell={ba_result,acc_result};
for k=1:numel(result_cell)
result=result_cell{k};
assertEqual(sort(fieldnames(result)),sort({'samples';'sa'}));
assertEqual(fieldnames(result.sa),{'labels'});
end
assertEqual(ba_result.sa.labels,{'balanced_accuracy'});
assertEqual(acc_result.sa.labels,{'accuracy'});
% compute expected result for balanced accuracy
[unused,unused,target_idx]=unique(ds.sa.targets);
assert(max(target_idx)==nclasses);
nfolds=numel(partitions.train_indices);
correct_count=zeros(nfolds,nclasses);
class_count=zeros(1,nclasses);
all_pred=NaN(nsamples,1);
for fold_i=1:nfolds
tr_idx=partitions.train_indices{fold_i};
te_idx=partitions.test_indices{fold_i};
ds_tr=cosmo_slice(ds,tr_idx);
ds_te=cosmo_slice(ds,te_idx);
target_idx_te=target_idx(te_idx);
pred=opt.classifier(ds_tr.samples,...
ds_tr.sa.targets,...
ds_te.samples);
all_pred(te_idx)=pred;
for class_i=1:nclasses
target_msk=target_idx_te==class_i;
is_correct=pred(target_msk)==ds_te.sa.targets(target_msk);
correct_count(fold_i,class_i)=sum(is_correct);
class_count(class_i)=class_count(class_i)+numel(is_correct);
end
end
class_acc=bsxfun(@rdivide,sum(correct_count,1),class_count);
% verify expected result for balanced accuracy
assertElementsAlmostEqual(mean(class_acc),ba_result.samples);
% verify expected result for predictions of each class
assertEqual(pred_result.samples,all_pred);
assertEqual(pred_result.sa.targets,ds.sa.targets);
function test_pca()
ntargets=2;
nchunks=5;
nfeatures=ceil(rand()*10+10);
nsamples=ntargets*nchunks*4*nfeatures;
idxs=(1:nsamples)'-1;
ds=struct();
ds.samples=randn(nsamples,nfeatures);
ds.sa.targets=mod(idxs,ntargets)+1;
ds.sa.chunks=mod(floor(idxs/(ntargets*nchunks)),nchunks)+1;
test_msk=ds.sa.chunks==nchunks;
partitions=struct();
partitions.train_indices={find(~test_msk)};
partitions.test_indices={find(test_msk)};
opt=struct();
opt.partitions=partitions;
opt.classifier=@cosmo_classify_lda;
opt.output='predictions';
for count=[1 ceil(nfeatures/2) nfeatures ceil(rand()*nfeatures)]
opt_count=opt;
opt_count.pca_explained_count=count;
helper_test_pca_count(ds,opt_count,count)
end
for ratio=[.1 .5 .9 1 rand()]
opt_ratio=opt;
opt_ratio.pca_explained_ratio=ratio;
helper_test_pca_ratio(ds,opt_ratio,ratio)
end
function helper_test_pca_count(ds,opt,count)
pred_full=cosmo_crossvalidation_measure(ds,opt);
% compute results manually
[expected_pred,test_indices]=helper_pca_crossval_single_fold(ds,...
opt,count);
% compare results
assertEqual(expected_pred,...
pred_full.samples(test_indices));
function [pred,test_indices]=helper_pca_crossval_single_fold(ds,opt,count)
partitions=opt.partitions;
assert(numel(partitions.train_indices)==1);
assert(numel(partitions.test_indices)==1);
ds_train=cosmo_slice(ds,partitions.train_indices{1});
[tr_pca,params]=cosmo_pca(ds_train.samples,count);
test_indices=partitions.test_indices{1};
ds_test=cosmo_slice(ds,test_indices);
te_pca=bsxfun(@minus,ds_test.samples,params.mu)*params.coef;
pred=opt.classifier(tr_pca,ds_train.sa.targets,te_pca);
function helper_test_pca_ratio(ds,opt,ratio)
partitions=opt.partitions;
assert(numel(partitions.train_indices)==1);
ds_train=cosmo_slice(ds,partitions.train_indices{1});
[unused,params]=cosmo_pca(ds_train.samples);
count=find(cumsum(params.explained)>=ratio*100,1);
if isempty(count)
count=numel(params.explained);
end
% delegate to count helepr
helper_test_pca_count(ds,opt,count)
function test_crossvalidation_measure_pca_exceptions
aet=@(varargin)assertExceptionThrown(@()...
cosmo_crossvalidation_measure(varargin{:}),'');
ds=cosmo_synthetic_dataset();
opt=struct();
opt.classifier=@cosmo_classify_lda;
opt.partitions=cosmo_nfold_partitioner(ds);
bad_opt=opt;
bad_opt.pca_explained_count=2;
bad_opt.pca_explained_ratio=.5;
aet(ds,bad_opt);