Home > vbmeg > external > mne > mne_read_source_spaces.m

mne_read_source_spaces

PURPOSE ^

SYNOPSIS ^

function [src] = mne_read_source_spaces(source,add_geom,tree)

DESCRIPTION ^

 [src] = mne_read_source_spaces(source,add_geom,tree)

 Reads source spaces from a fif file

 source      - The name of the file or an open file id
 add_geom    - Add geometry information to the source spaces
 tree        - Required if source is an open file id, search for the
               source spaces here

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [src] = mne_read_source_spaces(source,add_geom,tree)
0002 %
0003 % [src] = mne_read_source_spaces(source,add_geom,tree)
0004 %
0005 % Reads source spaces from a fif file
0006 %
0007 % source      - The name of the file or an open file id
0008 % add_geom    - Add geometry information to the source spaces
0009 % tree        - Required if source is an open file id, search for the
0010 %               source spaces here
0011 %
0012 
0013 %
0014 %
0015 %   Author : Matti Hamalainen, MGH Martinos Center
0016 %   License : BSD 3-clause
0017 %
0018 %   Revision 1.13  2007/03/11 13:32:22  msh
0019 %   Nearest numbers had to be incremented by one to comply with the 1-based Matlab indexing
0020 %
0021 %   Revision 1.12  2006/09/25 19:48:16  msh
0022 %   Added projection item kinds to fiff_define_constants
0023 %   Changed some fields to int32 in surface structures
0024 %
0025 %   Revision 1.11  2006/05/22 10:44:44  msh
0026 %   Added surface and curvature reading routines.
0027 %   Fixed error in mne_read_source_spaces: triangle normals were not normalized
0028 %
0029 %   Revision 1.10  2006/05/05 03:50:40  msh
0030 %   Added routines to compute L2-norm inverse solutions.
0031 %   Added mne_write_inverse_sol_stc to write them in stc files
0032 %   Several bug fixes in other files
0033 %
0034 %   Revision 1.9  2006/05/03 19:05:06  msh
0035 %   Eliminated more Matlab 6.5 incompatibility
0036 %
0037 %   Revision 1.8  2006/05/03 18:53:06  msh
0038 %   Approaching Matlab 6.5 backward compatibility
0039 %
0040 %   Revision 1.7  2006/05/03 15:50:39  msh
0041 %   Fixed some Matlab 6.5 incompatibility.
0042 %
0043 %   Revision 1.6  2006/04/28 18:01:27  msh
0044 %   Added mne_read_bem_surfaces
0045 %   Fixed errors in mne_read_source_spaces and improved triangle data computation.
0046 %
0047 %   Revision 1.5  2006/04/23 15:29:41  msh
0048 %   Added MGH to the copyright
0049 %
0050 %   Revision 1.4  2006/04/20 21:49:38  msh
0051 %   Added mne_read_inverse_operator
0052 %   Changed some of the routines accordingly for more flexibility.
0053 %
0054 %   Revision 1.3  2006/04/18 23:21:22  msh
0055 %   Added mne_transform_source_space_to and mne_transpose_named_matrix
0056 %
0057 %   Revision 1.2  2006/04/18 20:44:46  msh
0058 %   Added reading of forward solution.
0059 %   Use length instead of size when appropriate
0060 %
0061 %   Revision 1.1  2006/04/17 21:05:08  msh
0062 %   Added source space reading routine.
0063 %
0064 %
0065 
0066 me='MNE:mne_read_source_spaces';
0067 
0068 global FIFF;
0069 if isempty(FIFF)
0070     FIFF = fiff_define_constants();
0071 end
0072 
0073 if nargin == 1
0074     add_geom = false;
0075     fid      = -1;
0076     fname    = source;
0077 elseif nargin == 2
0078     fid      = -1;
0079     fname    = source;
0080 elseif nargin == 3
0081     a = whos('source');
0082     if strcmp(a.class,'char')
0083         fname = source;
0084         fid   = -1;
0085     else
0086         fid   = source;
0087     end
0088 else
0089     error(me,'Incorrect number of arguments');
0090 end
0091 %
0092 %   Open the file, create directory
0093 %
0094 if fid < 0
0095     [ fid, tree ] = fiff_open(fname);
0096     open_here = true;
0097 else
0098     open_here = false;
0099 end
0100 %
0101 %   Find all source spaces
0102 %
0103 spaces = fiff_dir_tree_find(tree,FIFF.FIFFB_MNE_SOURCE_SPACE);
0104 if isempty(spaces)
0105     close_file;
0106     error(me,'No source spaces found');
0107 end
0108 
0109 for k = 1:length(spaces)
0110     fprintf(1,'\tReading a source space...');
0111     this = read_source_space(spaces(k));
0112     fprintf(1,'[done]\n');
0113     if add_geom
0114         complete_source_space_info;
0115     end
0116     src(k) = this;
0117 end
0118 
0119 fprintf(1,'\t%d source spaces read\n',length(spaces));
0120 
0121 close_file;
0122 
0123 return;
0124 
0125     function [res] = read_source_space(this)
0126         %
0127         %   Read all the interesting stuff
0128         %
0129         FIFF_BEM_SURF_NTRI=3104;
0130         FIFF_BEM_SURF_TRIANGLES=3106;
0131         
0132         tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_ID);
0133         if isempty(tag)
0134             res.id = int32(FIFF.FIFFV_MNE_SURF_UNKNOWN);
0135         else
0136             res.id = int32(tag.data);
0137         end
0138         
0139         tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NPOINTS);
0140         if isempty(tag)
0141             close_file;
0142             error(me,'Number of vertices not found');
0143         end
0144         res.np = tag.data;
0145         
0146         tag = find_tag(this,FIFF_BEM_SURF_NTRI);
0147         if isempty(tag)
0148             tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NTRI);
0149             if isempty(tag)
0150                 res.ntri = 0;
0151             else
0152                 res.ntri = tag.data;
0153             end
0154         else
0155             res.ntri = tag.data;
0156         end
0157         
0158         tag = find_tag(this,FIFF.FIFF_MNE_COORD_FRAME);
0159         if isempty(tag)
0160             close_file;
0161             error(me,'Coordinate frame information not found');
0162         end
0163         res.coord_frame = tag.data;
0164         %
0165         %   Vertices, normals, and triangles
0166         %
0167         tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_POINTS);
0168         if isempty(tag)
0169             close_file;
0170             error(me,'Vertex data not found');
0171         end
0172         res.rr = tag.data;
0173         if size(res.rr,1) ~= res.np
0174             close_file;
0175             error(me,'Vertex information is incorrect');
0176         end
0177         
0178         tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NORMALS);
0179         if isempty(tag)
0180             close_file;
0181             error(me,'Vertex normals not found');
0182         end
0183         res.nn = tag.data;
0184         if size(res.nn,1) ~= res.np
0185             close_file;
0186             error(me,'Vertex normal information is incorrect');
0187         end
0188         
0189         if res.ntri > 0
0190             tag = find_tag(this,FIFF_BEM_SURF_TRIANGLES);
0191             if isempty(tag)
0192                 tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_TRIANGLES);
0193                 if isempty(tag)
0194                     close_file;
0195                     error(me,'Triangulation not found');
0196                 else
0197                     res.tris = tag.data;
0198                 end
0199             else
0200                 res.tris = tag.data;
0201             end
0202             if size(res.tris,1) ~= res.ntri
0203                 close_file;
0204                 error(me,'Triangulation information is incorrect');
0205             end
0206         else
0207             res.tris = [];
0208         end
0209         %
0210         %   Which vertices are active
0211         %
0212         tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NUSE);
0213         if isempty(tag)
0214             res.nuse   = 0;
0215             res.inuse  = int32(zeros(1,res.nuse));
0216             res.vertno = [];
0217         else
0218             res.nuse = tag.data;
0219             tag = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_SELECTION);
0220             if isempty(tag)
0221                 close_file;
0222                 error(me,'Source selection information missing');
0223             end
0224             res.inuse  = int32(tag.data');
0225             res.vertno = int32(zeros(1,res.nuse));
0226             if length(res.inuse) ~= res.np
0227                 close_file;
0228                 error(me,'Incorrect number of entries in source space selection');
0229             end
0230             pp = 0;
0231             for p = 1:res.np
0232                 if res.inuse(p)
0233                     pp = pp + 1;
0234                     res.vertno(pp) = p;
0235                 end
0236             end
0237         end
0238         %
0239         %   Use triangulation
0240         %
0241         tag1 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NUSE_TRI);
0242         tag2 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_USE_TRIANGLES);
0243         if isempty(tag1) || isempty(tag2)
0244             res.nuse_tri = int32(0);
0245             res.use_tris = [];
0246         else
0247             res.nuse_tri = tag1.data;
0248             res.use_tris = tag2.data;
0249         end
0250         %
0251         %   Patch-related information
0252         %
0253         tag1 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NEAREST);
0254         tag2 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_NEAREST_DIST);
0255         if isempty(tag1) || isempty(tag2)
0256             res.nearest = [];
0257             res.nearest_dist = [];
0258         else
0259             res.nearest = tag1.data' + 1;
0260             res.nearest_dist = tag2.data';
0261         end
0262         res.pinfo = mne_patch_info(res.nearest);
0263         if ~isempty(res.pinfo)
0264             fprintf(1,'Patch information added...');
0265         end
0266         %
0267         % Distances
0268         %
0269         tag1 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_DIST);
0270         tag2 = find_tag(this,FIFF.FIFF_MNE_SOURCE_SPACE_DIST_LIMIT);
0271         if isempty(tag1) || isempty(tag2)
0272             res.dist = [];
0273             res.dist_limit = [];
0274         else
0275             res.dist       = tag1.data;
0276             res.dist_limit = tag2.data;
0277             %
0278             %  Add the upper triangle
0279             %
0280             res.dist = res.dist + res.dist';
0281         end
0282         return;
0283         
0284         function [tag] = find_tag(node,findkind)
0285             
0286             for p = 1:node.nent
0287                 if node.dir(p).kind == findkind
0288                     tag = fiff_read_tag(fid,node.dir(p).pos);
0289                     return;
0290                 end
0291             end
0292             tag = [];
0293             return;
0294         end
0295         
0296     end
0297 
0298     function complete_source_space_info
0299         %
0300         %   Main triangulation
0301         %
0302         fprintf(1,'\tCompleting triangulation info...');
0303         this.tri_area = zeros(1,this.ntri);
0304         r1 = this.rr(this.tris(:,1),:);
0305         r2 = this.rr(this.tris(:,2),:);
0306         r3 = this.rr(this.tris(:,3),:);
0307         this.tri_cent = (r1+r2+r3)/3.0;
0308         this.tri_nn   = cross((r2-r1),(r3-r1));
0309         for p = 1:this.ntri
0310             size = sqrt(this.tri_nn(p,:)*this.tri_nn(p,:)');
0311             this.tri_area(p) = size/2.0;
0312             this.tri_nn(p,:) = this.tri_nn(p,:)/size;
0313         end
0314         fprintf(1,'[done]\n');
0315         %
0316         %   Selected triangles
0317         %
0318         fprintf(1,'\tCompleting selection triangulation info...');
0319         if this.nuse_tri > 0
0320             r1 = this.rr(this.use_tris(:,1),:);
0321             r2 = this.rr(this.use_tris(:,2),:);
0322             r3 = this.rr(this.use_tris(:,3),:);
0323             this.use_tri_cent = (r1+r2+r3)/3.0;
0324             this.use_tri_nn   = cross((r2-r1),(r3-r1));
0325             for p = 1:this.nuse_tri
0326                 this.use_tri_area(p)   = sqrt(this.use_tri_nn(p,:)*this.use_tri_nn(p,:)')/2.0;
0327             end
0328         end
0329         fprintf(1,'[done]\n');
0330     end
0331 
0332     function close_file
0333         
0334         if open_here
0335             fclose(fid);
0336         end
0337         
0338     end
0339 
0340 
0341 end

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