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

📄 fmri_convert_task.m

📁 绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX
💻 M
📖 第 1 页 / 共 3 页
字号:
function [ContrastFile,SessionProfiles,b_scores,boot_result,brainlv, ...
   cond_name,cond_selection,create_ver,d_scores,designlv,lv_evt_list, ...
   num_conditions,num_subj_lst,perm_result,s,st_coords,st_datamat, ...
   st_dims,st_origin,st_voxel_size,st_win_size,subj_group,subj_name] = ...
	fmri_convert_task(oldfile)

   load(oldfile);

   if ~exist('cond_name','var') | ~exist('num_conditions','var')
      load(SessionProfiles{1}{1});
      cond_name = session_info.condition;
      num_conditions = length(cond_name);
   end

   if ~exist('cond_selection','var')
      cond_selection = ones(1,num_conditions);
   end

%   h0 = figure('Position',[500,500,1,1], ...
%	'NumberTitle', 'off', ...
%   	'Menubar','none', ...
%   	'ToolBar','none');

   total_profiles = ValidateProfiles(SessionProfiles);
   progress_hdl = rri_progress_status('','Create','Converting result file...');
   progress_merge_factor = 1 / (total_profiles * 2); 
   setappdata(progress_hdl,'ProgressScale',progress_merge_factor * 0.2);
   setappdata(progress_hdl,'ProgressStart',0);

   [st_datamat, d_scores, lv_evt_list, num_subj_lst, subj_name] = ...
	convert_fmri_pls_analysis(SessionProfiles,[],0,0, ...
				95,[],1,1, cond_selection,{},[],{});

   subj_group = num_subj_lst;
   create_ver = plsgui_vernum;

   close(progress_hdl);
%   close(h0);
%   clear h0 total_profiles progress_hdl progress_merge_factor oldfile;

%   save(newfile);

   return;
   

%-----------------------------------------------------------------------------
function [st_datamat, d_scores, lv_evt_list, num_subj_lst, subj_name] = ...
	convert_fmri_pls_analysis(varargin)
%
%  USAGE: 
%          fmri_pls_analysis
%    or    fmri_pls_analysis(SessionProfiles,ContrastFile, ...
%                              num_perm,num_boot,grp_analysis_flg,output_file)
%
%    Apply PLS on the fMRI data based on the session information saved in
%    the "sessionFile".  Assume session information, and st_datamat have 
%    been created.
%
%
%  INPUT:
%    SessionProfiles - 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.
%    ContrastFile - the contrast file to be used to generate the design matrix.
%                   Helmert matrix will be specified using the string of
%                   'HELMERT'.  If contrast is empty, use deviation from 
%                   grand mean for the contrast.
%    num_perm - number of permutations to be performed.
%    num_boot - number of bootstrap resampling to be performed.
%    grp_analysis_flg - flag indicates group analysis.
%    output_file - (optional) the name of output file
%
%                  
%  OUTPUT FILE:
%         - file stores the information of the PLS result.
%
%  NOTE:
%    To create session information, use 'pls_input_session_info'.
%    To create st_datamat, use 'fmri_gen_datamat', 'fmri_combine_coords',
%       and 'fmri_gen_st_datamat'.
%
%  Script needed: fmri_perm_test.m, fmri_deviation_perm_test.m
%
%   -- Created July 2001 by Wilkin Chau, Rotman Research Institute
%

  if (nargin == 0),
     [SessionProfiles,ContrastFile,num_perm,group_analysis] = options_query;
  else
     SessionProfiles = varargin{1};
     ContrastFile = varargin{2};
     num_perm = varargin{3};
     num_boot = varargin{4};
     Clim = varargin{5};
     posthoc = varargin{6};
     save_datamat = varargin{7};
     group_analysis = varargin{8};
     cond_selection = varargin{9};
     behavname = varargin{10};
     behavdata = varargin{11};
     behavdata_lst = varargin{12};
     if (nargin > 12)
        output_file = varargin{13};
     end;
  end;

if(0)
  session_files_timestamp = SessionProfiles;
  datamat_files_timestamp = SessionProfiles;

  for i = 1:length(SessionProfiles)
     for j = 1:length(SessionProfiles{i})
        tmp = dir(SessionProfiles{i}{j});
        session_files_timestamp{i}{j} = tmp.date;

        load(SessionProfiles{i}{j},'session_info');
        datamat_prefix = session_info.datamat_prefix;

        if findstr('BfMRIsession.mat', SessionProfiles{i}{j})
           datamat_file = [datamat_prefix,'_BfMRIdatamat.mat'];
        else
           datamat_file = [datamat_prefix,'_fMRIdatamat.mat'];
        end

        tmp = dir(datamat_file);
        datamat_files_timestamp{i}{j} = tmp.date;

        if datenum(session_files_timestamp{i}{j}) > datenum(datamat_files_timestamp{i}{j})
%           msg = ['Datamat file "', datamat_file, '" is older than session '];
%           msg = [msg, 'file "', SessionProfiles{i}{j}, '". So the datamat file '];
%           msg = [msg, 'needs to be re-generated.'];

%           uiwait(msgbox(msg, 'Error', 'modal'));

           msg1 = ['Datamat file "', datamat_file, '" is older than session '];
           msg1 = [msg1, 'file "', SessionProfiles{i}{j}, '".'];
           msg2 = 'If you believe that the session file is just touched (e.g. due to copy) but not modified, you can click "Proceed".';
           msg3 = 'Otherwise, please click "Stop", and re-generate the datamat file.';

           quest = questdlg({msg1 '' msg2 '' msg3 ''}, 'Choose','Proceed','Stop','Stop');

           if strcmp(quest,'Stop')
              return;
           end
        end
     end
  end

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

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

    if findstr('BfMRIsession.mat', fn)
       [result_file,result_path] = ...
		uiputfile([datamat_prefix,'_BfMRIresult.mat'],'Saving PLS Result');
    else
       [result_file,result_path] = ...
		uiputfile([datamat_prefix,'_fMRIresult.mat'],'Saving PLS Result');
    end

    if isequal(result_file,0)			% Cancel was clicked
%%       if findstr('BfMRIsession.mat', fn)
%%          resultFile = 'BfMRIresult.mat';
%%       else
%%          resultFile = 'fMRIresult.mat';
%%       end
%
%       msg1 = ['WARNING: No file is saved.'];
%       msgbox(msg1,'Uncompleted');
%       resultFile = [];
%       disp('ERROR: Result file is not saved.');
       return;
    else
       resultFile = fullfile(result_path,result_file);
    end;
  end;
end

  progress_hdl = ShowProgress('initialize');

  if isempty(ContrastFile),                         % none: use deviation
     ContrastMethod = 1;
  elseif strcmp('HELMERT',upper(ContrastFile))      % use Helmert matrix
     ContrastMethod = 2;
  elseif strcmp('BEHAV',upper(ContrastFile))        % behav
     ContrastMethod = 4;
  else
     ContrastMethod = 3;                            % design with contrast file
  end;

  if isempty(behavdata)
     [st_datamat,st_coords,st_dims,num_conditions,st_evt_list, ...
	st_win_size,st_voxel_size,st_origin,subj_group,behavdata,behavname, ...
		subj_name, cond_name, num_behav_subj, ...
		behavdata_lst, newdata_lst, num_subj_lst ] = ...
		concat_st_datamat(SessionProfiles,progress_hdl,ContrastMethod, ...
		posthoc, cond_selection, group_analysis);
  else
     [st_datamat,st_coords,st_dims,num_conditions,st_evt_list, ...
	st_win_size,st_voxel_size,st_origin,subj_group, ...
		subj_name, cond_name, num_behav_subj, ...
		newdata_lst, num_subj_lst ] = ...
		concat_st_datamat2(SessionProfiles,progress_hdl,ContrastMethod, ...
		posthoc, behavdata, cond_selection);
  end

  if (group_analysis == 0)
     subj_group = [];		% for nongroup analysis
  end;

  if isempty(st_datamat),
	return;
  end;

  %  get the contrast 
  %
  switch  (ContrastMethod) 
     case {1}
        USE_DEVIATION_PERM_TEST = 1;
	isbehav = 0;
        ContrastFile = 'NONE';
     case {2}
        USE_DEVIATION_PERM_TEST = 0;
	isbehav = 0;
        contrasts = rri_helmert_matrix(num_conditions);
     case {3}
        USE_DEVIATION_PERM_TEST = 0;
	isbehav = 0;
        contrasts = load(ContrastFile);
     case {4}
        USE_DEVIATION_PERM_TEST = 0;
	isbehav = 1;
        contrasts = behavdata;
  end;

  create_ver = plsgui_vernum;

  %  start permutation PLS ...
  %

  perm_result = [];
  boot_result = [];
  if (USE_DEVIATION_PERM_TEST)		% mean

     if (num_boot > 0),
        num_rep = length(st_evt_list) / num_conditions;

%        boot_progress = rri_progress_ui('initialize');

        if isempty(subj_group)
           [min_subj_per_group,is_boot_samples,boot_samples,new_num_boot] ...
              = rri_boot_check(num_rep, 1, num_boot, 1);
%                boot_progress);
        else
           [min_subj_per_group,is_boot_samples,boot_samples,new_num_boot] ...
              = rri_boot_check(subj_group, num_conditions, num_boot, 1);
%                boot_progress);
        end;

        num_boot = new_num_boot;
     end;

     if (num_perm > 0) | (num_boot == 0),
        [brainlv,s,designlv,b_scores,d_scores,perm_result,lv_evt_list] = ...
           fmri_deviation_perm_test(st_datamat,num_conditions,st_evt_list, ...
                                   num_perm,subj_group);
     end;

     if (num_boot > 0),
        [brainlv2,s2,designlv2,b_scores2,d_scores2,boot_result,lv_evt_list2] = ...
           fmri_deviation_boot_test(st_datamat,num_conditions,st_evt_list, ...
                                   num_boot, subj_group, ...
         min_subj_per_group,is_boot_samples,boot_samples,new_num_boot);

        if num_perm == 0
           brainlv = brainlv2;
           s = s2;
           designlv = designlv2;
           b_scores = b_scores2;
           d_scores = d_scores2;
           lv_evt_list = lv_evt_list2;
           perm_result = [];
        end
     end;

     saved_info=['brainlv s designlv perm_result boot_result st_coords ', ...
  	         'st_dims lv_evt_list st_win_size st_voxel_size st_origin ', ...
		 'SessionProfiles ContrastFile b_scores d_scores ', ...
		 'subj_group num_conditions cond_name cond_selection ', ...
		 'num_subj_lst subj_name session_files_timestamp ', ...
		 'datamat_files_timestamp create_ver'];

     if save_datamat & ~isempty(brainlv)
        first = 1;
        last = 0;
        grp_datamat = [];
        for g = 1:length(num_subj_lst)
           last = last + num_conditions*num_subj_lst(g);
           [tmp idx] = sort(st_evt_list(first:last));
           tmp = st_datamat(first:last,:);
           grp_datamat = [grp_datamat; tmp(idx,:)];
           first = last + 1;
        end;
        
        st_datamat = grp_datamat;
        saved_info = [saved_info, ' st_datamat'];
     end

  else					% behav & no rotate

     if (isbehav)			% Behavior Analysis

        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_conditions, num_boot, 0);
%                boot_progress);

           num_boot = new_num_boot;
        end;

        if (num_perm > 0) | (num_boot == 0),
           [brainlv,s,behavlv,brainscores,behavscores,lvcorrs, ...
		origpost,perm_result,behavdata,lv_evt_list,datamatcorrs_lst] = ...
			fmri_perm_behav(st_datamat,contrasts,st_evt_list, ...
			behavdata_lst, newdata_lst, num_subj_lst, ...
			num_perm,num_conditions,num_behav_subj,posthoc);
        end;

        if (num_boot > 0),
           if num_perm == 0, origpost = []; end;
           [brainlv2,s2,behavlv2,brainscores2,behavscores2,lvcorrs2, ...
		boot_result,behavdata,lv_evt_list2,datamatcorrs_lst2] = ...
			fmri_boot_behav(st_datamat,contrasts,st_evt_list, ...
			behavdata_lst, newdata_lst, num_subj_lst, ...
			num_boot,num_conditions,num_behav_subj,Clim, ...
                        min_subj_per_group,is_boot_samples,boot_samples,new_num_boot);

           if num_perm == 0
              brainlv = brainlv2;
              s = s2;
              behavlv = behavlv2;
              brainscores = brainscores2;
              behavscores = behavscores2;
              lvcorrs = lvcorrs2;
              lv_evt_list = lv_evt_list2;
              datamatcorrs_lst = datamatcorrs_lst2;
              perm_result = [];
           end
        end;

        saved_info=['brainlv s behavlv brainscores behavscores lvcorrs ', ...
			'origpost perm_result boot_result st_coords ', ...
			'behavdata behavname datamatcorrs_lst ', ...
			'num_conditions subj_name cond_name cond_selection ', ...
			'st_dims lv_evt_list st_win_size st_voxel_size ', ...
			'subj_group behavdata_lst num_subj_lst ', ...
			'st_origin SessionProfiles ContrastFile ', ...
			'session_files_timestamp datamat_files_timestamp ', ...
			'create_ver'];

        if save_datamat & ~isempty(brainlv)
            first = 1;
            last = 0;
            grp_datamat = [];
            for g = 1:length(num_subj_lst)
               last = last + num_conditions*num_subj_lst(g);
               [tmp idx] = sort(st_evt_list(first:last));
               tmp = st_datamat(first:last,:);
               grp_datamat = [grp_datamat; tmp(idx,:)];
               first = last + 1;
            end;
        
            st_datamat = grp_datamat;
            saved_info = [saved_info, ' st_datamat'];
        end

     else

        if (num_boot > 0),
           num_rep = length(st_evt_list) / num_conditions;

%           boot_progress = rri_progress_ui('initialize');

           if isempty(subj_group)
              [min_subj_per_group,is_boot_samples,boot_samples,new_num_boot] ...
                 = rri_boot_check(num_rep, 1, num_boot, 0);
%                   boot_progress);
           else
              [min_subj_per_group,is_boot_samples,boot_samples,new_num_boot] ...
                 = rri_boot_check(subj_group, num_conditions, num_boot, 0);
%                   boot_progress);
           end

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

        [brainlv,s,designlv,b_scores,d_scores,lvintercorrs,design, ...
            perm_result,boot_result,lv_evt_list] = ...
		fmri_taskpls_norotate(st_datamat,contrasts,num_conditions,...
			st_evt_list,num_boot,num_perm,subj_group, ...
         min_subj_per_group,is_boot_samples,boot_samples,new_num_boot);

        saved_info=['brainlv s designlv perm_result boot_result st_coords ', ...
  	         'st_dims lv_evt_list st_win_size st_voxel_size st_origin ', ...
                 'SessionProfiles ContrastFile b_scores d_scores design ', ...
		 'subj_group num_conditions cond_name cond_selection ', ...
		 'num_subj_lst subj_name lvintercorrs design ', ...
		 'session_files_timestamp datamat_files_timestamp create_ver'];

        if save_datamat & ~isempty(brainlv)
            first = 1;
            last = 0;
            grp_datamat = [];
            for g = 1:length(num_subj_lst)
               last = last + num_conditions*num_subj_lst(g);
               [tmp idx] = sort(st_evt_list(first:last));
               tmp = st_datamat(first:last,:);
               grp_datamat = [grp_datamat; tmp(idx,:)];
               first = last + 1;
            end;
        
            st_datamat = grp_datamat;
            saved_info = [saved_info, ' st_datamat'];
        end

     end

  end;

  %  Requested by Nancy, because they no longer use "dp" or "dprob" to evaluate
  %  designlv
  %
  if ~isempty(perm_result) & ~isbehav
     perm_result = rmfield(perm_result, 'dp');
     perm_result = rmfield(perm_result, 'designlv_prob');
  end

  if exist('progress_hdl','var') & ishandle(progress_hdl)

     progress_bar = getappdata(gcf,'ProgressBar');
     set(progress_bar,'Position',[0 0 1 1],'Visible','on');

  end

if(0)
  if isempty(brainlv)
     done = 1;
  else
     done = 0;
  end

  while ~done
    try
       eval(['save ''' resultFile ''' ' saved_info]);
       done = 1;
    catch
       if findstr('BfMRIsession.mat', fn)
          [result_file,result_path] = uiputfile('*BfMRIresult.mat', ...
			'Can not write file, please try again',350,150);
       else
          [result_file,result_path] = uiputfile('*fMRIresult.mat', ...
			'Can not write file, please try again',350,150);
       end

       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;

⌨️ 快捷键说明

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