#NEWLINE# @ verbose #NEWLINE# def _read_cov( fid , node , cov_kind , limited = False , verbose = None ) : #NEWLINE# #INDENT# 'Read a noise covariance matrix.' #NEWLINE# covs = dir_tree_find ( node , FIFF . FIFFB_MNE_COV ) #NEWLINE# if ( len ( covs ) == 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'No covariance matrices found' ) #NEWLINE# #UNINDENT# for p in range ( len ( covs ) ) : #NEWLINE# #INDENT# tag = find_tag ( fid , covs [ p ] , FIFF . FIFF_MNE_COV_KIND ) #NEWLINE# if ( ( tag is not None ) and ( int ( tag . data ) == cov_kind ) ) : #NEWLINE# #INDENT# this = covs [ p ] #NEWLINE# tag = find_tag ( fid , this , FIFF . FIFF_MNE_COV_DIM ) #NEWLINE# if ( tag is None ) : #NEWLINE# #INDENT# raise ValueError ( 'Covariance matrix dimension not found' ) #NEWLINE# #UNINDENT# dim = int ( tag . data ) #NEWLINE# tag = find_tag ( fid , this , FIFF . FIFF_MNE_COV_NFREE ) #NEWLINE# if ( tag is None ) : #NEWLINE# #INDENT# nfree = ( - 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# nfree = int ( tag . data ) #NEWLINE# #UNINDENT# tag = find_tag ( fid , this , FIFF . FIFF_MNE_COV_METHOD ) #NEWLINE# if ( tag is None ) : #NEWLINE# #INDENT# method = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# method = tag . data #NEWLINE# #UNINDENT# tag = find_tag ( fid , this , FIFF . FIFF_MNE_COV_SCORE ) #NEWLINE# if ( tag is None ) : #NEWLINE# #INDENT# score = None #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# score = tag . data [ 0 ] #NEWLINE# #UNINDENT# tag = find_tag ( fid , this , FIFF . FIFF_MNE_ROW_NAMES ) #NEWLINE# if ( tag is None ) : #NEWLINE# #INDENT# names = [ ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# names = tag . data . split ( ':' ) #NEWLINE# if ( len ( names ) != dim ) : #NEWLINE# #INDENT# raise ValueError ( 'Number of names does not match covariance matrix dimension' ) #NEWLINE# #UNINDENT# #UNINDENT# tag = find_tag ( fid , this , FIFF . FIFF_MNE_COV ) #NEWLINE# if ( tag is None ) : #NEWLINE# #INDENT# tag = find_tag ( fid , this , FIFF . FIFF_MNE_COV_DIAG ) #NEWLINE# if ( tag is None ) : #NEWLINE# #INDENT# raise ValueError ( 'No covariance matrix data found' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# data = tag . data #NEWLINE# diag = True #NEWLINE# logger . info ( ( '    %d x %d diagonal covariance (kind = %d) found.' % ( dim , dim , cov_kind ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# from scipy import sparse #NEWLINE# if ( not sparse . issparse ( tag . data ) ) : #NEWLINE# #INDENT# vals = tag . data #NEWLINE# data = np . zeros ( ( dim , dim ) ) #NEWLINE# data [ ( np . tril ( np . ones ( ( dim , dim ) ) ) > 0 ) ] = vals #NEWLINE# data = ( data + data . T ) #NEWLINE# data . flat [ : : ( dim + 1 ) ] /= 2.0 #NEWLINE# diag = False #NEWLINE# logger . info ( ( '    %d x %d full covariance (kind = %d) found.' % ( dim , dim , cov_kind ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# diag = False #NEWLINE# data = tag . data #NEWLINE# logger . info ( ( '    %d x %d sparse covariance (kind = %d) found.' % ( dim , dim , cov_kind ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# tag1 = find_tag ( fid , this , FIFF . FIFF_MNE_COV_EIGENVALUES ) #NEWLINE# tag2 = find_tag ( fid , this , FIFF . FIFF_MNE_COV_EIGENVECTORS ) #NEWLINE# if ( ( tag1 is not None ) and ( tag2 is not None ) ) : #NEWLINE# #INDENT# eig = tag1 . data #NEWLINE# eigvec = fid . data #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# eig = None #NEWLINE# eigvec = None #NEWLINE# #UNINDENT# projs = _read_proj ( fid , this ) #NEWLINE# bads = read_bad_channels ( fid , this ) #NEWLINE# assert ( dim == len ( data ) ) #NEWLINE# assert ( data . ndim == ( 1 if diag else 2 ) ) #NEWLINE# cov = dict ( kind = cov_kind , diag = diag , dim = dim , names = names , data = data , projs = projs , bads = bads , nfree = nfree , eig = eig , eigvec = eigvec ) #NEWLINE# if ( score is not None ) : #NEWLINE# #INDENT# cov [ 'loglik' ] = score #NEWLINE# #UNINDENT# if ( method is not None ) : #NEWLINE# #INDENT# cov [ 'method' ] = method #NEWLINE# #UNINDENT# if limited : #NEWLINE# #INDENT# del cov [ 'kind' ] , cov [ 'dim' ] , cov [ 'diag' ] #NEWLINE# #UNINDENT# return cov #NEWLINE# #UNINDENT# #UNINDENT# logger . info ( ( '    Did not find the desired covariance matrix (kind = %d)' % cov_kind ) ) #NEWLINE# return None