0001 function [cov] = mne_read_cov(fid,node,cov_kind)
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 me='MNE:mne_read_cov';
0038
0039 global FIFF;
0040 if isempty(FIFF)
0041 FIFF = fiff_define_constants();
0042 end
0043
0044
0045
0046 covs = fiff_dir_tree_find(node,FIFF.FIFFB_MNE_COV);
0047 if isempty(covs)
0048 error(me,'No covariance matrices found');
0049 end
0050
0051
0052
0053 for p = 1:length(covs)
0054 tag = find_tag(covs(p),FIFF.FIFF_MNE_COV_KIND);
0055 if ~isempty(tag) && tag.data == cov_kind
0056 this = covs(p);
0057
0058
0059
0060 tag = find_tag(this,FIFF.FIFF_MNE_COV_DIM);
0061 if isempty(tag)
0062 error(me,'Covariance matrix dimension not found');
0063 end
0064 dim = tag.data;
0065 tag = find_tag(this,FIFF.FIFF_MNE_COV_NFREE);
0066 if isempty(tag)
0067 nfree = -1;
0068 else
0069 nfree = tag.data;
0070 end
0071 tag = find_tag(this,FIFF.FIFF_MNE_ROW_NAMES);
0072 if isempty(tag)
0073 names = [];
0074 else
0075 names = fiff_split_name_list(tag.data);
0076 if size(names,2) ~= dim
0077 error(me,'Number of names does not match covariance matrix dimension');
0078 end
0079 end
0080 tag = find_tag(this,FIFF.FIFF_MNE_COV);
0081 if isempty(tag)
0082 tag = find_tag(this,FIFF.FIFF_MNE_COV_DIAG);
0083 if isempty(tag)
0084 error(me,'No covariance matrix data found');
0085 else
0086
0087
0088
0089 data = tag.data;
0090 diagmat = true;
0091 fprintf('\t%d x %d diagonal covariance (kind = %d) found.\n',dim,dim,cov_kind);
0092 end
0093 else
0094 if ~issparse(tag.data)
0095
0096
0097
0098 vals = tag.data;
0099 data = zeros(dim,dim);
0100
0101 q = 1;
0102 for j = 1:dim
0103 for k = 1:j
0104 data(j,k) = vals(q);
0105 q = q + 1;
0106 end
0107 end
0108 for j = 1:dim
0109 for k = j+1:dim
0110 data(j,k) = data(k,j);
0111 end
0112 end
0113 diagmat = false;
0114 fprintf('\t%d x %d full covariance (kind = %d) found.\n',dim,dim,cov_kind);
0115 else
0116 diagmat = false;
0117 data = tag.data;
0118 fprintf('\t%d x %d sparse covariance (kind = %d) found.\n',dim,dim,cov_kind);
0119 end
0120 end
0121
0122
0123
0124 tag1 = find_tag(this,FIFF.FIFF_MNE_COV_EIGENVALUES);
0125 tag2 = find_tag(this,FIFF.FIFF_MNE_COV_EIGENVECTORS);
0126 if ~isempty(tag1) && ~isempty(tag2)
0127 eig = tag1.data;
0128 eigvec = tag2.data;
0129 else
0130 eig = [];
0131 eigvec = [];
0132 end
0133
0134
0135
0136 projs = fiff_read_proj(fid,this);
0137
0138
0139
0140 bads = fiff_read_bad_channels(fid,this);
0141
0142
0143
0144 cov.kind = cov_kind;
0145 cov.diag = diagmat;
0146 cov.dim = dim;
0147 cov.names = names;
0148 cov.data = data;
0149 cov.projs = projs;
0150 cov.bads = bads;
0151 cov.nfree = nfree;
0152 cov.eig = eig;
0153 cov.eigvec = eigvec;
0154
0155 return;
0156 end
0157 end
0158
0159 error(me,'Did not find the desired covariance matrix');
0160
0161 return;
0162
0163 function [tag] = find_tag(node,findkind)
0164
0165 for pp = 1:node.nent
0166 kind = node.dir(pp).kind;
0167 pos = node.dir(pp).pos;
0168 if kind == findkind
0169 tag = fiff_read_tag(fid,pos);
0170 return;
0171 end
0172 end
0173 tag = [];
0174 return
0175 end
0176
0177 end