Home > vbmeg > 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'

   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]
               :  [ basis_parm = vb_set_basis_parm; basis_parm.sigma ]

     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
   2017-03-27 (rhayashi) specify file_root = [], just use the path of head_files.  

 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 %
0013 %   joined_file : <optional> stored file path which has joined data
0014 %               :  ['(file_root)/joined_head.head.mat']
0015 %
0016 %         sigma : <optional> electric conductivities of each layer
0017 %               :  [N x 1] or [1 x N]
0018 %               :  [ basis_parm = vb_set_basis_parm; basis_parm.sigma ]
0019 %
0020 %     layer_tag : <optional> names of each layer (cell array)
0021 %               :  {N x 1} or {1 x N}
0022 %               :  [{'CSF', 'Skull', 'Scalp'}]
0023 %
0024 %     calc_mode : <optional> calculation method of radius (R)
0025 %               :  [1] or 2 or 3
0026 %               :  = 1) mean distance of each head
0027 %               :  = 2) minimum distance of each head
0028 %               :  = 3) maximum distance of each head
0029 %
0030 %   verbose_swt : <optional> switch to output verbose message
0031 %               :  [ON] or OFF
0032 %
0033 %   check_level : <optional> check level
0034 %               :  [0] or 1
0035 %               :  = 0) completely
0036 %               :  = 1) accept its mismatch of a number of files, sigma
0037 %               :       and layer_tag
0038 %
0039 % [output]
0040 %   void
0041 %
0042 % [note]
0043 %   stored data are as follow:
0044 %      XXhead : [Npoint x 3]
0045 %       Vhead : [NPoint x 3]
0046 %       Fhead : [NPatch x 3]
0047 %     Nvertex : [NL x 2]  : start and end index
0048 %      Npatch : [NL x 2]  : start and end index
0049 %           R : {NL}  : radii of each layer
0050 %     Rmethod : 'MEAN' or 'MAX' or 'MIN' : calculation method
0051 %       sigma : {NL} electric conductivities of each layer
0052 %   layer_tag : {NL} names of each layer
0053 %
0054 %   @see vb_out_normal.m
0055 %
0056 % [history]
0057 %   2006-12-04 (Sako) initial version
0058 %   2007-04-06 (Sako) added mesh_level
0059 %   2007-04-10 (Sako) changed default value of layer_tag (by cell array)
0060 %   2010-10-25 (Sako) supported the case that XXhead is empty
0061 %   2017-03-27 (rhayashi) specify file_root = [], just use the path of head_files.
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     f = fullfile(file_root, head_files{nf});
0111     Vhead = vb_head_load_vhead(f);
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(fullfile(file_root, 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 = fullfile(file_root, 'joined_head.head.mat');
0195 end
0196 
0197 if isempty(sigma)
0198     basis_parm = vb_set_basis_parm;
0199     sigma = basis_parm.sigma;
0200 end
0201 
0202 if isempty(layer_tag)
0203 %   layer_tag = char('CSF', 'Skull', 'Scalp');
0204   layer_tag = {'CSF', 'Skull', 'Scalp'};
0205 end
0206 
0207 if isempty(calc_mode)
0208   calc_mode = 1;  % mean distance
0209 elseif isempty(find(calc_mode == [1 2 3]))
0210   error('undefined calc_mode : %d', calc_mode);
0211 end
0212   
0213 if isempty(verbose_swt)
0214   VERBOSE = OFF;
0215 end
0216 
0217 if isempty(check_level)
0218   check_level = 0;
0219 end
0220 
0221 % arrange head files array
0222 head_files = vb_util_arrange_list(head_files, 1);
0223 sigma      = vb_util_arrange_list(sigma, 1);
0224 layer_tag  = vb_util_arrange_list(layer_tag, 1);
0225 
0226 % check consistency of data
0227 if check_level == 0
0228   inner_check_layer_number(head_files, sigma, layer_tag);
0229 
0230   % file check
0231   inner_check_files(head_files, file_root);
0232   
0233 elseif check_level == 1
0234   % do nothing
0235   
0236 else
0237   warning('unexpected check_level : %d', check_level);
0238 end
0239 %
0240 % --- end of inner_check_arguments()
0241 
0242 % --- inner_check_layer_number()
0243 %
0244 function inner_check_layer_number(head_files, sigma, layer_tag)
0245 
0246 Nfiles = size(head_files, 2);
0247 Nsigma = size(sigma, 2);
0248 Nlytag = size(layer_tag, 2);
0249   
0250 if (Nfiles ~= Nsigma) || (Nsigma ~= Nlytag) || (Nlytag ~= Nfiles) 
0251   error('not be consistent [Nfiles,Nsigma,Nlytag] = [%d,%d,%d]', ...
0252     Nfiles, Nsigma, Nlytag);
0253 end
0254 return;
0255 %
0256 % --- end of inner_check_layer_number()
0257 
0258 % --- inner_check_files()
0259 %
0260 function inner_check_files(files, root_dir)
0261 
0262 Nfiles = size(files, 2);
0263 for fn = 1:Nfiles
0264   cur_file = fullfile(root_dir, files{fn});
0265   if exist(cur_file, 'file') ~= 2
0266     error('cannot read file : %s', cur_file);
0267   end
0268 end
0269 return;
0270 %
0271 % --- end of inner_check_files()
0272 %
0273 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0274 %%% END OF FILE %%%

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005