Home > functions > job > vb_job_coreg_brain.m

vb_job_coreg_brain

PURPOSE ^

Calculate coregistration matrix and add to cortical surface model file.

SYNOPSIS ^

function vb_job_coreg_brain(varargin)

DESCRIPTION ^

 Calculate coregistration matrix and add to cortical surface model file.

 Coregistration matrix is added to the cortical surface model file
 specified by 'coreg_parm.source_file'. Coregistration matrix C is
 used to map cortical activity on the 'source' cortical model Js onto
 the 'target' cortical model. The source cortical activity Js and the
 mapped cortical activity Jt (on the target) has a linear relationship: 
 Jt=C*Js. 

 [syntax]
 vb_job_coreg_brain(coreg_parm)
 vb_job_coreg_brain(proj_root,coreg_parm)

 [input]
 proj_root : <<string>> VBMEG project root directory. 
 coreg_parm: <<struct>> Parameters for coregistration. 
 --- fields of coreg_parm
  source_file: <<string>> Cortical surface model file of the source. 
  source_key : <<string>> Coordinate system ID of the source. 
  target_file: <<string>> Cortical surface model file of the target. 
  target_key : <<string>> Coordinate system ID of the target. 
  key        : <<string>> ID of coregistration matrix. 
  R          : <optional> <<double>> Scaling parameter for
                     coregistration matrix (default = 0.01).
  distance   : <optional> <<string>> 'Sphere' or 'Euclid' 
               (default = 'Sphere'). 
 ---

 [output]
 Struct variable 'Call' will be added to the cortical surface model file
 specified by 'coreg_parm.brain_source_file'. 
 --- fields of Call
  C  : <<cell of matrix>> Coregistration matrix. 
  key: <<cell of string>> Used to get spherical coordinate values by
       vb_load_cortex. 
 ---

 [example]
 >> coreg_parm.source_file = './subjects/sbj_test/sbj_test.brain.mat';
 >> coreg_parm.source_key = 'sphere.reg';
 >> coreg_parm.target_file = './subjects/fsaverage/fsaverage.brain.mat';
 >> coreg_parm.target_key = 'sphere.reg';
 >> coreg_parm.key = 'fsaverage'; 
 >> vb_job_coreg_brain(coreg_parm); 

 [history]
 2010-11-18 Taku Yoshioka
 2011-02-24 taku-y
  [minor] Apply proj_root to source_file and target_file. 

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function vb_job_coreg_brain(varargin)
0002 % Calculate coregistration matrix and add to cortical surface model file.
0003 %
0004 % Coregistration matrix is added to the cortical surface model file
0005 % specified by 'coreg_parm.source_file'. Coregistration matrix C is
0006 % used to map cortical activity on the 'source' cortical model Js onto
0007 % the 'target' cortical model. The source cortical activity Js and the
0008 % mapped cortical activity Jt (on the target) has a linear relationship:
0009 % Jt=C*Js.
0010 %
0011 % [syntax]
0012 % vb_job_coreg_brain(coreg_parm)
0013 % vb_job_coreg_brain(proj_root,coreg_parm)
0014 %
0015 % [input]
0016 % proj_root : <<string>> VBMEG project root directory.
0017 % coreg_parm: <<struct>> Parameters for coregistration.
0018 % --- fields of coreg_parm
0019 %  source_file: <<string>> Cortical surface model file of the source.
0020 %  source_key : <<string>> Coordinate system ID of the source.
0021 %  target_file: <<string>> Cortical surface model file of the target.
0022 %  target_key : <<string>> Coordinate system ID of the target.
0023 %  key        : <<string>> ID of coregistration matrix.
0024 %  R          : <optional> <<double>> Scaling parameter for
0025 %                     coregistration matrix (default = 0.01).
0026 %  distance   : <optional> <<string>> 'Sphere' or 'Euclid'
0027 %               (default = 'Sphere').
0028 % ---
0029 %
0030 % [output]
0031 % Struct variable 'Call' will be added to the cortical surface model file
0032 % specified by 'coreg_parm.brain_source_file'.
0033 % --- fields of Call
0034 %  C  : <<cell of matrix>> Coregistration matrix.
0035 %  key: <<cell of string>> Used to get spherical coordinate values by
0036 %       vb_load_cortex.
0037 % ---
0038 %
0039 % [example]
0040 % >> coreg_parm.source_file = './subjects/sbj_test/sbj_test.brain.mat';
0041 % >> coreg_parm.source_key = 'sphere.reg';
0042 % >> coreg_parm.target_file = './subjects/fsaverage/fsaverage.brain.mat';
0043 % >> coreg_parm.target_key = 'sphere.reg';
0044 % >> coreg_parm.key = 'fsaverage';
0045 % >> vb_job_coreg_brain(coreg_parm);
0046 %
0047 % [history]
0048 % 2010-11-18 Taku Yoshioka
0049 % 2011-02-24 taku-y
0050 %  [minor] Apply proj_root to source_file and target_file.
0051 %
0052 % Copyright (C) 2011, ATR All Rights Reserved.
0053 % License : New BSD License(see VBMEG_LICENSE.txt)
0054 
0055 %
0056 % check input arguments
0057 %
0058 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0059 if length(varargin) == 1
0060   proj_root = [];
0061   coreg_parm = varargin{1};
0062 elseif length(varargin) == 2
0063   proj_root = varargin{1};
0064   coreg_parm = varargin{2};
0065   proj_root = vb_rm_trailing_slash(proj_root);
0066 
0067   % Make absolute path
0068   coreg_parm.source_file = [proj_root filesep coreg_parm.source_file];
0069   coreg_parm.target_file = [proj_root filesep coreg_parm.target_file];
0070 else
0071   error('Error: invalid usage of vb_job_coreg_brain.');
0072 end
0073 
0074 %
0075 % Verbose level
0076 %
0077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0078 global vbmeg_inst
0079 verbose_const = vb_define_verbose; 
0080 
0081 if isempty(vbmeg_inst) | ~isfield(vbmeg_inst,'verbose_level'), 
0082   verbose_level = verbose_const.VERBOSE_LEVEL_NOTICE;
0083 else
0084   verbose_level = vbmeg_inst.verbose_level;
0085 end
0086 
0087 %
0088 % Input parameters
0089 %
0090 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0091 if ~isfield(coreg_parm,'distance')
0092   coreg_parm.distance = 'Sphere'; % default is spherical coordinates
0093 end
0094 
0095 if ~isfield(coreg_parm,'R')
0096   coreg_parm.R = 0.01; % scaling parameter of coregistration matrix
0097 end
0098 
0099 %
0100 % Load coordinate values
0101 %
0102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0103 V = vb_load_cortex(coreg_parm.source_file, ...
0104                    coreg_parm.source_key);
0105 [tmp1,tmp2,ix_V_org] = vb_load_cortex_info(coreg_parm.source_file);
0106 Isl = length(ix_V_org.Left);
0107 Isr = length(ix_V_org.Right);
0108 Vsl = V(1:Isl,:);
0109 Vsr = V((Isl+1):end,:);
0110 
0111 V = vb_load_cortex(coreg_parm.target_file, ...
0112                    coreg_parm.target_key);
0113 [tmp1,tmp2,ix_V_org] = vb_load_cortex_info(coreg_parm.target_file);
0114 Itl = length(ix_V_org.Left);
0115 Itr = length(ix_V_org.Right);
0116 Vtl = V(1:Itl,:);
0117 Vtr = V((Itl+1):end,:);
0118 
0119 %
0120 % Calculate coregistration matrix
0121 %
0122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0123 Cl = subjob_coreg(Vsl,Vtl,coreg_parm);
0124 Cr = subjob_coreg(Vsr,Vtr,coreg_parm);
0125 C  = sparse([Cl sparse(Itl,Isr); sparse(Itr,Isl) Cr]);
0126 
0127 %
0128 % Add coregistration matrix to cortical surface model file
0129 %
0130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0131 S = load(coreg_parm.source_file);
0132 if isfield(S,'Call'), 
0133   Call = S.Call;
0134 else
0135   Call.C = cell(0);
0136   Call.key = cell(0);
0137 end
0138 
0139 flag = true;
0140 for i=1:length(Call.key)
0141   if strcmp(Call.key{i},coreg_parm.key), 
0142     flag = false;
0143     if verbose_level>=verbose_const.VERBOSE_LEVEL_NOTICE, 
0144       msg = ['Coregistration matrix ID ''' coreg_parm.key ...
0145              ''' is already used. Overwrite ?'];
0146       str = questdlg(msg,'Coregistration matrix registration','Yes', ...
0147              'Cancel','Cancel');
0148       if strcmp(str,'Yes')
0149     Call.C{i} = C;
0150     h = msgbox(['Coregistration matrix ID ''' coreg_parm.key ...
0151             ''' was overwritten.']);
0152     uiwait(h);
0153       end
0154     else
0155       vb_disp(['Coregistration matrix ID ''' coreg_parm.key ''' is already ' ...
0156                'used, so it was overwritten with new data. '], ...
0157               verbose_const.VERBOSE_LEVEL_WARNING); 
0158       Call.C{i} = C;
0159     end
0160   end
0161 end
0162 
0163 if flag, 
0164   Call.C{length(Call.key)+1} = C;
0165   Call.key{length(Call.key)+1} = coreg_parm.key;
0166 end
0167 
0168 vb_save(coreg_parm.source_file,'Call');
0169 
0170 %
0171 % project_file save
0172 %
0173 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0174 proj_file = get_project_filename;
0175 if isempty(proj_file)
0176   return;
0177 end
0178 
0179 project_file_mgr('load', proj_file);
0180 project_file_mgr('add', 'coreg_parm', coreg_parm);
0181 
0182 return; 
0183 
0184 %
0185 % Inner function: subjob_coreg
0186 %
0187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0188 function C = subjob_coreg(Vs,Vt,coreg_parm)
0189 % Modified from 'coreg_sphere3' to support TAL coordinates
0190 %
0191 % - Matthew 2010/7/21
0192 
0193 if strcmpi(coreg_parm.distance,'Sphere')
0194   % Normalization of sphere coordinate
0195   Vs = Vs./repmat(sqrt(sum(Vs.^2,2)),[1 3]);
0196   Vt = Vt./repmat(sqrt(sum(Vt.^2,2)),[1 3]);
0197     
0198   % Coregistration matrix
0199   %   - One minus dot product
0200   %     C(i,j) = 1 - sum(Vt(i,:).*Vs(j,:));
0201   C = 1-Vt*Vs';
0202 
0203 elseif strcmpi(coreg_parm.distance,'Euclid')
0204   % Normalize coordinates to lie in unit square
0205   Vs = Vs./max(abs(Vs(:)));
0206   Vt = Vt./max(abs(Vt(:)));
0207     
0208   % Coregistration matrix
0209   %   - Euclidean distance
0210   %     C(i,j) = sqrt(sum((Vt(i,:)-Vs(j,:)).^2));
0211   if exist('bsxfun','builtin')
0212     C = bsxfun(@minus, Vt(:,1), Vs(:,1)').^2;
0213     C = C+bsxfun(@minus, Vt(:,2), Vs(:,2)').^2;
0214     C = C+bsxfun(@minus, Vt(:,3), Vs(:,3)').^2;
0215     C = sqrt(C);
0216   else
0217     C = zeros(size(Vt,1),size(Vs,1));
0218     for i=1:size(Vt,1)
0219       C(i,:) = sqrt(sum((repmat(Vt(i,:),size(Vs,1),1)-Vs).^2,2));
0220     end
0221   end
0222 else
0223   error(['Unrecognized distance metric: %s',coreg_parm.distance]);
0224 end
0225 
0226 %
0227 % Scaling and normalization
0228 %
0229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0230 C = exp(-1*(C.^2)./(coreg_parm.R.^2));
0231 
0232 if exist('bsxfun','builtin')
0233   % This is much faster than using a for loop,
0234   % but 'bsxfun' is not available in older versions of Matlab
0235     
0236   C(bsxfun(@lt,C,0.99*max(C,[],2)))=0;  % remove elements < 0.99*(max value of row)
0237   C=bsxfun(@rdivide,C,sum(C,2));        % normalize so sum of each row is 1
0238 else
0239   for k=1:size(Vt,1)
0240     C(k,C(k,:)<0.99*max(C(k,:))) = 0; % remove elements < 0.99*(max value of row)
0241     C(k,:) = C(k,:)./sum(C(k,:),2);   % normalize so sum of row is 1
0242   end
0243 end
0244 
0245 C=sparse(C);                    % convert to sparse matrix
0246 
0247 return;

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005