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

📄 bfm_get_datamat.m

📁 绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX
💻 M
📖 第 1 页 / 共 2 页
字号:
                     datamat = [datamat; evt_datamat(:)'];
                  else
                     datamat = [datamat; evt_datamat];
                  end
               else
                  st_evt_cnt(idx) = st_evt_cnt(idx) + row_idx;

                  if SingleSubject
                     datamat(idx,:) = double(datamat(idx,:)) + double(evt_datamat(:)');
                  else
                     datamat(idx,:) = double(datamat(idx,:)) + double(evt_datamat);
                  end
               end;
            end;	% if across_run==0
         end;		% for (row_idx~=0)

      end;		% for num_conds

      %  save some memory usage
      %
      clear('tmp_datamat');

   end;			% for num_runs (run_idx)

   rri_progress_ui(progress_hdl,'','Postprocessing to shape the datamat, please wait ...');

   %    determine the coords of the brain region
   %
   if (use_brain_mask == 0)	% coords from thresh by 'find_onset_coords' 

      coords = find(coords == 0);

   end

   if (merge_across_runs_flg == 1) & SingleSubject	% merge across all runs 
      single_datamat = [];

      for j=1:length(st_evt_list)
         tmp = double(datamat(j,:)) / num_runs;
         SingleSubject_rows = length(tmp)/num_cols;
         tmp = reshape(tmp, [SingleSubject_rows num_cols]);
         single_datamat = [single_datamat; single(tmp)];
      end;

      datamat = single_datamat;
      clear single_datamat;

      st_evt_list = repmat(st_evt_list, [SingleSubject_rows 1]);
      st_evt_list = st_evt_list(:)';
   end;

   %    apply the coords of the brain region
   %
if 0
   for i = 1:size(datamat,1)
      tmp = datamat(i,:);
%      tmp = reshape(tmp, [win_size, num_voxels]);

      tmp = tmp(:, coords);
%      tmp = reshape(tmp, [1, win_size*length(coords)]);
      st_datamat = [st_datamat; tmp];
   end
end

   st_datamat = datamat(:, coords);

   if (merge_across_runs_flg == 1) & ~SingleSubject	% merge across all runs 
      for j=1:length(st_evt_list)
         st_datamat(j,:) = single(double(st_datamat(j,:)) / st_evt_cnt(j));
      end;
   end;

%   [new_evt_list, order_idx] = reorder_evt_list(st_evt_list,num_conds);
   if 0 % isempty(new_evt_list),
      close(progress_hdl);
      errmsg = sprintf('ERROR: Some conditions have no trials at all.\n');
      errordlg(errmsg,'Creating ST Datamat Error');
      waitfor(gcf);
      return;
   end;

%   st_datamat = st_datamat(order_idx,:);
 %  st_evt_list = new_evt_list;

   %  everything is done, ready to save the information
   %
   st_dims = dims;
   st_voxel_size = voxel_size;
   st_origin = origin;
   st_coords = coords;
   st_win_size = 1;	% win_size;

   st_sessionFile = sessionFile;

   if (st_sessionFile ~= filesep),
      curr = pwd;
      if isempty(curr)
         curr = filesep;
      end
			
      st_sessionFile = fullfile(curr,st_sessionFile);
   end;

   fname = sprintf('%s_BfMRIdatamat.mat',datamat_prefix);
   st_datafile = fullfile(curr,fname);

   rri_progress_ui(progress_hdl,'',1);
   rri_progress_ui(progress_hdl,'',['Saving ST Datamat into the file: ' fname]);

   if(exist(st_datafile,'file')==2)  % datamat file with same filename exist

if ~for_batch

      dlg_title = 'Confirm File Overwrite';
      msg = ['File ',fname,' exist. Are you sure you want to overwrite it?'];
      response = questdlg(msg,dlg_title,'Yes','No','Yes');

      if(strcmp(response,'No'))

       savfig = [];
       if strcmpi(get(gcf,'windowstyle'),'modal')
          savfig = gcf;
          set(gcf,'windowstyle','normal');
       end

       done1 = 0;
       while ~done1

         putfile_filter = [datamat_prefix,'_BfMRIdatamat.mat'];
         [filename, pathname] = uiputfile(putfile_filter,'Save As');
         if isequal(filename,0)
            close(progress_hdl);
            msg1 = ['ERROR: No file is saved.'];
            %uiwait(msgbox(msg1,'Uncomplete','modal'));
            set(findobj(session_win_hdl,'Tag','MessageLine'),'String',msg1);
            done1=1;
            return;
         else
          if rri_chkfname(filename, 'BfMRI', 'datamat')
            done1=1;
            st_datafile = fullfile(pathname, filename);
            session_info.datamat_prefix = strrep(filename,'_BfMRIdatamat.mat','');

            try
               save(sessionFile,'session_info');
               setappdata(session_win_hdl,'SessionDatamatPrefix',session_info.datamat_prefix);
               OldSessionInfo = getappdata(session_win_hdl,'OldSessionInfo');
               OldSessionInfo.datamat_prefix = session_info.datamat_prefix;
               setappdata(session_win_hdl,'OldSessionInfo',OldSessionInfo);
            catch
               close(progress_hdl);
               msg1 = ['ERROR: Session file write error, no file is saved'];
               set(findobj(session_win_hdl,'Tag','MessageLine'),'String',msg1);
               return;
            end

            set(findobj(session_win_hdl,'Tag','DatamatPrefixEdit'), ...
		'String', session_info.datamat_prefix);
            msg1 = ['WARNING: Prefix in Session Profile has been renamed to "', ...
			session_info.datamat_prefix, '"'];
            uiwait(msgbox(msg1,'Prefix has been renamed'));
          else
            msg = ['File name must be ended with _BfMRIdatamat.mat'];
            uiwait(msgbox(msg,'Wrong filename','modal'));
          end
         end

       end

       if ~isempty(savfig)
          set(savfig,'windowstyle','modal');
       end

      end
else
      disp(['WARNING: File ',fname,' is overwritten.']);
end
   end

   create_ver = plsgui_vernum;

   savfig = [];
   if strcmpi(get(gcf,'windowstyle'),'modal')
      savfig = gcf;
      set(gcf,'windowstyle','normal');
   end

   done = 0;

   v7 = version;
   if str2num(v7(1))<7
      st_datamat = double(st_datamat);
      singleprecision = 0;
   else
      singleprecision = 1;
   end

   while ~done
      try
         save(st_datafile,'st_datamat','st_coords','st_dims','st_voxel_size', ...
                'st_origin','st_evt_list', 'st_win_size','st_sessionFile', ...
		'normalize_volume_mean','behavdata','behavname','create_ver', ...
		'create_datamat_info','SingleSubject','singleprecision');
         done = 1;
      catch

       done1=0;
       while ~done1

         putfile_filter = [datamat_prefix,'_BfMRIdatamat.mat'];
         [filename, pathname] = uiputfile(putfile_filter, ...
		'Can not save datamat file, please try again');

         if isequal(filename,0)
            close(progress_hdl);
            msg1 = ['ERROR: No file is saved.'];
            %uiwait(msgbox(msg1,'Uncomplete','modal'));
            set(findobj(session_win_hdl,'Tag','MessageLine'),'String',msg1);
            done1=1;
            return;
         else
          if rri_chkfname(filename, 'BfMRI', 'datamat')
            done1=1;
            st_datafile = fullfile(pathname, filename);
            session_info.datamat_prefix = strrep(filename,'_BfMRIdatamat.mat','');

            try
               save(sessionFile,'session_info');
               setappdata(session_win_hdl,'SessionDatamatPrefix',session_info.datamat_prefix);
               OldSessionInfo = getappdata(session_win_hdl,'OldSessionInfo');
               OldSessionInfo.datamat_prefix = session_info.datamat_prefix;
               setappdata(session_win_hdl,'OldSessionInfo',OldSessionInfo);
            catch
               close(progress_hdl);
               msg1 = ['ERROR: Session file write error, no file is saved'];
               set(findobj(session_win_hdl,'Tag','MessageLine'),'String',msg1);
               return;
            end

            set(findobj(session_win_hdl,'Tag','DatamatPrefixEdit'), ...
		'String', session_info.datamat_prefix);
            msg1 = ['WARNING: Prefix in Session Profile has been renamed to "', ...
			session_info.datamat_prefix, '"'];
            uiwait(msgbox(msg1,'Prefix has been renamed'));
          else
            msg = ['File name must be ended with _BfMRIdatamat.mat'];
            uiwait(msgbox(msg,'Wrong filename','modal'));
          end
         end

       end

      end
   end

   if ~isempty(savfig)
      set(savfig,'windowstyle','modal');
   end

   close(progress_hdl);
   set(findobj(session_win_hdl,'Tag','MessageLine'),'String','');
   msg1 = ['WARNING: Do not change file name manually in command window.'];

   if ~for_batch
      uiwait(msgbox(msg1,'File has been saved'));
   end

   return;


   %    determine the coords of the brain region
   %
   if (use_brain_mask == 0)		% compute coords from (temp) datamat
      coords = rri_make_coords(datamat, 1/coord_thresh);
      datamat = datamat(:,coords);

      %  get rid of voxels that have standard deviation "sd_thresh" times larger 
      %  than the grand average standard deviation.
      %
      std_d = std(datamat);
      thresh = mean(std_d) * sd_thresh;
      idx = find(std_d < thresh);
      coords = coords(idx);
   else
      coords = find( brain_mask(:) > 0)';
      if ~isempty(ignore_slices)	% skip the slices if any
         m = zeros(dims);		
         m(coords) = 1;
         m(:,:,:,s_idx);
         coords = find(m == 1)';
      end;
   end;

   %    apply the coords of the brain region
   %
   for j = 1:num_conds
      num_onsets = length(run_info.blk_onsets{j});
      for k = 1:num_onsets
         tmp_datamat{j,k} = tmp_datamat{j,k}(:,coords);
         baseline_signals{j,k} = baseline_signals{j,k}(:,coords);

         if (normalize_volume_mean == 1),
            num_voxels = length(coords);

            mean_tmp_datamat = mean(tmp_datamat{j,k},2);
            tmp_datamat{j,k} = tmp_datamat{j,k} ./ ...
		mean_tmp_datamat(:,ones(1,num_voxels));

            mean_baseline_signals = mean(baseline_signals{j,k},2);
            baseline_signals{j,k} = baseline_signals{j,k} ./ ...
		mean_baseline_signals(:,ones(1,num_voxels));
         end;
      end;
   end;

   ShowProgress(progress_hdl, (num_conds * max_onsets + 1) * progress_step);

   %  save the datamat, which is a matrix contains only the brain voxels.
   %
   ShowProgress(progress_hdl,['Saving datamat into the file: ' fname ]);

   try 
      save(datamat_file,'tmp_datamat','baseline_signals','coords', ...
	'dims','voxel_size','origin','sessionFile','normalize_volume_mean');
   catch
      errmsg = sprintf('ERROR: Cannot save datamat to file: \n   %s.', ...
                       datamat_file);
      errordlg(errmsg,'Save Datamat Error');
      waitfor(gcf);
      return;
   end

   return;						% bfm_get_datamat


%-------------------------------------------------------------------------
function [new_evt_list,reorder_idx] = reorder_evt_list(evt_list,num_conditions)
%  make sure the row order of the st_datamat is repeat order of
%  conditions, i.e cond#1, cond#2,... cond#1, cond#2, .... etc
%

  num_rep = length(evt_list) / num_conditions;

  [new_evt_list, reorder_idx] = sort(evt_list);
  if ~isequal(reshape(new_evt_list,[num_rep,num_conditions]), ...
             repmat([1:num_conditions],num_rep,1))
     new_evt_list = [];
     reorder_idx = [];
  end;

  return;					% reorder evt_list


%-------------------------------------------------------------------------
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 hdl = ShowProgress2(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


%----------------------------------------------------------------------------
%
%  get the maximum number of onsets within a run
%
%----------------------------------------------------------------------------

function max_onsets = find_max_onsets(run_info)

   num_cond = length(run_info.blk_onsets);
   max_onsets = 0;

   for i = 1:num_cond
      if length(run_info.blk_onsets{i}) > max_onsets
         max_onsets = length(run_info.blk_onsets{i});
      end;
   end;

   return;						% find_max_onsets


%----------------------------------------------------------------------------
%
%  find brain voxel coords for each onset, and return onset_coords array
%
%	1:	represents non_brain voxels
%	0:	represents brain voxels
%
%----------------------------------------------------------------------------

function nonbrain_coords = find_onset_coords(dataset,coord_thresh,considerall)

   [num_scans num_voxels] = size(dataset);
   nonbrain_coords = zeros(1,num_voxels);

   for i=1:num_scans,
      scan_threshold = max(dataset(i,:)) * coord_thresh;

      if considerall
         idx = find(dataset(i,:) < scan_threshold);
      else
         idx = find(dataset(i,:) <= scan_threshold);
      end

      nonbrain_coords(idx) = 1; 
   end

   return;

⌨️ 快捷键说明

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