function [stat] = sourcestatistics(cfg, varargin)

% SOURCESTATISTICS computes probabilities for a given hypothesis
% using the general linear model or using a non-parametric randomization
% approach for the statistics.
% 
% Use as
%   [stat] = sourcestatistics(cfg, source1, source2, ...)
% where the input data is the result from SOURCEANALYSIS, SOURCEDESCRIPTIVES
% or SOURCEGRANDAVERAGE.  The source structures should be spatially alligned
% to each other and should have the same positions for the source grid.
%
% The configuration can contain the following options for data selection
%   cfg.parameter          = 'pow', 'coh' or any other scalar source parameter
%
% The configuration should contain the following options for the statistics
%   cfg.approach           = string, see below
%   cfg.method             = string, see below
%
% There are two options that you have to specify for the statistical test,
% the "approach" and the "method". The approach determines which statistical
% back-end is used, the method determines which specific statiistical test
% is performed. The statistical approach XXX is described in detail in the
% function STATISTICS_XXX. The different approaches that are available are
%   cfg.approach = 'stats'         uses the Matlab statistics toolbox, mainly parametric
%   cfg.approach = 'randomization' uses randomization of the data prior to source reconstruction
%   cfg.approach = 'randcluster'   uses randomization of the data prior to source reconstruction in combination with spatial clusters
% Other approaches will become available in the near future.
%
% The different methods (e.g. ttest, anova) that are available depend
% on the general approach. See the help of STATISTICS_XXX for more details,
% where XXX should be replaced by the approach that you want to take.
%
% See also SOURCEANALYSIS, SOURCEDESCRIPTIVES, SOURCEGRANDAVERAGE

% Copyright (C) 2005, Robert Oostenveld
%
% $Log: sourcestatistics.m,v $
% Revision 1.29  2005/10/05 11:06:30  roboos
% documentation change
%
% Revision 1.28  2005/05/17 17:50:39  roboos
% changed all "if" occurences of & and | into && and ||
% this makes the code more compatible with Octave and also seems to be in closer correspondence with Matlab documentation on shortcircuited evaluation of sequential boolean constructs
%
% Revision 1.27  2005/05/12 07:20:06  roboos
% fixed backward compatibility approach/method translation for randcluster
%
% Revision 1.26  2005/05/09 14:20:02  roboos
% made the check on cfg.method optional, so that it also works when only the approach is specified
%
% Revision 1.25  2005/04/07 17:12:21  roboos
% improved the general help documentation, no code changes
%
% Revision 1.24  2005/04/06 14:29:41  roboos
% added copyrights and a log message placeholder
%

% this wrapper should be compatible with the already existing statistical
% functions that only work for source input data
if isfield(cfg, 'method')
  % call the appropriate subfunction
  if (strcmp(cfg.method, 'zero-baseline') || ...
      strcmp(cfg.method, 'nai')           || ...
      strcmp(cfg.method, 'pseudo-t')      || ...
      strcmp(cfg.method, 'difference')    || ...
      strcmp(cfg.method, 'anova1')        || ...
      strcmp(cfg.method, 'kruskalwallis'))
    % these are all statistical methods that are implemented in the old SOURCESTATISTICS_PARAMETRIC subfunction
    cfg.approach = 'parametric';
  elseif strcmp(cfg.method, 'randomization')
    cfg.approach = 'randomization';
  elseif strcmp(cfg.method, 'randcluster')
    cfg.approach = 'randcluster';
  end
end

if strcmp(cfg.approach, 'parametric')
  % use the source-specific statistical subfunction
  stat = sourcestatistics_parametric(cfg, varargin{:});
elseif strcmp(cfg.approach, 'randomization')
  % use the source-specific statistical subfunction
  stat = sourcestatistics_randomization(cfg, varargin{:});
elseif strcmp(cfg.approach, 'randcluster')
  % use the source-specific statistical subfunction
  stat = sourcestatistics_randcluster(cfg, varargin{:});
else
  % use the data-indepentend statistical wrapper function
  stat = statistics_wrapper(cfg, varargin{:});
end

