Home > vbmeg > external > mne > mne_read_inverse_operator.m

mne_read_inverse_operator

PURPOSE ^

SYNOPSIS ^

function [inv] = mne_read_inverse_operator(fname)

DESCRIPTION ^

 [inv] = mne_read_inverse_operator(fname)

 Reads the inverse operator decomposition from a fif file

 fname        - The name of the file

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [inv] = mne_read_inverse_operator(fname)
0002 %
0003 % [inv] = mne_read_inverse_operator(fname)
0004 %
0005 % Reads the inverse operator decomposition from a fif file
0006 %
0007 % fname        - The name of the file
0008 %
0009 
0010 %
0011 %
0012 %   Author : Matti Hamalainen, MGH Martinos Center
0013 %   License : BSD 3-clause
0014 %
0015 %   Revision 1.8  2008/05/26 10:49:26  msh
0016 %   Update to incorporate the already weighted lead field basis
0017 %
0018 %   Revision 1.7  2007/01/29 21:21:22  msh
0019 %   Added reading of the additional source prior covariances.
0020 %
0021 %   Revision 1.6  2006/09/28 01:06:07  msh
0022 %   Added nchan field to the inverse operator structure.
0023 %
0024 %   Revision 1.5  2006/05/05 03:50:40  msh
0025 %   Added routines to compute L2-norm inverse solutions.
0026 %   Added mne_write_inverse_sol_stc to write them in stc files
0027 %   Several bug fixes in other files
0028 %
0029 %   Revision 1.4  2006/05/03 19:09:03  msh
0030 %   Fixed even more compatibility issues.
0031 %
0032 %   Revision 1.3  2006/04/23 15:29:41  msh
0033 %   Added MGH to the copyright
0034 %
0035 %   Revision 1.2  2006/04/21 21:33:07  msh
0036 %   Fixed error in combination of the forward solution.
0037 %   Fixed help text in mne_read_inverse_operator
0038 %
0039 %   Revision 1.1  2006/04/20 21:49:38  msh
0040 %   Added mne_read_inverse_operator
0041 %   Changed some of the routines accordingly for more flexibility.
0042 %
0043 %
0044 
0045 me='MNE:mne_read_inverse_operator';
0046 
0047 global FIFF;
0048 if isempty(FIFF)
0049    FIFF = fiff_define_constants();
0050 end
0051 
0052 if nargin ~= 1
0053     error(me,'Incorrect number of arguments');
0054 end
0055 %
0056 %   Open the file, create directory
0057 %
0058 fprintf(1,'Reading inverse operator decomposition from %s...\n',fname);
0059 [ fid, tree ] = fiff_open(fname);
0060 %
0061 %   Find all inverse operators
0062 %
0063 invs = fiff_dir_tree_find(tree,FIFF.FIFFB_MNE_INVERSE_SOLUTION);
0064 if isempty(invs)
0065     fclose(fid);
0066     error(me,'No inverse solutions in %s',fname);
0067 end
0068 invs = invs(1);
0069 %
0070 %   Parent MRI data
0071 %
0072 parent_mri = fiff_dir_tree_find(tree,FIFF.FIFFB_MNE_PARENT_MRI_FILE);
0073 if isempty(parent_mri)
0074     fclose(fid);
0075     error(me,'No parent MRI information in %s',fname);
0076 end
0077 fprintf(1,'\tReading inverse operator info...');
0078 %
0079 %   Methods and source orientations
0080 %
0081 tag = find_tag(invs,FIFF.FIFF_MNE_INCLUDED_METHODS);
0082 if isempty(tag)
0083     fclose(fid);
0084     error(me,'Modalities not found');
0085 end
0086 inv.methods = tag.data;
0087 %
0088 tag = find_tag(invs,FIFF.FIFF_MNE_SOURCE_ORIENTATION);
0089 if isempty(tag)
0090     fclose(fid);
0091     error(me,'Source orientation constraints not found');
0092 end
0093 inv.source_ori = tag.data;
0094 %
0095 tag = find_tag(invs,FIFF.FIFF_MNE_SOURCE_SPACE_NPOINTS);
0096 if isempty(tag)
0097     fclose(fid);
0098     error(me,'Number of sources not found');
0099 end
0100 inv.nsource = tag.data;
0101 inv.nchan   = 0;
0102 %
0103 %   Coordinate frame
0104 %
0105 tag = find_tag(invs,FIFF.FIFF_MNE_COORD_FRAME);
0106 if isempty(tag)
0107     fclose(fid);
0108     error(me,'Coordinate frame tag not found');
0109 end
0110 inv.coord_frame = tag.data;
0111 %
0112 %   The actual source orientation vectors
0113 %
0114 tag = find_tag(invs,FIFF.FIFF_MNE_INVERSE_SOURCE_ORIENTATIONS);
0115 if isempty(tag)
0116     fclose(fid);
0117     error(me,'Source orientation information not found');
0118 end
0119 inv.source_nn   = tag.data;
0120 fprintf(1,'[done]\n');
0121 %
0122 %   The SVD decomposition...
0123 %
0124 fprintf(1,'\tReading inverse operator decomposition...');
0125 tag = find_tag(invs,FIFF.FIFF_MNE_INVERSE_SING);
0126 if isempty(tag)
0127     fclose(fid);
0128     error(me,'Singular values not found');
0129 end
0130 inv.sing  = tag.data;
0131 inv.nchan = length(inv.sing);
0132 %
0133 %   The eigenleads and eigenfields
0134 %
0135 inv.eigen_leads_weighted = false;
0136 try
0137    inv.eigen_leads = fiff_read_named_matrix(fid,invs,FIFF.FIFF_MNE_INVERSE_LEADS);
0138 catch
0139    inv.eigen_leads_weighted = true;
0140    try
0141       inv.eigen_leads = fiff_read_named_matrix(fid,invs,FIFF.FIFF_MNE_INVERSE_LEADS_WEIGHTED);
0142    catch
0143       error(me,'%s',mne_omit_first_line(lasterr));
0144    end
0145 end
0146 %
0147 %   Having the eigenleads as columns is better for the inverse calculations
0148 %
0149 inv.eigen_leads = mne_transpose_named_matrix(inv.eigen_leads);
0150 try
0151     inv.eigen_fields = fiff_read_named_matrix(fid,invs,FIFF.FIFF_MNE_INVERSE_FIELDS);
0152 catch
0153     error(me,'%s',mne_omit_first_line(lasterr));
0154 end
0155 fprintf(1,'[done]\n');
0156 %
0157 %   Read the covariance matrices
0158 %
0159 try 
0160     inv.noise_cov = mne_read_cov(fid,invs,FIFF.FIFFV_MNE_NOISE_COV);
0161     fprintf('\tNoise covariance matrix read.\n');
0162 catch
0163     fclose(fid);
0164     error(me,'%s',mne_omit_first_line(lasterr));
0165 end
0166 try 
0167     inv.source_cov = mne_read_cov(fid,invs,FIFF.FIFFV_MNE_SOURCE_COV);
0168     fprintf('\tSource covariance matrix read.\n');
0169 catch
0170     fclose(fid);
0171     error(me,'%s',mne_omit_first_line(lasterr));
0172 end
0173 %
0174 %   Read the various priors
0175 %
0176 try 
0177     inv.orient_prior = mne_read_cov(fid,invs,FIFF.FIFFV_MNE_ORIENT_PRIOR_COV);
0178     fprintf('\tOrientation priors read.\n');
0179 catch
0180     inv.orient_prior = [];
0181 end
0182 try 
0183     inv.depth_prior = mne_read_cov(fid,invs,FIFF.FIFFV_MNE_DEPTH_PRIOR_COV);
0184     fprintf('\tDepth priors read.\n');
0185 catch
0186     inv.depth_prior = [];
0187 end
0188 try 
0189     inv.fmri_prior = mne_read_cov(fid,invs,FIFF.FIFFV_MNE_FMRI_PRIOR_COV);
0190     fprintf('\tfMRI priors read.\n');
0191 catch
0192     inv.fmri_prior = [];
0193 end
0194 %
0195 %   Read the source spaces
0196 %
0197 try
0198     inv.src = mne_read_source_spaces(fid,false,tree);
0199 catch
0200     fclose(fid);
0201     error(me,'Could not read the source spaces (%s)',mne_omit_first_line(lasterr));
0202 end
0203 for k = 1:length(inv.src)
0204    inv.src(k).id = mne_find_source_space_hemi(inv.src(k));
0205 end
0206 %
0207 %   Get the MRI <-> head coordinate transformation
0208 %
0209 tag = find_tag(parent_mri,FIFF.FIFF_COORD_TRANS);
0210 if isempty(tag)
0211     fclose(fid);
0212     error(me,'MRI/head coordinate transformation not found');
0213 else
0214     mri_head_t = tag.data;
0215     if mri_head_t.from ~= FIFF.FIFFV_COORD_MRI || mri_head_t.to ~= FIFF.FIFFV_COORD_HEAD
0216         mri_head_t = fiff_invert_transform(mri_head_t);
0217         if mri_head_t.from ~= FIFF.FIFFV_COORD_MRI || mri_head_t.to ~= FIFF.FIFFV_COORD_HEAD
0218             fclose(fid);
0219             error(me,'MRI/head coordinate transformation not found');
0220         end
0221     end
0222 end
0223 inv.mri_head_t  = mri_head_t;
0224 %
0225 %   Transform the source spaces to the correct coordinate frame
0226 %   if necessary
0227 %
0228 if inv.coord_frame ~= FIFF.FIFFV_COORD_MRI && ...
0229         inv.coord_frame ~= FIFF.FIFFV_COORD_HEAD
0230     fclose(fid);
0231     error(me,'Only inverse solutions computed in MRI or head coordinates are acceptable');
0232 end
0233 %
0234 %  Number of averages is initially one
0235 %
0236 inv.nave = 1;
0237 %
0238 %  We also need the SSP operator
0239 %
0240 inv.projs     = fiff_read_proj(fid,tree);
0241 %
0242 %  Some empty fields to be filled in later
0243 %
0244 inv.proj      = [];      %   This is the projector to apply to the data
0245 inv.whitener  = [];      %   This whitens the data
0246 inv.reginv    = [];      %   This the diagonal matrix implementing
0247                          %   regularization and the inverse
0248 inv.noisenorm = [];      %   These are the noise-normalization factors
0249 %
0250 nuse = 0;
0251 for k = 1:length(inv.src)
0252    try
0253       inv.src(k) = mne_transform_source_space_to(inv.src(k),inv.coord_frame,mri_head_t);
0254    catch
0255       fclose(fid);
0256       error(me,'Could not transform source space (%s)',mne_omit_first_line(lasterr));
0257    end
0258    nuse = nuse + inv.src(k).nuse;
0259 end
0260 fprintf(1,'\tSource spaces transformed to the inverse solution coordinate frame\n');
0261 %
0262 %   Done!
0263 %
0264 fclose(fid);
0265 
0266 return;
0267 
0268     function [tag] = find_tag(node,findkind)
0269 
0270         for p = 1:node.nent
0271             if node.dir(p).kind == findkind
0272                 tag = fiff_read_tag(fid,node.dir(p).pos);
0273                 return;
0274             end
0275         end
0276         tag = [];
0277         return;
0278     end
0279         
0280 end
0281 
0282  
0283

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