test meeg chantype

function test_suite = test_meeg_chantype
    % tests for cosmo_meeg_chantype
    %
    % #   For CoSMoMVPA's copyright information and license terms,   #
    % #   see the COPYING file distributed with CoSMoMVPA.           #
    try % assignment of 'localfunctions' is necessary in Matlab >= 2016
        test_functions = localfunctions();
    catch % no problem; early Matlab versions can use initTestSuite fine
    end
    initTestSuite;

function test_meeg_chantype_
    if cosmo_skip_test_if_no_external('fieldtrip')
        return
    end

    props = get_senstype_properties();
    n = numel(props);
    for k = 1:n
        prop = props{k};
        name = prop{1};
        nchan = prop{2};
        chan_labels = prop{3};
        chan_mapping = prop{4};

        sizes = {'small', 'big'};
        for j = 1:2
            size_ = sizes{j};
            ds = cosmo_synthetic_dataset('type', 'meeg', 'sens', name, ...
                                         'size', size_, ...
                                         'ntargets', 1, 'nchunks', 1);
            nf = numel(ds.a.fdim.values{1});
            if strcmp(size_, 'big')
                assertEqual(nf, nchan);
            end
            [tp, mp] = cosmo_meeg_chantype(ds);

            if ischar(chan_labels)
                assertEqual(unique(tp), {chan_labels});
            else
                chan_rep = repmat(chan_labels(:), ...
                                  ceil(nchan / numel(chan_labels)), 1);
                assertEqual(tp(:), chan_rep(1:nf));
            end

            for m = 1:numel(chan_mapping)
                cm = chan_mapping{m};
                assertEqual(mp.(cm{1}), cm{2});
            end
        end
    end

function props = get_senstype_properties()
    props = { {'neuromag306_all', 306, ...
               {'meg_axial', 'meg_planar', 'meg_planar'}, ...
               {{'meg_axial', 'neuromag306alt_mag'}, ...
                {'meg_planar', 'neuromag306alt_planar'}}}, ...
             {'neuromag306_planar', 204, ...
              'meg_planar', ...
              {{'meg_planar', 'neuromag306alt_planar'}}}, ...
             {'neuromag306_planar_combined', 102, ...
              'meg_planar_combined', ...
              {{'meg_planar_combined', ...
                'neuromag306alt_planar_combined'}}}, ...
             {'ctf151', 151, ...
              'meg_axial', ...
              {{'meg_axial', 'ctf151'}}}, ...
             {'ctf151_planar', 302, ...
              'meg_planar', ...
              {{'meg_planar', 'ctf151_planar'}}}, ...
             {'ctf151_planar_combined', 151, ...
              'meg_axial', ...
              {{'meg_axial', 'ctf151'}}}, ...
             {'4d148', 148, ...
              'meg_axial', ...
              {{'meg_axial', 'bti148'}}}, ...
             {'4d148_planar', 296, ...
              'meg_planar', ...
              {{'meg_planar', 'bti148_planar'}}}, ...
             {'4d148_planar_combined', 148, ...
              'meg_axial', ...
              {{'meg_axial', 'bti148'}}, false}, ... % not essential
             {'4d248', 248, ...
              'meg_axial', ...
              {{'meg_axial', 'bti248'}}}, ...
             {'4d248_planar', 496, ...
              'meg_planar', ...
              {{'meg_planar', 'bti248_planar'}}}, ...
             {'4d248_planar_combined', 248, ...
              'meg_axial', ...
              {{'meg_axial', 'bti248'}}, false}, ... % not essential
             {'yokogawa64_planar_combined', 64, ...
              'meg_axial', ...
              {{'meg_axial', 'yokogawa64'}}}, ...
             {'yokogawa64', 64, ...
              'meg_axial', ...
              {{'meg_axial', 'yokogawa64'}}}, ...
             {'yokogawa64_planar', 128, ...
              'meg_planar', ...
              {{'meg_planar', 'yokogawa64_planar'}}}, ...
             {'yokogawa64_planar_combined', 64, ...
              'meg_axial', ...
              {{'meg_axial', 'yokogawa64'}}}, ...
             {'eeg1020', 29, ...
              'eeg', ...
              {{'eeg', 'eeg1020'}}}, ...
             {'eeg1010', 94, ...
              'eeg', ...
              {{'eeg', 'eeg1010'}}}, ...
             {'eeg1005', 343, ...
              'eeg', ...
              {{'eeg', 'eeg1005'}}} ...
            };

    % these properties were generated by the following code:
    %
    % props={{'neuromag306',{'all','planar','planar_combined'}},...
    %         {'ctf151',{'','planar','planar_combined'}},...
    %         {'4d148',{'','planar','planar_combined'}},...
    %         {'4d248',{'','planar','planar_combined'}},...
    %         {'yokogawa64',{'','planar','planar_combined'}},...
    %         {'yokogawa64',{'','planar','planar_combined'}}};
    % n=numel(props);
    %
    % for k=1:n
    %     prop=props{k};
    %     name=prop{1};
    %     sens=prop{2};
    %
    %     opt=struct();
    %     opt.sens=sens;
    %     opt.type='meeg';
    %     opt.size='big';
    %
    %     cp=cosmo_cartprod(opt);
    %     for j=1:numel(cp)
    %         x=cp{j};
    %
    %         if isempty(x.sens)
    %             x.sens=name;
    %         else
    %             x.sens=[name '_' x.sens];
    %         end
    %         ds=cosmo_synthetic_dataset(x);
    %         nchan=numel(ds.a.fdim.values{1});
    %         [tp,mp]=cosmo_meeg_chantype(ds);
    %
    %         utp=unique(tp);
    %         if numel(utp)==1
    %             s=['''' tp{1} ''''];
    %         else
    %             tps=tp(1:3);
    %             s=['{''' cosmo_strjoin(tps,''', ''') '''}'];
    %         end
    %
    %
    %         t=cellfun(@(x)sprintf('{''%s'',''%s''}',x,mp.(x)),utp,...
    %                     'UniformOutput',false);
    %
    %         fprintf('{''%s'',%d,...\n\t%s,...\n\t%s},...\n',...
    %             x.sens,nchan,s,['{' cosmo_strjoin(t,',...\n\t') '}']);
    %
    %
    %     end
    % end