Home > vbmeg > functions > common > loadfunc > vb_load_meg_data.m

vb_load_meg_data

PURPOSE ^

load measured data of MEG (meg.mat) and EEG (eeg.mat).

SYNOPSIS ^

function [data, channel_info, time_info] = vb_load_meg_data(megfile, load_spec, new_file)

DESCRIPTION ^

 load measured data of MEG (meg.mat) and EEG (eeg.mat).

 This function supports three formats of megfile; Standard MEG format, Minimum MEG format and fileinfo format.
 By default, this function load only active channels and active trials kept in  
 MEGinfo.ActiveChannel and MEGinfo.ActiveTrial in case of Standard MEG format or
 fileinfo.channel_id and fileinfo.act_trial in case of fileinfo format.  
 
 [Usage]
 1. Load active channels and trials (Default) 
    [data] = vb_load_meg_data(megfile);

 2. Load all channels including inactive channels and trials
    load_spec.ActiveChannel = false;
    load_spec.ActiveTrial   = false;
    [data] = vb_load_meg_data(megfile, load_spec);

 [input]
     megfile : <required> <<file>> measurement file
             :   meg-mat file(.meg.mat)
             :   eeg-mat file(.eeg.mat)

   load_spec : <optional> <<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 ['MEG(EEG)']
                   :  'MEG'       : MEG channel data (only MEG)
                   :  'EEG'       : EEG channel data (only EEG)
                   :  'EXTRA'     : extra channel data
                   :  'REFERENCE' : reference channel data (only MEG)
                   :  'AXIAL'     : AxialGradioMeter (only MEG)
                   :  'PLANAR'    : PlanarGradioMeter (only MEG)
                   :  'ALL'       : 'MEG(EEG)'+'EXTRA'(+'REFERENCE')
                   :  'MAG_X'     : Magnetometer OPM channel data toward X-axis (only QZFM device)
                   :  'MAG_Y'     : Magnetometer OPM channel data toward Y-axis (only QZFM device)
                   :  'MAG_Z'     : Magnetometer OPM channel data toward Z-axis (only QZFM device)
                   :  - case insensitive
          .Trigger : <optional> [Ntrial x 1] [1]
       .Pretrigger : <optional> [0] equal to zero or over
      .Posttrigger : <optional> [Nsample - 1] equal to zero or over
      .TrialNumber : <optional> trial number list [] [Ntrial x 1]
                   :   "1" start
                   :    e.g. [1;2;3]
                   :    if this is empty, all the trials are specified
    .ActiveChannel : <optional> <<boolean>>
                   :  active channel filter switch
                   :   = true  : load active channels [default]
                   :   = false : load all the channels
                   :  active information : define MEG(EEG)info.ActiveChannel.
      .ActiveTrial : <optional> <<boolean>>
                   :  active trial filter switch
                   :  valid only when data type is Evoked_Raw
                   :   = true  : load active trials   [default]
                   :   = false : load all the trials
                   :  active information : define MEG(EEG)info.ActiveTrial.
             (only MEG)
          .saveman : <optional> <<struct>>
                   :  When you want to make a new file, loaded data will be
                   :  stored in external directory.
                   :  If this field is invalid or switch field is set false,
                   :  data is stored internally.
                   :    .data_dir  : data directory (relative path)
                   :               : ['(name of new_file)_bin']
                   :               : @see vb_device_make_data_dir.m
                   :    .precision : data type ['float64']
                   :    .switch    : <<optional>> switch to store externally
                   :               :  [true]) store the data externally
                   :               :   false) store the data internally
             (only EEG)
     .bin_data_dir : <optional> relative path from new_file
                   :  This field is valid only when #3 argument newfile is 
                   :  specified.
                   :  if this field is not given or is empty, sampling data
                   :  will be stored internally.

    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
             :  This parameter must include the full extension.
             :  MEG : ".meg.mat"
             :  EEG : ".eeg.mat"

 [output]
           data : loaded 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]
      time_info : <<struct>> time infomation of loaded data.
                :  .time in sec.          [1x Nsample]
                :  .sample_frequency(Hz)  [1x1 double]

 [note]
   See also
     vb_megfile_load_data
     vb_eegfile_load_data
     vb_load_channel_info

 [Active channel/Active trial information]
   * Place
    load(megfile, 'MEGinfo'); % EEG case, MEGinfo -> EEGinfo
    MEGInfo.ActiveChannel : Active channel list (boolean array) [Nchannelx 1]
           .ActiveTrial   : Active trial list   (boolean array) [Nrepeat x 1]
   * Inactivate channel/trial
     load_spec.ActiveChannel = false;
     load_spec.ActiveTrial   = false;
     [data] = vb_load_meg_data(meg_file, load_spec); % [Nchannel x Nsample x Ntrial]
     MEGinfo.ActiveChannel = ones(size(data, 1), 1); % All channel active
     MEGinfo.ActiveTrial   = ones(size(data, 3), 1); % All trial   active

     % Inactivate channel/trial
     MEGinfo.ActiveChannel(N) = 0; % Inactive data(N, :, :)
     MEGinfo.ActiveTrial(M)   = 0; % Inactive data(:, :, M)
     then save it. vb_save(megfile, 'MEGinfo');

 [history]
   2006-12-13 (Sako) modified for EEG (acceptable eeg.mat)
   2007-07-23 (Sako) added loaded channel mode
   2007-08-10 (Sako) added mode type 'EEG' for load_eeg_data
   2008-02-20 (Sako) thoroughly revised
   2008-04-16 (Sako) added .ActiveSwitch in load_spec
   2008-06-06 (Sako) you can specify ChannelType and get channel_info
   2008-11-28 (Taku Yoshioka) Use vb_disp() for displaying message
   2009-07-22 (Sako) added the case only to return internal data
   2009-08-19 (Sako) modified the case that simply return data,
                     to return only sensor data (EEG) and channel_info
   2010-06-30 (Sako) supported storing data externally (only MEG)
   2010-11-15 (Sako) supported load_spec for 'INFO' file
   2011-02-08 (Sako) modified for EEG to store data externally
                     when newfile is valid
   2011-02-09 (Sako) modified not to output warning message
                     when cheking internal data
   2011-06-01 (Sako) supported unknown devices to load
   2011-07-15 (Sako) modified help about saveman(MEG) and bin_data_dir(EEG)
   2011-07-15 (Sako) modified the internal EEG data can be loaded correctly
   2011-08-19 (Sako) modified to avoid errors for the minimum data format
   2017-04-15 (rhayashi) bugfix:ActiveChannel, ActiveTrial field work correctly.
   2017-11-24 (rhayashi) return value 'time_info' is added.
   2022-06-03 (k_suzuki) added case for OPM 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, time_info] = vb_load_meg_data(megfile, load_spec, new_file)
0002 % load measured data of MEG (meg.mat) and EEG (eeg.mat).
0003 %
0004 % This function supports three formats of megfile; Standard MEG format, Minimum MEG format and fileinfo format.
0005 % By default, this function load only active channels and active trials kept in
0006 % MEGinfo.ActiveChannel and MEGinfo.ActiveTrial in case of Standard MEG format or
0007 % fileinfo.channel_id and fileinfo.act_trial in case of fileinfo format.
0008 %
0009 % [Usage]
0010 % 1. Load active channels and trials (Default)
0011 %    [data] = vb_load_meg_data(megfile);
0012 %
0013 % 2. Load all channels including inactive channels and trials
0014 %    load_spec.ActiveChannel = false;
0015 %    load_spec.ActiveTrial   = false;
0016 %    [data] = vb_load_meg_data(megfile, load_spec);
0017 %
0018 % [input]
0019 %     megfile : <required> <<file>> measurement file
0020 %             :   meg-mat file(.meg.mat)
0021 %             :   eeg-mat file(.eeg.mat)
0022 %
0023 %   load_spec : <optional> <<struct>> loading instruction
0024 %      .ChannelName : <optional> channel name list [Nch x 1] []
0025 %                   :  name(string) list of  target channel
0026 %                   :  The way to be used this depends on the
0027 %                   :  "ChannelSwitch" field
0028 %                   :  e.g.
0029 %                   :   {'1';'2';'3';'4'}
0030 %                   :  If this is empty, all the channels are
0031 %                   :  specified
0032 %    .ChannelSwitch : <optiolal> <<boolean>> [true] | false
0033 %                   :   Which is "ChannelName", to read, or not?
0034 %                   :   [true] : to read
0035 %                   :    false : to omit
0036 %      .ChannelType : <optional> <<string>> channel type ['MEG(EEG)']
0037 %                   :  'MEG'       : MEG channel data (only MEG)
0038 %                   :  'EEG'       : EEG channel data (only EEG)
0039 %                   :  'EXTRA'     : extra channel data
0040 %                   :  'REFERENCE' : reference channel data (only MEG)
0041 %                   :  'AXIAL'     : AxialGradioMeter (only MEG)
0042 %                   :  'PLANAR'    : PlanarGradioMeter (only MEG)
0043 %                   :  'ALL'       : 'MEG(EEG)'+'EXTRA'(+'REFERENCE')
0044 %                   :  'MAG_X'     : Magnetometer OPM channel data toward X-axis (only QZFM device)
0045 %                   :  'MAG_Y'     : Magnetometer OPM channel data toward Y-axis (only QZFM device)
0046 %                   :  'MAG_Z'     : Magnetometer OPM channel data toward Z-axis (only QZFM device)
0047 %                   :  - case insensitive
0048 %          .Trigger : <optional> [Ntrial x 1] [1]
0049 %       .Pretrigger : <optional> [0] equal to zero or over
0050 %      .Posttrigger : <optional> [Nsample - 1] equal to zero or over
0051 %      .TrialNumber : <optional> trial number list [] [Ntrial x 1]
0052 %                   :   "1" start
0053 %                   :    e.g. [1;2;3]
0054 %                   :    if this is empty, all the trials are specified
0055 %    .ActiveChannel : <optional> <<boolean>>
0056 %                   :  active channel filter switch
0057 %                   :   = true  : load active channels [default]
0058 %                   :   = false : load all the channels
0059 %                   :  active information : define MEG(EEG)info.ActiveChannel.
0060 %      .ActiveTrial : <optional> <<boolean>>
0061 %                   :  active trial filter switch
0062 %                   :  valid only when data type is Evoked_Raw
0063 %                   :   = true  : load active trials   [default]
0064 %                   :   = false : load all the trials
0065 %                   :  active information : define MEG(EEG)info.ActiveTrial.
0066 %             (only MEG)
0067 %          .saveman : <optional> <<struct>>
0068 %                   :  When you want to make a new file, loaded data will be
0069 %                   :  stored in external directory.
0070 %                   :  If this field is invalid or switch field is set false,
0071 %                   :  data is stored internally.
0072 %                   :    .data_dir  : data directory (relative path)
0073 %                   :               : ['(name of new_file)_bin']
0074 %                   :               : @see vb_device_make_data_dir.m
0075 %                   :    .precision : data type ['float64']
0076 %                   :    .switch    : <<optional>> switch to store externally
0077 %                   :               :  [true]) store the data externally
0078 %                   :               :   false) store the data internally
0079 %             (only EEG)
0080 %     .bin_data_dir : <optional> relative path from new_file
0081 %                   :  This field is valid only when #3 argument newfile is
0082 %                   :  specified.
0083 %                   :  if this field is not given or is empty, sampling data
0084 %                   :  will be stored internally.
0085 %
0086 %    new_file : <optional> new MEG-MAT file name which is stored picked up
0087 %             :  data and changed information []
0088 %             :  if this is empty, new file will not be created
0089 %             :  This parameter must include the full extension.
0090 %             :  MEG : ".meg.mat"
0091 %             :  EEG : ".eeg.mat"
0092 %
0093 % [output]
0094 %           data : loaded data [Nchannel x Nsample x Ntrial]
0095 %   channel_info : <<struct>> channel information of loaded data.
0096 %                :  .Active [Nchannel x 1]
0097 %                :  .Name   [Nchannel x 1]
0098 %                :  .Type   [Nchannel x 1]
0099 %                :  .ID     [Nchannel x 1]
0100 %      time_info : <<struct>> time infomation of loaded data.
0101 %                :  .time in sec.          [1x Nsample]
0102 %                :  .sample_frequency(Hz)  [1x1 double]
0103 %
0104 % [note]
0105 %   See also
0106 %     vb_megfile_load_data
0107 %     vb_eegfile_load_data
0108 %     vb_load_channel_info
0109 %
0110 % [Active channel/Active trial information]
0111 %   * Place
0112 %    load(megfile, 'MEGinfo'); % EEG case, MEGinfo -> EEGinfo
0113 %    MEGInfo.ActiveChannel : Active channel list (boolean array) [Nchannelx 1]
0114 %           .ActiveTrial   : Active trial list   (boolean array) [Nrepeat x 1]
0115 %   * Inactivate channel/trial
0116 %     load_spec.ActiveChannel = false;
0117 %     load_spec.ActiveTrial   = false;
0118 %     [data] = vb_load_meg_data(meg_file, load_spec); % [Nchannel x Nsample x Ntrial]
0119 %     MEGinfo.ActiveChannel = ones(size(data, 1), 1); % All channel active
0120 %     MEGinfo.ActiveTrial   = ones(size(data, 3), 1); % All trial   active
0121 %
0122 %     % Inactivate channel/trial
0123 %     MEGinfo.ActiveChannel(N) = 0; % Inactive data(N, :, :)
0124 %     MEGinfo.ActiveTrial(M)   = 0; % Inactive data(:, :, M)
0125 %     then save it. vb_save(megfile, 'MEGinfo');
0126 %
0127 % [history]
0128 %   2006-12-13 (Sako) modified for EEG (acceptable eeg.mat)
0129 %   2007-07-23 (Sako) added loaded channel mode
0130 %   2007-08-10 (Sako) added mode type 'EEG' for load_eeg_data
0131 %   2008-02-20 (Sako) thoroughly revised
0132 %   2008-04-16 (Sako) added .ActiveSwitch in load_spec
0133 %   2008-06-06 (Sako) you can specify ChannelType and get channel_info
0134 %   2008-11-28 (Taku Yoshioka) Use vb_disp() for displaying message
0135 %   2009-07-22 (Sako) added the case only to return internal data
0136 %   2009-08-19 (Sako) modified the case that simply return data,
0137 %                     to return only sensor data (EEG) and channel_info
0138 %   2010-06-30 (Sako) supported storing data externally (only MEG)
0139 %   2010-11-15 (Sako) supported load_spec for 'INFO' file
0140 %   2011-02-08 (Sako) modified for EEG to store data externally
0141 %                     when newfile is valid
0142 %   2011-02-09 (Sako) modified not to output warning message
0143 %                     when cheking internal data
0144 %   2011-06-01 (Sako) supported unknown devices to load
0145 %   2011-07-15 (Sako) modified help about saveman(MEG) and bin_data_dir(EEG)
0146 %   2011-07-15 (Sako) modified the internal EEG data can be loaded correctly
0147 %   2011-08-19 (Sako) modified to avoid errors for the minimum data format
0148 %   2017-04-15 (rhayashi) bugfix:ActiveChannel, ActiveTrial field work correctly.
0149 %   2017-11-24 (rhayashi) return value 'time_info' is added.
0150 %   2022-06-03 (k_suzuki) added case for OPM data
0151 %
0152 % Copyright (C) 2011, ATR All Rights Reserved.
0153 % License : New BSD License(see VBMEG_LICENSE.txt)
0154 
0155 const = vb_define_verbose;
0156 VERBOSE_LEVEL_NOTICE = const.VERBOSE_LEVEL_NOTICE;
0157 
0158 % --- CHECK ARGUMENTS --- %
0159 if ~exist('megfile', 'var'), megfile = []; end
0160 if ~exist('load_spec', 'var'), load_spec = ''; end
0161 if ~exist('new_file', 'var'), new_file = ''; end
0162 [megfile, load_spec, new_file] = ...
0163   inner_check_arguments(megfile, load_spec, new_file);
0164 
0165 % --- MAIN PROCEDURE --------------------------------------------------------- %
0166 %
0167 func_ = mfilename;
0168 
0169 data = [];
0170 channel_info = [];
0171 time_info    = inner_time_info(megfile);
0172 
0173 [measurement, device] = vb_load_device(megfile);
0174 
0175 Measurement = upper(measurement);
0176 Device      = upper(device);
0177 
0178 switch Measurement
0179   case  'MEG'
0180     switch Device
0181       case 'SBI'
0182         vb_disp(['(' func_ ')return all data because this is ' ...
0183                  'SBI data file'],VERBOSE_LEVEL_NOTICE);
0184         [data] = load_sbi_meg_data(megfile);
0185 
0186       case {'YOKOGAWA', 'NEUROMAG'}
0187         [data, channel_info] = ...
0188           vb_megfile_load_data(megfile, load_spec, new_file);
0189               
0190       case 'QZFM'
0191         [data, channel_info] = ...
0192           vb_megfile_load_data(megfile, load_spec, new_file);
0193       
0194       case 'BASIC'
0195         load(megfile, 'bexp');
0196         if exist('bexp', 'var') && ~isempty(bexp)
0197           data = bexp;
0198           channel_info = [];
0199           return;
0200         end
0201       otherwise
0202         warning('(%s) unknown Device: %s of %s', func_, Device, Measurement);
0203 
0204         % --- simplest mode check
0205         if isempty(load_spec)
0206           [result, a_list] = ...
0207             vb_util_check_variable_in_matfile(megfile, 'bexp');
0208 
0209           if result == a_list.VALID
0210             load(megfile, 'bexp');
0211             data = bexp;
0212             channel_info = [];
0213             return;
0214           end
0215         end
0216 
0217         [data, channel_info] = ...
0218           vb_megfile_load_data(megfile, load_spec, new_file);
0219     end
0220     
0221   case  'EEG'
0222     switch Device
0223       case {'BIOSEMI', 'BRAINAMP'}
0224         [data, channel_info] = ...
0225           vb_eegfile_load_data(megfile, load_spec, new_file);
0226 
0227       case 'BASIC'
0228         load(megfile, 'eeg_data');
0229         if exist('eeg_data', 'var') && ~isempty(eeg_data)
0230           data = eeg_data;
0231           channel_info = [];
0232           return;
0233         end
0234 
0235       otherwise
0236         warning('(%s)unknown Device: %s of %s\n', func_, Device, Measurement);
0237 
0238         % --- simplest mode check
0239         if isempty(load_spec)
0240           [result, a_list] = ...
0241             vb_util_check_variable_in_matfile(megfile, 'eeg_data');
0242           if result == a_list.VALID
0243             % --- extract sensor data
0244             load(megfile, 'eeg_data');
0245             data = eeg_data;
0246             channel_info = [];
0247             return;
0248           end
0249         end
0250         [data, channel_info] = ...
0251           vb_eegfile_load_data(megfile, load_spec, new_file);
0252     end
0253 
0254   case  'INFO'
0255     load(megfile, 'fileinfo');
0256     
0257     % --- support loadspec --- %
0258     if ~isempty(load_spec)
0259       tmp_file = fileinfo.filename{1};
0260     
0261       % ----- check loadspec
0262       load_spec = vb_loadspec_check(load_spec, tmp_file);
0263 
0264       % ----- narrow channel by user-specified list and active filter
0265       meginfo = vb_load_meg_info(tmp_file, 4);
0266       [read_ch, idx] = vb_meginfo_get_read_channels(meginfo, ...
0267         load_spec.ChannelName, load_spec.ChannelSwitch, ...
0268         load_spec.ActiveChannel);
0269 
0270       % --- convert loadspec setting to fileinfo setting
0271       ch_info = vb_info_get_channel_info(meginfo, 2);
0272       fileinfo.channel_id = ch_info.ID(idx);
0273     end
0274     % --- support loadspec --- %
0275     [data, channel_info] = vb_load_active_trial_data(fileinfo);
0276 
0277   otherwise
0278     error('(%s)undefined Measurement : %s', Measurement);
0279 end
0280 return;
0281 %
0282 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0283 
0284 % --- INNER FUNCTIONS -------------------------------------------------------- %
0285 %
0286 % --- inner_check_arguments()
0287 %
0288 function [megfile, load_spec, new_file] = ...
0289   inner_check_arguments(megfile, load_spec, new_file)
0290 func_ = mfilename;
0291 
0292 if isempty(megfile)
0293   error('(%s)megfile is a required parameter', func_);
0294 end
0295 
0296 if exist(megfile, 'file') ~= 2
0297   error('(%s)cannot find megfile : %s', func_, megfile);
0298 end
0299 
0300 if isempty(load_spec)
0301   % require no action
0302 end
0303 
0304 if isempty(new_file)
0305   % require no action
0306 end
0307 return
0308 %
0309 % --- end of inner_check_arguments()
0310 
0311 function time_info = inner_time_info(megfile)
0312 time_info = struct;
0313 
0314 % Calculate time
0315 meginfo = vb_load_meg_info(megfile);
0316 Nsample = vb_meginfo_get_sample_number(meginfo);
0317 time = vb_index_to_time(1:Nsample, meginfo) / 1000;
0318 
0319 % Create time_info
0320 time_info.time = time;
0321 time_info.sample_frequency = vb_meginfo_get_sampling_frequency(meginfo);
0322 %
0323 % --- end of inner_time_info()
0324     
0325 
0326 % --- END OF FILE --- %

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