0001 function [inv] = mne_read_inverse_operator(fname)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
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
0057
0058 fprintf(1,'Reading inverse operator decomposition from %s...\n',fname);
0059 [ fid, tree ] = fiff_open(fname);
0060
0061
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
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
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
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
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
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
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
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
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
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
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
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
0226
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
0235
0236 inv.nave = 1;
0237
0238
0239
0240 inv.projs = fiff_read_proj(fid,tree);
0241
0242
0243
0244 inv.proj = [];
0245 inv.whitener = [];
0246 inv.reginv = [];
0247
0248 inv.noisenorm = [];
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
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