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

vb_megfile_make_new_megfile

PURPOSE ^

make new megfile

SYNOPSIS ^

function vb_megfile_make_new_megfile(orgfile, newfile, loadspec,meg_labels, ext_labels, ref_labels, meg, ext, ref, acqtype, new_samples)

DESCRIPTION ^

 make new megfile
 [usage]
   vb_megfile_make_new_megfile(orgfile, newfile, loadspec, ...
     meg_labels, ext_labels, ref_labels, meg, ext, ref, acqtype, new_samples)
 [input]
       orgfile : <required> <<file>> MEG-MAT file before loading
       newfile : <required> <<file>> new MEG-MAT file which has loading data
      loadspec : <required> <<struct>> this will be added to new MEG-MAT
               :   as it is
    meg_labels : <optional> {Nchannel_meg x 1} [[]]
               :  label list of new meg channels
    ext_labels : <optional> {Nchannel_ext x 1} [[]]
               :  label list of new extra channels
    ref_labels : <optional> {Nchannel_ref x 1} [[]]
               :  label list of new refmg channels
           meg : <optional> set MEG data [Nch_meg x Nsample x Ntrial] []
               :  which will be stored to bexp field
           ext : <optional> set EXTRA data [Nch_ext x Nsample x Ntrial] []
               :  which will be stored to bexp_ext field
           ref : <optional> set REFERENCE data [Nch_ref x Nsample x Ntrial]
               :  [] which will be stored to refmg field
       acqtype : <optional> <<string>> set acq_type field if this is set ['']
   new_samples : <optional> from-to list [Ntrial x 2] or [1 x 2]
               :  if loadspec.TrialNumber is empty,
               :  MEGinfo.Trial is made by this.
 [output]
   none
 [note]

 [history]
   2008-02-21 (Sako) initial version
   2008-04-24 (Sako) changed the rule of trial number continuous -> raw
   2009-07-14 (Sako) added three label arguments (meg,ext,ref), modified Trial
   2009-08-04 (Sako) modified to save only MEG channels in MEGinfo.ChannelInfo
   2010-06-30 (Sako) supported storing data externally
   2011-03-02 (Sako) added checking old style data fields exist or not
   2011-07-07 (Sako) modified ParentInfo format

 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_make_new_megfile(orgfile, newfile, loadspec, ...
0002   meg_labels, ext_labels, ref_labels, meg, ext, ref, acqtype, new_samples)
0003 % make new megfile
0004 % [usage]
0005 %   vb_megfile_make_new_megfile(orgfile, newfile, loadspec, ...
0006 %     meg_labels, ext_labels, ref_labels, meg, ext, ref, acqtype, new_samples)
0007 % [input]
0008 %       orgfile : <required> <<file>> MEG-MAT file before loading
0009 %       newfile : <required> <<file>> new MEG-MAT file which has loading data
0010 %      loadspec : <required> <<struct>> this will be added to new MEG-MAT
0011 %               :   as it is
0012 %    meg_labels : <optional> {Nchannel_meg x 1} [[]]
0013 %               :  label list of new meg channels
0014 %    ext_labels : <optional> {Nchannel_ext x 1} [[]]
0015 %               :  label list of new extra channels
0016 %    ref_labels : <optional> {Nchannel_ref x 1} [[]]
0017 %               :  label list of new refmg channels
0018 %           meg : <optional> set MEG data [Nch_meg x Nsample x Ntrial] []
0019 %               :  which will be stored to bexp field
0020 %           ext : <optional> set EXTRA data [Nch_ext x Nsample x Ntrial] []
0021 %               :  which will be stored to bexp_ext field
0022 %           ref : <optional> set REFERENCE data [Nch_ref x Nsample x Ntrial]
0023 %               :  [] which will be stored to refmg field
0024 %       acqtype : <optional> <<string>> set acq_type field if this is set ['']
0025 %   new_samples : <optional> from-to list [Ntrial x 2] or [1 x 2]
0026 %               :  if loadspec.TrialNumber is empty,
0027 %               :  MEGinfo.Trial is made by this.
0028 % [output]
0029 %   none
0030 % [note]
0031 %
0032 % [history]
0033 %   2008-02-21 (Sako) initial version
0034 %   2008-04-24 (Sako) changed the rule of trial number continuous -> raw
0035 %   2009-07-14 (Sako) added three label arguments (meg,ext,ref), modified Trial
0036 %   2009-08-04 (Sako) modified to save only MEG channels in MEGinfo.ChannelInfo
0037 %   2010-06-30 (Sako) supported storing data externally
0038 %   2011-03-02 (Sako) added checking old style data fields exist or not
0039 %   2011-07-07 (Sako) modified ParentInfo format
0040 %
0041 % Copyright (C) 2011, ATR All Rights Reserved.
0042 % License : New BSD License(see VBMEG_LICENSE.txt)
0043 
0044 % --- CHECK ARGUMENTS --- %
0045 if ~exist('orgfile', 'var'), orgfile = []; end
0046 if ~exist('loadspec', 'var'), loadspec = []; end
0047 if ~exist('newfile', 'var'), newfile = []; end
0048 if ~exist('meg_labels', 'var'), meg_labels = []; end
0049 if ~exist('ext_labels', 'var'), ext_labels = []; end
0050 if ~exist('ref_labels', 'var'), ref_labels = []; end
0051 if ~exist('meg', 'var'), meg = []; end
0052 if ~exist('ext', 'var'), ext = []; end
0053 if ~exist('ref', 'var'), ref = []; end
0054 if ~exist('acqtype', 'var'), acqtype = []; end
0055 if ~exist('new_samples', 'var'), new_samples = []; end
0056 [orgfile, newfile, LoadSpec, meg_labels, ext_labels, ref_labels, ...
0057   meg, ext, ref, acqtype, new_samples, data_save_ex, saveman] = ...
0058   inner_check_arguments(orgfile, newfile, loadspec, ...
0059     meg_labels, ext_labels, ref_labels, meg, ext, ref, acqtype, new_samples);
0060 
0061 % --- MAIN PROCEDURE --------------------------------------------------------- %
0062 %
0063 
0064 % --- make ParentInfo
0065 tmp_org = load(orgfile);
0066 if isfield(tmp_org, 'ParentInfo')
0067   ParentInfo.ParentInfo = tmp_org.ParentInfo;
0068 end
0069 
0070 if isfield(tmp_org, 'LoadSpec')
0071   ParentInfo.LoadSpec = tmp_org.LoadSpec;
0072 end
0073 
0074 % --- save parent file
0075 ParentInfo.meg_file = orgfile;
0076 
0077 clear tmp_org;
0078 
0079 pick = [];
0080 Qpick = [];
0081 ref_pick = [];
0082 ref_Qpick = [];
0083 MEGinfo = [];
0084 CoordType = '';
0085 Measurement = '';
0086 PositionFile = '';
0087 
0088 % - load valid data
0089 % --- pick
0090 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'pick');
0091 if state == const.VALID
0092   load(orgfile, 'pick');
0093 end
0094 
0095 % --- Qpick
0096 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'Qpick');
0097 if state == const.VALID
0098   load(orgfile, 'Qpick');
0099 end
0100 
0101 % --- ref_pick
0102 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'ref_pick');
0103 if state == const.VALID
0104   load(orgfile, 'ref_pick');
0105 end
0106 
0107 % --- ref_Qpick
0108 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'ref_Qpick');
0109 if state == const.VALID
0110   load(orgfile, 'ref_Qpick');
0111 end
0112 
0113 % --- MEGinfo
0114 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'MEGinfo');
0115 if state == const.VALID
0116   load(orgfile, 'MEGinfo');
0117 end
0118 ParentInfo.MEGinfo = MEGinfo;
0119 
0120 % --- CoordType
0121 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'CoordType');
0122 if state == const.VALID
0123   load(orgfile, 'CoordType');
0124 end
0125 
0126 % --- Measurement
0127 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'Measurement');
0128 if state == const.VALID
0129   load(orgfile, 'Measurement');
0130 end
0131 
0132 % --- PositionFile
0133 [state, const] = vb_util_check_variable_in_matfile(orgfile, 'PositionFile');
0134 if state == const.VALID
0135   load(orgfile, 'PositionFile');
0136 end
0137 
0138 % --- new MEGinfo
0139 
0140 % ----- acq_type
0141 if ~isempty(acqtype)
0142   MEGinfo = vb_meginfo_set_acqtype(MEGinfo, acqtype);
0143 end
0144 
0145 Nchannel = 0;
0146 Nsample  = 0;
0147 Ntrial   = 0;
0148 
0149 if ~isempty(meg)
0150   Nchannel = size(meg,1);
0151   Nsample  = size(meg,2);
0152   Ntrial   = size(meg,3);
0153 elseif ~isempty(ext)
0154   Nsample = size(ext,2);
0155   Ntrial  = size(ext,3);
0156 elseif ~isempty(ref)
0157   Nsample = size(ref,2);
0158   Ntrial  = size(ref,3);
0159 end
0160 
0161 % ----- Nchannel
0162 MEGinfo = vb_meginfo_set_channel_number(MEGinfo, Nchannel);
0163 
0164 % ----- Nsample
0165 MEGinfo = vb_meginfo_set_sample_number(MEGinfo, Nsample);
0166 
0167 % ----- Pretrigger
0168 if ~isempty(loadspec.Pretrigger)
0169   MEGinfo = vb_meginfo_set_pre_trigger(MEGinfo, loadspec.Pretrigger);
0170 else
0171   if ~isfield(MEGinfo, 'Pretrigger') || isempty(MEGinfo.Pretrigger)
0172     MEGinfo = vb_meginfo_set_pre_trigger(MEGinfo, 0);
0173   end
0174 end
0175 
0176 % ----- Nrepeat
0177 MEGinfo = vb_info_set_Nrepeat(MEGinfo, Ntrial);
0178 
0179 % ----- Trial
0180 if ~isempty(loadspec.TrialNumber)
0181   % --- chopped trial --> narrowed down chopped trial
0182   old_trial = vb_info_get_trial_data(MEGinfo, loadspec.TrialNumber);
0183 
0184   sample_idx = new_samples(1,1):new_samples(1,2);
0185 
0186   for i_trial = 1:length(old_trial)
0187     new_trial(i_trial) = old_trial(i_trial);
0188     
0189     % --- update only sample
0190     sample_old = old_trial(i_trial).sample;
0191     new_trial(i_trial).sample = sample_old(sample_idx);
0192   end
0193 
0194 else
0195   old_trial = vb_info_get_trial_data(MEGinfo);
0196   n_trial_old = length(old_trial);
0197 
0198   if n_trial_old == 1
0199     % --- continuous --> chopped trial or continuous
0200     n_trial_new = size(new_samples, 1);
0201 
0202     for i_trial = 1:n_trial_new
0203       sample_idx = new_samples(i_trial,1):new_samples(i_trial,2);
0204       new_trial(i_trial).sample = sample_idx;
0205       new_trial(i_trial).number = i_trial;
0206       new_trial(i_trial).Active = true;
0207     end
0208   else
0209     % --- chopped trial --> narrowed down chopped trial
0210     sample_idx = new_samples(1,1):new_samples(1,2);
0211 
0212     for i_trial = length(old_trial)
0213       new_trial(i_trial) = old_trial(i_trial);
0214     
0215       % --- update only sample
0216       sample_old = old_trial(i_trial).sample;
0217       new_trial(i_trial).sample = sample_old(sample_idx);
0218     end
0219   end
0220 end
0221 % [N x 1]
0222 MEGinfo.Trial = vb_util_arrange_list(new_trial, 0);
0223 
0224 MEGinfo = vb_info_adjust_trial(MEGinfo);
0225 
0226 % --- MEG channel information
0227 load_ch_name = vb_loadspec_get_channel_name(loadspec);
0228 
0229 if ~isempty(load_ch_name)
0230   % ----- MEGch_id
0231   [ch_idx] = ...
0232     vb_meginfo_get_channel_index_meg(MEGinfo, load_ch_name);
0233   MEGinfo.MEGch_id = MEGinfo.MEGch_id(ch_idx);
0234 
0235 % ----- MEGch_name
0236   MEGinfo.MEGch_name = MEGinfo.MEGch_name(ch_idx);
0237   
0238 % ----- ActiveChannel
0239   active_ch = vb_info_get_active_channel(MEGinfo);
0240   if isempty(active_ch)
0241     Nch = length(MEGinfo.MEGch_id);
0242     MEGinfo = vb_info_set_active_channel(MEGinfo, ones(Nch,1));
0243   elseif length(active_ch) ~= length(MEGinfo.MEGch_id)
0244     MEGinfo = vb_info_set_active_channel(MEGinfo, active_ch(ch_idx));
0245   end
0246 
0247 % ----- ChannelInfo
0248 %   new_channel_info = vb_megfile_make_channel_info(orgfile, ...
0249 %     meg_labels, ext_labels, ref_labels);
0250   % --- MEGinfo.ChannelInfo saves only MEG channels
0251   new_channel_info = vb_megfile_make_channel_info(orgfile, ...
0252     meg_labels, [], []);
0253 
0254 else
0255   MEGinfo.MEGch_id = [];
0256   MEGinfo.MEGch_name = '';
0257   MEGinfo = vb_info_set_active_channel(MEGinfo, []);
0258   new_channel_info = [];
0259   ch_idx = [];
0260 end
0261 
0262 MEGinfo = vb_info_set_channel_info(MEGinfo, new_channel_info);
0263 
0264 % ----- Sensor (pick,Qpick,sensor_weight)
0265 if ~isempty(pick) && ~isempty(Qpick)
0266   [pick, Qpick, MEGinfo] = vb_meg_adjust_sensor(pick, Qpick, ch_idx, MEGinfo);
0267 end
0268 
0269 % ----- ExtraChannelInfo
0270 if ~isempty(ref_pick) && ~isempty(ref_Qpick)
0271   [MEGinfo, ref_pick, ref_Qpick] = ...
0272     vb_meginfo_solve_extra_channel(MEGinfo, ref_pick, ref_Qpick, load_ch_name);
0273 end
0274 
0275 
0276 % ----- save sampling data
0277 if ~data_save_ex
0278   bexp = meg;
0279   bexp_ext = ext;
0280   refmg = ref;
0281   MEGinfo.saveman = [];
0282   
0283 else
0284   const = vb_define_yokogawa;
0285   data_ext = const.EXT_CHANNEL_BIN;
0286   
0287   bexp = [];
0288   bexp_ext = [];
0289   refmg = [];
0290   
0291   f_path = vb_get_file_parts(newfile);
0292   d_path = fullfile(f_path, vb_saveman_get_dir(saveman));
0293 
0294   prec = vb_saveman_get_precision(saveman);
0295   
0296   % --- MEG channel
0297   n_ch = length(meg_labels);
0298   for i_ch = 1:n_ch
0299     vb_util_make_external_data_file(d_path, meg_labels{i_ch}, ...
0300       data_ext, prec, meg(i_ch,:,:) );
0301   end
0302   
0303   % --- EXTRA channel
0304   n_ch = length(ext_labels);
0305   for i_ch = 1:n_ch
0306     vb_util_make_external_data_file(d_path, ext_labels{i_ch}, ...
0307       data_ext, prec, ext(i_ch,:,:) );
0308   end
0309   
0310   % --- REFERENCE channel
0311   n_ch = length(ref_labels);
0312   for i_ch = 1:n_ch
0313     vb_util_make_external_data_file(d_path, ref_labels{i_ch}, ...
0314       data_ext, prec, ref(i_ch,:,:) );
0315   end
0316 
0317   MEGinfo.saveman = saveman;
0318 end
0319 
0320 vb_fsave(newfile, 'pick', 'Qpick', 'ref_pick', 'ref_Qpick', 'CoordType', ...
0321   'bexp', 'bexp_ext', 'refmg', 'LoadSpec', 'ParentInfo', ...
0322   'MEGinfo', 'Measurement', 'PositionFile');
0323 % end
0324 return;
0325 %
0326 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0327 
0328 % --- INNER FUNCTIONS -------------------------------------------------------- %
0329 %
0330 % --- inner_check_arguments()
0331 %
0332 function [orgfile, newfile, loadspec, meg_labels, ext_labels, ref_labels, ...
0333   meg, ext, ref, acqtype, new_samples, data_save_ex, saveman] = ...
0334   inner_check_arguments(orgfile, newfile, loadspec, ...
0335     meg_labels, ext_labels, ref_labels, meg, ext, ref, acqtype, new_samples)
0336 func_ = mfilename;
0337 if isempty(orgfile)
0338   error('(%s)orgfile is a required parameter', func_);
0339 end
0340 
0341 if exist(orgfile, 'file') ~= 2
0342   error('(%s)cannot find orgfile : %s', func_, orgfile);
0343 end
0344 
0345 if isempty(newfile)
0346   error('(%s)newfile is a required parameter', func_);
0347 end
0348 if isempty(loadspec)
0349   error('(%s)loadspec is a required parameter', func_);
0350 end
0351 
0352 % --- where data will be stored
0353 data_save_ex = 0; % 0 means data will be stored internally (as before)
0354 saveman = [];
0355 if isfield(loadspec, 'saveman')
0356   saveman = loadspec.saveman;
0357   data_save_ex = vb_saveman_get_switch(loadspec.saveman);
0358 end
0359 
0360 if isempty(meg_labels)
0361   % require no action
0362 end
0363 if isempty(ext_labels)
0364   % require no action
0365 end
0366 if isempty(ref_labels)
0367   % require no action
0368 end
0369 
0370 if isempty(acqtype)
0371   % require no action
0372 end
0373 if isempty(new_samples)
0374   warning('(%s)new_samples is not specified\n', func_);
0375 end
0376 return;
0377 %
0378 % --- end of inner_check_arguments()
0379 %
0380 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0381 
0382 % --- END OF FILE --- %

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