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

📄 pet_create_datamat_ui.m

📁 绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX
💻 M
📖 第 1 页 / 共 3 页
字号:
    savepwd = curr;
    home = curr;	% pls_data_path;

    %  add path for all subject
    %
    for i = 1:k
        for j = 1:n
            subj_files{i,j} = fullfile(subject{j}, subj_files{i,j});
        end
    end

    orient_pattern = getappdata(gcf, 'orient_pattern');

    if isempty(orient_pattern)
       [dims, voxel_size, origin] = rri_imginfo(subj_files{1,1});
    else
       dims = getappdata(gcf, 'dims');
       voxel_size = getappdata(gcf, 'voxel_size');
       origin = getappdata(gcf, 'origin');
    end

    dims = [dims(1) dims(2) 1 dims(3)];

    if (use_brain_mask==1) & ~isequal(dims,mask_dims),
       errmsg ='ERROR: Dimensions of the data do not match that of the brain mask!';
       errordlg(errmsg,'Brain Mask Error');
       waitfor(gcf);
       return;
    end;

    if (use_brain_mask == 1)			% coords from brain_mask
       coords = find( brain_mask(:) > 0)';
       m = zeros(dims);		
       m(coords) = 1;
       coords = find(m == 1)';
    end;

    num_voxels = prod(dims);

    if (use_brain_mask == 0)
       coords = zeros(1, num_voxels);		% initial st_coords
    end

    %  to make progress bar in the center
    %
    fig1 = gcf;
    progress_hdl = rri_progress_ui('initialize', 'Creating Datamat');
    close(fig1);

    section1 = n*k/(n*k+10);		% 1st section of progress bar
    factor = 1/(n*k+10);		% factor for the 2nd section

    rri_progress_ui(progress_hdl, '', 0.5*factor);

    %  make tdatamat, which includes non_brain voxels
    %
    tdatamat=[];
    for i = 1:k

        temp=[];

        for j=1:n

            message=['Loading condition ',num2str(i),', subject ',num2str(j),'.'];
            rri_progress_ui(progress_hdl,'Loading Images',message);

            img = load_nii(subj_files{i,j}, 1);

            v7 = version;
            if str2num(v7(1))<7
               img = reshape(double(img.img), [img.hdr.dime.dim(2:3) 1 img.hdr.dime.dim(4)]);
            else
               img = reshape(single(img.img), [img.hdr.dime.dim(2:3) 1 img.hdr.dime.dim(4)]);
            end

            if ~isempty(orient_pattern)
               img = img(orient_pattern);
            end

%            img = reshape(img,[size(img,1)*size(img,2),size(img,4)]);

            temp = [temp; img(:)'];

            %  find brain voxel coords, and
            %  accumulated to find common coords for all img
            %
            if (use_brain_mask == 0)
               coords = coords + find_nonbrain_coords(img(:)',coord_thresh,options.ConsiderAllVoxels);
            end

        end

        tdatamat=[tdatamat;temp];
        rri_progress_ui(progress_hdl, '', ((i-1)*n+j)*factor);

    end

    message = 'Selecting only the brain voxels ...';
    rri_progress_ui(progress_hdl,'Building datamat',message);

    %  determine the coords of the brain region
    %
    if (use_brain_mask == 0)	% coords from thresh by 'find_nonbrain_coords' 
       coords = find(coords == 0);
    end

    [dr,dc]=size(tdatamat);
    factor = 0.1/dr;			% factor for the 2nd section
    for i=1:dr
        rri_progress_ui(progress_hdl,'',section1+(i*factor)*(1-section1));
    end

    %  remap data to eliminate non-brain voxels
    %
    datamat = tdatamat(:,coords);
%    raw_datamat = datamat;

    [dr,dc]=size(datamat);		% update dr/dc

    if (normalize_volume_mean == 1)

       % perform whole-brain ratio adjustment
       gmean=mean(datamat,2);		% grand mean for each image

       rri_progress_ui(progress_hdl,'Normalizing datamat',section1+0.2*(1-section1));

       message = 'Normalize datamat with its volume mean ...';
       rri_progress_ui(progress_hdl,'',message);

       factor = 0.8/dc;				% factor for the 2nd section
       checkpoint = floor(dc/10);		% set check point
       check = checkpoint;
       percentage = 10;

       for i=1:dc
           datamat(:,i)=(datamat(:,i))./gmean;	% normalized on the mean of each img

           if(i==check)
               rri_progress_ui(progress_hdl,'',section1+(0.2+i*factor)*(1-section1));
               message = [num2str(percentage), '% of the volume is normalized.'];
               rri_progress_ui(progress_hdl,'',message);
               check = check + checkpoint;
               percentage = percentage + 10;
           end
       end

    end

    if ~isempty(merged_conds)
        new_num_conditions = length(merged_conds);
        new_condition = {merged_conds.name};
        [b_dr b_dc] = size(behavdata);

        for i = 1:k
            datamat_cond{i} = datamat((i-1)*n+1:i*n,:);
            datamat_cond{i} = datamat_cond{i}(:)';

            if ~isempty(behavdata)
                behav_cond{i} = behavdata((i-1)*n+1:i*n,:);
                behav_cond{i} = behav_cond{i}(:)';
            end
        end

        clear datamat;

        for i = 1:new_num_conditions
            new_datamat_cond{i} = ...
		mean(cat(1, datamat_cond{merged_conds(i).cond_idx}), 1);
            new_datamat_cond{i} = reshape(new_datamat_cond{i}, [n dc]);

            if ~isempty(behavdata)
                new_behav_cond{i} = ...
			mean(cat(1, behav_cond{merged_conds(i).cond_idx}), 1);
                new_behav_cond{i} = reshape(new_behav_cond{i}, [n b_dc]);
            end
        end

        clear datamat_cond behav_cond;
        datamat = cat(1, new_datamat_cond{:});

        if ~isempty(behavdata)
            behavdata = cat(1, new_behav_cond{:});
            session_info.behavdata = behavdata;
            session_info.num_behavior = size(behavdata,1);
        end

        clear new_datamat_cond new_behav_cond;
        session_info.num_conditions = new_num_conditions;
        session_info.condition = new_condition;

    end

    rri_progress_ui(progress_hdl,'',1);
    message = 'Saving to the disk ...';
    rri_progress_ui(progress_hdl,'Save',message);

    elapsed_time = toc;
%    disp('Datamat is created ...');

    % save to disk

    datamatfile = fullfile(home, filename);

    if(exist(datamatfile,'file')==2)	% datamat file with the same file name exist

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

        if(strcmp(response,'No'))

            cd(home);

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

          done1 = 0;
          while ~done1

            putfile_filter = [datamat_prefix,'_PETdatamat.mat'];
            [filename, pathname] = uiputfile(putfile_filter,'Save As');

            if isequal(filename,0)
                datamatfile = [];
                close(progress_hdl);

                msg1 = ['WARNING: No file is saved.'];
                % msgbox(msg1,'Uncomplete');
                %uiwait(msgbox(msg1,'Uncomplete','modal'));
                set(findobj(gcf,'Tag','MessageLine'),'String',msg1);
                done1 = 1;
%                uiresume;
                return;
            else
                if rri_chkfname(filename, 'PET', 'datamat')
                   datamatfile = fullfile(pathname, filename);
                   done1 = 1;
                else
                   msg = ['File name must be ended with _PETdatamat.mat'];
                   uiwait(msgbox(msg,'Wrong filename','modal'));
                end
            end

          end

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

        end
    end

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

    create_ver = plsgui_vernum;
    done = 0;

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

    while ~done
       try
          save(datamatfile,'datamat','coords','behavdata','behavname', ...
		'dims','voxel_size','origin','session_file','session_info', ...
		'create_ver','create_datamat_info','singleprecision');
          done = 1;
       catch

          done1 = 0;
          while ~done1

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

            if isequal(filename,0)
                datamatfile = [];
                close(progress_hdl);

                msg1 = ['WARNING: No file is saved.'];
                % msgbox(msg1,'Uncomplete');
                %uiwait(msgbox(msg1,'Uncomplete','modal'));
                set(findobj(gcf,'Tag','MessageLine'),'String',msg1);
                done1=1;
%                uiresume;
                return;
            else
                if rri_chkfname(filename, 'PET', 'datamat')
                   datamatfile = fullfile(pathname, filename);
                   done1=1;
                else
                   msg = ['File name must be ended with _PETdatamat.mat'];
                   uiwait(msgbox(msg,'Wrong filename','modal'));
                end
            end

          end

       end
    end

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

    cd(savepwd);

    close(progress_hdl);

%    msg1 = ['Datamat file "',datamatfile,'" has been created and saved on your hard drive.'];
%    msg2 = ['The total elapse time to build this datamat is ',num2str(elapsed_time),' seconds.'];

%    uiwait(msgbox({msg1;'';msg2},'Completed','modal'));
%%    uiwait(msgbox(msg1,'Completed','modal'));
    %% msgbox({msg1;'';msg2},'Completed');	% however, this works for PC

   msg1 = ['WARNING: Do not change file name manually in command window.'];
   uiwait(msgbox(msg1,'File has been saved'));

%    uiresume;
    return;					% CreateDatamat


%----------------------------------------------------------------------------
function MergeConditions()

   session_file = getappdata(gcf,'SessionFile');
   load(session_file);
   condition = session_info.condition;
   merged_conds = fmri_merge_condition_ui(condition);
   setappdata(gcf, 'merged_conds', merged_conds);

   return;					% MergeConditions


%----------------------------------------------------------------------------
function orient()

   nii = getappdata(gcf, 'nii');
   orient_pattern = getappdata(gcf, 'orient_pattern');
   session_file = getappdata(gcf,'SessionFile');

   load(session_file);
   imgfile = fullfile(session_info.subject{1}, session_info.subj_files{1,1});

   [dims, voxel_size, origin, nii, orient_pattern] = ...
	rri_orient_pattern_ui(imgfile, nii, orient_pattern);

   if isempty(nii)
      return;
   end

   setappdata(gcf, 'dims', double(nii.hdr.dime.dim(2:4)));
   setappdata(gcf, 'voxel_size', double(nii.hdr.dime.pixdim(2:4)));
   setappdata(gcf, 'origin', double(nii.hdr.hist.originator(1:3)));
   setappdata(gcf, 'nii', nii);
   setappdata(gcf, 'orient_pattern', orient_pattern);

   return;					% orient

⌨️ 快捷键说明

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