Home > functions > leadfield > head > vb_head_join_files.m

vb_head_join_files

PURPOSE ^

join plural single layer head files to single head file serially-concatenated

SYNOPSIS ^

function vb_head_join_files(head_files, file_root,joined_file, sigma, layer_tag, calc_mode, verbose_swt, check_level)

DESCRIPTION ^

 join plural single layer head files to single head file serially-concatenated
 [usage]
   vb_head_join_files(head_files, file_root, 
     joined_file, sigma, layer_tag, calc_mode, verbose_swt, check_level)
 [input]
    head_files : <required> cell array of plural "single layer" head files
               :  {N x 1} or {1 x N}

     file_root : <optional> root directory for 'head_files'
               :  [current directory]

   joined_file : <optional> stored file path which has joined data
               :  ['(file_root)/joined_head.head.mat']
 
         sigma : <optional> electric conductivities of each layer
               :  [N x 1] or [1 x N]
               :  [ [ 1.0  0.0125  1.0 ] ]

     layer_tag : <optional> names of each layer (cell array)
               :  {N x 1} or {1 x N}
               :  [{'CSF', 'Skull', 'Scalp'}]

     calc_mode : <optional> calculation method of radius (R)
               :  [1] or 2 or 3
               :  = 1) mean distance of each head
               :  = 2) minimum distance of each head
               :  = 3) maximum distance of each head

   verbose_swt : <optional> switch to output verbose message
               :  [ON] or OFF
 
   check_level : <optional> check level
               :  [0] or 1
               :  = 0) completely
               :  = 1) accept its mismatch of a number of files, sigma 
               :       and layer_tag

 [output]
   void

 [note]
   stored data are as follow:
      XXhead : [Npoint x 3]
       Vhead : [NPoint x 3]
       Fhead : [NPatch x 3] 
     Nvertex : [NL x 2]  : start and end index
      Npatch : [NL x 2]  : start and end index
           R : {NL}  : radii of each layer
     Rmethod : 'MEAN' or 'MAX' or 'MIN' : calculation method
       sigma : {NL} electric conductivities of each layer
   layer_tag : {NL} names of each layer

   @see vb_out_normal.m

 [history]
   2006-12-04 (Sako) initial version
   2007-04-06 (Sako) added mesh_level
   2007-04-10 (Sako) changed default value of layer_tag (by cell array)
   2010-10-25 (Sako) supported the case that XXhead is empty

 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_head_join_files(head_files, file_root, ...
0002   joined_file, sigma, layer_tag, calc_mode, verbose_swt, check_level)
0003 % join plural single layer head files to single head file serially-concatenated
0004 % [usage]
0005 %   vb_head_join_files(head_files, file_root,
0006 %     joined_file, sigma, layer_tag, calc_mode, verbose_swt, check_level)
0007 % [input]
0008 %    head_files : <required> cell array of plural "single layer" head files
0009 %               :  {N x 1} or {1 x N}
0010 %
0011 %     file_root : <optional> root directory for 'head_files'
0012 %               :  [current directory]
0013 %
0014 %   joined_file : <optional> stored file path which has joined data
0015 %               :  ['(file_root)/joined_head.head.mat']
0016 %
0017 %         sigma : <optional> electric conductivities of each layer
0018 %               :  [N x 1] or [1 x N]
0019 %               :  [ [ 1.0  0.0125  1.0 ] ]
0020 %
0021 %     layer_tag : <optional> names of each layer (cell array)
0022 %               :  {N x 1} or {1 x N}
0023 %               :  [{'CSF', 'Skull', 'Scalp'}]
0024 %
0025 %     calc_mode : <optional> calculation method of radius (R)
0026 %               :  [1] or 2 or 3
0027 %               :  = 1) mean distance of each head
0028 %               :  = 2) minimum distance of each head
0029 %               :  = 3) maximum distance of each head
0030 %
0031 %   verbose_swt : <optional> switch to output verbose message
0032 %               :  [ON] or OFF
0033 %
0034 %   check_level : <optional> check level
0035 %               :  [0] or 1
0036 %               :  = 0) completely
0037 %               :  = 1) accept its mismatch of a number of files, sigma
0038 %               :       and layer_tag
0039 %
0040 % [output]
0041 %   void
0042 %
0043 % [note]
0044 %   stored data are as follow:
0045 %      XXhead : [Npoint x 3]
0046 %       Vhead : [NPoint x 3]
0047 %       Fhead : [NPatch x 3]
0048 %     Nvertex : [NL x 2]  : start and end index
0049 %      Npatch : [NL x 2]  : start and end index
0050 %           R : {NL}  : radii of each layer
0051 %     Rmethod : 'MEAN' or 'MAX' or 'MIN' : calculation method
0052 %       sigma : {NL} electric conductivities of each layer
0053 %   layer_tag : {NL} names of each layer
0054 %
0055 %   @see vb_out_normal.m
0056 %
0057 % [history]
0058 %   2006-12-04 (Sako) initial version
0059 %   2007-04-06 (Sako) added mesh_level
0060 %   2007-04-10 (Sako) changed default value of layer_tag (by cell array)
0061 %   2010-10-25 (Sako) supported the case that XXhead is empty
0062 %
0063 % Copyright (C) 2011, ATR All Rights Reserved.
0064 % License : New BSD License(see VBMEG_LICENSE.txt)
0065 
0066 try
0067   % --- CHECK ARGUMENTS --- %
0068   if ~exist('head_files', 'var'),  head_files = []; end;
0069   if ~exist('file_root', 'var'),   file_root = []; end;
0070   if ~exist('joined_file', 'var'), joined_file = []; end;
0071   if ~exist('sigma', 'var'),       sigma = []; end;
0072   if ~exist('layer_tag', 'var'),   layer_tag = []; end;
0073   if ~exist('calc_mode', 'var'),   calc_mode = []; end;
0074   if ~exist('verbose_swt', 'var'), verbose_swt = []; end;
0075   if ~exist('check_level', 'var'), check_level = []; end;
0076   
0077   [head_files, file_root, joined_file, sigma, layer_tag, calc_mode, ...
0078     VERBOSE, check_level] = inner_check_arguments( ...
0079       head_files, file_root, joined_file, sigma, layer_tag, calc_mode, ...
0080       verbose_swt, check_level);
0081 
0082   % --- head_files' check
0083   if ~iscell(head_files)
0084     % do nothing
0085     warning('%s is not cell so it is not required to join', head_files);
0086     return;
0087   end
0088 
0089   % --- MAIN PROCEDURE ------------------------------------------------------- %
0090   %
0091   XXhead = [];
0092   Vhead = [];
0093   Fhead = [];
0094   Nvertex = [];
0095   Npatch = [];
0096   R = [];
0097 
0098   % Rmethod
0099   switch calc_mode
0100     case  1, Rmethod = 'MEAN';
0101     case  2, Rmethod = 'MIN';
0102     case  3, Rmethod = 'MAX';
0103   end
0104   
0105   % sort by average radius
0106   RR = [];
0107   Nfile = size(head_files,2);
0108   for nf = 1:Nfile
0109 %     load([file_root filesep head_files{nf}], 'Vhead');
0110     
0111     Vhead = vb_head_load_vhead([file_root filesep head_files{nf}]);
0112     RR = [RR vb_util_distance_of_points(Vhead, calc_mode)];
0113   end
0114   [R idx] = sort(RR);
0115   Sigma = sigma(idx);
0116   LayerTag = layer_tag(:,idx);
0117   HeadFiles = head_files(:,idx);
0118 
0119   % re-initialize Vhead
0120   Vhead = [];
0121   
0122   begin_v = 1;
0123   begin_p = 1;
0124   Npoint  = 0;
0125   for n = 1:Nfile
0126     data = load([file_root filesep head_files{n}]);
0127       
0128 %     len_v = size(data.XXhead,1);
0129 %     len_p = size(data.Fhead,1);
0130     len_v = size(vb_headinfo_get_vhead(data),1);
0131     len_p = size(vb_headinfo_get_fhead(data),1);
0132 
0133     end_v = begin_v + len_v -1;
0134     end_p = begin_p + len_p -1;
0135     Nvertex = [Nvertex; begin_v end_v];
0136     Npatch  = [Npatch;  begin_p end_p];
0137     begin_v = end_v + 1;
0138     begin_p = end_p + 1;
0139   
0140 %     XXhead = [XXhead; data.XXhead];
0141 %     Vhead = [ Vhead; data.Vhead];
0142 %     Fhead = [ Fhead; data.Fhead + Npoint];
0143     Vhead = [ Vhead; vb_headinfo_get_vhead(data)];
0144     Fhead = [ Fhead; vb_headinfo_get_fhead(data) + Npoint];
0145     
0146     % support empty XXhead
0147     if isempty(data.XXhead)
0148       if ~isempty(data.Vhead) && ~isempty(data.Fhead)
0149         [new_f, new_v, data.XXhead] = vb_out_normal(data.Fhead, data.Vhead);
0150       else
0151         if isempty(data.Vhead), warning('Vhead is empty'); end
0152         if isempty(data.Fhead), warning('Fhead is empty'); end
0153         warning('XXhead is empty');
0154       end
0155     end
0156     XXhead = [XXhead; vb_headinfo_get_xxhead(data)];
0157     
0158     Npoint = Npoint + len_v;
0159   end
0160 
0161 
0162   vb_save(joined_file, ...
0163     'XXhead', 'Vhead', 'Fhead', ...
0164     'Nvertex', 'Npatch', 'R', 'Rmethod', 'Sigma', 'LayerTag');
0165   
0166   if VERBOSE
0167     fprintf('--- saved as current joined file : %s\n', joined_file);
0168   end
0169 
0170 catch
0171   rethrow(lasterror);
0172 end
0173 
0174 % --- INNER FUNCTIONS -------------------------------------------------------- %
0175 %
0176 % --- inner_check_arguments()
0177 %
0178 function [head_files, file_root, joined_file, sigma, layer_tag, calc_mode, ...
0179   VERBOSE, check_level] = inner_check_arguments( ...
0180     head_files, file_root, joined_file, sigma, layer_tag, calc_mode, ...
0181     verbose_swt, check_level)
0182 
0183 % required parameters
0184 if isempty(head_files)
0185   error('head_files is a required parameter');  
0186 end
0187 
0188 % optional parameters
0189 if isempty(file_root)
0190   file_root = '.';
0191 end
0192 
0193 if isempty(joined_file)
0194   joined_file = [file_root filesep 'joined_head.head.mat'];
0195 end
0196 
0197 if isempty(sigma)
0198     sigma = [ 1.0  0.0125  1.0 ];
0199 end
0200 
0201 if isempty(layer_tag)
0202 %   layer_tag = char('CSF', 'Skull', 'Scalp');
0203   layer_tag = {'CSF', 'Skull', 'Scalp'};
0204 end
0205 
0206 if isempty(calc_mode)
0207   calc_mode = 1;  % mean distance
0208 elseif isempty(find(calc_mode == [1 2 3]))
0209   error('undefined calc_mode : %d', calc_mode);
0210 end
0211   
0212 if isempty(verbose_swt)
0213   VERBOSE = ON;
0214 end
0215 
0216 if isempty(check_level)
0217   check_level = 0;
0218 end
0219 
0220 % arrange head files array
0221 head_files = vb_util_arrange_list(head_files, 1);
0222 sigma      = vb_util_arrange_list(sigma, 1);
0223 layer_tag  = vb_util_arrange_list(layer_tag, 1);
0224 
0225 % check consistency of data
0226 if check_level == 0
0227   inner_check_layer_number(head_files, sigma, layer_tag);
0228 
0229   % file check
0230   inner_check_files(head_files, file_root);
0231   
0232 elseif check_level == 1
0233   % do nothing
0234   
0235 else
0236   warning('unexpected check_level : %d', check_level);
0237 end
0238 %
0239 % --- end of inner_check_arguments()
0240 
0241 % --- inner_check_layer_number()
0242 %
0243 function inner_check_layer_number(head_files, sigma, layer_tag)
0244 
0245 Nfiles = size(head_files, 2);
0246 Nsigma = size(sigma, 2);
0247 Nlytag = size(layer_tag, 2);
0248   
0249 if (Nfiles ~= Nsigma) || (Nsigma ~= Nlytag) || (Nlytag ~= Nfiles) 
0250   error('not be consistent [Nfiles,Nsigma,Nlytag] = [%d,%d,%d]', ...
0251     Nfiles, Nsigma, Nlytag);
0252 end
0253 return;
0254 %
0255 % --- end of inner_check_layer_number()
0256 
0257 % --- inner_check_files()
0258 %
0259 function inner_check_files(files, root_dir)
0260 
0261 Nfiles = size(files, 2);
0262 for fn = 1:Nfiles
0263   cur_file = [root_dir filesep files{fn}];
0264   if exist(cur_file, 'file') ~= 2
0265     error('cannot read file : %s', cur_file);
0266   end
0267 end
0268 return;
0269 %
0270 % --- end of inner_check_files()
0271 %
0272 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0273 %%% END OF FILE %%%

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