Home > marsbar > mars_image_scaling.m

mars_image_scaling

PURPOSE ^

get image scaling data for images, maybe via SPM design

SYNOPSIS ^

function [VY,row] = mars_image_scaling(marsD)

DESCRIPTION ^

 get image scaling data for images, maybe via SPM design
 FORMAT [VY,row] = mars_image_scaling(marsD)
-----------------------------------------------------------------------

 Inputs
 marsD      - design matrix to (optionally) get parameters from
 
 Returns
 VY         - SPM vol structs with selected scaling
 row        - cell array, one per subject/session giving corresponding
              rows in for VY array

 $Id$

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [VY,row] = mars_image_scaling(marsD)
0002 % get image scaling data for images, maybe via SPM design
0003 % FORMAT [VY,row] = mars_image_scaling(marsD)
0004 %-----------------------------------------------------------------------
0005 %
0006 % Inputs
0007 % marsD      - design matrix to (optionally) get parameters from
0008 %
0009 % Returns
0010 % VY         - SPM vol structs with selected scaling
0011 % row        - cell array, one per subject/session giving corresponding
0012 %              rows in for VY array
0013 %
0014 % $Id$
0015 
0016 VY = [];
0017 if nargin < 1
0018   marsD = [];
0019 end
0020 if isempty(marsD)
0021   mod_code = spm_input('Modality of images to scale', '+1', 'b', ...
0022               'PET|FMRI|Other', [1 2 3], 2);
0023 else
0024   mod_code = is_fmri(marsD) + 1;
0025 end
0026 
0027 switch mod_code
0028  case 1  % PET
0029   dGM   = 50;
0030   sess_str = 'Subject';
0031  case 2   % FMRI
0032   dGM =   100;
0033   sess_str = 'Session';
0034  case 3   % Other
0035   dGM =   0;
0036   sess_str = 'Subject';
0037 end
0038   
0039 VY = [];
0040 Global = [];  
0041 
0042 [Finter,Fgraph,CmdLine] = spm('FnUIsetup','Extract data from ROI(s)');
0043 
0044 % images
0045 if isempty(marsD)
0046   spmf = 0;
0047 else
0048   spmf = spm_input('Images from:', '+1','b',['SPM design|GUI select'], ...
0049            [1 0], 2);
0050 end
0051 
0052 % get images, from design, or by hand
0053 if spmf
0054   if ~has_images(marsD);
0055     warning('Design structure does not specify images');
0056     return
0057   end
0058   VY = get_images(marsD);
0059   row = block_rows(marsD);
0060   nsess = length(row);
0061 end
0062 
0063 if isempty(VY)  % need to know about images
0064   % no of sessions / subjects
0065   nsess = spm_input(sprintf('No of %ss', sess_str), '+1', 'r', 1, 1); 
0066   % select files for each session
0067   for s = 1:nsess
0068     simgs = spm_get(Inf, mars_veropts('get_img_ext'), ...
0069             sprintf('Data images %s %d', sess_str, s));
0070     row{s} = (1:size(simgs, 1))'+size(VY,1);
0071     VY = strvcat(VY, simgs);
0072   end 
0073 end  % of image get routines
0074 if isempty(VY), return, end
0075 
0076 % global scaling options
0077 askGMf = 1;
0078 if spmf
0079   gopts =  {''};
0080   glabs = ['SPM design|' sess_str ' specific scaling',...
0081            '|Proportional scaling|Raw data'];
0082   tmp = spm_input('Scaling from:', '+1', 'm', glabs, 1:4, 1);
0083   if tmp == 1
0084     Global = [];
0085     askGMf = 0;
0086   else
0087     % force remap to wipe out previous SPM scaling
0088     VY = strvcat(VY(:).fname);
0089     if tmp == 2
0090       Global = 'None';
0091     elseif tmp == 3
0092       Global = 'Scale';
0093     elseif tmp == 4  
0094       Global = [];
0095     end
0096   end
0097 
0098 else % scaling by hand
0099   glabs = [sess_str ' specific scaling',...
0100        '|Proportional scaling|Raw data'];
0101   tmp = spm_input('Scaling from:', '+1', 'm', glabs, [1 2 3], 1);
0102   if tmp == 1
0103     Global = 'None';
0104   elseif tmp == 2
0105     Global = 'Scale';
0106   else
0107     Global = [];
0108   end
0109 end
0110 
0111 % Grand mean scaling
0112 GM = 0;
0113 if askGMf
0114   GM = spm_input('Scale grand mean to (0=raw)','+1','r',dGM,1);
0115 end
0116 
0117 % map files now, if not yet mapped
0118 if ischar(VY)
0119   fprintf('\n%-40s: %30s','Mapping files',' ')                     %-#
0120   VY = spm_vol(VY);
0121   fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),'...done')         %-#
0122 end
0123 
0124 % Apply scaling options if necessary
0125 if ~isempty(Global)  
0126   
0127 %-Compute Global variate
0128 %-----------------------------------------------------------------------
0129 q      = length(VY);
0130 g      = zeros(q,1);
0131 fprintf('%-40s: %30s','Calculating globals',' ')                     %-#
0132 for i  = 1:q
0133   fprintf('%s%30s',repmat(sprintf('\b'),1,30),sprintf('%4d/%-4d',i,q)) %-#
0134   g(i) = spm_global(VY(i));
0135 end
0136 fprintf('%s%30s\n',repmat(sprintf('\b'),1,30),'...done')               %-#
0137 
0138 % get null GM scaling
0139 if (GM == 0)
0140   GM = mean(g);
0141 end
0142 
0143 % scale if specified (otherwise subject / session specific grand mean scaling)
0144 %-----------------------------------------------------------------------
0145 gSF     = GM./g;
0146 if strcmp(Global,'None')
0147   for i = 1:nsess
0148     j      = row{i};
0149     gSF(j) = GM./mean(g(j));
0150   end
0151 end
0152 
0153 %-Apply gSF to memory-mapped scalefactors to implement scaling
0154 %-----------------------------------------------------------------------
0155 for  i = 1:q, VY(i).pinfo(1:2,:) = VY(i).pinfo(1:2,:)*gSF(i); end
0156 
0157 end % of global options
0158 
0159 
0160

Generated on Thu 17-Mar-2016 01:14:46 by m2html © 2005