Home > utils > prt_utils_update_mask.m

prt_utils_update_mask

PURPOSE ^

Script to update the first level mask to be entered in the Data and

SYNOPSIS ^

function [nmask] = prt_utils_update_mask(files,mask,dir)

DESCRIPTION ^

 Script to update the first level mask to be entered in the Data and
 Design step of a PRoNTo analysis. The mask is first resized to match the
 dimensions of provided images. Any NaN value found in any of the images
 will then be removed from the mask.

 Modalities/runs that will be concatenated in samples should be selected
 at the same time to ensure common features>

 Inputs (optional):
 - files: character array of the full file names to load
 - mask : full name of the mask to update (e.g. SPMnoeyes in /masks)
 - dir  : directory where to save the updated mask

 Outputs: the updated mask, saved in the specified directory.
--------------------------------------------------------------------------
 Written by J. Schrouff, 2015, as a preprocessing step for PRoNTo.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [nmask] = prt_utils_update_mask(files,mask,dir)
0002 
0003 % Script to update the first level mask to be entered in the Data and
0004 % Design step of a PRoNTo analysis. The mask is first resized to match the
0005 % dimensions of provided images. Any NaN value found in any of the images
0006 % will then be removed from the mask.
0007 %
0008 % Modalities/runs that will be concatenated in samples should be selected
0009 % at the same time to ensure common features>
0010 %
0011 % Inputs (optional):
0012 % - files: character array of the full file names to load
0013 % - mask : full name of the mask to update (e.g. SPMnoeyes in /masks)
0014 % - dir  : directory where to save the updated mask
0015 %
0016 % Outputs: the updated mask, saved in the specified directory.
0017 %--------------------------------------------------------------------------
0018 % Written by J. Schrouff, 2015, as a preprocessing step for PRoNTo.
0019 
0020 if nargin<1
0021     files=spm_select([1 Inf],'image','Select files to be analyzed in PRoNTo');
0022 end
0023 
0024 if nargin<2
0025     mask = spm_select(1,'image','Select maks image to be updated');
0026 end
0027 
0028 if nargin<3
0029     dir = uigetdir();
0030 end
0031 
0032 
0033 try
0034     M = nifti(mask);
0035 catch %#ok<*CTCH>
0036     error('prt_utils_update_mask:CouldNotLoadFile',...
0037         'Could not load mask file');
0038 end
0039 
0040 % First, resize mask to 1st image dimensions
0041 % -------------------------------------------------------------------------
0042 try 
0043     N = spm_vol(files(1,:));
0044 catch
0045     error('prt_utils_update_mask:CouldNotLoadFile',...
0046         'Could not load first image file');
0047 end
0048 
0049 if N.dim(3)==1, Npdim = N.dim(1:2); else Npdim = N.dim; end % handling case of 2D images
0050 if any(size(M.dat(:,:,:,1)) ~= Npdim)
0051     warning('prt_utils_update_mask:maskAndImagesDifferentDim',...
0052         'Mask has different dimensions to the image files. Resizing...');
0053     
0054     V2 = spm_vol(char(mask));
0055     % reslicing V2
0056     fl_res = struct('mean',false,'interp',0,'which',1,'prefix','tmp_');
0057     spm_reslice([N V2],fl_res)
0058     % now renaming the file
0059     [V2_pth,V2_fn,V2_ext] = spm_fileparts(V2.fname);
0060     rV2_fn = [fl_res.prefix,V2_fn];
0061     if strcmp(V2_ext,'.nii')
0062         % turn .nii into .img/.hdr image!
0063         V_in = spm_vol(fullfile(V2_pth,[rV2_fn,'.nii']));
0064         V_out = V_in; V_out.fname = fullfile(V2_pth,[rV2_fn,'.img']);
0065         spm_imcalc(V_in,V_out,'i1');
0066     end
0067     mfile_new = ['resized_',V2_fn];
0068     movefile(fullfile(V2_pth,[rV2_fn,'.img']), ...
0069         fullfile(dir,[mfile_new,'.img']));
0070     movefile(fullfile(V2_pth,[rV2_fn,'.hdr']), ...
0071         fullfile(dir,[mfile_new,'.hdr']));
0072     mask = fullfile(dir,[mfile_new,'.img']);
0073 end
0074 V2 = spm_vol(mask);
0075 M = spm_read_vols(V2);
0076 
0077 % Second, loop over all files to get the voxels with NaN values
0078 % -------------------------------------------------------------------------
0079 uNan = [];
0080 for i=1:size(files,1)
0081     try
0082         N = spm_vol(files(i,:));
0083     catch
0084         error('prt_utils_update_mask:CouldNotLoadFile',...
0085             ['Could not load image file ',num2str(i)]);
0086     end
0087     voxval = spm_read_vols(N);
0088     voxval = voxval(:);
0089     vNan = find(isnan(voxval));
0090     if ~isempty(vNan)
0091         uNan = union(uNan,vNan);
0092     end
0093 end
0094 
0095 if ~isempty(uNan)
0096     M(uNan) = 0;
0097 end
0098 
0099 [V2_pth,V2_fn,V2_ext] = spm_fileparts(V2.fname);
0100 mfile_new = ['updated_mask_',V2_fn];
0101 nmask = V2;
0102 nmask.fname = fullfile(dir,[mfile_new,V2_ext]);
0103 nmask = spm_write_vol(nmask,M);
0104

Generated on Tue 10-Feb-2015 18:16:33 by m2html © 2005