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

vb_megfile_load_data_internal

PURPOSE ^

load specified data from MEG-MAT file which has data internally

SYNOPSIS ^

function [data, channel_info] = vb_megfile_load_data_internal(megfile, loadspec, new_file, return_swt, verbose_swt, cmplmnt_swt)

DESCRIPTION ^

 load specified data from MEG-MAT file which has data internally

 [usage]
   [data, channel_info] = vb_megfile_load_data_internal(megfile, ...
             loadspec, new_file, return_swt, verbose_swt, cmplmnt_swt)

 [input]
       megfile : <required> <<file>> MEG-MAT file

      loadspec : <required> <<struct>> loading instruction
      .ChannelName : <optional> channel name list [Nch x 1] []
                   :  name(string) list of  target channel
                   :  The way to be used this depends on the
                   :  "ChannelSwitch" field
                   :  e.g.
                   :   {'1';'2';'3';'4'}
                   :  If this is empty, all the channels are
                   :  specified
    .ChannelSwitch : <optiolal> <<boolean>> [true] | false
                   :   Which is "ChannelName", to read, or not?
                   :   [true] : to read
                   :    false : to omit
      .ChannelType : <optional> <<string>> channel type
                   :  [(depend on vb_loadspec_check.m)]
                   :  'MEG'       : MEG channel data
                   :  'EXTRA'     : extra channel data
                   :  'REFERENCE' : reference channel data
                   :  'AXIAL'     : AxialGradioMeter
                   :  'PLANAR'    : PlanarGradioMeter
                   :  'ALL'       : 'MEG'+'EXTRA'+'REFERENCE'
                   :  - case insensitive
          .Trigger : <optional> trigger sample number list []
                   :   [Ntrigger(Ntrial) x 1]
                   :   e.g. [1000;2000;3000]
                   :   if plural triggers are specified to trial data,
                   :   make from sample and to sample by applying the 
                   :   first trigger
       .Pretrigger : <optional> sample number of pretrigger []
      .Posttrigger : <optional> sample number of posttrigger []
      .TrialNumber : <optional> trial number list [] [Ntrial x 1]
                   :   "1" start
                   :    e.g. [1;2;3]
                   :    empty means that all the trials are specified
    .ActiveChannel : <optional> <<boolean>> [false]
                   :  active channel filter switch
                   :    true) active channels
                   :   false) all the channels
      .ActiveTrial : <optional> <<boolean>> [false]
                   :  active trial filter switch
                   :  valid only when data type is Evoked_Raw
                   :    true) active trials
                   :   false) all the trials
          .saveman : <optional> <<struct>>
                   :  if you want to store data externally, this field is
                   :  necessary.
                   :  if this field is not given or is empty, sampling data
                   :  will store internally.
                   :   .data_dir : directory (relative path) for external file
                   :   .precision : usually 'float64'

      new_file : <optional> new MEG-MAT file name which is stored picked up
               :  data and changed information []
               :  if this is empty, new file will not be created
    return_swt : <optional> <<boolean>> 
               :  switch whether return loaded data or not
               :  [true] | false
   verbose_swt : <optional> <<boolean>> 
               :  switch whether output verbose message or not
               :  [true] | false
   cmplmnt_swt : <optional> <<boolean>> [false]
               :  NOT BE SUPPORTED NOW
               :  switch to complement to load data from external data
               :  if you do not get data from internal data
               :    true : try to get from external data
               :   false : do not try
 [output]
          data : read data [Nchannel x Nsample x Ntrial]
  channel_info : <<struct>> channel information of loaded data
               :  .Active [Nchannel x 1]
               :  .Name   [Nchannel x 1]
               :  .Type   [Nchannel x 1]
               :  .ID     [Nchannel x 1]
 [note]
   devided vb_megfile_load_data into external type and this internal one
   See also
     vb_megfile_load_data, vb_megfile_load_data_external
     vb_megfile_make_new_megfile

 [history]
   2008-04-24 (Sako) initial version
   2008-06-05 (Sako) modified interface spec - ChannelType, channel_info
   2009-07-03 (Sako) modified channel_info
   2010-06-30 (Sako) supported storing data externally
   2010-07-01 (Sako) modified how to pick up trial data

 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 [data, channel_info] = vb_megfile_load_data_internal( ...
0002   megfile, loadspec, new_file, return_swt, verbose_swt, cmplmnt_swt)
0003 % load specified data from MEG-MAT file which has data internally
0004 %
0005 % [usage]
0006 %   [data, channel_info] = vb_megfile_load_data_internal(megfile, ...
0007 %             loadspec, new_file, return_swt, verbose_swt, cmplmnt_swt)
0008 %
0009 % [input]
0010 %       megfile : <required> <<file>> MEG-MAT file
0011 %
0012 %      loadspec : <required> <<struct>> loading instruction
0013 %      .ChannelName : <optional> channel name list [Nch x 1] []
0014 %                   :  name(string) list of  target channel
0015 %                   :  The way to be used this depends on the
0016 %                   :  "ChannelSwitch" field
0017 %                   :  e.g.
0018 %                   :   {'1';'2';'3';'4'}
0019 %                   :  If this is empty, all the channels are
0020 %                   :  specified
0021 %    .ChannelSwitch : <optiolal> <<boolean>> [true] | false
0022 %                   :   Which is "ChannelName", to read, or not?
0023 %                   :   [true] : to read
0024 %                   :    false : to omit
0025 %      .ChannelType : <optional> <<string>> channel type
0026 %                   :  [(depend on vb_loadspec_check.m)]
0027 %                   :  'MEG'       : MEG channel data
0028 %                   :  'EXTRA'     : extra channel data
0029 %                   :  'REFERENCE' : reference channel data
0030 %                   :  'AXIAL'     : AxialGradioMeter
0031 %                   :  'PLANAR'    : PlanarGradioMeter
0032 %                   :  'ALL'       : 'MEG'+'EXTRA'+'REFERENCE'
0033 %                   :  - case insensitive
0034 %          .Trigger : <optional> trigger sample number list []
0035 %                   :   [Ntrigger(Ntrial) x 1]
0036 %                   :   e.g. [1000;2000;3000]
0037 %                   :   if plural triggers are specified to trial data,
0038 %                   :   make from sample and to sample by applying the
0039 %                   :   first trigger
0040 %       .Pretrigger : <optional> sample number of pretrigger []
0041 %      .Posttrigger : <optional> sample number of posttrigger []
0042 %      .TrialNumber : <optional> trial number list [] [Ntrial x 1]
0043 %                   :   "1" start
0044 %                   :    e.g. [1;2;3]
0045 %                   :    empty means that all the trials are specified
0046 %    .ActiveChannel : <optional> <<boolean>> [false]
0047 %                   :  active channel filter switch
0048 %                   :    true) active channels
0049 %                   :   false) all the channels
0050 %      .ActiveTrial : <optional> <<boolean>> [false]
0051 %                   :  active trial filter switch
0052 %                   :  valid only when data type is Evoked_Raw
0053 %                   :    true) active trials
0054 %                   :   false) all the trials
0055 %          .saveman : <optional> <<struct>>
0056 %                   :  if you want to store data externally, this field is
0057 %                   :  necessary.
0058 %                   :  if this field is not given or is empty, sampling data
0059 %                   :  will store internally.
0060 %                   :   .data_dir : directory (relative path) for external file
0061 %                   :   .precision : usually 'float64'
0062 %
0063 %      new_file : <optional> new MEG-MAT file name which is stored picked up
0064 %               :  data and changed information []
0065 %               :  if this is empty, new file will not be created
0066 %    return_swt : <optional> <<boolean>>
0067 %               :  switch whether return loaded data or not
0068 %               :  [true] | false
0069 %   verbose_swt : <optional> <<boolean>>
0070 %               :  switch whether output verbose message or not
0071 %               :  [true] | false
0072 %   cmplmnt_swt : <optional> <<boolean>> [false]
0073 %               :  NOT BE SUPPORTED NOW
0074 %               :  switch to complement to load data from external data
0075 %               :  if you do not get data from internal data
0076 %               :    true : try to get from external data
0077 %               :   false : do not try
0078 % [output]
0079 %          data : read data [Nchannel x Nsample x Ntrial]
0080 %  channel_info : <<struct>> channel information of loaded data
0081 %               :  .Active [Nchannel x 1]
0082 %               :  .Name   [Nchannel x 1]
0083 %               :  .Type   [Nchannel x 1]
0084 %               :  .ID     [Nchannel x 1]
0085 % [note]
0086 %   devided vb_megfile_load_data into external type and this internal one
0087 %   See also
0088 %     vb_megfile_load_data, vb_megfile_load_data_external
0089 %     vb_megfile_make_new_megfile
0090 %
0091 % [history]
0092 %   2008-04-24 (Sako) initial version
0093 %   2008-06-05 (Sako) modified interface spec - ChannelType, channel_info
0094 %   2009-07-03 (Sako) modified channel_info
0095 %   2010-06-30 (Sako) supported storing data externally
0096 %   2010-07-01 (Sako) modified how to pick up trial data
0097 %
0098 % Copyright (C) 2011, ATR All Rights Reserved.
0099 % License : New BSD License(see VBMEG_LICENSE.txt)
0100 
0101 % --- CHECK ARGUMENTS --- %
0102 if ~exist('megfile', 'var'), megfile = []; end
0103 if ~exist('loadspec', 'var'), loadspec = []; end
0104 if ~exist('new_file', 'var'), new_file = []; end
0105 if ~exist('return_swt', 'var'), return_swt = []; end
0106 if ~exist('verbose_swt', 'var'), verbose_swt = []; end
0107 if ~exist('cmplmnt_swt', 'var'), cmplmnt_swt = []; end
0108 [meginfo, bexp_src, exdata_src, refdata_src, ...
0109   ch_list, target_sample, tr_list, ...
0110     new_file, return_swt, root_dir, loadspec, VERBOSE, DO_COMPLEMENT] = ...
0111   inner_check_arguments(megfile, loadspec, new_file, return_swt, ...
0112   verbose_swt, cmplmnt_swt);
0113 
0114 % --- MAIN PROCEDURE --------------------------------------------------------- %
0115 %
0116 if VERBOSE >= 5 % temporarily upper than <notice> level
0117   fprintf('  --- now loading ...\n');
0118 end
0119 
0120 n_trial = vb_info_get_Nrepeat(meginfo);
0121 if n_trial == 1
0122   n_trial_new = size(target_sample,1);
0123 else
0124   n_trial_new = length(tr_list);
0125 end
0126 n_sample_new = target_sample(1,2)-target_sample(1,1)+1;
0127 
0128 %
0129 % --- get channel labels
0130 %
0131 meg_labels = vb_meginfo_get_channel_label_meg(meginfo);
0132 ext_labels = vb_meginfo_get_channel_label_extra(meginfo);
0133 ref_labels = vb_meginfo_get_channel_label_refmg(meginfo);
0134 
0135 bexp = [];
0136 exdata = [];
0137 refdata = [];
0138 
0139 %
0140 % --- EXTRA CHANNEL DATA
0141 %
0142 if ~isempty(ext_labels)
0143   [ext_ch_idx] = vb_util_get_index(ext_labels, ch_list);
0144   if ~isempty(ext_ch_idx)
0145     ext_ch_idx = vb_util_arrange_list(ext_ch_idx, 0);
0146   end
0147 else
0148   ext_ch_idx = [];
0149 end
0150 
0151 % if MEGfile has 'bexp_ext' data, pick up from it
0152 if ~isempty(exdata_src) && ~isempty(ext_ch_idx)
0153   n_channel_new = length(ext_ch_idx);
0154   exdata = zeros(n_channel_new, n_sample_new, n_trial_new);
0155   
0156   if n_trial == 1
0157     for i_trial = 1:n_trial_new
0158       exdata(:,:,i_trial) = exdata_src(ext_ch_idx, ...
0159           target_sample(i_trial,1):target_sample(i_trial,2));
0160     end
0161   else
0162     exdata = exdata_src(ext_ch_idx, ...
0163       target_sample(1,1):target_sample(1,2), tr_list);
0164   end
0165 end
0166 
0167 %
0168 % --- MEG CHANNEL DATA
0169 %
0170 if ~isempty(meg_labels)
0171   [meg_ch_idx] = vb_util_get_index(meg_labels, ch_list);
0172   if ~isempty(meg_ch_idx)
0173     meg_ch_idx = vb_util_arrange_list(meg_ch_idx, 0);
0174   end
0175 else
0176   meg_ch_idx = [];
0177 end
0178 
0179 % if MEGfile has 'bexp' data, pick up from it
0180 if ~isempty(bexp_src) && ~isempty(meg_ch_idx)
0181   n_channel_new = length(meg_ch_idx);
0182   bexp = zeros(n_channel_new, n_sample_new, n_trial_new);
0183   
0184   if n_trial == 1
0185     for i_trial = 1:n_trial_new
0186       bexp(:,:,i_trial) = bexp_src(meg_ch_idx, ...
0187         target_sample(i_trial,1):target_sample(i_trial,2));
0188     end
0189   else
0190     bexp = bexp_src(meg_ch_idx, ...
0191       target_sample(1,1):target_sample(1,2), tr_list);
0192   end
0193 end
0194 
0195 
0196 %
0197 % --- REFERENCE MAGNETOMETER CHANNEL DATA
0198 %
0199 if ~isempty(ref_labels)
0200   [ref_ch_idx] = vb_util_get_index(ref_labels, ch_list);
0201   if ~isempty(ref_ch_idx)
0202     ref_ch_idx = vb_util_arrange_list(ref_ch_idx, 0);
0203   end
0204 else
0205   ref_ch_idx = [];
0206 end
0207 
0208 % if MEGfile has 'eeg' data, pick up from it
0209 if ~isempty(refdata_src) && ~isempty(ref_ch_idx)
0210   n_channel_new = length(ref_ch_idx);
0211   refdata = zeros(n_channel_new, n_sample_new, n_trial_new);
0212   
0213   if n_trial == 1
0214     for i_trial = 1:n_trial_new
0215       refdata(:,:,i_trial) = refdata_src(ref_ch_idx, ...
0216         target_sample(i_trial,1):target_sample(i_trial,2));
0217     end
0218   else
0219     refdata = refdata_src(ref_ch_idx, ...
0220       target_sample(1,1):target_sample(1,2), tr_list);
0221   end
0222 end
0223 
0224 %
0225 % --- get read channels' label
0226 %
0227 read_meg_ch = meg_labels(meg_ch_idx);
0228 read_ext_ch = ext_labels(ext_ch_idx);
0229 read_ref_ch = ref_labels(ref_ch_idx);
0230 
0231 
0232 % if DO_COMPLEMENT
0233 %   % if MEGfile does not have 'bexp' nor 'eeg'
0234 %   if isempty(bexp) && isempty(exdata) && isempty(refdata)
0235 %     if ~isempty(read_meg_ch)
0236 %       bexp = vb_meginfo_read_data_file( ...
0237 %         meginfo, read_meg_ch, target_sample, 1, tr_label_list, root_dir);
0238 %     else
0239 %       bexp = [];
0240 %     end
0241 %
0242 %     if ~isempty(read_ext_ch)
0243 %       exdata = vb_meginfo_read_data_file( ...
0244 %         meginfo, read_ext_ch, target_sample, 1, tr_label_list, root_dir);
0245 %     else
0246 %       exdata = [];
0247 %     end
0248 %
0249 %     if ~isempty(read_ref_ch)
0250 %       refdata = vb_meginfo_read_data_file( ...
0251 %         meginfo, read_ref_ch, target_sample, 1, tr_label_list, root_dir);
0252 %     else
0253 %       refdata = [];
0254 %     end
0255 %
0256 %   elseif isempty(bexp) && ~isempty(meg_ch_idx)
0257 %
0258 %     bexp = vb_meginfo_read_data_file(meginfo, ...
0259 %       ch_list(meg_ch_idx), target_sample, [], tr_label_list, root_dir);
0260 %
0261 %   elseif isempty(exdata) && ~isempty(ext_ch_idx)
0262 %
0263 %     exdata = vb_meginfo_read_data_file(meginfo, ...
0264 %       ch_list(ext_ch_idx), target_sample, [], tr_label_list, root_dir);
0265 %
0266 %   elseif isempty(refdata) && ~isempty(ref_ch_idx)
0267 %
0268 %     refdata = vb_meginfo_read_data_file(meginfo, ...
0269 %       ch_list(ref_ch_idx), target_sample, [], tr_label_list, root_dir);
0270 %
0271 %   else
0272 %     % --- do nothing
0273 %   end
0274 % end
0275 
0276 % --- actions after loading
0277 
0278 % ----- save new meg-mat file
0279 if ~isempty(new_file)
0280   new_acqtype = vb_meginfo_get_next_acq_type(meginfo);
0281 
0282   vb_megfile_make_new_megfile(megfile, new_file, loadspec, ...
0283     read_meg_ch, read_ext_ch, read_ref_ch, ...
0284     bexp, exdata, refdata, new_acqtype, target_sample);
0285 end
0286 
0287 
0288 %
0289 % --- return values
0290 %
0291 % ----- arrange data
0292 data = [bexp;exdata;refdata];
0293 
0294 % ------- ChannelInfo
0295 %   .Active
0296 %   .Name
0297 %   .Type
0298 %   .ID
0299 channel_info = vb_megfile_make_channel_info(megfile, ...
0300   read_meg_ch, read_ext_ch, read_ref_ch);
0301 
0302 
0303 % ----- decide return value
0304 if ~return_swt
0305   data = [];
0306 end
0307 return;
0308 %
0309 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0310 
0311 % --- INNER FUNCTIONS -------------------------------------------------------- %
0312 %
0313 % --- inner_check_arguments()
0314 %
0315 function [meginfo, meg_data, ext_data, ref_data, ...
0316     read_ch_list, target_samples, trial_idx, ...
0317     new_file, return_swt, root_dir, loadspec, verbose_level, cmplmnt_swt] = ...
0318   inner_check_arguments(megfile, loadspec, new_file, return_swt, ...
0319   verbose_level, cmplmnt_swt)
0320 func_ = mfilename;
0321 
0322 if isempty(megfile)
0323   error('(%s)megfile is a required parameter', func_);
0324 end
0325 
0326 if exist(megfile, 'file') ~= 2
0327   error('(%s)cannot find megfile : %s', func_, megfile);
0328 end
0329 
0330 meginfo = vb_megfile_load_meginfo(megfile);
0331 
0332 % ----- megfile directory
0333 root_dir = vb_get_file_parts(megfile);
0334 
0335 
0336 % ----- load stored data on megfile
0337 meg_data = vb_megfile_get_internal_data_meg(megfile);
0338 ext_data = vb_megfile_get_internal_data_ext(megfile);
0339 ref_data = vb_megfile_get_internal_data_ref(megfile);
0340 
0341 % ----- complement LoadSpec
0342 loadspec = vb_loadspec_check(loadspec, megfile);
0343 
0344 % ----- match channels to read [Nch x 1]
0345 read_ch_list = loadspec.ChannelName;
0346 
0347 
0348 target_samples = vb_loadspec_make_trial_sample(loadspec);
0349 if isempty(target_samples)
0350   n_sample = vb_info_get_sample_number(meginfo);
0351   target_samples = [1 n_sample];
0352 end
0353 
0354 
0355 % ----- trial list
0356 % ----- target samples [Ntrial x 2]
0357 N_trial = vb_info_get_Nrepeat(meginfo);
0358 if N_trial == 1
0359 %   trial_list = [];
0360   trial_idx = [];
0361 
0362 else
0363   org_info = vb_load_measurement_info(megfile);
0364   org_trial = org_info.Trial;
0365   org_trial_idx = [org_trial.number];
0366   
0367   if size(target_samples, 1) > 1
0368     fprintf('(%s) loadspec.Trigger may be wrong\n', mfilename);
0369     target_samples = target_samples(1,:);
0370   end
0371   
0372   if isempty(loadspec.TrialNumber)
0373     cur_trial = vb_info_get_trial_data(meginfo);
0374     trial_idx = 1:length(cur_trial);
0375   else
0376     cur_trial = vb_info_get_trial_data(meginfo, loadspec.TrialNumber);
0377     dst_trial_idx = [cur_trial.number];
0378     [result, trial_idx] = ...
0379       vb_util_check_numerical_lists(org_trial_idx, dst_trial_idx);
0380   end     % if ~isempty(loadspec.TrialNumber)
0381   
0382   if length(trial_idx) ~= length(cur_trial)
0383     error('(%s) bad trial_idx', func_);
0384   end
0385 
0386   if loadspec.ActiveTrial && isequal(org_trial_idx(:), loadspec.TrialNumber(:));
0387     % if loadspec.TrialNumber not specified,
0388     % load only active trial data.
0389     active_list = vb_info_get_active_trial(meginfo);
0390     if isempty(active_list)
0391         active_list = [cur_trial.Active];
0392     end
0393     trial_idx = trial_idx(active_list==true);
0394   end
0395 end
0396 loadspec.TrialNumber = trial_idx;
0397 
0398 if isempty(new_file)
0399   % require no action
0400 end
0401 
0402 if isempty(return_swt)
0403   return_swt = true;
0404 end
0405 
0406 if isempty(new_file) && ~return_swt
0407   error('(%s)wrong combination - empty output file and no return', func_);
0408 end
0409 
0410 if isempty(verbose_level)
0411   verbose_level = 1;
0412 end
0413 
0414 % if isempty(cmplmnt_swt)
0415 %   cmplmnt_swt = false;
0416 % end
0417 cmplmnt_swt = false;
0418 return;
0419 %
0420 % --- end of inner_check_arguments()
0421 %
0422 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0423 
0424 % --- END OF FILE --- %

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