Skip to content
Permalink
Browse files

ENH+BK: drop support for partitioner with only chunks input, and incl…

…ude tests for balanced-ness as in other partitioners
  • Loading branch information
nno committed Jan 31, 2020
1 parent 94700a8 commit 3079b75cbbf138c8526adafae06439874e33a5e8
Showing with 29 additions and 10 deletions.
  1. +5 −4 mvpa/cosmo_oddeven_partitioner.m
  2. +24 −6 tests/test_oddeven_partitioner.m
@@ -4,8 +4,7 @@
% partitions=cosmo_oddeven_partitioner(chunks,[type])
%
% Input
% ds dataset struct with field .ds.chunks, or Px1 chunk
% indices (for P samples).
% ds dataset struct with field .ds.chunks.
% type One of:
% - 'full': two partitions are returned, training on odd
% unique chunks and testing on even unique chunks,
@@ -30,7 +29,8 @@
%
% Example:
% ds=struct();
% ds.sa.samples=NaN(6,99); % will be ignored by this function
% ds.samples=NaN(8,99); % will be ignored by this function
% ds.sa.targets=[8 9 8 9 8 9 8 9]';
% ds.sa.chunks=[1 1 2 2 6 7 7 6]';
% p=cosmo_oddeven_partitioner(ds);
% % note that chunks=6 ends up in the odd chunks and chunks=7 in the
@@ -133,7 +133,8 @@
partitions.train_indices=train_indices;
partitions.test_indices=test_indices;


% final check to make sure all is kosher (including balanced-ness)
cosmo_check_partitions(partitions,ds);

function ds=get_chunks(ds)
if isnumeric(ds) && isvector(ds)
@@ -10,11 +10,11 @@
initTestSuite;

function test_test_oddeven_partitioner_basics
for nchunks=[2 6 7 8]
for nchunks=[2 6 8]
ds=cosmo_synthetic_dataset('ntargets',3,'nchunks',nchunks);

nsamples=size(ds.samples,1);
rp=randperm(ceil(nsamples*.7));
rp=randperm(nsamples);
ds=cosmo_slice(ds,[rp rp]);

unq_chunks=unique(ds.sa.chunks);
@@ -27,15 +27,15 @@
fp.train_indices={idx_odd, idx_even};
fp.test_indices={idx_even, idx_odd};
assert_partitions_equal(fp,cosmo_oddeven_partitioner(ds,'full'));
c=ds.sa.chunks;
assert_partitions_equal(fp,cosmo_oddeven_partitioner(c,'full'));
%c=ds.sa.chunks;
%assert_partitions_equal(fp,cosmo_oddeven_partitioner(c,'full'));

hp=struct();
hp.train_indices={idx_odd};
hp.test_indices={idx_even};
assert_partitions_equal(hp,cosmo_oddeven_partitioner(ds,'half'));
c=ds.sa.chunks;
assert_partitions_equal(hp,cosmo_oddeven_partitioner(c,'half'));
%c=ds.sa.chunks;
%assert_partitions_equal(hp,cosmo_oddeven_partitioner(c,'half'));
end


@@ -69,7 +69,25 @@ function assert_cell_same_elements(p,q)
assertEqual(sort(p{k}),sort(q{k}));
end

function test_unbalanced_partitions()
ds=struct();
ntargets=randi();
nchunks=randi();
ds.samples=randn(ntargets*nchunks,1);
ds.sa.chunks=repmat(1:nchunks,1,ntargets)';
ds.sa.targets=repmat(1:ntargets,1,nchunks)';

idxs=cosmo_randperm(numel(ds.sa.chunks));
ds=cosmo_slice(ds,idxs);

% should be ok
cosmo_oddeven_partitioner(ds);

idx=ceil(rand()*ntargets*nchunks);
ds.sa.chunks(idx)=ds.sa.chunks(idx)+1;
assertExceptionThrown(@() cosmo_oddeven_partitioner(ds),'*');

function v=randi()
v=ceil(rand()*10+2);


0 comments on commit 3079b75

Please sign in to comment.
You can’t perform that action at this time.