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

📄 fmri_convert_task.m

📁 绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX
💻 M
📖 第 1 页 / 共 3 页
字号:
  new_behavdata = [];			%  stacked behavdata
  subj_name = {};
  curr_conditions = {};
  num_behav_subj = [];

  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
           clear('behavdata');
           warning off;

           load(st_datamatFile,'behavdata','st_coords','st_dims','st_evt_list', ...
			'behavname','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;
	   
        if ~exist('behavdata','var')
%           msg = 'ERROR: The datamats were generated in old version of the ';
%           msg = [msg 'program, you need to generate them again.'];
%           uiwait(msgbox(msg,'ERROR','modal'));
%           new_st_datamat = [];
%           return;
            behavdata = [];
        end

        if ~exist('behavname','var')
            behavname = {};
            for bcol=1:size(behavdata, 2)
               behavname = [behavname, {['behav', num2str(bcol)]}];
            end
        end

        if ContrastMethod == 4 & cnt > 1		% check behavcol among profiles

           if ~isequal(behavname, st_info{cnt-1}.behavname)
              msg = sprintf('behavior data names and columns should be same among datamats.');
              ShowProgress(progress_hdl,['ERROR: ', msg]);
              uiwait(msgbox(msg,'ERROR','modal'));
              new_st_datamat = [];
              return;
           end

        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;
        st_info{cnt}.behavdata = behavdata;
        st_info{cnt}.behavname = behavname;

        if ContrastMethod == 4	& isempty(behavdata)	% check behavdataempty

            msg = ['ERROR: Behavior data is required'];
            ShowProgress(progress_hdl,['ERROR: ', msg]);
            uiwait(msgbox(msg,'ERROR','modal'));
            new_st_datamat = [];
            return;

        end

        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;

  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);
  new_evt_list = [];
  first_row = 1;

  first_cond_order = [];

  num_behavdata_col = size(st_info{1}.behavdata,2);

if(0)
  num_behavdata_col = 99999;

  for i=1:num_profiles,
     tmp_col = size(st_info{i}.behavdata,2);
     if tmp_col < num_behavdata_col
        num_behavdata_col = tmp_col;
     end
  end
end

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

  tmp_new_st_datamat = [];
  tmp_new_behavdata = [];
  cnt=0;

  for i=1:num_groups,

    grp_tmp_new_st_datamat = [];
    grp_tmp_new_evt_list = [];
    grp_tmp_new_behavdata = [];
    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)'
       %
       tmp_new_behavdata = st_info{cnt}.behavdata(:,1:num_behavdata_col);

       behavmask = 1:size(tmp_new_behavdata,1);

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

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

       tmp_new_behavdata = tmp_new_behavdata(behavmask,:);

       grp_tmp_new_behavdata = [grp_tmp_new_behavdata; tmp_new_behavdata];
       new_behavdata = [new_behavdata; tmp_new_behavdata];		% stacked behavdata

end


       %  get subj_name for designPLS
       %
       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

    [grp_tmp_new_evt_list idx] = sort(grp_tmp_new_evt_list);
    
    new_evt_list = [new_evt_list, grp_tmp_new_evt_list];
    new_st_datamat = [new_st_datamat; grp_tmp_new_st_datamat(idx,:)];

    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_behavdata = grp_tmp_new_behavdata(grp_subj_mask,:);
       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);

       behavdata_lst{i} = grp_tmp_new_behavdata;
       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_behavdata = behavdata_lst{i};
        behavdata_lst{i} = new_behavdata(mask,:);

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

     end


     new_behavdata = [];
     new_st_datamat = [];
     new_evt_list = [];

     for i=1:num_groups,
        new_behavdata = [new_behavdata; behavdata_lst{i}];
        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
%        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

%  if group_analysis
%     num_subj_lst = subj_group;
%  else
%     % num_subj_lst = ones(1,subj_group);
%     num_subj_lst = [];
%  end

  num_subj_lst = subj_group;

  return;					% concat_st_datamat


%----------------------------------------------------------------------------
function total_profiles = ValidateProfiles(group_profiles,conditions),


   num_groups = length(group_profiles);

   cell_buffer = [];
   for i=1:num_groups,
      cell_buffer = [cell_buffer; group_profiles{i}];
   end;

   % check for duplicated profile
   %
   total_profiles = length(cell_buffer);
   for i=1:total_profiles-1,
      for j=i+1:total_profiles,
         if isequal(cell_buffer{i},cell_buffer{j}),
            [p_path, p_name, p_ext] = fileparts(cell_buffer{i});
            msg = sprintf('"%s" has been used for more than 1 group',p_name);
            set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
            total_profiles = -1;
            return;
         else
            set(findobj(gcf,'Tag','MessageLine'),'String','');
         end;
      end;
   end;


   % make sure all conditions are the same
   %
   s = load(cell_buffer{1},'session_info');
   prev_condition = s.session_info.condition;
   first_profile_idx = 2;
   for i=2:total_profiles,
      s = load(cell_buffer{i},'session_info');
      curr_condition = s.session_info.condition;
      if ~isequal(curr_condition,prev_condition),
          [p_path, p_name, p_ext] = fileparts(cell_buffer{i});
          msg = sprintf('Incompatible conditions found in "%s".',p_name);
          set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
          total_profiles = -1;
          return;
      else
          prev_condition = curr_condition;
      end;
   end;

   if exist('use_conditions','var')
      if ~isequal(curr_condition,use_condition),
          [p_path, p_name, p_ext] = fileparts(cell_buffer{i});
          msg = sprintf('The contrast condition is not compatible.".',p_name);
          set(findobj(gcf,'Tag','MessageLine'),'String',['ERROR: ', msg]);
          total_profiles = -1;
          return;
      end;
   end
   
   return;						% ValidateConditions

⌨️ 快捷键说明

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