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

📄 fmri_convert_task.m

📁 绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX
💻 M
📖 第 1 页 / 共 3 页
字号:
    end
  end
end

  return; 					% fmri_pls_analysis


%-------------------------------------------------------------------------
function contrasts = load_contrast_file(contrastFile)

  load(contrastFile);

  num_contrasts = length(pls_contrasts);
  num_conditions = length(pls_contrasts(1).value);

  contrasts = zeros(num_conditions,num_contrasts);
  for i=1:num_contrasts,
     contrasts(:,i) = pls_contrasts(i).value';
  end;

  return;					% get_contrast


%-------------------------------------------------------------------------
function  [SessionProfiles,ContrastFile,num_perm,single_subj] = options_query()

  %  get the session profiles
  %
  num_session = 0;
  not_done = 1;
  while (not_done)
     msg = [ '\nEnter the name of file contains the session information ', ...
             '(Press <Enter> when done): '];
     sessionFile = input(msg,'s');

     if (isempty(sessionFile))
        not_done = 0;
     else
        if sessionFile(1) ~= '/',
            sessionFile = sprintf('%s/%s',pwd,sessionFile);
        end;
        num_session = num_session + 1;
        SessionProfiles{num_session} = sessionFile;
     end;
  end;


  msg = ['\nEnter the contrast file: ', ...
         '\n   (Type "HELMERT" for Helmert matrix or ', ...
         '\n    press <Enter> to use deviation from grand mean) '];
  ContrastFile = input(msg,'s');

  %  get the number of iterations for the permutation test 
  %
  num_perm = input('\nNumber of permutations: ');


  %  single subject analysis ?
  %
  single_subj_ans=input('\nDoes the data come from single subject? [Y/N] ','s');
  if (upper(single_subj_ans) == 'Y')
     single_subj = 1;
  else
     single_subj = 0;
  end;

  return;					% options_query


%-------------------------------------------------------------------------
function hdl = ShowProgress(progress_hdl,info)

  %  'initialize' - return progress handle if any
  %
  if ischar(progress_hdl) & strcmp(lower(progress_hdl),'initialize'),
     if ~isempty(gcf) & isequal(get(gcf,'Tag'),'ProgressFigure'),
         hdl = gcf;
     else
         hdl = [];
     end;
     return;
  end;


  if ~isempty(progress_hdl)
     if ischar(info)
         rri_progress_status(progress_hdl,'Show_message',info);
     else
         rri_progress_status(progress_hdl,'Update_bar',info);
     end;
     return;
  end;

  if ischar(info),
     disp(info)
  end;

  return;					% ShowProgress


%-------------------------------------------------------------------------
function [new_st_datamat, new_st_coords, st_dims, num_conditions, new_evt_list, ...
	    win_size, voxel_size, origin, subj_group, ...
			subj_name, curr_conditions, num_behav_subj, ...
			newdata_lst, num_subj_lst ] = ...
    			concat_st_datamat2(SessionProfiles,progress_hdl,ContrastMethod, ...
			posthoc, behavdata, cond_selection)
%
%
  newdata_lst = {};
  num_subj_lst = [];

  new_st_datamat = [];			%  stacked datamat
  new_st_coords = [];
  st_dims = [];
  num_conditions = [];
  new_evt_list = [];
  new_evt_list_lst = {};
  win_size = [];
  voxel_size = [];
  origin = [];
  subj_group = [];
  subj_name = {};
  curr_conditions = {};
  num_behav_subj = [];
  num_behavdata_col = size(behavdata,2);

  msg = sprintf('Merging datamats ...');
  ShowProgress(progress_hdl,msg);

  num_groups = length(SessionProfiles);

  profile_list = [];
  session_group = zeros(1,num_groups);
  for i=1:num_groups,
     session_group(i) = length(SessionProfiles{i});
     profile_list = [profile_list; SessionProfiles{i}];
  end;
  num_profiles = length(profile_list);

  st_info = cell(1,num_profiles);

  %  get the coords ...
  %
  total_evts = 0;
  subj_group = zeros(1,num_groups);
  cnt = 0;

  fn = SessionProfiles{1}{1};

  for i=1:num_groups,
    for j=1:session_group(i),
       cnt = cnt+1;

       ShowProgress(progress_hdl,cnt/(num_profiles*10));
       msg = sprintf('Loading datamat: %d out of %d',cnt,num_profiles);
       ShowProgress(progress_hdl,msg);

       sessionFile = profile_list{cnt};
       load(sessionFile);
       curr_conditions = session_info.condition;

        %  get the st_datamat and st_evt_list 
        %
        datamat_prefix = session_info.datamat_prefix;

        if findstr('BfMRIsession.mat', fn)
           st_datamatFile = sprintf('%s_BfMRIdatamat.mat',datamat_prefix);
        else
           st_datamatFile = sprintf('%s_fMRIdatamat.mat',datamat_prefix);
        end

%        st_datamatFile = fullfile(session_info.pls_data_path,st_datamatFile);

        curr = pwd;
        if isempty(curr)
           curr = filesep;
        end

        st_datamatFile = fullfile(curr,st_datamatFile);
   
	try
           warning off;

           load(st_datamatFile,'st_coords','st_dims','st_evt_list', ...
			'st_win_size','st_voxel_size','st_origin');
           warning on;
	catch
	   disp(sprintf('ERROR: cannot open data file: %s',st_datamatFile));
	   new_st_datamat = [];
	   return;   
	end;

        st_info{cnt}.sessionFile = sessionFile;
        st_info{cnt}.datamatFile = st_datamatFile;
        st_info{cnt}.num_cond = session_info.num_conditions;
        st_info{cnt}.coords = st_coords;
        st_info{cnt}.dims = st_dims;
        st_info{cnt}.evt_list = st_evt_list;
        st_info{cnt}.win_size = st_win_size;
        st_info{cnt}.voxel_size = st_voxel_size;
        st_info{cnt}.origin = st_origin;

        num_evts = length(st_evt_list);
        total_evts = total_evts + num_evts;
        subj_group(i) = subj_group(i) + num_evts/length(curr_conditions);
   
        if (cnt > 1),	% make sure the st_datamat are compatible
           if (st_info{cnt-1}.dims ~= st_info{cnt}.dims),
              msg = 'The datamats have different volume dimension.';
              ShowProgress(progress_hdl,['ERROR: ', msg]);
disp(msg);
              return;
           end;
   
           if (st_info{cnt-1}.win_size ~= st_info{cnt}.win_size),
              msg = 'The datamats have different window size.';
              ShowProgress(progress_hdl,['ERROR: ', msg]);
disp(msg);
              return;
           end;

           if (st_info{cnt-1}.dims ~= st_info{cnt}.dims),
              msg = 'The datamats have different volume dimension.';
              ShowProgress(progress_hdl,['ERROR: ', msg]);
disp(msg);
              return;
           end;

           if (st_info{cnt-1}.voxel_size ~= st_info{cnt}.voxel_size),
              msg = 'The datamats have different voxel size.';
              ShowProgress(progress_hdl,['ERROR: ', msg]);
disp(msg);
              return;
           end;
   
           if ~isequal(curr_conditions,prev_conditions)
              msg='The datamats are created from different conditions.';
              ShowProgress(progress_hdl,['ERROR: ', msg]);
disp(msg);
              return;
           end;
        end;  % (cnt > 1)
   
        prev_conditions = curr_conditions;
   
    end;
  end;

  clear st_datamat;

  num_conditions = length(curr_conditions);  
  st_dims = st_info{1}.dims; 

  %  determine the common coords ...
  %
  m = zeros(1,prod(st_info{1}.dims));
  for i=1:num_profiles
     m(st_info{i}.coords) = m(st_info{i}.coords) + 1;
  end;
  new_st_coords = find(m == num_profiles);

  if isempty(new_st_coords)
     disp('ERROR: no common coords among datamats!');
     new_st_datamat = [];
     return;
  end

  %  stack the st_datamat together
  %
  win_size = st_info{1}.win_size;
  voxel_size = st_info{1}.voxel_size;

  if isempty(st_info{1}.origin) | isequal(st_info{1}.origin,[0 0 0]),
     origin = floor(st_dims([1 2 4])/2); 
  else
     origin = st_info{1}.origin;
  end;

  num_voxels = length(new_st_coords);
  new_st_datamat = zeros(total_evts,num_voxels*win_size);
  first_row = 1;

  first_cond_order = [];

  %  go through each subject, which is represented by each profile
  %
%  for i=1:num_profiles,

  tmp_new_st_datamat = [];
  cnt=0;

  for i=1:num_groups,

    grp_tmp_new_st_datamat = [];
    grp_tmp_new_evt_list = [];
    grp_tmp_new_evt_list = [];
    grp_first_cond_order = [];

    for j=1:session_group(i),
       cnt = cnt+1;

       ShowProgress(progress_hdl,cnt/(num_profiles*10)+1/10);

       load(st_info{cnt}.datamatFile);

       coord_idx = find( m(st_info{cnt}.coords) == num_profiles ); 
       nr = length(st_info{cnt}.evt_list);	% number of runs
       nc = length(st_info{cnt}.coords);
       last_row = nr + first_row - 1; 


if ContrastMethod == 4

       %  stack behavdata and get behavmask (re-order for each session file
       %  to make it 'each condition in each run (yes, reversed)'
       %
       behavmask = 1:size(st_datamat,1);

       %  nrr could be nr, depend on 'across run' or 'within run'
       %
       nrr = size(st_datamat,1) / num_conditions;

       behavmask = reshape(behavmask, [nrr, num_conditions]);
       behavmask = behavmask';
       behavmask = reshape(behavmask, [size(st_datamat,1),1]);

end


       this_subj_order = zeros(1, nr);
       first_cond = 1;
       jj = 1;

       %  get first_cond of each run
       %
       while first_cond <= nr
          this_subj_order(first_cond) = 1;
          first_cond = first_cond + num_conditions;

          subj_name = [subj_name, {['Subj', num2str(cnt), 'Run', num2str(jj)]}];

          jj = jj + 1;					% next run
       end
     
       first_cond_order = [first_cond_order, this_subj_order];
       grp_first_cond_order = [grp_first_cond_order, this_subj_order];	% for behavpls

% end


       %  stack datamat
       %
       tmp_datamat = reshape(st_datamat,[nr,win_size,nc]);
       tmp_new_st_datamat = ...
		reshape(tmp_datamat(:,:,coord_idx),[nr,win_size*num_voxels]);
       tmp_evt_list = st_info{cnt}.evt_list;

       %  intentionally reverse the order to each condition in each run, if behavpls
       %  because in behavpls, the whole thing will be then re-order again
       %
       if ContrastMethod == 4				% behavpls
          tmp_new_st_datamat = tmp_new_st_datamat(behavmask,:);
          tmp_evt_list = tmp_evt_list(behavmask);
       end

       grp_tmp_new_st_datamat = [grp_tmp_new_st_datamat; tmp_new_st_datamat];
       grp_tmp_new_evt_list = [grp_tmp_new_evt_list, tmp_evt_list];
       new_st_datamat(first_row:last_row,:) = tmp_new_st_datamat;	% stacked datamat

       clear st_datamat tmp_datamat;

       new_evt_list = [new_evt_list st_info{cnt}.evt_list];


       first_row = last_row + 1;

    end;				% session_group j

    if ContrastMethod == 4				% behavpls

       grp_subj_mask = [];

       for ii = 1:num_conditions
          grp_subj_mask = [grp_subj_mask, find(grp_first_cond_order)];
          grp_first_cond_order = [grp_first_cond_order(end), grp_first_cond_order(1:end-1)];
       end

       grp_tmp_new_st_datamat = grp_tmp_new_st_datamat(grp_subj_mask,:);
       grp_tmp_new_evt_list = grp_tmp_new_evt_list(grp_subj_mask);

       newdata_lst{i} = grp_tmp_new_st_datamat;
       num_subj_lst(i) = sum(grp_first_cond_order);
       new_evt_list_lst{i} = grp_tmp_new_evt_list;

    end

  end					% num_group i

  num_behav_subj = sum(first_cond_order);


  %  Deselect Conditions
  %
  num_conditions = sum(cond_selection);
  curr_conditions = curr_conditions(find(cond_selection));


  if ContrastMethod == 4				% behavpls

     for i = 1:length(new_evt_list_lst)

        tmp_new_evt_list = new_evt_list_lst{i};

        [mask, tmp_new_evt_list, evt_length] = ...
		fmri_mask_evt_list(tmp_new_evt_list, cond_selection);

        new_evt_list_lst{i} = tmp_new_evt_list;

        new_st_datamat = newdata_lst{i};
        newdata_lst{i} = new_st_datamat(mask,:);

     end


     new_st_datamat = [];
     new_evt_list = [];

     for i=1:num_groups,
        new_st_datamat = [new_st_datamat; newdata_lst{i}];
        new_evt_list = [new_evt_list new_evt_list_lst{i}];
     end

  else

     [mask, new_evt_list, evt_length] = ...
	fmri_mask_evt_list(new_evt_list, cond_selection);

     new_st_datamat = new_st_datamat(mask,:);

  end

  %  validate posthoc data
  %
  if ~isempty(posthoc)
     [r_posthoc,c_posthoc] = size(posthoc);
     if r_posthoc ~= num_behavdata_col * num_conditions * num_groups
        msg = sprintf('Rows in Posthoc data file do not match.');
        ShowProgress(progress_hdl,['ERROR: ', msg]);
        uiwait(msgbox(msg,'ERROR','modal'));
        new_st_datamat = [];
        return;
     end
  end

  return;					% concat_st_datamat2


%-------------------------------------------------------------------------
function [new_st_datamat, new_st_coords, st_dims, num_conditions, new_evt_list, ...
	    win_size, voxel_size, origin, subj_group, new_behavdata, behavname, ...
			subj_name, curr_conditions, num_behav_subj, ...
			behavdata_lst, newdata_lst, num_subj_lst ] = ...
    			concat_st_datamat(SessionProfiles,progress_hdl,ContrastMethod, ...
			posthoc,cond_selection,group_analysis)
%
%
  behavdata_lst = {};
  newdata_lst = {};
  num_subj_lst = [];

  new_st_datamat = [];			%  stacked datamat
  new_st_coords = [];
  st_dims = [];
  num_conditions = [];
  new_evt_list = [];
  new_evt_list_lst = {};
  win_size = [];
  voxel_size = [];
  origin = [];
  subj_group = [];

⌨️ 快捷键说明

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