⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 erp_analysis.m

📁 绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX
💻 M
字号:
%ERP_ANALYSIS Apply PLS on the ERP data based on the information saved in
%    the datamat file.
%
%    Usage: [resultFile, elapsed_time] = ...
%	erp_analysis(ismean, ishelmert, iscontrast, isbehav, ...
%	behavdata_col, contrastdata_col, datamat_files, ...
%	num_perm, num_boot, Clim, system, ContrastFile)
%
%    see also PLS_FMRI_ANALYSIS
%

%   Called by erp_analysis_ui
%
%  INPUT:
%    ismean - 1 if select grand mean deviation.
%    ishelmert - 1 if using helmert matrix.
%    iscontrast - 1 if using contrast data.
%    isbehav - 1 if using behavior data.
%    behavdata_col - Selected column mask for Behavior Data;
%    contrastdata_col - Selected column mask for Contrast Data;
%    posthoc - posthoc data.
%    datamat_files - a cell array, one element per group.  Each element
%               in the array is another cell array contains the names of
%               session profiles for the group.
%    num_perm - number of permutations to be performed.
%    num_boot - number of bootstrap resampling to be performed.
%    Clim - upper limit of confidence interval estimated
%
%   OUTPUT FILE:
%         - file stores the information of the PLS result.
%
%   Created July 2001 by Wilkin Chau, Rotman Research Institute
%   Modified on 02-OCT-2002 by Jimmy Shen
%   Modified on Jan 10, 03 to add contrast & helmert
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [resultFile, elapsed_time] = erp_analysis(varargin)

  progress_hdl = rri_progress_ui('initialize');

  ismean = varargin{1};
  ishelmert = varargin{2};
  iscontrast = varargin{3};
  isbehav = varargin{4};
  behavdata_col = varargin{5};
  contrastdata_col = varargin{6};
  posthoc = varargin{7};
  datamat_files = varargin{8};
  save_datamat = varargin{9};
  num_perm = varargin{10};
  num_boot = varargin{11};
  Clim = varargin{12};
  system = varargin{13};
  ContrastFile = varargin{14};
  cond_selection = varargin{15};
  behavdata_lst0 = varargin{16};
  behavname0 = varargin{17};
  ismultiblock = varargin{18};
  bscan = varargin{19};

  if (nargin > 19)
     output_file = varargin{20};
     for_batch = 1;
  else
     for_batch = 0;
  end;

  datamat_files_timestamp = datamat_files;

  for i = 1:length(datamat_files)
     tmp = dir(datamat_files{i});
     datamat_files_timestamp{i} = tmp.date;
  end

  if exist('output_file','var') & ~isempty(output_file)
    resultFile = output_file;
  else

    load(datamat_files{1},'session_info');
    datamat_prefix = session_info.datamat_prefix;

    [result_file,result_path] = uiputfile([datamat_prefix '_ERPresult.mat'], ...
	'Saving PLS Result');

    if isequal(result_file,0)		% Cancel was clicked
       resultFile = [];
%       msg1 = ['WARNING: No file is saved.'];
%%       uiwait(msgbox(msg1,'Uncompleted','modal'));
       elapsed_time = 0;
       return;
    else
       resultFile = fullfile(result_path,result_file);
    end;
  end;

  % load the session info and datamat
  %
  [newdata_lst, behavdata_lst, helmertdata_lst, contrastdata_lst, ...
	subj_name_lst, num_cond_lst, num_subj_lst, common_behav, ...
	common_channels, common_conditions, common_time_info, ...
	chan_order, cond_name, behavname] = erp_get_common(datamat_files, ...
	behavdata_col, contrastdata_col, cond_selection, progress_hdl);

  if ~isempty(behavdata_lst0) & ~isempty(behavname0)
     behavdata_lst = behavdata_lst0;
     behavname = behavname0;
  end

  for i = 1:length(newdata_lst)
    if isempty(newdata_lst{i})
      return;
    end
  end

  %  start PLS Run...
  %

  perm_result = [];
  boot_result = [];
  setting2 = [];
  setting3 = [];
  setting4 = [];

  create_ver = plsgui_vernum;

  if(ismultiblock)

     isbehav = 2;

     rri_progress_ui(progress_hdl, 'Runing Multiblock PLS', 'Runing Multiblock PLS ...');

     ibehavdata_lst = behavdata_lst;

     if (num_boot > 0)
%        boot_progress = rri_progress_ui('initialize');

        [min_subj_per_group,is_boot_samples,boot_samples,new_num_boot] ...
           = rri_boot_check(num_subj_lst, num_cond_lst(1), num_boot, 0, ...
             for_batch);
%             boot_progress, for_batch);

        num_boot = new_num_boot;
     end

     if (num_perm > 0) | (num_boot == 0)
        [salience,s,designlv,behavlv,scalpscores,designscores,behavscores, ...
		lvcorrs, origpost, perm_result, datamatcorrs_lst, ...
		b_scores, behav_row_idx, behavdata_lst] = ...
		rri_multiblock_perm(ismean, ishelmert, iscontrast, isbehav, ...
		newdata_lst,num_cond_lst,num_subj_lst, ...
		ibehavdata_lst, helmertdata_lst, contrastdata_lst, ...
		num_perm, posthoc, bscan);
     end

     if (num_boot > 0)
        if num_perm == 0, origpost = []; end;
        [salience2,s2,designlv2,behavlv2,scalpscores2,designscores2, ...
		behavscores2,lvcorrs2, boot_result, datamatcorrs_lst2, ...
		b_scores2, behav_row_idx2, behavdata_lst2] = ...
		rri_multiblock_boot(ismean, ishelmert, iscontrast, isbehav, ...
		newdata_lst,num_cond_lst,num_subj_lst, ...
		ibehavdata_lst, helmertdata_lst, contrastdata_lst, ...
		num_boot, Clim, ...
		min_subj_per_group,is_boot_samples,boot_samples, ...
		new_num_boot,bscan);

        if num_perm == 0
           salience = salience2;
           s = s2;
           designlv = designlv2;
           behavlv = behavlv2;
           scalpscores = scalpscores2;
           designscores = designscores2;
           behavscores = behavscores2;
           lvcorrs = lvcorrs2;
           perm_result = [];
           datamatcorrs_lst = datamatcorrs_lst2;
           b_scores = b_scores2;
	   behav_row_idx = behav_row_idx2;
           behavdata_lst = behavdata_lst2;
        end
     end

     saved_info=['''salience'', ''s'', ''designlv'', ''behavlv'', ''scalpscores'', ', ...
		'''designscores'', ''behavscores'', ''lvcorrs'', ''origpost'', ',...
		'''perm_result'', ''boot_result'', ', ...
		'''cond_name'', ''behavname'', ''cond_selection'', ', ...
		'''common_channels'', ''b_scores'', ''bscan'', ', ...
		'''common_conditions'', ''common_time_info'', ', ...
		'''num_cond_lst'', ''num_subj_lst'', ', ...
		'''subj_name_lst'', ''ismultiblock'', ''system'', ', ...
		'''chan_order'', ''setting2'', ''setting3'', ', ...
		'''setting4'', ''datamat_files'', ''isbehav'', ', ...
		'''behavdata_lst'', ''datamatcorrs_lst'', ', ...
		'''datamat_files_timestamp'', ''create_ver'''];

     if save_datamat
         saved_info = [saved_info, ', ''newdata_lst'''];
     end

  elseif(isbehav)

     rri_progress_ui(progress_hdl, 'Runing Behavior PLS', 'Runing Behavior PLS ...');

     if (num_boot > 0)
%        boot_progress = rri_progress_ui('initialize');

        [min_subj_per_group,is_boot_samples,boot_samples,new_num_boot] ...
           = rri_boot_check(num_subj_lst, num_cond_lst(1), num_boot, 0, ...
             for_batch);
%             boot_progress, for_batch);

        num_boot = new_num_boot;
     end

     if (num_perm > 0) | (num_boot == 0)
        [salience,s,behavlv,scalpscores,behavscores,lvcorrs, ...
		origpost, perm_result, datamatcorrs_lst] = ...
		rri_analysis_perm(ismean, ishelmert, iscontrast, isbehav, ...
		newdata_lst,num_cond_lst,num_subj_lst, ...
		behavdata_lst, helmertdata_lst, contrastdata_lst, ...
		num_perm, posthoc);
     end

     if (num_boot > 0)
        if num_perm == 0, origpost = []; end;
        [salience2,s2,behavlv2,scalpscores2,behavscores2,lvcorrs2,boot_result,datamatcorrs_lst2] = ...
		rri_analysis_boot(ismean, ishelmert, iscontrast, isbehav, ...
		newdata_lst,num_cond_lst,num_subj_lst, ...
		behavdata_lst, helmertdata_lst, contrastdata_lst, ...
		num_boot, Clim, ...
		min_subj_per_group,is_boot_samples,boot_samples,new_num_boot);

        if num_perm == 0
           salience = salience2;
           s = s2;
           behavlv = behavlv2;
           scalpscores = scalpscores2;
           behavscores = behavscores2;
           lvcorrs = lvcorrs2;
           perm_result = [];
           datamatcorrs_lst = datamatcorrs_lst2;
        end
     end

     saved_info=['''salience'', ''s'', ''behavlv'', ''scalpscores'', ', ...
		'''behavscores'', ''lvcorrs'', ''origpost'', ',...
		'''perm_result'', ''boot_result'', ', ...
		'''cond_name'', ''behavname'', ''cond_selection'', ', ...
		'''common_channels'', ', ...
		'''common_conditions'', ''common_time_info'', ', ...
		'''num_cond_lst'', ''num_subj_lst'', ', ...
		'''subj_name_lst'', ''system'', ', ...
		'''chan_order'', ''setting2'', ''setting3'', ', ...
		'''setting4'', ''datamat_files'', ''isbehav'', ', ...
		'''behavdata_lst'', ''datamatcorrs_lst'', ', ...
		'''datamat_files_timestamp'', ''create_ver'''];

     if save_datamat
         saved_info = [saved_info, ', ''newdata_lst'''];
     end

  elseif(iscontrast)			% contrast analysis

     rri_progress_ui(progress_hdl, 'Runing Task PLS', 'Runing Task PLS ...');

     if isnumeric(ContrastFile)
        design = ContrastFile;
     else
        design = load(ContrastFile);
     end

     if (num_boot > 0)
%        boot_progress = rri_progress_ui('initialize');

        [min_subj_per_group,is_boot_samples,boot_samples,new_num_boot] ...
           = rri_boot_check(num_subj_lst, num_cond_lst(1), num_boot, 0, ...
             for_batch);
%             boot_progress, for_batch);

        num_boot = new_num_boot;
     else
        min_subj_per_group=[];is_boot_samples=[];boot_samples=[];new_num_boot=[];
     end

     [salience,s,designlv,scalpscores,designscores,lvintercorrs,design, ...
	perm_result,boot_result] = rri_taskpls_norotate(newdata_lst,design, ...
	num_subj_lst,num_cond_lst,num_boot,num_perm, ...
	min_subj_per_group,is_boot_samples,boot_samples,new_num_boot);

     saved_info=['''salience'', ''s'', ''designlv'', ''scalpscores'', ', ...
		'''designscores'', ''lvintercorrs'', ''design'', ', ...
		'''perm_result'', ''boot_result'', ', ...
		'''cond_name'', ''cond_selection'', ''common_channels'', ', ...
		'''common_conditions'', ''common_time_info'', ', ...
		'''num_cond_lst'', ''num_subj_lst'', ', ...
		'''subj_name_lst'', ''system'', ', ...
		'''chan_order'', ''setting2'', ''setting3'', ', ...
		'''setting4'', ''datamat_files'', ''isbehav'', ', ...
		'''datamat_files_timestamp'', ''create_ver'''];

     if save_datamat
         saved_info = [saved_info, ', ''newdata_lst'''];
     end

  else					% deviation analysis

     rri_progress_ui(progress_hdl, 'Runing Task PLS', 'Runing Task PLS ...');

     if (num_boot > 0)
%        boot_progress = rri_progress_ui('initialize');

        [min_subj_per_group,is_boot_samples,boot_samples,new_num_boot] ...
           = rri_boot_check(num_subj_lst, num_cond_lst(1), num_boot, 1, ...
             for_batch);
%             boot_progress, for_batch);

        num_boot = new_num_boot;
     end

     if (num_perm > 0) | (num_boot == 0)
        [salience,s,designlv,scalpscores,designscores,lvcorrs, ...
		origpost, perm_result] = ...
		rri_analysis_perm(ismean, ishelmert, iscontrast, isbehav, ...
		newdata_lst,num_cond_lst,num_subj_lst, ...
		behavdata_lst, helmertdata_lst, contrastdata_lst, ...
		num_perm, posthoc);
     end

     if (num_boot > 0)
        [salience2,s2,designlv2,scalpscores2,designscores2,lvcorrs2,boot_result]= ...
		rri_analysis_boot(ismean, ishelmert, iscontrast, isbehav, ...
		newdata_lst,num_cond_lst,num_subj_lst, ...
		behavdata_lst, helmertdata_lst, contrastdata_lst, ...
		num_boot, Clim, ...
		min_subj_per_group,is_boot_samples,boot_samples,new_num_boot);

        if num_perm == 0
           salience = salience2;
           s = s2;
           designlv = designlv2;
           scalpscores = scalpscores2;
           designscores = designscores2;
           lvcorrs = lvcorrs2;
           perm_result = [];
        end
     end

     saved_info=['''salience'', ''s'', ''designlv'', ''scalpscores'', ', ...
		'''designscores'', ', ...
		'''perm_result'', ''boot_result'', ', ...
		'''cond_name'', ''cond_selection'', ''common_channels'', ', ...
		'''common_conditions'', ''common_time_info'', ', ...
		'''num_cond_lst'', ''num_subj_lst'', ', ...
		'''subj_name_lst'', ''system'', ', ...
		'''chan_order'', ''setting2'', ''setting3'', ', ...
		'''setting4'', ''datamat_files'', ''isbehav'', ', ...
		'''datamat_files_timestamp'', ''create_ver'''];

     if save_datamat
         saved_info = [saved_info, ', ''newdata_lst'''];
     end

  end

  %  save results
  %
  msg = 'Saving to the disk ...';

  if exist('progress_hdl','var') & ishandle(progress_hdl)
     rri_progress_ui(progress_hdl, 'Save', msg);
  end

  if ~for_batch
     elapsed_time = toc;
     disp('RunPLS is done ...');
  end

  done = 0;

  while ~done
    try
       eval(['save(''', resultFile, ''',' saved_info,');']);
       done = 1;
    catch
       [result_file,result_path] = uiputfile('*ERPresult.mat', ...
			'Can not write file, please try again');

       if isequal(result_file,0)		% Cancel was clicked
          resultFile = [];
          msg1 = ['WARNING: No file is saved.'];
%          uiwait(msgbox(msg1,'Uncompleted','modal'));
          return;
       else
          resultFile = fullfile(result_path,result_file);
       end;
    end
  end

  return; 					% erp_analysis

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -