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

vb_megfile_inherit_gene

PURPOSE ^

make new megfile which inherit features from orgfile

SYNOPSIS ^

function vb_megfile_inherit_gene(orgfile, newfile,meg, ext, ref, saveman, meg_labels, ext_labels, ref_labels)

DESCRIPTION ^

 make new megfile which inherit features from orgfile
 [usage]
   vb_megfile_inherit_gene(orgfile, newfile, ...
     meg, ext, ref, saveman, meg_labels, ext_labels, ref_labels)
 [input]
       orgfile : <required> <<file>> MEG-MAT file before loading
       newfile : <required> <<file>> new MEG-MAT file name
           meg : <required> MEG data [Nch_meg x Nsample x Ntrial] []
           ext : <required> EXTRA data [Nch_ext x Nsample x Ntrial] []
           ref : <required> REFERENCE data [Nch_ref x Nsample x Ntrial]
       saveman : <optional> <<struct>> 
               :  If this is not given or empty, data (meg, ext, ref) will be
               :  stored internally as (bext/bexp_ext/refmg).
    meg_labels : <optional> {Nchannel_meg x 1} [orgfile one]
               :  label list of new meg channels
               :  the number of which must be the same as that of meg
    ext_labels : <optional> {Nchannel_ext x 1} [orgfile one]
               :  label list of new extra channels
               :  the number of which must be the same as that of ext
    ref_labels : <optional> {Nchannel_ref x 1} [orgfile one]
               :  label list of new refmg channels
               :  the number of which must be the same as that of ref
 [output]
   none
 [note]
   !NOTICE! Trial information in header will not be changed.
 [history]
   2010-06-28 (Sako) initial version
   2011-03-02 (Sako) does not save empty data fields(bexp,bexp_ext,refmg)
   2011-07-21 (Sako) modified making new MEGinfo

 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_inherit_gene(orgfile, newfile, ...
0002   meg, ext, ref, saveman, meg_labels, ext_labels, ref_labels)
0003 % make new megfile which inherit features from orgfile
0004 % [usage]
0005 %   vb_megfile_inherit_gene(orgfile, newfile, ...
0006 %     meg, ext, ref, saveman, meg_labels, ext_labels, ref_labels)
0007 % [input]
0008 %       orgfile : <required> <<file>> MEG-MAT file before loading
0009 %       newfile : <required> <<file>> new MEG-MAT file name
0010 %           meg : <required> MEG data [Nch_meg x Nsample x Ntrial] []
0011 %           ext : <required> EXTRA data [Nch_ext x Nsample x Ntrial] []
0012 %           ref : <required> REFERENCE data [Nch_ref x Nsample x Ntrial]
0013 %       saveman : <optional> <<struct>>
0014 %               :  If this is not given or empty, data (meg, ext, ref) will be
0015 %               :  stored internally as (bext/bexp_ext/refmg).
0016 %    meg_labels : <optional> {Nchannel_meg x 1} [orgfile one]
0017 %               :  label list of new meg channels
0018 %               :  the number of which must be the same as that of meg
0019 %    ext_labels : <optional> {Nchannel_ext x 1} [orgfile one]
0020 %               :  label list of new extra channels
0021 %               :  the number of which must be the same as that of ext
0022 %    ref_labels : <optional> {Nchannel_ref x 1} [orgfile one]
0023 %               :  label list of new refmg channels
0024 %               :  the number of which must be the same as that of ref
0025 % [output]
0026 %   none
0027 % [note]
0028 %   !NOTICE! Trial information in header will not be changed.
0029 % [history]
0030 %   2010-06-28 (Sako) initial version
0031 %   2011-03-02 (Sako) does not save empty data fields(bexp,bexp_ext,refmg)
0032 %   2011-07-21 (Sako) modified making new MEGinfo
0033 %
0034 % Copyright (C) 2011, ATR All Rights Reserved.
0035 % License : New BSD License(see VBMEG_LICENSE.txt)
0036 
0037 % --- CHECK ARGUMENTS --- %
0038 if ~exist('orgfile', 'var'), orgfile = []; end
0039 if ~exist('newfile', 'var'), newfile = []; end
0040 if ~exist('meg', 'var'), meg = []; end
0041 if ~exist('ext', 'var'), ext = []; end
0042 if ~exist('ref', 'var'), ref = []; end
0043 
0044 if ~exist('saveman', 'var'), saveman = []; end
0045 
0046 if ~exist('meg_labels', 'var'), meg_labels = []; end
0047 if ~exist('ext_labels', 'var'), ext_labels = []; end
0048 if ~exist('ref_labels', 'var'), ref_labels = []; end
0049 [orgfile, newfile, meg, ext, ref, ...
0050   saveman, meg_labels, ext_labels, ref_labels] = ...
0051   inner_check_arguments(orgfile, newfile, meg, ext, ref, ...
0052     saveman, meg_labels, ext_labels, ref_labels);
0053 
0054 % --- MAIN PROCEDURE --------------------------------------------------------- %
0055 %
0056 func_ = mfilename;
0057 
0058 org = load(orgfile);
0059 
0060 % --- new MEGinfo
0061 MEGinfo = org.MEGinfo;
0062 
0063 Nchannel_meg = 0;
0064 Nchannel_ext = 0;
0065 Nchannel_ref = 0;
0066 
0067 Nsample_meg  = 0;
0068 Nsample_ext  = 0;
0069 Nsample_ref  = 0;
0070 
0071 Ntrial_meg   = 0;
0072 Ntrial_ext   = 0;
0073 Ntrial_ref   = 0;
0074 
0075 if ~isempty(meg)
0076   Nchannel_meg = size(meg, 1);
0077   Nsample_meg  = size(meg, 2);
0078   Ntrial_meg   = size(meg, 3);
0079 end
0080 
0081 if ~isempty(ext)
0082   Nchannel_ext = size(ext, 1);
0083   Nsample_ext  = size(ext, 2);
0084   Ntrial_ext   = size(ext, 3);
0085 end
0086 
0087 if ~isempty(ref)
0088   Nchannel_ref = size(ref, 1);
0089   Nsample_ref  = size(ref, 2);
0090   Ntrial_ref   = size(ref, 3);
0091 end
0092 
0093 % --- check consistency
0094 
0095 % --- channel
0096 if ~iscell(meg_labels), meg_labels = {meg_labels}; end
0097 if ~iscell(ext_labels), ext_labels = {ext_labels}; end
0098 if ~iscell(ref_labels), ref_labels = {ref_labels}; end
0099 
0100 if Nchannel_meg ~= length(meg_labels)
0101   error('(%s) Nchannel_meg is incorrect (%d, %d)', ...
0102     func_, Nchannel_meg, length(meg_labels));
0103 end
0104 
0105 if Nchannel_ext ~= length(ext_labels)
0106   error('(%s) Nchannel_ext is incorrect (%d, %d)', ...
0107     func_, Nchannel_ext, length(ext_labels));
0108 end
0109 
0110 if Nchannel_ref ~= length(ref_labels)
0111   error('(%s) Nchannel_ref is incorrect (%d, %d)', ...
0112     func_, Nchannel_ref, length(ref_labels));
0113 end
0114 
0115 % --- sample
0116 if ~(Nsample_meg == Nsample_ext && Nsample_meg == Nsample_ref)
0117   error('(%s) the number of sample must be the same (%d, %d, %d)', ...
0118     func_, Nsample_meg, Nsample_ext, Nsample_ref);
0119 end
0120 
0121 % --- trial
0122 if ~(Ntrial_meg == Ntrial_ext && Ntrial_meg == Ntrial_ref)
0123   error('(%s) the number of trial must be the same (%d, %d, %d)', ...
0124     func_, Ntrial_meg, Ntrial_ext, Ntrial_ref);
0125 end
0126 
0127 % --- representative number
0128 Nsample_new = Nsample_meg;
0129 Ntrial_new  = Ntrial_meg;
0130 
0131 % --- update Nchannel
0132 MEGinfo = vb_meginfo_set_channel_number(MEGinfo, Nchannel_meg);
0133 
0134 % --- update Nsample
0135 MEGinfo = vb_meginfo_set_sample_number(MEGinfo, Nsample_new);
0136 
0137 % ----- Nrepeat
0138 MEGinfo = vb_info_set_Nrepeat(MEGinfo, Ntrial_new);
0139 
0140 % MEGinfo = vb_info_adjust_trial(MEGinfo);
0141 
0142 % ----- MEGch_id
0143 [ch_idx] = ...
0144   vb_meginfo_get_channel_index_meg(MEGinfo, meg_labels);
0145 MEGinfo.MEGch_id = MEGinfo.MEGch_id(ch_idx);
0146 
0147 % ----- MEGch_name
0148 MEGinfo.MEGch_name = MEGinfo.MEGch_name(ch_idx);
0149   
0150 % ----- ActiveChannel
0151 active_ch = vb_info_get_active_channel(MEGinfo);
0152 if isempty(active_ch)
0153   Nch = length(MEGinfo.MEGch_id);
0154   MEGinfo = vb_info_set_active_channel(MEGinfo, ones(Nch,1));
0155 elseif length(active_ch) ~= length(MEGinfo.MEGch_id)
0156   MEGinfo = vb_info_set_active_channel(MEGinfo, active_ch(ch_idx));
0157 end
0158 
0159 % ----- ChannelInfo
0160 %   new_channel_info = vb_megfile_make_channel_info(orgfile, ...
0161 %     meg_labels, ext_labels, ref_labels);
0162 % --- MEGinfo.ChannelInfo saves only MEG channels
0163 new_channel_info = vb_megfile_make_channel_info(orgfile, meg_labels, [], []);
0164 MEGinfo = vb_info_set_channel_info(MEGinfo, new_channel_info);
0165 
0166 % --- Extra channel
0167 
0168 % ----- Sensor (pick,Qpick,sensor_weight)
0169 if ~isempty(org.pick) && ~isempty(org.Qpick)
0170   [org.pick, org.Qpick, MEGinfo] = ...
0171     vb_meg_adjust_sensor(org.pick, org.Qpick, ch_idx, MEGinfo);
0172 end
0173 
0174 % ----- ExtraChannelInfo
0175 all_ex_label = [ext_labels; ref_labels];
0176 [MEGinfo, org.ref_pick, org.ref_Qpick] = ...
0177   vb_meginfo_solve_extra_channel( MEGinfo, ...
0178     org.ref_pick, org.ref_Qpick, all_ex_label);
0179 
0180 do_save_ext = vb_saveman_get_switch(saveman);
0181 new_meg = org;
0182 new_meg.MEGinfo = MEGinfo;
0183 
0184 if ~do_save_ext
0185   new_meg.bexp = meg;
0186   new_meg.bexp_ext = ext;
0187   new_meg.refmg = ref;
0188 end
0189 
0190 % --- make meg.mat file
0191 vb_save_struct(newfile, new_meg);
0192 
0193 if do_save_ext
0194   const = vb_define_yokogawa;
0195 
0196   f_path = vb_get_file_parts(newfile);
0197   b_path = vb_saveman_get_dir(saveman);
0198   PRECISION = vb_saveman_get_precision(saveman);
0199   
0200   if exist([f_path '/' b_path], 'dir') ~= 7
0201     mkdir([f_path '/' b_path]);
0202   end
0203 
0204   % --- MEG channel
0205   for i_ch = 1:Nchannel_meg
0206     inner_make_external_data_file( ...
0207       f_path, b_path, meg_labels{i_ch}, const.EXT_CHANNEL_BIN, ...
0208       PRECISION, meg(i_ch,:,:));
0209   end
0210 
0211   % --- EXTRA channel
0212   for i_ch = 1:Nchannel_ext
0213     inner_make_external_data_file( ...
0214       f_path, b_path, ext_labels{i_ch}, const.EXT_CHANNEL_BIN, ...
0215       PRECISION, ext(i_ch,:,:));
0216   end
0217 
0218   % --- REFERENCE channel
0219   for i_ch = 1:Nchannel_ref
0220     inner_make_external_data_file( ...
0221       f_path, b_path, ref_labels{i_ch}, const.EXT_CHANNEL_BIN, ...
0222       PRECISION, ref(i_ch,:,:));
0223   end
0224 end
0225 return;
0226 %
0227 % --- END OF MAIN PROCEDURE -------------------------------------------------- %
0228 
0229 % --- INNER FUNCTIONS -------------------------------------------------------- %
0230 %
0231 % --- inner_check_arguments()
0232 %
0233 function [orgfile, newfile, meg, ext, ref, ...
0234   saveman, meg_labels, ext_labels, ref_labels] = ...
0235   inner_check_arguments(orgfile, newfile, meg, ext, ref, ...
0236     saveman, meg_labels, ext_labels, ref_labels)
0237 func_ = mfilename;
0238 if isempty(orgfile)
0239   error('(%s)orgfile is a required parameter', func_);
0240 end
0241 
0242 if exist(orgfile, 'file') ~= 2
0243   error('(%s)cannot find orgfile : %s', func_, orgfile);
0244 end
0245 
0246 if isempty(newfile)
0247   error('(%s)newfile is a required parameter', func_);
0248 end
0249 
0250 if isempty(meg)
0251   error('(%s)meg is a required parameter', func_);
0252 end
0253 
0254 if isempty(ext)
0255   error('(%s)ext is a required parameter', func_);
0256 end
0257 
0258 if isempty(ref)
0259   error('(%s)ref is a required parameter', func_);
0260 end
0261 
0262 if isempty(saveman)
0263   % require no action
0264 end
0265 
0266 if isempty(meg_labels)
0267   meg_labels = vb_megfile_get_channel_label_meg(orgfile);
0268 end
0269 if isempty(ext_labels)
0270   ext_labels = vb_megfile_get_channel_label_extra(orgfile);
0271 end
0272 if isempty(ref_labels)
0273   ref_labels = vb_megfile_get_channel_label_refmg(orgfile);
0274 end
0275 return;
0276 %
0277 % --- end of inner_check_arguments()
0278 
0279 % --- inner_make_external_data_file()
0280 %
0281 function inner_make_external_data_file(fpath, d_dir, fname, f_ext, prec, data)
0282 func_ = mfilename;
0283 
0284 ch_file = sprintf('%s/%s/%s.%s', fpath, d_dir, fname, f_ext);
0285 fid = fopen(ch_file, 'wb');
0286 if fid == -1
0287   error('(%s)cannot open file (%s)', func_, ch_file);
0288 end
0289 
0290 fwrite(fid, data(:), prec);
0291 fclose(fid);
0292 return;
0293 %
0294 % --- end of inner_make_external_data_file()
0295 %
0296 % --- END OF INNER FUNCTIONS ------------------------------------------------- %
0297 
0298 % --- END OF FILE --- %

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