Home > vbmeg > functions > tool_box > neuromag > neuromag_to_megmatx.m

neuromag_to_megmatx

PURPOSE ^

make MEG-MAT & EEG-MAT files from neuromag fiffile.

SYNOPSIS ^

function megmat_file = neuromag_to_megmatx(data_fiffile, mri_niifile,mri_fidsfile, headpoints_file, output_prefix, face_file, sensor_mode)

DESCRIPTION ^

 make MEG-MAT & EEG-MAT files from neuromag fiffile.
 [USAGE]
    neuromag_to_megmatx(data_fiffile, mri_niifile, ...
                       output_prefix, face_file, sensor_mode)
 [IN]
      data_fiffile : Data fiffile.
       mri_niifile : MRI niifile.
      mri_fidsfile : MAT file containing MRI coordinate values of LPA,NAS,RPA.
   headpoints_file : Headpoints in MRI coordinate
     output_prefix : output filename prefix.
                     (ex. '/home/RH/RH' => /home/RH/RH.meg.mat)
 [optional input]
         face_file : If you want to calculate by using spherical model,
                     center position and radius of spherical model are 
                     necessary.
                     A face file which is made by positioning tool has them.
                     For face file, please refer to the manual of the
                     positioning tool
       sensor_mode : if sensor_mode = 'Planar', planar sensor is selected
                     otherwise all MEG sensor is selected
 [OUT]
    meg.mat & eeg.mat files with output_prefix are saved

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

 2012/11/16 M.Fukushima
 * modified to import Henson dataset
 2013/02/09 M.Fukushima
 * added headpoints import

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function megmat_file = neuromag_to_megmatx(data_fiffile, mri_niifile, ...
0002   mri_fidsfile, headpoints_file, output_prefix, face_file, sensor_mode)
0003 % make MEG-MAT & EEG-MAT files from neuromag fiffile.
0004 % [USAGE]
0005 %    neuromag_to_megmatx(data_fiffile, mri_niifile, ...
0006 %                       output_prefix, face_file, sensor_mode)
0007 % [IN]
0008 %      data_fiffile : Data fiffile.
0009 %       mri_niifile : MRI niifile.
0010 %      mri_fidsfile : MAT file containing MRI coordinate values of LPA,NAS,RPA.
0011 %   headpoints_file : Headpoints in MRI coordinate
0012 %     output_prefix : output filename prefix.
0013 %                     (ex. '/home/RH/RH' => /home/RH/RH.meg.mat)
0014 % [optional input]
0015 %         face_file : If you want to calculate by using spherical model,
0016 %                     center position and radius of spherical model are
0017 %                     necessary.
0018 %                     A face file which is made by positioning tool has them.
0019 %                     For face file, please refer to the manual of the
0020 %                     positioning tool
0021 %       sensor_mode : if sensor_mode = 'Planar', planar sensor is selected
0022 %                     otherwise all MEG sensor is selected
0023 % [OUT]
0024 %    meg.mat & eeg.mat files with output_prefix are saved
0025 %
0026 % Copyright (C) 2011, ATR All Rights Reserved.
0027 % License : New BSD License(see VBMEG_LICENSE.txt)
0028 %
0029 % 2012/11/16 M.Fukushima
0030 % * modified to import Henson dataset
0031 % 2013/02/09 M.Fukushima
0032 % * added headpoints import
0033 
0034 %
0035 % --- Previous check
0036 %
0037 if ~exist('data_fiffile', 'var')
0038     error('data_fiffile is a required parameter.');
0039 end
0040 if ~exist('mri_niifile', 'var')
0041     error('mri_niifile is a required parameter.');
0042 end
0043 if ~exist('mri_fidsfile', 'var')
0044     error('mri_fidsfile is a required parameter.');
0045 end
0046 if ~exist('output_prefix', 'var')
0047     error('output_prefix is a required parameter.');
0048 end
0049 if ~exist('face_file', 'var')
0050     face_file = [];
0051 end
0052 if exist(data_fiffile, 'file') ~= 2
0053     error('data_fiffile:%s is not found.', data_fiffile);
0054 end
0055 if exist(mri_niifile, 'file') ~= 2
0056     error('mri_niifile:%s is not found.', mri_niifile);
0057 end
0058 if exist(mri_fidsfile, 'file') ~= 2
0059     error('mri_fidsfile:%s is not found', mri_fidsfile);
0060 end
0061 if exist(headpoints_file, 'file') ~= 2
0062     error('headpoints_file:%s is not found', headpoints_file);
0063 end
0064 %if ~exist('analyzefile', 'var')
0065 %    error('analyzefile is a required parameter.');
0066 %end
0067 %if exist(analyzefile, 'file') ~= 2
0068 %    error('analyzefile:%s is not found.', analyzefile);
0069 %end
0070 [f_path] = vb_get_file_parts(output_prefix);
0071 if exist(f_path, 'dir') ~= 7
0072     error('output_prefix directory:%s is invalid.', f_path);
0073 end
0074 if ~exist('sensor_mode','var'), sensor_mode = 'All'; end
0075 
0076 %
0077 % --- Main Procedure
0078 %
0079 
0080 
0081 %
0082 % --- Load Header
0083 %
0084 fprintf('--- now loading neuromag header.\n');
0085 [MEGinfo_list, sensor_info] = neuromag_load_meg_headerx(data_fiffile, ...
0086                                 mri_niifile, mri_fidsfile, headpoints_file);%, ...
0087 %                                                       analyzefile);
0088 
0089 %
0090 % --- Make sphere model
0091 %
0092 if ~isempty(face_file)
0093 
0094     if exist(face_file,'file') ~= 2
0095     warning('face_file:%s is not found.\n', face_file);
0096 %     error('face_file:%s is not found.', face_file);
0097   end
0098   
0099   [state, guide_def] = ...
0100     vb_util_check_variable_in_matfile(face_file, 'spherical_head');
0101 
0102   if state == guide_def.VALID
0103     load(face_file, 'spherical_head');
0104     Vcenter = spherical_head.Vcenter;
0105     Vradius = spherical_head.Vradius;
0106   else
0107     [state, guide_def] = ...
0108       vb_util_check_variable_in_matfile(face_file, 'surf_face');
0109     if state == guide_def.VALID
0110       % --- calculate center position and radius of spherical model
0111       % --- Upper face is used for fitting sphere model
0112       %    --- ix = find(V(:,3) > (h * Zmax + (1-h) * Zmin));
0113       h = 0.4;
0114         load(face_file, 'surf_face')
0115     
0116         V = surf_face.V_reduce;
0117         Zmax = max(V(:,3));
0118         Zmin = min(V(:,3));
0119         ix = find(V(:,3) > (h * Zmax + (1-h) * Zmin));
0120     
0121         % Fit face to sphere
0122         [Vcenter,Vradius] = vb_center_sphere(V(ix,:));
0123     else
0124         Vcenter = [];
0125         Vradius = [];
0126     end
0127   end
0128     
0129 else
0130     Vcenter = [];
0131     Vradius = [];
0132 end
0133 
0134 pick   = sensor_info.pick;
0135 Qpick  = sensor_info.Qpick;
0136 weight = sensor_info.Weight;
0137 
0138 if strcmp(sensor_mode,'Planar'),
0139     % select planar sensor from MEG data
0140     MEGinfo_base = MEGinfo_list{1};
0141     ix_ch = find( MEGinfo_base.ChannelInfo.Type == 3 );
0142     
0143     weight  = weight(ix_ch,:);
0144     ix_coil = find( sum(abs(weight),1) > 0);
0145     weight  = weight(:,ix_coil);
0146     
0147     pick  = pick(ix_coil,:);
0148     Qpick = Qpick(ix_coil,:);
0149 else
0150     ix_ch = [];
0151 end
0152 
0153 Ninfo = length(MEGinfo_list);
0154 
0155 %
0156 % --- Make MEG-MAT & EEG-MAT files.
0157 %
0158 fprintf('--- now loading neuromag data.\n');
0159 for k=1:Ninfo
0160   % -- Make file name
0161   if Ninfo == 1
0162     megmat_file{1} = [output_prefix, '.meg.mat'];
0163     eegmat_file{1} = [output_prefix, '.eeg.mat'];
0164   else
0165     megmat_file{k} = [output_prefix, '_', num2str(k), '.meg.mat'];
0166     eegmat_file{k} = [output_prefix, '_', num2str(k), '.eeg.mat'];
0167   end
0168 
0169   % load data
0170   [bexp, ch_info, sfreq] = neuromag_load_meg_data(data_fiffile);
0171   [bexp_ext, extra_info] = neuromag_load_extra_data(data_fiffile);
0172 
0173 
0174   % Set MEGinfo
0175   MEGinfo = MEGinfo_list{k};
0176       
0177   if ~isempty(Vcenter)
0178       MEGinfo.Vcenter = Vcenter;
0179       MEGinfo.Vradius = Vradius;
0180   end
0181     
0182   MEGinfo.Nsample    = size(bexp, 2);
0183   MEGinfo.SampleFreq = sfreq;
0184 
0185   % ----- MEGinfo.Trial
0186   Trial(1).number = 1;
0187   Trial(1).sample = 1:MEGinfo.Nsample;
0188   Trial(1).Active = true;
0189   MEGinfo.Trial = Trial;
0190 
0191   MEGinfo.sensor_weight = weight;
0192   MEGinfo.ExtraChannelInfo = extra_info;
0193 
0194   MEGinfo.device   = 'NEUROMAG';
0195   CoordType   = 'SPM_Right_m';
0196     
0197   Measurement = 'MEG';
0198 
0199   if ~isempty(ix_ch)
0200       bexp = bexp(ix_ch,:);
0201   end
0202     
0203   
0204   MEGinfo.Measurement = Measurement;
0205   MEGinfo.ActiveChannel = MEGinfo.ChannelInfo.Active;
0206   MEGinfo.ActiveTrial = MEGinfo.Trial.Active;
0207   
0208   % save MEG-MAT file
0209   vb_fsave(megmat_file{k}, 'MEGinfo', 'Measurement', 'CoordType', ...
0210        'pick', 'Qpick', 'bexp', 'bexp_ext');
0211   fprintf('%s was created.\n', megmat_file{k});
0212   
0213 %!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0214 % RHAYASHI output EEG-MAT not supported yet.
0215 %!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0216 %   % --- Make EEG-MAT file
0217 %   ind_eeg = [1:60 65:74];
0218 %   eeg_data = bexp_ext(ind_eeg,:);
0219 %   Measurement = 'EEG';
0220 %   EEGinfo.Measurement = Measurement;
0221 %   EEGinfo.Device = MEGinfo.device;
0222 %   EEGinfo.Nchannel = 70;
0223 %   EEGinfo.Nsample = MEGinfo.Nsample;
0224 %   EEGinfo.Nrepeat = MEGinfo.Nrepeat;
0225 %   EEGinfo.Pretrigger = MEGinfo.Pretrigger;
0226 %   EEGinfo.SampleFrequency = MEGinfo.SampleFreq;
0227 %   EEGinfo.ChannelID = (1:EEGinfo.Nchannel)';
0228 %   EEGinfo.ChannelName = MEGinfo.ExtraChannelInfo.Channel_name(ind_eeg);
0229 %   EEGinfo.ActiveChannel = ones(EEGinfo.Nchannel,1);
0230 %   EEGinfo.ActiveTrial = ones(EEGinfo.Nrepeat,1);
0231 %   EEGinfo.Vcenter = MEGinfo.Vcenter;
0232 %   EEGinfo.Vradius = MEGinfo.Vradius;
0233 %   EEGinfo.MRI_ID = MEGinfo.MRI_ID;
0234 %   EEGinfo.Trial = MEGinfo.Trial;
0235 %   EEGinfo.ChannelInfo.ID = EEGinfo.ChannelID;
0236 %   EEGinfo.ChannelInfo.Name = EEGinfo.ChannelName;
0237 %   EEGinfo.ChannelInfo.Type = ones(EEGinfo.Nchannel,1);
0238 %   EEGinfo.ChannelInfo.Active = EEGinfo.ActiveChannel;
0239 %   EEGinfo.ChannelInfo.PhysicalUnit = cell(EEGinfo.Nchannel,1);
0240 %   for ii = 1:EEGinfo.Nchannel
0241 %     EEGinfo.ChannelInfo.PhysicalUnit{ii} = 'V';
0242 %   end
0243 %   EEGinfo.ExtraChannelInfo.Channel_id = [];
0244 %   EEGinfo.ExtraChannelInfo.Channel_name = {};
0245 %   EEGinfo.ExtraChannelInfo.Channel_type = [];
0246 %   EEGinfo.ExtraChannelInfo.Channel_active = [];
0247 %   EEGinfo.ExtraChannelInfo.PhysicalUnit = {};
0248 %   for ii = 1:(EEGinfo.Nchannel + length(EEGinfo.ExtraChannelInfo.Channel_id))
0249 %     EEGinfo.DataType{ii} = 'Unknown';
0250 %   end
0251 %
0252 %   [R, KI, NU] = hpipoints(data_fiffile);
0253 %   CoordHead = R(:,KI==3)';
0254 %   [ans, ind_tmp] = intersect(NU(KI==3),ind_eeg);
0255 %   CoordHead = CoordHead(ind_tmp,:);
0256 %   % load voxel data and transform matrix from mri niifile.
0257 %   [voxel, mri_trans_info] = neuromag_load_mri_infox...
0258 %       (data_fiffile, mri_niifile, mri_fidsfile, headpoints_file);
0259 %   Vdim = size(voxel);
0260 %   % transform coil coordinates in head coordinate to SPM coordinate.
0261 %   Coord  = neuro_head_to_spm_right(CoordHead, mri_trans_info, Vdim);
0262 %
0263 %   EEGinfo.Coord = Coord;
0264 %   EEGinfo.CoordType = CoordType;
0265 %
0266 %   % save EEG-MAT file
0267 %   vb_fsave(eegmat_file{k}, 'EEGinfo', 'Measurement', 'eeg_data');
0268 %   fprintf('%s was created.\n', eegmat_file{k});
0269 end
0270 fprintf('--- conversion were finished.\n');

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