Home > vbmeg > functions > device > meg > vb_megfile_import_extra_data.m

vb_megfile_import_extra_data

PURPOSE ^

import extra data to a megfile

SYNOPSIS ^

function vb_megfile_import_extra_data(megfile, extdata, ch_info, ow_flag, newfile)

DESCRIPTION ^

 import extra data to a megfile
 [usage]
   vb_megfile_import_extra_data(megfile, extdata, ch_info, ow_flag, newfile)
 [input]
  megfile : <required> MEG-MAT file for base
  extdata : <required> extra data which will be appended as extra channel data
          :  [n_channel x n_sample x n_trial]
  ch_info : <required> <<struct>> ChannelInfo type struct
          :  the fields of which are as follows.
          :  .Active <optional> [n_channel x 1 boolean] [1]
          :  .Name   <required> {n_channel x 1 cell} - must be unique
          :  .Type   <optional> [n_channel x 1 double]  [-999]
          :  You can make sure defined 'TYPES' in [note].
  ow_flag : <optional> overwrite flag
          :  [0]) overlapped ID data will not be overwritten
          :   1 ) overlapped ID data will be forcely overwritten
  newfile : <optional> file to be saved updated data [megfile]
          :  -notice- If this is not given, megfile will be overwritten.
 [output]
   none
 [note]
   defined channel types:
     0) Null Channel
     1) MagnetoMeter
     2) AxialGradioMeter
     3) PlannarGradioMeter
    -1) TriggerChannel
    -2) EegChannel
    -3) EcgChannel
    -4) EtcChannel
   Extra channels' ID are automatically determined.
 [history]
   2010-06-17 (Sako) initial version
   2011-02-17 (Sako) changed how to call vb_saveman_get_dir

 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_megfile_import_extra_data(megfile, extdata, ch_info, ow_flag, newfile)
0002 % import extra data to a megfile
0003 % [usage]
0004 %   vb_megfile_import_extra_data(megfile, extdata, ch_info, ow_flag, newfile)
0005 % [input]
0006 %  megfile : <required> MEG-MAT file for base
0007 %  extdata : <required> extra data which will be appended as extra channel data
0008 %          :  [n_channel x n_sample x n_trial]
0009 %  ch_info : <required> <<struct>> ChannelInfo type struct
0010 %          :  the fields of which are as follows.
0011 %          :  .Active <optional> [n_channel x 1 boolean] [1]
0012 %          :  .Name   <required> {n_channel x 1 cell} - must be unique
0013 %          :  .Type   <optional> [n_channel x 1 double]  [-999]
0014 %          :  You can make sure defined 'TYPES' in [note].
0015 %  ow_flag : <optional> overwrite flag
0016 %          :  [0]) overlapped ID data will not be overwritten
0017 %          :   1 ) overlapped ID data will be forcely overwritten
0018 %  newfile : <optional> file to be saved updated data [megfile]
0019 %          :  -notice- If this is not given, megfile will be overwritten.
0020 % [output]
0021 %   none
0022 % [note]
0023 %   defined channel types:
0024 %     0) Null Channel
0025 %     1) MagnetoMeter
0026 %     2) AxialGradioMeter
0027 %     3) PlannarGradioMeter
0028 %    -1) TriggerChannel
0029 %    -2) EegChannel
0030 %    -3) EcgChannel
0031 %    -4) EtcChannel
0032 %   Extra channels' ID are automatically determined.
0033 % [history]
0034 %   2010-06-17 (Sako) initial version
0035 %   2011-02-17 (Sako) changed how to call vb_saveman_get_dir
0036 %
0037 % Copyright (C) 2011, ATR All Rights Reserved.
0038 % License : New BSD License(see VBMEG_LICENSE.txt)
0039 
0040 % --- CHECK ARGUMENTS --- %
0041 if ~exist('megfile', 'var'), megfile = ''; end
0042 if ~exist('extdata', 'var'), extdata = []; end
0043 if ~exist('ch_info', 'var'), ch_info = []; end
0044 if ~exist('ow_flag', 'var'), ow_flag = []; end
0045 if ~exist('newfile', 'var'), newfile = ''; end
0046 [megfile, extdata, ch_info, ow_flag, newfile] = ...
0047   inner_check_arguments(megfile, extdata, ch_info, ow_flag, newfile);
0048 
0049 % --- MAIN PROCEDURE --------------------------------------------------------- %
0050 %
0051 func_ = mfilename;
0052 
0053 org_info = vb_load_measurement_info(megfile);
0054 n_sp_org = vb_info_get_sample_number(org_info);
0055 n_tr_org = vb_info_get_trial_number(org_info);
0056 
0057 [n_ch_ext, n_sp_ext, n_tr_ext] = size(extdata);
0058 
0059 % --- confirm whether extdata is valid or not
0060 if n_sp_org ~= n_sp_ext
0061   error('(%s) number of sample must be the same (%d, %d)', ...
0062     func_, n_sp_org, n_sp_ext);
0063 end
0064 
0065 if n_tr_org ~= n_tr_ext
0066   error('(%s) number of trial must be the same (%d, %d)', ...
0067     func_, n_tr_org, n_tr_ext);
0068 end
0069 
0070 % --- overlap test
0071 % ----- Name
0072 idx_name_meg = [];
0073 idx_name_ext = [];
0074 
0075 % ------- MEG Channel
0076 org_meg_name_list = org_info.MEGch_name;
0077 if ~isempty(org_meg_name_list)
0078   [result_name_meg, idx_name_meg] = vb_util_check_string_lists( ...
0079     org_meg_name_list, ch_info.Name);
0080 
0081   if ~isempty(idx_name_meg) && ow_flag == 0
0082     for i_ch = 1:length(idx_name_meg)
0083       fprintf('(%s)', org_meg_name_list{idx_name_meg(i_ch)});
0084     end
0085     error('(%s) some ch_info.Name has been always used', func_);
0086   end
0087 end
0088 
0089 % ------- Extra Channel
0090 org_ext_name_list = org_info.ExtraChannelInfo.Channel_name;
0091 if ~isempty(org_ext_name_list)
0092   [result_name_ext, idx_name_ext] = vb_util_check_string_lists( ...
0093     org_ext_name_list, ch_info.Name);
0094 
0095   if ~isempty(idx_name_ext) && ow_flag == 0
0096     for i_ch = 1:length(idx_name_ext)
0097       fprintf('(%s)', org_ext_name_list{idx_name_ext(i_ch)});
0098     end
0099     error('(%s) some ch_info.Name has been always used', func_);
0100   end
0101 end
0102 
0103 if isempty(idx_name_meg) && isempty(idx_name_ext) ...
0104   % force ow_flag to set 0 and extdata would be added as ExtraChannel Data
0105   ow_flag = 0;
0106 end
0107 
0108 % --- load original whole data
0109 org_data = load(megfile);
0110 const = vb_define_yokogawa;
0111 data_dir = vb_saveman_get_dir(org_info.saveman);
0112 
0113 
0114 if ow_flag == 0
0115   % --- not be allowed to overwrite
0116   % --- update ExtraChannelInfo
0117   [org_ext_info] = inner_update_extra_ch_info(org_data.MEGinfo, ch_info);
0118   
0119 %   org_ext_info = org_data.MEGinfo.ExtraChannelInfo;
0120 %   n_ch_org = size(org_ext_info.Channel_active, 1);
0121 %
0122 %   % --- resolve new ch_info.ID
0123 %   if ~isempty(org_ext_info.Channel_id)
0124 %     last_ch_id_org = max(org_ext_info.Channel_id);
0125 %   elseif ~isempty(org_data.MEGinfo.MEGch_id)
0126 %     last_ch_id_org = max(org_data.MEGinfo.MEGch_id);
0127 %   else
0128 %     last_ch_id_org = 0;
0129 %   end
0130 %   ch_info.ID = [];
0131 %
0132 %   next_ch_id = last_ch_id_org + 1;
0133 %   for i_ch = 1:n_ch_ext
0134 %     ch_info.ID = [ch_info.ID; next_ch_id];
0135 %     next_ch_id = next_ch_id + 1;
0136 %   end
0137 %
0138 %   org_ext_info.Channel_active = [org_ext_info.Channel_active; ch_info.Active];
0139 %   org_ext_info.Channel_type   = [org_ext_info.Channel_type;   ch_info.Type];
0140 %   org_ext_info.Channel_id     = [org_ext_info.Channel_id;     ch_info.ID];
0141 %
0142 %   for i_ch = 1:n_ch_ext
0143 %     org_ext_info.Channel_name{n_ch_org+i_ch} = ch_info.Name{i_ch};
0144 %   end
0145 %
0146 %   if n_ch_org == 0
0147 %     org_ext_info.Channel_name = org_ext_info.Channel_name';
0148 %   end
0149 
0150   org_data.MEGinfo.ExtraChannelInfo = org_ext_info;
0151 
0152   % ----- check internal data
0153   if inner_is_stored_externally(megfile)
0154 
0155     % store externally
0156     f_path = vb_get_file_parts(newfile);
0157     PRECISION = vb_meginfo_get_precision(org_info);
0158 
0159     for i_ch = 1:n_ch_ext
0160       inner_make_external_data_file( ...
0161         f_path, data_dir, ch_info.Name{i_ch}, const.EXT_CHANNEL_BIN, ...
0162         PRECISION, extdata(i_ch,:,:));
0163     end
0164 
0165   else
0166     % store internally
0167     for i_ch = 1:n_ch_ext
0168       org_data.bexp_ext = [org_data.bexp_ext; extdata(i_ch,:,:)];
0169     end
0170   end
0171 
0172 % ---------------------------------------------------------------------------- %
0173 else
0174   % --- overwritable
0175 
0176   % ----- devide ch_info into next 3 types
0177   ch_info_idx_meg = [];
0178   ch_info_idx_ext = [];
0179   ch_info_idx_new = [];
0180   
0181   info = [];
0182   if ~isempty(idx_name_meg)
0183     % --- MEG channel data
0184     ow_name = org_meg_name_list(idx_name_meg);
0185  
0186     % --- get index of ch_info
0187     [res_tmp, idx_ext] = vb_util_check_string_lists(ch_info.Name, ow_name);
0188     ch_info_idx_meg = idx_ext;
0189     
0190     % --- update Actve Channel and Type
0191     info = org_data.MEGinfo;
0192     for i_ch = 1:length(idx_name_meg)
0193       idx_org = idx_name_meg(i_ch);
0194       idx_new = idx_ext(i_ch);
0195       info.ActiveChannel(idx_org)      = ch_info.Active(idx_new);
0196       info.ChannelInfo.Active(idx_org) = ch_info.Active(idx_new);
0197       info.ChannelInfo.Type(idx_org)   = ch_info.Type(idx_new);
0198     end
0199   end
0200   % --- update MEGinfo
0201   if ~isempty(info)
0202     org_data.MEGinfo = info;
0203     info = [];
0204   end
0205   
0206   if ~isempty(idx_name_ext)
0207     % --- MEG channel data
0208     ow_name = org_ext_name_list(idx_name_ext);
0209 
0210     % --- get index of ch_info
0211     [res_tmp, idx_ext] = vb_util_check_string_lists(ch_info.Name, ow_name);
0212     ch_info_idx_ext = idx_ext;
0213 
0214     % --- update Actve Channel and Type
0215     info = org_data.MEGinfo.ExtraChannelInfo;
0216     for i_ch = 1:length(idx_name_ext)
0217       idx_org = idx_name_ext(i_ch);
0218       idx_new = idx_ext(i_ch);
0219       info.Channel_active(idx_org) = ch_info.Active(idx_new);
0220       info.Channel_type(idx_org)   = ch_info.Type(idx_new);
0221     end
0222   end
0223   % --- update MEGinfo.ExtraChannelInfo
0224   if ~isempty(info)
0225     org_data.MEGinfo.ExtraChannelInfo = info;
0226     info = [];
0227   end
0228   
0229   % --- check whether undefined name channels are given or not
0230   whole_list = [org_meg_name_list; org_ext_name_list];
0231   [new_list, idx] = vb_util_omit_list(ch_info.Name, whole_list);
0232   ch_info_idx_new = idx;
0233   
0234   cur_ch_info.Name   = new_list;
0235   cur_ch_info.Active = ch_info.Active(idx);
0236   cur_ch_info.Type   = ch_info.Type(idx);
0237   
0238   [ext_info] = inner_update_extra_ch_info(org_data.MEGinfo, cur_ch_info);
0239   org_data.MEGinfo.ExtraChannelInfo = ext_info;
0240   
0241   % --- update data
0242   if inner_is_stored_externally(megfile)
0243 
0244     % --- store externally
0245     f_path = vb_get_file_parts(newfile);
0246     PRECISION = vb_meginfo_get_precision(org_info);
0247 
0248     for i_ch = 1:n_ch_ext
0249       inner_make_external_data_file( ...
0250         f_path, data_dir, ch_info.Name{i_ch}, const.EXT_CHANNEL_BIN, ...
0251         PRECISION, extdata(i_ch,:,:));
0252     end
0253 
0254   else
0255     % --- store internally
0256     if ~isempty(ch_info_idx_meg)
0257       % --- replace
0258       org_data.bexp(idx_name_meg,:,:) = extdata(ch_info_idx_meg,:,:);
0259     end
0260     
0261     if ~isempty(ch_info_idx_ext)
0262       % --- replace
0263       org_data.bexp_ext(idx_name_ext,:,:) = extdata(ch_info_idx_ext,:,:);
0264     end
0265     
0266     if ~isempty(ch_info_idx_new)
0267       % --- append
0268       org_data.bexp_ext = [org_data.bexp_ext; extdata(ch_info_idx_new,:,:)];
0269     end
0270   end
0271 end
0272 
0273 % --- make new file
0274 vb_save_struct(newfile, org_data);
0275 return;
0276 %
0277 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0278 
0279 % --- INNER FUNCTIONS -------------------------------------------------------- %
0280 %
0281 % --- inner_check_arguments()
0282 %
0283 function [megfile, extdata, ch_info, ow_flag, newfile] = ...
0284   inner_check_arguments(megfile, extdata, ch_info, ow_flag, newfile)
0285 
0286 func_ = mfilename;
0287 % --- megfile
0288 if isempty(megfile)
0289   error('(%s) megfile is a required parameter', func_);
0290 end
0291 
0292 if exist(megfile, 'file') ~= 2
0293   error('(%s) cannot find megfile %s', func_, megfile);
0294 end
0295 
0296 % --- extdata
0297 if isempty(extdata)
0298   error('(%s) extdata is a required parameter', func_);
0299 end
0300 
0301 % --- ch_info
0302 if isempty(ch_info)
0303   error('(%s) ch_info is a required parameter', func_);
0304 
0305 elseif ~isfield(ch_info, 'Active')
0306   ch_info.Active = 1;
0307 
0308 elseif ~isfield(ch_info, 'Type')
0309   ch_info.Type   = -9999;
0310 
0311 elseif ~isfield(ch_info, 'Name') || isempty(ch_info.Name)
0312   error('(%s) Name is a required field of ch_info', func_);
0313 end
0314 
0315 % --- ow_flag
0316 if isempty(ow_flag)
0317   ow_flag = 0;
0318 end
0319 
0320 % --- newfile
0321 if isempty(newfile)
0322   newfile = megfile;
0323 end
0324 return;
0325 %
0326 % --- end of inner_check_arguments()
0327 
0328 % --- inner_is_stored_externally()
0329 %
0330 function [result] = inner_is_stored_externally(megfile)
0331 
0332 result = 0;
0333 
0334 [state_bexp, guide_const] = vb_util_check_variable_in_matfile(megfile, 'bexp');
0335 [state_bexp_ext] = vb_util_check_variable_in_matfile(megfile, 'bexp_ext');
0336 [state_refmg] = vb_util_check_variable_in_matfile(megfile, 'refmg');
0337 
0338 if state_bexp ~= guide_const.VALID ...
0339     && state_bexp_ext ~= guide_const.VALID ...
0340     && state_refmg ~= guide_const.VALID
0341   % --- this means storing externally
0342   result = 1;
0343 else
0344   % --- this means storing internally
0345   result = 0;
0346 end
0347 return;
0348 %
0349 % --- end of inner_is_stored_externally()
0350 
0351 % --- inner_make_external_data_file()
0352 %
0353 function inner_make_external_data_file(fpath, d_dir, fname, f_ext, prec, data)
0354 func_ = mfilename;
0355 
0356 ch_file = sprintf('%s/%s/%s.%s', fpath, d_dir, fname, f_ext);
0357 fid = fopen(ch_file, 'wb');
0358 if fid == -1
0359   error('(%s)cannot open file (%s)', func_, ch_file);
0360 end
0361 
0362 fwrite(fid, data(:), prec);
0363 fclose(fid);
0364 return;
0365 %
0366 % --- end of inner_make_external_data_file()
0367 
0368 
0369 % --- inner_update_extra_ch_info()
0370 %
0371 function [new_ext_info] = inner_update_extra_ch_info(meg_info, ch_info)
0372 
0373 ext_info = meg_info.ExtraChannelInfo;
0374 
0375 n_ch_org = size(ext_info.Channel_active, 1);
0376 n_ch_ext = size(ch_info.Active, 1);
0377 
0378 % --- resolve new ch_info.ID
0379 if ~isempty(ext_info.Channel_id)
0380   last_ch_id_org = max(ext_info.Channel_id);
0381 elseif ~isempty(meg_info.MEGch_id)
0382   last_ch_id_org = max(meg_info.MEGch_id);
0383 else
0384   last_ch_id_org = 0;
0385 end
0386 ch_info.ID = [];
0387   
0388 next_ch_id = last_ch_id_org + 1;
0389 for i_ch = 1:n_ch_ext
0390   ch_info.ID = [ch_info.ID; next_ch_id];
0391   next_ch_id = next_ch_id + 1;
0392 end
0393   
0394 ext_info.Channel_active = [ext_info.Channel_active; ch_info.Active];
0395 ext_info.Channel_type   = [ext_info.Channel_type;   ch_info.Type];
0396 ext_info.Channel_id     = [ext_info.Channel_id;     ch_info.ID];
0397 
0398 for i_ch = 1:n_ch_ext
0399   ext_info.Channel_name{n_ch_org+i_ch} = ch_info.Name{i_ch};
0400 end
0401 
0402 if n_ch_org == 0
0403   ext_info.Channel_name = ext_info.Channel_name';
0404 end
0405 
0406 new_ext_info = ext_info;
0407 return;
0408 %
0409 % --- end of inner_update_extra_ch_info()
0410 %
0411 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0412 
0413 % --- END OF FILE --- %

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