Skip to content

Commit

Permalink
ENH: make checking the dataset optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nno committed Mar 30, 2017
1 parent cbf0014 commit 656424b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
19 changes: 13 additions & 6 deletions mvpa/cosmo_phase_itc.m
Expand Up @@ -21,11 +21,14 @@
% each other
% .fa } optional feature attributes
% .a } optional sample attributes
% 'samples_are_unit_length',u (optional)
% 'samples_are_unit_length',u (optional, default=false)
% If u==true, then all elements in ds.samples
% are assumed to be already of unit length. If
% this is indeed true, this can speed up the
% computation of the output.
% 'check_dataset',c (optional, default=true)
% if c==false, there is no check for consistency
% of the ds input.
%
% Output:
% itc_ds dataset struct with fields:
Expand Down Expand Up @@ -59,9 +62,12 @@
samples=samples./abs(samples);
end

% split based on .sa.targets
[idxs,classes]=cosmo_index_unique(ds.sa.targets);
nclasses=numel(classes);
nfeatures=size(samples,2);

% allocate space for output
itc=zeros(nclasses+1,nfeatures);

% ITC for each class
Expand Down Expand Up @@ -125,12 +131,13 @@ function quick_check_some_samples_being_unit_length(samples)

function check_input(ds,opt)
% must be a proper dataset
raise_exception=true;
cosmo_check_dataset(ds,raise_exception);

% must have targets and chunks
cosmo_isfield(ds,{'sa.targets','sa.chunks'},raise_exception);
if opt.check_dataset
raise_exception=true;
cosmo_check_dataset(ds,raise_exception);

% must have targets and chunks
cosmo_isfield(ds,{'sa.targets','sa.chunks'},raise_exception);
end

% all chunks must be unique
if ~isequal(sort(ds.sa.chunks),unique(ds.sa.chunks))
Expand Down
34 changes: 18 additions & 16 deletions tests/test_phase_itc.m
Expand Up @@ -12,6 +12,7 @@
function r=randint()
r=ceil(2+rand()*10);


function test_phase_itc_basics
nclasses=randint();
classes=1:2:(2*nclasses);
Expand Down Expand Up @@ -41,6 +42,7 @@


function test_phase_itc_unit_length()
% test with 'samples_are_unit_length' option
ds=generate_random_dataset(1:10,randint(),randint());
ds_unit=ds;
ds_unit.samples=ds_unit.samples./abs(ds_unit.samples);
Expand All @@ -50,6 +52,20 @@ function test_phase_itc_unit_length()

assert_datasets_almost_equal(itc_ds,itc_unit_ds);


function test_phase_itc_sdim_field
ds=cosmo_synthetic_dataset('ntargets',3,'nchunks',3);
ds.samples=ds.samples+1i*randn(size(ds.samples));
ds.sa.chunks(:)=1:9;

% add sample dimension
ds=cosmo_dim_insert(ds,1,1,{'foo'},{[1:9]},{[1:9]'});

itc_ds=cosmo_phase_itc(ds);
assert(~isfield(itc_ds.a,'sdim'));
assert(~isfield(itc_ds.sa,'foo'));


function assert_datasets_almost_equal(p,q)
assertElementsAlmostEqual(p.samples,q.samples);

Expand All @@ -59,8 +75,6 @@ function assert_datasets_almost_equal(p,q)

assertEqual(p,q);



function ds=generate_random_dataset(classes,nrepeats,nfeatures)
nclasses=numel(classes);
nsamples=nclasses*nrepeats;
Expand All @@ -76,26 +90,14 @@ function assert_datasets_almost_equal(p,q)
ds=cosmo_slice(ds,cosmo_randperm(nsamples));


function test_phase_itc_sdim_field
ds=cosmo_synthetic_dataset('ntargets',3,'nchunks',3);
ds.samples=ds.samples+1i*randn(size(ds.samples));
ds.sa.chunks(:)=1:9;

% add sample dimension
ds=cosmo_dim_insert(ds,1,1,{'foo'},{[1:9]},{[1:9]'});

itc_ds=cosmo_phase_itc(ds);
assert(~isfield(itc_ds.a,'sdim'));
assert(~isfield(itc_ds.sa,'foo'));



function itc=quick_itc(samples)
s=samples./abs(samples);
itc=abs(mean(s,1));


function test_phase_itc_exceptions

function test_phase_itc_exceptions()
aet=@(varargin)assertExceptionThrown(...
@()cosmo_phase_itc(varargin{:}),'');

Expand Down

0 comments on commit 656424b

Please sign in to comment.