function example_djm_unbalanced()
%{
%%%%%%%%%%%%%%%%%%%%%
djm: This shows a simple group analysis for the vt mask of the AK6 dataset.
It illustrates the issue with unbalanced samples, and three possible solutions.
(Note this code could be made more efficient be rearranging the loops, but
instead I've tried to keep it reasonably simple.)
%%%%%%%%%%%%%%%%%%%%%
%}

%% Define data
config=cosmo_config();
subs={'s01','s02','s03','s04','s05','s06','s07','s08'};
outputs={'accuracy','balanced_accuracy','acc_rebal'};
% The first two of these are possible arguments to cosmo_crossvalidation_measure
% The last one will use "accuracy" after calling "cosmo_balance_partitions"

figure(10); clf(10);

rng(0); % reset random number generator for reproducability

for balanced=[true false]
  for signal=[true false]
    situation=sprintf('Signal=%g; balanced=%g',signal,balanced);
    fprintf('\n%s',situation) % display current situation to command line

    result=nan( numel(subs), numel(outputs) ); % djm, preallocate array
    for s=1:numel(subs)
      fprintf('.')
      data_path=fullfile(config.tutorial_data_path,'ak6',subs{s});
      data_fn=fullfile(data_path,'glm_T_stats_perrun.nii');
      mask_fn=fullfile(data_path,'vt_mask.nii');
      ds=cosmo_fmri_dataset(data_fn,'mask',mask_fn,...
      'targets',repmat(1:6,1,10),... % 6 types of animal
      'chunks',floor(((1:60)-1)/6)+1); % 10 runs

      % remove constant features (due to liberal masking)
      ds=cosmo_remove_useless_data(ds);

      % Assign a function handle to the cosmo_crossvalidation_measure
      measure=@cosmo_crossvalidation_measure;

      % Make a struct containing the arguments for the measure:
      args=struct();
      args.classifier=@cosmo_classify_naive_bayes; % use naive_bayes in this example because lda insists on balanced samples
      args.partitions=cosmo_nfold_partitioner(ds);

      % if signal==false, replace real data with random data
      if ~signal, ds.samples=randn(size(ds.samples)); end

      % reassign targets into two classes, and assume chance is 0.5:
      if balanced,
        ds.sa.targets=  (ds.sa.targets<4)+1; % larger versus smaller animals
      else
        ds.sa.targets=  (ds.sa.targets<3)+1; % primates vs others
        args.check_partitions=false; % otherwise CoSMoMVPA should spot this and return a helpful error message
      endif

      for o=1:numel(outputs)

        if strcmp(outputs{o},'acc_rebal')
          args.output='accuracy';
          args.partitions=cosmo_balance_partitions(args.partitions,ds);
        else
          args.output=outputs{o};
        endif

        % Apply the measure to ds, with args as second argument.
        ds_accuracy=measure(ds,args);

        % store the result as accuracy minus chance (so positve values indicate information present)
        result(s,o)=ds_accuracy.samples-0.5;

      end % next output score
    end % next subject

    % Run ttest versus chance (now zero)
    [h, p, ci, stat]=ttest(result);

    % plot results with 95% confidence intervals
    figure(10); subplot(2,2,signal*2+balanced+1)
    errorbar(mean(result),range(ci)./2,'o')
    set(gca,'xtick',1:numel(outputs),'xticklabel',outputs)
    ylabel('accuracy minus chance')
    title(situation)
    ylim([-0.2 0.5]);
    hold on
    plot(xlim,[0 0],'--')
    drawnow

    % pause to discuss
    %keyboard

  end % signal present?
end %balanced samples?

return

